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 "exec/memory.h"
17 #include "exec/address-spaces.h"
18 #include "exec/ioport.h"
19 #include "qapi/visitor.h"
20 #include "qemu/bitops.h"
21 #include "qom/object.h"
25 #include "exec/memory-internal.h"
26 #include "exec/ram_addr.h"
27 #include "sysemu/sysemu.h"
29 //#define DEBUG_UNASSIGNED
31 static unsigned memory_region_transaction_depth
;
32 static bool memory_region_update_pending
;
33 static bool ioeventfd_update_pending
;
34 static bool global_dirty_log
= false;
36 /* flat_view_mutex is taken around reading as->current_map; the critical
37 * section is extremely short, so I'm using a single mutex for every AS.
38 * We could also RCU for the read-side.
40 * The BQL is taken around transaction commits, hence both locks are taken
41 * while writing to as->current_map (with the BQL taken outside).
43 static QemuMutex flat_view_mutex
;
45 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
46 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
48 static QTAILQ_HEAD(, AddressSpace
) address_spaces
49 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
51 static void memory_init(void)
53 qemu_mutex_init(&flat_view_mutex
);
56 typedef struct AddrRange AddrRange
;
59 * Note using signed integers limits us to physical addresses at most
60 * 63 bits wide. They are needed for negative offsetting in aliases
61 * (large MemoryRegion::alias_offset).
68 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
70 return (AddrRange
) { start
, size
};
73 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
75 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
78 static Int128
addrrange_end(AddrRange r
)
80 return int128_add(r
.start
, r
.size
);
83 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
85 int128_addto(&range
.start
, delta
);
89 static bool addrrange_contains(AddrRange range
, Int128 addr
)
91 return int128_ge(addr
, range
.start
)
92 && int128_lt(addr
, addrrange_end(range
));
95 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
97 return addrrange_contains(r1
, r2
.start
)
98 || addrrange_contains(r2
, r1
.start
);
101 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
103 Int128 start
= int128_max(r1
.start
, r2
.start
);
104 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
105 return addrrange_make(start
, int128_sub(end
, start
));
108 enum ListenerDirection
{ Forward
, Reverse
};
110 static bool memory_listener_match(MemoryListener
*listener
,
111 MemoryRegionSection
*section
)
113 return !listener
->address_space_filter
114 || listener
->address_space_filter
== section
->address_space
;
117 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
119 MemoryListener *_listener; \
121 switch (_direction) { \
123 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
124 if (_listener->_callback) { \
125 _listener->_callback(_listener, ##_args); \
130 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
131 memory_listeners, link) { \
132 if (_listener->_callback) { \
133 _listener->_callback(_listener, ##_args); \
142 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
144 MemoryListener *_listener; \
146 switch (_direction) { \
148 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
149 if (_listener->_callback \
150 && memory_listener_match(_listener, _section)) { \
151 _listener->_callback(_listener, _section, ##_args); \
156 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
157 memory_listeners, link) { \
158 if (_listener->_callback \
159 && memory_listener_match(_listener, _section)) { \
160 _listener->_callback(_listener, _section, ##_args); \
169 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
170 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
171 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
173 .address_space = (as), \
174 .offset_within_region = (fr)->offset_in_region, \
175 .size = (fr)->addr.size, \
176 .offset_within_address_space = int128_get64((fr)->addr.start), \
177 .readonly = (fr)->readonly, \
180 struct CoalescedMemoryRange
{
182 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
185 struct MemoryRegionIoeventfd
{
192 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
193 MemoryRegionIoeventfd b
)
195 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
197 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
199 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
201 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
203 } else if (a
.match_data
< b
.match_data
) {
205 } else if (a
.match_data
> b
.match_data
) {
207 } else if (a
.match_data
) {
208 if (a
.data
< b
.data
) {
210 } else if (a
.data
> b
.data
) {
216 } else if (a
.e
> b
.e
) {
222 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
223 MemoryRegionIoeventfd b
)
225 return !memory_region_ioeventfd_before(a
, b
)
226 && !memory_region_ioeventfd_before(b
, a
);
229 typedef struct FlatRange FlatRange
;
230 typedef struct FlatView FlatView
;
232 /* Range of memory in the global map. Addresses are absolute. */
235 hwaddr offset_in_region
;
237 uint8_t dirty_log_mask
;
242 /* Flattened global view of current active memory hierarchy. Kept in sorted
249 unsigned nr_allocated
;
252 typedef struct AddressSpaceOps AddressSpaceOps
;
254 #define FOR_EACH_FLAT_RANGE(var, view) \
255 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
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 void memory_region_oldmmio_read_accessor(MemoryRegion
*mr
,
394 tmp
= mr
->ops
->old_mmio
.read
[ctz32(size
)](mr
->opaque
, addr
);
395 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
396 *value
|= (tmp
& mask
) << shift
;
399 static void memory_region_read_accessor(MemoryRegion
*mr
,
408 if (mr
->flush_coalesced_mmio
) {
409 qemu_flush_coalesced_mmio_buffer();
411 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
412 trace_memory_region_ops_read(mr
, addr
, tmp
, size
);
413 *value
|= (tmp
& mask
) << shift
;
416 static void memory_region_oldmmio_write_accessor(MemoryRegion
*mr
,
425 tmp
= (*value
>> shift
) & mask
;
426 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
427 mr
->ops
->old_mmio
.write
[ctz32(size
)](mr
->opaque
, addr
, tmp
);
430 static void memory_region_write_accessor(MemoryRegion
*mr
,
439 if (mr
->flush_coalesced_mmio
) {
440 qemu_flush_coalesced_mmio_buffer();
442 tmp
= (*value
>> shift
) & mask
;
443 trace_memory_region_ops_write(mr
, addr
, tmp
, size
);
444 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
447 static void access_with_adjusted_size(hwaddr addr
,
450 unsigned access_size_min
,
451 unsigned access_size_max
,
452 void (*access
)(MemoryRegion
*mr
,
460 uint64_t access_mask
;
461 unsigned access_size
;
464 if (!access_size_min
) {
467 if (!access_size_max
) {
471 /* FIXME: support unaligned access? */
472 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
473 access_mask
= -1ULL >> (64 - access_size
* 8);
474 if (memory_region_big_endian(mr
)) {
475 for (i
= 0; i
< size
; i
+= access_size
) {
476 access(mr
, addr
+ i
, value
, access_size
,
477 (size
- access_size
- i
) * 8, access_mask
);
480 for (i
= 0; i
< size
; i
+= access_size
) {
481 access(mr
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
486 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
490 while (mr
->container
) {
493 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
494 if (mr
== as
->root
) {
501 /* Render a memory region into the global view. Ranges in @view obscure
504 static void render_memory_region(FlatView
*view
,
510 MemoryRegion
*subregion
;
512 hwaddr offset_in_region
;
522 int128_addto(&base
, int128_make64(mr
->addr
));
523 readonly
|= mr
->readonly
;
525 tmp
= addrrange_make(base
, mr
->size
);
527 if (!addrrange_intersects(tmp
, clip
)) {
531 clip
= addrrange_intersection(tmp
, clip
);
534 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
535 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
536 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
540 /* Render subregions in priority order. */
541 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
542 render_memory_region(view
, subregion
, base
, clip
, readonly
);
545 if (!mr
->terminates
) {
549 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
554 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
555 fr
.romd_mode
= mr
->romd_mode
;
556 fr
.readonly
= readonly
;
558 /* Render the region itself into any gaps left by the current view. */
559 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
560 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
563 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
564 now
= int128_min(remain
,
565 int128_sub(view
->ranges
[i
].addr
.start
, base
));
566 fr
.offset_in_region
= offset_in_region
;
567 fr
.addr
= addrrange_make(base
, now
);
568 flatview_insert(view
, i
, &fr
);
570 int128_addto(&base
, now
);
571 offset_in_region
+= int128_get64(now
);
572 int128_subfrom(&remain
, now
);
574 now
= int128_sub(int128_min(int128_add(base
, remain
),
575 addrrange_end(view
->ranges
[i
].addr
)),
577 int128_addto(&base
, now
);
578 offset_in_region
+= int128_get64(now
);
579 int128_subfrom(&remain
, now
);
581 if (int128_nz(remain
)) {
582 fr
.offset_in_region
= offset_in_region
;
583 fr
.addr
= addrrange_make(base
, remain
);
584 flatview_insert(view
, i
, &fr
);
588 /* Render a memory topology into a list of disjoint absolute ranges. */
589 static FlatView
*generate_memory_topology(MemoryRegion
*mr
)
593 view
= g_new(FlatView
, 1);
597 render_memory_region(view
, mr
, int128_zero(),
598 addrrange_make(int128_zero(), int128_2_64()), false);
600 flatview_simplify(view
);
605 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
606 MemoryRegionIoeventfd
*fds_new
,
608 MemoryRegionIoeventfd
*fds_old
,
612 MemoryRegionIoeventfd
*fd
;
613 MemoryRegionSection section
;
615 /* Generate a symmetric difference of the old and new fd sets, adding
616 * and deleting as necessary.
620 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
621 if (iold
< fds_old_nb
622 && (inew
== fds_new_nb
623 || memory_region_ioeventfd_before(fds_old
[iold
],
626 section
= (MemoryRegionSection
) {
628 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
629 .size
= fd
->addr
.size
,
631 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
632 fd
->match_data
, fd
->data
, fd
->e
);
634 } else if (inew
< fds_new_nb
635 && (iold
== fds_old_nb
636 || memory_region_ioeventfd_before(fds_new
[inew
],
639 section
= (MemoryRegionSection
) {
641 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
642 .size
= fd
->addr
.size
,
644 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
645 fd
->match_data
, fd
->data
, fd
->e
);
654 static FlatView
*address_space_get_flatview(AddressSpace
*as
)
658 qemu_mutex_lock(&flat_view_mutex
);
659 view
= as
->current_map
;
661 qemu_mutex_unlock(&flat_view_mutex
);
665 static void address_space_update_ioeventfds(AddressSpace
*as
)
669 unsigned ioeventfd_nb
= 0;
670 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
674 view
= address_space_get_flatview(as
);
675 FOR_EACH_FLAT_RANGE(fr
, view
) {
676 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
677 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
678 int128_sub(fr
->addr
.start
,
679 int128_make64(fr
->offset_in_region
)));
680 if (addrrange_intersects(fr
->addr
, tmp
)) {
682 ioeventfds
= g_realloc(ioeventfds
,
683 ioeventfd_nb
* sizeof(*ioeventfds
));
684 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
685 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
690 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
691 as
->ioeventfds
, as
->ioeventfd_nb
);
693 g_free(as
->ioeventfds
);
694 as
->ioeventfds
= ioeventfds
;
695 as
->ioeventfd_nb
= ioeventfd_nb
;
696 flatview_unref(view
);
699 static void address_space_update_topology_pass(AddressSpace
*as
,
700 const FlatView
*old_view
,
701 const FlatView
*new_view
,
705 FlatRange
*frold
, *frnew
;
707 /* Generate a symmetric difference of the old and new memory maps.
708 * Kill ranges in the old map, and instantiate ranges in the new map.
711 while (iold
< old_view
->nr
|| inew
< new_view
->nr
) {
712 if (iold
< old_view
->nr
) {
713 frold
= &old_view
->ranges
[iold
];
717 if (inew
< new_view
->nr
) {
718 frnew
= &new_view
->ranges
[inew
];
725 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
726 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
727 && !flatrange_equal(frold
, frnew
)))) {
728 /* In old but not in new, or in both but attributes changed. */
731 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
735 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
736 /* In both and unchanged (except logging may have changed) */
739 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
740 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
741 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
742 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
743 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
753 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
762 static void address_space_update_topology(AddressSpace
*as
)
764 FlatView
*old_view
= address_space_get_flatview(as
);
765 FlatView
*new_view
= generate_memory_topology(as
->root
);
767 address_space_update_topology_pass(as
, old_view
, new_view
, false);
768 address_space_update_topology_pass(as
, old_view
, new_view
, true);
770 qemu_mutex_lock(&flat_view_mutex
);
771 flatview_unref(as
->current_map
);
772 as
->current_map
= new_view
;
773 qemu_mutex_unlock(&flat_view_mutex
);
775 /* Note that all the old MemoryRegions are still alive up to this
776 * point. This relieves most MemoryListeners from the need to
777 * ref/unref the MemoryRegions they get---unless they use them
778 * outside the iothread mutex, in which case precise reference
779 * counting is necessary.
781 flatview_unref(old_view
);
783 address_space_update_ioeventfds(as
);
786 void memory_region_transaction_begin(void)
788 qemu_flush_coalesced_mmio_buffer();
789 ++memory_region_transaction_depth
;
792 static void memory_region_clear_pending(void)
794 memory_region_update_pending
= false;
795 ioeventfd_update_pending
= false;
798 void memory_region_transaction_commit(void)
802 assert(memory_region_transaction_depth
);
803 --memory_region_transaction_depth
;
804 if (!memory_region_transaction_depth
) {
805 if (memory_region_update_pending
) {
806 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
808 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
809 address_space_update_topology(as
);
812 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
813 } else if (ioeventfd_update_pending
) {
814 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
815 address_space_update_ioeventfds(as
);
818 memory_region_clear_pending();
822 static void memory_region_destructor_none(MemoryRegion
*mr
)
826 static void memory_region_destructor_ram(MemoryRegion
*mr
)
828 qemu_ram_free(mr
->ram_addr
);
831 static void memory_region_destructor_alias(MemoryRegion
*mr
)
833 memory_region_unref(mr
->alias
);
836 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
838 qemu_ram_free_from_ptr(mr
->ram_addr
);
841 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
843 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
846 static bool memory_region_need_escape(char c
)
848 return c
== '/' || c
== '[' || c
== '\\' || c
== ']';
851 static char *memory_region_escape_name(const char *name
)
858 for (p
= name
; *p
; p
++) {
859 bytes
+= memory_region_need_escape(*p
) ? 4 : 1;
861 if (bytes
== p
- name
) {
862 return g_memdup(name
, bytes
+ 1);
865 escaped
= g_malloc(bytes
+ 1);
866 for (p
= name
, q
= escaped
; *p
; p
++) {
868 if (unlikely(memory_region_need_escape(c
))) {
871 *q
++ = "0123456789abcdef"[c
>> 4];
872 c
= "0123456789abcdef"[c
& 15];
880 static void object_property_add_child_array(Object
*owner
,
885 char *base_name
= memory_region_escape_name(name
);
888 char *full_name
= g_strdup_printf("%s[%d]", base_name
, i
);
889 Error
*local_err
= NULL
;
891 object_property_add_child(owner
, full_name
, child
, &local_err
);
897 error_free(local_err
);
904 void memory_region_init(MemoryRegion
*mr
,
910 owner
= qdev_get_machine();
913 object_initialize(mr
, sizeof(*mr
), TYPE_MEMORY_REGION
);
914 mr
->size
= int128_make64(size
);
915 if (size
== UINT64_MAX
) {
916 mr
->size
= int128_2_64();
918 mr
->name
= g_strdup(name
);
921 object_property_add_child_array(owner
, name
, OBJECT(mr
));
922 object_unref(OBJECT(mr
));
926 static void memory_region_get_addr(Object
*obj
, Visitor
*v
, void *opaque
,
927 const char *name
, Error
**errp
)
929 MemoryRegion
*mr
= MEMORY_REGION(obj
);
930 uint64_t value
= mr
->addr
;
932 visit_type_uint64(v
, &value
, name
, errp
);
935 static void memory_region_get_container(Object
*obj
, Visitor
*v
, void *opaque
,
936 const char *name
, Error
**errp
)
938 MemoryRegion
*mr
= MEMORY_REGION(obj
);
939 gchar
*path
= (gchar
*)"";
942 path
= object_get_canonical_path(OBJECT(mr
->container
));
944 visit_type_str(v
, &path
, name
, errp
);
950 static Object
*memory_region_resolve_container(Object
*obj
, void *opaque
,
953 MemoryRegion
*mr
= MEMORY_REGION(obj
);
955 return OBJECT(mr
->container
);
958 static void memory_region_get_priority(Object
*obj
, Visitor
*v
, void *opaque
,
959 const char *name
, Error
**errp
)
961 MemoryRegion
*mr
= MEMORY_REGION(obj
);
962 int32_t value
= mr
->priority
;
964 visit_type_int32(v
, &value
, name
, errp
);
967 static bool memory_region_get_may_overlap(Object
*obj
, Error
**errp
)
969 MemoryRegion
*mr
= MEMORY_REGION(obj
);
971 return mr
->may_overlap
;
974 static void memory_region_get_size(Object
*obj
, Visitor
*v
, void *opaque
,
975 const char *name
, Error
**errp
)
977 MemoryRegion
*mr
= MEMORY_REGION(obj
);
978 uint64_t value
= memory_region_size(mr
);
980 visit_type_uint64(v
, &value
, name
, errp
);
983 static void memory_region_initfn(Object
*obj
)
985 MemoryRegion
*mr
= MEMORY_REGION(obj
);
988 mr
->ops
= &unassigned_mem_ops
;
990 mr
->romd_mode
= true;
991 mr
->destructor
= memory_region_destructor_none
;
992 QTAILQ_INIT(&mr
->subregions
);
993 QTAILQ_INIT(&mr
->coalesced
);
995 op
= object_property_add(OBJECT(mr
), "container",
996 "link<" TYPE_MEMORY_REGION
">",
997 memory_region_get_container
,
998 NULL
, /* memory_region_set_container */
999 NULL
, NULL
, &error_abort
);
1000 op
->resolve
= memory_region_resolve_container
;
1002 object_property_add(OBJECT(mr
), "addr", "uint64",
1003 memory_region_get_addr
,
1004 NULL
, /* memory_region_set_addr */
1005 NULL
, NULL
, &error_abort
);
1006 object_property_add(OBJECT(mr
), "priority", "uint32",
1007 memory_region_get_priority
,
1008 NULL
, /* memory_region_set_priority */
1009 NULL
, NULL
, &error_abort
);
1010 object_property_add_bool(OBJECT(mr
), "may-overlap",
1011 memory_region_get_may_overlap
,
1012 NULL
, /* memory_region_set_may_overlap */
1014 object_property_add(OBJECT(mr
), "size", "uint64",
1015 memory_region_get_size
,
1016 NULL
, /* memory_region_set_size, */
1017 NULL
, NULL
, &error_abort
);
1020 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
1023 #ifdef DEBUG_UNASSIGNED
1024 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
1026 if (current_cpu
!= NULL
) {
1027 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
1032 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
1033 uint64_t val
, unsigned size
)
1035 #ifdef DEBUG_UNASSIGNED
1036 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1038 if (current_cpu
!= NULL
) {
1039 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1043 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1044 unsigned size
, bool is_write
)
1049 const MemoryRegionOps unassigned_mem_ops
= {
1050 .valid
.accepts
= unassigned_mem_accepts
,
1051 .endianness
= DEVICE_NATIVE_ENDIAN
,
1054 bool memory_region_access_valid(MemoryRegion
*mr
,
1059 int access_size_min
, access_size_max
;
1062 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1066 if (!mr
->ops
->valid
.accepts
) {
1070 access_size_min
= mr
->ops
->valid
.min_access_size
;
1071 if (!mr
->ops
->valid
.min_access_size
) {
1072 access_size_min
= 1;
1075 access_size_max
= mr
->ops
->valid
.max_access_size
;
1076 if (!mr
->ops
->valid
.max_access_size
) {
1077 access_size_max
= 4;
1080 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1081 for (i
= 0; i
< size
; i
+= access_size
) {
1082 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1091 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
1097 if (mr
->ops
->read
) {
1098 access_with_adjusted_size(addr
, &data
, size
,
1099 mr
->ops
->impl
.min_access_size
,
1100 mr
->ops
->impl
.max_access_size
,
1101 memory_region_read_accessor
, mr
);
1103 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1104 memory_region_oldmmio_read_accessor
, mr
);
1110 static bool memory_region_dispatch_read(MemoryRegion
*mr
,
1115 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1116 *pval
= unassigned_mem_read(mr
, addr
, size
);
1120 *pval
= memory_region_dispatch_read1(mr
, addr
, size
);
1121 adjust_endianness(mr
, pval
, size
);
1125 static bool memory_region_dispatch_write(MemoryRegion
*mr
,
1130 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1131 unassigned_mem_write(mr
, addr
, data
, size
);
1135 adjust_endianness(mr
, &data
, size
);
1137 if (mr
->ops
->write
) {
1138 access_with_adjusted_size(addr
, &data
, size
,
1139 mr
->ops
->impl
.min_access_size
,
1140 mr
->ops
->impl
.max_access_size
,
1141 memory_region_write_accessor
, mr
);
1143 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1144 memory_region_oldmmio_write_accessor
, mr
);
1149 void memory_region_init_io(MemoryRegion
*mr
,
1151 const MemoryRegionOps
*ops
,
1156 memory_region_init(mr
, owner
, name
, size
);
1158 mr
->opaque
= opaque
;
1159 mr
->terminates
= true;
1160 mr
->ram_addr
= ~(ram_addr_t
)0;
1163 void memory_region_init_ram(MemoryRegion
*mr
,
1168 memory_region_init(mr
, owner
, name
, size
);
1170 mr
->terminates
= true;
1171 mr
->destructor
= memory_region_destructor_ram
;
1172 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1176 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1177 struct Object
*owner
,
1184 memory_region_init(mr
, owner
, name
, size
);
1186 mr
->terminates
= true;
1187 mr
->destructor
= memory_region_destructor_ram
;
1188 mr
->ram_addr
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1192 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1198 memory_region_init(mr
, owner
, name
, size
);
1200 mr
->terminates
= true;
1201 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1202 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
1205 void memory_region_init_alias(MemoryRegion
*mr
,
1212 memory_region_init(mr
, owner
, name
, size
);
1213 memory_region_ref(orig
);
1214 mr
->destructor
= memory_region_destructor_alias
;
1216 mr
->alias_offset
= offset
;
1219 void memory_region_init_rom_device(MemoryRegion
*mr
,
1221 const MemoryRegionOps
*ops
,
1226 memory_region_init(mr
, owner
, name
, size
);
1228 mr
->opaque
= opaque
;
1229 mr
->terminates
= true;
1230 mr
->rom_device
= true;
1231 mr
->destructor
= memory_region_destructor_rom_device
;
1232 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1235 void memory_region_init_iommu(MemoryRegion
*mr
,
1237 const MemoryRegionIOMMUOps
*ops
,
1241 memory_region_init(mr
, owner
, name
, size
);
1242 mr
->iommu_ops
= ops
,
1243 mr
->terminates
= true; /* then re-forwards */
1244 notifier_list_init(&mr
->iommu_notify
);
1247 void memory_region_init_reservation(MemoryRegion
*mr
,
1252 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1255 static void memory_region_finalize(Object
*obj
)
1257 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1259 assert(QTAILQ_EMPTY(&mr
->subregions
));
1260 assert(memory_region_transaction_depth
== 0);
1262 memory_region_clear_coalescing(mr
);
1263 g_free((char *)mr
->name
);
1264 g_free(mr
->ioeventfds
);
1267 void memory_region_destroy(MemoryRegion
*mr
)
1269 object_unparent(OBJECT(mr
));
1273 Object
*memory_region_owner(MemoryRegion
*mr
)
1275 Object
*obj
= OBJECT(mr
);
1279 void memory_region_ref(MemoryRegion
*mr
)
1281 /* MMIO callbacks most likely will access data that belongs
1282 * to the owner, hence the need to ref/unref the owner whenever
1283 * the memory region is in use.
1285 * The memory region is a child of its owner. As long as the
1286 * owner doesn't call unparent itself on the memory region,
1287 * ref-ing the owner will also keep the memory region alive.
1288 * Memory regions without an owner are supposed to never go away,
1289 * but we still ref/unref them for debugging purposes.
1291 Object
*obj
= OBJECT(mr
);
1292 if (obj
&& obj
->parent
) {
1293 object_ref(obj
->parent
);
1299 void memory_region_unref(MemoryRegion
*mr
)
1301 Object
*obj
= OBJECT(mr
);
1302 if (obj
&& obj
->parent
) {
1303 object_unref(obj
->parent
);
1309 uint64_t memory_region_size(MemoryRegion
*mr
)
1311 if (int128_eq(mr
->size
, int128_2_64())) {
1314 return int128_get64(mr
->size
);
1317 const char *memory_region_name(MemoryRegion
*mr
)
1322 bool memory_region_is_ram(MemoryRegion
*mr
)
1327 bool memory_region_is_logging(MemoryRegion
*mr
)
1329 return mr
->dirty_log_mask
;
1332 bool memory_region_is_rom(MemoryRegion
*mr
)
1334 return mr
->ram
&& mr
->readonly
;
1337 bool memory_region_is_iommu(MemoryRegion
*mr
)
1339 return mr
->iommu_ops
;
1342 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1344 notifier_list_add(&mr
->iommu_notify
, n
);
1347 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1352 void memory_region_notify_iommu(MemoryRegion
*mr
,
1353 IOMMUTLBEntry entry
)
1355 assert(memory_region_is_iommu(mr
));
1356 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1359 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1361 uint8_t mask
= 1 << client
;
1363 memory_region_transaction_begin();
1364 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1365 memory_region_update_pending
|= mr
->enabled
;
1366 memory_region_transaction_commit();
1369 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1370 hwaddr size
, unsigned client
)
1372 assert(mr
->terminates
);
1373 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1376 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1379 assert(mr
->terminates
);
1380 cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
);
1383 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1384 hwaddr size
, unsigned client
)
1387 assert(mr
->terminates
);
1388 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1390 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1396 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1401 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1402 FlatView
*view
= address_space_get_flatview(as
);
1403 FOR_EACH_FLAT_RANGE(fr
, view
) {
1405 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1408 flatview_unref(view
);
1412 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1414 if (mr
->readonly
!= readonly
) {
1415 memory_region_transaction_begin();
1416 mr
->readonly
= readonly
;
1417 memory_region_update_pending
|= mr
->enabled
;
1418 memory_region_transaction_commit();
1422 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1424 if (mr
->romd_mode
!= romd_mode
) {
1425 memory_region_transaction_begin();
1426 mr
->romd_mode
= romd_mode
;
1427 memory_region_update_pending
|= mr
->enabled
;
1428 memory_region_transaction_commit();
1432 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1433 hwaddr size
, unsigned client
)
1435 assert(mr
->terminates
);
1436 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1439 int memory_region_get_fd(MemoryRegion
*mr
)
1442 return memory_region_get_fd(mr
->alias
);
1445 assert(mr
->terminates
);
1447 return qemu_get_ram_fd(mr
->ram_addr
& TARGET_PAGE_MASK
);
1450 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1453 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1456 assert(mr
->terminates
);
1458 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1461 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1465 CoalescedMemoryRange
*cmr
;
1467 MemoryRegionSection section
;
1469 view
= address_space_get_flatview(as
);
1470 FOR_EACH_FLAT_RANGE(fr
, view
) {
1472 section
= (MemoryRegionSection
) {
1473 .address_space
= as
,
1474 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1475 .size
= fr
->addr
.size
,
1478 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1479 int128_get64(fr
->addr
.start
),
1480 int128_get64(fr
->addr
.size
));
1481 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1482 tmp
= addrrange_shift(cmr
->addr
,
1483 int128_sub(fr
->addr
.start
,
1484 int128_make64(fr
->offset_in_region
)));
1485 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1488 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1489 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1490 int128_get64(tmp
.start
),
1491 int128_get64(tmp
.size
));
1495 flatview_unref(view
);
1498 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1502 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1503 memory_region_update_coalesced_range_as(mr
, as
);
1507 void memory_region_set_coalescing(MemoryRegion
*mr
)
1509 memory_region_clear_coalescing(mr
);
1510 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1513 void memory_region_add_coalescing(MemoryRegion
*mr
,
1517 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1519 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1520 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1521 memory_region_update_coalesced_range(mr
);
1522 memory_region_set_flush_coalesced(mr
);
1525 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1527 CoalescedMemoryRange
*cmr
;
1528 bool updated
= false;
1530 qemu_flush_coalesced_mmio_buffer();
1531 mr
->flush_coalesced_mmio
= false;
1533 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1534 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1535 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1541 memory_region_update_coalesced_range(mr
);
1545 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1547 mr
->flush_coalesced_mmio
= true;
1550 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1552 qemu_flush_coalesced_mmio_buffer();
1553 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1554 mr
->flush_coalesced_mmio
= false;
1558 void memory_region_add_eventfd(MemoryRegion
*mr
,
1565 MemoryRegionIoeventfd mrfd
= {
1566 .addr
.start
= int128_make64(addr
),
1567 .addr
.size
= int128_make64(size
),
1568 .match_data
= match_data
,
1574 adjust_endianness(mr
, &mrfd
.data
, size
);
1575 memory_region_transaction_begin();
1576 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1577 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1582 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1583 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1584 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1585 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1586 mr
->ioeventfds
[i
] = mrfd
;
1587 ioeventfd_update_pending
|= mr
->enabled
;
1588 memory_region_transaction_commit();
1591 void memory_region_del_eventfd(MemoryRegion
*mr
,
1598 MemoryRegionIoeventfd mrfd
= {
1599 .addr
.start
= int128_make64(addr
),
1600 .addr
.size
= int128_make64(size
),
1601 .match_data
= match_data
,
1607 adjust_endianness(mr
, &mrfd
.data
, size
);
1608 memory_region_transaction_begin();
1609 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1610 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1614 assert(i
!= mr
->ioeventfd_nb
);
1615 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1616 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1618 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1619 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1620 ioeventfd_update_pending
|= mr
->enabled
;
1621 memory_region_transaction_commit();
1624 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
1626 hwaddr offset
= subregion
->addr
;
1627 MemoryRegion
*mr
= subregion
->container
;
1628 MemoryRegion
*other
;
1630 memory_region_transaction_begin();
1632 memory_region_ref(subregion
);
1633 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1634 if (subregion
->may_overlap
|| other
->may_overlap
) {
1637 if (int128_ge(int128_make64(offset
),
1638 int128_add(int128_make64(other
->addr
), other
->size
))
1639 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1640 int128_make64(other
->addr
))) {
1644 printf("warning: subregion collision %llx/%llx (%s) "
1645 "vs %llx/%llx (%s)\n",
1646 (unsigned long long)offset
,
1647 (unsigned long long)int128_get64(subregion
->size
),
1649 (unsigned long long)other
->addr
,
1650 (unsigned long long)int128_get64(other
->size
),
1654 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1655 if (subregion
->priority
>= other
->priority
) {
1656 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1660 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1662 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1663 memory_region_transaction_commit();
1666 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1668 MemoryRegion
*subregion
)
1670 assert(!subregion
->container
);
1671 subregion
->container
= mr
;
1672 subregion
->addr
= offset
;
1673 memory_region_update_container_subregions(subregion
);
1676 void memory_region_add_subregion(MemoryRegion
*mr
,
1678 MemoryRegion
*subregion
)
1680 subregion
->may_overlap
= false;
1681 subregion
->priority
= 0;
1682 memory_region_add_subregion_common(mr
, offset
, subregion
);
1685 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1687 MemoryRegion
*subregion
,
1690 subregion
->may_overlap
= true;
1691 subregion
->priority
= priority
;
1692 memory_region_add_subregion_common(mr
, offset
, subregion
);
1695 void memory_region_del_subregion(MemoryRegion
*mr
,
1696 MemoryRegion
*subregion
)
1698 memory_region_transaction_begin();
1699 assert(subregion
->container
== mr
);
1700 subregion
->container
= NULL
;
1701 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1702 memory_region_unref(subregion
);
1703 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1704 memory_region_transaction_commit();
1707 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1709 if (enabled
== mr
->enabled
) {
1712 memory_region_transaction_begin();
1713 mr
->enabled
= enabled
;
1714 memory_region_update_pending
= true;
1715 memory_region_transaction_commit();
1718 static void memory_region_readd_subregion(MemoryRegion
*mr
)
1720 MemoryRegion
*container
= mr
->container
;
1723 memory_region_transaction_begin();
1724 memory_region_ref(mr
);
1725 memory_region_del_subregion(container
, mr
);
1726 mr
->container
= container
;
1727 memory_region_update_container_subregions(mr
);
1728 memory_region_unref(mr
);
1729 memory_region_transaction_commit();
1733 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1735 if (addr
!= mr
->addr
) {
1737 memory_region_readd_subregion(mr
);
1741 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1745 if (offset
== mr
->alias_offset
) {
1749 memory_region_transaction_begin();
1750 mr
->alias_offset
= offset
;
1751 memory_region_update_pending
|= mr
->enabled
;
1752 memory_region_transaction_commit();
1755 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1757 return mr
->ram_addr
;
1760 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1762 const AddrRange
*addr
= addr_
;
1763 const FlatRange
*fr
= fr_
;
1765 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1767 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1773 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1775 return bsearch(&addr
, view
->ranges
, view
->nr
,
1776 sizeof(FlatRange
), cmp_flatrange_addr
);
1779 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
1781 MemoryRegion
*mr
= memory_region_find(container
, addr
, 1).mr
;
1782 if (!mr
|| (mr
== container
)) {
1785 memory_region_unref(mr
);
1789 bool memory_region_is_mapped(MemoryRegion
*mr
)
1791 return mr
->container
? true : false;
1794 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1795 hwaddr addr
, uint64_t size
)
1797 MemoryRegionSection ret
= { .mr
= NULL
};
1805 for (root
= mr
; root
->container
; ) {
1806 root
= root
->container
;
1810 as
= memory_region_to_address_space(root
);
1814 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1816 view
= address_space_get_flatview(as
);
1817 fr
= flatview_lookup(view
, range
);
1819 flatview_unref(view
);
1823 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1828 ret
.address_space
= as
;
1829 range
= addrrange_intersection(range
, fr
->addr
);
1830 ret
.offset_within_region
= fr
->offset_in_region
;
1831 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1833 ret
.size
= range
.size
;
1834 ret
.offset_within_address_space
= int128_get64(range
.start
);
1835 ret
.readonly
= fr
->readonly
;
1836 memory_region_ref(ret
.mr
);
1838 flatview_unref(view
);
1842 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1847 view
= address_space_get_flatview(as
);
1848 FOR_EACH_FLAT_RANGE(fr
, view
) {
1849 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1851 flatview_unref(view
);
1854 void memory_global_dirty_log_start(void)
1856 global_dirty_log
= true;
1857 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1860 void memory_global_dirty_log_stop(void)
1862 global_dirty_log
= false;
1863 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1866 static void listener_add_address_space(MemoryListener
*listener
,
1872 if (listener
->address_space_filter
1873 && listener
->address_space_filter
!= as
) {
1877 if (global_dirty_log
) {
1878 if (listener
->log_global_start
) {
1879 listener
->log_global_start(listener
);
1883 view
= address_space_get_flatview(as
);
1884 FOR_EACH_FLAT_RANGE(fr
, view
) {
1885 MemoryRegionSection section
= {
1887 .address_space
= as
,
1888 .offset_within_region
= fr
->offset_in_region
,
1889 .size
= fr
->addr
.size
,
1890 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1891 .readonly
= fr
->readonly
,
1893 if (listener
->region_add
) {
1894 listener
->region_add(listener
, §ion
);
1897 flatview_unref(view
);
1900 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1902 MemoryListener
*other
= NULL
;
1905 listener
->address_space_filter
= filter
;
1906 if (QTAILQ_EMPTY(&memory_listeners
)
1907 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1908 memory_listeners
)->priority
) {
1909 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1911 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1912 if (listener
->priority
< other
->priority
) {
1916 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1919 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1920 listener_add_address_space(listener
, as
);
1924 void memory_listener_unregister(MemoryListener
*listener
)
1926 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1929 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1931 if (QTAILQ_EMPTY(&address_spaces
)) {
1935 memory_region_transaction_begin();
1937 as
->current_map
= g_new(FlatView
, 1);
1938 flatview_init(as
->current_map
);
1939 as
->ioeventfd_nb
= 0;
1940 as
->ioeventfds
= NULL
;
1941 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1942 as
->name
= g_strdup(name
? name
: "anonymous");
1943 address_space_init_dispatch(as
);
1944 memory_region_update_pending
|= root
->enabled
;
1945 memory_region_transaction_commit();
1948 void address_space_destroy(AddressSpace
*as
)
1950 MemoryListener
*listener
;
1952 /* Flush out anything from MemoryListeners listening in on this */
1953 memory_region_transaction_begin();
1955 memory_region_transaction_commit();
1956 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1957 address_space_destroy_dispatch(as
);
1959 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
1960 assert(listener
->address_space_filter
!= as
);
1963 flatview_unref(as
->current_map
);
1965 g_free(as
->ioeventfds
);
1968 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
1970 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
1973 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1974 uint64_t val
, unsigned size
)
1976 return memory_region_dispatch_write(mr
, addr
, val
, size
);
1979 typedef struct MemoryRegionList MemoryRegionList
;
1981 struct MemoryRegionList
{
1982 const MemoryRegion
*mr
;
1984 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1987 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1989 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1990 const MemoryRegion
*mr
, unsigned int level
,
1992 MemoryRegionListHead
*alias_print_queue
)
1994 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1995 MemoryRegionListHead submr_print_queue
;
1996 const MemoryRegion
*submr
;
1999 if (!mr
|| !mr
->enabled
) {
2003 for (i
= 0; i
< level
; i
++) {
2008 MemoryRegionList
*ml
;
2011 /* check if the alias is already in the queue */
2012 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
2013 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
2019 ml
= g_new(MemoryRegionList
, 1);
2021 ml
->printed
= false;
2022 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
2024 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
2025 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
2026 "-" TARGET_FMT_plx
"\n",
2029 + (int128_nz(mr
->size
) ?
2030 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2031 int128_one())) : 0),
2033 mr
->romd_mode
? 'R' : '-',
2034 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2040 + (int128_nz(mr
->size
) ?
2041 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2042 int128_one())) : 0));
2045 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
2048 + (int128_nz(mr
->size
) ?
2049 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2050 int128_one())) : 0),
2052 mr
->romd_mode
? 'R' : '-',
2053 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2058 QTAILQ_INIT(&submr_print_queue
);
2060 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2061 new_ml
= g_new(MemoryRegionList
, 1);
2063 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2064 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2065 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2066 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2067 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
2073 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
2077 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2078 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
2082 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
2087 void mtree_info(fprintf_function mon_printf
, void *f
)
2089 MemoryRegionListHead ml_head
;
2090 MemoryRegionList
*ml
, *ml2
;
2093 QTAILQ_INIT(&ml_head
);
2095 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2096 mon_printf(f
, "%s\n", as
->name
);
2097 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
2100 mon_printf(f
, "aliases\n");
2101 /* print aliased regions */
2102 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
2104 mon_printf(f
, "%s\n", ml
->mr
->name
);
2105 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
2109 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
2114 static const TypeInfo memory_region_info
= {
2115 .parent
= TYPE_OBJECT
,
2116 .name
= TYPE_MEMORY_REGION
,
2117 .instance_size
= sizeof(MemoryRegion
),
2118 .instance_init
= memory_region_initfn
,
2119 .instance_finalize
= memory_region_finalize
,
2122 static void memory_register_types(void)
2124 type_register_static(&memory_region_info
);
2127 type_init(memory_register_types
)