2 * Physical memory management
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "exec/memory.h"
18 #include "exec/address-spaces.h"
19 #include "exec/ioport.h"
20 #include "qapi/visitor.h"
21 #include "qemu/bitops.h"
22 #include "qemu/error-report.h"
23 #include "qom/object.h"
26 #include "exec/memory-internal.h"
27 #include "exec/ram_addr.h"
28 #include "sysemu/kvm.h"
29 #include "sysemu/sysemu.h"
31 //#define DEBUG_UNASSIGNED
33 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
35 static unsigned memory_region_transaction_depth;
36 static bool memory_region_update_pending;
37 static bool ioeventfd_update_pending;
38 static bool global_dirty_log = false;
40 static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners
41 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
43 static QTAILQ_HEAD(, AddressSpace) address_spaces
44 = QTAILQ_HEAD_INITIALIZER(address_spaces);
46 typedef struct AddrRange AddrRange;
49 * Note that signed integers are needed for negative offsetting in aliases
50 * (large MemoryRegion::alias_offset).
57 static AddrRange addrrange_make(Int128 start, Int128 size)
59 return (AddrRange) { start, size };
62 static bool addrrange_equal(AddrRange r1, AddrRange r2)
64 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
67 static Int128 addrrange_end(AddrRange r)
69 return int128_add(r.start, r.size);
72 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
74 int128_addto(&range.start, delta);
78 static bool addrrange_contains(AddrRange range, Int128 addr)
80 return int128_ge(addr, range.start)
81 && int128_lt(addr, addrrange_end(range));
84 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
86 return addrrange_contains(r1, r2.start)
87 || addrrange_contains(r2, r1.start);
90 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
92 Int128 start = int128_max(r1.start, r2.start);
93 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
94 return addrrange_make(start, int128_sub(end, start));
97 enum ListenerDirection { Forward, Reverse };
99 static bool memory_listener_match(MemoryListener *listener,
100 MemoryRegionSection *section)
102 return !listener->address_space_filter
103 || listener->address_space_filter == section->address_space;
106 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
108 MemoryListener *_listener; \
110 switch (_direction) { \
112 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
113 if (_listener->_callback) { \
114 _listener->_callback(_listener, ##_args); \
119 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
120 memory_listeners, link) { \
121 if (_listener->_callback) { \
122 _listener->_callback(_listener, ##_args); \
131 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
133 MemoryListener *_listener; \
135 switch (_direction) { \
137 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
138 if (_listener->_callback \
139 && memory_listener_match(_listener, _section)) { \
140 _listener->_callback(_listener, _section, ##_args); \
145 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
146 memory_listeners, link) { \
147 if (_listener->_callback \
148 && memory_listener_match(_listener, _section)) { \
149 _listener->_callback(_listener, _section, ##_args); \
158 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
159 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
160 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
162 .address_space = (as), \
163 .offset_within_region = (fr)->offset_in_region, \
164 .size = (fr)->addr.size, \
165 .offset_within_address_space = int128_get64((fr)->addr.start), \
166 .readonly = (fr)->readonly, \
169 struct CoalescedMemoryRange {
171 QTAILQ_ENTRY(CoalescedMemoryRange) link;
174 struct MemoryRegionIoeventfd {
181 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
182 MemoryRegionIoeventfd b)
184 if (int128_lt(a.addr.start, b.addr.start)) {
186 } else if (int128_gt(a.addr.start, b.addr.start)) {
188 } else if (int128_lt(a.addr.size, b.addr.size)) {
190 } else if (int128_gt(a.addr.size, b.addr.size)) {
192 } else if (a.match_data < b.match_data) {
194 } else if (a.match_data > b.match_data) {
196 } else if (a.match_data) {
197 if (a.data < b.data) {
199 } else if (a.data > b.data) {
205 } else if (a.e > b.e) {
211 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
212 MemoryRegionIoeventfd b)
214 return !memory_region_ioeventfd_before(a, b)
215 && !memory_region_ioeventfd_before(b, a);
218 typedef struct FlatRange FlatRange;
219 typedef struct FlatView FlatView;
221 /* Range of memory in the global map. Addresses are absolute. */
224 hwaddr offset_in_region;
226 uint8_t dirty_log_mask;
231 /* Flattened global view of current active memory hierarchy. Kept in sorted
239 unsigned nr_allocated;
242 typedef struct AddressSpaceOps AddressSpaceOps;
244 #define FOR_EACH_FLAT_RANGE(var, view) \
245 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
247 static bool flatrange_equal(FlatRange *a, FlatRange *b)
249 return a->mr == b->mr
250 && addrrange_equal(a->addr, b->addr)
251 && a->offset_in_region == b->offset_in_region
252 && a->romd_mode == b->romd_mode
253 && a->readonly == b->readonly;
256 static void flatview_init(FlatView *view)
261 view->nr_allocated = 0;
264 /* Insert a range into a given position. Caller is responsible for maintaining
267 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
269 if (view->nr == view->nr_allocated) {
270 view->nr_allocated = MAX(2 * view->nr, 10);
271 view->ranges = g_realloc(view->ranges,
272 view->nr_allocated * sizeof(*view->ranges));
274 memmove(view->ranges + pos + 1, view->ranges + pos,
275 (view->nr - pos) * sizeof(FlatRange));
276 view->ranges[pos] = *range;
277 memory_region_ref(range->mr);
281 static void flatview_destroy(FlatView *view)
285 for (i = 0; i < view->nr; i++) {
286 memory_region_unref(view->ranges[i].mr);
288 g_free(view->ranges);
292 static void flatview_ref(FlatView *view)
294 atomic_inc(&view->ref);
297 static void flatview_unref(FlatView *view)
299 if (atomic_fetch_dec(&view->ref) == 1) {
300 flatview_destroy(view);
304 static bool can_merge(FlatRange *r1, FlatRange *r2)
306 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
308 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
310 int128_make64(r2->offset_in_region))
311 && r1->dirty_log_mask == r2->dirty_log_mask
312 && r1->romd_mode == r2->romd_mode
313 && r1->readonly == r2->readonly;
316 /* Attempt to simplify a view by merging adjacent ranges */
317 static void flatview_simplify(FlatView *view)
322 while (i < view->nr) {
325 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
326 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
330 memmove(&view->ranges[i], &view->ranges[j],
331 (view->nr - j) * sizeof(view->ranges[j]));
336 static bool memory_region_big_endian(MemoryRegion *mr)
338 #ifdef TARGET_WORDS_BIGENDIAN
339 return mr->ops->endianness != DEVICE_LITTLE_ENDIAN;
341 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
345 static bool memory_region_wrong_endianness(MemoryRegion *mr)
347 #ifdef TARGET_WORDS_BIGENDIAN
348 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
350 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
354 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
356 if (memory_region_wrong_endianness(mr)) {
361 *data = bswap16(*data);
364 *data = bswap32(*data);
367 *data = bswap64(*data);
375 static hwaddr memory_region_to_absolute_addr(MemoryRegion *mr, hwaddr offset)
378 hwaddr abs_addr = offset;
380 abs_addr += mr->addr;
381 for (root = mr; root->container; ) {
382 root = root->container;
383 abs_addr += root->addr;
389 static MemTxResult memory_region_oldmmio_read_accessor(MemoryRegion *mr,
399 tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);
401 trace_memory_region_subpage_read(mr, addr, tmp, size);
402 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
403 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
404 trace_memory_region_ops_read(mr, abs_addr, tmp, size);
406 *value |= (tmp & mask) << shift;
410 static MemTxResult memory_region_read_accessor(MemoryRegion *mr,
420 tmp = mr->ops->read(mr->opaque, addr, size);
422 trace_memory_region_subpage_read(mr, addr, tmp, size);
423 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
424 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
425 trace_memory_region_ops_read(mr, abs_addr, tmp, size);
427 *value |= (tmp & mask) << shift;
431 static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr,
442 r = mr->ops->read_with_attrs(mr->opaque, addr, &tmp, size, attrs);
444 trace_memory_region_subpage_read(mr, addr, tmp, size);
445 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
446 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
447 trace_memory_region_ops_read(mr, abs_addr, tmp, size);
449 *value |= (tmp & mask) << shift;
453 static MemTxResult memory_region_oldmmio_write_accessor(MemoryRegion *mr,
463 tmp = (*value >> shift) & mask;
465 trace_memory_region_subpage_write(mr, addr, tmp, size);
466 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
467 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
468 trace_memory_region_ops_write(mr, abs_addr, tmp, size);
470 mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp);
474 static MemTxResult memory_region_write_accessor(MemoryRegion *mr,
484 tmp = (*value >> shift) & mask;
486 trace_memory_region_subpage_write(mr, addr, tmp, size);
487 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
488 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
489 trace_memory_region_ops_write(mr, abs_addr, tmp, size);
491 mr->ops->write(mr->opaque, addr, tmp, size);
495 static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr,
505 tmp = (*value >> shift) & mask;
507 trace_memory_region_subpage_write(mr, addr, tmp, size);
508 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
509 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
510 trace_memory_region_ops_write(mr, abs_addr, tmp, size);
512 return mr->ops->write_with_attrs(mr->opaque, addr, tmp, size, attrs);
515 static MemTxResult access_with_adjusted_size(hwaddr addr,
518 unsigned access_size_min,
519 unsigned access_size_max,
520 MemTxResult (*access)(MemoryRegion *mr,
530 uint64_t access_mask;
531 unsigned access_size;
533 MemTxResult r = MEMTX_OK;
535 if (!access_size_min) {
538 if (!access_size_max) {
542 /* FIXME: support unaligned access? */
543 access_size = MAX(MIN(size, access_size_max), access_size_min);
544 access_mask = -1ULL >> (64 - access_size * 8);
545 if (memory_region_big_endian(mr)) {
546 for (i = 0; i < size; i += access_size) {
547 r |= access(mr, addr + i, value, access_size,
548 (size - access_size - i) * 8, access_mask, attrs);
551 for (i = 0; i < size; i += access_size) {
552 r |= access(mr, addr + i, value, access_size, i * 8,
559 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
563 while (mr->container) {
566 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
567 if (mr == as->root) {
574 /* Render a memory region into the global view. Ranges in @view obscure
577 static void render_memory_region(FlatView *view,
583 MemoryRegion *subregion;
585 hwaddr offset_in_region;
595 int128_addto(&base, int128_make64(mr->addr));
596 readonly |= mr->readonly;
598 tmp = addrrange_make(base, mr->size);
600 if (!addrrange_intersects(tmp, clip)) {
604 clip = addrrange_intersection(tmp, clip);
607 int128_subfrom(&base, int128_make64(mr->alias->addr));
608 int128_subfrom(&base, int128_make64(mr->alias_offset));
609 render_memory_region(view, mr->alias, base, clip, readonly);
613 /* Render subregions in priority order. */
614 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
615 render_memory_region(view, subregion, base, clip, readonly);
618 if (!mr->terminates) {
622 offset_in_region = int128_get64(int128_sub(clip.start, base));
627 fr.dirty_log_mask = memory_region_get_dirty_log_mask(mr);
628 fr.romd_mode = mr->romd_mode;
629 fr.readonly = readonly;
631 /* Render the region itself into any gaps left by the current view. */
632 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
633 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
636 if (int128_lt(base, view->ranges[i].addr.start)) {
637 now = int128_min(remain,
638 int128_sub(view->ranges[i].addr.start, base));
639 fr.offset_in_region = offset_in_region;
640 fr.addr = addrrange_make(base, now);
641 flatview_insert(view, i, &fr);
643 int128_addto(&base, now);
644 offset_in_region += int128_get64(now);
645 int128_subfrom(&remain, now);
647 now = int128_sub(int128_min(int128_add(base, remain),
648 addrrange_end(view->ranges[i].addr)),
650 int128_addto(&base, now);
651 offset_in_region += int128_get64(now);
652 int128_subfrom(&remain, now);
654 if (int128_nz(remain)) {
655 fr.offset_in_region = offset_in_region;
656 fr.addr = addrrange_make(base, remain);
657 flatview_insert(view, i, &fr);
661 /* Render a memory topology into a list of disjoint absolute ranges. */
662 static FlatView *generate_memory_topology(MemoryRegion *mr)
666 view = g_new(FlatView, 1);
670 render_memory_region(view, mr, int128_zero(),
671 addrrange_make(int128_zero(), int128_2_64()), false);
673 flatview_simplify(view);
678 static void address_space_add_del_ioeventfds(AddressSpace *as,
679 MemoryRegionIoeventfd *fds_new,
681 MemoryRegionIoeventfd *fds_old,
685 MemoryRegionIoeventfd *fd;
686 MemoryRegionSection section;
688 /* Generate a symmetric difference of the old and new fd sets, adding
689 * and deleting as necessary.
693 while (iold < fds_old_nb || inew < fds_new_nb) {
694 if (iold < fds_old_nb
695 && (inew == fds_new_nb
696 || memory_region_ioeventfd_before(fds_old[iold],
699 section = (MemoryRegionSection) {
701 .offset_within_address_space = int128_get64(fd->addr.start),
702 .size = fd->addr.size,
704 MEMORY_LISTENER_CALL(eventfd_del, Forward, §ion,
705 fd->match_data, fd->data, fd->e);
707 } else if (inew < fds_new_nb
708 && (iold == fds_old_nb
709 || memory_region_ioeventfd_before(fds_new[inew],
712 section = (MemoryRegionSection) {
714 .offset_within_address_space = int128_get64(fd->addr.start),
715 .size = fd->addr.size,
717 MEMORY_LISTENER_CALL(eventfd_add, Reverse, §ion,
718 fd->match_data, fd->data, fd->e);
727 static FlatView *address_space_get_flatview(AddressSpace *as)
732 view = atomic_rcu_read(&as->current_map);
738 static void address_space_update_ioeventfds(AddressSpace *as)
742 unsigned ioeventfd_nb = 0;
743 MemoryRegionIoeventfd *ioeventfds = NULL;
747 view = address_space_get_flatview(as);
748 FOR_EACH_FLAT_RANGE(fr, view) {
749 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
750 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
751 int128_sub(fr->addr.start,
752 int128_make64(fr->offset_in_region)));
753 if (addrrange_intersects(fr->addr, tmp)) {
755 ioeventfds = g_realloc(ioeventfds,
756 ioeventfd_nb * sizeof(*ioeventfds));
757 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
758 ioeventfds[ioeventfd_nb-1].addr = tmp;
763 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
764 as->ioeventfds, as->ioeventfd_nb);
766 g_free(as->ioeventfds);
767 as->ioeventfds = ioeventfds;
768 as->ioeventfd_nb = ioeventfd_nb;
769 flatview_unref(view);
772 static void address_space_update_topology_pass(AddressSpace *as,
773 const FlatView *old_view,
774 const FlatView *new_view,
778 FlatRange *frold, *frnew;
780 /* Generate a symmetric difference of the old and new memory maps.
781 * Kill ranges in the old map, and instantiate ranges in the new map.
784 while (iold < old_view->nr || inew < new_view->nr) {
785 if (iold < old_view->nr) {
786 frold = &old_view->ranges[iold];
790 if (inew < new_view->nr) {
791 frnew = &new_view->ranges[inew];
798 || int128_lt(frold->addr.start, frnew->addr.start)
799 || (int128_eq(frold->addr.start, frnew->addr.start)
800 && !flatrange_equal(frold, frnew)))) {
801 /* In old but not in new, or in both but attributes changed. */
804 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
808 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
809 /* In both and unchanged (except logging may have changed) */
812 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
813 if (frnew->dirty_log_mask & ~frold->dirty_log_mask) {
814 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start,
815 frold->dirty_log_mask,
816 frnew->dirty_log_mask);
818 if (frold->dirty_log_mask & ~frnew->dirty_log_mask) {
819 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop,
820 frold->dirty_log_mask,
821 frnew->dirty_log_mask);
831 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
840 static void address_space_update_topology(AddressSpace *as)
842 FlatView *old_view = address_space_get_flatview(as);
843 FlatView *new_view = generate_memory_topology(as->root);
845 address_space_update_topology_pass(as, old_view, new_view, false);
846 address_space_update_topology_pass(as, old_view, new_view, true);
848 /* Writes are protected by the BQL. */
849 atomic_rcu_set(&as->current_map, new_view);
850 call_rcu(old_view, flatview_unref, rcu);
852 /* Note that all the old MemoryRegions are still alive up to this
853 * point. This relieves most MemoryListeners from the need to
854 * ref/unref the MemoryRegions they get---unless they use them
855 * outside the iothread mutex, in which case precise reference
856 * counting is necessary.
858 flatview_unref(old_view);
860 address_space_update_ioeventfds(as);
863 void memory_region_transaction_begin(void)
865 qemu_flush_coalesced_mmio_buffer();
866 ++memory_region_transaction_depth;
869 static void memory_region_clear_pending(void)
871 memory_region_update_pending = false;
872 ioeventfd_update_pending = false;
875 void memory_region_transaction_commit(void)
879 assert(memory_region_transaction_depth);
880 --memory_region_transaction_depth;
881 if (!memory_region_transaction_depth) {
882 if (memory_region_update_pending) {
883 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
885 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
886 address_space_update_topology(as);
889 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
890 } else if (ioeventfd_update_pending) {
891 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
892 address_space_update_ioeventfds(as);
895 memory_region_clear_pending();
899 static void memory_region_destructor_none(MemoryRegion *mr)
903 static void memory_region_destructor_ram(MemoryRegion *mr)
905 qemu_ram_free(mr->ram_addr);
908 static void memory_region_destructor_rom_device(MemoryRegion *mr)
910 qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
913 static bool memory_region_need_escape(char c)
915 return c == '/' || c == '[' || c == '\\' || c == ']';
918 static char *memory_region_escape_name(const char *name)
925 for (p = name; *p; p++) {
926 bytes += memory_region_need_escape(*p) ? 4 : 1;
928 if (bytes == p - name) {
929 return g_memdup(name, bytes + 1);
932 escaped = g_malloc(bytes + 1);
933 for (p = name, q = escaped; *p; p++) {
935 if (unlikely(memory_region_need_escape(c))) {
938 *q++ = "0123456789abcdef"[c >> 4];
939 c = "0123456789abcdef"[c & 15];
947 void memory_region_init(MemoryRegion *mr,
952 object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION);
953 mr->size = int128_make64(size);
954 if (size == UINT64_MAX) {
955 mr->size = int128_2_64();
957 mr->name = g_strdup(name);
959 mr->ram_block = NULL;
962 char *escaped_name = memory_region_escape_name(name);
963 char *name_array = g_strdup_printf("%s[*]", escaped_name);
966 owner = container_get(qdev_get_machine(), "/unattached");
969 object_property_add_child(owner, name_array, OBJECT(mr), &error_abort);
970 object_unref(OBJECT(mr));
972 g_free(escaped_name);
976 static void memory_region_get_addr(Object *obj, Visitor *v, const char *name,
977 void *opaque, Error **errp)
979 MemoryRegion *mr = MEMORY_REGION(obj);
980 uint64_t value = mr->addr;
982 visit_type_uint64(v, name, &value, errp);
985 static void memory_region_get_container(Object *obj, Visitor *v,
986 const char *name, void *opaque,
989 MemoryRegion *mr = MEMORY_REGION(obj);
990 gchar *path = (gchar *)"";
993 path = object_get_canonical_path(OBJECT(mr->container));
995 visit_type_str(v, name, &path, errp);
1001 static Object *memory_region_resolve_container(Object *obj, void *opaque,
1004 MemoryRegion *mr = MEMORY_REGION(obj);
1006 return OBJECT(mr->container);
1009 static void memory_region_get_priority(Object *obj, Visitor *v,
1010 const char *name, void *opaque,
1013 MemoryRegion *mr = MEMORY_REGION(obj);
1014 int32_t value = mr->priority;
1016 visit_type_int32(v, name, &value, errp);
1019 static bool memory_region_get_may_overlap(Object *obj, Error **errp)
1021 MemoryRegion *mr = MEMORY_REGION(obj);
1023 return mr->may_overlap;
1026 static void memory_region_get_size(Object *obj, Visitor *v, const char *name,
1027 void *opaque, Error **errp)
1029 MemoryRegion *mr = MEMORY_REGION(obj);
1030 uint64_t value = memory_region_size(mr);
1032 visit_type_uint64(v, name, &value, errp);
1035 static void memory_region_initfn(Object *obj)
1037 MemoryRegion *mr = MEMORY_REGION(obj);
1040 mr->ops = &unassigned_mem_ops;
1041 mr->ram_addr = RAM_ADDR_INVALID;
1043 mr->romd_mode = true;
1044 mr->global_locking = true;
1045 mr->destructor = memory_region_destructor_none;
1046 QTAILQ_INIT(&mr->subregions);
1047 QTAILQ_INIT(&mr->coalesced);
1049 op = object_property_add(OBJECT(mr), "container",
1050 "link<" TYPE_MEMORY_REGION ">",
1051 memory_region_get_container,
1052 NULL, /* memory_region_set_container */
1053 NULL, NULL, &error_abort);
1054 op->resolve = memory_region_resolve_container;
1056 object_property_add(OBJECT(mr), "addr", "uint64",
1057 memory_region_get_addr,
1058 NULL, /* memory_region_set_addr */
1059 NULL, NULL, &error_abort);
1060 object_property_add(OBJECT(mr), "priority", "uint32",
1061 memory_region_get_priority,
1062 NULL, /* memory_region_set_priority */
1063 NULL, NULL, &error_abort);
1064 object_property_add_bool(OBJECT(mr), "may-overlap",
1065 memory_region_get_may_overlap,
1066 NULL, /* memory_region_set_may_overlap */
1068 object_property_add(OBJECT(mr), "size", "uint64",
1069 memory_region_get_size,
1070 NULL, /* memory_region_set_size, */
1071 NULL, NULL, &error_abort);
1074 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
1077 #ifdef DEBUG_UNASSIGNED
1078 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
1080 if (current_cpu != NULL) {
1081 cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
1086 static void unassigned_mem_write(void *opaque, hwaddr addr,
1087 uint64_t val, unsigned size)
1089 #ifdef DEBUG_UNASSIGNED
1090 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
1092 if (current_cpu != NULL) {
1093 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
1097 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
1098 unsigned size, bool is_write)
1103 const MemoryRegionOps unassigned_mem_ops = {
1104 .valid.accepts = unassigned_mem_accepts,
1105 .endianness = DEVICE_NATIVE_ENDIAN,
1108 bool memory_region_access_valid(MemoryRegion *mr,
1113 int access_size_min, access_size_max;
1116 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
1120 if (!mr->ops->valid.accepts) {
1124 access_size_min = mr->ops->valid.min_access_size;
1125 if (!mr->ops->valid.min_access_size) {
1126 access_size_min = 1;
1129 access_size_max = mr->ops->valid.max_access_size;
1130 if (!mr->ops->valid.max_access_size) {
1131 access_size_max = 4;
1134 access_size = MAX(MIN(size, access_size_max), access_size_min);
1135 for (i = 0; i < size; i += access_size) {
1136 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
1145 static MemTxResult memory_region_dispatch_read1(MemoryRegion *mr,
1153 if (mr->ops->read) {
1154 return access_with_adjusted_size(addr, pval, size,
1155 mr->ops->impl.min_access_size,
1156 mr->ops->impl.max_access_size,
1157 memory_region_read_accessor,
1159 } else if (mr->ops->read_with_attrs) {
1160 return access_with_adjusted_size(addr, pval, size,
1161 mr->ops->impl.min_access_size,
1162 mr->ops->impl.max_access_size,
1163 memory_region_read_with_attrs_accessor,
1166 return access_with_adjusted_size(addr, pval, size, 1, 4,
1167 memory_region_oldmmio_read_accessor,
1172 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1180 if (!memory_region_access_valid(mr, addr, size, false)) {
1181 *pval = unassigned_mem_read(mr, addr, size);
1182 return MEMTX_DECODE_ERROR;
1185 r = memory_region_dispatch_read1(mr, addr, pval, size, attrs);
1186 adjust_endianness(mr, pval, size);
1190 /* Return true if an eventfd was signalled */
1191 static bool memory_region_dispatch_write_eventfds(MemoryRegion *mr,
1197 MemoryRegionIoeventfd ioeventfd = {
1198 .addr = addrrange_make(int128_make64(addr), int128_make64(size)),
1203 for (i = 0; i < mr->ioeventfd_nb; i++) {
1204 ioeventfd.match_data = mr->ioeventfds[i].match_data;
1205 ioeventfd.e = mr->ioeventfds[i].e;
1207 if (memory_region_ioeventfd_equal(ioeventfd, mr->ioeventfds[i])) {
1208 event_notifier_set(ioeventfd.e);
1216 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1222 if (!memory_region_access_valid(mr, addr, size, true)) {
1223 unassigned_mem_write(mr, addr, data, size);
1224 return MEMTX_DECODE_ERROR;
1227 adjust_endianness(mr, &data, size);
1229 if ((!kvm_eventfds_enabled()) &&
1230 memory_region_dispatch_write_eventfds(mr, addr, data, size, attrs)) {
1234 if (mr->ops->write) {
1235 return access_with_adjusted_size(addr, &data, size,
1236 mr->ops->impl.min_access_size,
1237 mr->ops->impl.max_access_size,
1238 memory_region_write_accessor, mr,
1240 } else if (mr->ops->write_with_attrs) {
1242 access_with_adjusted_size(addr, &data, size,
1243 mr->ops->impl.min_access_size,
1244 mr->ops->impl.max_access_size,
1245 memory_region_write_with_attrs_accessor,
1248 return access_with_adjusted_size(addr, &data, size, 1, 4,
1249 memory_region_oldmmio_write_accessor,
1254 void memory_region_init_io(MemoryRegion *mr,
1256 const MemoryRegionOps *ops,
1261 memory_region_init(mr, owner, name, size);
1262 mr->ops = ops ? ops : &unassigned_mem_ops;
1263 mr->opaque = opaque;
1264 mr->terminates = true;
1267 void memory_region_init_ram(MemoryRegion *mr,
1273 RAMBlock *ram_block;
1275 memory_region_init(mr, owner, name, size);
1277 mr->terminates = true;
1278 mr->destructor = memory_region_destructor_ram;
1279 ram_block = qemu_ram_alloc(size, mr, errp);
1280 mr->ram_addr = ram_block->offset;
1281 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1284 void memory_region_init_resizeable_ram(MemoryRegion *mr,
1289 void (*resized)(const char*,
1294 RAMBlock *ram_block;
1296 memory_region_init(mr, owner, name, size);
1298 mr->terminates = true;
1299 mr->destructor = memory_region_destructor_ram;
1300 ram_block = qemu_ram_alloc_resizeable(size, max_size, resized, mr, errp);
1301 mr->ram_addr = ram_block->offset;
1302 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1306 void memory_region_init_ram_from_file(MemoryRegion *mr,
1307 struct Object *owner,
1314 RAMBlock *ram_block;
1316 memory_region_init(mr, owner, name, size);
1318 mr->terminates = true;
1319 mr->destructor = memory_region_destructor_ram;
1320 ram_block = qemu_ram_alloc_from_file(size, mr, share, path, errp);
1321 mr->ram_addr = ram_block->offset;
1322 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1326 void memory_region_init_ram_ptr(MemoryRegion *mr,
1332 RAMBlock *ram_block;
1334 memory_region_init(mr, owner, name, size);
1336 mr->terminates = true;
1337 mr->destructor = memory_region_destructor_ram;
1338 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1340 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1341 assert(ptr != NULL);
1342 ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
1343 mr->ram_addr = ram_block->offset;
1346 void memory_region_set_skip_dump(MemoryRegion *mr)
1348 mr->skip_dump = true;
1351 void memory_region_init_alias(MemoryRegion *mr,
1358 memory_region_init(mr, owner, name, size);
1360 mr->alias_offset = offset;
1363 void memory_region_init_rom_device(MemoryRegion *mr,
1365 const MemoryRegionOps *ops,
1371 RAMBlock *ram_block;
1373 memory_region_init(mr, owner, name, size);
1375 mr->opaque = opaque;
1376 mr->terminates = true;
1377 mr->rom_device = true;
1378 mr->destructor = memory_region_destructor_rom_device;
1379 ram_block = qemu_ram_alloc(size, mr, errp);
1380 mr->ram_addr = ram_block->offset;
1383 void memory_region_init_iommu(MemoryRegion *mr,
1385 const MemoryRegionIOMMUOps *ops,
1389 memory_region_init(mr, owner, name, size);
1390 mr->iommu_ops = ops,
1391 mr->terminates = true; /* then re-forwards */
1392 notifier_list_init(&mr->iommu_notify);
1395 static void memory_region_finalize(Object *obj)
1397 MemoryRegion *mr = MEMORY_REGION(obj);
1399 assert(!mr->container);
1401 /* We know the region is not visible in any address space (it
1402 * does not have a container and cannot be a root either because
1403 * it has no references, so we can blindly clear mr->enabled.
1404 * memory_region_set_enabled instead could trigger a transaction
1405 * and cause an infinite loop.
1407 mr->enabled = false;
1408 memory_region_transaction_begin();
1409 while (!QTAILQ_EMPTY(&mr->subregions)) {
1410 MemoryRegion *subregion = QTAILQ_FIRST(&mr->subregions);
1411 memory_region_del_subregion(mr, subregion);
1413 memory_region_transaction_commit();
1416 memory_region_clear_coalescing(mr);
1417 g_free((char *)mr->name);
1418 g_free(mr->ioeventfds);
1421 Object *memory_region_owner(MemoryRegion *mr)
1423 Object *obj = OBJECT(mr);
1427 void memory_region_ref(MemoryRegion *mr)
1429 /* MMIO callbacks most likely will access data that belongs
1430 * to the owner, hence the need to ref/unref the owner whenever
1431 * the memory region is in use.
1433 * The memory region is a child of its owner. As long as the
1434 * owner doesn't call unparent itself on the memory region,
1435 * ref-ing the owner will also keep the memory region alive.
1436 * Memory regions without an owner are supposed to never go away;
1437 * we do not ref/unref them because it slows down DMA sensibly.
1439 if (mr && mr->owner) {
1440 object_ref(mr->owner);
1444 void memory_region_unref(MemoryRegion *mr)
1446 if (mr && mr->owner) {
1447 object_unref(mr->owner);
1451 uint64_t memory_region_size(MemoryRegion *mr)
1453 if (int128_eq(mr->size, int128_2_64())) {
1456 return int128_get64(mr->size);
1459 const char *memory_region_name(const MemoryRegion *mr)
1462 ((MemoryRegion *)mr)->name =
1463 object_get_canonical_path_component(OBJECT(mr));
1468 bool memory_region_is_skip_dump(MemoryRegion *mr)
1470 return mr->skip_dump;
1473 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
1475 uint8_t mask = mr->dirty_log_mask;
1476 if (global_dirty_log) {
1477 mask |= (1 << DIRTY_MEMORY_MIGRATION);
1482 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client)
1484 return memory_region_get_dirty_log_mask(mr) & (1 << client);
1487 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n)
1489 notifier_list_add(&mr->iommu_notify, n);
1492 void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n,
1493 hwaddr granularity, bool is_write)
1496 IOMMUTLBEntry iotlb;
1498 for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
1499 iotlb = mr->iommu_ops->translate(mr, addr, is_write);
1500 if (iotlb.perm != IOMMU_NONE) {
1501 n->notify(n, &iotlb);
1504 /* if (2^64 - MR size) < granularity, it's possible to get an
1505 * infinite loop here. This should catch such a wraparound */
1506 if ((addr + granularity) < addr) {
1512 void memory_region_unregister_iommu_notifier(Notifier *n)
1517 void memory_region_notify_iommu(MemoryRegion *mr,
1518 IOMMUTLBEntry entry)
1520 assert(memory_region_is_iommu(mr));
1521 notifier_list_notify(&mr->iommu_notify, &entry);
1524 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1526 uint8_t mask = 1 << client;
1527 uint8_t old_logging;
1529 assert(client == DIRTY_MEMORY_VGA);
1530 old_logging = mr->vga_logging_count;
1531 mr->vga_logging_count += log ? 1 : -1;
1532 if (!!old_logging == !!mr->vga_logging_count) {
1536 memory_region_transaction_begin();
1537 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1538 memory_region_update_pending |= mr->enabled;
1539 memory_region_transaction_commit();
1542 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1543 hwaddr size, unsigned client)
1545 assert(mr->ram_addr != RAM_ADDR_INVALID);
1546 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, size, client);
1549 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1552 assert(mr->ram_addr != RAM_ADDR_INVALID);
1553 cpu_physical_memory_set_dirty_range(mr->ram_addr + addr, size,
1554 memory_region_get_dirty_log_mask(mr));
1557 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
1558 hwaddr size, unsigned client)
1560 assert(mr->ram_addr != RAM_ADDR_INVALID);
1561 return cpu_physical_memory_test_and_clear_dirty(mr->ram_addr + addr,
1566 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1571 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1572 FlatView *view = address_space_get_flatview(as);
1573 FOR_EACH_FLAT_RANGE(fr, view) {
1575 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1578 flatview_unref(view);
1582 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1584 if (mr->readonly != readonly) {
1585 memory_region_transaction_begin();
1586 mr->readonly = readonly;
1587 memory_region_update_pending |= mr->enabled;
1588 memory_region_transaction_commit();
1592 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
1594 if (mr->romd_mode != romd_mode) {
1595 memory_region_transaction_begin();
1596 mr->romd_mode = romd_mode;
1597 memory_region_update_pending |= mr->enabled;
1598 memory_region_transaction_commit();
1602 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1603 hwaddr size, unsigned client)
1605 assert(mr->ram_addr != RAM_ADDR_INVALID);
1606 cpu_physical_memory_test_and_clear_dirty(mr->ram_addr + addr, size,
1610 int memory_region_get_fd(MemoryRegion *mr)
1613 return memory_region_get_fd(mr->alias);
1616 assert(mr->ram_addr != RAM_ADDR_INVALID);
1618 return qemu_get_ram_fd(mr->ram_addr & TARGET_PAGE_MASK);
1621 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1624 uint64_t offset = 0;
1628 offset += mr->alias_offset;
1631 assert(mr->ram_addr != RAM_ADDR_INVALID);
1632 ptr = qemu_get_ram_ptr(mr->ram_block, mr->ram_addr & TARGET_PAGE_MASK);
1635 return ptr + offset;
1638 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp)
1640 assert(mr->ram_addr != RAM_ADDR_INVALID);
1642 qemu_ram_resize(mr->ram_addr, newsize, errp);
1645 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
1649 CoalescedMemoryRange *cmr;
1651 MemoryRegionSection section;
1653 view = address_space_get_flatview(as);
1654 FOR_EACH_FLAT_RANGE(fr, view) {
1656 section = (MemoryRegionSection) {
1657 .address_space = as,
1658 .offset_within_address_space = int128_get64(fr->addr.start),
1659 .size = fr->addr.size,
1662 MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, §ion,
1663 int128_get64(fr->addr.start),
1664 int128_get64(fr->addr.size));
1665 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1666 tmp = addrrange_shift(cmr->addr,
1667 int128_sub(fr->addr.start,
1668 int128_make64(fr->offset_in_region)));
1669 if (!addrrange_intersects(tmp, fr->addr)) {
1672 tmp = addrrange_intersection(tmp, fr->addr);
1673 MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, §ion,
1674 int128_get64(tmp.start),
1675 int128_get64(tmp.size));
1679 flatview_unref(view);
1682 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1686 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1687 memory_region_update_coalesced_range_as(mr, as);
1691 void memory_region_set_coalescing(MemoryRegion *mr)
1693 memory_region_clear_coalescing(mr);
1694 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1697 void memory_region_add_coalescing(MemoryRegion *mr,
1701 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1703 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1704 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1705 memory_region_update_coalesced_range(mr);
1706 memory_region_set_flush_coalesced(mr);
1709 void memory_region_clear_coalescing(MemoryRegion *mr)
1711 CoalescedMemoryRange *cmr;
1712 bool updated = false;
1714 qemu_flush_coalesced_mmio_buffer();
1715 mr->flush_coalesced_mmio = false;
1717 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1718 cmr = QTAILQ_FIRST(&mr->coalesced);
1719 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1725 memory_region_update_coalesced_range(mr);
1729 void memory_region_set_flush_coalesced(MemoryRegion *mr)
1731 mr->flush_coalesced_mmio = true;
1734 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
1736 qemu_flush_coalesced_mmio_buffer();
1737 if (QTAILQ_EMPTY(&mr->coalesced)) {
1738 mr->flush_coalesced_mmio = false;
1742 void memory_region_set_global_locking(MemoryRegion *mr)
1744 mr->global_locking = true;
1747 void memory_region_clear_global_locking(MemoryRegion *mr)
1749 mr->global_locking = false;
1752 static bool userspace_eventfd_warning;
1754 void memory_region_add_eventfd(MemoryRegion *mr,
1761 MemoryRegionIoeventfd mrfd = {
1762 .addr.start = int128_make64(addr),
1763 .addr.size = int128_make64(size),
1764 .match_data = match_data,
1770 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
1771 userspace_eventfd_warning))) {
1772 userspace_eventfd_warning = true;
1773 error_report("Using eventfd without MMIO binding in KVM. "
1774 "Suboptimal performance expected");
1778 adjust_endianness(mr, &mrfd.data, size);
1780 memory_region_transaction_begin();
1781 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1782 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1787 mr->ioeventfds = g_realloc(mr->ioeventfds,
1788 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1789 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1790 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1791 mr->ioeventfds[i] = mrfd;
1792 ioeventfd_update_pending |= mr->enabled;
1793 memory_region_transaction_commit();
1796 void memory_region_del_eventfd(MemoryRegion *mr,
1803 MemoryRegionIoeventfd mrfd = {
1804 .addr.start = int128_make64(addr),
1805 .addr.size = int128_make64(size),
1806 .match_data = match_data,
1813 adjust_endianness(mr, &mrfd.data, size);
1815 memory_region_transaction_begin();
1816 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1817 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1821 assert(i != mr->ioeventfd_nb);
1822 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1823 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1825 mr->ioeventfds = g_realloc(mr->ioeventfds,
1826 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1827 ioeventfd_update_pending |= mr->enabled;
1828 memory_region_transaction_commit();
1831 static void memory_region_update_container_subregions(MemoryRegion *subregion)
1833 hwaddr offset = subregion->addr;
1834 MemoryRegion *mr = subregion->container;
1835 MemoryRegion *other;
1837 memory_region_transaction_begin();
1839 memory_region_ref(subregion);
1840 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1841 if (subregion->may_overlap || other->may_overlap) {
1844 if (int128_ge(int128_make64(offset),
1845 int128_add(int128_make64(other->addr), other->size))
1846 || int128_le(int128_add(int128_make64(offset), subregion->size),
1847 int128_make64(other->addr))) {
1851 printf("warning: subregion collision %llx/%llx (%s) "
1852 "vs %llx/%llx (%s)\n",
1853 (unsigned long long)offset,
1854 (unsigned long long)int128_get64(subregion->size),
1856 (unsigned long long)other->addr,
1857 (unsigned long long)int128_get64(other->size),
1861 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1862 if (subregion->priority >= other->priority) {
1863 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1867 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1869 memory_region_update_pending |= mr->enabled && subregion->enabled;
1870 memory_region_transaction_commit();
1873 static void memory_region_add_subregion_common(MemoryRegion *mr,
1875 MemoryRegion *subregion)
1877 assert(!subregion->container);
1878 subregion->container = mr;
1879 subregion->addr = offset;
1880 memory_region_update_container_subregions(subregion);
1883 void memory_region_add_subregion(MemoryRegion *mr,
1885 MemoryRegion *subregion)
1887 subregion->may_overlap = false;
1888 subregion->priority = 0;
1889 memory_region_add_subregion_common(mr, offset, subregion);
1892 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1894 MemoryRegion *subregion,
1897 subregion->may_overlap = true;
1898 subregion->priority = priority;
1899 memory_region_add_subregion_common(mr, offset, subregion);
1902 void memory_region_del_subregion(MemoryRegion *mr,
1903 MemoryRegion *subregion)
1905 memory_region_transaction_begin();
1906 assert(subregion->container == mr);
1907 subregion->container = NULL;
1908 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1909 memory_region_unref(subregion);
1910 memory_region_update_pending |= mr->enabled && subregion->enabled;
1911 memory_region_transaction_commit();
1914 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1916 if (enabled == mr->enabled) {
1919 memory_region_transaction_begin();
1920 mr->enabled = enabled;
1921 memory_region_update_pending = true;
1922 memory_region_transaction_commit();
1925 void memory_region_set_size(MemoryRegion *mr, uint64_t size)
1927 Int128 s = int128_make64(size);
1929 if (size == UINT64_MAX) {
1932 if (int128_eq(s, mr->size)) {
1935 memory_region_transaction_begin();
1937 memory_region_update_pending = true;
1938 memory_region_transaction_commit();
1941 static void memory_region_readd_subregion(MemoryRegion *mr)
1943 MemoryRegion *container = mr->container;
1946 memory_region_transaction_begin();
1947 memory_region_ref(mr);
1948 memory_region_del_subregion(container, mr);
1949 mr->container = container;
1950 memory_region_update_container_subregions(mr);
1951 memory_region_unref(mr);
1952 memory_region_transaction_commit();
1956 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
1958 if (addr != mr->addr) {
1960 memory_region_readd_subregion(mr);
1964 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
1968 if (offset == mr->alias_offset) {
1972 memory_region_transaction_begin();
1973 mr->alias_offset = offset;
1974 memory_region_update_pending |= mr->enabled;
1975 memory_region_transaction_commit();
1978 uint64_t memory_region_get_alignment(const MemoryRegion *mr)
1983 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1985 const AddrRange *addr = addr_;
1986 const FlatRange *fr = fr_;
1988 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1990 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1996 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
1998 return bsearch(&addr, view->ranges, view->nr,
1999 sizeof(FlatRange), cmp_flatrange_addr);
2002 bool memory_region_is_mapped(MemoryRegion *mr)
2004 return mr->container ? true : false;
2007 /* Same as memory_region_find, but it does not add a reference to the
2008 * returned region. It must be called from an RCU critical section.
2010 static MemoryRegionSection memory_region_find_rcu(MemoryRegion *mr,
2011 hwaddr addr, uint64_t size)
2013 MemoryRegionSection ret = { .mr = NULL };
2021 for (root = mr; root->container; ) {
2022 root = root->container;
2026 as = memory_region_to_address_space(root);
2030 range = addrrange_make(int128_make64(addr), int128_make64(size));
2032 view = atomic_rcu_read(&as->current_map);
2033 fr = flatview_lookup(view, range);
2038 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
2043 ret.address_space = as;
2044 range = addrrange_intersection(range, fr->addr);
2045 ret.offset_within_region = fr->offset_in_region;
2046 ret.offset_within_region += int128_get64(int128_sub(range.start,
2048 ret.size = range.size;
2049 ret.offset_within_address_space = int128_get64(range.start);
2050 ret.readonly = fr->readonly;
2054 MemoryRegionSection memory_region_find(MemoryRegion *mr,
2055 hwaddr addr, uint64_t size)
2057 MemoryRegionSection ret;
2059 ret = memory_region_find_rcu(mr, addr, size);
2061 memory_region_ref(ret.mr);
2067 bool memory_region_present(MemoryRegion *container, hwaddr addr)
2072 mr = memory_region_find_rcu(container, addr, 1).mr;
2074 return mr && mr != container;
2077 void address_space_sync_dirty_bitmap(AddressSpace *as)
2082 view = address_space_get_flatview(as);
2083 FOR_EACH_FLAT_RANGE(fr, view) {
2084 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
2086 flatview_unref(view);
2089 void memory_global_dirty_log_start(void)
2091 global_dirty_log = true;
2093 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
2095 /* Refresh DIRTY_LOG_MIGRATION bit. */
2096 memory_region_transaction_begin();
2097 memory_region_update_pending = true;
2098 memory_region_transaction_commit();
2101 void memory_global_dirty_log_stop(void)
2103 global_dirty_log = false;
2105 /* Refresh DIRTY_LOG_MIGRATION bit. */
2106 memory_region_transaction_begin();
2107 memory_region_update_pending = true;
2108 memory_region_transaction_commit();
2110 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
2113 static void listener_add_address_space(MemoryListener *listener,
2119 if (listener->address_space_filter
2120 && listener->address_space_filter != as) {
2124 if (listener->begin) {
2125 listener->begin(listener);
2127 if (global_dirty_log) {
2128 if (listener->log_global_start) {
2129 listener->log_global_start(listener);
2133 view = address_space_get_flatview(as);
2134 FOR_EACH_FLAT_RANGE(fr, view) {
2135 MemoryRegionSection section = {
2137 .address_space = as,
2138 .offset_within_region = fr->offset_in_region,
2139 .size = fr->addr.size,
2140 .offset_within_address_space = int128_get64(fr->addr.start),
2141 .readonly = fr->readonly,
2143 if (fr->dirty_log_mask && listener->log_start) {
2144 listener->log_start(listener, §ion, 0, fr->dirty_log_mask);
2146 if (listener->region_add) {
2147 listener->region_add(listener, §ion);
2150 if (listener->commit) {
2151 listener->commit(listener);
2153 flatview_unref(view);
2156 void memory_listener_register(MemoryListener *listener, AddressSpace *filter)
2158 MemoryListener *other = NULL;
2161 listener->address_space_filter = filter;
2162 if (QTAILQ_EMPTY(&memory_listeners)
2163 || listener->priority >= QTAILQ_LAST(&memory_listeners,
2164 memory_listeners)->priority) {
2165 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
2167 QTAILQ_FOREACH(other, &memory_listeners, link) {
2168 if (listener->priority < other->priority) {
2172 QTAILQ_INSERT_BEFORE(other, listener, link);
2175 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2176 listener_add_address_space(listener, as);
2180 void memory_listener_unregister(MemoryListener *listener)
2182 QTAILQ_REMOVE(&memory_listeners, listener, link);
2185 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
2187 memory_region_ref(root);
2188 memory_region_transaction_begin();
2191 as->malloced = false;
2192 as->current_map = g_new(FlatView, 1);
2193 flatview_init(as->current_map);
2194 as->ioeventfd_nb = 0;
2195 as->ioeventfds = NULL;
2196 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
2197 as->name = g_strdup(name ? name : "anonymous");
2198 address_space_init_dispatch(as);
2199 memory_region_update_pending |= root->enabled;
2200 memory_region_transaction_commit();
2203 static void do_address_space_destroy(AddressSpace *as)
2205 MemoryListener *listener;
2206 bool do_free = as->malloced;
2208 address_space_destroy_dispatch(as);
2210 QTAILQ_FOREACH(listener, &memory_listeners, link) {
2211 assert(listener->address_space_filter != as);
2214 flatview_unref(as->current_map);
2216 g_free(as->ioeventfds);
2217 memory_region_unref(as->root);
2223 AddressSpace *address_space_init_shareable(MemoryRegion *root, const char *name)
2227 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2228 if (root == as->root && as->malloced) {
2234 as = g_malloc0(sizeof *as);
2235 address_space_init(as, root, name);
2236 as->malloced = true;
2240 void address_space_destroy(AddressSpace *as)
2242 MemoryRegion *root = as->root;
2245 if (as->ref_count) {
2248 /* Flush out anything from MemoryListeners listening in on this */
2249 memory_region_transaction_begin();
2251 memory_region_transaction_commit();
2252 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
2253 address_space_unregister(as);
2255 /* At this point, as->dispatch and as->current_map are dummy
2256 * entries that the guest should never use. Wait for the old
2257 * values to expire before freeing the data.
2260 call_rcu(as, do_address_space_destroy, rcu);
2263 typedef struct MemoryRegionList MemoryRegionList;
2265 struct MemoryRegionList {
2266 const MemoryRegion *mr;
2267 QTAILQ_ENTRY(MemoryRegionList) queue;
2270 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
2272 static void mtree_print_mr(fprintf_function mon_printf, void *f,
2273 const MemoryRegion *mr, unsigned int level,
2275 MemoryRegionListHead *alias_print_queue)
2277 MemoryRegionList *new_ml, *ml, *next_ml;
2278 MemoryRegionListHead submr_print_queue;
2279 const MemoryRegion *submr;
2286 for (i = 0; i < level; i++) {
2291 MemoryRegionList *ml;
2294 /* check if the alias is already in the queue */
2295 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
2296 if (ml->mr == mr->alias) {
2302 ml = g_new(MemoryRegionList, 1);
2304 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
2306 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
2307 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
2308 "-" TARGET_FMT_plx "%s\n",
2311 + (int128_nz(mr->size) ?
2312 (hwaddr)int128_get64(int128_sub(mr->size,
2313 int128_one())) : 0),
2315 mr->romd_mode ? 'R' : '-',
2316 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
2318 memory_region_name(mr),
2319 memory_region_name(mr->alias),
2322 + (int128_nz(mr->size) ?
2323 (hwaddr)int128_get64(int128_sub(mr->size,
2324 int128_one())) : 0),
2325 mr->enabled ? "" : " [disabled]");
2328 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %c%c): %s%s\n",
2331 + (int128_nz(mr->size) ?
2332 (hwaddr)int128_get64(int128_sub(mr->size,
2333 int128_one())) : 0),
2335 mr->romd_mode ? 'R' : '-',
2336 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
2338 memory_region_name(mr),
2339 mr->enabled ? "" : " [disabled]");
2342 QTAILQ_INIT(&submr_print_queue);
2344 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
2345 new_ml = g_new(MemoryRegionList, 1);
2347 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
2348 if (new_ml->mr->addr < ml->mr->addr ||
2349 (new_ml->mr->addr == ml->mr->addr &&
2350 new_ml->mr->priority > ml->mr->priority)) {
2351 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
2357 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
2361 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
2362 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
2366 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
2371 void mtree_info(fprintf_function mon_printf, void *f)
2373 MemoryRegionListHead ml_head;
2374 MemoryRegionList *ml, *ml2;
2377 QTAILQ_INIT(&ml_head);
2379 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2380 mon_printf(f, "address-space: %s\n", as->name);
2381 mtree_print_mr(mon_printf, f, as->root, 1, 0, &ml_head);
2382 mon_printf(f, "\n");
2385 /* print aliased regions */
2386 QTAILQ_FOREACH(ml, &ml_head, queue) {
2387 mon_printf(f, "memory-region: %s\n", memory_region_name(ml->mr));
2388 mtree_print_mr(mon_printf, f, ml->mr, 1, 0, &ml_head);
2389 mon_printf(f, "\n");
2392 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
2397 static const TypeInfo memory_region_info = {
2398 .parent = TYPE_OBJECT,
2399 .name = TYPE_MEMORY_REGION,
2400 .instance_size = sizeof(MemoryRegion),
2401 .instance_init = memory_region_initfn,
2402 .instance_finalize = memory_region_finalize,
2405 static void memory_register_types(void)
2407 type_register_static(&memory_region_info);
2410 type_init(memory_register_types)