qemu-iotests: Fix cleanup for 192
[qemu/ar7.git] / memory.c
blobbb2b71ee3833d9e39f58c7418b9828e60e357dd1
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 "qemu-common.h"
19 #include "cpu.h"
20 #include "exec/memory.h"
21 #include "exec/address-spaces.h"
22 #include "qapi/visitor.h"
23 #include "qemu/bitops.h"
24 #include "qemu/error-report.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/sysemu.h"
33 #include "hw/qdev-properties.h"
34 #include "migration/vmstate.h"
36 //#define DEBUG_UNASSIGNED
38 static unsigned memory_region_transaction_depth;
39 static bool memory_region_update_pending;
40 static bool ioeventfd_update_pending;
41 static bool global_dirty_log = false;
43 static QTAILQ_HEAD(, MemoryListener) memory_listeners
44 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
46 static QTAILQ_HEAD(, AddressSpace) address_spaces
47 = QTAILQ_HEAD_INITIALIZER(address_spaces);
49 static GHashTable *flat_views;
51 typedef struct AddrRange AddrRange;
54 * Note that signed integers are needed for negative offsetting in aliases
55 * (large MemoryRegion::alias_offset).
57 struct AddrRange {
58 Int128 start;
59 Int128 size;
62 static AddrRange addrrange_make(Int128 start, Int128 size)
64 return (AddrRange) { start, size };
67 static bool addrrange_equal(AddrRange r1, AddrRange r2)
69 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
72 static Int128 addrrange_end(AddrRange r)
74 return int128_add(r.start, r.size);
77 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
79 int128_addto(&range.start, delta);
80 return range;
83 static bool addrrange_contains(AddrRange range, Int128 addr)
85 return int128_ge(addr, range.start)
86 && int128_lt(addr, addrrange_end(range));
89 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
91 return addrrange_contains(r1, r2.start)
92 || addrrange_contains(r2, r1.start);
95 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
97 Int128 start = int128_max(r1.start, r2.start);
98 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
99 return addrrange_make(start, int128_sub(end, start));
102 enum ListenerDirection { Forward, Reverse };
104 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
105 do { \
106 MemoryListener *_listener; \
108 switch (_direction) { \
109 case Forward: \
110 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
111 if (_listener->_callback) { \
112 _listener->_callback(_listener, ##_args); \
115 break; \
116 case Reverse: \
117 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, link) { \
118 if (_listener->_callback) { \
119 _listener->_callback(_listener, ##_args); \
122 break; \
123 default: \
124 abort(); \
126 } while (0)
128 #define MEMORY_LISTENER_CALL(_as, _callback, _direction, _section, _args...) \
129 do { \
130 MemoryListener *_listener; \
132 switch (_direction) { \
133 case Forward: \
134 QTAILQ_FOREACH(_listener, &(_as)->listeners, link_as) { \
135 if (_listener->_callback) { \
136 _listener->_callback(_listener, _section, ##_args); \
139 break; \
140 case Reverse: \
141 QTAILQ_FOREACH_REVERSE(_listener, &(_as)->listeners, link_as) { \
142 if (_listener->_callback) { \
143 _listener->_callback(_listener, _section, ##_args); \
146 break; \
147 default: \
148 abort(); \
150 } while (0)
152 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
153 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
154 do { \
155 MemoryRegionSection mrs = section_from_flat_range(fr, \
156 address_space_to_flatview(as)); \
157 MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \
158 } while(0)
160 struct CoalescedMemoryRange {
161 AddrRange addr;
162 QTAILQ_ENTRY(CoalescedMemoryRange) link;
165 struct MemoryRegionIoeventfd {
166 AddrRange addr;
167 bool match_data;
168 uint64_t data;
169 EventNotifier *e;
172 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd *a,
173 MemoryRegionIoeventfd *b)
175 if (int128_lt(a->addr.start, b->addr.start)) {
176 return true;
177 } else if (int128_gt(a->addr.start, b->addr.start)) {
178 return false;
179 } else if (int128_lt(a->addr.size, b->addr.size)) {
180 return true;
181 } else if (int128_gt(a->addr.size, b->addr.size)) {
182 return false;
183 } else if (a->match_data < b->match_data) {
184 return true;
185 } else if (a->match_data > b->match_data) {
186 return false;
187 } else if (a->match_data) {
188 if (a->data < b->data) {
189 return true;
190 } else if (a->data > b->data) {
191 return false;
194 if (a->e < b->e) {
195 return true;
196 } else if (a->e > b->e) {
197 return false;
199 return false;
202 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd *a,
203 MemoryRegionIoeventfd *b)
205 return !memory_region_ioeventfd_before(a, b)
206 && !memory_region_ioeventfd_before(b, a);
209 /* Range of memory in the global map. Addresses are absolute. */
210 struct FlatRange {
211 MemoryRegion *mr;
212 hwaddr offset_in_region;
213 AddrRange addr;
214 uint8_t dirty_log_mask;
215 bool romd_mode;
216 bool readonly;
217 bool nonvolatile;
218 int has_coalesced_range;
221 #define FOR_EACH_FLAT_RANGE(var, view) \
222 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
224 static inline MemoryRegionSection
225 section_from_flat_range(FlatRange *fr, FlatView *fv)
227 return (MemoryRegionSection) {
228 .mr = fr->mr,
229 .fv = fv,
230 .offset_within_region = fr->offset_in_region,
231 .size = fr->addr.size,
232 .offset_within_address_space = int128_get64(fr->addr.start),
233 .readonly = fr->readonly,
234 .nonvolatile = fr->nonvolatile,
238 static bool flatrange_equal(FlatRange *a, FlatRange *b)
240 return a->mr == b->mr
241 && addrrange_equal(a->addr, b->addr)
242 && a->offset_in_region == b->offset_in_region
243 && a->romd_mode == b->romd_mode
244 && a->readonly == b->readonly
245 && a->nonvolatile == b->nonvolatile;
248 static FlatView *flatview_new(MemoryRegion *mr_root)
250 FlatView *view;
252 view = g_new0(FlatView, 1);
253 view->ref = 1;
254 view->root = mr_root;
255 memory_region_ref(mr_root);
256 trace_flatview_new(view, mr_root);
258 return view;
261 /* Insert a range into a given position. Caller is responsible for maintaining
262 * sorting order.
264 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
266 if (view->nr == view->nr_allocated) {
267 view->nr_allocated = MAX(2 * view->nr, 10);
268 view->ranges = g_realloc(view->ranges,
269 view->nr_allocated * sizeof(*view->ranges));
271 memmove(view->ranges + pos + 1, view->ranges + pos,
272 (view->nr - pos) * sizeof(FlatRange));
273 view->ranges[pos] = *range;
274 memory_region_ref(range->mr);
275 ++view->nr;
278 static void flatview_destroy(FlatView *view)
280 int i;
282 trace_flatview_destroy(view, view->root);
283 if (view->dispatch) {
284 address_space_dispatch_free(view->dispatch);
286 for (i = 0; i < view->nr; i++) {
287 memory_region_unref(view->ranges[i].mr);
289 g_free(view->ranges);
290 memory_region_unref(view->root);
291 g_free(view);
294 static bool flatview_ref(FlatView *view)
296 return atomic_fetch_inc_nonzero(&view->ref) > 0;
299 void flatview_unref(FlatView *view)
301 if (atomic_fetch_dec(&view->ref) == 1) {
302 trace_flatview_destroy_rcu(view, view->root);
303 assert(view->root);
304 call_rcu(view, flatview_destroy, rcu);
308 static bool can_merge(FlatRange *r1, FlatRange *r2)
310 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
311 && r1->mr == r2->mr
312 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
313 r1->addr.size),
314 int128_make64(r2->offset_in_region))
315 && r1->dirty_log_mask == r2->dirty_log_mask
316 && r1->romd_mode == r2->romd_mode
317 && r1->readonly == r2->readonly
318 && r1->nonvolatile == r2->nonvolatile;
321 /* Attempt to simplify a view by merging adjacent ranges */
322 static void flatview_simplify(FlatView *view)
324 unsigned i, j;
326 i = 0;
327 while (i < view->nr) {
328 j = i + 1;
329 while (j < view->nr
330 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
331 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
332 ++j;
334 ++i;
335 memmove(&view->ranges[i], &view->ranges[j],
336 (view->nr - j) * sizeof(view->ranges[j]));
337 view->nr -= j - i;
341 static bool memory_region_big_endian(MemoryRegion *mr)
343 #ifdef TARGET_WORDS_BIGENDIAN
344 return mr->ops->endianness != DEVICE_LITTLE_ENDIAN;
345 #else
346 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
347 #endif
350 static bool memory_region_wrong_endianness(MemoryRegion *mr)
352 #ifdef TARGET_WORDS_BIGENDIAN
353 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
354 #else
355 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
356 #endif
359 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
361 if (memory_region_wrong_endianness(mr)) {
362 switch (size) {
363 case 1:
364 break;
365 case 2:
366 *data = bswap16(*data);
367 break;
368 case 4:
369 *data = bswap32(*data);
370 break;
371 case 8:
372 *data = bswap64(*data);
373 break;
374 default:
375 abort();
380 static inline void memory_region_shift_read_access(uint64_t *value,
381 signed shift,
382 uint64_t mask,
383 uint64_t tmp)
385 if (shift >= 0) {
386 *value |= (tmp & mask) << shift;
387 } else {
388 *value |= (tmp & mask) >> -shift;
392 static inline uint64_t memory_region_shift_write_access(uint64_t *value,
393 signed shift,
394 uint64_t mask)
396 uint64_t tmp;
398 if (shift >= 0) {
399 tmp = (*value >> shift) & mask;
400 } else {
401 tmp = (*value << -shift) & mask;
404 return tmp;
407 static hwaddr memory_region_to_absolute_addr(MemoryRegion *mr, hwaddr offset)
409 MemoryRegion *root;
410 hwaddr abs_addr = offset;
412 abs_addr += mr->addr;
413 for (root = mr; root->container; ) {
414 root = root->container;
415 abs_addr += root->addr;
418 return abs_addr;
421 static int get_cpu_index(void)
423 if (current_cpu) {
424 return current_cpu->cpu_index;
426 return -1;
429 static MemTxResult memory_region_read_accessor(MemoryRegion *mr,
430 hwaddr addr,
431 uint64_t *value,
432 unsigned size,
433 signed shift,
434 uint64_t mask,
435 MemTxAttrs attrs)
437 uint64_t tmp;
439 tmp = mr->ops->read(mr->opaque, addr, size);
440 if (mr->subpage) {
441 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
442 } else if (mr == &io_mem_notdirty) {
443 /* Accesses to code which has previously been translated into a TB show
444 * up in the MMIO path, as accesses to the io_mem_notdirty
445 * MemoryRegion. */
446 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
447 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
448 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
449 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
451 memory_region_shift_read_access(value, shift, mask, tmp);
452 return MEMTX_OK;
455 static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr,
456 hwaddr addr,
457 uint64_t *value,
458 unsigned size,
459 signed shift,
460 uint64_t mask,
461 MemTxAttrs attrs)
463 uint64_t tmp = 0;
464 MemTxResult r;
466 r = mr->ops->read_with_attrs(mr->opaque, addr, &tmp, size, attrs);
467 if (mr->subpage) {
468 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
469 } else if (mr == &io_mem_notdirty) {
470 /* Accesses to code which has previously been translated into a TB show
471 * up in the MMIO path, as accesses to the io_mem_notdirty
472 * MemoryRegion. */
473 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
474 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
475 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
476 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
478 memory_region_shift_read_access(value, shift, mask, tmp);
479 return r;
482 static MemTxResult memory_region_write_accessor(MemoryRegion *mr,
483 hwaddr addr,
484 uint64_t *value,
485 unsigned size,
486 signed shift,
487 uint64_t mask,
488 MemTxAttrs attrs)
490 uint64_t tmp = memory_region_shift_write_access(value, shift, mask);
492 if (mr->subpage) {
493 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
494 } else if (mr == &io_mem_notdirty) {
495 /* Accesses to code which has previously been translated into a TB show
496 * up in the MMIO path, as accesses to the io_mem_notdirty
497 * MemoryRegion. */
498 trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
499 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
500 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
501 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
503 mr->ops->write(mr->opaque, addr, tmp, size);
504 return MEMTX_OK;
507 static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr,
508 hwaddr addr,
509 uint64_t *value,
510 unsigned size,
511 signed shift,
512 uint64_t mask,
513 MemTxAttrs attrs)
515 uint64_t tmp = memory_region_shift_write_access(value, shift, mask);
517 if (mr->subpage) {
518 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
519 } else if (mr == &io_mem_notdirty) {
520 /* Accesses to code which has previously been translated into a TB show
521 * up in the MMIO path, as accesses to the io_mem_notdirty
522 * MemoryRegion. */
523 trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
524 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
525 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
526 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
528 return mr->ops->write_with_attrs(mr->opaque, addr, tmp, size, attrs);
531 static MemTxResult access_with_adjusted_size(hwaddr addr,
532 uint64_t *value,
533 unsigned size,
534 unsigned access_size_min,
535 unsigned access_size_max,
536 MemTxResult (*access_fn)
537 (MemoryRegion *mr,
538 hwaddr addr,
539 uint64_t *value,
540 unsigned size,
541 signed shift,
542 uint64_t mask,
543 MemTxAttrs attrs),
544 MemoryRegion *mr,
545 MemTxAttrs attrs)
547 uint64_t access_mask;
548 unsigned access_size;
549 unsigned i;
550 MemTxResult r = MEMTX_OK;
552 if (!access_size_min) {
553 access_size_min = 1;
555 if (!access_size_max) {
556 access_size_max = 4;
559 /* FIXME: support unaligned access? */
560 access_size = MAX(MIN(size, access_size_max), access_size_min);
561 access_mask = MAKE_64BIT_MASK(0, access_size * 8);
562 if (memory_region_big_endian(mr)) {
563 for (i = 0; i < size; i += access_size) {
564 r |= access_fn(mr, addr + i, value, access_size,
565 (size - access_size - i) * 8, access_mask, attrs);
567 } else {
568 for (i = 0; i < size; i += access_size) {
569 r |= access_fn(mr, addr + i, value, access_size, i * 8,
570 access_mask, attrs);
573 return r;
576 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
578 AddressSpace *as;
580 while (mr->container) {
581 mr = mr->container;
583 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
584 if (mr == as->root) {
585 return as;
588 return NULL;
591 /* Render a memory region into the global view. Ranges in @view obscure
592 * ranges in @mr.
594 static void render_memory_region(FlatView *view,
595 MemoryRegion *mr,
596 Int128 base,
597 AddrRange clip,
598 bool readonly,
599 bool nonvolatile)
601 MemoryRegion *subregion;
602 unsigned i;
603 hwaddr offset_in_region;
604 Int128 remain;
605 Int128 now;
606 FlatRange fr;
607 AddrRange tmp;
609 if (!mr->enabled) {
610 return;
613 int128_addto(&base, int128_make64(mr->addr));
614 readonly |= mr->readonly;
615 nonvolatile |= mr->nonvolatile;
617 tmp = addrrange_make(base, mr->size);
619 if (!addrrange_intersects(tmp, clip)) {
620 return;
623 clip = addrrange_intersection(tmp, clip);
625 if (mr->alias) {
626 int128_subfrom(&base, int128_make64(mr->alias->addr));
627 int128_subfrom(&base, int128_make64(mr->alias_offset));
628 render_memory_region(view, mr->alias, base, clip,
629 readonly, nonvolatile);
630 return;
633 /* Render subregions in priority order. */
634 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
635 render_memory_region(view, subregion, base, clip,
636 readonly, nonvolatile);
639 if (!mr->terminates) {
640 return;
643 offset_in_region = int128_get64(int128_sub(clip.start, base));
644 base = clip.start;
645 remain = clip.size;
647 fr.mr = mr;
648 fr.dirty_log_mask = memory_region_get_dirty_log_mask(mr);
649 fr.romd_mode = mr->romd_mode;
650 fr.readonly = readonly;
651 fr.nonvolatile = nonvolatile;
652 fr.has_coalesced_range = 0;
654 /* Render the region itself into any gaps left by the current view. */
655 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
656 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
657 continue;
659 if (int128_lt(base, view->ranges[i].addr.start)) {
660 now = int128_min(remain,
661 int128_sub(view->ranges[i].addr.start, base));
662 fr.offset_in_region = offset_in_region;
663 fr.addr = addrrange_make(base, now);
664 flatview_insert(view, i, &fr);
665 ++i;
666 int128_addto(&base, now);
667 offset_in_region += int128_get64(now);
668 int128_subfrom(&remain, now);
670 now = int128_sub(int128_min(int128_add(base, remain),
671 addrrange_end(view->ranges[i].addr)),
672 base);
673 int128_addto(&base, now);
674 offset_in_region += int128_get64(now);
675 int128_subfrom(&remain, now);
677 if (int128_nz(remain)) {
678 fr.offset_in_region = offset_in_region;
679 fr.addr = addrrange_make(base, remain);
680 flatview_insert(view, i, &fr);
684 static MemoryRegion *memory_region_get_flatview_root(MemoryRegion *mr)
686 while (mr->enabled) {
687 if (mr->alias) {
688 if (!mr->alias_offset && int128_ge(mr->size, mr->alias->size)) {
689 /* The alias is included in its entirety. Use it as
690 * the "real" root, so that we can share more FlatViews.
692 mr = mr->alias;
693 continue;
695 } else if (!mr->terminates) {
696 unsigned int found = 0;
697 MemoryRegion *child, *next = NULL;
698 QTAILQ_FOREACH(child, &mr->subregions, subregions_link) {
699 if (child->enabled) {
700 if (++found > 1) {
701 next = NULL;
702 break;
704 if (!child->addr && int128_ge(mr->size, child->size)) {
705 /* A child is included in its entirety. If it's the only
706 * enabled one, use it in the hope of finding an alias down the
707 * way. This will also let us share FlatViews.
709 next = child;
713 if (found == 0) {
714 return NULL;
716 if (next) {
717 mr = next;
718 continue;
722 return mr;
725 return NULL;
728 /* Render a memory topology into a list of disjoint absolute ranges. */
729 static FlatView *generate_memory_topology(MemoryRegion *mr)
731 int i;
732 FlatView *view;
734 view = flatview_new(mr);
736 if (mr) {
737 render_memory_region(view, mr, int128_zero(),
738 addrrange_make(int128_zero(), int128_2_64()),
739 false, false);
741 flatview_simplify(view);
743 view->dispatch = address_space_dispatch_new(view);
744 for (i = 0; i < view->nr; i++) {
745 MemoryRegionSection mrs =
746 section_from_flat_range(&view->ranges[i], view);
747 flatview_add_to_dispatch(view, &mrs);
749 address_space_dispatch_compact(view->dispatch);
750 g_hash_table_replace(flat_views, mr, view);
752 return view;
755 static void address_space_add_del_ioeventfds(AddressSpace *as,
756 MemoryRegionIoeventfd *fds_new,
757 unsigned fds_new_nb,
758 MemoryRegionIoeventfd *fds_old,
759 unsigned fds_old_nb)
761 unsigned iold, inew;
762 MemoryRegionIoeventfd *fd;
763 MemoryRegionSection section;
765 /* Generate a symmetric difference of the old and new fd sets, adding
766 * and deleting as necessary.
769 iold = inew = 0;
770 while (iold < fds_old_nb || inew < fds_new_nb) {
771 if (iold < fds_old_nb
772 && (inew == fds_new_nb
773 || memory_region_ioeventfd_before(&fds_old[iold],
774 &fds_new[inew]))) {
775 fd = &fds_old[iold];
776 section = (MemoryRegionSection) {
777 .fv = address_space_to_flatview(as),
778 .offset_within_address_space = int128_get64(fd->addr.start),
779 .size = fd->addr.size,
781 MEMORY_LISTENER_CALL(as, eventfd_del, Forward, &section,
782 fd->match_data, fd->data, fd->e);
783 ++iold;
784 } else if (inew < fds_new_nb
785 && (iold == fds_old_nb
786 || memory_region_ioeventfd_before(&fds_new[inew],
787 &fds_old[iold]))) {
788 fd = &fds_new[inew];
789 section = (MemoryRegionSection) {
790 .fv = address_space_to_flatview(as),
791 .offset_within_address_space = int128_get64(fd->addr.start),
792 .size = fd->addr.size,
794 MEMORY_LISTENER_CALL(as, eventfd_add, Reverse, &section,
795 fd->match_data, fd->data, fd->e);
796 ++inew;
797 } else {
798 ++iold;
799 ++inew;
804 FlatView *address_space_get_flatview(AddressSpace *as)
806 FlatView *view;
808 rcu_read_lock();
809 do {
810 view = address_space_to_flatview(as);
811 /* If somebody has replaced as->current_map concurrently,
812 * flatview_ref returns false.
814 } while (!flatview_ref(view));
815 rcu_read_unlock();
816 return view;
819 static void address_space_update_ioeventfds(AddressSpace *as)
821 FlatView *view;
822 FlatRange *fr;
823 unsigned ioeventfd_nb = 0;
824 MemoryRegionIoeventfd *ioeventfds = NULL;
825 AddrRange tmp;
826 unsigned i;
828 view = address_space_get_flatview(as);
829 FOR_EACH_FLAT_RANGE(fr, view) {
830 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
831 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
832 int128_sub(fr->addr.start,
833 int128_make64(fr->offset_in_region)));
834 if (addrrange_intersects(fr->addr, tmp)) {
835 ++ioeventfd_nb;
836 ioeventfds = g_realloc(ioeventfds,
837 ioeventfd_nb * sizeof(*ioeventfds));
838 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
839 ioeventfds[ioeventfd_nb-1].addr = tmp;
844 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
845 as->ioeventfds, as->ioeventfd_nb);
847 g_free(as->ioeventfds);
848 as->ioeventfds = ioeventfds;
849 as->ioeventfd_nb = ioeventfd_nb;
850 flatview_unref(view);
853 static void flat_range_coalesced_io_del(FlatRange *fr, AddressSpace *as)
855 if (!fr->has_coalesced_range) {
856 return;
859 if (--fr->has_coalesced_range > 0) {
860 return;
863 MEMORY_LISTENER_UPDATE_REGION(fr, as, Reverse, coalesced_io_del,
864 int128_get64(fr->addr.start),
865 int128_get64(fr->addr.size));
868 static void flat_range_coalesced_io_add(FlatRange *fr, AddressSpace *as)
870 MemoryRegion *mr = fr->mr;
871 CoalescedMemoryRange *cmr;
872 AddrRange tmp;
874 if (QTAILQ_EMPTY(&mr->coalesced)) {
875 return;
878 if (fr->has_coalesced_range++) {
879 return;
882 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
883 tmp = addrrange_shift(cmr->addr,
884 int128_sub(fr->addr.start,
885 int128_make64(fr->offset_in_region)));
886 if (!addrrange_intersects(tmp, fr->addr)) {
887 continue;
889 tmp = addrrange_intersection(tmp, fr->addr);
890 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, coalesced_io_add,
891 int128_get64(tmp.start),
892 int128_get64(tmp.size));
896 static void address_space_update_topology_pass(AddressSpace *as,
897 const FlatView *old_view,
898 const FlatView *new_view,
899 bool adding)
901 unsigned iold, inew;
902 FlatRange *frold, *frnew;
904 /* Generate a symmetric difference of the old and new memory maps.
905 * Kill ranges in the old map, and instantiate ranges in the new map.
907 iold = inew = 0;
908 while (iold < old_view->nr || inew < new_view->nr) {
909 if (iold < old_view->nr) {
910 frold = &old_view->ranges[iold];
911 } else {
912 frold = NULL;
914 if (inew < new_view->nr) {
915 frnew = &new_view->ranges[inew];
916 } else {
917 frnew = NULL;
920 if (frold
921 && (!frnew
922 || int128_lt(frold->addr.start, frnew->addr.start)
923 || (int128_eq(frold->addr.start, frnew->addr.start)
924 && !flatrange_equal(frold, frnew)))) {
925 /* In old but not in new, or in both but attributes changed. */
927 if (!adding) {
928 flat_range_coalesced_io_del(frold, as);
929 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
932 ++iold;
933 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
934 /* In both and unchanged (except logging may have changed) */
936 if (adding) {
937 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
938 if (frnew->dirty_log_mask & ~frold->dirty_log_mask) {
939 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start,
940 frold->dirty_log_mask,
941 frnew->dirty_log_mask);
943 if (frold->dirty_log_mask & ~frnew->dirty_log_mask) {
944 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop,
945 frold->dirty_log_mask,
946 frnew->dirty_log_mask);
950 ++iold;
951 ++inew;
952 } else {
953 /* In new */
955 if (adding) {
956 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
957 flat_range_coalesced_io_add(frnew, as);
960 ++inew;
965 static void flatviews_init(void)
967 static FlatView *empty_view;
969 if (flat_views) {
970 return;
973 flat_views = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
974 (GDestroyNotify) flatview_unref);
975 if (!empty_view) {
976 empty_view = generate_memory_topology(NULL);
977 /* We keep it alive forever in the global variable. */
978 flatview_ref(empty_view);
979 } else {
980 g_hash_table_replace(flat_views, NULL, empty_view);
981 flatview_ref(empty_view);
985 static void flatviews_reset(void)
987 AddressSpace *as;
989 if (flat_views) {
990 g_hash_table_unref(flat_views);
991 flat_views = NULL;
993 flatviews_init();
995 /* Render unique FVs */
996 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
997 MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
999 if (g_hash_table_lookup(flat_views, physmr)) {
1000 continue;
1003 generate_memory_topology(physmr);
1007 static void address_space_set_flatview(AddressSpace *as)
1009 FlatView *old_view = address_space_to_flatview(as);
1010 MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
1011 FlatView *new_view = g_hash_table_lookup(flat_views, physmr);
1013 assert(new_view);
1015 if (old_view == new_view) {
1016 return;
1019 if (old_view) {
1020 flatview_ref(old_view);
1023 flatview_ref(new_view);
1025 if (!QTAILQ_EMPTY(&as->listeners)) {
1026 FlatView tmpview = { .nr = 0 }, *old_view2 = old_view;
1028 if (!old_view2) {
1029 old_view2 = &tmpview;
1031 address_space_update_topology_pass(as, old_view2, new_view, false);
1032 address_space_update_topology_pass(as, old_view2, new_view, true);
1035 /* Writes are protected by the BQL. */
1036 atomic_rcu_set(&as->current_map, new_view);
1037 if (old_view) {
1038 flatview_unref(old_view);
1041 /* Note that all the old MemoryRegions are still alive up to this
1042 * point. This relieves most MemoryListeners from the need to
1043 * ref/unref the MemoryRegions they get---unless they use them
1044 * outside the iothread mutex, in which case precise reference
1045 * counting is necessary.
1047 if (old_view) {
1048 flatview_unref(old_view);
1052 static void address_space_update_topology(AddressSpace *as)
1054 MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
1056 flatviews_init();
1057 if (!g_hash_table_lookup(flat_views, physmr)) {
1058 generate_memory_topology(physmr);
1060 address_space_set_flatview(as);
1063 void memory_region_transaction_begin(void)
1065 qemu_flush_coalesced_mmio_buffer();
1066 ++memory_region_transaction_depth;
1069 void memory_region_transaction_commit(void)
1071 AddressSpace *as;
1073 assert(memory_region_transaction_depth);
1074 assert(qemu_mutex_iothread_locked());
1076 --memory_region_transaction_depth;
1077 if (!memory_region_transaction_depth) {
1078 if (memory_region_update_pending) {
1079 flatviews_reset();
1081 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
1083 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1084 address_space_set_flatview(as);
1085 address_space_update_ioeventfds(as);
1087 memory_region_update_pending = false;
1088 ioeventfd_update_pending = false;
1089 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
1090 } else if (ioeventfd_update_pending) {
1091 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1092 address_space_update_ioeventfds(as);
1094 ioeventfd_update_pending = false;
1099 static void memory_region_destructor_none(MemoryRegion *mr)
1103 static void memory_region_destructor_ram(MemoryRegion *mr)
1105 qemu_ram_free(mr->ram_block);
1108 static bool memory_region_need_escape(char c)
1110 return c == '/' || c == '[' || c == '\\' || c == ']';
1113 static char *memory_region_escape_name(const char *name)
1115 const char *p;
1116 char *escaped, *q;
1117 uint8_t c;
1118 size_t bytes = 0;
1120 for (p = name; *p; p++) {
1121 bytes += memory_region_need_escape(*p) ? 4 : 1;
1123 if (bytes == p - name) {
1124 return g_memdup(name, bytes + 1);
1127 escaped = g_malloc(bytes + 1);
1128 for (p = name, q = escaped; *p; p++) {
1129 c = *p;
1130 if (unlikely(memory_region_need_escape(c))) {
1131 *q++ = '\\';
1132 *q++ = 'x';
1133 *q++ = "0123456789abcdef"[c >> 4];
1134 c = "0123456789abcdef"[c & 15];
1136 *q++ = c;
1138 *q = 0;
1139 return escaped;
1142 static void memory_region_do_init(MemoryRegion *mr,
1143 Object *owner,
1144 const char *name,
1145 uint64_t size)
1147 mr->size = int128_make64(size);
1148 if (size == UINT64_MAX) {
1149 mr->size = int128_2_64();
1151 mr->name = g_strdup(name);
1152 mr->owner = owner;
1153 mr->ram_block = NULL;
1155 if (name) {
1156 char *escaped_name = memory_region_escape_name(name);
1157 char *name_array = g_strdup_printf("%s[*]", escaped_name);
1159 if (!owner) {
1160 owner = container_get(qdev_get_machine(), "/unattached");
1163 object_property_add_child(owner, name_array, OBJECT(mr), &error_abort);
1164 object_unref(OBJECT(mr));
1165 g_free(name_array);
1166 g_free(escaped_name);
1170 void memory_region_init(MemoryRegion *mr,
1171 Object *owner,
1172 const char *name,
1173 uint64_t size)
1175 object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION);
1176 memory_region_do_init(mr, owner, name, size);
1179 static void memory_region_get_addr(Object *obj, Visitor *v, const char *name,
1180 void *opaque, Error **errp)
1182 MemoryRegion *mr = MEMORY_REGION(obj);
1183 uint64_t value = mr->addr;
1185 visit_type_uint64(v, name, &value, errp);
1188 static void memory_region_get_container(Object *obj, Visitor *v,
1189 const char *name, void *opaque,
1190 Error **errp)
1192 MemoryRegion *mr = MEMORY_REGION(obj);
1193 gchar *path = (gchar *)"";
1195 if (mr->container) {
1196 path = object_get_canonical_path(OBJECT(mr->container));
1198 visit_type_str(v, name, &path, errp);
1199 if (mr->container) {
1200 g_free(path);
1204 static Object *memory_region_resolve_container(Object *obj, void *opaque,
1205 const char *part)
1207 MemoryRegion *mr = MEMORY_REGION(obj);
1209 return OBJECT(mr->container);
1212 static void memory_region_get_priority(Object *obj, Visitor *v,
1213 const char *name, void *opaque,
1214 Error **errp)
1216 MemoryRegion *mr = MEMORY_REGION(obj);
1217 int32_t value = mr->priority;
1219 visit_type_int32(v, name, &value, errp);
1222 static void memory_region_get_size(Object *obj, Visitor *v, const char *name,
1223 void *opaque, Error **errp)
1225 MemoryRegion *mr = MEMORY_REGION(obj);
1226 uint64_t value = memory_region_size(mr);
1228 visit_type_uint64(v, name, &value, errp);
1231 static void memory_region_initfn(Object *obj)
1233 MemoryRegion *mr = MEMORY_REGION(obj);
1234 ObjectProperty *op;
1236 mr->ops = &unassigned_mem_ops;
1237 mr->enabled = true;
1238 mr->romd_mode = true;
1239 mr->global_locking = true;
1240 mr->destructor = memory_region_destructor_none;
1241 QTAILQ_INIT(&mr->subregions);
1242 QTAILQ_INIT(&mr->coalesced);
1244 op = object_property_add(OBJECT(mr), "container",
1245 "link<" TYPE_MEMORY_REGION ">",
1246 memory_region_get_container,
1247 NULL, /* memory_region_set_container */
1248 NULL, NULL, &error_abort);
1249 op->resolve = memory_region_resolve_container;
1251 object_property_add(OBJECT(mr), "addr", "uint64",
1252 memory_region_get_addr,
1253 NULL, /* memory_region_set_addr */
1254 NULL, NULL, &error_abort);
1255 object_property_add(OBJECT(mr), "priority", "uint32",
1256 memory_region_get_priority,
1257 NULL, /* memory_region_set_priority */
1258 NULL, NULL, &error_abort);
1259 object_property_add(OBJECT(mr), "size", "uint64",
1260 memory_region_get_size,
1261 NULL, /* memory_region_set_size, */
1262 NULL, NULL, &error_abort);
1265 static void iommu_memory_region_initfn(Object *obj)
1267 MemoryRegion *mr = MEMORY_REGION(obj);
1269 mr->is_iommu = true;
1272 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
1273 unsigned size)
1275 #ifdef DEBUG_UNASSIGNED
1276 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
1277 #endif
1278 if (current_cpu != NULL) {
1279 bool is_exec = current_cpu->mem_io_access_type == MMU_INST_FETCH;
1280 cpu_unassigned_access(current_cpu, addr, false, is_exec, 0, size);
1282 return 0;
1285 static void unassigned_mem_write(void *opaque, hwaddr addr,
1286 uint64_t val, unsigned size)
1288 #ifdef DEBUG_UNASSIGNED
1289 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
1290 #endif
1291 if (current_cpu != NULL) {
1292 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
1296 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
1297 unsigned size, bool is_write,
1298 MemTxAttrs attrs)
1300 return false;
1303 const MemoryRegionOps unassigned_mem_ops = {
1304 .valid.accepts = unassigned_mem_accepts,
1305 .endianness = DEVICE_NATIVE_ENDIAN,
1308 static uint64_t memory_region_ram_device_read(void *opaque,
1309 hwaddr addr, unsigned size)
1311 MemoryRegion *mr = opaque;
1312 uint64_t data = (uint64_t)~0;
1314 switch (size) {
1315 case 1:
1316 data = *(uint8_t *)(mr->ram_block->host + addr);
1317 break;
1318 case 2:
1319 data = *(uint16_t *)(mr->ram_block->host + addr);
1320 break;
1321 case 4:
1322 data = *(uint32_t *)(mr->ram_block->host + addr);
1323 break;
1324 case 8:
1325 data = *(uint64_t *)(mr->ram_block->host + addr);
1326 break;
1329 trace_memory_region_ram_device_read(get_cpu_index(), mr, addr, data, size);
1331 return data;
1334 static void memory_region_ram_device_write(void *opaque, hwaddr addr,
1335 uint64_t data, unsigned size)
1337 MemoryRegion *mr = opaque;
1339 trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size);
1341 switch (size) {
1342 case 1:
1343 *(uint8_t *)(mr->ram_block->host + addr) = (uint8_t)data;
1344 break;
1345 case 2:
1346 *(uint16_t *)(mr->ram_block->host + addr) = (uint16_t)data;
1347 break;
1348 case 4:
1349 *(uint32_t *)(mr->ram_block->host + addr) = (uint32_t)data;
1350 break;
1351 case 8:
1352 *(uint64_t *)(mr->ram_block->host + addr) = data;
1353 break;
1357 static const MemoryRegionOps ram_device_mem_ops = {
1358 .read = memory_region_ram_device_read,
1359 .write = memory_region_ram_device_write,
1360 .endianness = DEVICE_HOST_ENDIAN,
1361 .valid = {
1362 .min_access_size = 1,
1363 .max_access_size = 8,
1364 .unaligned = true,
1366 .impl = {
1367 .min_access_size = 1,
1368 .max_access_size = 8,
1369 .unaligned = true,
1373 bool memory_region_access_valid(MemoryRegion *mr,
1374 hwaddr addr,
1375 unsigned size,
1376 bool is_write,
1377 MemTxAttrs attrs)
1379 int access_size_min, access_size_max;
1380 int access_size, i;
1382 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
1383 return false;
1386 if (!mr->ops->valid.accepts) {
1387 return true;
1390 access_size_min = mr->ops->valid.min_access_size;
1391 if (!mr->ops->valid.min_access_size) {
1392 access_size_min = 1;
1395 access_size_max = mr->ops->valid.max_access_size;
1396 if (!mr->ops->valid.max_access_size) {
1397 access_size_max = 4;
1400 access_size = MAX(MIN(size, access_size_max), access_size_min);
1401 for (i = 0; i < size; i += access_size) {
1402 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
1403 is_write, attrs)) {
1404 return false;
1408 return true;
1411 static MemTxResult memory_region_dispatch_read1(MemoryRegion *mr,
1412 hwaddr addr,
1413 uint64_t *pval,
1414 unsigned size,
1415 MemTxAttrs attrs)
1417 *pval = 0;
1419 if (mr->ops->read) {
1420 return access_with_adjusted_size(addr, pval, size,
1421 mr->ops->impl.min_access_size,
1422 mr->ops->impl.max_access_size,
1423 memory_region_read_accessor,
1424 mr, attrs);
1425 } else {
1426 return access_with_adjusted_size(addr, pval, size,
1427 mr->ops->impl.min_access_size,
1428 mr->ops->impl.max_access_size,
1429 memory_region_read_with_attrs_accessor,
1430 mr, attrs);
1434 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1435 hwaddr addr,
1436 uint64_t *pval,
1437 unsigned size,
1438 MemTxAttrs attrs)
1440 MemTxResult r;
1442 if (!memory_region_access_valid(mr, addr, size, false, attrs)) {
1443 *pval = unassigned_mem_read(mr, addr, size);
1444 return MEMTX_DECODE_ERROR;
1447 r = memory_region_dispatch_read1(mr, addr, pval, size, attrs);
1448 adjust_endianness(mr, pval, size);
1449 return r;
1452 /* Return true if an eventfd was signalled */
1453 static bool memory_region_dispatch_write_eventfds(MemoryRegion *mr,
1454 hwaddr addr,
1455 uint64_t data,
1456 unsigned size,
1457 MemTxAttrs attrs)
1459 MemoryRegionIoeventfd ioeventfd = {
1460 .addr = addrrange_make(int128_make64(addr), int128_make64(size)),
1461 .data = data,
1463 unsigned i;
1465 for (i = 0; i < mr->ioeventfd_nb; i++) {
1466 ioeventfd.match_data = mr->ioeventfds[i].match_data;
1467 ioeventfd.e = mr->ioeventfds[i].e;
1469 if (memory_region_ioeventfd_equal(&ioeventfd, &mr->ioeventfds[i])) {
1470 event_notifier_set(ioeventfd.e);
1471 return true;
1475 return false;
1478 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1479 hwaddr addr,
1480 uint64_t data,
1481 unsigned size,
1482 MemTxAttrs attrs)
1484 if (!memory_region_access_valid(mr, addr, size, true, attrs)) {
1485 unassigned_mem_write(mr, addr, data, size);
1486 return MEMTX_DECODE_ERROR;
1489 adjust_endianness(mr, &data, size);
1491 if ((!kvm_eventfds_enabled()) &&
1492 memory_region_dispatch_write_eventfds(mr, addr, data, size, attrs)) {
1493 return MEMTX_OK;
1496 if (mr->ops->write) {
1497 return access_with_adjusted_size(addr, &data, size,
1498 mr->ops->impl.min_access_size,
1499 mr->ops->impl.max_access_size,
1500 memory_region_write_accessor, mr,
1501 attrs);
1502 } else {
1503 return
1504 access_with_adjusted_size(addr, &data, size,
1505 mr->ops->impl.min_access_size,
1506 mr->ops->impl.max_access_size,
1507 memory_region_write_with_attrs_accessor,
1508 mr, attrs);
1512 void memory_region_init_io(MemoryRegion *mr,
1513 Object *owner,
1514 const MemoryRegionOps *ops,
1515 void *opaque,
1516 const char *name,
1517 uint64_t size)
1519 memory_region_init(mr, owner, name, size);
1520 mr->ops = ops ? ops : &unassigned_mem_ops;
1521 mr->opaque = opaque;
1522 mr->terminates = true;
1525 void memory_region_init_ram_nomigrate(MemoryRegion *mr,
1526 Object *owner,
1527 const char *name,
1528 uint64_t size,
1529 Error **errp)
1531 memory_region_init_ram_shared_nomigrate(mr, owner, name, size, false, errp);
1534 void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
1535 Object *owner,
1536 const char *name,
1537 uint64_t size,
1538 bool share,
1539 Error **errp)
1541 Error *err = NULL;
1542 memory_region_init(mr, owner, name, size);
1543 mr->ram = true;
1544 mr->terminates = true;
1545 mr->destructor = memory_region_destructor_ram;
1546 mr->ram_block = qemu_ram_alloc(size, share, mr, &err);
1547 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1548 if (err) {
1549 mr->size = int128_zero();
1550 object_unparent(OBJECT(mr));
1551 error_propagate(errp, err);
1555 void memory_region_init_resizeable_ram(MemoryRegion *mr,
1556 Object *owner,
1557 const char *name,
1558 uint64_t size,
1559 uint64_t max_size,
1560 void (*resized)(const char*,
1561 uint64_t length,
1562 void *host),
1563 Error **errp)
1565 Error *err = NULL;
1566 memory_region_init(mr, owner, name, size);
1567 mr->ram = true;
1568 mr->terminates = true;
1569 mr->destructor = memory_region_destructor_ram;
1570 mr->ram_block = qemu_ram_alloc_resizeable(size, max_size, resized,
1571 mr, &err);
1572 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1573 if (err) {
1574 mr->size = int128_zero();
1575 object_unparent(OBJECT(mr));
1576 error_propagate(errp, err);
1580 #ifdef CONFIG_POSIX
1581 void memory_region_init_ram_from_file(MemoryRegion *mr,
1582 struct Object *owner,
1583 const char *name,
1584 uint64_t size,
1585 uint64_t align,
1586 uint32_t ram_flags,
1587 const char *path,
1588 Error **errp)
1590 Error *err = NULL;
1591 memory_region_init(mr, owner, name, size);
1592 mr->ram = true;
1593 mr->terminates = true;
1594 mr->destructor = memory_region_destructor_ram;
1595 mr->align = align;
1596 mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path, &err);
1597 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1598 if (err) {
1599 mr->size = int128_zero();
1600 object_unparent(OBJECT(mr));
1601 error_propagate(errp, err);
1605 void memory_region_init_ram_from_fd(MemoryRegion *mr,
1606 struct Object *owner,
1607 const char *name,
1608 uint64_t size,
1609 bool share,
1610 int fd,
1611 Error **errp)
1613 Error *err = NULL;
1614 memory_region_init(mr, owner, name, size);
1615 mr->ram = true;
1616 mr->terminates = true;
1617 mr->destructor = memory_region_destructor_ram;
1618 mr->ram_block = qemu_ram_alloc_from_fd(size, mr,
1619 share ? RAM_SHARED : 0,
1620 fd, &err);
1621 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1622 if (err) {
1623 mr->size = int128_zero();
1624 object_unparent(OBJECT(mr));
1625 error_propagate(errp, err);
1628 #endif
1630 void memory_region_init_ram_ptr(MemoryRegion *mr,
1631 Object *owner,
1632 const char *name,
1633 uint64_t size,
1634 void *ptr)
1636 memory_region_init(mr, owner, name, size);
1637 mr->ram = true;
1638 mr->terminates = true;
1639 mr->destructor = memory_region_destructor_ram;
1640 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1642 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1643 assert(ptr != NULL);
1644 mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
1647 void memory_region_init_ram_device_ptr(MemoryRegion *mr,
1648 Object *owner,
1649 const char *name,
1650 uint64_t size,
1651 void *ptr)
1653 memory_region_init(mr, owner, name, size);
1654 mr->ram = true;
1655 mr->terminates = true;
1656 mr->ram_device = true;
1657 mr->ops = &ram_device_mem_ops;
1658 mr->opaque = mr;
1659 mr->destructor = memory_region_destructor_ram;
1660 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1661 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1662 assert(ptr != NULL);
1663 mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
1666 void memory_region_init_alias(MemoryRegion *mr,
1667 Object *owner,
1668 const char *name,
1669 MemoryRegion *orig,
1670 hwaddr offset,
1671 uint64_t size)
1673 memory_region_init(mr, owner, name, size);
1674 mr->alias = orig;
1675 mr->alias_offset = offset;
1678 void memory_region_init_rom_nomigrate(MemoryRegion *mr,
1679 struct Object *owner,
1680 const char *name,
1681 uint64_t size,
1682 Error **errp)
1684 Error *err = NULL;
1685 memory_region_init(mr, owner, name, size);
1686 mr->ram = true;
1687 mr->readonly = true;
1688 mr->terminates = true;
1689 mr->destructor = memory_region_destructor_ram;
1690 mr->ram_block = qemu_ram_alloc(size, false, mr, &err);
1691 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1692 if (err) {
1693 mr->size = int128_zero();
1694 object_unparent(OBJECT(mr));
1695 error_propagate(errp, err);
1699 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
1700 Object *owner,
1701 const MemoryRegionOps *ops,
1702 void *opaque,
1703 const char *name,
1704 uint64_t size,
1705 Error **errp)
1707 Error *err = NULL;
1708 assert(ops);
1709 memory_region_init(mr, owner, name, size);
1710 mr->ops = ops;
1711 mr->opaque = opaque;
1712 mr->terminates = true;
1713 mr->rom_device = true;
1714 mr->destructor = memory_region_destructor_ram;
1715 mr->ram_block = qemu_ram_alloc(size, false, mr, &err);
1716 if (err) {
1717 mr->size = int128_zero();
1718 object_unparent(OBJECT(mr));
1719 error_propagate(errp, err);
1723 void memory_region_init_iommu(void *_iommu_mr,
1724 size_t instance_size,
1725 const char *mrtypename,
1726 Object *owner,
1727 const char *name,
1728 uint64_t size)
1730 struct IOMMUMemoryRegion *iommu_mr;
1731 struct MemoryRegion *mr;
1733 object_initialize(_iommu_mr, instance_size, mrtypename);
1734 mr = MEMORY_REGION(_iommu_mr);
1735 memory_region_do_init(mr, owner, name, size);
1736 iommu_mr = IOMMU_MEMORY_REGION(mr);
1737 mr->terminates = true; /* then re-forwards */
1738 QLIST_INIT(&iommu_mr->iommu_notify);
1739 iommu_mr->iommu_notify_flags = IOMMU_NOTIFIER_NONE;
1742 static void memory_region_finalize(Object *obj)
1744 MemoryRegion *mr = MEMORY_REGION(obj);
1746 assert(!mr->container);
1748 /* We know the region is not visible in any address space (it
1749 * does not have a container and cannot be a root either because
1750 * it has no references, so we can blindly clear mr->enabled.
1751 * memory_region_set_enabled instead could trigger a transaction
1752 * and cause an infinite loop.
1754 mr->enabled = false;
1755 memory_region_transaction_begin();
1756 while (!QTAILQ_EMPTY(&mr->subregions)) {
1757 MemoryRegion *subregion = QTAILQ_FIRST(&mr->subregions);
1758 memory_region_del_subregion(mr, subregion);
1760 memory_region_transaction_commit();
1762 mr->destructor(mr);
1763 memory_region_clear_coalescing(mr);
1764 g_free((char *)mr->name);
1765 g_free(mr->ioeventfds);
1768 Object *memory_region_owner(MemoryRegion *mr)
1770 Object *obj = OBJECT(mr);
1771 return obj->parent;
1774 void memory_region_ref(MemoryRegion *mr)
1776 /* MMIO callbacks most likely will access data that belongs
1777 * to the owner, hence the need to ref/unref the owner whenever
1778 * the memory region is in use.
1780 * The memory region is a child of its owner. As long as the
1781 * owner doesn't call unparent itself on the memory region,
1782 * ref-ing the owner will also keep the memory region alive.
1783 * Memory regions without an owner are supposed to never go away;
1784 * we do not ref/unref them because it slows down DMA sensibly.
1786 if (mr && mr->owner) {
1787 object_ref(mr->owner);
1791 void memory_region_unref(MemoryRegion *mr)
1793 if (mr && mr->owner) {
1794 object_unref(mr->owner);
1798 uint64_t memory_region_size(MemoryRegion *mr)
1800 if (int128_eq(mr->size, int128_2_64())) {
1801 return UINT64_MAX;
1803 return int128_get64(mr->size);
1806 const char *memory_region_name(const MemoryRegion *mr)
1808 if (!mr->name) {
1809 ((MemoryRegion *)mr)->name =
1810 object_get_canonical_path_component(OBJECT(mr));
1812 return mr->name;
1815 bool memory_region_is_ram_device(MemoryRegion *mr)
1817 return mr->ram_device;
1820 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
1822 uint8_t mask = mr->dirty_log_mask;
1823 if (global_dirty_log && mr->ram_block) {
1824 mask |= (1 << DIRTY_MEMORY_MIGRATION);
1826 return mask;
1829 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client)
1831 return memory_region_get_dirty_log_mask(mr) & (1 << client);
1834 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion *iommu_mr)
1836 IOMMUNotifierFlag flags = IOMMU_NOTIFIER_NONE;
1837 IOMMUNotifier *iommu_notifier;
1838 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1840 IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) {
1841 flags |= iommu_notifier->notifier_flags;
1844 if (flags != iommu_mr->iommu_notify_flags && imrc->notify_flag_changed) {
1845 imrc->notify_flag_changed(iommu_mr,
1846 iommu_mr->iommu_notify_flags,
1847 flags);
1850 iommu_mr->iommu_notify_flags = flags;
1853 void memory_region_register_iommu_notifier(MemoryRegion *mr,
1854 IOMMUNotifier *n)
1856 IOMMUMemoryRegion *iommu_mr;
1858 if (mr->alias) {
1859 memory_region_register_iommu_notifier(mr->alias, n);
1860 return;
1863 /* We need to register for at least one bitfield */
1864 iommu_mr = IOMMU_MEMORY_REGION(mr);
1865 assert(n->notifier_flags != IOMMU_NOTIFIER_NONE);
1866 assert(n->start <= n->end);
1867 assert(n->iommu_idx >= 0 &&
1868 n->iommu_idx < memory_region_iommu_num_indexes(iommu_mr));
1870 QLIST_INSERT_HEAD(&iommu_mr->iommu_notify, n, node);
1871 memory_region_update_iommu_notify_flags(iommu_mr);
1874 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr)
1876 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1878 if (imrc->get_min_page_size) {
1879 return imrc->get_min_page_size(iommu_mr);
1881 return TARGET_PAGE_SIZE;
1884 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
1886 MemoryRegion *mr = MEMORY_REGION(iommu_mr);
1887 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1888 hwaddr addr, granularity;
1889 IOMMUTLBEntry iotlb;
1891 /* If the IOMMU has its own replay callback, override */
1892 if (imrc->replay) {
1893 imrc->replay(iommu_mr, n);
1894 return;
1897 granularity = memory_region_iommu_get_min_page_size(iommu_mr);
1899 for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
1900 iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE, n->iommu_idx);
1901 if (iotlb.perm != IOMMU_NONE) {
1902 n->notify(n, &iotlb);
1905 /* if (2^64 - MR size) < granularity, it's possible to get an
1906 * infinite loop here. This should catch such a wraparound */
1907 if ((addr + granularity) < addr) {
1908 break;
1913 void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr)
1915 IOMMUNotifier *notifier;
1917 IOMMU_NOTIFIER_FOREACH(notifier, iommu_mr) {
1918 memory_region_iommu_replay(iommu_mr, notifier);
1922 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1923 IOMMUNotifier *n)
1925 IOMMUMemoryRegion *iommu_mr;
1927 if (mr->alias) {
1928 memory_region_unregister_iommu_notifier(mr->alias, n);
1929 return;
1931 QLIST_REMOVE(n, node);
1932 iommu_mr = IOMMU_MEMORY_REGION(mr);
1933 memory_region_update_iommu_notify_flags(iommu_mr);
1936 void memory_region_notify_one(IOMMUNotifier *notifier,
1937 IOMMUTLBEntry *entry)
1939 IOMMUNotifierFlag request_flags;
1942 * Skip the notification if the notification does not overlap
1943 * with registered range.
1945 if (notifier->start > entry->iova + entry->addr_mask ||
1946 notifier->end < entry->iova) {
1947 return;
1950 if (entry->perm & IOMMU_RW) {
1951 request_flags = IOMMU_NOTIFIER_MAP;
1952 } else {
1953 request_flags = IOMMU_NOTIFIER_UNMAP;
1956 if (notifier->notifier_flags & request_flags) {
1957 notifier->notify(notifier, entry);
1961 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1962 int iommu_idx,
1963 IOMMUTLBEntry entry)
1965 IOMMUNotifier *iommu_notifier;
1967 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr)));
1969 IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) {
1970 if (iommu_notifier->iommu_idx == iommu_idx) {
1971 memory_region_notify_one(iommu_notifier, &entry);
1976 int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1977 enum IOMMUMemoryRegionAttr attr,
1978 void *data)
1980 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1982 if (!imrc->get_attr) {
1983 return -EINVAL;
1986 return imrc->get_attr(iommu_mr, attr, data);
1989 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr,
1990 MemTxAttrs attrs)
1992 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1994 if (!imrc->attrs_to_index) {
1995 return 0;
1998 return imrc->attrs_to_index(iommu_mr, attrs);
2001 int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr)
2003 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
2005 if (!imrc->num_indexes) {
2006 return 1;
2009 return imrc->num_indexes(iommu_mr);
2012 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
2014 uint8_t mask = 1 << client;
2015 uint8_t old_logging;
2017 assert(client == DIRTY_MEMORY_VGA);
2018 old_logging = mr->vga_logging_count;
2019 mr->vga_logging_count += log ? 1 : -1;
2020 if (!!old_logging == !!mr->vga_logging_count) {
2021 return;
2024 memory_region_transaction_begin();
2025 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
2026 memory_region_update_pending |= mr->enabled;
2027 memory_region_transaction_commit();
2030 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
2031 hwaddr size, unsigned client)
2033 assert(mr->ram_block);
2034 return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr) + addr,
2035 size, client);
2038 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
2039 hwaddr size)
2041 assert(mr->ram_block);
2042 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr) + addr,
2043 size,
2044 memory_region_get_dirty_log_mask(mr));
2047 static void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
2049 MemoryListener *listener;
2050 AddressSpace *as;
2051 FlatView *view;
2052 FlatRange *fr;
2054 /* If the same address space has multiple log_sync listeners, we
2055 * visit that address space's FlatView multiple times. But because
2056 * log_sync listeners are rare, it's still cheaper than walking each
2057 * address space once.
2059 QTAILQ_FOREACH(listener, &memory_listeners, link) {
2060 if (!listener->log_sync) {
2061 continue;
2063 as = listener->address_space;
2064 view = address_space_get_flatview(as);
2065 FOR_EACH_FLAT_RANGE(fr, view) {
2066 if (fr->dirty_log_mask && (!mr || fr->mr == mr)) {
2067 MemoryRegionSection mrs = section_from_flat_range(fr, view);
2068 listener->log_sync(listener, &mrs);
2071 flatview_unref(view);
2075 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
2076 hwaddr addr,
2077 hwaddr size,
2078 unsigned client)
2080 assert(mr->ram_block);
2081 memory_region_sync_dirty_bitmap(mr);
2082 return cpu_physical_memory_snapshot_and_clear_dirty(
2083 memory_region_get_ram_addr(mr) + addr, size, client);
2086 bool memory_region_snapshot_get_dirty(MemoryRegion *mr, DirtyBitmapSnapshot *snap,
2087 hwaddr addr, hwaddr size)
2089 assert(mr->ram_block);
2090 return cpu_physical_memory_snapshot_get_dirty(snap,
2091 memory_region_get_ram_addr(mr) + addr, size);
2094 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
2096 if (mr->readonly != readonly) {
2097 memory_region_transaction_begin();
2098 mr->readonly = readonly;
2099 memory_region_update_pending |= mr->enabled;
2100 memory_region_transaction_commit();
2104 void memory_region_set_nonvolatile(MemoryRegion *mr, bool nonvolatile)
2106 if (mr->nonvolatile != nonvolatile) {
2107 memory_region_transaction_begin();
2108 mr->nonvolatile = nonvolatile;
2109 memory_region_update_pending |= mr->enabled;
2110 memory_region_transaction_commit();
2114 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
2116 if (mr->romd_mode != romd_mode) {
2117 memory_region_transaction_begin();
2118 mr->romd_mode = romd_mode;
2119 memory_region_update_pending |= mr->enabled;
2120 memory_region_transaction_commit();
2124 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
2125 hwaddr size, unsigned client)
2127 assert(mr->ram_block);
2128 cpu_physical_memory_test_and_clear_dirty(
2129 memory_region_get_ram_addr(mr) + addr, size, client);
2132 int memory_region_get_fd(MemoryRegion *mr)
2134 int fd;
2136 rcu_read_lock();
2137 while (mr->alias) {
2138 mr = mr->alias;
2140 fd = mr->ram_block->fd;
2141 rcu_read_unlock();
2143 return fd;
2146 void *memory_region_get_ram_ptr(MemoryRegion *mr)
2148 void *ptr;
2149 uint64_t offset = 0;
2151 rcu_read_lock();
2152 while (mr->alias) {
2153 offset += mr->alias_offset;
2154 mr = mr->alias;
2156 assert(mr->ram_block);
2157 ptr = qemu_map_ram_ptr(mr->ram_block, offset);
2158 rcu_read_unlock();
2160 return ptr;
2163 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset)
2165 RAMBlock *block;
2167 block = qemu_ram_block_from_host(ptr, false, offset);
2168 if (!block) {
2169 return NULL;
2172 return block->mr;
2175 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
2177 return mr->ram_block ? mr->ram_block->offset : RAM_ADDR_INVALID;
2180 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp)
2182 assert(mr->ram_block);
2184 qemu_ram_resize(mr->ram_block, newsize, errp);
2187 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
2189 FlatView *view;
2190 FlatRange *fr;
2192 view = address_space_get_flatview(as);
2193 FOR_EACH_FLAT_RANGE(fr, view) {
2194 if (fr->mr == mr) {
2195 flat_range_coalesced_io_del(fr, as);
2196 flat_range_coalesced_io_add(fr, as);
2199 flatview_unref(view);
2202 static void memory_region_update_coalesced_range(MemoryRegion *mr)
2204 AddressSpace *as;
2206 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2207 memory_region_update_coalesced_range_as(mr, as);
2211 void memory_region_set_coalescing(MemoryRegion *mr)
2213 memory_region_clear_coalescing(mr);
2214 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
2217 void memory_region_add_coalescing(MemoryRegion *mr,
2218 hwaddr offset,
2219 uint64_t size)
2221 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
2223 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
2224 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
2225 memory_region_update_coalesced_range(mr);
2226 memory_region_set_flush_coalesced(mr);
2229 void memory_region_clear_coalescing(MemoryRegion *mr)
2231 CoalescedMemoryRange *cmr;
2232 bool updated = false;
2234 qemu_flush_coalesced_mmio_buffer();
2235 mr->flush_coalesced_mmio = false;
2237 while (!QTAILQ_EMPTY(&mr->coalesced)) {
2238 cmr = QTAILQ_FIRST(&mr->coalesced);
2239 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
2240 g_free(cmr);
2241 updated = true;
2244 if (updated) {
2245 memory_region_update_coalesced_range(mr);
2249 void memory_region_set_flush_coalesced(MemoryRegion *mr)
2251 mr->flush_coalesced_mmio = true;
2254 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
2256 qemu_flush_coalesced_mmio_buffer();
2257 if (QTAILQ_EMPTY(&mr->coalesced)) {
2258 mr->flush_coalesced_mmio = false;
2262 void memory_region_clear_global_locking(MemoryRegion *mr)
2264 mr->global_locking = false;
2267 static bool userspace_eventfd_warning;
2269 void memory_region_add_eventfd(MemoryRegion *mr,
2270 hwaddr addr,
2271 unsigned size,
2272 bool match_data,
2273 uint64_t data,
2274 EventNotifier *e)
2276 MemoryRegionIoeventfd mrfd = {
2277 .addr.start = int128_make64(addr),
2278 .addr.size = int128_make64(size),
2279 .match_data = match_data,
2280 .data = data,
2281 .e = e,
2283 unsigned i;
2285 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2286 userspace_eventfd_warning))) {
2287 userspace_eventfd_warning = true;
2288 error_report("Using eventfd without MMIO binding in KVM. "
2289 "Suboptimal performance expected");
2292 if (size) {
2293 adjust_endianness(mr, &mrfd.data, size);
2295 memory_region_transaction_begin();
2296 for (i = 0; i < mr->ioeventfd_nb; ++i) {
2297 if (memory_region_ioeventfd_before(&mrfd, &mr->ioeventfds[i])) {
2298 break;
2301 ++mr->ioeventfd_nb;
2302 mr->ioeventfds = g_realloc(mr->ioeventfds,
2303 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
2304 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
2305 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
2306 mr->ioeventfds[i] = mrfd;
2307 ioeventfd_update_pending |= mr->enabled;
2308 memory_region_transaction_commit();
2311 void memory_region_del_eventfd(MemoryRegion *mr,
2312 hwaddr addr,
2313 unsigned size,
2314 bool match_data,
2315 uint64_t data,
2316 EventNotifier *e)
2318 MemoryRegionIoeventfd mrfd = {
2319 .addr.start = int128_make64(addr),
2320 .addr.size = int128_make64(size),
2321 .match_data = match_data,
2322 .data = data,
2323 .e = e,
2325 unsigned i;
2327 if (size) {
2328 adjust_endianness(mr, &mrfd.data, size);
2330 memory_region_transaction_begin();
2331 for (i = 0; i < mr->ioeventfd_nb; ++i) {
2332 if (memory_region_ioeventfd_equal(&mrfd, &mr->ioeventfds[i])) {
2333 break;
2336 assert(i != mr->ioeventfd_nb);
2337 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
2338 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
2339 --mr->ioeventfd_nb;
2340 mr->ioeventfds = g_realloc(mr->ioeventfds,
2341 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
2342 ioeventfd_update_pending |= mr->enabled;
2343 memory_region_transaction_commit();
2346 static void memory_region_update_container_subregions(MemoryRegion *subregion)
2348 MemoryRegion *mr = subregion->container;
2349 MemoryRegion *other;
2351 memory_region_transaction_begin();
2353 memory_region_ref(subregion);
2354 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
2355 if (subregion->priority >= other->priority) {
2356 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
2357 goto done;
2360 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
2361 done:
2362 memory_region_update_pending |= mr->enabled && subregion->enabled;
2363 memory_region_transaction_commit();
2366 static void memory_region_add_subregion_common(MemoryRegion *mr,
2367 hwaddr offset,
2368 MemoryRegion *subregion)
2370 assert(!subregion->container);
2371 subregion->container = mr;
2372 subregion->addr = offset;
2373 memory_region_update_container_subregions(subregion);
2376 void memory_region_add_subregion(MemoryRegion *mr,
2377 hwaddr offset,
2378 MemoryRegion *subregion)
2380 subregion->priority = 0;
2381 memory_region_add_subregion_common(mr, offset, subregion);
2384 void memory_region_add_subregion_overlap(MemoryRegion *mr,
2385 hwaddr offset,
2386 MemoryRegion *subregion,
2387 int priority)
2389 subregion->priority = priority;
2390 memory_region_add_subregion_common(mr, offset, subregion);
2393 void memory_region_del_subregion(MemoryRegion *mr,
2394 MemoryRegion *subregion)
2396 memory_region_transaction_begin();
2397 assert(subregion->container == mr);
2398 subregion->container = NULL;
2399 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
2400 memory_region_unref(subregion);
2401 memory_region_update_pending |= mr->enabled && subregion->enabled;
2402 memory_region_transaction_commit();
2405 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
2407 if (enabled == mr->enabled) {
2408 return;
2410 memory_region_transaction_begin();
2411 mr->enabled = enabled;
2412 memory_region_update_pending = true;
2413 memory_region_transaction_commit();
2416 void memory_region_set_size(MemoryRegion *mr, uint64_t size)
2418 Int128 s = int128_make64(size);
2420 if (size == UINT64_MAX) {
2421 s = int128_2_64();
2423 if (int128_eq(s, mr->size)) {
2424 return;
2426 memory_region_transaction_begin();
2427 mr->size = s;
2428 memory_region_update_pending = true;
2429 memory_region_transaction_commit();
2432 static void memory_region_readd_subregion(MemoryRegion *mr)
2434 MemoryRegion *container = mr->container;
2436 if (container) {
2437 memory_region_transaction_begin();
2438 memory_region_ref(mr);
2439 memory_region_del_subregion(container, mr);
2440 mr->container = container;
2441 memory_region_update_container_subregions(mr);
2442 memory_region_unref(mr);
2443 memory_region_transaction_commit();
2447 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
2449 if (addr != mr->addr) {
2450 mr->addr = addr;
2451 memory_region_readd_subregion(mr);
2455 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
2457 assert(mr->alias);
2459 if (offset == mr->alias_offset) {
2460 return;
2463 memory_region_transaction_begin();
2464 mr->alias_offset = offset;
2465 memory_region_update_pending |= mr->enabled;
2466 memory_region_transaction_commit();
2469 uint64_t memory_region_get_alignment(const MemoryRegion *mr)
2471 return mr->align;
2474 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
2476 const AddrRange *addr = addr_;
2477 const FlatRange *fr = fr_;
2479 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
2480 return -1;
2481 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
2482 return 1;
2484 return 0;
2487 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
2489 return bsearch(&addr, view->ranges, view->nr,
2490 sizeof(FlatRange), cmp_flatrange_addr);
2493 bool memory_region_is_mapped(MemoryRegion *mr)
2495 return mr->container ? true : false;
2498 /* Same as memory_region_find, but it does not add a reference to the
2499 * returned region. It must be called from an RCU critical section.
2501 static MemoryRegionSection memory_region_find_rcu(MemoryRegion *mr,
2502 hwaddr addr, uint64_t size)
2504 MemoryRegionSection ret = { .mr = NULL };
2505 MemoryRegion *root;
2506 AddressSpace *as;
2507 AddrRange range;
2508 FlatView *view;
2509 FlatRange *fr;
2511 addr += mr->addr;
2512 for (root = mr; root->container; ) {
2513 root = root->container;
2514 addr += root->addr;
2517 as = memory_region_to_address_space(root);
2518 if (!as) {
2519 return ret;
2521 range = addrrange_make(int128_make64(addr), int128_make64(size));
2523 view = address_space_to_flatview(as);
2524 fr = flatview_lookup(view, range);
2525 if (!fr) {
2526 return ret;
2529 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
2530 --fr;
2533 ret.mr = fr->mr;
2534 ret.fv = view;
2535 range = addrrange_intersection(range, fr->addr);
2536 ret.offset_within_region = fr->offset_in_region;
2537 ret.offset_within_region += int128_get64(int128_sub(range.start,
2538 fr->addr.start));
2539 ret.size = range.size;
2540 ret.offset_within_address_space = int128_get64(range.start);
2541 ret.readonly = fr->readonly;
2542 ret.nonvolatile = fr->nonvolatile;
2543 return ret;
2546 MemoryRegionSection memory_region_find(MemoryRegion *mr,
2547 hwaddr addr, uint64_t size)
2549 MemoryRegionSection ret;
2550 rcu_read_lock();
2551 ret = memory_region_find_rcu(mr, addr, size);
2552 if (ret.mr) {
2553 memory_region_ref(ret.mr);
2555 rcu_read_unlock();
2556 return ret;
2559 bool memory_region_present(MemoryRegion *container, hwaddr addr)
2561 MemoryRegion *mr;
2563 rcu_read_lock();
2564 mr = memory_region_find_rcu(container, addr, 1).mr;
2565 rcu_read_unlock();
2566 return mr && mr != container;
2569 void memory_global_dirty_log_sync(void)
2571 memory_region_sync_dirty_bitmap(NULL);
2574 static VMChangeStateEntry *vmstate_change;
2576 void memory_global_dirty_log_start(void)
2578 if (vmstate_change) {
2579 qemu_del_vm_change_state_handler(vmstate_change);
2580 vmstate_change = NULL;
2583 global_dirty_log = true;
2585 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
2587 /* Refresh DIRTY_LOG_MIGRATION bit. */
2588 memory_region_transaction_begin();
2589 memory_region_update_pending = true;
2590 memory_region_transaction_commit();
2593 static void memory_global_dirty_log_do_stop(void)
2595 global_dirty_log = false;
2597 /* Refresh DIRTY_LOG_MIGRATION bit. */
2598 memory_region_transaction_begin();
2599 memory_region_update_pending = true;
2600 memory_region_transaction_commit();
2602 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
2605 static void memory_vm_change_state_handler(void *opaque, int running,
2606 RunState state)
2608 if (running) {
2609 memory_global_dirty_log_do_stop();
2611 if (vmstate_change) {
2612 qemu_del_vm_change_state_handler(vmstate_change);
2613 vmstate_change = NULL;
2618 void memory_global_dirty_log_stop(void)
2620 if (!runstate_is_running()) {
2621 if (vmstate_change) {
2622 return;
2624 vmstate_change = qemu_add_vm_change_state_handler(
2625 memory_vm_change_state_handler, NULL);
2626 return;
2629 memory_global_dirty_log_do_stop();
2632 static void listener_add_address_space(MemoryListener *listener,
2633 AddressSpace *as)
2635 FlatView *view;
2636 FlatRange *fr;
2638 if (listener->begin) {
2639 listener->begin(listener);
2641 if (global_dirty_log) {
2642 if (listener->log_global_start) {
2643 listener->log_global_start(listener);
2647 view = address_space_get_flatview(as);
2648 FOR_EACH_FLAT_RANGE(fr, view) {
2649 MemoryRegionSection section = section_from_flat_range(fr, view);
2651 if (listener->region_add) {
2652 listener->region_add(listener, &section);
2654 if (fr->dirty_log_mask && listener->log_start) {
2655 listener->log_start(listener, &section, 0, fr->dirty_log_mask);
2658 if (listener->commit) {
2659 listener->commit(listener);
2661 flatview_unref(view);
2664 static void listener_del_address_space(MemoryListener *listener,
2665 AddressSpace *as)
2667 FlatView *view;
2668 FlatRange *fr;
2670 if (listener->begin) {
2671 listener->begin(listener);
2673 view = address_space_get_flatview(as);
2674 FOR_EACH_FLAT_RANGE(fr, view) {
2675 MemoryRegionSection section = section_from_flat_range(fr, view);
2677 if (fr->dirty_log_mask && listener->log_stop) {
2678 listener->log_stop(listener, &section, fr->dirty_log_mask, 0);
2680 if (listener->region_del) {
2681 listener->region_del(listener, &section);
2684 if (listener->commit) {
2685 listener->commit(listener);
2687 flatview_unref(view);
2690 void memory_listener_register(MemoryListener *listener, AddressSpace *as)
2692 MemoryListener *other = NULL;
2694 listener->address_space = as;
2695 if (QTAILQ_EMPTY(&memory_listeners)
2696 || listener->priority >= QTAILQ_LAST(&memory_listeners)->priority) {
2697 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
2698 } else {
2699 QTAILQ_FOREACH(other, &memory_listeners, link) {
2700 if (listener->priority < other->priority) {
2701 break;
2704 QTAILQ_INSERT_BEFORE(other, listener, link);
2707 if (QTAILQ_EMPTY(&as->listeners)
2708 || listener->priority >= QTAILQ_LAST(&as->listeners)->priority) {
2709 QTAILQ_INSERT_TAIL(&as->listeners, listener, link_as);
2710 } else {
2711 QTAILQ_FOREACH(other, &as->listeners, link_as) {
2712 if (listener->priority < other->priority) {
2713 break;
2716 QTAILQ_INSERT_BEFORE(other, listener, link_as);
2719 listener_add_address_space(listener, as);
2722 void memory_listener_unregister(MemoryListener *listener)
2724 if (!listener->address_space) {
2725 return;
2728 listener_del_address_space(listener, listener->address_space);
2729 QTAILQ_REMOVE(&memory_listeners, listener, link);
2730 QTAILQ_REMOVE(&listener->address_space->listeners, listener, link_as);
2731 listener->address_space = NULL;
2734 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
2736 memory_region_ref(root);
2737 as->root = root;
2738 as->current_map = NULL;
2739 as->ioeventfd_nb = 0;
2740 as->ioeventfds = NULL;
2741 QTAILQ_INIT(&as->listeners);
2742 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
2743 as->name = g_strdup(name ? name : "anonymous");
2744 address_space_update_topology(as);
2745 address_space_update_ioeventfds(as);
2748 static void do_address_space_destroy(AddressSpace *as)
2750 assert(QTAILQ_EMPTY(&as->listeners));
2752 flatview_unref(as->current_map);
2753 g_free(as->name);
2754 g_free(as->ioeventfds);
2755 memory_region_unref(as->root);
2758 void address_space_destroy(AddressSpace *as)
2760 MemoryRegion *root = as->root;
2762 /* Flush out anything from MemoryListeners listening in on this */
2763 memory_region_transaction_begin();
2764 as->root = NULL;
2765 memory_region_transaction_commit();
2766 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
2768 /* At this point, as->dispatch and as->current_map are dummy
2769 * entries that the guest should never use. Wait for the old
2770 * values to expire before freeing the data.
2772 as->root = root;
2773 call_rcu(as, do_address_space_destroy, rcu);
2776 static const char *memory_region_type(MemoryRegion *mr)
2778 if (memory_region_is_ram_device(mr)) {
2779 return "ramd";
2780 } else if (memory_region_is_romd(mr)) {
2781 return "romd";
2782 } else if (memory_region_is_rom(mr)) {
2783 return "rom";
2784 } else if (memory_region_is_ram(mr)) {
2785 return "ram";
2786 } else {
2787 return "i/o";
2791 typedef struct MemoryRegionList MemoryRegionList;
2793 struct MemoryRegionList {
2794 const MemoryRegion *mr;
2795 QTAILQ_ENTRY(MemoryRegionList) mrqueue;
2798 typedef QTAILQ_HEAD(, MemoryRegionList) MemoryRegionListHead;
2800 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2801 int128_sub((size), int128_one())) : 0)
2802 #define MTREE_INDENT " "
2804 static void mtree_expand_owner(const char *label, Object *obj)
2806 DeviceState *dev = (DeviceState *) object_dynamic_cast(obj, TYPE_DEVICE);
2808 qemu_printf(" %s:{%s", label, dev ? "dev" : "obj");
2809 if (dev && dev->id) {
2810 qemu_printf(" id=%s", dev->id);
2811 } else {
2812 gchar *canonical_path = object_get_canonical_path(obj);
2813 if (canonical_path) {
2814 qemu_printf(" path=%s", canonical_path);
2815 g_free(canonical_path);
2816 } else {
2817 qemu_printf(" type=%s", object_get_typename(obj));
2820 qemu_printf("}");
2823 static void mtree_print_mr_owner(const MemoryRegion *mr)
2825 Object *owner = mr->owner;
2826 Object *parent = memory_region_owner((MemoryRegion *)mr);
2828 if (!owner && !parent) {
2829 qemu_printf(" orphan");
2830 return;
2832 if (owner) {
2833 mtree_expand_owner("owner", owner);
2835 if (parent && parent != owner) {
2836 mtree_expand_owner("parent", parent);
2840 static void mtree_print_mr(const MemoryRegion *mr, unsigned int level,
2841 hwaddr base,
2842 MemoryRegionListHead *alias_print_queue,
2843 bool owner)
2845 MemoryRegionList *new_ml, *ml, *next_ml;
2846 MemoryRegionListHead submr_print_queue;
2847 const MemoryRegion *submr;
2848 unsigned int i;
2849 hwaddr cur_start, cur_end;
2851 if (!mr) {
2852 return;
2855 for (i = 0; i < level; i++) {
2856 qemu_printf(MTREE_INDENT);
2859 cur_start = base + mr->addr;
2860 cur_end = cur_start + MR_SIZE(mr->size);
2863 * Try to detect overflow of memory region. This should never
2864 * happen normally. When it happens, we dump something to warn the
2865 * user who is observing this.
2867 if (cur_start < base || cur_end < cur_start) {
2868 qemu_printf("[DETECTED OVERFLOW!] ");
2871 if (mr->alias) {
2872 MemoryRegionList *ml;
2873 bool found = false;
2875 /* check if the alias is already in the queue */
2876 QTAILQ_FOREACH(ml, alias_print_queue, mrqueue) {
2877 if (ml->mr == mr->alias) {
2878 found = true;
2882 if (!found) {
2883 ml = g_new(MemoryRegionList, 1);
2884 ml->mr = mr->alias;
2885 QTAILQ_INSERT_TAIL(alias_print_queue, ml, mrqueue);
2887 qemu_printf(TARGET_FMT_plx "-" TARGET_FMT_plx
2888 " (prio %d, %s%s): alias %s @%s " TARGET_FMT_plx
2889 "-" TARGET_FMT_plx "%s",
2890 cur_start, cur_end,
2891 mr->priority,
2892 mr->nonvolatile ? "nv-" : "",
2893 memory_region_type((MemoryRegion *)mr),
2894 memory_region_name(mr),
2895 memory_region_name(mr->alias),
2896 mr->alias_offset,
2897 mr->alias_offset + MR_SIZE(mr->size),
2898 mr->enabled ? "" : " [disabled]");
2899 if (owner) {
2900 mtree_print_mr_owner(mr);
2902 } else {
2903 qemu_printf(TARGET_FMT_plx "-" TARGET_FMT_plx
2904 " (prio %d, %s%s): %s%s",
2905 cur_start, cur_end,
2906 mr->priority,
2907 mr->nonvolatile ? "nv-" : "",
2908 memory_region_type((MemoryRegion *)mr),
2909 memory_region_name(mr),
2910 mr->enabled ? "" : " [disabled]");
2911 if (owner) {
2912 mtree_print_mr_owner(mr);
2915 qemu_printf("\n");
2917 QTAILQ_INIT(&submr_print_queue);
2919 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
2920 new_ml = g_new(MemoryRegionList, 1);
2921 new_ml->mr = submr;
2922 QTAILQ_FOREACH(ml, &submr_print_queue, mrqueue) {
2923 if (new_ml->mr->addr < ml->mr->addr ||
2924 (new_ml->mr->addr == ml->mr->addr &&
2925 new_ml->mr->priority > ml->mr->priority)) {
2926 QTAILQ_INSERT_BEFORE(ml, new_ml, mrqueue);
2927 new_ml = NULL;
2928 break;
2931 if (new_ml) {
2932 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, mrqueue);
2936 QTAILQ_FOREACH(ml, &submr_print_queue, mrqueue) {
2937 mtree_print_mr(ml->mr, level + 1, cur_start,
2938 alias_print_queue, owner);
2941 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, mrqueue, next_ml) {
2942 g_free(ml);
2946 struct FlatViewInfo {
2947 int counter;
2948 bool dispatch_tree;
2949 bool owner;
2952 static void mtree_print_flatview(gpointer key, gpointer value,
2953 gpointer user_data)
2955 FlatView *view = key;
2956 GArray *fv_address_spaces = value;
2957 struct FlatViewInfo *fvi = user_data;
2958 FlatRange *range = &view->ranges[0];
2959 MemoryRegion *mr;
2960 int n = view->nr;
2961 int i;
2962 AddressSpace *as;
2964 qemu_printf("FlatView #%d\n", fvi->counter);
2965 ++fvi->counter;
2967 for (i = 0; i < fv_address_spaces->len; ++i) {
2968 as = g_array_index(fv_address_spaces, AddressSpace*, i);
2969 qemu_printf(" AS \"%s\", root: %s",
2970 as->name, memory_region_name(as->root));
2971 if (as->root->alias) {
2972 qemu_printf(", alias %s", memory_region_name(as->root->alias));
2974 qemu_printf("\n");
2977 qemu_printf(" Root memory region: %s\n",
2978 view->root ? memory_region_name(view->root) : "(none)");
2980 if (n <= 0) {
2981 qemu_printf(MTREE_INDENT "No rendered FlatView\n\n");
2982 return;
2985 while (n--) {
2986 mr = range->mr;
2987 if (range->offset_in_region) {
2988 qemu_printf(MTREE_INDENT TARGET_FMT_plx "-" TARGET_FMT_plx
2989 " (prio %d, %s%s): %s @" TARGET_FMT_plx,
2990 int128_get64(range->addr.start),
2991 int128_get64(range->addr.start)
2992 + MR_SIZE(range->addr.size),
2993 mr->priority,
2994 range->nonvolatile ? "nv-" : "",
2995 range->readonly ? "rom" : memory_region_type(mr),
2996 memory_region_name(mr),
2997 range->offset_in_region);
2998 } else {
2999 qemu_printf(MTREE_INDENT TARGET_FMT_plx "-" TARGET_FMT_plx
3000 " (prio %d, %s%s): %s",
3001 int128_get64(range->addr.start),
3002 int128_get64(range->addr.start)
3003 + MR_SIZE(range->addr.size),
3004 mr->priority,
3005 range->nonvolatile ? "nv-" : "",
3006 range->readonly ? "rom" : memory_region_type(mr),
3007 memory_region_name(mr));
3009 if (fvi->owner) {
3010 mtree_print_mr_owner(mr);
3012 qemu_printf("\n");
3013 range++;
3016 #if !defined(CONFIG_USER_ONLY)
3017 if (fvi->dispatch_tree && view->root) {
3018 mtree_print_dispatch(view->dispatch, view->root);
3020 #endif
3022 qemu_printf("\n");
3025 static gboolean mtree_info_flatview_free(gpointer key, gpointer value,
3026 gpointer user_data)
3028 FlatView *view = key;
3029 GArray *fv_address_spaces = value;
3031 g_array_unref(fv_address_spaces);
3032 flatview_unref(view);
3034 return true;
3037 void mtree_info(bool flatview, bool dispatch_tree, bool owner)
3039 MemoryRegionListHead ml_head;
3040 MemoryRegionList *ml, *ml2;
3041 AddressSpace *as;
3043 if (flatview) {
3044 FlatView *view;
3045 struct FlatViewInfo fvi = {
3046 .counter = 0,
3047 .dispatch_tree = dispatch_tree,
3048 .owner = owner,
3050 GArray *fv_address_spaces;
3051 GHashTable *views = g_hash_table_new(g_direct_hash, g_direct_equal);
3053 /* Gather all FVs in one table */
3054 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
3055 view = address_space_get_flatview(as);
3057 fv_address_spaces = g_hash_table_lookup(views, view);
3058 if (!fv_address_spaces) {
3059 fv_address_spaces = g_array_new(false, false, sizeof(as));
3060 g_hash_table_insert(views, view, fv_address_spaces);
3063 g_array_append_val(fv_address_spaces, as);
3066 /* Print */
3067 g_hash_table_foreach(views, mtree_print_flatview, &fvi);
3069 /* Free */
3070 g_hash_table_foreach_remove(views, mtree_info_flatview_free, 0);
3071 g_hash_table_unref(views);
3073 return;
3076 QTAILQ_INIT(&ml_head);
3078 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
3079 qemu_printf("address-space: %s\n", as->name);
3080 mtree_print_mr(as->root, 1, 0, &ml_head, owner);
3081 qemu_printf("\n");
3084 /* print aliased regions */
3085 QTAILQ_FOREACH(ml, &ml_head, mrqueue) {
3086 qemu_printf("memory-region: %s\n", memory_region_name(ml->mr));
3087 mtree_print_mr(ml->mr, 1, 0, &ml_head, owner);
3088 qemu_printf("\n");
3091 QTAILQ_FOREACH_SAFE(ml, &ml_head, mrqueue, ml2) {
3092 g_free(ml);
3096 void memory_region_init_ram(MemoryRegion *mr,
3097 struct Object *owner,
3098 const char *name,
3099 uint64_t size,
3100 Error **errp)
3102 DeviceState *owner_dev;
3103 Error *err = NULL;
3105 memory_region_init_ram_nomigrate(mr, owner, name, size, &err);
3106 if (err) {
3107 error_propagate(errp, err);
3108 return;
3110 /* This will assert if owner is neither NULL nor a DeviceState.
3111 * We only want the owner here for the purposes of defining a
3112 * unique name for migration. TODO: Ideally we should implement
3113 * a naming scheme for Objects which are not DeviceStates, in
3114 * which case we can relax this restriction.
3116 owner_dev = DEVICE(owner);
3117 vmstate_register_ram(mr, owner_dev);
3120 void memory_region_init_rom(MemoryRegion *mr,
3121 struct Object *owner,
3122 const char *name,
3123 uint64_t size,
3124 Error **errp)
3126 DeviceState *owner_dev;
3127 Error *err = NULL;
3129 memory_region_init_rom_nomigrate(mr, owner, name, size, &err);
3130 if (err) {
3131 error_propagate(errp, err);
3132 return;
3134 /* This will assert if owner is neither NULL nor a DeviceState.
3135 * We only want the owner here for the purposes of defining a
3136 * unique name for migration. TODO: Ideally we should implement
3137 * a naming scheme for Objects which are not DeviceStates, in
3138 * which case we can relax this restriction.
3140 owner_dev = DEVICE(owner);
3141 vmstate_register_ram(mr, owner_dev);
3144 void memory_region_init_rom_device(MemoryRegion *mr,
3145 struct Object *owner,
3146 const MemoryRegionOps *ops,
3147 void *opaque,
3148 const char *name,
3149 uint64_t size,
3150 Error **errp)
3152 DeviceState *owner_dev;
3153 Error *err = NULL;
3155 memory_region_init_rom_device_nomigrate(mr, owner, ops, opaque,
3156 name, size, &err);
3157 if (err) {
3158 error_propagate(errp, err);
3159 return;
3161 /* This will assert if owner is neither NULL nor a DeviceState.
3162 * We only want the owner here for the purposes of defining a
3163 * unique name for migration. TODO: Ideally we should implement
3164 * a naming scheme for Objects which are not DeviceStates, in
3165 * which case we can relax this restriction.
3167 owner_dev = DEVICE(owner);
3168 vmstate_register_ram(mr, owner_dev);
3171 static const TypeInfo memory_region_info = {
3172 .parent = TYPE_OBJECT,
3173 .name = TYPE_MEMORY_REGION,
3174 .instance_size = sizeof(MemoryRegion),
3175 .instance_init = memory_region_initfn,
3176 .instance_finalize = memory_region_finalize,
3179 static const TypeInfo iommu_memory_region_info = {
3180 .parent = TYPE_MEMORY_REGION,
3181 .name = TYPE_IOMMU_MEMORY_REGION,
3182 .class_size = sizeof(IOMMUMemoryRegionClass),
3183 .instance_size = sizeof(IOMMUMemoryRegion),
3184 .instance_init = iommu_memory_region_initfn,
3185 .abstract = true,
3188 static void memory_register_types(void)
3190 type_register_static(&memory_region_info);
3191 type_register_static(&iommu_memory_region_info);
3194 type_init(memory_register_types)