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 int get_cpu_index(void)
392 return current_cpu->cpu_index;
397 static MemTxResult memory_region_oldmmio_read_accessor(MemoryRegion *mr,
407 tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);
409 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
410 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
411 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
412 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
414 *value |= (tmp & mask) << shift;
418 static MemTxResult memory_region_read_accessor(MemoryRegion *mr,
428 tmp = mr->ops->read(mr->opaque, addr, size);
430 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
431 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
432 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
433 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
435 *value |= (tmp & mask) << shift;
439 static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr,
450 r = mr->ops->read_with_attrs(mr->opaque, addr, &tmp, size, attrs);
452 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
453 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
454 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
455 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
457 *value |= (tmp & mask) << shift;
461 static MemTxResult memory_region_oldmmio_write_accessor(MemoryRegion *mr,
471 tmp = (*value >> shift) & mask;
473 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
474 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
475 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
476 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
478 mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp);
482 static MemTxResult memory_region_write_accessor(MemoryRegion *mr,
492 tmp = (*value >> shift) & mask;
494 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
495 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
496 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
497 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
499 mr->ops->write(mr->opaque, addr, tmp, size);
503 static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr,
513 tmp = (*value >> shift) & mask;
515 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
516 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
517 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
518 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
520 return mr->ops->write_with_attrs(mr->opaque, addr, tmp, size, attrs);
523 static MemTxResult access_with_adjusted_size(hwaddr addr,
526 unsigned access_size_min,
527 unsigned access_size_max,
528 MemTxResult (*access)(MemoryRegion *mr,
538 uint64_t access_mask;
539 unsigned access_size;
541 MemTxResult r = MEMTX_OK;
543 if (!access_size_min) {
546 if (!access_size_max) {
550 /* FIXME: support unaligned access? */
551 access_size = MAX(MIN(size, access_size_max), access_size_min);
552 access_mask = -1ULL >> (64 - access_size * 8);
553 if (memory_region_big_endian(mr)) {
554 for (i = 0; i < size; i += access_size) {
555 r |= access(mr, addr + i, value, access_size,
556 (size - access_size - i) * 8, access_mask, attrs);
559 for (i = 0; i < size; i += access_size) {
560 r |= access(mr, addr + i, value, access_size, i * 8,
567 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
571 while (mr->container) {
574 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
575 if (mr == as->root) {
582 /* Render a memory region into the global view. Ranges in @view obscure
585 static void render_memory_region(FlatView *view,
591 MemoryRegion *subregion;
593 hwaddr offset_in_region;
603 int128_addto(&base, int128_make64(mr->addr));
604 readonly |= mr->readonly;
606 tmp = addrrange_make(base, mr->size);
608 if (!addrrange_intersects(tmp, clip)) {
612 clip = addrrange_intersection(tmp, clip);
615 int128_subfrom(&base, int128_make64(mr->alias->addr));
616 int128_subfrom(&base, int128_make64(mr->alias_offset));
617 render_memory_region(view, mr->alias, base, clip, readonly);
621 /* Render subregions in priority order. */
622 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
623 render_memory_region(view, subregion, base, clip, readonly);
626 if (!mr->terminates) {
630 offset_in_region = int128_get64(int128_sub(clip.start, base));
635 fr.dirty_log_mask = memory_region_get_dirty_log_mask(mr);
636 fr.romd_mode = mr->romd_mode;
637 fr.readonly = readonly;
639 /* Render the region itself into any gaps left by the current view. */
640 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
641 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
644 if (int128_lt(base, view->ranges[i].addr.start)) {
645 now = int128_min(remain,
646 int128_sub(view->ranges[i].addr.start, base));
647 fr.offset_in_region = offset_in_region;
648 fr.addr = addrrange_make(base, now);
649 flatview_insert(view, i, &fr);
651 int128_addto(&base, now);
652 offset_in_region += int128_get64(now);
653 int128_subfrom(&remain, now);
655 now = int128_sub(int128_min(int128_add(base, remain),
656 addrrange_end(view->ranges[i].addr)),
658 int128_addto(&base, now);
659 offset_in_region += int128_get64(now);
660 int128_subfrom(&remain, now);
662 if (int128_nz(remain)) {
663 fr.offset_in_region = offset_in_region;
664 fr.addr = addrrange_make(base, remain);
665 flatview_insert(view, i, &fr);
669 /* Render a memory topology into a list of disjoint absolute ranges. */
670 static FlatView *generate_memory_topology(MemoryRegion *mr)
674 view = g_new(FlatView, 1);
678 render_memory_region(view, mr, int128_zero(),
679 addrrange_make(int128_zero(), int128_2_64()), false);
681 flatview_simplify(view);
686 static void address_space_add_del_ioeventfds(AddressSpace *as,
687 MemoryRegionIoeventfd *fds_new,
689 MemoryRegionIoeventfd *fds_old,
693 MemoryRegionIoeventfd *fd;
694 MemoryRegionSection section;
696 /* Generate a symmetric difference of the old and new fd sets, adding
697 * and deleting as necessary.
701 while (iold < fds_old_nb || inew < fds_new_nb) {
702 if (iold < fds_old_nb
703 && (inew == fds_new_nb
704 || memory_region_ioeventfd_before(fds_old[iold],
707 section = (MemoryRegionSection) {
709 .offset_within_address_space = int128_get64(fd->addr.start),
710 .size = fd->addr.size,
712 MEMORY_LISTENER_CALL(eventfd_del, Forward, §ion,
713 fd->match_data, fd->data, fd->e);
715 } else if (inew < fds_new_nb
716 && (iold == fds_old_nb
717 || memory_region_ioeventfd_before(fds_new[inew],
720 section = (MemoryRegionSection) {
722 .offset_within_address_space = int128_get64(fd->addr.start),
723 .size = fd->addr.size,
725 MEMORY_LISTENER_CALL(eventfd_add, Reverse, §ion,
726 fd->match_data, fd->data, fd->e);
735 static FlatView *address_space_get_flatview(AddressSpace *as)
740 view = atomic_rcu_read(&as->current_map);
746 static void address_space_update_ioeventfds(AddressSpace *as)
750 unsigned ioeventfd_nb = 0;
751 MemoryRegionIoeventfd *ioeventfds = NULL;
755 view = address_space_get_flatview(as);
756 FOR_EACH_FLAT_RANGE(fr, view) {
757 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
758 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
759 int128_sub(fr->addr.start,
760 int128_make64(fr->offset_in_region)));
761 if (addrrange_intersects(fr->addr, tmp)) {
763 ioeventfds = g_realloc(ioeventfds,
764 ioeventfd_nb * sizeof(*ioeventfds));
765 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
766 ioeventfds[ioeventfd_nb-1].addr = tmp;
771 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
772 as->ioeventfds, as->ioeventfd_nb);
774 g_free(as->ioeventfds);
775 as->ioeventfds = ioeventfds;
776 as->ioeventfd_nb = ioeventfd_nb;
777 flatview_unref(view);
780 static void address_space_update_topology_pass(AddressSpace *as,
781 const FlatView *old_view,
782 const FlatView *new_view,
786 FlatRange *frold, *frnew;
788 /* Generate a symmetric difference of the old and new memory maps.
789 * Kill ranges in the old map, and instantiate ranges in the new map.
792 while (iold < old_view->nr || inew < new_view->nr) {
793 if (iold < old_view->nr) {
794 frold = &old_view->ranges[iold];
798 if (inew < new_view->nr) {
799 frnew = &new_view->ranges[inew];
806 || int128_lt(frold->addr.start, frnew->addr.start)
807 || (int128_eq(frold->addr.start, frnew->addr.start)
808 && !flatrange_equal(frold, frnew)))) {
809 /* In old but not in new, or in both but attributes changed. */
812 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
816 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
817 /* In both and unchanged (except logging may have changed) */
820 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
821 if (frnew->dirty_log_mask & ~frold->dirty_log_mask) {
822 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start,
823 frold->dirty_log_mask,
824 frnew->dirty_log_mask);
826 if (frold->dirty_log_mask & ~frnew->dirty_log_mask) {
827 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop,
828 frold->dirty_log_mask,
829 frnew->dirty_log_mask);
839 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
848 static void address_space_update_topology(AddressSpace *as)
850 FlatView *old_view = address_space_get_flatview(as);
851 FlatView *new_view = generate_memory_topology(as->root);
853 address_space_update_topology_pass(as, old_view, new_view, false);
854 address_space_update_topology_pass(as, old_view, new_view, true);
856 /* Writes are protected by the BQL. */
857 atomic_rcu_set(&as->current_map, new_view);
858 call_rcu(old_view, flatview_unref, rcu);
860 /* Note that all the old MemoryRegions are still alive up to this
861 * point. This relieves most MemoryListeners from the need to
862 * ref/unref the MemoryRegions they get---unless they use them
863 * outside the iothread mutex, in which case precise reference
864 * counting is necessary.
866 flatview_unref(old_view);
868 address_space_update_ioeventfds(as);
871 void memory_region_transaction_begin(void)
873 qemu_flush_coalesced_mmio_buffer();
874 ++memory_region_transaction_depth;
877 static void memory_region_clear_pending(void)
879 memory_region_update_pending = false;
880 ioeventfd_update_pending = false;
883 void memory_region_transaction_commit(void)
887 assert(memory_region_transaction_depth);
888 --memory_region_transaction_depth;
889 if (!memory_region_transaction_depth) {
890 if (memory_region_update_pending) {
891 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
893 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
894 address_space_update_topology(as);
897 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
898 } else if (ioeventfd_update_pending) {
899 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
900 address_space_update_ioeventfds(as);
903 memory_region_clear_pending();
907 static void memory_region_destructor_none(MemoryRegion *mr)
911 static void memory_region_destructor_ram(MemoryRegion *mr)
913 qemu_ram_free(mr->ram_block);
916 static void memory_region_destructor_rom_device(MemoryRegion *mr)
918 qemu_ram_free(mr->ram_block);
921 static bool memory_region_need_escape(char c)
923 return c == '/' || c == '[' || c == '\\' || c == ']';
926 static char *memory_region_escape_name(const char *name)
933 for (p = name; *p; p++) {
934 bytes += memory_region_need_escape(*p) ? 4 : 1;
936 if (bytes == p - name) {
937 return g_memdup(name, bytes + 1);
940 escaped = g_malloc(bytes + 1);
941 for (p = name, q = escaped; *p; p++) {
943 if (unlikely(memory_region_need_escape(c))) {
946 *q++ = "0123456789abcdef"[c >> 4];
947 c = "0123456789abcdef"[c & 15];
955 void memory_region_init(MemoryRegion *mr,
960 object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION);
961 mr->size = int128_make64(size);
962 if (size == UINT64_MAX) {
963 mr->size = int128_2_64();
965 mr->name = g_strdup(name);
967 mr->ram_block = NULL;
970 char *escaped_name = memory_region_escape_name(name);
971 char *name_array = g_strdup_printf("%s[*]", escaped_name);
974 owner = container_get(qdev_get_machine(), "/unattached");
977 object_property_add_child(owner, name_array, OBJECT(mr), &error_abort);
978 object_unref(OBJECT(mr));
980 g_free(escaped_name);
984 static void memory_region_get_addr(Object *obj, Visitor *v, const char *name,
985 void *opaque, Error **errp)
987 MemoryRegion *mr = MEMORY_REGION(obj);
988 uint64_t value = mr->addr;
990 visit_type_uint64(v, name, &value, errp);
993 static void memory_region_get_container(Object *obj, Visitor *v,
994 const char *name, void *opaque,
997 MemoryRegion *mr = MEMORY_REGION(obj);
998 gchar *path = (gchar *)"";
1000 if (mr->container) {
1001 path = object_get_canonical_path(OBJECT(mr->container));
1003 visit_type_str(v, name, &path, errp);
1004 if (mr->container) {
1009 static Object *memory_region_resolve_container(Object *obj, void *opaque,
1012 MemoryRegion *mr = MEMORY_REGION(obj);
1014 return OBJECT(mr->container);
1017 static void memory_region_get_priority(Object *obj, Visitor *v,
1018 const char *name, void *opaque,
1021 MemoryRegion *mr = MEMORY_REGION(obj);
1022 int32_t value = mr->priority;
1024 visit_type_int32(v, name, &value, errp);
1027 static bool memory_region_get_may_overlap(Object *obj, Error **errp)
1029 MemoryRegion *mr = MEMORY_REGION(obj);
1031 return mr->may_overlap;
1034 static void memory_region_get_size(Object *obj, Visitor *v, const char *name,
1035 void *opaque, Error **errp)
1037 MemoryRegion *mr = MEMORY_REGION(obj);
1038 uint64_t value = memory_region_size(mr);
1040 visit_type_uint64(v, name, &value, errp);
1043 static void memory_region_initfn(Object *obj)
1045 MemoryRegion *mr = MEMORY_REGION(obj);
1048 mr->ops = &unassigned_mem_ops;
1050 mr->romd_mode = true;
1051 mr->global_locking = true;
1052 mr->destructor = memory_region_destructor_none;
1053 QTAILQ_INIT(&mr->subregions);
1054 QTAILQ_INIT(&mr->coalesced);
1056 op = object_property_add(OBJECT(mr), "container",
1057 "link<" TYPE_MEMORY_REGION ">",
1058 memory_region_get_container,
1059 NULL, /* memory_region_set_container */
1060 NULL, NULL, &error_abort);
1061 op->resolve = memory_region_resolve_container;
1063 object_property_add(OBJECT(mr), "addr", "uint64",
1064 memory_region_get_addr,
1065 NULL, /* memory_region_set_addr */
1066 NULL, NULL, &error_abort);
1067 object_property_add(OBJECT(mr), "priority", "uint32",
1068 memory_region_get_priority,
1069 NULL, /* memory_region_set_priority */
1070 NULL, NULL, &error_abort);
1071 object_property_add_bool(OBJECT(mr), "may-overlap",
1072 memory_region_get_may_overlap,
1073 NULL, /* memory_region_set_may_overlap */
1075 object_property_add(OBJECT(mr), "size", "uint64",
1076 memory_region_get_size,
1077 NULL, /* memory_region_set_size, */
1078 NULL, NULL, &error_abort);
1081 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
1084 #ifdef DEBUG_UNASSIGNED
1085 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
1087 if (current_cpu != NULL) {
1088 cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
1093 static void unassigned_mem_write(void *opaque, hwaddr addr,
1094 uint64_t val, unsigned size)
1096 #ifdef DEBUG_UNASSIGNED
1097 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
1099 if (current_cpu != NULL) {
1100 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
1104 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
1105 unsigned size, bool is_write)
1110 const MemoryRegionOps unassigned_mem_ops = {
1111 .valid.accepts = unassigned_mem_accepts,
1112 .endianness = DEVICE_NATIVE_ENDIAN,
1115 bool memory_region_access_valid(MemoryRegion *mr,
1120 int access_size_min, access_size_max;
1123 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
1127 if (!mr->ops->valid.accepts) {
1131 access_size_min = mr->ops->valid.min_access_size;
1132 if (!mr->ops->valid.min_access_size) {
1133 access_size_min = 1;
1136 access_size_max = mr->ops->valid.max_access_size;
1137 if (!mr->ops->valid.max_access_size) {
1138 access_size_max = 4;
1141 access_size = MAX(MIN(size, access_size_max), access_size_min);
1142 for (i = 0; i < size; i += access_size) {
1143 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
1152 static MemTxResult memory_region_dispatch_read1(MemoryRegion *mr,
1160 if (mr->ops->read) {
1161 return access_with_adjusted_size(addr, pval, size,
1162 mr->ops->impl.min_access_size,
1163 mr->ops->impl.max_access_size,
1164 memory_region_read_accessor,
1166 } else if (mr->ops->read_with_attrs) {
1167 return access_with_adjusted_size(addr, pval, size,
1168 mr->ops->impl.min_access_size,
1169 mr->ops->impl.max_access_size,
1170 memory_region_read_with_attrs_accessor,
1173 return access_with_adjusted_size(addr, pval, size, 1, 4,
1174 memory_region_oldmmio_read_accessor,
1179 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1187 if (!memory_region_access_valid(mr, addr, size, false)) {
1188 *pval = unassigned_mem_read(mr, addr, size);
1189 return MEMTX_DECODE_ERROR;
1192 r = memory_region_dispatch_read1(mr, addr, pval, size, attrs);
1193 adjust_endianness(mr, pval, size);
1197 /* Return true if an eventfd was signalled */
1198 static bool memory_region_dispatch_write_eventfds(MemoryRegion *mr,
1204 MemoryRegionIoeventfd ioeventfd = {
1205 .addr = addrrange_make(int128_make64(addr), int128_make64(size)),
1210 for (i = 0; i < mr->ioeventfd_nb; i++) {
1211 ioeventfd.match_data = mr->ioeventfds[i].match_data;
1212 ioeventfd.e = mr->ioeventfds[i].e;
1214 if (memory_region_ioeventfd_equal(ioeventfd, mr->ioeventfds[i])) {
1215 event_notifier_set(ioeventfd.e);
1223 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1229 if (!memory_region_access_valid(mr, addr, size, true)) {
1230 unassigned_mem_write(mr, addr, data, size);
1231 return MEMTX_DECODE_ERROR;
1234 adjust_endianness(mr, &data, size);
1236 if ((!kvm_eventfds_enabled()) &&
1237 memory_region_dispatch_write_eventfds(mr, addr, data, size, attrs)) {
1241 if (mr->ops->write) {
1242 return 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_accessor, mr,
1247 } else if (mr->ops->write_with_attrs) {
1249 access_with_adjusted_size(addr, &data, size,
1250 mr->ops->impl.min_access_size,
1251 mr->ops->impl.max_access_size,
1252 memory_region_write_with_attrs_accessor,
1255 return access_with_adjusted_size(addr, &data, size, 1, 4,
1256 memory_region_oldmmio_write_accessor,
1261 void memory_region_init_io(MemoryRegion *mr,
1263 const MemoryRegionOps *ops,
1268 memory_region_init(mr, owner, name, size);
1269 mr->ops = ops ? ops : &unassigned_mem_ops;
1270 mr->opaque = opaque;
1271 mr->terminates = true;
1274 void memory_region_init_ram(MemoryRegion *mr,
1280 memory_region_init(mr, owner, name, size);
1282 mr->terminates = true;
1283 mr->destructor = memory_region_destructor_ram;
1284 mr->ram_block = qemu_ram_alloc(size, mr, errp);
1285 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1288 void memory_region_init_resizeable_ram(MemoryRegion *mr,
1293 void (*resized)(const char*,
1298 memory_region_init(mr, owner, name, size);
1300 mr->terminates = true;
1301 mr->destructor = memory_region_destructor_ram;
1302 mr->ram_block = qemu_ram_alloc_resizeable(size, max_size, resized,
1304 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1308 void memory_region_init_ram_from_file(MemoryRegion *mr,
1309 struct Object *owner,
1316 memory_region_init(mr, owner, name, size);
1318 mr->terminates = true;
1319 mr->destructor = memory_region_destructor_ram;
1320 mr->ram_block = qemu_ram_alloc_from_file(size, mr, share, path, errp);
1321 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1325 void memory_region_init_ram_ptr(MemoryRegion *mr,
1331 memory_region_init(mr, owner, name, size);
1333 mr->terminates = true;
1334 mr->destructor = memory_region_destructor_ram;
1335 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1337 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1338 assert(ptr != NULL);
1339 mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
1342 void memory_region_set_skip_dump(MemoryRegion *mr)
1344 mr->skip_dump = true;
1347 void memory_region_init_alias(MemoryRegion *mr,
1354 memory_region_init(mr, owner, name, size);
1356 mr->alias_offset = offset;
1359 void memory_region_init_rom_device(MemoryRegion *mr,
1361 const MemoryRegionOps *ops,
1367 memory_region_init(mr, owner, name, size);
1369 mr->opaque = opaque;
1370 mr->terminates = true;
1371 mr->rom_device = true;
1372 mr->destructor = memory_region_destructor_rom_device;
1373 mr->ram_block = qemu_ram_alloc(size, mr, errp);
1376 void memory_region_init_iommu(MemoryRegion *mr,
1378 const MemoryRegionIOMMUOps *ops,
1382 memory_region_init(mr, owner, name, size);
1383 mr->iommu_ops = ops,
1384 mr->terminates = true; /* then re-forwards */
1385 notifier_list_init(&mr->iommu_notify);
1388 static void memory_region_finalize(Object *obj)
1390 MemoryRegion *mr = MEMORY_REGION(obj);
1392 assert(!mr->container);
1394 /* We know the region is not visible in any address space (it
1395 * does not have a container and cannot be a root either because
1396 * it has no references, so we can blindly clear mr->enabled.
1397 * memory_region_set_enabled instead could trigger a transaction
1398 * and cause an infinite loop.
1400 mr->enabled = false;
1401 memory_region_transaction_begin();
1402 while (!QTAILQ_EMPTY(&mr->subregions)) {
1403 MemoryRegion *subregion = QTAILQ_FIRST(&mr->subregions);
1404 memory_region_del_subregion(mr, subregion);
1406 memory_region_transaction_commit();
1409 memory_region_clear_coalescing(mr);
1410 g_free((char *)mr->name);
1411 g_free(mr->ioeventfds);
1414 Object *memory_region_owner(MemoryRegion *mr)
1416 Object *obj = OBJECT(mr);
1420 void memory_region_ref(MemoryRegion *mr)
1422 /* MMIO callbacks most likely will access data that belongs
1423 * to the owner, hence the need to ref/unref the owner whenever
1424 * the memory region is in use.
1426 * The memory region is a child of its owner. As long as the
1427 * owner doesn't call unparent itself on the memory region,
1428 * ref-ing the owner will also keep the memory region alive.
1429 * Memory regions without an owner are supposed to never go away;
1430 * we do not ref/unref them because it slows down DMA sensibly.
1432 if (mr && mr->owner) {
1433 object_ref(mr->owner);
1437 void memory_region_unref(MemoryRegion *mr)
1439 if (mr && mr->owner) {
1440 object_unref(mr->owner);
1444 uint64_t memory_region_size(MemoryRegion *mr)
1446 if (int128_eq(mr->size, int128_2_64())) {
1449 return int128_get64(mr->size);
1452 const char *memory_region_name(const MemoryRegion *mr)
1455 ((MemoryRegion *)mr)->name =
1456 object_get_canonical_path_component(OBJECT(mr));
1461 bool memory_region_is_skip_dump(MemoryRegion *mr)
1463 return mr->skip_dump;
1466 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
1468 uint8_t mask = mr->dirty_log_mask;
1469 if (global_dirty_log) {
1470 mask |= (1 << DIRTY_MEMORY_MIGRATION);
1475 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client)
1477 return memory_region_get_dirty_log_mask(mr) & (1 << client);
1480 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n)
1482 notifier_list_add(&mr->iommu_notify, n);
1485 void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n,
1486 hwaddr granularity, bool is_write)
1489 IOMMUTLBEntry iotlb;
1491 for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
1492 iotlb = mr->iommu_ops->translate(mr, addr, is_write);
1493 if (iotlb.perm != IOMMU_NONE) {
1494 n->notify(n, &iotlb);
1497 /* if (2^64 - MR size) < granularity, it's possible to get an
1498 * infinite loop here. This should catch such a wraparound */
1499 if ((addr + granularity) < addr) {
1505 void memory_region_unregister_iommu_notifier(Notifier *n)
1510 void memory_region_notify_iommu(MemoryRegion *mr,
1511 IOMMUTLBEntry entry)
1513 assert(memory_region_is_iommu(mr));
1514 notifier_list_notify(&mr->iommu_notify, &entry);
1517 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1519 uint8_t mask = 1 << client;
1520 uint8_t old_logging;
1522 assert(client == DIRTY_MEMORY_VGA);
1523 old_logging = mr->vga_logging_count;
1524 mr->vga_logging_count += log ? 1 : -1;
1525 if (!!old_logging == !!mr->vga_logging_count) {
1529 memory_region_transaction_begin();
1530 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1531 memory_region_update_pending |= mr->enabled;
1532 memory_region_transaction_commit();
1535 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1536 hwaddr size, unsigned client)
1538 assert(mr->ram_block);
1539 return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr) + addr,
1543 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1546 assert(mr->ram_block);
1547 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr) + addr,
1549 memory_region_get_dirty_log_mask(mr));
1552 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
1553 hwaddr size, unsigned client)
1555 assert(mr->ram_block);
1556 return cpu_physical_memory_test_and_clear_dirty(
1557 memory_region_get_ram_addr(mr) + addr, size, client);
1561 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1566 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1567 FlatView *view = address_space_get_flatview(as);
1568 FOR_EACH_FLAT_RANGE(fr, view) {
1570 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1573 flatview_unref(view);
1577 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1579 if (mr->readonly != readonly) {
1580 memory_region_transaction_begin();
1581 mr->readonly = readonly;
1582 memory_region_update_pending |= mr->enabled;
1583 memory_region_transaction_commit();
1587 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
1589 if (mr->romd_mode != romd_mode) {
1590 memory_region_transaction_begin();
1591 mr->romd_mode = romd_mode;
1592 memory_region_update_pending |= mr->enabled;
1593 memory_region_transaction_commit();
1597 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1598 hwaddr size, unsigned client)
1600 assert(mr->ram_block);
1601 cpu_physical_memory_test_and_clear_dirty(
1602 memory_region_get_ram_addr(mr) + addr, size, client);
1605 int memory_region_get_fd(MemoryRegion *mr)
1608 return memory_region_get_fd(mr->alias);
1611 assert(mr->ram_block);
1613 return qemu_get_ram_fd(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK);
1616 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1619 uint64_t offset = 0;
1623 offset += mr->alias_offset;
1626 assert(mr->ram_block);
1627 ptr = qemu_get_ram_ptr(mr->ram_block,
1628 memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK);
1631 return ptr + offset;
1634 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1636 return mr->ram_block ? mr->ram_block->offset : RAM_ADDR_INVALID;
1639 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp)
1641 assert(mr->ram_block);
1643 qemu_ram_resize(memory_region_get_ram_addr(mr), newsize, errp);
1646 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
1650 CoalescedMemoryRange *cmr;
1652 MemoryRegionSection section;
1654 view = address_space_get_flatview(as);
1655 FOR_EACH_FLAT_RANGE(fr, view) {
1657 section = (MemoryRegionSection) {
1658 .address_space = as,
1659 .offset_within_address_space = int128_get64(fr->addr.start),
1660 .size = fr->addr.size,
1663 MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, §ion,
1664 int128_get64(fr->addr.start),
1665 int128_get64(fr->addr.size));
1666 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1667 tmp = addrrange_shift(cmr->addr,
1668 int128_sub(fr->addr.start,
1669 int128_make64(fr->offset_in_region)));
1670 if (!addrrange_intersects(tmp, fr->addr)) {
1673 tmp = addrrange_intersection(tmp, fr->addr);
1674 MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, §ion,
1675 int128_get64(tmp.start),
1676 int128_get64(tmp.size));
1680 flatview_unref(view);
1683 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1687 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1688 memory_region_update_coalesced_range_as(mr, as);
1692 void memory_region_set_coalescing(MemoryRegion *mr)
1694 memory_region_clear_coalescing(mr);
1695 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1698 void memory_region_add_coalescing(MemoryRegion *mr,
1702 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1704 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1705 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1706 memory_region_update_coalesced_range(mr);
1707 memory_region_set_flush_coalesced(mr);
1710 void memory_region_clear_coalescing(MemoryRegion *mr)
1712 CoalescedMemoryRange *cmr;
1713 bool updated = false;
1715 qemu_flush_coalesced_mmio_buffer();
1716 mr->flush_coalesced_mmio = false;
1718 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1719 cmr = QTAILQ_FIRST(&mr->coalesced);
1720 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1726 memory_region_update_coalesced_range(mr);
1730 void memory_region_set_flush_coalesced(MemoryRegion *mr)
1732 mr->flush_coalesced_mmio = true;
1735 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
1737 qemu_flush_coalesced_mmio_buffer();
1738 if (QTAILQ_EMPTY(&mr->coalesced)) {
1739 mr->flush_coalesced_mmio = false;
1743 void memory_region_set_global_locking(MemoryRegion *mr)
1745 mr->global_locking = true;
1748 void memory_region_clear_global_locking(MemoryRegion *mr)
1750 mr->global_locking = false;
1753 static bool userspace_eventfd_warning;
1755 void memory_region_add_eventfd(MemoryRegion *mr,
1762 MemoryRegionIoeventfd mrfd = {
1763 .addr.start = int128_make64(addr),
1764 .addr.size = int128_make64(size),
1765 .match_data = match_data,
1771 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
1772 userspace_eventfd_warning))) {
1773 userspace_eventfd_warning = true;
1774 error_report("Using eventfd without MMIO binding in KVM. "
1775 "Suboptimal performance expected");
1779 adjust_endianness(mr, &mrfd.data, size);
1781 memory_region_transaction_begin();
1782 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1783 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1788 mr->ioeventfds = g_realloc(mr->ioeventfds,
1789 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1790 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1791 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1792 mr->ioeventfds[i] = mrfd;
1793 ioeventfd_update_pending |= mr->enabled;
1794 memory_region_transaction_commit();
1797 void memory_region_del_eventfd(MemoryRegion *mr,
1804 MemoryRegionIoeventfd mrfd = {
1805 .addr.start = int128_make64(addr),
1806 .addr.size = int128_make64(size),
1807 .match_data = match_data,
1814 adjust_endianness(mr, &mrfd.data, size);
1816 memory_region_transaction_begin();
1817 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1818 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1822 assert(i != mr->ioeventfd_nb);
1823 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1824 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1826 mr->ioeventfds = g_realloc(mr->ioeventfds,
1827 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1828 ioeventfd_update_pending |= mr->enabled;
1829 memory_region_transaction_commit();
1832 static void memory_region_update_container_subregions(MemoryRegion *subregion)
1834 hwaddr offset = subregion->addr;
1835 MemoryRegion *mr = subregion->container;
1836 MemoryRegion *other;
1838 memory_region_transaction_begin();
1840 memory_region_ref(subregion);
1841 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1842 if (subregion->may_overlap || other->may_overlap) {
1845 if (int128_ge(int128_make64(offset),
1846 int128_add(int128_make64(other->addr), other->size))
1847 || int128_le(int128_add(int128_make64(offset), subregion->size),
1848 int128_make64(other->addr))) {
1852 printf("warning: subregion collision %llx/%llx (%s) "
1853 "vs %llx/%llx (%s)\n",
1854 (unsigned long long)offset,
1855 (unsigned long long)int128_get64(subregion->size),
1857 (unsigned long long)other->addr,
1858 (unsigned long long)int128_get64(other->size),
1862 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1863 if (subregion->priority >= other->priority) {
1864 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1868 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1870 memory_region_update_pending |= mr->enabled && subregion->enabled;
1871 memory_region_transaction_commit();
1874 static void memory_region_add_subregion_common(MemoryRegion *mr,
1876 MemoryRegion *subregion)
1878 assert(!subregion->container);
1879 subregion->container = mr;
1880 subregion->addr = offset;
1881 memory_region_update_container_subregions(subregion);
1884 void memory_region_add_subregion(MemoryRegion *mr,
1886 MemoryRegion *subregion)
1888 subregion->may_overlap = false;
1889 subregion->priority = 0;
1890 memory_region_add_subregion_common(mr, offset, subregion);
1893 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1895 MemoryRegion *subregion,
1898 subregion->may_overlap = true;
1899 subregion->priority = priority;
1900 memory_region_add_subregion_common(mr, offset, subregion);
1903 void memory_region_del_subregion(MemoryRegion *mr,
1904 MemoryRegion *subregion)
1906 memory_region_transaction_begin();
1907 assert(subregion->container == mr);
1908 subregion->container = NULL;
1909 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1910 memory_region_unref(subregion);
1911 memory_region_update_pending |= mr->enabled && subregion->enabled;
1912 memory_region_transaction_commit();
1915 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1917 if (enabled == mr->enabled) {
1920 memory_region_transaction_begin();
1921 mr->enabled = enabled;
1922 memory_region_update_pending = true;
1923 memory_region_transaction_commit();
1926 void memory_region_set_size(MemoryRegion *mr, uint64_t size)
1928 Int128 s = int128_make64(size);
1930 if (size == UINT64_MAX) {
1933 if (int128_eq(s, mr->size)) {
1936 memory_region_transaction_begin();
1938 memory_region_update_pending = true;
1939 memory_region_transaction_commit();
1942 static void memory_region_readd_subregion(MemoryRegion *mr)
1944 MemoryRegion *container = mr->container;
1947 memory_region_transaction_begin();
1948 memory_region_ref(mr);
1949 memory_region_del_subregion(container, mr);
1950 mr->container = container;
1951 memory_region_update_container_subregions(mr);
1952 memory_region_unref(mr);
1953 memory_region_transaction_commit();
1957 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
1959 if (addr != mr->addr) {
1961 memory_region_readd_subregion(mr);
1965 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
1969 if (offset == mr->alias_offset) {
1973 memory_region_transaction_begin();
1974 mr->alias_offset = offset;
1975 memory_region_update_pending |= mr->enabled;
1976 memory_region_transaction_commit();
1979 uint64_t memory_region_get_alignment(const MemoryRegion *mr)
1984 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1986 const AddrRange *addr = addr_;
1987 const FlatRange *fr = fr_;
1989 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1991 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1997 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
1999 return bsearch(&addr, view->ranges, view->nr,
2000 sizeof(FlatRange), cmp_flatrange_addr);
2003 bool memory_region_is_mapped(MemoryRegion *mr)
2005 return mr->container ? true : false;
2008 /* Same as memory_region_find, but it does not add a reference to the
2009 * returned region. It must be called from an RCU critical section.
2011 static MemoryRegionSection memory_region_find_rcu(MemoryRegion *mr,
2012 hwaddr addr, uint64_t size)
2014 MemoryRegionSection ret = { .mr = NULL };
2022 for (root = mr; root->container; ) {
2023 root = root->container;
2027 as = memory_region_to_address_space(root);
2031 range = addrrange_make(int128_make64(addr), int128_make64(size));
2033 view = atomic_rcu_read(&as->current_map);
2034 fr = flatview_lookup(view, range);
2039 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
2044 ret.address_space = as;
2045 range = addrrange_intersection(range, fr->addr);
2046 ret.offset_within_region = fr->offset_in_region;
2047 ret.offset_within_region += int128_get64(int128_sub(range.start,
2049 ret.size = range.size;
2050 ret.offset_within_address_space = int128_get64(range.start);
2051 ret.readonly = fr->readonly;
2055 MemoryRegionSection memory_region_find(MemoryRegion *mr,
2056 hwaddr addr, uint64_t size)
2058 MemoryRegionSection ret;
2060 ret = memory_region_find_rcu(mr, addr, size);
2062 memory_region_ref(ret.mr);
2068 bool memory_region_present(MemoryRegion *container, hwaddr addr)
2073 mr = memory_region_find_rcu(container, addr, 1).mr;
2075 return mr && mr != container;
2078 void address_space_sync_dirty_bitmap(AddressSpace *as)
2083 view = address_space_get_flatview(as);
2084 FOR_EACH_FLAT_RANGE(fr, view) {
2085 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
2087 flatview_unref(view);
2090 void memory_global_dirty_log_start(void)
2092 global_dirty_log = true;
2094 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
2096 /* Refresh DIRTY_LOG_MIGRATION bit. */
2097 memory_region_transaction_begin();
2098 memory_region_update_pending = true;
2099 memory_region_transaction_commit();
2102 void memory_global_dirty_log_stop(void)
2104 global_dirty_log = false;
2106 /* Refresh DIRTY_LOG_MIGRATION bit. */
2107 memory_region_transaction_begin();
2108 memory_region_update_pending = true;
2109 memory_region_transaction_commit();
2111 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
2114 static void listener_add_address_space(MemoryListener *listener,
2120 if (listener->address_space_filter
2121 && listener->address_space_filter != as) {
2125 if (listener->begin) {
2126 listener->begin(listener);
2128 if (global_dirty_log) {
2129 if (listener->log_global_start) {
2130 listener->log_global_start(listener);
2134 view = address_space_get_flatview(as);
2135 FOR_EACH_FLAT_RANGE(fr, view) {
2136 MemoryRegionSection section = {
2138 .address_space = as,
2139 .offset_within_region = fr->offset_in_region,
2140 .size = fr->addr.size,
2141 .offset_within_address_space = int128_get64(fr->addr.start),
2142 .readonly = fr->readonly,
2144 if (fr->dirty_log_mask && listener->log_start) {
2145 listener->log_start(listener, §ion, 0, fr->dirty_log_mask);
2147 if (listener->region_add) {
2148 listener->region_add(listener, §ion);
2151 if (listener->commit) {
2152 listener->commit(listener);
2154 flatview_unref(view);
2157 void memory_listener_register(MemoryListener *listener, AddressSpace *filter)
2159 MemoryListener *other = NULL;
2162 listener->address_space_filter = filter;
2163 if (QTAILQ_EMPTY(&memory_listeners)
2164 || listener->priority >= QTAILQ_LAST(&memory_listeners,
2165 memory_listeners)->priority) {
2166 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
2168 QTAILQ_FOREACH(other, &memory_listeners, link) {
2169 if (listener->priority < other->priority) {
2173 QTAILQ_INSERT_BEFORE(other, listener, link);
2176 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2177 listener_add_address_space(listener, as);
2181 void memory_listener_unregister(MemoryListener *listener)
2183 QTAILQ_REMOVE(&memory_listeners, listener, link);
2186 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
2188 memory_region_ref(root);
2189 memory_region_transaction_begin();
2192 as->malloced = false;
2193 as->current_map = g_new(FlatView, 1);
2194 flatview_init(as->current_map);
2195 as->ioeventfd_nb = 0;
2196 as->ioeventfds = NULL;
2197 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
2198 as->name = g_strdup(name ? name : "anonymous");
2199 address_space_init_dispatch(as);
2200 memory_region_update_pending |= root->enabled;
2201 memory_region_transaction_commit();
2204 static void do_address_space_destroy(AddressSpace *as)
2206 MemoryListener *listener;
2207 bool do_free = as->malloced;
2209 address_space_destroy_dispatch(as);
2211 QTAILQ_FOREACH(listener, &memory_listeners, link) {
2212 assert(listener->address_space_filter != as);
2215 flatview_unref(as->current_map);
2217 g_free(as->ioeventfds);
2218 memory_region_unref(as->root);
2224 AddressSpace *address_space_init_shareable(MemoryRegion *root, const char *name)
2228 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2229 if (root == as->root && as->malloced) {
2235 as = g_malloc0(sizeof *as);
2236 address_space_init(as, root, name);
2237 as->malloced = true;
2241 void address_space_destroy(AddressSpace *as)
2243 MemoryRegion *root = as->root;
2246 if (as->ref_count) {
2249 /* Flush out anything from MemoryListeners listening in on this */
2250 memory_region_transaction_begin();
2252 memory_region_transaction_commit();
2253 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
2254 address_space_unregister(as);
2256 /* At this point, as->dispatch and as->current_map are dummy
2257 * entries that the guest should never use. Wait for the old
2258 * values to expire before freeing the data.
2261 call_rcu(as, do_address_space_destroy, rcu);
2264 typedef struct MemoryRegionList MemoryRegionList;
2266 struct MemoryRegionList {
2267 const MemoryRegion *mr;
2268 QTAILQ_ENTRY(MemoryRegionList) queue;
2271 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
2273 static void mtree_print_mr(fprintf_function mon_printf, void *f,
2274 const MemoryRegion *mr, unsigned int level,
2276 MemoryRegionListHead *alias_print_queue)
2278 MemoryRegionList *new_ml, *ml, *next_ml;
2279 MemoryRegionListHead submr_print_queue;
2280 const MemoryRegion *submr;
2287 for (i = 0; i < level; i++) {
2292 MemoryRegionList *ml;
2295 /* check if the alias is already in the queue */
2296 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
2297 if (ml->mr == mr->alias) {
2303 ml = g_new(MemoryRegionList, 1);
2305 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
2307 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
2308 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
2309 "-" TARGET_FMT_plx "%s\n",
2312 + (int128_nz(mr->size) ?
2313 (hwaddr)int128_get64(int128_sub(mr->size,
2314 int128_one())) : 0),
2316 mr->romd_mode ? 'R' : '-',
2317 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
2319 memory_region_name(mr),
2320 memory_region_name(mr->alias),
2323 + (int128_nz(mr->size) ?
2324 (hwaddr)int128_get64(int128_sub(mr->size,
2325 int128_one())) : 0),
2326 mr->enabled ? "" : " [disabled]");
2329 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %c%c): %s%s\n",
2332 + (int128_nz(mr->size) ?
2333 (hwaddr)int128_get64(int128_sub(mr->size,
2334 int128_one())) : 0),
2336 mr->romd_mode ? 'R' : '-',
2337 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
2339 memory_region_name(mr),
2340 mr->enabled ? "" : " [disabled]");
2343 QTAILQ_INIT(&submr_print_queue);
2345 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
2346 new_ml = g_new(MemoryRegionList, 1);
2348 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
2349 if (new_ml->mr->addr < ml->mr->addr ||
2350 (new_ml->mr->addr == ml->mr->addr &&
2351 new_ml->mr->priority > ml->mr->priority)) {
2352 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
2358 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
2362 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
2363 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
2367 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
2372 void mtree_info(fprintf_function mon_printf, void *f)
2374 MemoryRegionListHead ml_head;
2375 MemoryRegionList *ml, *ml2;
2378 QTAILQ_INIT(&ml_head);
2380 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2381 mon_printf(f, "address-space: %s\n", as->name);
2382 mtree_print_mr(mon_printf, f, as->root, 1, 0, &ml_head);
2383 mon_printf(f, "\n");
2386 /* print aliased regions */
2387 QTAILQ_FOREACH(ml, &ml_head, queue) {
2388 mon_printf(f, "memory-region: %s\n", memory_region_name(ml->mr));
2389 mtree_print_mr(mon_printf, f, ml->mr, 1, 0, &ml_head);
2390 mon_printf(f, "\n");
2393 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
2398 static const TypeInfo memory_region_info = {
2399 .parent = TYPE_OBJECT,
2400 .name = TYPE_MEMORY_REGION,
2401 .instance_size = sizeof(MemoryRegion),
2402 .instance_init = memory_region_initfn,
2403 .instance_finalize = memory_region_finalize,
2406 static void memory_register_types(void)
2408 type_register_static(&memory_region_info);
2411 type_init(memory_register_types)