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_initfn(Object
*obj
)
960 MemoryRegion
*mr
= MEMORY_REGION(obj
);
963 mr
->ops
= &unassigned_mem_ops
;
965 mr
->romd_mode
= true;
966 mr
->destructor
= memory_region_destructor_none
;
967 QTAILQ_INIT(&mr
->subregions
);
968 QTAILQ_INIT(&mr
->coalesced
);
970 op
= object_property_add(OBJECT(mr
), "container",
971 "link<" TYPE_MEMORY_REGION
">",
972 memory_region_get_container
,
973 NULL
, /* memory_region_set_container */
974 NULL
, NULL
, &error_abort
);
975 op
->resolve
= memory_region_resolve_container
;
977 object_property_add(OBJECT(mr
), "addr", "uint64",
978 memory_region_get_addr
,
979 NULL
, /* memory_region_set_addr */
980 NULL
, NULL
, &error_abort
);
983 static uint64_t unassigned_mem_read(void *opaque
, hwaddr addr
,
986 #ifdef DEBUG_UNASSIGNED
987 printf("Unassigned mem read " TARGET_FMT_plx
"\n", addr
);
989 if (current_cpu
!= NULL
) {
990 cpu_unassigned_access(current_cpu
, addr
, false, false, 0, size
);
995 static void unassigned_mem_write(void *opaque
, hwaddr addr
,
996 uint64_t val
, unsigned size
)
998 #ifdef DEBUG_UNASSIGNED
999 printf("Unassigned mem write " TARGET_FMT_plx
" = 0x%"PRIx64
"\n", addr
, val
);
1001 if (current_cpu
!= NULL
) {
1002 cpu_unassigned_access(current_cpu
, addr
, true, false, 0, size
);
1006 static bool unassigned_mem_accepts(void *opaque
, hwaddr addr
,
1007 unsigned size
, bool is_write
)
1012 const MemoryRegionOps unassigned_mem_ops
= {
1013 .valid
.accepts
= unassigned_mem_accepts
,
1014 .endianness
= DEVICE_NATIVE_ENDIAN
,
1017 bool memory_region_access_valid(MemoryRegion
*mr
,
1022 int access_size_min
, access_size_max
;
1025 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
1029 if (!mr
->ops
->valid
.accepts
) {
1033 access_size_min
= mr
->ops
->valid
.min_access_size
;
1034 if (!mr
->ops
->valid
.min_access_size
) {
1035 access_size_min
= 1;
1038 access_size_max
= mr
->ops
->valid
.max_access_size
;
1039 if (!mr
->ops
->valid
.max_access_size
) {
1040 access_size_max
= 4;
1043 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
1044 for (i
= 0; i
< size
; i
+= access_size
) {
1045 if (!mr
->ops
->valid
.accepts(mr
->opaque
, addr
+ i
, access_size
,
1054 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
1060 if (mr
->ops
->read
) {
1061 access_with_adjusted_size(addr
, &data
, size
,
1062 mr
->ops
->impl
.min_access_size
,
1063 mr
->ops
->impl
.max_access_size
,
1064 memory_region_read_accessor
, mr
);
1066 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1067 memory_region_oldmmio_read_accessor
, mr
);
1073 static bool memory_region_dispatch_read(MemoryRegion
*mr
,
1078 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
1079 *pval
= unassigned_mem_read(mr
, addr
, size
);
1083 *pval
= memory_region_dispatch_read1(mr
, addr
, size
);
1084 adjust_endianness(mr
, pval
, size
);
1088 static bool memory_region_dispatch_write(MemoryRegion
*mr
,
1093 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
1094 unassigned_mem_write(mr
, addr
, data
, size
);
1098 adjust_endianness(mr
, &data
, size
);
1100 if (mr
->ops
->write
) {
1101 access_with_adjusted_size(addr
, &data
, size
,
1102 mr
->ops
->impl
.min_access_size
,
1103 mr
->ops
->impl
.max_access_size
,
1104 memory_region_write_accessor
, mr
);
1106 access_with_adjusted_size(addr
, &data
, size
, 1, 4,
1107 memory_region_oldmmio_write_accessor
, mr
);
1112 void memory_region_init_io(MemoryRegion
*mr
,
1114 const MemoryRegionOps
*ops
,
1119 memory_region_init(mr
, owner
, name
, size
);
1121 mr
->opaque
= opaque
;
1122 mr
->terminates
= true;
1123 mr
->ram_addr
= ~(ram_addr_t
)0;
1126 void memory_region_init_ram(MemoryRegion
*mr
,
1131 memory_region_init(mr
, owner
, name
, size
);
1133 mr
->terminates
= true;
1134 mr
->destructor
= memory_region_destructor_ram
;
1135 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1139 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1140 struct Object
*owner
,
1147 memory_region_init(mr
, owner
, name
, size
);
1149 mr
->terminates
= true;
1150 mr
->destructor
= memory_region_destructor_ram
;
1151 mr
->ram_addr
= qemu_ram_alloc_from_file(size
, mr
, share
, path
, errp
);
1155 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1161 memory_region_init(mr
, owner
, name
, size
);
1163 mr
->terminates
= true;
1164 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1165 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
1168 void memory_region_init_alias(MemoryRegion
*mr
,
1175 memory_region_init(mr
, owner
, name
, size
);
1176 memory_region_ref(orig
);
1177 mr
->destructor
= memory_region_destructor_alias
;
1179 mr
->alias_offset
= offset
;
1182 void memory_region_init_rom_device(MemoryRegion
*mr
,
1184 const MemoryRegionOps
*ops
,
1189 memory_region_init(mr
, owner
, name
, size
);
1191 mr
->opaque
= opaque
;
1192 mr
->terminates
= true;
1193 mr
->rom_device
= true;
1194 mr
->destructor
= memory_region_destructor_rom_device
;
1195 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1198 void memory_region_init_iommu(MemoryRegion
*mr
,
1200 const MemoryRegionIOMMUOps
*ops
,
1204 memory_region_init(mr
, owner
, name
, size
);
1205 mr
->iommu_ops
= ops
,
1206 mr
->terminates
= true; /* then re-forwards */
1207 notifier_list_init(&mr
->iommu_notify
);
1210 void memory_region_init_reservation(MemoryRegion
*mr
,
1215 memory_region_init_io(mr
, owner
, &unassigned_mem_ops
, mr
, name
, size
);
1218 static void memory_region_finalize(Object
*obj
)
1220 MemoryRegion
*mr
= MEMORY_REGION(obj
);
1222 assert(QTAILQ_EMPTY(&mr
->subregions
));
1223 assert(memory_region_transaction_depth
== 0);
1225 memory_region_clear_coalescing(mr
);
1226 g_free((char *)mr
->name
);
1227 g_free(mr
->ioeventfds
);
1230 void memory_region_destroy(MemoryRegion
*mr
)
1232 object_unparent(OBJECT(mr
));
1236 Object
*memory_region_owner(MemoryRegion
*mr
)
1238 Object
*obj
= OBJECT(mr
);
1242 void memory_region_ref(MemoryRegion
*mr
)
1244 /* MMIO callbacks most likely will access data that belongs
1245 * to the owner, hence the need to ref/unref the owner whenever
1246 * the memory region is in use.
1248 * The memory region is a child of its owner. As long as the
1249 * owner doesn't call unparent itself on the memory region,
1250 * ref-ing the owner will also keep the memory region alive.
1251 * Memory regions without an owner are supposed to never go away,
1252 * but we still ref/unref them for debugging purposes.
1254 Object
*obj
= OBJECT(mr
);
1255 if (obj
&& obj
->parent
) {
1256 object_ref(obj
->parent
);
1262 void memory_region_unref(MemoryRegion
*mr
)
1264 Object
*obj
= OBJECT(mr
);
1265 if (obj
&& obj
->parent
) {
1266 object_unref(obj
->parent
);
1272 uint64_t memory_region_size(MemoryRegion
*mr
)
1274 if (int128_eq(mr
->size
, int128_2_64())) {
1277 return int128_get64(mr
->size
);
1280 const char *memory_region_name(MemoryRegion
*mr
)
1285 bool memory_region_is_ram(MemoryRegion
*mr
)
1290 bool memory_region_is_logging(MemoryRegion
*mr
)
1292 return mr
->dirty_log_mask
;
1295 bool memory_region_is_rom(MemoryRegion
*mr
)
1297 return mr
->ram
&& mr
->readonly
;
1300 bool memory_region_is_iommu(MemoryRegion
*mr
)
1302 return mr
->iommu_ops
;
1305 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
)
1307 notifier_list_add(&mr
->iommu_notify
, n
);
1310 void memory_region_unregister_iommu_notifier(Notifier
*n
)
1315 void memory_region_notify_iommu(MemoryRegion
*mr
,
1316 IOMMUTLBEntry entry
)
1318 assert(memory_region_is_iommu(mr
));
1319 notifier_list_notify(&mr
->iommu_notify
, &entry
);
1322 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1324 uint8_t mask
= 1 << client
;
1326 memory_region_transaction_begin();
1327 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1328 memory_region_update_pending
|= mr
->enabled
;
1329 memory_region_transaction_commit();
1332 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1333 hwaddr size
, unsigned client
)
1335 assert(mr
->terminates
);
1336 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1339 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1342 assert(mr
->terminates
);
1343 cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
);
1346 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1347 hwaddr size
, unsigned client
)
1350 assert(mr
->terminates
);
1351 ret
= cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
, client
);
1353 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1359 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1364 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1365 FlatView
*view
= address_space_get_flatview(as
);
1366 FOR_EACH_FLAT_RANGE(fr
, view
) {
1368 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1371 flatview_unref(view
);
1375 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1377 if (mr
->readonly
!= readonly
) {
1378 memory_region_transaction_begin();
1379 mr
->readonly
= readonly
;
1380 memory_region_update_pending
|= mr
->enabled
;
1381 memory_region_transaction_commit();
1385 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
)
1387 if (mr
->romd_mode
!= romd_mode
) {
1388 memory_region_transaction_begin();
1389 mr
->romd_mode
= romd_mode
;
1390 memory_region_update_pending
|= mr
->enabled
;
1391 memory_region_transaction_commit();
1395 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1396 hwaddr size
, unsigned client
)
1398 assert(mr
->terminates
);
1399 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
, size
, client
);
1402 int memory_region_get_fd(MemoryRegion
*mr
)
1405 return memory_region_get_fd(mr
->alias
);
1408 assert(mr
->terminates
);
1410 return qemu_get_ram_fd(mr
->ram_addr
& TARGET_PAGE_MASK
);
1413 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1416 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1419 assert(mr
->terminates
);
1421 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1424 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1428 CoalescedMemoryRange
*cmr
;
1430 MemoryRegionSection section
;
1432 view
= address_space_get_flatview(as
);
1433 FOR_EACH_FLAT_RANGE(fr
, view
) {
1435 section
= (MemoryRegionSection
) {
1436 .address_space
= as
,
1437 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1438 .size
= fr
->addr
.size
,
1441 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1442 int128_get64(fr
->addr
.start
),
1443 int128_get64(fr
->addr
.size
));
1444 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1445 tmp
= addrrange_shift(cmr
->addr
,
1446 int128_sub(fr
->addr
.start
,
1447 int128_make64(fr
->offset_in_region
)));
1448 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1451 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1452 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1453 int128_get64(tmp
.start
),
1454 int128_get64(tmp
.size
));
1458 flatview_unref(view
);
1461 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1465 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1466 memory_region_update_coalesced_range_as(mr
, as
);
1470 void memory_region_set_coalescing(MemoryRegion
*mr
)
1472 memory_region_clear_coalescing(mr
);
1473 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1476 void memory_region_add_coalescing(MemoryRegion
*mr
,
1480 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1482 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1483 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1484 memory_region_update_coalesced_range(mr
);
1485 memory_region_set_flush_coalesced(mr
);
1488 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1490 CoalescedMemoryRange
*cmr
;
1491 bool updated
= false;
1493 qemu_flush_coalesced_mmio_buffer();
1494 mr
->flush_coalesced_mmio
= false;
1496 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1497 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1498 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1504 memory_region_update_coalesced_range(mr
);
1508 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1510 mr
->flush_coalesced_mmio
= true;
1513 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1515 qemu_flush_coalesced_mmio_buffer();
1516 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1517 mr
->flush_coalesced_mmio
= false;
1521 void memory_region_add_eventfd(MemoryRegion
*mr
,
1528 MemoryRegionIoeventfd mrfd
= {
1529 .addr
.start
= int128_make64(addr
),
1530 .addr
.size
= int128_make64(size
),
1531 .match_data
= match_data
,
1537 adjust_endianness(mr
, &mrfd
.data
, size
);
1538 memory_region_transaction_begin();
1539 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1540 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1545 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1546 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1547 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1548 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1549 mr
->ioeventfds
[i
] = mrfd
;
1550 ioeventfd_update_pending
|= mr
->enabled
;
1551 memory_region_transaction_commit();
1554 void memory_region_del_eventfd(MemoryRegion
*mr
,
1561 MemoryRegionIoeventfd mrfd
= {
1562 .addr
.start
= int128_make64(addr
),
1563 .addr
.size
= int128_make64(size
),
1564 .match_data
= match_data
,
1570 adjust_endianness(mr
, &mrfd
.data
, size
);
1571 memory_region_transaction_begin();
1572 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1573 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1577 assert(i
!= mr
->ioeventfd_nb
);
1578 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1579 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1581 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1582 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1583 ioeventfd_update_pending
|= mr
->enabled
;
1584 memory_region_transaction_commit();
1587 static void memory_region_update_container_subregions(MemoryRegion
*subregion
)
1589 hwaddr offset
= subregion
->addr
;
1590 MemoryRegion
*mr
= subregion
->container
;
1591 MemoryRegion
*other
;
1593 memory_region_transaction_begin();
1595 memory_region_ref(subregion
);
1596 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1597 if (subregion
->may_overlap
|| other
->may_overlap
) {
1600 if (int128_ge(int128_make64(offset
),
1601 int128_add(int128_make64(other
->addr
), other
->size
))
1602 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1603 int128_make64(other
->addr
))) {
1607 printf("warning: subregion collision %llx/%llx (%s) "
1608 "vs %llx/%llx (%s)\n",
1609 (unsigned long long)offset
,
1610 (unsigned long long)int128_get64(subregion
->size
),
1612 (unsigned long long)other
->addr
,
1613 (unsigned long long)int128_get64(other
->size
),
1617 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1618 if (subregion
->priority
>= other
->priority
) {
1619 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1623 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1625 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1626 memory_region_transaction_commit();
1629 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1631 MemoryRegion
*subregion
)
1633 assert(!subregion
->container
);
1634 subregion
->container
= mr
;
1635 subregion
->addr
= offset
;
1636 memory_region_update_container_subregions(subregion
);
1639 void memory_region_add_subregion(MemoryRegion
*mr
,
1641 MemoryRegion
*subregion
)
1643 subregion
->may_overlap
= false;
1644 subregion
->priority
= 0;
1645 memory_region_add_subregion_common(mr
, offset
, subregion
);
1648 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1650 MemoryRegion
*subregion
,
1653 subregion
->may_overlap
= true;
1654 subregion
->priority
= priority
;
1655 memory_region_add_subregion_common(mr
, offset
, subregion
);
1658 void memory_region_del_subregion(MemoryRegion
*mr
,
1659 MemoryRegion
*subregion
)
1661 memory_region_transaction_begin();
1662 assert(subregion
->container
== mr
);
1663 subregion
->container
= NULL
;
1664 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1665 memory_region_unref(subregion
);
1666 memory_region_update_pending
|= mr
->enabled
&& subregion
->enabled
;
1667 memory_region_transaction_commit();
1670 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1672 if (enabled
== mr
->enabled
) {
1675 memory_region_transaction_begin();
1676 mr
->enabled
= enabled
;
1677 memory_region_update_pending
= true;
1678 memory_region_transaction_commit();
1681 static void memory_region_readd_subregion(MemoryRegion
*mr
)
1683 MemoryRegion
*container
= mr
->container
;
1686 memory_region_transaction_begin();
1687 memory_region_ref(mr
);
1688 memory_region_del_subregion(container
, mr
);
1689 mr
->container
= container
;
1690 memory_region_update_container_subregions(mr
);
1691 memory_region_unref(mr
);
1692 memory_region_transaction_commit();
1696 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1698 if (addr
!= mr
->addr
) {
1700 memory_region_readd_subregion(mr
);
1704 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1708 if (offset
== mr
->alias_offset
) {
1712 memory_region_transaction_begin();
1713 mr
->alias_offset
= offset
;
1714 memory_region_update_pending
|= mr
->enabled
;
1715 memory_region_transaction_commit();
1718 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1720 return mr
->ram_addr
;
1723 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1725 const AddrRange
*addr
= addr_
;
1726 const FlatRange
*fr
= fr_
;
1728 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1730 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1736 static FlatRange
*flatview_lookup(FlatView
*view
, AddrRange addr
)
1738 return bsearch(&addr
, view
->ranges
, view
->nr
,
1739 sizeof(FlatRange
), cmp_flatrange_addr
);
1742 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
)
1744 MemoryRegion
*mr
= memory_region_find(container
, addr
, 1).mr
;
1745 if (!mr
|| (mr
== container
)) {
1748 memory_region_unref(mr
);
1752 bool memory_region_is_mapped(MemoryRegion
*mr
)
1754 return mr
->container
? true : false;
1757 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1758 hwaddr addr
, uint64_t size
)
1760 MemoryRegionSection ret
= { .mr
= NULL
};
1768 for (root
= mr
; root
->container
; ) {
1769 root
= root
->container
;
1773 as
= memory_region_to_address_space(root
);
1777 range
= addrrange_make(int128_make64(addr
), int128_make64(size
));
1779 view
= address_space_get_flatview(as
);
1780 fr
= flatview_lookup(view
, range
);
1782 flatview_unref(view
);
1786 while (fr
> view
->ranges
&& addrrange_intersects(fr
[-1].addr
, range
)) {
1791 ret
.address_space
= as
;
1792 range
= addrrange_intersection(range
, fr
->addr
);
1793 ret
.offset_within_region
= fr
->offset_in_region
;
1794 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1796 ret
.size
= range
.size
;
1797 ret
.offset_within_address_space
= int128_get64(range
.start
);
1798 ret
.readonly
= fr
->readonly
;
1799 memory_region_ref(ret
.mr
);
1801 flatview_unref(view
);
1805 void address_space_sync_dirty_bitmap(AddressSpace
*as
)
1810 view
= address_space_get_flatview(as
);
1811 FOR_EACH_FLAT_RANGE(fr
, view
) {
1812 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1814 flatview_unref(view
);
1817 void memory_global_dirty_log_start(void)
1819 global_dirty_log
= true;
1820 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1823 void memory_global_dirty_log_stop(void)
1825 global_dirty_log
= false;
1826 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1829 static void listener_add_address_space(MemoryListener
*listener
,
1835 if (listener
->address_space_filter
1836 && listener
->address_space_filter
!= as
) {
1840 if (global_dirty_log
) {
1841 if (listener
->log_global_start
) {
1842 listener
->log_global_start(listener
);
1846 view
= address_space_get_flatview(as
);
1847 FOR_EACH_FLAT_RANGE(fr
, view
) {
1848 MemoryRegionSection section
= {
1850 .address_space
= as
,
1851 .offset_within_region
= fr
->offset_in_region
,
1852 .size
= fr
->addr
.size
,
1853 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1854 .readonly
= fr
->readonly
,
1856 if (listener
->region_add
) {
1857 listener
->region_add(listener
, §ion
);
1860 flatview_unref(view
);
1863 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1865 MemoryListener
*other
= NULL
;
1868 listener
->address_space_filter
= filter
;
1869 if (QTAILQ_EMPTY(&memory_listeners
)
1870 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1871 memory_listeners
)->priority
) {
1872 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1874 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1875 if (listener
->priority
< other
->priority
) {
1879 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1882 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1883 listener_add_address_space(listener
, as
);
1887 void memory_listener_unregister(MemoryListener
*listener
)
1889 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1892 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
)
1894 if (QTAILQ_EMPTY(&address_spaces
)) {
1898 memory_region_transaction_begin();
1900 as
->current_map
= g_new(FlatView
, 1);
1901 flatview_init(as
->current_map
);
1902 as
->ioeventfd_nb
= 0;
1903 as
->ioeventfds
= NULL
;
1904 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1905 as
->name
= g_strdup(name
? name
: "anonymous");
1906 address_space_init_dispatch(as
);
1907 memory_region_update_pending
|= root
->enabled
;
1908 memory_region_transaction_commit();
1911 void address_space_destroy(AddressSpace
*as
)
1913 MemoryListener
*listener
;
1915 /* Flush out anything from MemoryListeners listening in on this */
1916 memory_region_transaction_begin();
1918 memory_region_transaction_commit();
1919 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1920 address_space_destroy_dispatch(as
);
1922 QTAILQ_FOREACH(listener
, &memory_listeners
, link
) {
1923 assert(listener
->address_space_filter
!= as
);
1926 flatview_unref(as
->current_map
);
1928 g_free(as
->ioeventfds
);
1931 bool io_mem_read(MemoryRegion
*mr
, hwaddr addr
, uint64_t *pval
, unsigned size
)
1933 return memory_region_dispatch_read(mr
, addr
, pval
, size
);
1936 bool io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1937 uint64_t val
, unsigned size
)
1939 return memory_region_dispatch_write(mr
, addr
, val
, size
);
1942 typedef struct MemoryRegionList MemoryRegionList
;
1944 struct MemoryRegionList
{
1945 const MemoryRegion
*mr
;
1947 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1950 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1952 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1953 const MemoryRegion
*mr
, unsigned int level
,
1955 MemoryRegionListHead
*alias_print_queue
)
1957 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1958 MemoryRegionListHead submr_print_queue
;
1959 const MemoryRegion
*submr
;
1962 if (!mr
|| !mr
->enabled
) {
1966 for (i
= 0; i
< level
; i
++) {
1971 MemoryRegionList
*ml
;
1974 /* check if the alias is already in the queue */
1975 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1976 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1982 ml
= g_new(MemoryRegionList
, 1);
1984 ml
->printed
= false;
1985 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1987 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1988 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1989 "-" TARGET_FMT_plx
"\n",
1992 + (int128_nz(mr
->size
) ?
1993 (hwaddr
)int128_get64(int128_sub(mr
->size
,
1994 int128_one())) : 0),
1996 mr
->romd_mode
? 'R' : '-',
1997 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2003 + (int128_nz(mr
->size
) ?
2004 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2005 int128_one())) : 0));
2008 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
2011 + (int128_nz(mr
->size
) ?
2012 (hwaddr
)int128_get64(int128_sub(mr
->size
,
2013 int128_one())) : 0),
2015 mr
->romd_mode
? 'R' : '-',
2016 !mr
->readonly
&& !(mr
->rom_device
&& mr
->romd_mode
) ? 'W'
2021 QTAILQ_INIT(&submr_print_queue
);
2023 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
2024 new_ml
= g_new(MemoryRegionList
, 1);
2026 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2027 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
2028 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
2029 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
2030 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
2036 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
2040 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
2041 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
2045 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
2050 void mtree_info(fprintf_function mon_printf
, void *f
)
2052 MemoryRegionListHead ml_head
;
2053 MemoryRegionList
*ml
, *ml2
;
2056 QTAILQ_INIT(&ml_head
);
2058 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
2059 mon_printf(f
, "%s\n", as
->name
);
2060 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
2063 mon_printf(f
, "aliases\n");
2064 /* print aliased regions */
2065 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
2067 mon_printf(f
, "%s\n", ml
->mr
->name
);
2068 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
2072 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
2077 static const TypeInfo memory_region_info
= {
2078 .parent
= TYPE_OBJECT
,
2079 .name
= TYPE_MEMORY_REGION
,
2080 .instance_size
= sizeof(MemoryRegion
),
2081 .instance_init
= memory_region_initfn
,
2082 .instance_finalize
= memory_region_finalize
,
2085 static void memory_register_types(void)
2087 type_register_static(&memory_region_info
);
2090 type_init(memory_register_types
)