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...) \
162 MemoryRegionSection mrs = section_from_flat_range(fr, as); \
163 MEMORY_LISTENER_CALL(callback, dir, &mrs, ##_args); \
166 struct CoalescedMemoryRange
{
168 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
171 struct MemoryRegionIoeventfd
{
178 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
179 MemoryRegionIoeventfd b
)
181 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
183 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
185 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
187 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
189 } else if (a
.match_data
< b
.match_data
) {
191 } else if (a
.match_data
> b
.match_data
) {
193 } else if (a
.match_data
) {
194 if (a
.data
< b
.data
) {
196 } else if (a
.data
> b
.data
) {
202 } else if (a
.e
> b
.e
) {
208 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
209 MemoryRegionIoeventfd b
)
211 return !memory_region_ioeventfd_before(a
, b
)
212 && !memory_region_ioeventfd_before(b
, a
);
215 typedef struct FlatRange FlatRange
;
216 typedef struct FlatView FlatView
;
218 /* Range of memory in the global map. Addresses are absolute. */
221 hwaddr offset_in_region
;
223 uint8_t dirty_log_mask
;
228 /* Flattened global view of current active memory hierarchy. Kept in sorted
236 unsigned nr_allocated
;
239 typedef struct AddressSpaceOps AddressSpaceOps
;
241 #define FOR_EACH_FLAT_RANGE(var, view) \
242 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
244 static inline MemoryRegionSection
245 section_from_flat_range(FlatRange
*fr
, AddressSpace
*as
)
247 return (MemoryRegionSection
) {
250 .offset_within_region
= fr
->offset_in_region
,
251 .size
= fr
->addr
.size
,
252 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
253 .readonly
= fr
->readonly
,
257 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
259 return a
->mr
== b
->mr
260 && addrrange_equal(a
->addr
, b
->addr
)
261 && a
->offset_in_region
== b
->offset_in_region
262 && a
->romd_mode
== b
->romd_mode
263 && a
->readonly
== b
->readonly
;
266 static void flatview_init(FlatView
*view
)
271 view
->nr_allocated
= 0;
274 /* Insert a range into a given position. Caller is responsible for maintaining
277 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
279 if (view
->nr
== view
->nr_allocated
) {
280 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
281 view
->ranges
= g_realloc(view
->ranges
,
282 view
->nr_allocated
* sizeof(*view
->ranges
));
284 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
285 (view
->nr
- pos
) * sizeof(FlatRange
));
286 view
->ranges
[pos
] = *range
;
287 memory_region_ref(range
->mr
);
291 static void flatview_destroy(FlatView
*view
)
295 for (i
= 0; i
< view
->nr
; i
++) {
296 memory_region_unref(view
->ranges
[i
].mr
);
298 g_free(view
->ranges
);
302 static void flatview_ref(FlatView
*view
)
304 atomic_inc(&view
->ref
);
307 static void flatview_unref(FlatView
*view
)
309 if (atomic_fetch_dec(&view
->ref
) == 1) {
310 flatview_destroy(view
);
314 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
316 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
318 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
320 int128_make64(r2
->offset_in_region
))
321 && r1
->dirty_log_mask
== r2
->dirty_log_mask
322 && r1
->romd_mode
== r2
->romd_mode
323 && r1
->readonly
== r2
->readonly
;
326 /* Attempt to simplify a view by merging adjacent ranges */
327 static void flatview_simplify(FlatView
*view
)
332 while (i
< view
->nr
) {
335 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
336 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
340 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
341 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
346 static bool memory_region_big_endian(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 bool memory_region_wrong_endianness(MemoryRegion
*mr
)
357 #ifdef TARGET_WORDS_BIGENDIAN
358 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
360 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
364 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
366 if (memory_region_wrong_endianness(mr
)) {
371 *data
= bswap16(*data
);
374 *data
= bswap32(*data
);
377 *data
= bswap64(*data
);
385 static hwaddr
memory_region_to_absolute_addr(MemoryRegion
*mr
, hwaddr offset
)
388 hwaddr abs_addr
= offset
;
390 abs_addr
+= mr
->addr
;
391 for (root
= mr
; root
->container
; ) {
392 root
= root
->container
;
393 abs_addr
+= root
->addr
;
399 static int get_cpu_index(void)
402 return current_cpu
->cpu_index
;
407 static MemTxResult
memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
417 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
419 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
420 } else if (mr
== &io_mem_notdirty
) {
421 /* Accesses to code which has previously been translated into a TB show
422 * up in the MMIO path, as accesses to the io_mem_notdirty
424 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
425 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
426 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
427 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
429 *value
|= (tmp
& mask
) << shift
;
433 static MemTxResult
memory_region_read_accessor(MemoryRegion
*mr
,
443 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
445 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
446 } else if (mr
== &io_mem_notdirty
) {
447 /* Accesses to code which has previously been translated into a TB show
448 * up in the MMIO path, as accesses to the io_mem_notdirty
450 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
451 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
452 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
453 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
455 *value
|= (tmp
& mask
) << shift
;
459 static MemTxResult
memory_region_read_with_attrs_accessor(MemoryRegion
*mr
,
470 r
= mr
->ops
->read_with_attrs(mr
->opaque
, addr
, &tmp
, size
, attrs
);
472 trace_memory_region_subpage_read(get_cpu_index(), mr
, addr
, tmp
, size
);
473 } else if (mr
== &io_mem_notdirty
) {
474 /* Accesses to code which has previously been translated into a TB show
475 * up in the MMIO path, as accesses to the io_mem_notdirty
477 trace_memory_region_tb_read(get_cpu_index(), addr
, tmp
, size
);
478 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED
) {
479 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
480 trace_memory_region_ops_read(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
482 *value
|= (tmp
& mask
) << shift
;
486 static MemTxResult
memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
496 tmp
= (*value
>> shift
) & mask
;
498 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
499 } else if (mr
== &io_mem_notdirty
) {
500 /* Accesses to code which has previously been translated into a TB show
501 * up in the MMIO path, as accesses to the io_mem_notdirty
503 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
504 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
505 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
506 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
508 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
512 static MemTxResult
memory_region_write_accessor(MemoryRegion
*mr
,
522 tmp
= (*value
>> shift
) & mask
;
524 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
525 } else if (mr
== &io_mem_notdirty
) {
526 /* Accesses to code which has previously been translated into a TB show
527 * up in the MMIO path, as accesses to the io_mem_notdirty
529 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
530 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
531 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
532 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
534 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
538 static MemTxResult
memory_region_write_with_attrs_accessor(MemoryRegion
*mr
,
548 tmp
= (*value
>> shift
) & mask
;
550 trace_memory_region_subpage_write(get_cpu_index(), mr
, addr
, tmp
, size
);
551 } else if (mr
== &io_mem_notdirty
) {
552 /* Accesses to code which has previously been translated into a TB show
553 * up in the MMIO path, as accesses to the io_mem_notdirty
555 trace_memory_region_tb_write(get_cpu_index(), addr
, tmp
, size
);
556 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED
) {
557 hwaddr abs_addr
= memory_region_to_absolute_addr(mr
, addr
);
558 trace_memory_region_ops_write(get_cpu_index(), mr
, abs_addr
, tmp
, size
);
560 return mr
->ops
->write_with_attrs(mr
->opaque
, addr
, tmp
, size
, attrs
);
563 static MemTxResult
access_with_adjusted_size(hwaddr addr
,
566 unsigned access_size_min
,
567 unsigned access_size_max
,
568 MemTxResult (*access
)(MemoryRegion
*mr
,
578 uint64_t access_mask
;
579 unsigned access_size
;
581 MemTxResult r
= MEMTX_OK
;
583 if (!access_size_min
) {
586 if (!access_size_max
) {
590 /* FIXME: support unaligned access? */
591 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
592 access_mask
= -1ULL >> (64 - access_size
* 8);
593 if (memory_region_big_endian(mr
)) {
594 for (i
= 0; i
< size
; i
+= access_size
) {
595 r
|= access(mr
, addr
+ i
, value
, access_size
,
596 (size
- access_size
- i
) * 8, access_mask
, attrs
);
599 for (i
= 0; i
< size
; i
+= access_size
) {
600 r
|= access(mr
, addr
+ i
, value
, access_size
, i
* 8,
607 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
611 while (mr
->container
) {
614 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
615 if (mr
== as
->root
) {
622 /* Render a memory region into the global view. Ranges in @view obscure
625 static void render_memory_region(FlatView
*view
,
631 MemoryRegion
*subregion
;
633 hwaddr offset_in_region
;
643 int128_addto(&base
, int128_make64(mr
->addr
));
644 readonly
|= mr
->readonly
;
646 tmp
= addrrange_make(base
, mr
->size
);
648 if (!addrrange_intersects(tmp
, clip
)) {
652 clip
= addrrange_intersection(tmp
, clip
);
655 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
656 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
657 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
661 /* Render subregions in priority order. */
662 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
663 render_memory_region(view
, subregion
, base
, clip
, readonly
);
666 if (!mr
->terminates
) {
670 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
675 fr
.dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
676 fr
.romd_mode
= mr
->romd_mode
;
677 fr
.readonly
= readonly
;
679 /* Render the region itself into any gaps left by the current view. */
680 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
681 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
684 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
685 now
= int128_min(remain
,
686 int128_sub(view
->ranges
[i
].addr
.start
, base
));
687 fr
.offset_in_region
= offset_in_region
;
688 fr
.addr
= addrrange_make(base
, now
);
689 flatview_insert(view
, i
, &fr
);
691 int128_addto(&base
, now
);
692 offset_in_region
+= int128_get64(now
);
693 int128_subfrom(&remain
, now
);
695 now
= int128_sub(int128_min(int128_add(base
, remain
),
696 addrrange_end(view
->ranges
[i
].addr
)),
698 int128_addto(&base
, now
);
699 offset_in_region
+= int128_get64(now
);
700 int128_subfrom(&remain
, now
);
702 if (int128_nz(remain
)) {
703 fr
.offset_in_region
= offset_in_region
;
704 fr
.addr
= addrrange_make(base
, remain
);
705 flatview_insert(view
, i
, &fr
);
709 /* Render a memory topology into a list of disjoint absolute ranges. */
710 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
714 view
= g_new(FlatView
, 1);
718 render_memory_region(view
, mr
, int128_zero(),
719 addrrange_make(int128_zero(), int128_2_64()), false);
721 flatview_simplify(view
);
726 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
727 MemoryRegionIoeventfd
*fds_new
,
729 MemoryRegionIoeventfd
*fds_old
,
733 MemoryRegionIoeventfd
*fd
;
734 MemoryRegionSection section
;
736 /* Generate a symmetric difference of the old and new fd sets, adding
737 * and deleting as necessary.
741 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
742 if (iold
< fds_old_nb
743 && (inew
== fds_new_nb
744 || memory_region_ioeventfd_before(fds_old
[iold
],
747 section
= (MemoryRegionSection
) {
749 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
750 .size
= fd
->addr
.size
,
752 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
753 fd
->match_data
, fd
->data
, fd
->e
);
755 } else if (inew
< fds_new_nb
756 && (iold
== fds_old_nb
757 || memory_region_ioeventfd_before(fds_new
[inew
],
760 section
= (MemoryRegionSection
) {
762 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
763 .size
= fd
->addr
.size
,
765 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
766 fd
->match_data
, fd
->data
, fd
->e
);
775 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
780 view
= atomic_rcu_read(&as
->current_map
);
786 static void address_space_update_ioeventfds(AddressSpace
*as
)
790 unsigned ioeventfd_nb
= 0;
791 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
795 view
= address_space_get_flatview(as
);
796 FOR_EACH_FLAT_RANGE(fr
, view
) {
797 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
798 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
799 int128_sub(fr
->addr
.start
,
800 int128_make64(fr
->offset_in_region
)));
801 if (addrrange_intersects(fr
->addr
, tmp
)) {
803 ioeventfds
= g_realloc(ioeventfds
,
804 ioeventfd_nb
* sizeof(*ioeventfds
));
805 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
806 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
811 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
812 as
->ioeventfds
, as
->ioeventfd_nb
);
814 g_free(as
->ioeventfds
);
815 as
->ioeventfds
= ioeventfds
;
816 as
->ioeventfd_nb
= ioeventfd_nb
;
817 flatview_unref(view
);
820 static void address_space_update_topology_pass(AddressSpace
*as
,
821 const FlatView
*old_view
,
822 const FlatView
*new_view
,
826 FlatRange
*frold
, *frnew
;
828 /* Generate a symmetric difference of the old and new memory maps.
829 * Kill ranges in the old map, and instantiate ranges in the new map.
832 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
833 if (iold
< old_view
->nr
) {
834 frold
= &old_view
->ranges
[iold
];
838 if (inew
< new_view
->nr
) {
839 frnew
= &new_view
->ranges
[inew
];
846 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
847 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
848 && !flatrange_equal(frold
, frnew
)))) {
849 /* In old but not in new, or in both but attributes changed. */
852 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
856 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
857 /* In both and unchanged (except logging may have changed) */
860 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
861 if (frnew
->dirty_log_mask
& ~frold
->dirty_log_mask
) {
862 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
,
863 frold
->dirty_log_mask
,
864 frnew
->dirty_log_mask
);
866 if (frold
->dirty_log_mask
& ~frnew
->dirty_log_mask
) {
867 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
,
868 frold
->dirty_log_mask
,
869 frnew
->dirty_log_mask
);
879 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
888 static void address_space_update_topology(AddressSpace
*as
)
890 FlatView
*old_view
= address_space_get_flatview(as
);
891 FlatView
*new_view
= generate_memory_topology(as
->root
);
893 address_space_update_topology_pass(as
, old_view
, new_view
, false);
894 address_space_update_topology_pass(as
, old_view
, new_view
, true);
896 /* Writes are protected by the BQL. */
897 atomic_rcu_set(&as
->current_map
, new_view
);
898 call_rcu(old_view
, flatview_unref
, rcu
);
900 /* Note that all the old MemoryRegions are still alive up to this
901 * point. This relieves most MemoryListeners from the need to
902 * ref/unref the MemoryRegions they get---unless they use them
903 * outside the iothread mutex, in which case precise reference
904 * counting is necessary.
906 flatview_unref(old_view
);
908 address_space_update_ioeventfds(as
);
911 void memory_region_transaction_begin(void)
913 qemu_flush_coalesced_mmio_buffer();
914 ++memory_region_transaction_depth
;
917 static void memory_region_clear_pending(void)
919 memory_region_update_pending
= false;
920 ioeventfd_update_pending
= false;
923 void memory_region_transaction_commit(void)
927 assert(memory_region_transaction_depth
);
928 --memory_region_transaction_depth
;
929 if (!memory_region_transaction_depth
) {
930 if (memory_region_update_pending
) {
931 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
933 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
934 address_space_update_topology(as
);
937 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
938 } else if (ioeventfd_update_pending
) {
939 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
940 address_space_update_ioeventfds(as
);
943 memory_region_clear_pending();
947 static void memory_region_destructor_none(MemoryRegion
*mr
)
951 static void memory_region_destructor_ram(MemoryRegion
*mr
)
953 qemu_ram_free(mr
->ram_block
);
956 static bool memory_region_need_escape(char c
)
958 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
961 static char *memory_region_escape_name(const char *name
)
968 for (p
= name
; *p
; p
++) {
969 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
971 if (bytes
== p
- name
) {
972 return g_memdup(name
, bytes
+ 1);
975 escaped
= g_malloc(bytes
+ 1);
976 for (p
= name
, q
= escaped
; *p
; p
++) {
978 if (unlikely(memory_region_need_escape(c
))) {
981 *q
++ = "0123456789abcdef"[c
>> 4];
982 c
= "0123456789abcdef"[c
& 15];
990 void memory_region_init(MemoryRegion
*mr
,
995 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
996 mr
->size
= int128_make64(size
);
997 if (size
== UINT64_MAX
) {
998 mr
->size
= int128_2_64();
1000 mr
->name
= g_strdup(name
);
1002 mr
->ram_block
= NULL
;
1005 char *escaped_name
= memory_region_escape_name(name
);
1006 char *name_array
= g_strdup_printf("%s[*]", escaped_name
);
1009 owner
= container_get(qdev_get_machine(), "/unattached");
1012 object_property_add_child(owner
, name_array
, OBJECT(mr
), &error_abort
);
1013 object_unref(OBJECT(mr
));
1015 g_free(escaped_name
);
1019 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, const char *name
,
1020 void *opaque
, Error
**errp
)
1022 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1023 uint64_t value
= mr
->addr
;
1025 visit_type_uint64(v
, name
, &value
, errp
);
1028 static void memory_region_get_container(Object
*obj
, Visitor
*v
,
1029 const char *name
, void *opaque
,
1032 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1033 gchar
*path
= (gchar
*)"";
1035 if (mr
->container
) {
1036 path
= object_get_canonical_path(OBJECT(mr
->container
));
1038 visit_type_str(v
, name
, &path
, errp
);
1039 if (mr
->container
) {
1044 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
1047 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1049 return OBJECT(mr
->container
);
1052 static void memory_region_get_priority(Object
*obj
, Visitor
*v
,
1053 const char *name
, void *opaque
,
1056 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1057 int32_t value
= mr
->priority
;
1059 visit_type_int32(v
, name
, &value
, errp
);
1062 static void memory_region_get_size(Object
*obj
, Visitor
*v
, const char *name
,
1063 void *opaque
, Error
**errp
)
1065 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1066 uint64_t value
= memory_region_size(mr
);
1068 visit_type_uint64(v
, name
, &value
, errp
);
1071 static void memory_region_initfn(Object
*obj
)
1073 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1076 mr
->ops
= &unassigned_mem_ops
;
1078 mr
->romd_mode
= true;
1079 mr
->global_locking
= true;
1080 mr
->destructor
= memory_region_destructor_none
;
1081 QTAILQ_INIT(&mr
->subregions
);
1082 QTAILQ_INIT(&mr
->coalesced
);
1084 op
= object_property_add(OBJECT(mr
), "container",
1085 "link<" TYPE_MEMORY_REGION
">",
1086 memory_region_get_container
,
1087 NULL
, /* memory_region_set_container */
1088 NULL
, NULL
, &error_abort
);
1089 op
->resolve
= memory_region_resolve_container
;
1091 object_property_add(OBJECT(mr
), "addr", "uint64",
1092 memory_region_get_addr
,
1093 NULL
, /* memory_region_set_addr */
1094 NULL
, NULL
, &error_abort
);
1095 object_property_add(OBJECT(mr
), "priority", "uint32",
1096 memory_region_get_priority
,
1097 NULL
, /* memory_region_set_priority */
1098 NULL
, NULL
, &error_abort
);
1099 object_property_add(OBJECT(mr
), "size", "uint64",
1100 memory_region_get_size
,
1101 NULL
, /* memory_region_set_size, */
1102 NULL
, NULL
, &error_abort
);
1105 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1108 #ifdef DEBUG_UNASSIGNED
1109 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1111 if (current_cpu
!= NULL
) {
1112 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
1117 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1118 uint64_t val
, unsigned size
)
1120 #ifdef DEBUG_UNASSIGNED
1121 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1123 if (current_cpu
!= NULL
) {
1124 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1128 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1129 unsigned size
, bool is_write
)
1134 const MemoryRegionOps unassigned_mem_ops
= {
1135 .valid
.accepts
= unassigned_mem_accepts
,
1136 .endianness
= DEVICE_NATIVE_ENDIAN
,
1139 bool memory_region_access_valid(MemoryRegion
*mr
,
1144 int access_size_min
, access_size_max
;
1147 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1151 if (!mr
->ops
->valid
.accepts
) {
1155 access_size_min
= mr
->ops
->valid
.min_access_size
;
1156 if (!mr
->ops
->valid
.min_access_size
) {
1157 access_size_min
= 1;
1160 access_size_max
= mr
->ops
->valid
.max_access_size
;
1161 if (!mr
->ops
->valid
.max_access_size
) {
1162 access_size_max
= 4;
1165 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1166 for (i
= 0; i
< size
; i
+= access_size
) {
1167 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1176 static MemTxResult
memory_region_dispatch_read1(MemoryRegion
*mr
,
1184 if (mr
->ops
->read
) {
1185 return access_with_adjusted_size(addr
, pval
, size
,
1186 mr
->ops
->impl
.min_access_size
,
1187 mr
->ops
->impl
.max_access_size
,
1188 memory_region_read_accessor
,
1190 } else if (mr
->ops
->read_with_attrs
) {
1191 return access_with_adjusted_size(addr
, pval
, size
,
1192 mr
->ops
->impl
.min_access_size
,
1193 mr
->ops
->impl
.max_access_size
,
1194 memory_region_read_with_attrs_accessor
,
1197 return access_with_adjusted_size(addr
, pval
, size
, 1, 4,
1198 memory_region_oldmmio_read_accessor
,
1203 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1211 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1212 *pval
= unassigned_mem_read(mr
, addr
, size
);
1213 return MEMTX_DECODE_ERROR
;
1216 r
= memory_region_dispatch_read1(mr
, addr
, pval
, size
, attrs
);
1217 adjust_endianness(mr
, pval
, size
);
1221 /* Return true if an eventfd was signalled */
1222 static bool memory_region_dispatch_write_eventfds(MemoryRegion
*mr
,
1228 MemoryRegionIoeventfd ioeventfd
= {
1229 .addr
= addrrange_make(int128_make64(addr
), int128_make64(size
)),
1234 for (i
= 0; i
< mr
->ioeventfd_nb
; i
++) {
1235 ioeventfd
.match_data
= mr
->ioeventfds
[i
].match_data
;
1236 ioeventfd
.e
= mr
->ioeventfds
[i
].e
;
1238 if (memory_region_ioeventfd_equal(ioeventfd
, mr
->ioeventfds
[i
])) {
1239 event_notifier_set(ioeventfd
.e
);
1247 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1253 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1254 unassigned_mem_write(mr
, addr
, data
, size
);
1255 return MEMTX_DECODE_ERROR
;
1258 adjust_endianness(mr
, &data
, size
);
1260 if ((!kvm_eventfds_enabled()) &&
1261 memory_region_dispatch_write_eventfds(mr
, addr
, data
, size
, attrs
)) {
1265 if (mr
->ops
->write
) {
1266 return access_with_adjusted_size(addr
, &data
, size
,
1267 mr
->ops
->impl
.min_access_size
,
1268 mr
->ops
->impl
.max_access_size
,
1269 memory_region_write_accessor
, mr
,
1271 } else if (mr
->ops
->write_with_attrs
) {
1273 access_with_adjusted_size(addr
, &data
, size
,
1274 mr
->ops
->impl
.min_access_size
,
1275 mr
->ops
->impl
.max_access_size
,
1276 memory_region_write_with_attrs_accessor
,
1279 return access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1280 memory_region_oldmmio_write_accessor
,
1285 void memory_region_init_io(MemoryRegion
*mr
,
1287 const MemoryRegionOps
*ops
,
1292 memory_region_init(mr
, owner
, name
, size
);
1293 mr
->ops
= ops
? ops
: &unassigned_mem_ops
;
1294 mr
->opaque
= opaque
;
1295 mr
->terminates
= true;
1298 void memory_region_init_ram(MemoryRegion
*mr
,
1304 memory_region_init(mr
, owner
, name
, size
);
1306 mr
->terminates
= true;
1307 mr
->destructor
= memory_region_destructor_ram
;
1308 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1309 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1312 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1317 void (*resized
)(const char*,
1322 memory_region_init(mr
, owner
, name
, size
);
1324 mr
->terminates
= true;
1325 mr
->destructor
= memory_region_destructor_ram
;
1326 mr
->ram_block
= qemu_ram_alloc_resizeable(size
, max_size
, resized
,
1328 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1332 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1333 struct Object
*owner
,
1340 memory_region_init(mr
, owner
, name
, size
);
1342 mr
->terminates
= true;
1343 mr
->destructor
= memory_region_destructor_ram
;
1344 mr
->ram_block
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1345 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1349 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1355 memory_region_init(mr
, owner
, name
, size
);
1357 mr
->terminates
= true;
1358 mr
->destructor
= memory_region_destructor_ram
;
1359 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1361 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1362 assert(ptr
!= NULL
);
1363 mr
->ram_block
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
, &error_fatal
);
1366 void memory_region_set_skip_dump(MemoryRegion
*mr
)
1368 mr
->skip_dump
= true;
1371 void memory_region_init_alias(MemoryRegion
*mr
,
1378 memory_region_init(mr
, owner
, name
, size
);
1380 mr
->alias_offset
= offset
;
1383 void memory_region_init_rom(MemoryRegion
*mr
,
1384 struct Object
*owner
,
1389 memory_region_init(mr
, owner
, name
, size
);
1391 mr
->readonly
= true;
1392 mr
->terminates
= true;
1393 mr
->destructor
= memory_region_destructor_ram
;
1394 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1395 mr
->dirty_log_mask
= tcg_enabled() ? (1 << DIRTY_MEMORY_CODE
) : 0;
1398 void memory_region_init_rom_device(MemoryRegion
*mr
,
1400 const MemoryRegionOps
*ops
,
1407 memory_region_init(mr
, owner
, name
, size
);
1409 mr
->opaque
= opaque
;
1410 mr
->terminates
= true;
1411 mr
->rom_device
= true;
1412 mr
->destructor
= memory_region_destructor_ram
;
1413 mr
->ram_block
= qemu_ram_alloc(size
, mr
, errp
);
1416 void memory_region_init_iommu(MemoryRegion
*mr
,
1418 const MemoryRegionIOMMUOps
*ops
,
1422 memory_region_init(mr
, owner
, name
, size
);
1423 mr
->iommu_ops
= ops
,
1424 mr
->terminates
= true; /* then re-forwards */
1425 QLIST_INIT(&mr
->iommu_notify
);
1426 mr
->iommu_notify_flags
= IOMMU_NOTIFIER_NONE
;
1429 static void memory_region_finalize(Object
*obj
)
1431 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1433 assert(!mr
->container
);
1435 /* We know the region is not visible in any address space (it
1436 * does not have a container and cannot be a root either because
1437 * it has no references, so we can blindly clear mr->enabled.
1438 * memory_region_set_enabled instead could trigger a transaction
1439 * and cause an infinite loop.
1441 mr
->enabled
= false;
1442 memory_region_transaction_begin();
1443 while (!QTAILQ_EMPTY(&mr
->subregions
)) {
1444 MemoryRegion
*subregion
= QTAILQ_FIRST(&mr
->subregions
);
1445 memory_region_del_subregion(mr
, subregion
);
1447 memory_region_transaction_commit();
1450 memory_region_clear_coalescing(mr
);
1451 g_free((char *)mr
->name
);
1452 g_free(mr
->ioeventfds
);
1455 Object
*memory_region_owner(MemoryRegion
*mr
)
1457 Object
*obj
= OBJECT(mr
);
1461 void memory_region_ref(MemoryRegion
*mr
)
1463 /* MMIO callbacks most likely will access data that belongs
1464 * to the owner, hence the need to ref/unref the owner whenever
1465 * the memory region is in use.
1467 * The memory region is a child of its owner. As long as the
1468 * owner doesn't call unparent itself on the memory region,
1469 * ref-ing the owner will also keep the memory region alive.
1470 * Memory regions without an owner are supposed to never go away;
1471 * we do not ref/unref them because it slows down DMA sensibly.
1473 if (mr
&& mr
->owner
) {
1474 object_ref(mr
->owner
);
1478 void memory_region_unref(MemoryRegion
*mr
)
1480 if (mr
&& mr
->owner
) {
1481 object_unref(mr
->owner
);
1485 uint64_t memory_region_size(MemoryRegion
*mr
)
1487 if (int128_eq(mr
->size
, int128_2_64())) {
1490 return int128_get64(mr
->size
);
1493 const char *memory_region_name(const MemoryRegion
*mr
)
1496 ((MemoryRegion
*)mr
)->name
=
1497 object_get_canonical_path_component(OBJECT(mr
));
1502 bool memory_region_is_skip_dump(MemoryRegion
*mr
)
1504 return mr
->skip_dump
;
1507 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
)
1509 uint8_t mask
= mr
->dirty_log_mask
;
1510 if (global_dirty_log
) {
1511 mask
|= (1 << DIRTY_MEMORY_MIGRATION
);
1516 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
)
1518 return memory_region_get_dirty_log_mask(mr
) & (1 << client
);
1521 static void memory_region_update_iommu_notify_flags(MemoryRegion
*mr
)
1523 IOMMUNotifierFlag flags
= IOMMU_NOTIFIER_NONE
;
1524 IOMMUNotifier
*iommu_notifier
;
1526 QLIST_FOREACH(iommu_notifier
, &mr
->iommu_notify
, node
) {
1527 flags
|= iommu_notifier
->notifier_flags
;
1530 if (flags
!= mr
->iommu_notify_flags
&&
1531 mr
->iommu_ops
->notify_flag_changed
) {
1532 mr
->iommu_ops
->notify_flag_changed(mr
, mr
->iommu_notify_flags
,
1536 mr
->iommu_notify_flags
= flags
;
1539 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1542 /* We need to register for at least one bitfield */
1543 assert(n
->notifier_flags
!= IOMMU_NOTIFIER_NONE
);
1544 QLIST_INSERT_HEAD(&mr
->iommu_notify
, n
, node
);
1545 memory_region_update_iommu_notify_flags(mr
);
1548 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion
*mr
)
1550 assert(memory_region_is_iommu(mr
));
1551 if (mr
->iommu_ops
&& mr
->iommu_ops
->get_min_page_size
) {
1552 return mr
->iommu_ops
->get_min_page_size(mr
);
1554 return TARGET_PAGE_SIZE
;
1557 void memory_region_iommu_replay(MemoryRegion
*mr
, IOMMUNotifier
*n
,
1560 hwaddr addr
, granularity
;
1561 IOMMUTLBEntry iotlb
;
1563 granularity
= memory_region_iommu_get_min_page_size(mr
);
1565 for (addr
= 0; addr
< memory_region_size(mr
); addr
+= granularity
) {
1566 iotlb
= mr
->iommu_ops
->translate(mr
, addr
, is_write
);
1567 if (iotlb
.perm
!= IOMMU_NONE
) {
1568 n
->notify(n
, &iotlb
);
1571 /* if (2^64 - MR size) < granularity, it's possible to get an
1572 * infinite loop here. This should catch such a wraparound */
1573 if ((addr
+ granularity
) < addr
) {
1579 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1582 QLIST_REMOVE(n
, node
);
1583 memory_region_update_iommu_notify_flags(mr
);
1586 void memory_region_notify_iommu(MemoryRegion
*mr
,
1587 IOMMUTLBEntry entry
)
1589 IOMMUNotifier
*iommu_notifier
;
1590 IOMMUNotifierFlag request_flags
;
1592 assert(memory_region_is_iommu(mr
));
1594 if (entry
.perm
& IOMMU_RW
) {
1595 request_flags
= IOMMU_NOTIFIER_MAP
;
1597 request_flags
= IOMMU_NOTIFIER_UNMAP
;
1600 QLIST_FOREACH(iommu_notifier
, &mr
->iommu_notify
, node
) {
1601 if (iommu_notifier
->notifier_flags
& request_flags
) {
1602 iommu_notifier
->notify(iommu_notifier
, &entry
);
1607 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1609 uint8_t mask
= 1 << client
;
1610 uint8_t old_logging
;
1612 assert(client
== DIRTY_MEMORY_VGA
);
1613 old_logging
= mr
->vga_logging_count
;
1614 mr
->vga_logging_count
+= log
? 1 : -1;
1615 if (!!old_logging
== !!mr
->vga_logging_count
) {
1619 memory_region_transaction_begin();
1620 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1621 memory_region_update_pending
|= mr
->enabled
;
1622 memory_region_transaction_commit();
1625 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1626 hwaddr size
, unsigned client
)
1628 assert(mr
->ram_block
);
1629 return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr
) + addr
,
1633 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1636 assert(mr
->ram_block
);
1637 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr
) + addr
,
1639 memory_region_get_dirty_log_mask(mr
));
1642 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1643 hwaddr size
, unsigned client
)
1645 assert(mr
->ram_block
);
1646 return cpu_physical_memory_test_and_clear_dirty(
1647 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1651 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1656 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1657 FlatView
*view
= address_space_get_flatview(as
);
1658 FOR_EACH_FLAT_RANGE(fr
, view
) {
1660 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1663 flatview_unref(view
);
1667 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1669 if (mr
->readonly
!= readonly
) {
1670 memory_region_transaction_begin();
1671 mr
->readonly
= readonly
;
1672 memory_region_update_pending
|= mr
->enabled
;
1673 memory_region_transaction_commit();
1677 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1679 if (mr
->romd_mode
!= romd_mode
) {
1680 memory_region_transaction_begin();
1681 mr
->romd_mode
= romd_mode
;
1682 memory_region_update_pending
|= mr
->enabled
;
1683 memory_region_transaction_commit();
1687 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1688 hwaddr size
, unsigned client
)
1690 assert(mr
->ram_block
);
1691 cpu_physical_memory_test_and_clear_dirty(
1692 memory_region_get_ram_addr(mr
) + addr
, size
, client
);
1695 int memory_region_get_fd(MemoryRegion
*mr
)
1703 fd
= mr
->ram_block
->fd
;
1709 void memory_region_set_fd(MemoryRegion
*mr
, int fd
)
1715 mr
->ram_block
->fd
= fd
;
1719 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1722 uint64_t offset
= 0;
1726 offset
+= mr
->alias_offset
;
1729 assert(mr
->ram_block
);
1730 ptr
= qemu_map_ram_ptr(mr
->ram_block
, offset
);
1736 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
)
1740 block
= qemu_ram_block_from_host(ptr
, false, offset
);
1748 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1750 return mr
->ram_block
? mr
->ram_block
->offset
: RAM_ADDR_INVALID
;
1753 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
, Error
**errp
)
1755 assert(mr
->ram_block
);
1757 qemu_ram_resize(mr
->ram_block
, newsize
, errp
);
1760 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1764 CoalescedMemoryRange
*cmr
;
1766 MemoryRegionSection section
;
1768 view
= address_space_get_flatview(as
);
1769 FOR_EACH_FLAT_RANGE(fr
, view
) {
1771 section
= (MemoryRegionSection
) {
1772 .address_space
= as
,
1773 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1774 .size
= fr
->addr
.size
,
1777 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1778 int128_get64(fr
->addr
.start
),
1779 int128_get64(fr
->addr
.size
));
1780 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1781 tmp
= addrrange_shift(cmr
->addr
,
1782 int128_sub(fr
->addr
.start
,
1783 int128_make64(fr
->offset_in_region
)));
1784 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1787 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1788 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1789 int128_get64(tmp
.start
),
1790 int128_get64(tmp
.size
));
1794 flatview_unref(view
);
1797 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1801 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1802 memory_region_update_coalesced_range_as(mr
, as
);
1806 void memory_region_set_coalescing(MemoryRegion
*mr
)
1808 memory_region_clear_coalescing(mr
);
1809 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1812 void memory_region_add_coalescing(MemoryRegion
*mr
,
1816 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1818 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1819 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1820 memory_region_update_coalesced_range(mr
);
1821 memory_region_set_flush_coalesced(mr
);
1824 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1826 CoalescedMemoryRange
*cmr
;
1827 bool updated
= false;
1829 qemu_flush_coalesced_mmio_buffer();
1830 mr
->flush_coalesced_mmio
= false;
1832 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1833 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1834 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1840 memory_region_update_coalesced_range(mr
);
1844 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1846 mr
->flush_coalesced_mmio
= true;
1849 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1851 qemu_flush_coalesced_mmio_buffer();
1852 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1853 mr
->flush_coalesced_mmio
= false;
1857 void memory_region_set_global_locking(MemoryRegion
*mr
)
1859 mr
->global_locking
= true;
1862 void memory_region_clear_global_locking(MemoryRegion
*mr
)
1864 mr
->global_locking
= false;
1867 static bool userspace_eventfd_warning
;
1869 void memory_region_add_eventfd(MemoryRegion
*mr
,
1876 MemoryRegionIoeventfd mrfd
= {
1877 .addr
.start
= int128_make64(addr
),
1878 .addr
.size
= int128_make64(size
),
1879 .match_data
= match_data
,
1885 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
1886 userspace_eventfd_warning
))) {
1887 userspace_eventfd_warning
= true;
1888 error_report("Using eventfd without MMIO binding in KVM. "
1889 "Suboptimal performance expected");
1893 adjust_endianness(mr
, &mrfd
.data
, size
);
1895 memory_region_transaction_begin();
1896 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1897 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1902 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1903 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1904 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1905 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1906 mr
->ioeventfds
[i
] = mrfd
;
1907 ioeventfd_update_pending
|= mr
->enabled
;
1908 memory_region_transaction_commit();
1911 void memory_region_del_eventfd(MemoryRegion
*mr
,
1918 MemoryRegionIoeventfd mrfd
= {
1919 .addr
.start
= int128_make64(addr
),
1920 .addr
.size
= int128_make64(size
),
1921 .match_data
= match_data
,
1928 adjust_endianness(mr
, &mrfd
.data
, size
);
1930 memory_region_transaction_begin();
1931 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1932 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1936 assert(i
!= mr
->ioeventfd_nb
);
1937 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1938 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1940 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1941 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1942 ioeventfd_update_pending
|= mr
->enabled
;
1943 memory_region_transaction_commit();
1946 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
1948 MemoryRegion
*mr
= subregion
->container
;
1949 MemoryRegion
*other
;
1951 memory_region_transaction_begin();
1953 memory_region_ref(subregion
);
1954 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1955 if (subregion
->priority
>= other
->priority
) {
1956 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1960 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1962 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1963 memory_region_transaction_commit();
1966 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1968 MemoryRegion
*subregion
)
1970 assert(!subregion
->container
);
1971 subregion
->container
= mr
;
1972 subregion
->addr
= offset
;
1973 memory_region_update_container_subregions(subregion
);
1976 void memory_region_add_subregion(MemoryRegion
*mr
,
1978 MemoryRegion
*subregion
)
1980 subregion
->priority
= 0;
1981 memory_region_add_subregion_common(mr
, offset
, subregion
);
1984 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1986 MemoryRegion
*subregion
,
1989 subregion
->priority
= priority
;
1990 memory_region_add_subregion_common(mr
, offset
, subregion
);
1993 void memory_region_del_subregion(MemoryRegion
*mr
,
1994 MemoryRegion
*subregion
)
1996 memory_region_transaction_begin();
1997 assert(subregion
->container
== mr
);
1998 subregion
->container
= NULL
;
1999 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
2000 memory_region_unref(subregion
);
2001 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
2002 memory_region_transaction_commit();
2005 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
2007 if (enabled
== mr
->enabled
) {
2010 memory_region_transaction_begin();
2011 mr
->enabled
= enabled
;
2012 memory_region_update_pending
= true;
2013 memory_region_transaction_commit();
2016 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
)
2018 Int128 s
= int128_make64(size
);
2020 if (size
== UINT64_MAX
) {
2023 if (int128_eq(s
, mr
->size
)) {
2026 memory_region_transaction_begin();
2028 memory_region_update_pending
= true;
2029 memory_region_transaction_commit();
2032 static void memory_region_readd_subregion(MemoryRegion
*mr
)
2034 MemoryRegion
*container
= mr
->container
;
2037 memory_region_transaction_begin();
2038 memory_region_ref(mr
);
2039 memory_region_del_subregion(container
, mr
);
2040 mr
->container
= container
;
2041 memory_region_update_container_subregions(mr
);
2042 memory_region_unref(mr
);
2043 memory_region_transaction_commit();
2047 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
2049 if (addr
!= mr
->addr
) {
2051 memory_region_readd_subregion(mr
);
2055 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
2059 if (offset
== mr
->alias_offset
) {
2063 memory_region_transaction_begin();
2064 mr
->alias_offset
= offset
;
2065 memory_region_update_pending
|= mr
->enabled
;
2066 memory_region_transaction_commit();
2069 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
)
2074 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
2076 const AddrRange
*addr
= addr_
;
2077 const FlatRange
*fr
= fr_
;
2079 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
2081 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
2087 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
2089 return bsearch(&addr
, view
->ranges
, view
->nr
,
2090 sizeof(FlatRange
), cmp_flatrange_addr
);
2093 bool memory_region_is_mapped(MemoryRegion
*mr
)
2095 return mr
->container
? true : false;
2098 /* Same as memory_region_find, but it does not add a reference to the
2099 * returned region. It must be called from an RCU critical section.
2101 static MemoryRegionSection
memory_region_find_rcu(MemoryRegion
*mr
,
2102 hwaddr addr
, uint64_t size
)
2104 MemoryRegionSection ret
= { .mr
= NULL
};
2112 for (root
= mr
; root
->container
; ) {
2113 root
= root
->container
;
2117 as
= memory_region_to_address_space(root
);
2121 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
2123 view
= atomic_rcu_read(&as
->current_map
);
2124 fr
= flatview_lookup(view
, range
);
2129 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
2134 ret
.address_space
= as
;
2135 range
= addrrange_intersection(range
, fr
->addr
);
2136 ret
.offset_within_region
= fr
->offset_in_region
;
2137 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
2139 ret
.size
= range
.size
;
2140 ret
.offset_within_address_space
= int128_get64(range
.start
);
2141 ret
.readonly
= fr
->readonly
;
2145 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2146 hwaddr addr
, uint64_t size
)
2148 MemoryRegionSection ret
;
2150 ret
= memory_region_find_rcu(mr
, addr
, size
);
2152 memory_region_ref(ret
.mr
);
2158 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
2163 mr
= memory_region_find_rcu(container
, addr
, 1).mr
;
2165 return mr
&& mr
!= container
;
2168 void memory_global_dirty_log_sync(void)
2170 MemoryListener
*listener
;
2175 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2176 if (!listener
->log_sync
) {
2179 /* Global listeners are being phased out. */
2180 assert(listener
->address_space_filter
);
2181 as
= listener
->address_space_filter
;
2182 view
= address_space_get_flatview(as
);
2183 FOR_EACH_FLAT_RANGE(fr
, view
) {
2184 MemoryRegionSection mrs
= section_from_flat_range(fr
, as
);
2185 listener
->log_sync(listener
, &mrs
);
2187 flatview_unref(view
);
2191 void memory_global_dirty_log_start(void)
2193 global_dirty_log
= true;
2195 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
2197 /* Refresh DIRTY_LOG_MIGRATION bit. */
2198 memory_region_transaction_begin();
2199 memory_region_update_pending
= true;
2200 memory_region_transaction_commit();
2203 void memory_global_dirty_log_stop(void)
2205 global_dirty_log
= false;
2207 /* Refresh DIRTY_LOG_MIGRATION bit. */
2208 memory_region_transaction_begin();
2209 memory_region_update_pending
= true;
2210 memory_region_transaction_commit();
2212 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
2215 static void listener_add_address_space(MemoryListener
*listener
,
2221 if (listener
->address_space_filter
2222 && listener
->address_space_filter
!= as
) {
2226 if (listener
->begin
) {
2227 listener
->begin(listener
);
2229 if (global_dirty_log
) {
2230 if (listener
->log_global_start
) {
2231 listener
->log_global_start(listener
);
2235 view
= address_space_get_flatview(as
);
2236 FOR_EACH_FLAT_RANGE(fr
, view
) {
2237 MemoryRegionSection section
= {
2239 .address_space
= as
,
2240 .offset_within_region
= fr
->offset_in_region
,
2241 .size
= fr
->addr
.size
,
2242 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
2243 .readonly
= fr
->readonly
,
2245 if (fr
->dirty_log_mask
&& listener
->log_start
) {
2246 listener
->log_start(listener
, §ion
, 0, fr
->dirty_log_mask
);
2248 if (listener
->region_add
) {
2249 listener
->region_add(listener
, §ion
);
2252 if (listener
->commit
) {
2253 listener
->commit(listener
);
2255 flatview_unref(view
);
2258 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
2260 MemoryListener
*other
= NULL
;
2263 listener
->address_space_filter
= filter
;
2264 if (QTAILQ_EMPTY(&memory_listeners
)
2265 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
2266 memory_listeners
)->priority
) {
2267 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
2269 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
2270 if (listener
->priority
< other
->priority
) {
2274 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
2277 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2278 listener_add_address_space(listener
, as
);
2282 void memory_listener_unregister(MemoryListener
*listener
)
2284 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
2287 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
2289 memory_region_ref(root
);
2290 memory_region_transaction_begin();
2293 as
->malloced
= false;
2294 as
->current_map
= g_new(FlatView
, 1);
2295 flatview_init(as
->current_map
);
2296 as
->ioeventfd_nb
= 0;
2297 as
->ioeventfds
= NULL
;
2298 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
2299 as
->name
= g_strdup(name
? name
: "anonymous");
2300 address_space_init_dispatch(as
);
2301 memory_region_update_pending
|= root
->enabled
;
2302 memory_region_transaction_commit();
2305 static void do_address_space_destroy(AddressSpace
*as
)
2307 MemoryListener
*listener
;
2308 bool do_free
= as
->malloced
;
2310 address_space_destroy_dispatch(as
);
2312 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
2313 assert(listener
->address_space_filter
!= as
);
2316 flatview_unref(as
->current_map
);
2318 g_free(as
->ioeventfds
);
2319 memory_region_unref(as
->root
);
2325 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
, const char *name
)
2329 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2330 if (root
== as
->root
&& as
->malloced
) {
2336 as
= g_malloc0(sizeof *as
);
2337 address_space_init(as
, root
, name
);
2338 as
->malloced
= true;
2342 void address_space_destroy(AddressSpace
*as
)
2344 MemoryRegion
*root
= as
->root
;
2347 if (as
->ref_count
) {
2350 /* Flush out anything from MemoryListeners listening in on this */
2351 memory_region_transaction_begin();
2353 memory_region_transaction_commit();
2354 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
2355 address_space_unregister(as
);
2357 /* At this point, as->dispatch and as->current_map are dummy
2358 * entries that the guest should never use. Wait for the old
2359 * values to expire before freeing the data.
2362 call_rcu(as
, do_address_space_destroy
, rcu
);
2365 typedef struct MemoryRegionList MemoryRegionList
;
2367 struct MemoryRegionList
{
2368 const MemoryRegion
*mr
;
2369 QTAILQ_ENTRY(MemoryRegionList
) queue
;
2372 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
2374 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
2375 const MemoryRegion
*mr
, unsigned int level
,
2377 MemoryRegionListHead
*alias_print_queue
)
2379 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
2380 MemoryRegionListHead submr_print_queue
;
2381 const MemoryRegion
*submr
;
2388 for (i
= 0; i
< level
; i
++) {
2393 MemoryRegionList
*ml
;
2396 /* check if the alias is already in the queue */
2397 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
2398 if (ml
->mr
== mr
->alias
) {
2404 ml
= g_new(MemoryRegionList
, 1);
2406 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
2408 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
2409 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
2410 "-" TARGET_FMT_plx
"%s\n",
2413 + (int128_nz(mr
->size
) ?
2414 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2415 int128_one())) : 0),
2417 mr
->romd_mode
? 'R' : '-',
2418 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2420 memory_region_name(mr
),
2421 memory_region_name(mr
->alias
),
2424 + (int128_nz(mr
->size
) ?
2425 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2426 int128_one())) : 0),
2427 mr
->enabled
? "" : " [disabled]");
2430 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s%s\n",
2433 + (int128_nz(mr
->size
) ?
2434 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2435 int128_one())) : 0),
2437 mr
->romd_mode
? 'R' : '-',
2438 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2440 memory_region_name(mr
),
2441 mr
->enabled
? "" : " [disabled]");
2444 QTAILQ_INIT(&submr_print_queue
);
2446 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2447 new_ml
= g_new(MemoryRegionList
, 1);
2449 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2450 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2451 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2452 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2453 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
2459 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
2463 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2464 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
2468 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
2473 void mtree_info(fprintf_function mon_printf
, void *f
)
2475 MemoryRegionListHead ml_head
;
2476 MemoryRegionList
*ml
, *ml2
;
2479 QTAILQ_INIT(&ml_head
);
2481 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2482 mon_printf(f
, "address-space: %s\n", as
->name
);
2483 mtree_print_mr(mon_printf
, f
, as
->root
, 1, 0, &ml_head
);
2484 mon_printf(f
, "\n");
2487 /* print aliased regions */
2488 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
2489 mon_printf(f
, "memory-region: %s\n", memory_region_name(ml
->mr
));
2490 mtree_print_mr(mon_printf
, f
, ml
->mr
, 1, 0, &ml_head
);
2491 mon_printf(f
, "\n");
2494 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
2499 static const TypeInfo memory_region_info
= {
2500 .parent
= TYPE_OBJECT
,
2501 .name
= TYPE_MEMORY_REGION
,
2502 .instance_size
= sizeof(MemoryRegion
),
2503 .instance_init
= memory_region_initfn
,
2504 .instance_finalize
= memory_region_finalize
,
2507 static void memory_register_types(void)
2509 type_register_static(&memory_region_info
);
2512 type_init(memory_register_types
)