cpus-common: nuke finish_safe_work
[qemu/ar7.git] / memory.c
blob4aa38eb5b1a3ec89700f49656794d0cd862a32bb
1 /*
2 * Physical memory management
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "cpu.h"
19 #include "exec/memory.h"
20 #include "exec/address-spaces.h"
21 #include "qapi/visitor.h"
22 #include "qemu/bitops.h"
23 #include "qemu/error-report.h"
24 #include "qemu/main-loop.h"
25 #include "qemu/qemu-print.h"
26 #include "qom/object.h"
27 #include "trace-root.h"
29 #include "exec/memory-internal.h"
30 #include "exec/ram_addr.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/runstate.h"
33 #include "sysemu/tcg.h"
34 #include "sysemu/accel.h"
35 #include "hw/boards.h"
36 #include "migration/vmstate.h"
38 //#define DEBUG_UNASSIGNED
40 static unsigned memory_region_transaction_depth;
41 static bool memory_region_update_pending;
42 static bool ioeventfd_update_pending;
43 bool global_dirty_log;
45 static QTAILQ_HEAD(, MemoryListener) memory_listeners
46 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
48 static QTAILQ_HEAD(, AddressSpace) address_spaces
49 = QTAILQ_HEAD_INITIALIZER(address_spaces);
51 static GHashTable *flat_views;
53 typedef struct AddrRange AddrRange;
56 * Note that signed integers are needed for negative offsetting in aliases
57 * (large MemoryRegion::alias_offset).
59 struct AddrRange {
60 Int128 start;
61 Int128 size;
64 static AddrRange addrrange_make(Int128 start, Int128 size)
66 return (AddrRange) { start, size };
69 static bool addrrange_equal(AddrRange r1, AddrRange r2)
71 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
74 static Int128 addrrange_end(AddrRange r)
76 return int128_add(r.start, r.size);
79 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
81 int128_addto(&range.start, delta);
82 return range;
85 static bool addrrange_contains(AddrRange range, Int128 addr)
87 return int128_ge(addr, range.start)
88 && int128_lt(addr, addrrange_end(range));
91 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
93 return addrrange_contains(r1, r2.start)
94 || addrrange_contains(r2, r1.start);
97 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
99 Int128 start = int128_max(r1.start, r2.start);
100 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
101 return addrrange_make(start, int128_sub(end, start));
104 enum ListenerDirection { Forward, Reverse };
106 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
107 do { \
108 MemoryListener *_listener; \
110 switch (_direction) { \
111 case Forward: \
112 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
113 if (_listener->_callback) { \
114 _listener->_callback(_listener, ##_args); \
117 break; \
118 case Reverse: \
119 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, link) { \
120 if (_listener->_callback) { \
121 _listener->_callback(_listener, ##_args); \
124 break; \
125 default: \
126 abort(); \
128 } while (0)
130 #define MEMORY_LISTENER_CALL(_as, _callback, _direction, _section, _args...) \
131 do { \
132 MemoryListener *_listener; \
134 switch (_direction) { \
135 case Forward: \
136 QTAILQ_FOREACH(_listener, &(_as)->listeners, link_as) { \
137 if (_listener->_callback) { \
138 _listener->_callback(_listener, _section, ##_args); \
141 break; \
142 case Reverse: \
143 QTAILQ_FOREACH_REVERSE(_listener, &(_as)->listeners, link_as) { \
144 if (_listener->_callback) { \
145 _listener->_callback(_listener, _section, ##_args); \
148 break; \
149 default: \
150 abort(); \
152 } while (0)
154 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
155 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
156 do { \
157 MemoryRegionSection mrs = section_from_flat_range(fr, \
158 address_space_to_flatview(as)); \
159 MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \
160 } while(0)
162 struct CoalescedMemoryRange {
163 AddrRange addr;
164 QTAILQ_ENTRY(CoalescedMemoryRange) link;
167 struct MemoryRegionIoeventfd {
168 AddrRange addr;
169 bool match_data;
170 uint64_t data;
171 EventNotifier *e;
174 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd *a,
175 MemoryRegionIoeventfd *b)
177 if (int128_lt(a->addr.start, b->addr.start)) {
178 return true;
179 } else if (int128_gt(a->addr.start, b->addr.start)) {
180 return false;
181 } else if (int128_lt(a->addr.size, b->addr.size)) {
182 return true;
183 } else if (int128_gt(a->addr.size, b->addr.size)) {
184 return false;
185 } else if (a->match_data < b->match_data) {
186 return true;
187 } else if (a->match_data > b->match_data) {
188 return false;
189 } else if (a->match_data) {
190 if (a->data < b->data) {
191 return true;
192 } else if (a->data > b->data) {
193 return false;
196 if (a->e < b->e) {
197 return true;
198 } else if (a->e > b->e) {
199 return false;
201 return false;
204 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd *a,
205 MemoryRegionIoeventfd *b)
207 return !memory_region_ioeventfd_before(a, b)
208 && !memory_region_ioeventfd_before(b, a);
211 /* Range of memory in the global map. Addresses are absolute. */
212 struct FlatRange {
213 MemoryRegion *mr;
214 hwaddr offset_in_region;
215 AddrRange addr;
216 uint8_t dirty_log_mask;
217 bool romd_mode;
218 bool readonly;
219 bool nonvolatile;
220 int has_coalesced_range;
223 #define FOR_EACH_FLAT_RANGE(var, view) \
224 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
226 static inline MemoryRegionSection
227 section_from_flat_range(FlatRange *fr, FlatView *fv)
229 return (MemoryRegionSection) {
230 .mr = fr->mr,
231 .fv = fv,
232 .offset_within_region = fr->offset_in_region,
233 .size = fr->addr.size,
234 .offset_within_address_space = int128_get64(fr->addr.start),
235 .readonly = fr->readonly,
236 .nonvolatile = fr->nonvolatile,
240 static bool flatrange_equal(FlatRange *a, FlatRange *b)
242 return a->mr == b->mr
243 && addrrange_equal(a->addr, b->addr)
244 && a->offset_in_region == b->offset_in_region
245 && a->romd_mode == b->romd_mode
246 && a->readonly == b->readonly
247 && a->nonvolatile == b->nonvolatile;
250 static FlatView *flatview_new(MemoryRegion *mr_root)
252 FlatView *view;
254 view = g_new0(FlatView, 1);
255 view->ref = 1;
256 view->root = mr_root;
257 memory_region_ref(mr_root);
258 trace_flatview_new(view, mr_root);
260 return view;
263 /* Insert a range into a given position. Caller is responsible for maintaining
264 * sorting order.
266 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
268 if (view->nr == view->nr_allocated) {
269 view->nr_allocated = MAX(2 * view->nr, 10);
270 view->ranges = g_realloc(view->ranges,
271 view->nr_allocated * sizeof(*view->ranges));
273 memmove(view->ranges + pos + 1, view->ranges + pos,
274 (view->nr - pos) * sizeof(FlatRange));
275 view->ranges[pos] = *range;
276 memory_region_ref(range->mr);
277 ++view->nr;
280 static void flatview_destroy(FlatView *view)
282 int i;
284 trace_flatview_destroy(view, view->root);
285 if (view->dispatch) {
286 address_space_dispatch_free(view->dispatch);
288 for (i = 0; i < view->nr; i++) {
289 memory_region_unref(view->ranges[i].mr);
291 g_free(view->ranges);
292 memory_region_unref(view->root);
293 g_free(view);
296 static bool flatview_ref(FlatView *view)
298 return atomic_fetch_inc_nonzero(&view->ref) > 0;
301 void flatview_unref(FlatView *view)
303 if (atomic_fetch_dec(&view->ref) == 1) {
304 trace_flatview_destroy_rcu(view, view->root);
305 assert(view->root);
306 call_rcu(view, flatview_destroy, rcu);
310 static bool can_merge(FlatRange *r1, FlatRange *r2)
312 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
313 && r1->mr == r2->mr
314 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
315 r1->addr.size),
316 int128_make64(r2->offset_in_region))
317 && r1->dirty_log_mask == r2->dirty_log_mask
318 && r1->romd_mode == r2->romd_mode
319 && r1->readonly == r2->readonly
320 && r1->nonvolatile == r2->nonvolatile;
323 /* Attempt to simplify a view by merging adjacent ranges */
324 static void flatview_simplify(FlatView *view)
326 unsigned i, j, k;
328 i = 0;
329 while (i < view->nr) {
330 j = i + 1;
331 while (j < view->nr
332 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
333 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
334 ++j;
336 ++i;
337 for (k = i; k < j; k++) {
338 memory_region_unref(view->ranges[k].mr);
340 memmove(&view->ranges[i], &view->ranges[j],
341 (view->nr - j) * sizeof(view->ranges[j]));
342 view->nr -= j - i;
346 static bool memory_region_big_endian(MemoryRegion *mr)
348 #ifdef TARGET_WORDS_BIGENDIAN
349 return mr->ops->endianness != DEVICE_LITTLE_ENDIAN;
350 #else
351 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
352 #endif
355 static bool memory_region_wrong_endianness(MemoryRegion *mr)
357 #ifdef TARGET_WORDS_BIGENDIAN
358 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
359 #else
360 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
361 #endif
364 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
366 if (memory_region_wrong_endianness(mr)) {
367 switch (size) {
368 case 1:
369 break;
370 case 2:
371 *data = bswap16(*data);
372 break;
373 case 4:
374 *data = bswap32(*data);
375 break;
376 case 8:
377 *data = bswap64(*data);
378 break;
379 default:
380 abort();
385 static inline void memory_region_shift_read_access(uint64_t *value,
386 signed shift,
387 uint64_t mask,
388 uint64_t tmp)
390 if (shift >= 0) {
391 *value |= (tmp & mask) << shift;
392 } else {
393 *value |= (tmp & mask) >> -shift;
397 static inline uint64_t memory_region_shift_write_access(uint64_t *value,
398 signed shift,
399 uint64_t mask)
401 uint64_t tmp;
403 if (shift >= 0) {
404 tmp = (*value >> shift) & mask;
405 } else {
406 tmp = (*value << -shift) & mask;
409 return tmp;
412 static hwaddr memory_region_to_absolute_addr(MemoryRegion *mr, hwaddr offset)
414 MemoryRegion *root;
415 hwaddr abs_addr = offset;
417 abs_addr += mr->addr;
418 for (root = mr; root->container; ) {
419 root = root->container;
420 abs_addr += root->addr;
423 return abs_addr;
426 static int get_cpu_index(void)
428 if (current_cpu) {
429 return current_cpu->cpu_index;
431 return -1;
434 static MemTxResult memory_region_read_accessor(MemoryRegion *mr,
435 hwaddr addr,
436 uint64_t *value,
437 unsigned size,
438 signed shift,
439 uint64_t mask,
440 MemTxAttrs attrs)
442 uint64_t tmp;
444 tmp = mr->ops->read(mr->opaque, addr, size);
445 if (mr->subpage) {
446 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
447 } else if (mr == &io_mem_notdirty) {
448 /* Accesses to code which has previously been translated into a TB show
449 * up in the MMIO path, as accesses to the io_mem_notdirty
450 * MemoryRegion. */
451 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
452 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
453 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
454 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
456 memory_region_shift_read_access(value, shift, mask, tmp);
457 return MEMTX_OK;
460 static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr,
461 hwaddr addr,
462 uint64_t *value,
463 unsigned size,
464 signed shift,
465 uint64_t mask,
466 MemTxAttrs attrs)
468 uint64_t tmp = 0;
469 MemTxResult r;
471 r = mr->ops->read_with_attrs(mr->opaque, addr, &tmp, size, attrs);
472 if (mr->subpage) {
473 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
474 } else if (mr == &io_mem_notdirty) {
475 /* Accesses to code which has previously been translated into a TB show
476 * up in the MMIO path, as accesses to the io_mem_notdirty
477 * MemoryRegion. */
478 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
479 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
480 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
481 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
483 memory_region_shift_read_access(value, shift, mask, tmp);
484 return r;
487 static MemTxResult memory_region_write_accessor(MemoryRegion *mr,
488 hwaddr addr,
489 uint64_t *value,
490 unsigned size,
491 signed shift,
492 uint64_t mask,
493 MemTxAttrs attrs)
495 uint64_t tmp = memory_region_shift_write_access(value, shift, mask);
497 if (mr->subpage) {
498 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
499 } else if (mr == &io_mem_notdirty) {
500 /* Accesses to code which has previously been translated into a TB show
501 * up in the MMIO path, as accesses to the io_mem_notdirty
502 * MemoryRegion. */
503 trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
504 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
505 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
506 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
508 mr->ops->write(mr->opaque, addr, tmp, size);
509 return MEMTX_OK;
512 static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr,
513 hwaddr addr,
514 uint64_t *value,
515 unsigned size,
516 signed shift,
517 uint64_t mask,
518 MemTxAttrs attrs)
520 uint64_t tmp = memory_region_shift_write_access(value, shift, mask);
522 if (mr->subpage) {
523 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
524 } else if (mr == &io_mem_notdirty) {
525 /* Accesses to code which has previously been translated into a TB show
526 * up in the MMIO path, as accesses to the io_mem_notdirty
527 * MemoryRegion. */
528 trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
529 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
530 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
531 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
533 return mr->ops->write_with_attrs(mr->opaque, addr, tmp, size, attrs);
536 static MemTxResult access_with_adjusted_size(hwaddr addr,
537 uint64_t *value,
538 unsigned size,
539 unsigned access_size_min,
540 unsigned access_size_max,
541 MemTxResult (*access_fn)
542 (MemoryRegion *mr,
543 hwaddr addr,
544 uint64_t *value,
545 unsigned size,
546 signed shift,
547 uint64_t mask,
548 MemTxAttrs attrs),
549 MemoryRegion *mr,
550 MemTxAttrs attrs)
552 uint64_t access_mask;
553 unsigned access_size;
554 unsigned i;
555 MemTxResult r = MEMTX_OK;
557 if (!access_size_min) {
558 access_size_min = 1;
560 if (!access_size_max) {
561 access_size_max = 4;
564 /* FIXME: support unaligned access? */
565 access_size = MAX(MIN(size, access_size_max), access_size_min);
566 access_mask = MAKE_64BIT_MASK(0, access_size * 8);
567 if (memory_region_big_endian(mr)) {
568 for (i = 0; i < size; i += access_size) {
569 r |= access_fn(mr, addr + i, value, access_size,
570 (size - access_size - i) * 8, access_mask, attrs);
572 } else {
573 for (i = 0; i < size; i += access_size) {
574 r |= access_fn(mr, addr + i, value, access_size, i * 8,
575 access_mask, attrs);
578 return r;
581 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
583 AddressSpace *as;
585 while (mr->container) {
586 mr = mr->container;
588 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
589 if (mr == as->root) {
590 return as;
593 return NULL;
596 /* Render a memory region into the global view. Ranges in @view obscure
597 * ranges in @mr.
599 static void render_memory_region(FlatView *view,
600 MemoryRegion *mr,
601 Int128 base,
602 AddrRange clip,
603 bool readonly,
604 bool nonvolatile)
606 MemoryRegion *subregion;
607 unsigned i;
608 hwaddr offset_in_region;
609 Int128 remain;
610 Int128 now;
611 FlatRange fr;
612 AddrRange tmp;
614 if (!mr->enabled) {
615 return;
618 int128_addto(&base, int128_make64(mr->addr));
619 readonly |= mr->readonly;
620 nonvolatile |= mr->nonvolatile;
622 tmp = addrrange_make(base, mr->size);
624 if (!addrrange_intersects(tmp, clip)) {
625 return;
628 clip = addrrange_intersection(tmp, clip);
630 if (mr->alias) {
631 int128_subfrom(&base, int128_make64(mr->alias->addr));
632 int128_subfrom(&base, int128_make64(mr->alias_offset));
633 render_memory_region(view, mr->alias, base, clip,
634 readonly, nonvolatile);
635 return;
638 /* Render subregions in priority order. */
639 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
640 render_memory_region(view, subregion, base, clip,
641 readonly, nonvolatile);
644 if (!mr->terminates) {
645 return;
648 offset_in_region = int128_get64(int128_sub(clip.start, base));
649 base = clip.start;
650 remain = clip.size;
652 fr.mr = mr;
653 fr.dirty_log_mask = memory_region_get_dirty_log_mask(mr);
654 fr.romd_mode = mr->romd_mode;
655 fr.readonly = readonly;
656 fr.nonvolatile = nonvolatile;
657 fr.has_coalesced_range = 0;
659 /* Render the region itself into any gaps left by the current view. */
660 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
661 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
662 continue;
664 if (int128_lt(base, view->ranges[i].addr.start)) {
665 now = int128_min(remain,
666 int128_sub(view->ranges[i].addr.start, base));
667 fr.offset_in_region = offset_in_region;
668 fr.addr = addrrange_make(base, now);
669 flatview_insert(view, i, &fr);
670 ++i;
671 int128_addto(&base, now);
672 offset_in_region += int128_get64(now);
673 int128_subfrom(&remain, now);
675 now = int128_sub(int128_min(int128_add(base, remain),
676 addrrange_end(view->ranges[i].addr)),
677 base);
678 int128_addto(&base, now);
679 offset_in_region += int128_get64(now);
680 int128_subfrom(&remain, now);
682 if (int128_nz(remain)) {
683 fr.offset_in_region = offset_in_region;
684 fr.addr = addrrange_make(base, remain);
685 flatview_insert(view, i, &fr);
689 static MemoryRegion *memory_region_get_flatview_root(MemoryRegion *mr)
691 while (mr->enabled) {
692 if (mr->alias) {
693 if (!mr->alias_offset && int128_ge(mr->size, mr->alias->size)) {
694 /* The alias is included in its entirety. Use it as
695 * the "real" root, so that we can share more FlatViews.
697 mr = mr->alias;
698 continue;
700 } else if (!mr->terminates) {
701 unsigned int found = 0;
702 MemoryRegion *child, *next = NULL;
703 QTAILQ_FOREACH(child, &mr->subregions, subregions_link) {
704 if (child->enabled) {
705 if (++found > 1) {
706 next = NULL;
707 break;
709 if (!child->addr && int128_ge(mr->size, child->size)) {
710 /* A child is included in its entirety. If it's the only
711 * enabled one, use it in the hope of finding an alias down the
712 * way. This will also let us share FlatViews.
714 next = child;
718 if (found == 0) {
719 return NULL;
721 if (next) {
722 mr = next;
723 continue;
727 return mr;
730 return NULL;
733 /* Render a memory topology into a list of disjoint absolute ranges. */
734 static FlatView *generate_memory_topology(MemoryRegion *mr)
736 int i;
737 FlatView *view;
739 view = flatview_new(mr);
741 if (mr) {
742 render_memory_region(view, mr, int128_zero(),
743 addrrange_make(int128_zero(), int128_2_64()),
744 false, false);
746 flatview_simplify(view);
748 view->dispatch = address_space_dispatch_new(view);
749 for (i = 0; i < view->nr; i++) {
750 MemoryRegionSection mrs =
751 section_from_flat_range(&view->ranges[i], view);
752 flatview_add_to_dispatch(view, &mrs);
754 address_space_dispatch_compact(view->dispatch);
755 g_hash_table_replace(flat_views, mr, view);
757 return view;
760 static void address_space_add_del_ioeventfds(AddressSpace *as,
761 MemoryRegionIoeventfd *fds_new,
762 unsigned fds_new_nb,
763 MemoryRegionIoeventfd *fds_old,
764 unsigned fds_old_nb)
766 unsigned iold, inew;
767 MemoryRegionIoeventfd *fd;
768 MemoryRegionSection section;
770 /* Generate a symmetric difference of the old and new fd sets, adding
771 * and deleting as necessary.
774 iold = inew = 0;
775 while (iold < fds_old_nb || inew < fds_new_nb) {
776 if (iold < fds_old_nb
777 && (inew == fds_new_nb
778 || memory_region_ioeventfd_before(&fds_old[iold],
779 &fds_new[inew]))) {
780 fd = &fds_old[iold];
781 section = (MemoryRegionSection) {
782 .fv = address_space_to_flatview(as),
783 .offset_within_address_space = int128_get64(fd->addr.start),
784 .size = fd->addr.size,
786 MEMORY_LISTENER_CALL(as, eventfd_del, Forward, &section,
787 fd->match_data, fd->data, fd->e);
788 ++iold;
789 } else if (inew < fds_new_nb
790 && (iold == fds_old_nb
791 || memory_region_ioeventfd_before(&fds_new[inew],
792 &fds_old[iold]))) {
793 fd = &fds_new[inew];
794 section = (MemoryRegionSection) {
795 .fv = address_space_to_flatview(as),
796 .offset_within_address_space = int128_get64(fd->addr.start),
797 .size = fd->addr.size,
799 MEMORY_LISTENER_CALL(as, eventfd_add, Reverse, &section,
800 fd->match_data, fd->data, fd->e);
801 ++inew;
802 } else {
803 ++iold;
804 ++inew;
809 FlatView *address_space_get_flatview(AddressSpace *as)
811 FlatView *view;
813 rcu_read_lock();
814 do {
815 view = address_space_to_flatview(as);
816 /* If somebody has replaced as->current_map concurrently,
817 * flatview_ref returns false.
819 } while (!flatview_ref(view));
820 rcu_read_unlock();
821 return view;
824 static void address_space_update_ioeventfds(AddressSpace *as)
826 FlatView *view;
827 FlatRange *fr;
828 unsigned ioeventfd_nb = 0;
829 MemoryRegionIoeventfd *ioeventfds = NULL;
830 AddrRange tmp;
831 unsigned i;
833 view = address_space_get_flatview(as);
834 FOR_EACH_FLAT_RANGE(fr, view) {
835 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
836 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
837 int128_sub(fr->addr.start,
838 int128_make64(fr->offset_in_region)));
839 if (addrrange_intersects(fr->addr, tmp)) {
840 ++ioeventfd_nb;
841 ioeventfds = g_realloc(ioeventfds,
842 ioeventfd_nb * sizeof(*ioeventfds));
843 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
844 ioeventfds[ioeventfd_nb-1].addr = tmp;
849 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
850 as->ioeventfds, as->ioeventfd_nb);
852 g_free(as->ioeventfds);
853 as->ioeventfds = ioeventfds;
854 as->ioeventfd_nb = ioeventfd_nb;
855 flatview_unref(view);
858 static void flat_range_coalesced_io_del(FlatRange *fr, AddressSpace *as)
860 if (!fr->has_coalesced_range) {
861 return;
864 if (--fr->has_coalesced_range > 0) {
865 return;
868 MEMORY_LISTENER_UPDATE_REGION(fr, as, Reverse, coalesced_io_del,
869 int128_get64(fr->addr.start),
870 int128_get64(fr->addr.size));
873 static void flat_range_coalesced_io_add(FlatRange *fr, AddressSpace *as)
875 MemoryRegion *mr = fr->mr;
876 CoalescedMemoryRange *cmr;
877 AddrRange tmp;
879 if (QTAILQ_EMPTY(&mr->coalesced)) {
880 return;
883 if (fr->has_coalesced_range++) {
884 return;
887 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
888 tmp = addrrange_shift(cmr->addr,
889 int128_sub(fr->addr.start,
890 int128_make64(fr->offset_in_region)));
891 if (!addrrange_intersects(tmp, fr->addr)) {
892 continue;
894 tmp = addrrange_intersection(tmp, fr->addr);
895 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, coalesced_io_add,
896 int128_get64(tmp.start),
897 int128_get64(tmp.size));
901 static void address_space_update_topology_pass(AddressSpace *as,
902 const FlatView *old_view,
903 const FlatView *new_view,
904 bool adding)
906 unsigned iold, inew;
907 FlatRange *frold, *frnew;
909 /* Generate a symmetric difference of the old and new memory maps.
910 * Kill ranges in the old map, and instantiate ranges in the new map.
912 iold = inew = 0;
913 while (iold < old_view->nr || inew < new_view->nr) {
914 if (iold < old_view->nr) {
915 frold = &old_view->ranges[iold];
916 } else {
917 frold = NULL;
919 if (inew < new_view->nr) {
920 frnew = &new_view->ranges[inew];
921 } else {
922 frnew = NULL;
925 if (frold
926 && (!frnew
927 || int128_lt(frold->addr.start, frnew->addr.start)
928 || (int128_eq(frold->addr.start, frnew->addr.start)
929 && !flatrange_equal(frold, frnew)))) {
930 /* In old but not in new, or in both but attributes changed. */
932 if (!adding) {
933 flat_range_coalesced_io_del(frold, as);
934 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
937 ++iold;
938 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
939 /* In both and unchanged (except logging may have changed) */
941 if (adding) {
942 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
943 if (frnew->dirty_log_mask & ~frold->dirty_log_mask) {
944 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start,
945 frold->dirty_log_mask,
946 frnew->dirty_log_mask);
948 if (frold->dirty_log_mask & ~frnew->dirty_log_mask) {
949 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop,
950 frold->dirty_log_mask,
951 frnew->dirty_log_mask);
955 ++iold;
956 ++inew;
957 } else {
958 /* In new */
960 if (adding) {
961 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
962 flat_range_coalesced_io_add(frnew, as);
965 ++inew;
970 static void flatviews_init(void)
972 static FlatView *empty_view;
974 if (flat_views) {
975 return;
978 flat_views = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
979 (GDestroyNotify) flatview_unref);
980 if (!empty_view) {
981 empty_view = generate_memory_topology(NULL);
982 /* We keep it alive forever in the global variable. */
983 flatview_ref(empty_view);
984 } else {
985 g_hash_table_replace(flat_views, NULL, empty_view);
986 flatview_ref(empty_view);
990 static void flatviews_reset(void)
992 AddressSpace *as;
994 if (flat_views) {
995 g_hash_table_unref(flat_views);
996 flat_views = NULL;
998 flatviews_init();
1000 /* Render unique FVs */
1001 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1002 MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
1004 if (g_hash_table_lookup(flat_views, physmr)) {
1005 continue;
1008 generate_memory_topology(physmr);
1012 static void address_space_set_flatview(AddressSpace *as)
1014 FlatView *old_view = address_space_to_flatview(as);
1015 MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
1016 FlatView *new_view = g_hash_table_lookup(flat_views, physmr);
1018 assert(new_view);
1020 if (old_view == new_view) {
1021 return;
1024 if (old_view) {
1025 flatview_ref(old_view);
1028 flatview_ref(new_view);
1030 if (!QTAILQ_EMPTY(&as->listeners)) {
1031 FlatView tmpview = { .nr = 0 }, *old_view2 = old_view;
1033 if (!old_view2) {
1034 old_view2 = &tmpview;
1036 address_space_update_topology_pass(as, old_view2, new_view, false);
1037 address_space_update_topology_pass(as, old_view2, new_view, true);
1040 /* Writes are protected by the BQL. */
1041 atomic_rcu_set(&as->current_map, new_view);
1042 if (old_view) {
1043 flatview_unref(old_view);
1046 /* Note that all the old MemoryRegions are still alive up to this
1047 * point. This relieves most MemoryListeners from the need to
1048 * ref/unref the MemoryRegions they get---unless they use them
1049 * outside the iothread mutex, in which case precise reference
1050 * counting is necessary.
1052 if (old_view) {
1053 flatview_unref(old_view);
1057 static void address_space_update_topology(AddressSpace *as)
1059 MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
1061 flatviews_init();
1062 if (!g_hash_table_lookup(flat_views, physmr)) {
1063 generate_memory_topology(physmr);
1065 address_space_set_flatview(as);
1068 void memory_region_transaction_begin(void)
1070 qemu_flush_coalesced_mmio_buffer();
1071 ++memory_region_transaction_depth;
1074 void memory_region_transaction_commit(void)
1076 AddressSpace *as;
1078 assert(memory_region_transaction_depth);
1079 assert(qemu_mutex_iothread_locked());
1081 --memory_region_transaction_depth;
1082 if (!memory_region_transaction_depth) {
1083 if (memory_region_update_pending) {
1084 flatviews_reset();
1086 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
1088 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1089 address_space_set_flatview(as);
1090 address_space_update_ioeventfds(as);
1092 memory_region_update_pending = false;
1093 ioeventfd_update_pending = false;
1094 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
1095 } else if (ioeventfd_update_pending) {
1096 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1097 address_space_update_ioeventfds(as);
1099 ioeventfd_update_pending = false;
1104 static void memory_region_destructor_none(MemoryRegion *mr)
1108 static void memory_region_destructor_ram(MemoryRegion *mr)
1110 qemu_ram_free(mr->ram_block);
1113 static bool memory_region_need_escape(char c)
1115 return c == '/' || c == '[' || c == '\\' || c == ']';
1118 static char *memory_region_escape_name(const char *name)
1120 const char *p;
1121 char *escaped, *q;
1122 uint8_t c;
1123 size_t bytes = 0;
1125 for (p = name; *p; p++) {
1126 bytes += memory_region_need_escape(*p) ? 4 : 1;
1128 if (bytes == p - name) {
1129 return g_memdup(name, bytes + 1);
1132 escaped = g_malloc(bytes + 1);
1133 for (p = name, q = escaped; *p; p++) {
1134 c = *p;
1135 if (unlikely(memory_region_need_escape(c))) {
1136 *q++ = '\\';
1137 *q++ = 'x';
1138 *q++ = "0123456789abcdef"[c >> 4];
1139 c = "0123456789abcdef"[c & 15];
1141 *q++ = c;
1143 *q = 0;
1144 return escaped;
1147 static void memory_region_do_init(MemoryRegion *mr,
1148 Object *owner,
1149 const char *name,
1150 uint64_t size)
1152 mr->size = int128_make64(size);
1153 if (size == UINT64_MAX) {
1154 mr->size = int128_2_64();
1156 mr->name = g_strdup(name);
1157 mr->owner = owner;
1158 mr->ram_block = NULL;
1160 if (name) {
1161 char *escaped_name = memory_region_escape_name(name);
1162 char *name_array = g_strdup_printf("%s[*]", escaped_name);
1164 if (!owner) {
1165 owner = container_get(qdev_get_machine(), "/unattached");
1168 object_property_add_child(owner, name_array, OBJECT(mr), &error_abort);
1169 object_unref(OBJECT(mr));
1170 g_free(name_array);
1171 g_free(escaped_name);
1175 void memory_region_init(MemoryRegion *mr,
1176 Object *owner,
1177 const char *name,
1178 uint64_t size)
1180 object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION);
1181 memory_region_do_init(mr, owner, name, size);
1184 static void memory_region_get_addr(Object *obj, Visitor *v, const char *name,
1185 void *opaque, Error **errp)
1187 MemoryRegion *mr = MEMORY_REGION(obj);
1188 uint64_t value = mr->addr;
1190 visit_type_uint64(v, name, &value, errp);
1193 static void memory_region_get_container(Object *obj, Visitor *v,
1194 const char *name, void *opaque,
1195 Error **errp)
1197 MemoryRegion *mr = MEMORY_REGION(obj);
1198 gchar *path = (gchar *)"";
1200 if (mr->container) {
1201 path = object_get_canonical_path(OBJECT(mr->container));
1203 visit_type_str(v, name, &path, errp);
1204 if (mr->container) {
1205 g_free(path);
1209 static Object *memory_region_resolve_container(Object *obj, void *opaque,
1210 const char *part)
1212 MemoryRegion *mr = MEMORY_REGION(obj);
1214 return OBJECT(mr->container);
1217 static void memory_region_get_priority(Object *obj, Visitor *v,
1218 const char *name, void *opaque,
1219 Error **errp)
1221 MemoryRegion *mr = MEMORY_REGION(obj);
1222 int32_t value = mr->priority;
1224 visit_type_int32(v, name, &value, errp);
1227 static void memory_region_get_size(Object *obj, Visitor *v, const char *name,
1228 void *opaque, Error **errp)
1230 MemoryRegion *mr = MEMORY_REGION(obj);
1231 uint64_t value = memory_region_size(mr);
1233 visit_type_uint64(v, name, &value, errp);
1236 static void memory_region_initfn(Object *obj)
1238 MemoryRegion *mr = MEMORY_REGION(obj);
1239 ObjectProperty *op;
1241 mr->ops = &unassigned_mem_ops;
1242 mr->enabled = true;
1243 mr->romd_mode = true;
1244 mr->global_locking = true;
1245 mr->destructor = memory_region_destructor_none;
1246 QTAILQ_INIT(&mr->subregions);
1247 QTAILQ_INIT(&mr->coalesced);
1249 op = object_property_add(OBJECT(mr), "container",
1250 "link<" TYPE_MEMORY_REGION ">",
1251 memory_region_get_container,
1252 NULL, /* memory_region_set_container */
1253 NULL, NULL, &error_abort);
1254 op->resolve = memory_region_resolve_container;
1256 object_property_add(OBJECT(mr), "addr", "uint64",
1257 memory_region_get_addr,
1258 NULL, /* memory_region_set_addr */
1259 NULL, NULL, &error_abort);
1260 object_property_add(OBJECT(mr), "priority", "uint32",
1261 memory_region_get_priority,
1262 NULL, /* memory_region_set_priority */
1263 NULL, NULL, &error_abort);
1264 object_property_add(OBJECT(mr), "size", "uint64",
1265 memory_region_get_size,
1266 NULL, /* memory_region_set_size, */
1267 NULL, NULL, &error_abort);
1270 static void iommu_memory_region_initfn(Object *obj)
1272 MemoryRegion *mr = MEMORY_REGION(obj);
1274 mr->is_iommu = true;
1277 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
1278 unsigned size)
1280 #ifdef DEBUG_UNASSIGNED
1281 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
1282 #endif
1283 if (current_cpu != NULL) {
1284 bool is_exec = current_cpu->mem_io_access_type == MMU_INST_FETCH;
1285 cpu_unassigned_access(current_cpu, addr, false, is_exec, 0, size);
1287 return 0;
1290 static void unassigned_mem_write(void *opaque, hwaddr addr,
1291 uint64_t val, unsigned size)
1293 #ifdef DEBUG_UNASSIGNED
1294 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
1295 #endif
1296 if (current_cpu != NULL) {
1297 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
1301 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
1302 unsigned size, bool is_write,
1303 MemTxAttrs attrs)
1305 return false;
1308 const MemoryRegionOps unassigned_mem_ops = {
1309 .valid.accepts = unassigned_mem_accepts,
1310 .endianness = DEVICE_NATIVE_ENDIAN,
1313 static uint64_t memory_region_ram_device_read(void *opaque,
1314 hwaddr addr, unsigned size)
1316 MemoryRegion *mr = opaque;
1317 uint64_t data = (uint64_t)~0;
1319 switch (size) {
1320 case 1:
1321 data = *(uint8_t *)(mr->ram_block->host + addr);
1322 break;
1323 case 2:
1324 data = *(uint16_t *)(mr->ram_block->host + addr);
1325 break;
1326 case 4:
1327 data = *(uint32_t *)(mr->ram_block->host + addr);
1328 break;
1329 case 8:
1330 data = *(uint64_t *)(mr->ram_block->host + addr);
1331 break;
1334 trace_memory_region_ram_device_read(get_cpu_index(), mr, addr, data, size);
1336 return data;
1339 static void memory_region_ram_device_write(void *opaque, hwaddr addr,
1340 uint64_t data, unsigned size)
1342 MemoryRegion *mr = opaque;
1344 trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size);
1346 switch (size) {
1347 case 1:
1348 *(uint8_t *)(mr->ram_block->host + addr) = (uint8_t)data;
1349 break;
1350 case 2:
1351 *(uint16_t *)(mr->ram_block->host + addr) = (uint16_t)data;
1352 break;
1353 case 4:
1354 *(uint32_t *)(mr->ram_block->host + addr) = (uint32_t)data;
1355 break;
1356 case 8:
1357 *(uint64_t *)(mr->ram_block->host + addr) = data;
1358 break;
1362 static const MemoryRegionOps ram_device_mem_ops = {
1363 .read = memory_region_ram_device_read,
1364 .write = memory_region_ram_device_write,
1365 .endianness = DEVICE_HOST_ENDIAN,
1366 .valid = {
1367 .min_access_size = 1,
1368 .max_access_size = 8,
1369 .unaligned = true,
1371 .impl = {
1372 .min_access_size = 1,
1373 .max_access_size = 8,
1374 .unaligned = true,
1378 bool memory_region_access_valid(MemoryRegion *mr,
1379 hwaddr addr,
1380 unsigned size,
1381 bool is_write,
1382 MemTxAttrs attrs)
1384 int access_size_min, access_size_max;
1385 int access_size, i;
1387 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
1388 return false;
1391 if (!mr->ops->valid.accepts) {
1392 return true;
1395 access_size_min = mr->ops->valid.min_access_size;
1396 if (!mr->ops->valid.min_access_size) {
1397 access_size_min = 1;
1400 access_size_max = mr->ops->valid.max_access_size;
1401 if (!mr->ops->valid.max_access_size) {
1402 access_size_max = 4;
1405 access_size = MAX(MIN(size, access_size_max), access_size_min);
1406 for (i = 0; i < size; i += access_size) {
1407 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
1408 is_write, attrs)) {
1409 return false;
1413 return true;
1416 static MemTxResult memory_region_dispatch_read1(MemoryRegion *mr,
1417 hwaddr addr,
1418 uint64_t *pval,
1419 unsigned size,
1420 MemTxAttrs attrs)
1422 *pval = 0;
1424 if (mr->ops->read) {
1425 return access_with_adjusted_size(addr, pval, size,
1426 mr->ops->impl.min_access_size,
1427 mr->ops->impl.max_access_size,
1428 memory_region_read_accessor,
1429 mr, attrs);
1430 } else {
1431 return access_with_adjusted_size(addr, pval, size,
1432 mr->ops->impl.min_access_size,
1433 mr->ops->impl.max_access_size,
1434 memory_region_read_with_attrs_accessor,
1435 mr, attrs);
1439 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1440 hwaddr addr,
1441 uint64_t *pval,
1442 unsigned size,
1443 MemTxAttrs attrs)
1445 MemTxResult r;
1447 if (!memory_region_access_valid(mr, addr, size, false, attrs)) {
1448 *pval = unassigned_mem_read(mr, addr, size);
1449 return MEMTX_DECODE_ERROR;
1452 r = memory_region_dispatch_read1(mr, addr, pval, size, attrs);
1453 adjust_endianness(mr, pval, size);
1454 return r;
1457 /* Return true if an eventfd was signalled */
1458 static bool memory_region_dispatch_write_eventfds(MemoryRegion *mr,
1459 hwaddr addr,
1460 uint64_t data,
1461 unsigned size,
1462 MemTxAttrs attrs)
1464 MemoryRegionIoeventfd ioeventfd = {
1465 .addr = addrrange_make(int128_make64(addr), int128_make64(size)),
1466 .data = data,
1468 unsigned i;
1470 for (i = 0; i < mr->ioeventfd_nb; i++) {
1471 ioeventfd.match_data = mr->ioeventfds[i].match_data;
1472 ioeventfd.e = mr->ioeventfds[i].e;
1474 if (memory_region_ioeventfd_equal(&ioeventfd, &mr->ioeventfds[i])) {
1475 event_notifier_set(ioeventfd.e);
1476 return true;
1480 return false;
1483 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1484 hwaddr addr,
1485 uint64_t data,
1486 unsigned size,
1487 MemTxAttrs attrs)
1489 if (!memory_region_access_valid(mr, addr, size, true, attrs)) {
1490 unassigned_mem_write(mr, addr, data, size);
1491 return MEMTX_DECODE_ERROR;
1494 adjust_endianness(mr, &data, size);
1496 if ((!kvm_eventfds_enabled()) &&
1497 memory_region_dispatch_write_eventfds(mr, addr, data, size, attrs)) {
1498 return MEMTX_OK;
1501 if (mr->ops->write) {
1502 return access_with_adjusted_size(addr, &data, size,
1503 mr->ops->impl.min_access_size,
1504 mr->ops->impl.max_access_size,
1505 memory_region_write_accessor, mr,
1506 attrs);
1507 } else {
1508 return
1509 access_with_adjusted_size(addr, &data, size,
1510 mr->ops->impl.min_access_size,
1511 mr->ops->impl.max_access_size,
1512 memory_region_write_with_attrs_accessor,
1513 mr, attrs);
1517 void memory_region_init_io(MemoryRegion *mr,
1518 Object *owner,
1519 const MemoryRegionOps *ops,
1520 void *opaque,
1521 const char *name,
1522 uint64_t size)
1524 memory_region_init(mr, owner, name, size);
1525 mr->ops = ops ? ops : &unassigned_mem_ops;
1526 mr->opaque = opaque;
1527 mr->terminates = true;
1530 void memory_region_init_ram_nomigrate(MemoryRegion *mr,
1531 Object *owner,
1532 const char *name,
1533 uint64_t size,
1534 Error **errp)
1536 memory_region_init_ram_shared_nomigrate(mr, owner, name, size, false, errp);
1539 void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
1540 Object *owner,
1541 const char *name,
1542 uint64_t size,
1543 bool share,
1544 Error **errp)
1546 Error *err = NULL;
1547 memory_region_init(mr, owner, name, size);
1548 mr->ram = true;
1549 mr->terminates = true;
1550 mr->destructor = memory_region_destructor_ram;
1551 mr->ram_block = qemu_ram_alloc(size, share, mr, &err);
1552 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1553 if (err) {
1554 mr->size = int128_zero();
1555 object_unparent(OBJECT(mr));
1556 error_propagate(errp, err);
1560 void memory_region_init_resizeable_ram(MemoryRegion *mr,
1561 Object *owner,
1562 const char *name,
1563 uint64_t size,
1564 uint64_t max_size,
1565 void (*resized)(const char*,
1566 uint64_t length,
1567 void *host),
1568 Error **errp)
1570 Error *err = NULL;
1571 memory_region_init(mr, owner, name, size);
1572 mr->ram = true;
1573 mr->terminates = true;
1574 mr->destructor = memory_region_destructor_ram;
1575 mr->ram_block = qemu_ram_alloc_resizeable(size, max_size, resized,
1576 mr, &err);
1577 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1578 if (err) {
1579 mr->size = int128_zero();
1580 object_unparent(OBJECT(mr));
1581 error_propagate(errp, err);
1585 #ifdef CONFIG_POSIX
1586 void memory_region_init_ram_from_file(MemoryRegion *mr,
1587 struct Object *owner,
1588 const char *name,
1589 uint64_t size,
1590 uint64_t align,
1591 uint32_t ram_flags,
1592 const char *path,
1593 Error **errp)
1595 Error *err = NULL;
1596 memory_region_init(mr, owner, name, size);
1597 mr->ram = true;
1598 mr->terminates = true;
1599 mr->destructor = memory_region_destructor_ram;
1600 mr->align = align;
1601 mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path, &err);
1602 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1603 if (err) {
1604 mr->size = int128_zero();
1605 object_unparent(OBJECT(mr));
1606 error_propagate(errp, err);
1610 void memory_region_init_ram_from_fd(MemoryRegion *mr,
1611 struct Object *owner,
1612 const char *name,
1613 uint64_t size,
1614 bool share,
1615 int fd,
1616 Error **errp)
1618 Error *err = NULL;
1619 memory_region_init(mr, owner, name, size);
1620 mr->ram = true;
1621 mr->terminates = true;
1622 mr->destructor = memory_region_destructor_ram;
1623 mr->ram_block = qemu_ram_alloc_from_fd(size, mr,
1624 share ? RAM_SHARED : 0,
1625 fd, &err);
1626 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1627 if (err) {
1628 mr->size = int128_zero();
1629 object_unparent(OBJECT(mr));
1630 error_propagate(errp, err);
1633 #endif
1635 void memory_region_init_ram_ptr(MemoryRegion *mr,
1636 Object *owner,
1637 const char *name,
1638 uint64_t size,
1639 void *ptr)
1641 memory_region_init(mr, owner, name, size);
1642 mr->ram = true;
1643 mr->terminates = true;
1644 mr->destructor = memory_region_destructor_ram;
1645 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1647 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1648 assert(ptr != NULL);
1649 mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
1652 void memory_region_init_ram_device_ptr(MemoryRegion *mr,
1653 Object *owner,
1654 const char *name,
1655 uint64_t size,
1656 void *ptr)
1658 memory_region_init(mr, owner, name, size);
1659 mr->ram = true;
1660 mr->terminates = true;
1661 mr->ram_device = true;
1662 mr->ops = &ram_device_mem_ops;
1663 mr->opaque = mr;
1664 mr->destructor = memory_region_destructor_ram;
1665 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1666 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1667 assert(ptr != NULL);
1668 mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
1671 void memory_region_init_alias(MemoryRegion *mr,
1672 Object *owner,
1673 const char *name,
1674 MemoryRegion *orig,
1675 hwaddr offset,
1676 uint64_t size)
1678 memory_region_init(mr, owner, name, size);
1679 mr->alias = orig;
1680 mr->alias_offset = offset;
1683 void memory_region_init_rom_nomigrate(MemoryRegion *mr,
1684 struct Object *owner,
1685 const char *name,
1686 uint64_t size,
1687 Error **errp)
1689 Error *err = NULL;
1690 memory_region_init(mr, owner, name, size);
1691 mr->ram = true;
1692 mr->readonly = true;
1693 mr->terminates = true;
1694 mr->destructor = memory_region_destructor_ram;
1695 mr->ram_block = qemu_ram_alloc(size, false, mr, &err);
1696 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1697 if (err) {
1698 mr->size = int128_zero();
1699 object_unparent(OBJECT(mr));
1700 error_propagate(errp, err);
1704 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
1705 Object *owner,
1706 const MemoryRegionOps *ops,
1707 void *opaque,
1708 const char *name,
1709 uint64_t size,
1710 Error **errp)
1712 Error *err = NULL;
1713 assert(ops);
1714 memory_region_init(mr, owner, name, size);
1715 mr->ops = ops;
1716 mr->opaque = opaque;
1717 mr->terminates = true;
1718 mr->rom_device = true;
1719 mr->destructor = memory_region_destructor_ram;
1720 mr->ram_block = qemu_ram_alloc(size, false, mr, &err);
1721 if (err) {
1722 mr->size = int128_zero();
1723 object_unparent(OBJECT(mr));
1724 error_propagate(errp, err);
1728 void memory_region_init_iommu(void *_iommu_mr,
1729 size_t instance_size,
1730 const char *mrtypename,
1731 Object *owner,
1732 const char *name,
1733 uint64_t size)
1735 struct IOMMUMemoryRegion *iommu_mr;
1736 struct MemoryRegion *mr;
1738 object_initialize(_iommu_mr, instance_size, mrtypename);
1739 mr = MEMORY_REGION(_iommu_mr);
1740 memory_region_do_init(mr, owner, name, size);
1741 iommu_mr = IOMMU_MEMORY_REGION(mr);
1742 mr->terminates = true; /* then re-forwards */
1743 QLIST_INIT(&iommu_mr->iommu_notify);
1744 iommu_mr->iommu_notify_flags = IOMMU_NOTIFIER_NONE;
1747 static void memory_region_finalize(Object *obj)
1749 MemoryRegion *mr = MEMORY_REGION(obj);
1751 assert(!mr->container);
1753 /* We know the region is not visible in any address space (it
1754 * does not have a container and cannot be a root either because
1755 * it has no references, so we can blindly clear mr->enabled.
1756 * memory_region_set_enabled instead could trigger a transaction
1757 * and cause an infinite loop.
1759 mr->enabled = false;
1760 memory_region_transaction_begin();
1761 while (!QTAILQ_EMPTY(&mr->subregions)) {
1762 MemoryRegion *subregion = QTAILQ_FIRST(&mr->subregions);
1763 memory_region_del_subregion(mr, subregion);
1765 memory_region_transaction_commit();
1767 mr->destructor(mr);
1768 memory_region_clear_coalescing(mr);
1769 g_free((char *)mr->name);
1770 g_free(mr->ioeventfds);
1773 Object *memory_region_owner(MemoryRegion *mr)
1775 Object *obj = OBJECT(mr);
1776 return obj->parent;
1779 void memory_region_ref(MemoryRegion *mr)
1781 /* MMIO callbacks most likely will access data that belongs
1782 * to the owner, hence the need to ref/unref the owner whenever
1783 * the memory region is in use.
1785 * The memory region is a child of its owner. As long as the
1786 * owner doesn't call unparent itself on the memory region,
1787 * ref-ing the owner will also keep the memory region alive.
1788 * Memory regions without an owner are supposed to never go away;
1789 * we do not ref/unref them because it slows down DMA sensibly.
1791 if (mr && mr->owner) {
1792 object_ref(mr->owner);
1796 void memory_region_unref(MemoryRegion *mr)
1798 if (mr && mr->owner) {
1799 object_unref(mr->owner);
1803 uint64_t memory_region_size(MemoryRegion *mr)
1805 if (int128_eq(mr->size, int128_2_64())) {
1806 return UINT64_MAX;
1808 return int128_get64(mr->size);
1811 const char *memory_region_name(const MemoryRegion *mr)
1813 if (!mr->name) {
1814 ((MemoryRegion *)mr)->name =
1815 object_get_canonical_path_component(OBJECT(mr));
1817 return mr->name;
1820 bool memory_region_is_ram_device(MemoryRegion *mr)
1822 return mr->ram_device;
1825 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
1827 uint8_t mask = mr->dirty_log_mask;
1828 if (global_dirty_log && mr->ram_block) {
1829 mask |= (1 << DIRTY_MEMORY_MIGRATION);
1831 return mask;
1834 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client)
1836 return memory_region_get_dirty_log_mask(mr) & (1 << client);
1839 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion *iommu_mr)
1841 IOMMUNotifierFlag flags = IOMMU_NOTIFIER_NONE;
1842 IOMMUNotifier *iommu_notifier;
1843 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1845 IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) {
1846 flags |= iommu_notifier->notifier_flags;
1849 if (flags != iommu_mr->iommu_notify_flags && imrc->notify_flag_changed) {
1850 imrc->notify_flag_changed(iommu_mr,
1851 iommu_mr->iommu_notify_flags,
1852 flags);
1855 iommu_mr->iommu_notify_flags = flags;
1858 void memory_region_register_iommu_notifier(MemoryRegion *mr,
1859 IOMMUNotifier *n)
1861 IOMMUMemoryRegion *iommu_mr;
1863 if (mr->alias) {
1864 memory_region_register_iommu_notifier(mr->alias, n);
1865 return;
1868 /* We need to register for at least one bitfield */
1869 iommu_mr = IOMMU_MEMORY_REGION(mr);
1870 assert(n->notifier_flags != IOMMU_NOTIFIER_NONE);
1871 assert(n->start <= n->end);
1872 assert(n->iommu_idx >= 0 &&
1873 n->iommu_idx < memory_region_iommu_num_indexes(iommu_mr));
1875 QLIST_INSERT_HEAD(&iommu_mr->iommu_notify, n, node);
1876 memory_region_update_iommu_notify_flags(iommu_mr);
1879 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr)
1881 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1883 if (imrc->get_min_page_size) {
1884 return imrc->get_min_page_size(iommu_mr);
1886 return TARGET_PAGE_SIZE;
1889 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
1891 MemoryRegion *mr = MEMORY_REGION(iommu_mr);
1892 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1893 hwaddr addr, granularity;
1894 IOMMUTLBEntry iotlb;
1896 /* If the IOMMU has its own replay callback, override */
1897 if (imrc->replay) {
1898 imrc->replay(iommu_mr, n);
1899 return;
1902 granularity = memory_region_iommu_get_min_page_size(iommu_mr);
1904 for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
1905 iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE, n->iommu_idx);
1906 if (iotlb.perm != IOMMU_NONE) {
1907 n->notify(n, &iotlb);
1910 /* if (2^64 - MR size) < granularity, it's possible to get an
1911 * infinite loop here. This should catch such a wraparound */
1912 if ((addr + granularity) < addr) {
1913 break;
1918 void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr)
1920 IOMMUNotifier *notifier;
1922 IOMMU_NOTIFIER_FOREACH(notifier, iommu_mr) {
1923 memory_region_iommu_replay(iommu_mr, notifier);
1927 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1928 IOMMUNotifier *n)
1930 IOMMUMemoryRegion *iommu_mr;
1932 if (mr->alias) {
1933 memory_region_unregister_iommu_notifier(mr->alias, n);
1934 return;
1936 QLIST_REMOVE(n, node);
1937 iommu_mr = IOMMU_MEMORY_REGION(mr);
1938 memory_region_update_iommu_notify_flags(iommu_mr);
1941 void memory_region_notify_one(IOMMUNotifier *notifier,
1942 IOMMUTLBEntry *entry)
1944 IOMMUNotifierFlag request_flags;
1945 hwaddr entry_end = entry->iova + entry->addr_mask;
1948 * Skip the notification if the notification does not overlap
1949 * with registered range.
1951 if (notifier->start > entry_end || notifier->end < entry->iova) {
1952 return;
1955 assert(entry->iova >= notifier->start && entry_end <= notifier->end);
1957 if (entry->perm & IOMMU_RW) {
1958 request_flags = IOMMU_NOTIFIER_MAP;
1959 } else {
1960 request_flags = IOMMU_NOTIFIER_UNMAP;
1963 if (notifier->notifier_flags & request_flags) {
1964 notifier->notify(notifier, entry);
1968 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1969 int iommu_idx,
1970 IOMMUTLBEntry entry)
1972 IOMMUNotifier *iommu_notifier;
1974 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr)));
1976 IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) {
1977 if (iommu_notifier->iommu_idx == iommu_idx) {
1978 memory_region_notify_one(iommu_notifier, &entry);
1983 int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1984 enum IOMMUMemoryRegionAttr attr,
1985 void *data)
1987 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1989 if (!imrc->get_attr) {
1990 return -EINVAL;
1993 return imrc->get_attr(iommu_mr, attr, data);
1996 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr,
1997 MemTxAttrs attrs)
1999 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
2001 if (!imrc->attrs_to_index) {
2002 return 0;
2005 return imrc->attrs_to_index(iommu_mr, attrs);
2008 int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr)
2010 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
2012 if (!imrc->num_indexes) {
2013 return 1;
2016 return imrc->num_indexes(iommu_mr);
2019 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
2021 uint8_t mask = 1 << client;
2022 uint8_t old_logging;
2024 assert(client == DIRTY_MEMORY_VGA);
2025 old_logging = mr->vga_logging_count;
2026 mr->vga_logging_count += log ? 1 : -1;
2027 if (!!old_logging == !!mr->vga_logging_count) {
2028 return;
2031 memory_region_transaction_begin();
2032 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
2033 memory_region_update_pending |= mr->enabled;
2034 memory_region_transaction_commit();
2037 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
2038 hwaddr size)
2040 assert(mr->ram_block);
2041 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr) + addr,
2042 size,
2043 memory_region_get_dirty_log_mask(mr));
2046 static void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
2048 MemoryListener *listener;
2049 AddressSpace *as;
2050 FlatView *view;
2051 FlatRange *fr;
2053 /* If the same address space has multiple log_sync listeners, we
2054 * visit that address space's FlatView multiple times. But because
2055 * log_sync listeners are rare, it's still cheaper than walking each
2056 * address space once.
2058 QTAILQ_FOREACH(listener, &memory_listeners, link) {
2059 if (!listener->log_sync) {
2060 continue;
2062 as = listener->address_space;
2063 view = address_space_get_flatview(as);
2064 FOR_EACH_FLAT_RANGE(fr, view) {
2065 if (fr->dirty_log_mask && (!mr || fr->mr == mr)) {
2066 MemoryRegionSection mrs = section_from_flat_range(fr, view);
2067 listener->log_sync(listener, &mrs);
2070 flatview_unref(view);
2074 void memory_region_clear_dirty_bitmap(MemoryRegion *mr, hwaddr start,
2075 hwaddr len)
2077 MemoryRegionSection mrs;
2078 MemoryListener *listener;
2079 AddressSpace *as;
2080 FlatView *view;
2081 FlatRange *fr;
2082 hwaddr sec_start, sec_end, sec_size;
2084 QTAILQ_FOREACH(listener, &memory_listeners, link) {
2085 if (!listener->log_clear) {
2086 continue;
2088 as = listener->address_space;
2089 view = address_space_get_flatview(as);
2090 FOR_EACH_FLAT_RANGE(fr, view) {
2091 if (!fr->dirty_log_mask || fr->mr != mr) {
2093 * Clear dirty bitmap operation only applies to those
2094 * regions whose dirty logging is at least enabled
2096 continue;
2099 mrs = section_from_flat_range(fr, view);
2101 sec_start = MAX(mrs.offset_within_region, start);
2102 sec_end = mrs.offset_within_region + int128_get64(mrs.size);
2103 sec_end = MIN(sec_end, start + len);
2105 if (sec_start >= sec_end) {
2107 * If this memory region section has no intersection
2108 * with the requested range, skip.
2110 continue;
2113 /* Valid case; shrink the section if needed */
2114 mrs.offset_within_address_space +=
2115 sec_start - mrs.offset_within_region;
2116 mrs.offset_within_region = sec_start;
2117 sec_size = sec_end - sec_start;
2118 mrs.size = int128_make64(sec_size);
2119 listener->log_clear(listener, &mrs);
2121 flatview_unref(view);
2125 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
2126 hwaddr addr,
2127 hwaddr size,
2128 unsigned client)
2130 DirtyBitmapSnapshot *snapshot;
2131 assert(mr->ram_block);
2132 memory_region_sync_dirty_bitmap(mr);
2133 snapshot = cpu_physical_memory_snapshot_and_clear_dirty(mr, addr, size, client);
2134 memory_global_after_dirty_log_sync();
2135 return snapshot;
2138 bool memory_region_snapshot_get_dirty(MemoryRegion *mr, DirtyBitmapSnapshot *snap,
2139 hwaddr addr, hwaddr size)
2141 assert(mr->ram_block);
2142 return cpu_physical_memory_snapshot_get_dirty(snap,
2143 memory_region_get_ram_addr(mr) + addr, size);
2146 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
2148 if (mr->readonly != readonly) {
2149 memory_region_transaction_begin();
2150 mr->readonly = readonly;
2151 memory_region_update_pending |= mr->enabled;
2152 memory_region_transaction_commit();
2156 void memory_region_set_nonvolatile(MemoryRegion *mr, bool nonvolatile)
2158 if (mr->nonvolatile != nonvolatile) {
2159 memory_region_transaction_begin();
2160 mr->nonvolatile = nonvolatile;
2161 memory_region_update_pending |= mr->enabled;
2162 memory_region_transaction_commit();
2166 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
2168 if (mr->romd_mode != romd_mode) {
2169 memory_region_transaction_begin();
2170 mr->romd_mode = romd_mode;
2171 memory_region_update_pending |= mr->enabled;
2172 memory_region_transaction_commit();
2176 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
2177 hwaddr size, unsigned client)
2179 assert(mr->ram_block);
2180 cpu_physical_memory_test_and_clear_dirty(
2181 memory_region_get_ram_addr(mr) + addr, size, client);
2184 int memory_region_get_fd(MemoryRegion *mr)
2186 int fd;
2188 rcu_read_lock();
2189 while (mr->alias) {
2190 mr = mr->alias;
2192 fd = mr->ram_block->fd;
2193 rcu_read_unlock();
2195 return fd;
2198 void *memory_region_get_ram_ptr(MemoryRegion *mr)
2200 void *ptr;
2201 uint64_t offset = 0;
2203 rcu_read_lock();
2204 while (mr->alias) {
2205 offset += mr->alias_offset;
2206 mr = mr->alias;
2208 assert(mr->ram_block);
2209 ptr = qemu_map_ram_ptr(mr->ram_block, offset);
2210 rcu_read_unlock();
2212 return ptr;
2215 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset)
2217 RAMBlock *block;
2219 block = qemu_ram_block_from_host(ptr, false, offset);
2220 if (!block) {
2221 return NULL;
2224 return block->mr;
2227 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
2229 return mr->ram_block ? mr->ram_block->offset : RAM_ADDR_INVALID;
2232 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp)
2234 assert(mr->ram_block);
2236 qemu_ram_resize(mr->ram_block, newsize, errp);
2239 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
2241 FlatView *view;
2242 FlatRange *fr;
2244 view = address_space_get_flatview(as);
2245 FOR_EACH_FLAT_RANGE(fr, view) {
2246 if (fr->mr == mr) {
2247 flat_range_coalesced_io_del(fr, as);
2248 flat_range_coalesced_io_add(fr, as);
2251 flatview_unref(view);
2254 static void memory_region_update_coalesced_range(MemoryRegion *mr)
2256 AddressSpace *as;
2258 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2259 memory_region_update_coalesced_range_as(mr, as);
2263 void memory_region_set_coalescing(MemoryRegion *mr)
2265 memory_region_clear_coalescing(mr);
2266 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
2269 void memory_region_add_coalescing(MemoryRegion *mr,
2270 hwaddr offset,
2271 uint64_t size)
2273 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
2275 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
2276 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
2277 memory_region_update_coalesced_range(mr);
2278 memory_region_set_flush_coalesced(mr);
2281 void memory_region_clear_coalescing(MemoryRegion *mr)
2283 CoalescedMemoryRange *cmr;
2284 bool updated = false;
2286 qemu_flush_coalesced_mmio_buffer();
2287 mr->flush_coalesced_mmio = false;
2289 while (!QTAILQ_EMPTY(&mr->coalesced)) {
2290 cmr = QTAILQ_FIRST(&mr->coalesced);
2291 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
2292 g_free(cmr);
2293 updated = true;
2296 if (updated) {
2297 memory_region_update_coalesced_range(mr);
2301 void memory_region_set_flush_coalesced(MemoryRegion *mr)
2303 mr->flush_coalesced_mmio = true;
2306 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
2308 qemu_flush_coalesced_mmio_buffer();
2309 if (QTAILQ_EMPTY(&mr->coalesced)) {
2310 mr->flush_coalesced_mmio = false;
2314 void memory_region_clear_global_locking(MemoryRegion *mr)
2316 mr->global_locking = false;
2319 static bool userspace_eventfd_warning;
2321 void memory_region_add_eventfd(MemoryRegion *mr,
2322 hwaddr addr,
2323 unsigned size,
2324 bool match_data,
2325 uint64_t data,
2326 EventNotifier *e)
2328 MemoryRegionIoeventfd mrfd = {
2329 .addr.start = int128_make64(addr),
2330 .addr.size = int128_make64(size),
2331 .match_data = match_data,
2332 .data = data,
2333 .e = e,
2335 unsigned i;
2337 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2338 userspace_eventfd_warning))) {
2339 userspace_eventfd_warning = true;
2340 error_report("Using eventfd without MMIO binding in KVM. "
2341 "Suboptimal performance expected");
2344 if (size) {
2345 adjust_endianness(mr, &mrfd.data, size);
2347 memory_region_transaction_begin();
2348 for (i = 0; i < mr->ioeventfd_nb; ++i) {
2349 if (memory_region_ioeventfd_before(&mrfd, &mr->ioeventfds[i])) {
2350 break;
2353 ++mr->ioeventfd_nb;
2354 mr->ioeventfds = g_realloc(mr->ioeventfds,
2355 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
2356 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
2357 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
2358 mr->ioeventfds[i] = mrfd;
2359 ioeventfd_update_pending |= mr->enabled;
2360 memory_region_transaction_commit();
2363 void memory_region_del_eventfd(MemoryRegion *mr,
2364 hwaddr addr,
2365 unsigned size,
2366 bool match_data,
2367 uint64_t data,
2368 EventNotifier *e)
2370 MemoryRegionIoeventfd mrfd = {
2371 .addr.start = int128_make64(addr),
2372 .addr.size = int128_make64(size),
2373 .match_data = match_data,
2374 .data = data,
2375 .e = e,
2377 unsigned i;
2379 if (size) {
2380 adjust_endianness(mr, &mrfd.data, size);
2382 memory_region_transaction_begin();
2383 for (i = 0; i < mr->ioeventfd_nb; ++i) {
2384 if (memory_region_ioeventfd_equal(&mrfd, &mr->ioeventfds[i])) {
2385 break;
2388 assert(i != mr->ioeventfd_nb);
2389 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
2390 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
2391 --mr->ioeventfd_nb;
2392 mr->ioeventfds = g_realloc(mr->ioeventfds,
2393 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
2394 ioeventfd_update_pending |= mr->enabled;
2395 memory_region_transaction_commit();
2398 static void memory_region_update_container_subregions(MemoryRegion *subregion)
2400 MemoryRegion *mr = subregion->container;
2401 MemoryRegion *other;
2403 memory_region_transaction_begin();
2405 memory_region_ref(subregion);
2406 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
2407 if (subregion->priority >= other->priority) {
2408 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
2409 goto done;
2412 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
2413 done:
2414 memory_region_update_pending |= mr->enabled && subregion->enabled;
2415 memory_region_transaction_commit();
2418 static void memory_region_add_subregion_common(MemoryRegion *mr,
2419 hwaddr offset,
2420 MemoryRegion *subregion)
2422 assert(!subregion->container);
2423 subregion->container = mr;
2424 subregion->addr = offset;
2425 memory_region_update_container_subregions(subregion);
2428 void memory_region_add_subregion(MemoryRegion *mr,
2429 hwaddr offset,
2430 MemoryRegion *subregion)
2432 subregion->priority = 0;
2433 memory_region_add_subregion_common(mr, offset, subregion);
2436 void memory_region_add_subregion_overlap(MemoryRegion *mr,
2437 hwaddr offset,
2438 MemoryRegion *subregion,
2439 int priority)
2441 subregion->priority = priority;
2442 memory_region_add_subregion_common(mr, offset, subregion);
2445 void memory_region_del_subregion(MemoryRegion *mr,
2446 MemoryRegion *subregion)
2448 memory_region_transaction_begin();
2449 assert(subregion->container == mr);
2450 subregion->container = NULL;
2451 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
2452 memory_region_unref(subregion);
2453 memory_region_update_pending |= mr->enabled && subregion->enabled;
2454 memory_region_transaction_commit();
2457 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
2459 if (enabled == mr->enabled) {
2460 return;
2462 memory_region_transaction_begin();
2463 mr->enabled = enabled;
2464 memory_region_update_pending = true;
2465 memory_region_transaction_commit();
2468 void memory_region_set_size(MemoryRegion *mr, uint64_t size)
2470 Int128 s = int128_make64(size);
2472 if (size == UINT64_MAX) {
2473 s = int128_2_64();
2475 if (int128_eq(s, mr->size)) {
2476 return;
2478 memory_region_transaction_begin();
2479 mr->size = s;
2480 memory_region_update_pending = true;
2481 memory_region_transaction_commit();
2484 static void memory_region_readd_subregion(MemoryRegion *mr)
2486 MemoryRegion *container = mr->container;
2488 if (container) {
2489 memory_region_transaction_begin();
2490 memory_region_ref(mr);
2491 memory_region_del_subregion(container, mr);
2492 mr->container = container;
2493 memory_region_update_container_subregions(mr);
2494 memory_region_unref(mr);
2495 memory_region_transaction_commit();
2499 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
2501 if (addr != mr->addr) {
2502 mr->addr = addr;
2503 memory_region_readd_subregion(mr);
2507 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
2509 assert(mr->alias);
2511 if (offset == mr->alias_offset) {
2512 return;
2515 memory_region_transaction_begin();
2516 mr->alias_offset = offset;
2517 memory_region_update_pending |= mr->enabled;
2518 memory_region_transaction_commit();
2521 uint64_t memory_region_get_alignment(const MemoryRegion *mr)
2523 return mr->align;
2526 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
2528 const AddrRange *addr = addr_;
2529 const FlatRange *fr = fr_;
2531 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
2532 return -1;
2533 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
2534 return 1;
2536 return 0;
2539 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
2541 return bsearch(&addr, view->ranges, view->nr,
2542 sizeof(FlatRange), cmp_flatrange_addr);
2545 bool memory_region_is_mapped(MemoryRegion *mr)
2547 return mr->container ? true : false;
2550 /* Same as memory_region_find, but it does not add a reference to the
2551 * returned region. It must be called from an RCU critical section.
2553 static MemoryRegionSection memory_region_find_rcu(MemoryRegion *mr,
2554 hwaddr addr, uint64_t size)
2556 MemoryRegionSection ret = { .mr = NULL };
2557 MemoryRegion *root;
2558 AddressSpace *as;
2559 AddrRange range;
2560 FlatView *view;
2561 FlatRange *fr;
2563 addr += mr->addr;
2564 for (root = mr; root->container; ) {
2565 root = root->container;
2566 addr += root->addr;
2569 as = memory_region_to_address_space(root);
2570 if (!as) {
2571 return ret;
2573 range = addrrange_make(int128_make64(addr), int128_make64(size));
2575 view = address_space_to_flatview(as);
2576 fr = flatview_lookup(view, range);
2577 if (!fr) {
2578 return ret;
2581 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
2582 --fr;
2585 ret.mr = fr->mr;
2586 ret.fv = view;
2587 range = addrrange_intersection(range, fr->addr);
2588 ret.offset_within_region = fr->offset_in_region;
2589 ret.offset_within_region += int128_get64(int128_sub(range.start,
2590 fr->addr.start));
2591 ret.size = range.size;
2592 ret.offset_within_address_space = int128_get64(range.start);
2593 ret.readonly = fr->readonly;
2594 ret.nonvolatile = fr->nonvolatile;
2595 return ret;
2598 MemoryRegionSection memory_region_find(MemoryRegion *mr,
2599 hwaddr addr, uint64_t size)
2601 MemoryRegionSection ret;
2602 rcu_read_lock();
2603 ret = memory_region_find_rcu(mr, addr, size);
2604 if (ret.mr) {
2605 memory_region_ref(ret.mr);
2607 rcu_read_unlock();
2608 return ret;
2611 bool memory_region_present(MemoryRegion *container, hwaddr addr)
2613 MemoryRegion *mr;
2615 rcu_read_lock();
2616 mr = memory_region_find_rcu(container, addr, 1).mr;
2617 rcu_read_unlock();
2618 return mr && mr != container;
2621 void memory_global_dirty_log_sync(void)
2623 memory_region_sync_dirty_bitmap(NULL);
2626 void memory_global_after_dirty_log_sync(void)
2628 MEMORY_LISTENER_CALL_GLOBAL(log_global_after_sync, Forward);
2631 static VMChangeStateEntry *vmstate_change;
2633 void memory_global_dirty_log_start(void)
2635 if (vmstate_change) {
2636 qemu_del_vm_change_state_handler(vmstate_change);
2637 vmstate_change = NULL;
2640 global_dirty_log = true;
2642 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
2644 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2645 memory_region_transaction_begin();
2646 memory_region_update_pending = true;
2647 memory_region_transaction_commit();
2650 static void memory_global_dirty_log_do_stop(void)
2652 global_dirty_log = false;
2654 /* Refresh DIRTY_MEMORY_MIGRATION bit. */
2655 memory_region_transaction_begin();
2656 memory_region_update_pending = true;
2657 memory_region_transaction_commit();
2659 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
2662 static void memory_vm_change_state_handler(void *opaque, int running,
2663 RunState state)
2665 if (running) {
2666 memory_global_dirty_log_do_stop();
2668 if (vmstate_change) {
2669 qemu_del_vm_change_state_handler(vmstate_change);
2670 vmstate_change = NULL;
2675 void memory_global_dirty_log_stop(void)
2677 if (!runstate_is_running()) {
2678 if (vmstate_change) {
2679 return;
2681 vmstate_change = qemu_add_vm_change_state_handler(
2682 memory_vm_change_state_handler, NULL);
2683 return;
2686 memory_global_dirty_log_do_stop();
2689 static void listener_add_address_space(MemoryListener *listener,
2690 AddressSpace *as)
2692 FlatView *view;
2693 FlatRange *fr;
2695 if (listener->begin) {
2696 listener->begin(listener);
2698 if (global_dirty_log) {
2699 if (listener->log_global_start) {
2700 listener->log_global_start(listener);
2704 view = address_space_get_flatview(as);
2705 FOR_EACH_FLAT_RANGE(fr, view) {
2706 MemoryRegionSection section = section_from_flat_range(fr, view);
2708 if (listener->region_add) {
2709 listener->region_add(listener, &section);
2711 if (fr->dirty_log_mask && listener->log_start) {
2712 listener->log_start(listener, &section, 0, fr->dirty_log_mask);
2715 if (listener->commit) {
2716 listener->commit(listener);
2718 flatview_unref(view);
2721 static void listener_del_address_space(MemoryListener *listener,
2722 AddressSpace *as)
2724 FlatView *view;
2725 FlatRange *fr;
2727 if (listener->begin) {
2728 listener->begin(listener);
2730 view = address_space_get_flatview(as);
2731 FOR_EACH_FLAT_RANGE(fr, view) {
2732 MemoryRegionSection section = section_from_flat_range(fr, view);
2734 if (fr->dirty_log_mask && listener->log_stop) {
2735 listener->log_stop(listener, &section, fr->dirty_log_mask, 0);
2737 if (listener->region_del) {
2738 listener->region_del(listener, &section);
2741 if (listener->commit) {
2742 listener->commit(listener);
2744 flatview_unref(view);
2747 void memory_listener_register(MemoryListener *listener, AddressSpace *as)
2749 MemoryListener *other = NULL;
2751 listener->address_space = as;
2752 if (QTAILQ_EMPTY(&memory_listeners)
2753 || listener->priority >= QTAILQ_LAST(&memory_listeners)->priority) {
2754 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
2755 } else {
2756 QTAILQ_FOREACH(other, &memory_listeners, link) {
2757 if (listener->priority < other->priority) {
2758 break;
2761 QTAILQ_INSERT_BEFORE(other, listener, link);
2764 if (QTAILQ_EMPTY(&as->listeners)
2765 || listener->priority >= QTAILQ_LAST(&as->listeners)->priority) {
2766 QTAILQ_INSERT_TAIL(&as->listeners, listener, link_as);
2767 } else {
2768 QTAILQ_FOREACH(other, &as->listeners, link_as) {
2769 if (listener->priority < other->priority) {
2770 break;
2773 QTAILQ_INSERT_BEFORE(other, listener, link_as);
2776 listener_add_address_space(listener, as);
2779 void memory_listener_unregister(MemoryListener *listener)
2781 if (!listener->address_space) {
2782 return;
2785 listener_del_address_space(listener, listener->address_space);
2786 QTAILQ_REMOVE(&memory_listeners, listener, link);
2787 QTAILQ_REMOVE(&listener->address_space->listeners, listener, link_as);
2788 listener->address_space = NULL;
2791 void address_space_remove_listeners(AddressSpace *as)
2793 while (!QTAILQ_EMPTY(&as->listeners)) {
2794 memory_listener_unregister(QTAILQ_FIRST(&as->listeners));
2798 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
2800 memory_region_ref(root);
2801 as->root = root;
2802 as->current_map = NULL;
2803 as->ioeventfd_nb = 0;
2804 as->ioeventfds = NULL;
2805 QTAILQ_INIT(&as->listeners);
2806 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
2807 as->name = g_strdup(name ? name : "anonymous");
2808 address_space_update_topology(as);
2809 address_space_update_ioeventfds(as);
2812 static void do_address_space_destroy(AddressSpace *as)
2814 assert(QTAILQ_EMPTY(&as->listeners));
2816 flatview_unref(as->current_map);
2817 g_free(as->name);
2818 g_free(as->ioeventfds);
2819 memory_region_unref(as->root);
2822 void address_space_destroy(AddressSpace *as)
2824 MemoryRegion *root = as->root;
2826 /* Flush out anything from MemoryListeners listening in on this */
2827 memory_region_transaction_begin();
2828 as->root = NULL;
2829 memory_region_transaction_commit();
2830 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
2832 /* At this point, as->dispatch and as->current_map are dummy
2833 * entries that the guest should never use. Wait for the old
2834 * values to expire before freeing the data.
2836 as->root = root;
2837 call_rcu(as, do_address_space_destroy, rcu);
2840 static const char *memory_region_type(MemoryRegion *mr)
2842 if (memory_region_is_ram_device(mr)) {
2843 return "ramd";
2844 } else if (memory_region_is_romd(mr)) {
2845 return "romd";
2846 } else if (memory_region_is_rom(mr)) {
2847 return "rom";
2848 } else if (memory_region_is_ram(mr)) {
2849 return "ram";
2850 } else {
2851 return "i/o";
2855 typedef struct MemoryRegionList MemoryRegionList;
2857 struct MemoryRegionList {
2858 const MemoryRegion *mr;
2859 QTAILQ_ENTRY(MemoryRegionList) mrqueue;
2862 typedef QTAILQ_HEAD(, MemoryRegionList) MemoryRegionListHead;
2864 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2865 int128_sub((size), int128_one())) : 0)
2866 #define MTREE_INDENT " "
2868 static void mtree_expand_owner(const char *label, Object *obj)
2870 DeviceState *dev = (DeviceState *) object_dynamic_cast(obj, TYPE_DEVICE);
2872 qemu_printf(" %s:{%s", label, dev ? "dev" : "obj");
2873 if (dev && dev->id) {
2874 qemu_printf(" id=%s", dev->id);
2875 } else {
2876 gchar *canonical_path = object_get_canonical_path(obj);
2877 if (canonical_path) {
2878 qemu_printf(" path=%s", canonical_path);
2879 g_free(canonical_path);
2880 } else {
2881 qemu_printf(" type=%s", object_get_typename(obj));
2884 qemu_printf("}");
2887 static void mtree_print_mr_owner(const MemoryRegion *mr)
2889 Object *owner = mr->owner;
2890 Object *parent = memory_region_owner((MemoryRegion *)mr);
2892 if (!owner && !parent) {
2893 qemu_printf(" orphan");
2894 return;
2896 if (owner) {
2897 mtree_expand_owner("owner", owner);
2899 if (parent && parent != owner) {
2900 mtree_expand_owner("parent", parent);
2904 static void mtree_print_mr(const MemoryRegion *mr, unsigned int level,
2905 hwaddr base,
2906 MemoryRegionListHead *alias_print_queue,
2907 bool owner)
2909 MemoryRegionList *new_ml, *ml, *next_ml;
2910 MemoryRegionListHead submr_print_queue;
2911 const MemoryRegion *submr;
2912 unsigned int i;
2913 hwaddr cur_start, cur_end;
2915 if (!mr) {
2916 return;
2919 for (i = 0; i < level; i++) {
2920 qemu_printf(MTREE_INDENT);
2923 cur_start = base + mr->addr;
2924 cur_end = cur_start + MR_SIZE(mr->size);
2927 * Try to detect overflow of memory region. This should never
2928 * happen normally. When it happens, we dump something to warn the
2929 * user who is observing this.
2931 if (cur_start < base || cur_end < cur_start) {
2932 qemu_printf("[DETECTED OVERFLOW!] ");
2935 if (mr->alias) {
2936 MemoryRegionList *ml;
2937 bool found = false;
2939 /* check if the alias is already in the queue */
2940 QTAILQ_FOREACH(ml, alias_print_queue, mrqueue) {
2941 if (ml->mr == mr->alias) {
2942 found = true;
2946 if (!found) {
2947 ml = g_new(MemoryRegionList, 1);
2948 ml->mr = mr->alias;
2949 QTAILQ_INSERT_TAIL(alias_print_queue, ml, mrqueue);
2951 qemu_printf(TARGET_FMT_plx "-" TARGET_FMT_plx
2952 " (prio %d, %s%s): alias %s @%s " TARGET_FMT_plx
2953 "-" TARGET_FMT_plx "%s",
2954 cur_start, cur_end,
2955 mr->priority,
2956 mr->nonvolatile ? "nv-" : "",
2957 memory_region_type((MemoryRegion *)mr),
2958 memory_region_name(mr),
2959 memory_region_name(mr->alias),
2960 mr->alias_offset,
2961 mr->alias_offset + MR_SIZE(mr->size),
2962 mr->enabled ? "" : " [disabled]");
2963 if (owner) {
2964 mtree_print_mr_owner(mr);
2966 } else {
2967 qemu_printf(TARGET_FMT_plx "-" TARGET_FMT_plx
2968 " (prio %d, %s%s): %s%s",
2969 cur_start, cur_end,
2970 mr->priority,
2971 mr->nonvolatile ? "nv-" : "",
2972 memory_region_type((MemoryRegion *)mr),
2973 memory_region_name(mr),
2974 mr->enabled ? "" : " [disabled]");
2975 if (owner) {
2976 mtree_print_mr_owner(mr);
2979 qemu_printf("\n");
2981 QTAILQ_INIT(&submr_print_queue);
2983 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
2984 new_ml = g_new(MemoryRegionList, 1);
2985 new_ml->mr = submr;
2986 QTAILQ_FOREACH(ml, &submr_print_queue, mrqueue) {
2987 if (new_ml->mr->addr < ml->mr->addr ||
2988 (new_ml->mr->addr == ml->mr->addr &&
2989 new_ml->mr->priority > ml->mr->priority)) {
2990 QTAILQ_INSERT_BEFORE(ml, new_ml, mrqueue);
2991 new_ml = NULL;
2992 break;
2995 if (new_ml) {
2996 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, mrqueue);
3000 QTAILQ_FOREACH(ml, &submr_print_queue, mrqueue) {
3001 mtree_print_mr(ml->mr, level + 1, cur_start,
3002 alias_print_queue, owner);
3005 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, mrqueue, next_ml) {
3006 g_free(ml);
3010 struct FlatViewInfo {
3011 int counter;
3012 bool dispatch_tree;
3013 bool owner;
3014 AccelClass *ac;
3015 const char *ac_name;
3018 static void mtree_print_flatview(gpointer key, gpointer value,
3019 gpointer user_data)
3021 FlatView *view = key;
3022 GArray *fv_address_spaces = value;
3023 struct FlatViewInfo *fvi = user_data;
3024 FlatRange *range = &view->ranges[0];
3025 MemoryRegion *mr;
3026 int n = view->nr;
3027 int i;
3028 AddressSpace *as;
3030 qemu_printf("FlatView #%d\n", fvi->counter);
3031 ++fvi->counter;
3033 for (i = 0; i < fv_address_spaces->len; ++i) {
3034 as = g_array_index(fv_address_spaces, AddressSpace*, i);
3035 qemu_printf(" AS \"%s\", root: %s",
3036 as->name, memory_region_name(as->root));
3037 if (as->root->alias) {
3038 qemu_printf(", alias %s", memory_region_name(as->root->alias));
3040 qemu_printf("\n");
3043 qemu_printf(" Root memory region: %s\n",
3044 view->root ? memory_region_name(view->root) : "(none)");
3046 if (n <= 0) {
3047 qemu_printf(MTREE_INDENT "No rendered FlatView\n\n");
3048 return;
3051 while (n--) {
3052 mr = range->mr;
3053 if (range->offset_in_region) {
3054 qemu_printf(MTREE_INDENT TARGET_FMT_plx "-" TARGET_FMT_plx
3055 " (prio %d, %s%s): %s @" TARGET_FMT_plx,
3056 int128_get64(range->addr.start),
3057 int128_get64(range->addr.start)
3058 + MR_SIZE(range->addr.size),
3059 mr->priority,
3060 range->nonvolatile ? "nv-" : "",
3061 range->readonly ? "rom" : memory_region_type(mr),
3062 memory_region_name(mr),
3063 range->offset_in_region);
3064 } else {
3065 qemu_printf(MTREE_INDENT TARGET_FMT_plx "-" TARGET_FMT_plx
3066 " (prio %d, %s%s): %s",
3067 int128_get64(range->addr.start),
3068 int128_get64(range->addr.start)
3069 + MR_SIZE(range->addr.size),
3070 mr->priority,
3071 range->nonvolatile ? "nv-" : "",
3072 range->readonly ? "rom" : memory_region_type(mr),
3073 memory_region_name(mr));
3075 if (fvi->owner) {
3076 mtree_print_mr_owner(mr);
3079 if (fvi->ac) {
3080 for (i = 0; i < fv_address_spaces->len; ++i) {
3081 as = g_array_index(fv_address_spaces, AddressSpace*, i);
3082 if (fvi->ac->has_memory(current_machine, as,
3083 int128_get64(range->addr.start),
3084 MR_SIZE(range->addr.size) + 1)) {
3085 qemu_printf(" %s", fvi->ac_name);
3089 qemu_printf("\n");
3090 range++;
3093 #if !defined(CONFIG_USER_ONLY)
3094 if (fvi->dispatch_tree && view->root) {
3095 mtree_print_dispatch(view->dispatch, view->root);
3097 #endif
3099 qemu_printf("\n");
3102 static gboolean mtree_info_flatview_free(gpointer key, gpointer value,
3103 gpointer user_data)
3105 FlatView *view = key;
3106 GArray *fv_address_spaces = value;
3108 g_array_unref(fv_address_spaces);
3109 flatview_unref(view);
3111 return true;
3114 void mtree_info(bool flatview, bool dispatch_tree, bool owner)
3116 MemoryRegionListHead ml_head;
3117 MemoryRegionList *ml, *ml2;
3118 AddressSpace *as;
3120 if (flatview) {
3121 FlatView *view;
3122 struct FlatViewInfo fvi = {
3123 .counter = 0,
3124 .dispatch_tree = dispatch_tree,
3125 .owner = owner,
3127 GArray *fv_address_spaces;
3128 GHashTable *views = g_hash_table_new(g_direct_hash, g_direct_equal);
3129 AccelClass *ac = ACCEL_GET_CLASS(current_machine->accelerator);
3131 if (ac->has_memory) {
3132 fvi.ac = ac;
3133 fvi.ac_name = current_machine->accel ? current_machine->accel :
3134 object_class_get_name(OBJECT_CLASS(ac));
3137 /* Gather all FVs in one table */
3138 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
3139 view = address_space_get_flatview(as);
3141 fv_address_spaces = g_hash_table_lookup(views, view);
3142 if (!fv_address_spaces) {
3143 fv_address_spaces = g_array_new(false, false, sizeof(as));
3144 g_hash_table_insert(views, view, fv_address_spaces);
3147 g_array_append_val(fv_address_spaces, as);
3150 /* Print */
3151 g_hash_table_foreach(views, mtree_print_flatview, &fvi);
3153 /* Free */
3154 g_hash_table_foreach_remove(views, mtree_info_flatview_free, 0);
3155 g_hash_table_unref(views);
3157 return;
3160 QTAILQ_INIT(&ml_head);
3162 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
3163 qemu_printf("address-space: %s\n", as->name);
3164 mtree_print_mr(as->root, 1, 0, &ml_head, owner);
3165 qemu_printf("\n");
3168 /* print aliased regions */
3169 QTAILQ_FOREACH(ml, &ml_head, mrqueue) {
3170 qemu_printf("memory-region: %s\n", memory_region_name(ml->mr));
3171 mtree_print_mr(ml->mr, 1, 0, &ml_head, owner);
3172 qemu_printf("\n");
3175 QTAILQ_FOREACH_SAFE(ml, &ml_head, mrqueue, ml2) {
3176 g_free(ml);
3180 void memory_region_init_ram(MemoryRegion *mr,
3181 struct Object *owner,
3182 const char *name,
3183 uint64_t size,
3184 Error **errp)
3186 DeviceState *owner_dev;
3187 Error *err = NULL;
3189 memory_region_init_ram_nomigrate(mr, owner, name, size, &err);
3190 if (err) {
3191 error_propagate(errp, err);
3192 return;
3194 /* This will assert if owner is neither NULL nor a DeviceState.
3195 * We only want the owner here for the purposes of defining a
3196 * unique name for migration. TODO: Ideally we should implement
3197 * a naming scheme for Objects which are not DeviceStates, in
3198 * which case we can relax this restriction.
3200 owner_dev = DEVICE(owner);
3201 vmstate_register_ram(mr, owner_dev);
3204 void memory_region_init_rom(MemoryRegion *mr,
3205 struct Object *owner,
3206 const char *name,
3207 uint64_t size,
3208 Error **errp)
3210 DeviceState *owner_dev;
3211 Error *err = NULL;
3213 memory_region_init_rom_nomigrate(mr, owner, name, size, &err);
3214 if (err) {
3215 error_propagate(errp, err);
3216 return;
3218 /* This will assert if owner is neither NULL nor a DeviceState.
3219 * We only want the owner here for the purposes of defining a
3220 * unique name for migration. TODO: Ideally we should implement
3221 * a naming scheme for Objects which are not DeviceStates, in
3222 * which case we can relax this restriction.
3224 owner_dev = DEVICE(owner);
3225 vmstate_register_ram(mr, owner_dev);
3228 void memory_region_init_rom_device(MemoryRegion *mr,
3229 struct Object *owner,
3230 const MemoryRegionOps *ops,
3231 void *opaque,
3232 const char *name,
3233 uint64_t size,
3234 Error **errp)
3236 DeviceState *owner_dev;
3237 Error *err = NULL;
3239 memory_region_init_rom_device_nomigrate(mr, owner, ops, opaque,
3240 name, size, &err);
3241 if (err) {
3242 error_propagate(errp, err);
3243 return;
3245 /* This will assert if owner is neither NULL nor a DeviceState.
3246 * We only want the owner here for the purposes of defining a
3247 * unique name for migration. TODO: Ideally we should implement
3248 * a naming scheme for Objects which are not DeviceStates, in
3249 * which case we can relax this restriction.
3251 owner_dev = DEVICE(owner);
3252 vmstate_register_ram(mr, owner_dev);
3255 static const TypeInfo memory_region_info = {
3256 .parent = TYPE_OBJECT,
3257 .name = TYPE_MEMORY_REGION,
3258 .class_size = sizeof(MemoryRegionClass),
3259 .instance_size = sizeof(MemoryRegion),
3260 .instance_init = memory_region_initfn,
3261 .instance_finalize = memory_region_finalize,
3264 static const TypeInfo iommu_memory_region_info = {
3265 .parent = TYPE_MEMORY_REGION,
3266 .name = TYPE_IOMMU_MEMORY_REGION,
3267 .class_size = sizeof(IOMMUMemoryRegionClass),
3268 .instance_size = sizeof(IOMMUMemoryRegion),
3269 .instance_init = iommu_memory_region_initfn,
3270 .abstract = true,
3273 static void memory_register_types(void)
3275 type_register_static(&memory_region_info);
3276 type_register_static(&iommu_memory_region_info);
3279 type_init(memory_register_types)