hw/isa/Kconfig: i82378 SuperIO requires PC speaker device
[qemu/ar7.git] / memory.c
blob9fbca52e05b4e66c2726ae5527ce99e7aae09a47
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 "qom/object.h"
26 #include "trace-root.h"
28 #include "exec/memory-internal.h"
29 #include "exec/ram_addr.h"
30 #include "sysemu/kvm.h"
31 #include "sysemu/sysemu.h"
32 #include "hw/qdev-properties.h"
33 #include "migration/vmstate.h"
35 //#define DEBUG_UNASSIGNED
37 static unsigned memory_region_transaction_depth;
38 static bool memory_region_update_pending;
39 static bool ioeventfd_update_pending;
40 static bool global_dirty_log = false;
42 static QTAILQ_HEAD(, MemoryListener) memory_listeners
43 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
45 static QTAILQ_HEAD(, AddressSpace) address_spaces
46 = QTAILQ_HEAD_INITIALIZER(address_spaces);
48 static GHashTable *flat_views;
50 typedef struct AddrRange AddrRange;
53 * Note that signed integers are needed for negative offsetting in aliases
54 * (large MemoryRegion::alias_offset).
56 struct AddrRange {
57 Int128 start;
58 Int128 size;
61 static AddrRange addrrange_make(Int128 start, Int128 size)
63 return (AddrRange) { start, size };
66 static bool addrrange_equal(AddrRange r1, AddrRange r2)
68 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
71 static Int128 addrrange_end(AddrRange r)
73 return int128_add(r.start, r.size);
76 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
78 int128_addto(&range.start, delta);
79 return range;
82 static bool addrrange_contains(AddrRange range, Int128 addr)
84 return int128_ge(addr, range.start)
85 && int128_lt(addr, addrrange_end(range));
88 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
90 return addrrange_contains(r1, r2.start)
91 || addrrange_contains(r2, r1.start);
94 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
96 Int128 start = int128_max(r1.start, r2.start);
97 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
98 return addrrange_make(start, int128_sub(end, start));
101 enum ListenerDirection { Forward, Reverse };
103 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
104 do { \
105 MemoryListener *_listener; \
107 switch (_direction) { \
108 case Forward: \
109 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
110 if (_listener->_callback) { \
111 _listener->_callback(_listener, ##_args); \
114 break; \
115 case Reverse: \
116 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, link) { \
117 if (_listener->_callback) { \
118 _listener->_callback(_listener, ##_args); \
121 break; \
122 default: \
123 abort(); \
125 } while (0)
127 #define MEMORY_LISTENER_CALL(_as, _callback, _direction, _section, _args...) \
128 do { \
129 MemoryListener *_listener; \
131 switch (_direction) { \
132 case Forward: \
133 QTAILQ_FOREACH(_listener, &(_as)->listeners, link_as) { \
134 if (_listener->_callback) { \
135 _listener->_callback(_listener, _section, ##_args); \
138 break; \
139 case Reverse: \
140 QTAILQ_FOREACH_REVERSE(_listener, &(_as)->listeners, link_as) { \
141 if (_listener->_callback) { \
142 _listener->_callback(_listener, _section, ##_args); \
145 break; \
146 default: \
147 abort(); \
149 } while (0)
151 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
152 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
153 do { \
154 MemoryRegionSection mrs = section_from_flat_range(fr, \
155 address_space_to_flatview(as)); \
156 MEMORY_LISTENER_CALL(as, callback, dir, &mrs, ##_args); \
157 } while(0)
159 struct CoalescedMemoryRange {
160 AddrRange addr;
161 QTAILQ_ENTRY(CoalescedMemoryRange) link;
164 struct MemoryRegionIoeventfd {
165 AddrRange addr;
166 bool match_data;
167 uint64_t data;
168 EventNotifier *e;
171 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd *a,
172 MemoryRegionIoeventfd *b)
174 if (int128_lt(a->addr.start, b->addr.start)) {
175 return true;
176 } else if (int128_gt(a->addr.start, b->addr.start)) {
177 return false;
178 } else if (int128_lt(a->addr.size, b->addr.size)) {
179 return true;
180 } else if (int128_gt(a->addr.size, b->addr.size)) {
181 return false;
182 } else if (a->match_data < b->match_data) {
183 return true;
184 } else if (a->match_data > b->match_data) {
185 return false;
186 } else if (a->match_data) {
187 if (a->data < b->data) {
188 return true;
189 } else if (a->data > b->data) {
190 return false;
193 if (a->e < b->e) {
194 return true;
195 } else if (a->e > b->e) {
196 return false;
198 return false;
201 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd *a,
202 MemoryRegionIoeventfd *b)
204 return !memory_region_ioeventfd_before(a, b)
205 && !memory_region_ioeventfd_before(b, a);
208 /* Range of memory in the global map. Addresses are absolute. */
209 struct FlatRange {
210 MemoryRegion *mr;
211 hwaddr offset_in_region;
212 AddrRange addr;
213 uint8_t dirty_log_mask;
214 bool romd_mode;
215 bool readonly;
216 bool nonvolatile;
217 int has_coalesced_range;
220 #define FOR_EACH_FLAT_RANGE(var, view) \
221 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
223 static inline MemoryRegionSection
224 section_from_flat_range(FlatRange *fr, FlatView *fv)
226 return (MemoryRegionSection) {
227 .mr = fr->mr,
228 .fv = fv,
229 .offset_within_region = fr->offset_in_region,
230 .size = fr->addr.size,
231 .offset_within_address_space = int128_get64(fr->addr.start),
232 .readonly = fr->readonly,
233 .nonvolatile = fr->nonvolatile,
237 static bool flatrange_equal(FlatRange *a, FlatRange *b)
239 return a->mr == b->mr
240 && addrrange_equal(a->addr, b->addr)
241 && a->offset_in_region == b->offset_in_region
242 && a->romd_mode == b->romd_mode
243 && a->readonly == b->readonly
244 && a->nonvolatile == b->nonvolatile;
247 static FlatView *flatview_new(MemoryRegion *mr_root)
249 FlatView *view;
251 view = g_new0(FlatView, 1);
252 view->ref = 1;
253 view->root = mr_root;
254 memory_region_ref(mr_root);
255 trace_flatview_new(view, mr_root);
257 return view;
260 /* Insert a range into a given position. Caller is responsible for maintaining
261 * sorting order.
263 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
265 if (view->nr == view->nr_allocated) {
266 view->nr_allocated = MAX(2 * view->nr, 10);
267 view->ranges = g_realloc(view->ranges,
268 view->nr_allocated * sizeof(*view->ranges));
270 memmove(view->ranges + pos + 1, view->ranges + pos,
271 (view->nr - pos) * sizeof(FlatRange));
272 view->ranges[pos] = *range;
273 memory_region_ref(range->mr);
274 ++view->nr;
277 static void flatview_destroy(FlatView *view)
279 int i;
281 trace_flatview_destroy(view, view->root);
282 if (view->dispatch) {
283 address_space_dispatch_free(view->dispatch);
285 for (i = 0; i < view->nr; i++) {
286 memory_region_unref(view->ranges[i].mr);
288 g_free(view->ranges);
289 memory_region_unref(view->root);
290 g_free(view);
293 static bool flatview_ref(FlatView *view)
295 return atomic_fetch_inc_nonzero(&view->ref) > 0;
298 void flatview_unref(FlatView *view)
300 if (atomic_fetch_dec(&view->ref) == 1) {
301 trace_flatview_destroy_rcu(view, view->root);
302 assert(view->root);
303 call_rcu(view, flatview_destroy, rcu);
307 static bool can_merge(FlatRange *r1, FlatRange *r2)
309 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
310 && r1->mr == r2->mr
311 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
312 r1->addr.size),
313 int128_make64(r2->offset_in_region))
314 && r1->dirty_log_mask == r2->dirty_log_mask
315 && r1->romd_mode == r2->romd_mode
316 && r1->readonly == r2->readonly
317 && r1->nonvolatile == r2->nonvolatile;
320 /* Attempt to simplify a view by merging adjacent ranges */
321 static void flatview_simplify(FlatView *view)
323 unsigned i, j;
325 i = 0;
326 while (i < view->nr) {
327 j = i + 1;
328 while (j < view->nr
329 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
330 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
331 ++j;
333 ++i;
334 memmove(&view->ranges[i], &view->ranges[j],
335 (view->nr - j) * sizeof(view->ranges[j]));
336 view->nr -= j - i;
340 static bool memory_region_big_endian(MemoryRegion *mr)
342 #ifdef TARGET_WORDS_BIGENDIAN
343 return mr->ops->endianness != DEVICE_LITTLE_ENDIAN;
344 #else
345 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
346 #endif
349 static bool memory_region_wrong_endianness(MemoryRegion *mr)
351 #ifdef TARGET_WORDS_BIGENDIAN
352 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
353 #else
354 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
355 #endif
358 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
360 if (memory_region_wrong_endianness(mr)) {
361 switch (size) {
362 case 1:
363 break;
364 case 2:
365 *data = bswap16(*data);
366 break;
367 case 4:
368 *data = bswap32(*data);
369 break;
370 case 8:
371 *data = bswap64(*data);
372 break;
373 default:
374 abort();
379 static inline void memory_region_shift_read_access(uint64_t *value,
380 signed shift,
381 uint64_t mask,
382 uint64_t tmp)
384 if (shift >= 0) {
385 *value |= (tmp & mask) << shift;
386 } else {
387 *value |= (tmp & mask) >> -shift;
391 static inline uint64_t memory_region_shift_write_access(uint64_t *value,
392 signed shift,
393 uint64_t mask)
395 uint64_t tmp;
397 if (shift >= 0) {
398 tmp = (*value >> shift) & mask;
399 } else {
400 tmp = (*value << -shift) & mask;
403 return tmp;
406 static hwaddr memory_region_to_absolute_addr(MemoryRegion *mr, hwaddr offset)
408 MemoryRegion *root;
409 hwaddr abs_addr = offset;
411 abs_addr += mr->addr;
412 for (root = mr; root->container; ) {
413 root = root->container;
414 abs_addr += root->addr;
417 return abs_addr;
420 static int get_cpu_index(void)
422 if (current_cpu) {
423 return current_cpu->cpu_index;
425 return -1;
428 static MemTxResult memory_region_read_accessor(MemoryRegion *mr,
429 hwaddr addr,
430 uint64_t *value,
431 unsigned size,
432 signed shift,
433 uint64_t mask,
434 MemTxAttrs attrs)
436 uint64_t tmp;
438 tmp = mr->ops->read(mr->opaque, addr, size);
439 if (mr->subpage) {
440 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
441 } else if (mr == &io_mem_notdirty) {
442 /* Accesses to code which has previously been translated into a TB show
443 * up in the MMIO path, as accesses to the io_mem_notdirty
444 * MemoryRegion. */
445 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
446 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
447 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
448 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
450 memory_region_shift_read_access(value, shift, mask, tmp);
451 return MEMTX_OK;
454 static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr,
455 hwaddr addr,
456 uint64_t *value,
457 unsigned size,
458 signed shift,
459 uint64_t mask,
460 MemTxAttrs attrs)
462 uint64_t tmp = 0;
463 MemTxResult r;
465 r = mr->ops->read_with_attrs(mr->opaque, addr, &tmp, size, attrs);
466 if (mr->subpage) {
467 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
468 } else if (mr == &io_mem_notdirty) {
469 /* Accesses to code which has previously been translated into a TB show
470 * up in the MMIO path, as accesses to the io_mem_notdirty
471 * MemoryRegion. */
472 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
473 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
474 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
475 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
477 memory_region_shift_read_access(value, shift, mask, tmp);
478 return r;
481 static MemTxResult memory_region_write_accessor(MemoryRegion *mr,
482 hwaddr addr,
483 uint64_t *value,
484 unsigned size,
485 signed shift,
486 uint64_t mask,
487 MemTxAttrs attrs)
489 uint64_t tmp = memory_region_shift_write_access(value, shift, mask);
491 if (mr->subpage) {
492 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
493 } else if (mr == &io_mem_notdirty) {
494 /* Accesses to code which has previously been translated into a TB show
495 * up in the MMIO path, as accesses to the io_mem_notdirty
496 * MemoryRegion. */
497 trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
498 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
499 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
500 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
502 mr->ops->write(mr->opaque, addr, tmp, size);
503 return MEMTX_OK;
506 static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr,
507 hwaddr addr,
508 uint64_t *value,
509 unsigned size,
510 signed shift,
511 uint64_t mask,
512 MemTxAttrs attrs)
514 uint64_t tmp = memory_region_shift_write_access(value, shift, mask);
516 if (mr->subpage) {
517 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
518 } else if (mr == &io_mem_notdirty) {
519 /* Accesses to code which has previously been translated into a TB show
520 * up in the MMIO path, as accesses to the io_mem_notdirty
521 * MemoryRegion. */
522 trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
523 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
524 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
525 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
527 return mr->ops->write_with_attrs(mr->opaque, addr, tmp, size, attrs);
530 static MemTxResult access_with_adjusted_size(hwaddr addr,
531 uint64_t *value,
532 unsigned size,
533 unsigned access_size_min,
534 unsigned access_size_max,
535 MemTxResult (*access_fn)
536 (MemoryRegion *mr,
537 hwaddr addr,
538 uint64_t *value,
539 unsigned size,
540 signed shift,
541 uint64_t mask,
542 MemTxAttrs attrs),
543 MemoryRegion *mr,
544 MemTxAttrs attrs)
546 uint64_t access_mask;
547 unsigned access_size;
548 unsigned i;
549 MemTxResult r = MEMTX_OK;
551 if (!access_size_min) {
552 access_size_min = 1;
554 if (!access_size_max) {
555 access_size_max = 4;
558 /* FIXME: support unaligned access? */
559 access_size = MAX(MIN(size, access_size_max), access_size_min);
560 access_mask = MAKE_64BIT_MASK(0, access_size * 8);
561 if (memory_region_big_endian(mr)) {
562 for (i = 0; i < size; i += access_size) {
563 r |= access_fn(mr, addr + i, value, access_size,
564 (size - access_size - i) * 8, access_mask, attrs);
566 } else {
567 for (i = 0; i < size; i += access_size) {
568 r |= access_fn(mr, addr + i, value, access_size, i * 8,
569 access_mask, attrs);
572 return r;
575 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
577 AddressSpace *as;
579 while (mr->container) {
580 mr = mr->container;
582 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
583 if (mr == as->root) {
584 return as;
587 return NULL;
590 /* Render a memory region into the global view. Ranges in @view obscure
591 * ranges in @mr.
593 static void render_memory_region(FlatView *view,
594 MemoryRegion *mr,
595 Int128 base,
596 AddrRange clip,
597 bool readonly,
598 bool nonvolatile)
600 MemoryRegion *subregion;
601 unsigned i;
602 hwaddr offset_in_region;
603 Int128 remain;
604 Int128 now;
605 FlatRange fr;
606 AddrRange tmp;
608 if (!mr->enabled) {
609 return;
612 int128_addto(&base, int128_make64(mr->addr));
613 readonly |= mr->readonly;
614 nonvolatile |= mr->nonvolatile;
616 tmp = addrrange_make(base, mr->size);
618 if (!addrrange_intersects(tmp, clip)) {
619 return;
622 clip = addrrange_intersection(tmp, clip);
624 if (mr->alias) {
625 int128_subfrom(&base, int128_make64(mr->alias->addr));
626 int128_subfrom(&base, int128_make64(mr->alias_offset));
627 render_memory_region(view, mr->alias, base, clip,
628 readonly, nonvolatile);
629 return;
632 /* Render subregions in priority order. */
633 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
634 render_memory_region(view, subregion, base, clip,
635 readonly, nonvolatile);
638 if (!mr->terminates) {
639 return;
642 offset_in_region = int128_get64(int128_sub(clip.start, base));
643 base = clip.start;
644 remain = clip.size;
646 fr.mr = mr;
647 fr.dirty_log_mask = memory_region_get_dirty_log_mask(mr);
648 fr.romd_mode = mr->romd_mode;
649 fr.readonly = readonly;
650 fr.nonvolatile = nonvolatile;
651 fr.has_coalesced_range = 0;
653 /* Render the region itself into any gaps left by the current view. */
654 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
655 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
656 continue;
658 if (int128_lt(base, view->ranges[i].addr.start)) {
659 now = int128_min(remain,
660 int128_sub(view->ranges[i].addr.start, base));
661 fr.offset_in_region = offset_in_region;
662 fr.addr = addrrange_make(base, now);
663 flatview_insert(view, i, &fr);
664 ++i;
665 int128_addto(&base, now);
666 offset_in_region += int128_get64(now);
667 int128_subfrom(&remain, now);
669 now = int128_sub(int128_min(int128_add(base, remain),
670 addrrange_end(view->ranges[i].addr)),
671 base);
672 int128_addto(&base, now);
673 offset_in_region += int128_get64(now);
674 int128_subfrom(&remain, now);
676 if (int128_nz(remain)) {
677 fr.offset_in_region = offset_in_region;
678 fr.addr = addrrange_make(base, remain);
679 flatview_insert(view, i, &fr);
683 static MemoryRegion *memory_region_get_flatview_root(MemoryRegion *mr)
685 while (mr->enabled) {
686 if (mr->alias) {
687 if (!mr->alias_offset && int128_ge(mr->size, mr->alias->size)) {
688 /* The alias is included in its entirety. Use it as
689 * the "real" root, so that we can share more FlatViews.
691 mr = mr->alias;
692 continue;
694 } else if (!mr->terminates) {
695 unsigned int found = 0;
696 MemoryRegion *child, *next = NULL;
697 QTAILQ_FOREACH(child, &mr->subregions, subregions_link) {
698 if (child->enabled) {
699 if (++found > 1) {
700 next = NULL;
701 break;
703 if (!child->addr && int128_ge(mr->size, child->size)) {
704 /* A child is included in its entirety. If it's the only
705 * enabled one, use it in the hope of finding an alias down the
706 * way. This will also let us share FlatViews.
708 next = child;
712 if (found == 0) {
713 return NULL;
715 if (next) {
716 mr = next;
717 continue;
721 return mr;
724 return NULL;
727 /* Render a memory topology into a list of disjoint absolute ranges. */
728 static FlatView *generate_memory_topology(MemoryRegion *mr)
730 int i;
731 FlatView *view;
733 view = flatview_new(mr);
735 if (mr) {
736 render_memory_region(view, mr, int128_zero(),
737 addrrange_make(int128_zero(), int128_2_64()),
738 false, false);
740 flatview_simplify(view);
742 view->dispatch = address_space_dispatch_new(view);
743 for (i = 0; i < view->nr; i++) {
744 MemoryRegionSection mrs =
745 section_from_flat_range(&view->ranges[i], view);
746 flatview_add_to_dispatch(view, &mrs);
748 address_space_dispatch_compact(view->dispatch);
749 g_hash_table_replace(flat_views, mr, view);
751 return view;
754 static void address_space_add_del_ioeventfds(AddressSpace *as,
755 MemoryRegionIoeventfd *fds_new,
756 unsigned fds_new_nb,
757 MemoryRegionIoeventfd *fds_old,
758 unsigned fds_old_nb)
760 unsigned iold, inew;
761 MemoryRegionIoeventfd *fd;
762 MemoryRegionSection section;
764 /* Generate a symmetric difference of the old and new fd sets, adding
765 * and deleting as necessary.
768 iold = inew = 0;
769 while (iold < fds_old_nb || inew < fds_new_nb) {
770 if (iold < fds_old_nb
771 && (inew == fds_new_nb
772 || memory_region_ioeventfd_before(&fds_old[iold],
773 &fds_new[inew]))) {
774 fd = &fds_old[iold];
775 section = (MemoryRegionSection) {
776 .fv = address_space_to_flatview(as),
777 .offset_within_address_space = int128_get64(fd->addr.start),
778 .size = fd->addr.size,
780 MEMORY_LISTENER_CALL(as, eventfd_del, Forward, &section,
781 fd->match_data, fd->data, fd->e);
782 ++iold;
783 } else if (inew < fds_new_nb
784 && (iold == fds_old_nb
785 || memory_region_ioeventfd_before(&fds_new[inew],
786 &fds_old[iold]))) {
787 fd = &fds_new[inew];
788 section = (MemoryRegionSection) {
789 .fv = address_space_to_flatview(as),
790 .offset_within_address_space = int128_get64(fd->addr.start),
791 .size = fd->addr.size,
793 MEMORY_LISTENER_CALL(as, eventfd_add, Reverse, &section,
794 fd->match_data, fd->data, fd->e);
795 ++inew;
796 } else {
797 ++iold;
798 ++inew;
803 FlatView *address_space_get_flatview(AddressSpace *as)
805 FlatView *view;
807 rcu_read_lock();
808 do {
809 view = address_space_to_flatview(as);
810 /* If somebody has replaced as->current_map concurrently,
811 * flatview_ref returns false.
813 } while (!flatview_ref(view));
814 rcu_read_unlock();
815 return view;
818 static void address_space_update_ioeventfds(AddressSpace *as)
820 FlatView *view;
821 FlatRange *fr;
822 unsigned ioeventfd_nb = 0;
823 MemoryRegionIoeventfd *ioeventfds = NULL;
824 AddrRange tmp;
825 unsigned i;
827 view = address_space_get_flatview(as);
828 FOR_EACH_FLAT_RANGE(fr, view) {
829 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
830 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
831 int128_sub(fr->addr.start,
832 int128_make64(fr->offset_in_region)));
833 if (addrrange_intersects(fr->addr, tmp)) {
834 ++ioeventfd_nb;
835 ioeventfds = g_realloc(ioeventfds,
836 ioeventfd_nb * sizeof(*ioeventfds));
837 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
838 ioeventfds[ioeventfd_nb-1].addr = tmp;
843 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
844 as->ioeventfds, as->ioeventfd_nb);
846 g_free(as->ioeventfds);
847 as->ioeventfds = ioeventfds;
848 as->ioeventfd_nb = ioeventfd_nb;
849 flatview_unref(view);
852 static void flat_range_coalesced_io_del(FlatRange *fr, AddressSpace *as)
854 if (!fr->has_coalesced_range) {
855 return;
858 if (--fr->has_coalesced_range > 0) {
859 return;
862 MEMORY_LISTENER_UPDATE_REGION(fr, as, Reverse, coalesced_io_del,
863 int128_get64(fr->addr.start),
864 int128_get64(fr->addr.size));
867 static void flat_range_coalesced_io_add(FlatRange *fr, AddressSpace *as)
869 MemoryRegion *mr = fr->mr;
870 CoalescedMemoryRange *cmr;
871 AddrRange tmp;
873 if (QTAILQ_EMPTY(&mr->coalesced)) {
874 return;
877 if (fr->has_coalesced_range++) {
878 return;
881 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
882 tmp = addrrange_shift(cmr->addr,
883 int128_sub(fr->addr.start,
884 int128_make64(fr->offset_in_region)));
885 if (!addrrange_intersects(tmp, fr->addr)) {
886 continue;
888 tmp = addrrange_intersection(tmp, fr->addr);
889 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, coalesced_io_add,
890 int128_get64(tmp.start),
891 int128_get64(tmp.size));
895 static void address_space_update_topology_pass(AddressSpace *as,
896 const FlatView *old_view,
897 const FlatView *new_view,
898 bool adding)
900 unsigned iold, inew;
901 FlatRange *frold, *frnew;
903 /* Generate a symmetric difference of the old and new memory maps.
904 * Kill ranges in the old map, and instantiate ranges in the new map.
906 iold = inew = 0;
907 while (iold < old_view->nr || inew < new_view->nr) {
908 if (iold < old_view->nr) {
909 frold = &old_view->ranges[iold];
910 } else {
911 frold = NULL;
913 if (inew < new_view->nr) {
914 frnew = &new_view->ranges[inew];
915 } else {
916 frnew = NULL;
919 if (frold
920 && (!frnew
921 || int128_lt(frold->addr.start, frnew->addr.start)
922 || (int128_eq(frold->addr.start, frnew->addr.start)
923 && !flatrange_equal(frold, frnew)))) {
924 /* In old but not in new, or in both but attributes changed. */
926 if (!adding) {
927 flat_range_coalesced_io_del(frold, as);
928 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
931 ++iold;
932 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
933 /* In both and unchanged (except logging may have changed) */
935 if (adding) {
936 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
937 if (frnew->dirty_log_mask & ~frold->dirty_log_mask) {
938 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start,
939 frold->dirty_log_mask,
940 frnew->dirty_log_mask);
942 if (frold->dirty_log_mask & ~frnew->dirty_log_mask) {
943 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop,
944 frold->dirty_log_mask,
945 frnew->dirty_log_mask);
949 ++iold;
950 ++inew;
951 } else {
952 /* In new */
954 if (adding) {
955 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
956 flat_range_coalesced_io_add(frnew, as);
959 ++inew;
964 static void flatviews_init(void)
966 static FlatView *empty_view;
968 if (flat_views) {
969 return;
972 flat_views = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
973 (GDestroyNotify) flatview_unref);
974 if (!empty_view) {
975 empty_view = generate_memory_topology(NULL);
976 /* We keep it alive forever in the global variable. */
977 flatview_ref(empty_view);
978 } else {
979 g_hash_table_replace(flat_views, NULL, empty_view);
980 flatview_ref(empty_view);
984 static void flatviews_reset(void)
986 AddressSpace *as;
988 if (flat_views) {
989 g_hash_table_unref(flat_views);
990 flat_views = NULL;
992 flatviews_init();
994 /* Render unique FVs */
995 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
996 MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
998 if (g_hash_table_lookup(flat_views, physmr)) {
999 continue;
1002 generate_memory_topology(physmr);
1006 static void address_space_set_flatview(AddressSpace *as)
1008 FlatView *old_view = address_space_to_flatview(as);
1009 MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
1010 FlatView *new_view = g_hash_table_lookup(flat_views, physmr);
1012 assert(new_view);
1014 if (old_view == new_view) {
1015 return;
1018 if (old_view) {
1019 flatview_ref(old_view);
1022 flatview_ref(new_view);
1024 if (!QTAILQ_EMPTY(&as->listeners)) {
1025 FlatView tmpview = { .nr = 0 }, *old_view2 = old_view;
1027 if (!old_view2) {
1028 old_view2 = &tmpview;
1030 address_space_update_topology_pass(as, old_view2, new_view, false);
1031 address_space_update_topology_pass(as, old_view2, new_view, true);
1034 /* Writes are protected by the BQL. */
1035 atomic_rcu_set(&as->current_map, new_view);
1036 if (old_view) {
1037 flatview_unref(old_view);
1040 /* Note that all the old MemoryRegions are still alive up to this
1041 * point. This relieves most MemoryListeners from the need to
1042 * ref/unref the MemoryRegions they get---unless they use them
1043 * outside the iothread mutex, in which case precise reference
1044 * counting is necessary.
1046 if (old_view) {
1047 flatview_unref(old_view);
1051 static void address_space_update_topology(AddressSpace *as)
1053 MemoryRegion *physmr = memory_region_get_flatview_root(as->root);
1055 flatviews_init();
1056 if (!g_hash_table_lookup(flat_views, physmr)) {
1057 generate_memory_topology(physmr);
1059 address_space_set_flatview(as);
1062 void memory_region_transaction_begin(void)
1064 qemu_flush_coalesced_mmio_buffer();
1065 ++memory_region_transaction_depth;
1068 void memory_region_transaction_commit(void)
1070 AddressSpace *as;
1072 assert(memory_region_transaction_depth);
1073 assert(qemu_mutex_iothread_locked());
1075 --memory_region_transaction_depth;
1076 if (!memory_region_transaction_depth) {
1077 if (memory_region_update_pending) {
1078 flatviews_reset();
1080 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
1082 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1083 address_space_set_flatview(as);
1084 address_space_update_ioeventfds(as);
1086 memory_region_update_pending = false;
1087 ioeventfd_update_pending = false;
1088 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
1089 } else if (ioeventfd_update_pending) {
1090 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1091 address_space_update_ioeventfds(as);
1093 ioeventfd_update_pending = false;
1098 static void memory_region_destructor_none(MemoryRegion *mr)
1102 static void memory_region_destructor_ram(MemoryRegion *mr)
1104 qemu_ram_free(mr->ram_block);
1107 static bool memory_region_need_escape(char c)
1109 return c == '/' || c == '[' || c == '\\' || c == ']';
1112 static char *memory_region_escape_name(const char *name)
1114 const char *p;
1115 char *escaped, *q;
1116 uint8_t c;
1117 size_t bytes = 0;
1119 for (p = name; *p; p++) {
1120 bytes += memory_region_need_escape(*p) ? 4 : 1;
1122 if (bytes == p - name) {
1123 return g_memdup(name, bytes + 1);
1126 escaped = g_malloc(bytes + 1);
1127 for (p = name, q = escaped; *p; p++) {
1128 c = *p;
1129 if (unlikely(memory_region_need_escape(c))) {
1130 *q++ = '\\';
1131 *q++ = 'x';
1132 *q++ = "0123456789abcdef"[c >> 4];
1133 c = "0123456789abcdef"[c & 15];
1135 *q++ = c;
1137 *q = 0;
1138 return escaped;
1141 static void memory_region_do_init(MemoryRegion *mr,
1142 Object *owner,
1143 const char *name,
1144 uint64_t size)
1146 mr->size = int128_make64(size);
1147 if (size == UINT64_MAX) {
1148 mr->size = int128_2_64();
1150 mr->name = g_strdup(name);
1151 mr->owner = owner;
1152 mr->ram_block = NULL;
1154 if (name) {
1155 char *escaped_name = memory_region_escape_name(name);
1156 char *name_array = g_strdup_printf("%s[*]", escaped_name);
1158 if (!owner) {
1159 owner = container_get(qdev_get_machine(), "/unattached");
1162 object_property_add_child(owner, name_array, OBJECT(mr), &error_abort);
1163 object_unref(OBJECT(mr));
1164 g_free(name_array);
1165 g_free(escaped_name);
1169 void memory_region_init(MemoryRegion *mr,
1170 Object *owner,
1171 const char *name,
1172 uint64_t size)
1174 object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION);
1175 memory_region_do_init(mr, owner, name, size);
1178 static void memory_region_get_addr(Object *obj, Visitor *v, const char *name,
1179 void *opaque, Error **errp)
1181 MemoryRegion *mr = MEMORY_REGION(obj);
1182 uint64_t value = mr->addr;
1184 visit_type_uint64(v, name, &value, errp);
1187 static void memory_region_get_container(Object *obj, Visitor *v,
1188 const char *name, void *opaque,
1189 Error **errp)
1191 MemoryRegion *mr = MEMORY_REGION(obj);
1192 gchar *path = (gchar *)"";
1194 if (mr->container) {
1195 path = object_get_canonical_path(OBJECT(mr->container));
1197 visit_type_str(v, name, &path, errp);
1198 if (mr->container) {
1199 g_free(path);
1203 static Object *memory_region_resolve_container(Object *obj, void *opaque,
1204 const char *part)
1206 MemoryRegion *mr = MEMORY_REGION(obj);
1208 return OBJECT(mr->container);
1211 static void memory_region_get_priority(Object *obj, Visitor *v,
1212 const char *name, void *opaque,
1213 Error **errp)
1215 MemoryRegion *mr = MEMORY_REGION(obj);
1216 int32_t value = mr->priority;
1218 visit_type_int32(v, name, &value, errp);
1221 static void memory_region_get_size(Object *obj, Visitor *v, const char *name,
1222 void *opaque, Error **errp)
1224 MemoryRegion *mr = MEMORY_REGION(obj);
1225 uint64_t value = memory_region_size(mr);
1227 visit_type_uint64(v, name, &value, errp);
1230 static void memory_region_initfn(Object *obj)
1232 MemoryRegion *mr = MEMORY_REGION(obj);
1233 ObjectProperty *op;
1235 mr->ops = &unassigned_mem_ops;
1236 mr->enabled = true;
1237 mr->romd_mode = true;
1238 mr->global_locking = true;
1239 mr->destructor = memory_region_destructor_none;
1240 QTAILQ_INIT(&mr->subregions);
1241 QTAILQ_INIT(&mr->coalesced);
1243 op = object_property_add(OBJECT(mr), "container",
1244 "link<" TYPE_MEMORY_REGION ">",
1245 memory_region_get_container,
1246 NULL, /* memory_region_set_container */
1247 NULL, NULL, &error_abort);
1248 op->resolve = memory_region_resolve_container;
1250 object_property_add(OBJECT(mr), "addr", "uint64",
1251 memory_region_get_addr,
1252 NULL, /* memory_region_set_addr */
1253 NULL, NULL, &error_abort);
1254 object_property_add(OBJECT(mr), "priority", "uint32",
1255 memory_region_get_priority,
1256 NULL, /* memory_region_set_priority */
1257 NULL, NULL, &error_abort);
1258 object_property_add(OBJECT(mr), "size", "uint64",
1259 memory_region_get_size,
1260 NULL, /* memory_region_set_size, */
1261 NULL, NULL, &error_abort);
1264 static void iommu_memory_region_initfn(Object *obj)
1266 MemoryRegion *mr = MEMORY_REGION(obj);
1268 mr->is_iommu = true;
1271 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
1272 unsigned size)
1274 #ifdef DEBUG_UNASSIGNED
1275 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
1276 #endif
1277 if (current_cpu != NULL) {
1278 bool is_exec = current_cpu->mem_io_access_type == MMU_INST_FETCH;
1279 cpu_unassigned_access(current_cpu, addr, false, is_exec, 0, size);
1281 return 0;
1284 static void unassigned_mem_write(void *opaque, hwaddr addr,
1285 uint64_t val, unsigned size)
1287 #ifdef DEBUG_UNASSIGNED
1288 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
1289 #endif
1290 if (current_cpu != NULL) {
1291 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
1295 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
1296 unsigned size, bool is_write,
1297 MemTxAttrs attrs)
1299 return false;
1302 const MemoryRegionOps unassigned_mem_ops = {
1303 .valid.accepts = unassigned_mem_accepts,
1304 .endianness = DEVICE_NATIVE_ENDIAN,
1307 static uint64_t memory_region_ram_device_read(void *opaque,
1308 hwaddr addr, unsigned size)
1310 MemoryRegion *mr = opaque;
1311 uint64_t data = (uint64_t)~0;
1313 switch (size) {
1314 case 1:
1315 data = *(uint8_t *)(mr->ram_block->host + addr);
1316 break;
1317 case 2:
1318 data = *(uint16_t *)(mr->ram_block->host + addr);
1319 break;
1320 case 4:
1321 data = *(uint32_t *)(mr->ram_block->host + addr);
1322 break;
1323 case 8:
1324 data = *(uint64_t *)(mr->ram_block->host + addr);
1325 break;
1328 trace_memory_region_ram_device_read(get_cpu_index(), mr, addr, data, size);
1330 return data;
1333 static void memory_region_ram_device_write(void *opaque, hwaddr addr,
1334 uint64_t data, unsigned size)
1336 MemoryRegion *mr = opaque;
1338 trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size);
1340 switch (size) {
1341 case 1:
1342 *(uint8_t *)(mr->ram_block->host + addr) = (uint8_t)data;
1343 break;
1344 case 2:
1345 *(uint16_t *)(mr->ram_block->host + addr) = (uint16_t)data;
1346 break;
1347 case 4:
1348 *(uint32_t *)(mr->ram_block->host + addr) = (uint32_t)data;
1349 break;
1350 case 8:
1351 *(uint64_t *)(mr->ram_block->host + addr) = data;
1352 break;
1356 static const MemoryRegionOps ram_device_mem_ops = {
1357 .read = memory_region_ram_device_read,
1358 .write = memory_region_ram_device_write,
1359 .endianness = DEVICE_HOST_ENDIAN,
1360 .valid = {
1361 .min_access_size = 1,
1362 .max_access_size = 8,
1363 .unaligned = true,
1365 .impl = {
1366 .min_access_size = 1,
1367 .max_access_size = 8,
1368 .unaligned = true,
1372 bool memory_region_access_valid(MemoryRegion *mr,
1373 hwaddr addr,
1374 unsigned size,
1375 bool is_write,
1376 MemTxAttrs attrs)
1378 int access_size_min, access_size_max;
1379 int access_size, i;
1381 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
1382 return false;
1385 if (!mr->ops->valid.accepts) {
1386 return true;
1389 access_size_min = mr->ops->valid.min_access_size;
1390 if (!mr->ops->valid.min_access_size) {
1391 access_size_min = 1;
1394 access_size_max = mr->ops->valid.max_access_size;
1395 if (!mr->ops->valid.max_access_size) {
1396 access_size_max = 4;
1399 access_size = MAX(MIN(size, access_size_max), access_size_min);
1400 for (i = 0; i < size; i += access_size) {
1401 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
1402 is_write, attrs)) {
1403 return false;
1407 return true;
1410 static MemTxResult memory_region_dispatch_read1(MemoryRegion *mr,
1411 hwaddr addr,
1412 uint64_t *pval,
1413 unsigned size,
1414 MemTxAttrs attrs)
1416 *pval = 0;
1418 if (mr->ops->read) {
1419 return access_with_adjusted_size(addr, pval, size,
1420 mr->ops->impl.min_access_size,
1421 mr->ops->impl.max_access_size,
1422 memory_region_read_accessor,
1423 mr, attrs);
1424 } else {
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_with_attrs_accessor,
1429 mr, attrs);
1433 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1434 hwaddr addr,
1435 uint64_t *pval,
1436 unsigned size,
1437 MemTxAttrs attrs)
1439 MemTxResult r;
1441 if (!memory_region_access_valid(mr, addr, size, false, attrs)) {
1442 *pval = unassigned_mem_read(mr, addr, size);
1443 return MEMTX_DECODE_ERROR;
1446 r = memory_region_dispatch_read1(mr, addr, pval, size, attrs);
1447 adjust_endianness(mr, pval, size);
1448 return r;
1451 /* Return true if an eventfd was signalled */
1452 static bool memory_region_dispatch_write_eventfds(MemoryRegion *mr,
1453 hwaddr addr,
1454 uint64_t data,
1455 unsigned size,
1456 MemTxAttrs attrs)
1458 MemoryRegionIoeventfd ioeventfd = {
1459 .addr = addrrange_make(int128_make64(addr), int128_make64(size)),
1460 .data = data,
1462 unsigned i;
1464 for (i = 0; i < mr->ioeventfd_nb; i++) {
1465 ioeventfd.match_data = mr->ioeventfds[i].match_data;
1466 ioeventfd.e = mr->ioeventfds[i].e;
1468 if (memory_region_ioeventfd_equal(&ioeventfd, &mr->ioeventfds[i])) {
1469 event_notifier_set(ioeventfd.e);
1470 return true;
1474 return false;
1477 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1478 hwaddr addr,
1479 uint64_t data,
1480 unsigned size,
1481 MemTxAttrs attrs)
1483 if (!memory_region_access_valid(mr, addr, size, true, attrs)) {
1484 unassigned_mem_write(mr, addr, data, size);
1485 return MEMTX_DECODE_ERROR;
1488 adjust_endianness(mr, &data, size);
1490 if ((!kvm_eventfds_enabled()) &&
1491 memory_region_dispatch_write_eventfds(mr, addr, data, size, attrs)) {
1492 return MEMTX_OK;
1495 if (mr->ops->write) {
1496 return access_with_adjusted_size(addr, &data, size,
1497 mr->ops->impl.min_access_size,
1498 mr->ops->impl.max_access_size,
1499 memory_region_write_accessor, mr,
1500 attrs);
1501 } else {
1502 return
1503 access_with_adjusted_size(addr, &data, size,
1504 mr->ops->impl.min_access_size,
1505 mr->ops->impl.max_access_size,
1506 memory_region_write_with_attrs_accessor,
1507 mr, attrs);
1511 void memory_region_init_io(MemoryRegion *mr,
1512 Object *owner,
1513 const MemoryRegionOps *ops,
1514 void *opaque,
1515 const char *name,
1516 uint64_t size)
1518 memory_region_init(mr, owner, name, size);
1519 mr->ops = ops ? ops : &unassigned_mem_ops;
1520 mr->opaque = opaque;
1521 mr->terminates = true;
1524 void memory_region_init_ram_nomigrate(MemoryRegion *mr,
1525 Object *owner,
1526 const char *name,
1527 uint64_t size,
1528 Error **errp)
1530 memory_region_init_ram_shared_nomigrate(mr, owner, name, size, false, errp);
1533 void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
1534 Object *owner,
1535 const char *name,
1536 uint64_t size,
1537 bool share,
1538 Error **errp)
1540 Error *err = NULL;
1541 memory_region_init(mr, owner, name, size);
1542 mr->ram = true;
1543 mr->terminates = true;
1544 mr->destructor = memory_region_destructor_ram;
1545 mr->ram_block = qemu_ram_alloc(size, share, mr, &err);
1546 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1547 if (err) {
1548 mr->size = int128_zero();
1549 object_unparent(OBJECT(mr));
1550 error_propagate(errp, err);
1554 void memory_region_init_resizeable_ram(MemoryRegion *mr,
1555 Object *owner,
1556 const char *name,
1557 uint64_t size,
1558 uint64_t max_size,
1559 void (*resized)(const char*,
1560 uint64_t length,
1561 void *host),
1562 Error **errp)
1564 Error *err = NULL;
1565 memory_region_init(mr, owner, name, size);
1566 mr->ram = true;
1567 mr->terminates = true;
1568 mr->destructor = memory_region_destructor_ram;
1569 mr->ram_block = qemu_ram_alloc_resizeable(size, max_size, resized,
1570 mr, &err);
1571 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1572 if (err) {
1573 mr->size = int128_zero();
1574 object_unparent(OBJECT(mr));
1575 error_propagate(errp, err);
1579 #ifdef CONFIG_POSIX
1580 void memory_region_init_ram_from_file(MemoryRegion *mr,
1581 struct Object *owner,
1582 const char *name,
1583 uint64_t size,
1584 uint64_t align,
1585 uint32_t ram_flags,
1586 const char *path,
1587 Error **errp)
1589 Error *err = NULL;
1590 memory_region_init(mr, owner, name, size);
1591 mr->ram = true;
1592 mr->terminates = true;
1593 mr->destructor = memory_region_destructor_ram;
1594 mr->align = align;
1595 mr->ram_block = qemu_ram_alloc_from_file(size, mr, ram_flags, path, &err);
1596 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1597 if (err) {
1598 mr->size = int128_zero();
1599 object_unparent(OBJECT(mr));
1600 error_propagate(errp, err);
1604 void memory_region_init_ram_from_fd(MemoryRegion *mr,
1605 struct Object *owner,
1606 const char *name,
1607 uint64_t size,
1608 bool share,
1609 int fd,
1610 Error **errp)
1612 Error *err = NULL;
1613 memory_region_init(mr, owner, name, size);
1614 mr->ram = true;
1615 mr->terminates = true;
1616 mr->destructor = memory_region_destructor_ram;
1617 mr->ram_block = qemu_ram_alloc_from_fd(size, mr,
1618 share ? RAM_SHARED : 0,
1619 fd, &err);
1620 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1621 if (err) {
1622 mr->size = int128_zero();
1623 object_unparent(OBJECT(mr));
1624 error_propagate(errp, err);
1627 #endif
1629 void memory_region_init_ram_ptr(MemoryRegion *mr,
1630 Object *owner,
1631 const char *name,
1632 uint64_t size,
1633 void *ptr)
1635 memory_region_init(mr, owner, name, size);
1636 mr->ram = true;
1637 mr->terminates = true;
1638 mr->destructor = memory_region_destructor_ram;
1639 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1641 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1642 assert(ptr != NULL);
1643 mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
1646 void memory_region_init_ram_device_ptr(MemoryRegion *mr,
1647 Object *owner,
1648 const char *name,
1649 uint64_t size,
1650 void *ptr)
1652 memory_region_init(mr, owner, name, size);
1653 mr->ram = true;
1654 mr->terminates = true;
1655 mr->ram_device = true;
1656 mr->ops = &ram_device_mem_ops;
1657 mr->opaque = mr;
1658 mr->destructor = memory_region_destructor_ram;
1659 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1660 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1661 assert(ptr != NULL);
1662 mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
1665 void memory_region_init_alias(MemoryRegion *mr,
1666 Object *owner,
1667 const char *name,
1668 MemoryRegion *orig,
1669 hwaddr offset,
1670 uint64_t size)
1672 memory_region_init(mr, owner, name, size);
1673 mr->alias = orig;
1674 mr->alias_offset = offset;
1677 void memory_region_init_rom_nomigrate(MemoryRegion *mr,
1678 struct Object *owner,
1679 const char *name,
1680 uint64_t size,
1681 Error **errp)
1683 Error *err = NULL;
1684 memory_region_init(mr, owner, name, size);
1685 mr->ram = true;
1686 mr->readonly = true;
1687 mr->terminates = true;
1688 mr->destructor = memory_region_destructor_ram;
1689 mr->ram_block = qemu_ram_alloc(size, false, mr, &err);
1690 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1691 if (err) {
1692 mr->size = int128_zero();
1693 object_unparent(OBJECT(mr));
1694 error_propagate(errp, err);
1698 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
1699 Object *owner,
1700 const MemoryRegionOps *ops,
1701 void *opaque,
1702 const char *name,
1703 uint64_t size,
1704 Error **errp)
1706 Error *err = NULL;
1707 assert(ops);
1708 memory_region_init(mr, owner, name, size);
1709 mr->ops = ops;
1710 mr->opaque = opaque;
1711 mr->terminates = true;
1712 mr->rom_device = true;
1713 mr->destructor = memory_region_destructor_ram;
1714 mr->ram_block = qemu_ram_alloc(size, false, mr, &err);
1715 if (err) {
1716 mr->size = int128_zero();
1717 object_unparent(OBJECT(mr));
1718 error_propagate(errp, err);
1722 void memory_region_init_iommu(void *_iommu_mr,
1723 size_t instance_size,
1724 const char *mrtypename,
1725 Object *owner,
1726 const char *name,
1727 uint64_t size)
1729 struct IOMMUMemoryRegion *iommu_mr;
1730 struct MemoryRegion *mr;
1732 object_initialize(_iommu_mr, instance_size, mrtypename);
1733 mr = MEMORY_REGION(_iommu_mr);
1734 memory_region_do_init(mr, owner, name, size);
1735 iommu_mr = IOMMU_MEMORY_REGION(mr);
1736 mr->terminates = true; /* then re-forwards */
1737 QLIST_INIT(&iommu_mr->iommu_notify);
1738 iommu_mr->iommu_notify_flags = IOMMU_NOTIFIER_NONE;
1741 static void memory_region_finalize(Object *obj)
1743 MemoryRegion *mr = MEMORY_REGION(obj);
1745 assert(!mr->container);
1747 /* We know the region is not visible in any address space (it
1748 * does not have a container and cannot be a root either because
1749 * it has no references, so we can blindly clear mr->enabled.
1750 * memory_region_set_enabled instead could trigger a transaction
1751 * and cause an infinite loop.
1753 mr->enabled = false;
1754 memory_region_transaction_begin();
1755 while (!QTAILQ_EMPTY(&mr->subregions)) {
1756 MemoryRegion *subregion = QTAILQ_FIRST(&mr->subregions);
1757 memory_region_del_subregion(mr, subregion);
1759 memory_region_transaction_commit();
1761 mr->destructor(mr);
1762 memory_region_clear_coalescing(mr);
1763 g_free((char *)mr->name);
1764 g_free(mr->ioeventfds);
1767 Object *memory_region_owner(MemoryRegion *mr)
1769 Object *obj = OBJECT(mr);
1770 return obj->parent;
1773 void memory_region_ref(MemoryRegion *mr)
1775 /* MMIO callbacks most likely will access data that belongs
1776 * to the owner, hence the need to ref/unref the owner whenever
1777 * the memory region is in use.
1779 * The memory region is a child of its owner. As long as the
1780 * owner doesn't call unparent itself on the memory region,
1781 * ref-ing the owner will also keep the memory region alive.
1782 * Memory regions without an owner are supposed to never go away;
1783 * we do not ref/unref them because it slows down DMA sensibly.
1785 if (mr && mr->owner) {
1786 object_ref(mr->owner);
1790 void memory_region_unref(MemoryRegion *mr)
1792 if (mr && mr->owner) {
1793 object_unref(mr->owner);
1797 uint64_t memory_region_size(MemoryRegion *mr)
1799 if (int128_eq(mr->size, int128_2_64())) {
1800 return UINT64_MAX;
1802 return int128_get64(mr->size);
1805 const char *memory_region_name(const MemoryRegion *mr)
1807 if (!mr->name) {
1808 ((MemoryRegion *)mr)->name =
1809 object_get_canonical_path_component(OBJECT(mr));
1811 return mr->name;
1814 bool memory_region_is_ram_device(MemoryRegion *mr)
1816 return mr->ram_device;
1819 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
1821 uint8_t mask = mr->dirty_log_mask;
1822 if (global_dirty_log && mr->ram_block) {
1823 mask |= (1 << DIRTY_MEMORY_MIGRATION);
1825 return mask;
1828 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client)
1830 return memory_region_get_dirty_log_mask(mr) & (1 << client);
1833 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion *iommu_mr)
1835 IOMMUNotifierFlag flags = IOMMU_NOTIFIER_NONE;
1836 IOMMUNotifier *iommu_notifier;
1837 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1839 IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) {
1840 flags |= iommu_notifier->notifier_flags;
1843 if (flags != iommu_mr->iommu_notify_flags && imrc->notify_flag_changed) {
1844 imrc->notify_flag_changed(iommu_mr,
1845 iommu_mr->iommu_notify_flags,
1846 flags);
1849 iommu_mr->iommu_notify_flags = flags;
1852 void memory_region_register_iommu_notifier(MemoryRegion *mr,
1853 IOMMUNotifier *n)
1855 IOMMUMemoryRegion *iommu_mr;
1857 if (mr->alias) {
1858 memory_region_register_iommu_notifier(mr->alias, n);
1859 return;
1862 /* We need to register for at least one bitfield */
1863 iommu_mr = IOMMU_MEMORY_REGION(mr);
1864 assert(n->notifier_flags != IOMMU_NOTIFIER_NONE);
1865 assert(n->start <= n->end);
1866 assert(n->iommu_idx >= 0 &&
1867 n->iommu_idx < memory_region_iommu_num_indexes(iommu_mr));
1869 QLIST_INSERT_HEAD(&iommu_mr->iommu_notify, n, node);
1870 memory_region_update_iommu_notify_flags(iommu_mr);
1873 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr)
1875 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1877 if (imrc->get_min_page_size) {
1878 return imrc->get_min_page_size(iommu_mr);
1880 return TARGET_PAGE_SIZE;
1883 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
1885 MemoryRegion *mr = MEMORY_REGION(iommu_mr);
1886 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1887 hwaddr addr, granularity;
1888 IOMMUTLBEntry iotlb;
1890 /* If the IOMMU has its own replay callback, override */
1891 if (imrc->replay) {
1892 imrc->replay(iommu_mr, n);
1893 return;
1896 granularity = memory_region_iommu_get_min_page_size(iommu_mr);
1898 for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
1899 iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE, n->iommu_idx);
1900 if (iotlb.perm != IOMMU_NONE) {
1901 n->notify(n, &iotlb);
1904 /* if (2^64 - MR size) < granularity, it's possible to get an
1905 * infinite loop here. This should catch such a wraparound */
1906 if ((addr + granularity) < addr) {
1907 break;
1912 void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr)
1914 IOMMUNotifier *notifier;
1916 IOMMU_NOTIFIER_FOREACH(notifier, iommu_mr) {
1917 memory_region_iommu_replay(iommu_mr, notifier);
1921 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1922 IOMMUNotifier *n)
1924 IOMMUMemoryRegion *iommu_mr;
1926 if (mr->alias) {
1927 memory_region_unregister_iommu_notifier(mr->alias, n);
1928 return;
1930 QLIST_REMOVE(n, node);
1931 iommu_mr = IOMMU_MEMORY_REGION(mr);
1932 memory_region_update_iommu_notify_flags(iommu_mr);
1935 void memory_region_notify_one(IOMMUNotifier *notifier,
1936 IOMMUTLBEntry *entry)
1938 IOMMUNotifierFlag request_flags;
1941 * Skip the notification if the notification does not overlap
1942 * with registered range.
1944 if (notifier->start > entry->iova + entry->addr_mask ||
1945 notifier->end < entry->iova) {
1946 return;
1949 if (entry->perm & IOMMU_RW) {
1950 request_flags = IOMMU_NOTIFIER_MAP;
1951 } else {
1952 request_flags = IOMMU_NOTIFIER_UNMAP;
1955 if (notifier->notifier_flags & request_flags) {
1956 notifier->notify(notifier, entry);
1960 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1961 int iommu_idx,
1962 IOMMUTLBEntry entry)
1964 IOMMUNotifier *iommu_notifier;
1966 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr)));
1968 IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) {
1969 if (iommu_notifier->iommu_idx == iommu_idx) {
1970 memory_region_notify_one(iommu_notifier, &entry);
1975 int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1976 enum IOMMUMemoryRegionAttr attr,
1977 void *data)
1979 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1981 if (!imrc->get_attr) {
1982 return -EINVAL;
1985 return imrc->get_attr(iommu_mr, attr, data);
1988 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr,
1989 MemTxAttrs attrs)
1991 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1993 if (!imrc->attrs_to_index) {
1994 return 0;
1997 return imrc->attrs_to_index(iommu_mr, attrs);
2000 int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr)
2002 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
2004 if (!imrc->num_indexes) {
2005 return 1;
2008 return imrc->num_indexes(iommu_mr);
2011 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
2013 uint8_t mask = 1 << client;
2014 uint8_t old_logging;
2016 assert(client == DIRTY_MEMORY_VGA);
2017 old_logging = mr->vga_logging_count;
2018 mr->vga_logging_count += log ? 1 : -1;
2019 if (!!old_logging == !!mr->vga_logging_count) {
2020 return;
2023 memory_region_transaction_begin();
2024 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
2025 memory_region_update_pending |= mr->enabled;
2026 memory_region_transaction_commit();
2029 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
2030 hwaddr size, unsigned client)
2032 assert(mr->ram_block);
2033 return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr) + addr,
2034 size, client);
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 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
2075 hwaddr addr,
2076 hwaddr size,
2077 unsigned client)
2079 assert(mr->ram_block);
2080 memory_region_sync_dirty_bitmap(mr);
2081 return cpu_physical_memory_snapshot_and_clear_dirty(
2082 memory_region_get_ram_addr(mr) + addr, size, client);
2085 bool memory_region_snapshot_get_dirty(MemoryRegion *mr, DirtyBitmapSnapshot *snap,
2086 hwaddr addr, hwaddr size)
2088 assert(mr->ram_block);
2089 return cpu_physical_memory_snapshot_get_dirty(snap,
2090 memory_region_get_ram_addr(mr) + addr, size);
2093 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
2095 if (mr->readonly != readonly) {
2096 memory_region_transaction_begin();
2097 mr->readonly = readonly;
2098 memory_region_update_pending |= mr->enabled;
2099 memory_region_transaction_commit();
2103 void memory_region_set_nonvolatile(MemoryRegion *mr, bool nonvolatile)
2105 if (mr->nonvolatile != nonvolatile) {
2106 memory_region_transaction_begin();
2107 mr->nonvolatile = nonvolatile;
2108 memory_region_update_pending |= mr->enabled;
2109 memory_region_transaction_commit();
2113 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
2115 if (mr->romd_mode != romd_mode) {
2116 memory_region_transaction_begin();
2117 mr->romd_mode = romd_mode;
2118 memory_region_update_pending |= mr->enabled;
2119 memory_region_transaction_commit();
2123 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
2124 hwaddr size, unsigned client)
2126 assert(mr->ram_block);
2127 cpu_physical_memory_test_and_clear_dirty(
2128 memory_region_get_ram_addr(mr) + addr, size, client);
2131 int memory_region_get_fd(MemoryRegion *mr)
2133 int fd;
2135 rcu_read_lock();
2136 while (mr->alias) {
2137 mr = mr->alias;
2139 fd = mr->ram_block->fd;
2140 rcu_read_unlock();
2142 return fd;
2145 void *memory_region_get_ram_ptr(MemoryRegion *mr)
2147 void *ptr;
2148 uint64_t offset = 0;
2150 rcu_read_lock();
2151 while (mr->alias) {
2152 offset += mr->alias_offset;
2153 mr = mr->alias;
2155 assert(mr->ram_block);
2156 ptr = qemu_map_ram_ptr(mr->ram_block, offset);
2157 rcu_read_unlock();
2159 return ptr;
2162 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset)
2164 RAMBlock *block;
2166 block = qemu_ram_block_from_host(ptr, false, offset);
2167 if (!block) {
2168 return NULL;
2171 return block->mr;
2174 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
2176 return mr->ram_block ? mr->ram_block->offset : RAM_ADDR_INVALID;
2179 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp)
2181 assert(mr->ram_block);
2183 qemu_ram_resize(mr->ram_block, newsize, errp);
2186 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
2188 FlatView *view;
2189 FlatRange *fr;
2191 view = address_space_get_flatview(as);
2192 FOR_EACH_FLAT_RANGE(fr, view) {
2193 if (fr->mr == mr) {
2194 flat_range_coalesced_io_del(fr, as);
2195 flat_range_coalesced_io_add(fr, as);
2198 flatview_unref(view);
2201 static void memory_region_update_coalesced_range(MemoryRegion *mr)
2203 AddressSpace *as;
2205 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2206 memory_region_update_coalesced_range_as(mr, as);
2210 void memory_region_set_coalescing(MemoryRegion *mr)
2212 memory_region_clear_coalescing(mr);
2213 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
2216 void memory_region_add_coalescing(MemoryRegion *mr,
2217 hwaddr offset,
2218 uint64_t size)
2220 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
2222 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
2223 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
2224 memory_region_update_coalesced_range(mr);
2225 memory_region_set_flush_coalesced(mr);
2228 void memory_region_clear_coalescing(MemoryRegion *mr)
2230 CoalescedMemoryRange *cmr;
2231 bool updated = false;
2233 qemu_flush_coalesced_mmio_buffer();
2234 mr->flush_coalesced_mmio = false;
2236 while (!QTAILQ_EMPTY(&mr->coalesced)) {
2237 cmr = QTAILQ_FIRST(&mr->coalesced);
2238 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
2239 g_free(cmr);
2240 updated = true;
2243 if (updated) {
2244 memory_region_update_coalesced_range(mr);
2248 void memory_region_set_flush_coalesced(MemoryRegion *mr)
2250 mr->flush_coalesced_mmio = true;
2253 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
2255 qemu_flush_coalesced_mmio_buffer();
2256 if (QTAILQ_EMPTY(&mr->coalesced)) {
2257 mr->flush_coalesced_mmio = false;
2261 void memory_region_clear_global_locking(MemoryRegion *mr)
2263 mr->global_locking = false;
2266 static bool userspace_eventfd_warning;
2268 void memory_region_add_eventfd(MemoryRegion *mr,
2269 hwaddr addr,
2270 unsigned size,
2271 bool match_data,
2272 uint64_t data,
2273 EventNotifier *e)
2275 MemoryRegionIoeventfd mrfd = {
2276 .addr.start = int128_make64(addr),
2277 .addr.size = int128_make64(size),
2278 .match_data = match_data,
2279 .data = data,
2280 .e = e,
2282 unsigned i;
2284 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2285 userspace_eventfd_warning))) {
2286 userspace_eventfd_warning = true;
2287 error_report("Using eventfd without MMIO binding in KVM. "
2288 "Suboptimal performance expected");
2291 if (size) {
2292 adjust_endianness(mr, &mrfd.data, size);
2294 memory_region_transaction_begin();
2295 for (i = 0; i < mr->ioeventfd_nb; ++i) {
2296 if (memory_region_ioeventfd_before(&mrfd, &mr->ioeventfds[i])) {
2297 break;
2300 ++mr->ioeventfd_nb;
2301 mr->ioeventfds = g_realloc(mr->ioeventfds,
2302 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
2303 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
2304 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
2305 mr->ioeventfds[i] = mrfd;
2306 ioeventfd_update_pending |= mr->enabled;
2307 memory_region_transaction_commit();
2310 void memory_region_del_eventfd(MemoryRegion *mr,
2311 hwaddr addr,
2312 unsigned size,
2313 bool match_data,
2314 uint64_t data,
2315 EventNotifier *e)
2317 MemoryRegionIoeventfd mrfd = {
2318 .addr.start = int128_make64(addr),
2319 .addr.size = int128_make64(size),
2320 .match_data = match_data,
2321 .data = data,
2322 .e = e,
2324 unsigned i;
2326 if (size) {
2327 adjust_endianness(mr, &mrfd.data, size);
2329 memory_region_transaction_begin();
2330 for (i = 0; i < mr->ioeventfd_nb; ++i) {
2331 if (memory_region_ioeventfd_equal(&mrfd, &mr->ioeventfds[i])) {
2332 break;
2335 assert(i != mr->ioeventfd_nb);
2336 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
2337 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
2338 --mr->ioeventfd_nb;
2339 mr->ioeventfds = g_realloc(mr->ioeventfds,
2340 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
2341 ioeventfd_update_pending |= mr->enabled;
2342 memory_region_transaction_commit();
2345 static void memory_region_update_container_subregions(MemoryRegion *subregion)
2347 MemoryRegion *mr = subregion->container;
2348 MemoryRegion *other;
2350 memory_region_transaction_begin();
2352 memory_region_ref(subregion);
2353 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
2354 if (subregion->priority >= other->priority) {
2355 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
2356 goto done;
2359 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
2360 done:
2361 memory_region_update_pending |= mr->enabled && subregion->enabled;
2362 memory_region_transaction_commit();
2365 static void memory_region_add_subregion_common(MemoryRegion *mr,
2366 hwaddr offset,
2367 MemoryRegion *subregion)
2369 assert(!subregion->container);
2370 subregion->container = mr;
2371 subregion->addr = offset;
2372 memory_region_update_container_subregions(subregion);
2375 void memory_region_add_subregion(MemoryRegion *mr,
2376 hwaddr offset,
2377 MemoryRegion *subregion)
2379 subregion->priority = 0;
2380 memory_region_add_subregion_common(mr, offset, subregion);
2383 void memory_region_add_subregion_overlap(MemoryRegion *mr,
2384 hwaddr offset,
2385 MemoryRegion *subregion,
2386 int priority)
2388 subregion->priority = priority;
2389 memory_region_add_subregion_common(mr, offset, subregion);
2392 void memory_region_del_subregion(MemoryRegion *mr,
2393 MemoryRegion *subregion)
2395 memory_region_transaction_begin();
2396 assert(subregion->container == mr);
2397 subregion->container = NULL;
2398 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
2399 memory_region_unref(subregion);
2400 memory_region_update_pending |= mr->enabled && subregion->enabled;
2401 memory_region_transaction_commit();
2404 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
2406 if (enabled == mr->enabled) {
2407 return;
2409 memory_region_transaction_begin();
2410 mr->enabled = enabled;
2411 memory_region_update_pending = true;
2412 memory_region_transaction_commit();
2415 void memory_region_set_size(MemoryRegion *mr, uint64_t size)
2417 Int128 s = int128_make64(size);
2419 if (size == UINT64_MAX) {
2420 s = int128_2_64();
2422 if (int128_eq(s, mr->size)) {
2423 return;
2425 memory_region_transaction_begin();
2426 mr->size = s;
2427 memory_region_update_pending = true;
2428 memory_region_transaction_commit();
2431 static void memory_region_readd_subregion(MemoryRegion *mr)
2433 MemoryRegion *container = mr->container;
2435 if (container) {
2436 memory_region_transaction_begin();
2437 memory_region_ref(mr);
2438 memory_region_del_subregion(container, mr);
2439 mr->container = container;
2440 memory_region_update_container_subregions(mr);
2441 memory_region_unref(mr);
2442 memory_region_transaction_commit();
2446 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
2448 if (addr != mr->addr) {
2449 mr->addr = addr;
2450 memory_region_readd_subregion(mr);
2454 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
2456 assert(mr->alias);
2458 if (offset == mr->alias_offset) {
2459 return;
2462 memory_region_transaction_begin();
2463 mr->alias_offset = offset;
2464 memory_region_update_pending |= mr->enabled;
2465 memory_region_transaction_commit();
2468 uint64_t memory_region_get_alignment(const MemoryRegion *mr)
2470 return mr->align;
2473 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
2475 const AddrRange *addr = addr_;
2476 const FlatRange *fr = fr_;
2478 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
2479 return -1;
2480 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
2481 return 1;
2483 return 0;
2486 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
2488 return bsearch(&addr, view->ranges, view->nr,
2489 sizeof(FlatRange), cmp_flatrange_addr);
2492 bool memory_region_is_mapped(MemoryRegion *mr)
2494 return mr->container ? true : false;
2497 /* Same as memory_region_find, but it does not add a reference to the
2498 * returned region. It must be called from an RCU critical section.
2500 static MemoryRegionSection memory_region_find_rcu(MemoryRegion *mr,
2501 hwaddr addr, uint64_t size)
2503 MemoryRegionSection ret = { .mr = NULL };
2504 MemoryRegion *root;
2505 AddressSpace *as;
2506 AddrRange range;
2507 FlatView *view;
2508 FlatRange *fr;
2510 addr += mr->addr;
2511 for (root = mr; root->container; ) {
2512 root = root->container;
2513 addr += root->addr;
2516 as = memory_region_to_address_space(root);
2517 if (!as) {
2518 return ret;
2520 range = addrrange_make(int128_make64(addr), int128_make64(size));
2522 view = address_space_to_flatview(as);
2523 fr = flatview_lookup(view, range);
2524 if (!fr) {
2525 return ret;
2528 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
2529 --fr;
2532 ret.mr = fr->mr;
2533 ret.fv = view;
2534 range = addrrange_intersection(range, fr->addr);
2535 ret.offset_within_region = fr->offset_in_region;
2536 ret.offset_within_region += int128_get64(int128_sub(range.start,
2537 fr->addr.start));
2538 ret.size = range.size;
2539 ret.offset_within_address_space = int128_get64(range.start);
2540 ret.readonly = fr->readonly;
2541 ret.nonvolatile = fr->nonvolatile;
2542 return ret;
2545 MemoryRegionSection memory_region_find(MemoryRegion *mr,
2546 hwaddr addr, uint64_t size)
2548 MemoryRegionSection ret;
2549 rcu_read_lock();
2550 ret = memory_region_find_rcu(mr, addr, size);
2551 if (ret.mr) {
2552 memory_region_ref(ret.mr);
2554 rcu_read_unlock();
2555 return ret;
2558 bool memory_region_present(MemoryRegion *container, hwaddr addr)
2560 MemoryRegion *mr;
2562 rcu_read_lock();
2563 mr = memory_region_find_rcu(container, addr, 1).mr;
2564 rcu_read_unlock();
2565 return mr && mr != container;
2568 void memory_global_dirty_log_sync(void)
2570 memory_region_sync_dirty_bitmap(NULL);
2573 static VMChangeStateEntry *vmstate_change;
2575 void memory_global_dirty_log_start(void)
2577 if (vmstate_change) {
2578 qemu_del_vm_change_state_handler(vmstate_change);
2579 vmstate_change = NULL;
2582 global_dirty_log = true;
2584 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
2586 /* Refresh DIRTY_LOG_MIGRATION bit. */
2587 memory_region_transaction_begin();
2588 memory_region_update_pending = true;
2589 memory_region_transaction_commit();
2592 static void memory_global_dirty_log_do_stop(void)
2594 global_dirty_log = false;
2596 /* Refresh DIRTY_LOG_MIGRATION bit. */
2597 memory_region_transaction_begin();
2598 memory_region_update_pending = true;
2599 memory_region_transaction_commit();
2601 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
2604 static void memory_vm_change_state_handler(void *opaque, int running,
2605 RunState state)
2607 if (running) {
2608 memory_global_dirty_log_do_stop();
2610 if (vmstate_change) {
2611 qemu_del_vm_change_state_handler(vmstate_change);
2612 vmstate_change = NULL;
2617 void memory_global_dirty_log_stop(void)
2619 if (!runstate_is_running()) {
2620 if (vmstate_change) {
2621 return;
2623 vmstate_change = qemu_add_vm_change_state_handler(
2624 memory_vm_change_state_handler, NULL);
2625 return;
2628 memory_global_dirty_log_do_stop();
2631 static void listener_add_address_space(MemoryListener *listener,
2632 AddressSpace *as)
2634 FlatView *view;
2635 FlatRange *fr;
2637 if (listener->begin) {
2638 listener->begin(listener);
2640 if (global_dirty_log) {
2641 if (listener->log_global_start) {
2642 listener->log_global_start(listener);
2646 view = address_space_get_flatview(as);
2647 FOR_EACH_FLAT_RANGE(fr, view) {
2648 MemoryRegionSection section = section_from_flat_range(fr, view);
2650 if (listener->region_add) {
2651 listener->region_add(listener, &section);
2653 if (fr->dirty_log_mask && listener->log_start) {
2654 listener->log_start(listener, &section, 0, fr->dirty_log_mask);
2657 if (listener->commit) {
2658 listener->commit(listener);
2660 flatview_unref(view);
2663 static void listener_del_address_space(MemoryListener *listener,
2664 AddressSpace *as)
2666 FlatView *view;
2667 FlatRange *fr;
2669 if (listener->begin) {
2670 listener->begin(listener);
2672 view = address_space_get_flatview(as);
2673 FOR_EACH_FLAT_RANGE(fr, view) {
2674 MemoryRegionSection section = section_from_flat_range(fr, view);
2676 if (fr->dirty_log_mask && listener->log_stop) {
2677 listener->log_stop(listener, &section, fr->dirty_log_mask, 0);
2679 if (listener->region_del) {
2680 listener->region_del(listener, &section);
2683 if (listener->commit) {
2684 listener->commit(listener);
2686 flatview_unref(view);
2689 void memory_listener_register(MemoryListener *listener, AddressSpace *as)
2691 MemoryListener *other = NULL;
2693 listener->address_space = as;
2694 if (QTAILQ_EMPTY(&memory_listeners)
2695 || listener->priority >= QTAILQ_LAST(&memory_listeners)->priority) {
2696 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
2697 } else {
2698 QTAILQ_FOREACH(other, &memory_listeners, link) {
2699 if (listener->priority < other->priority) {
2700 break;
2703 QTAILQ_INSERT_BEFORE(other, listener, link);
2706 if (QTAILQ_EMPTY(&as->listeners)
2707 || listener->priority >= QTAILQ_LAST(&as->listeners)->priority) {
2708 QTAILQ_INSERT_TAIL(&as->listeners, listener, link_as);
2709 } else {
2710 QTAILQ_FOREACH(other, &as->listeners, link_as) {
2711 if (listener->priority < other->priority) {
2712 break;
2715 QTAILQ_INSERT_BEFORE(other, listener, link_as);
2718 listener_add_address_space(listener, as);
2721 void memory_listener_unregister(MemoryListener *listener)
2723 if (!listener->address_space) {
2724 return;
2727 listener_del_address_space(listener, listener->address_space);
2728 QTAILQ_REMOVE(&memory_listeners, listener, link);
2729 QTAILQ_REMOVE(&listener->address_space->listeners, listener, link_as);
2730 listener->address_space = NULL;
2733 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
2735 memory_region_ref(root);
2736 as->root = root;
2737 as->current_map = NULL;
2738 as->ioeventfd_nb = 0;
2739 as->ioeventfds = NULL;
2740 QTAILQ_INIT(&as->listeners);
2741 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
2742 as->name = g_strdup(name ? name : "anonymous");
2743 address_space_update_topology(as);
2744 address_space_update_ioeventfds(as);
2747 static void do_address_space_destroy(AddressSpace *as)
2749 assert(QTAILQ_EMPTY(&as->listeners));
2751 flatview_unref(as->current_map);
2752 g_free(as->name);
2753 g_free(as->ioeventfds);
2754 memory_region_unref(as->root);
2757 void address_space_destroy(AddressSpace *as)
2759 MemoryRegion *root = as->root;
2761 /* Flush out anything from MemoryListeners listening in on this */
2762 memory_region_transaction_begin();
2763 as->root = NULL;
2764 memory_region_transaction_commit();
2765 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
2767 /* At this point, as->dispatch and as->current_map are dummy
2768 * entries that the guest should never use. Wait for the old
2769 * values to expire before freeing the data.
2771 as->root = root;
2772 call_rcu(as, do_address_space_destroy, rcu);
2775 static const char *memory_region_type(MemoryRegion *mr)
2777 if (memory_region_is_ram_device(mr)) {
2778 return "ramd";
2779 } else if (memory_region_is_romd(mr)) {
2780 return "romd";
2781 } else if (memory_region_is_rom(mr)) {
2782 return "rom";
2783 } else if (memory_region_is_ram(mr)) {
2784 return "ram";
2785 } else {
2786 return "i/o";
2790 typedef struct MemoryRegionList MemoryRegionList;
2792 struct MemoryRegionList {
2793 const MemoryRegion *mr;
2794 QTAILQ_ENTRY(MemoryRegionList) mrqueue;
2797 typedef QTAILQ_HEAD(, MemoryRegionList) MemoryRegionListHead;
2799 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2800 int128_sub((size), int128_one())) : 0)
2801 #define MTREE_INDENT " "
2803 static void mtree_expand_owner(fprintf_function mon_printf, void *f,
2804 const char *label, Object *obj)
2806 DeviceState *dev = (DeviceState *) object_dynamic_cast(obj, TYPE_DEVICE);
2808 mon_printf(f, " %s:{%s", label, dev ? "dev" : "obj");
2809 if (dev && dev->id) {
2810 mon_printf(f, " id=%s", dev->id);
2811 } else {
2812 gchar *canonical_path = object_get_canonical_path(obj);
2813 if (canonical_path) {
2814 mon_printf(f, " path=%s", canonical_path);
2815 g_free(canonical_path);
2816 } else {
2817 mon_printf(f, " type=%s", object_get_typename(obj));
2820 mon_printf(f, "}");
2823 static void mtree_print_mr_owner(fprintf_function mon_printf, void *f,
2824 const MemoryRegion *mr)
2826 Object *owner = mr->owner;
2827 Object *parent = memory_region_owner((MemoryRegion *)mr);
2829 if (!owner && !parent) {
2830 mon_printf(f, " orphan");
2831 return;
2833 if (owner) {
2834 mtree_expand_owner(mon_printf, f, "owner", owner);
2836 if (parent && parent != owner) {
2837 mtree_expand_owner(mon_printf, f, "parent", parent);
2841 static void mtree_print_mr(fprintf_function mon_printf, void *f,
2842 const MemoryRegion *mr, unsigned int level,
2843 hwaddr base,
2844 MemoryRegionListHead *alias_print_queue,
2845 bool owner)
2847 MemoryRegionList *new_ml, *ml, *next_ml;
2848 MemoryRegionListHead submr_print_queue;
2849 const MemoryRegion *submr;
2850 unsigned int i;
2851 hwaddr cur_start, cur_end;
2853 if (!mr) {
2854 return;
2857 for (i = 0; i < level; i++) {
2858 mon_printf(f, MTREE_INDENT);
2861 cur_start = base + mr->addr;
2862 cur_end = cur_start + MR_SIZE(mr->size);
2865 * Try to detect overflow of memory region. This should never
2866 * happen normally. When it happens, we dump something to warn the
2867 * user who is observing this.
2869 if (cur_start < base || cur_end < cur_start) {
2870 mon_printf(f, "[DETECTED OVERFLOW!] ");
2873 if (mr->alias) {
2874 MemoryRegionList *ml;
2875 bool found = false;
2877 /* check if the alias is already in the queue */
2878 QTAILQ_FOREACH(ml, alias_print_queue, mrqueue) {
2879 if (ml->mr == mr->alias) {
2880 found = true;
2884 if (!found) {
2885 ml = g_new(MemoryRegionList, 1);
2886 ml->mr = mr->alias;
2887 QTAILQ_INSERT_TAIL(alias_print_queue, ml, mrqueue);
2889 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
2890 " (prio %d, %s%s): alias %s @%s " TARGET_FMT_plx
2891 "-" TARGET_FMT_plx "%s",
2892 cur_start, cur_end,
2893 mr->priority,
2894 mr->nonvolatile ? "nv-" : "",
2895 memory_region_type((MemoryRegion *)mr),
2896 memory_region_name(mr),
2897 memory_region_name(mr->alias),
2898 mr->alias_offset,
2899 mr->alias_offset + MR_SIZE(mr->size),
2900 mr->enabled ? "" : " [disabled]");
2901 if (owner) {
2902 mtree_print_mr_owner(mon_printf, f, mr);
2904 } else {
2905 mon_printf(f,
2906 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %s%s): %s%s",
2907 cur_start, cur_end,
2908 mr->priority,
2909 mr->nonvolatile ? "nv-" : "",
2910 memory_region_type((MemoryRegion *)mr),
2911 memory_region_name(mr),
2912 mr->enabled ? "" : " [disabled]");
2913 if (owner) {
2914 mtree_print_mr_owner(mon_printf, f, mr);
2917 mon_printf(f, "\n");
2919 QTAILQ_INIT(&submr_print_queue);
2921 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
2922 new_ml = g_new(MemoryRegionList, 1);
2923 new_ml->mr = submr;
2924 QTAILQ_FOREACH(ml, &submr_print_queue, mrqueue) {
2925 if (new_ml->mr->addr < ml->mr->addr ||
2926 (new_ml->mr->addr == ml->mr->addr &&
2927 new_ml->mr->priority > ml->mr->priority)) {
2928 QTAILQ_INSERT_BEFORE(ml, new_ml, mrqueue);
2929 new_ml = NULL;
2930 break;
2933 if (new_ml) {
2934 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, mrqueue);
2938 QTAILQ_FOREACH(ml, &submr_print_queue, mrqueue) {
2939 mtree_print_mr(mon_printf, f, ml->mr, level + 1, cur_start,
2940 alias_print_queue, owner);
2943 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, mrqueue, next_ml) {
2944 g_free(ml);
2948 struct FlatViewInfo {
2949 fprintf_function mon_printf;
2950 void *f;
2951 int counter;
2952 bool dispatch_tree;
2953 bool owner;
2956 static void mtree_print_flatview(gpointer key, gpointer value,
2957 gpointer user_data)
2959 FlatView *view = key;
2960 GArray *fv_address_spaces = value;
2961 struct FlatViewInfo *fvi = user_data;
2962 fprintf_function p = fvi->mon_printf;
2963 void *f = fvi->f;
2964 FlatRange *range = &view->ranges[0];
2965 MemoryRegion *mr;
2966 int n = view->nr;
2967 int i;
2968 AddressSpace *as;
2970 p(f, "FlatView #%d\n", fvi->counter);
2971 ++fvi->counter;
2973 for (i = 0; i < fv_address_spaces->len; ++i) {
2974 as = g_array_index(fv_address_spaces, AddressSpace*, i);
2975 p(f, " AS \"%s\", root: %s", as->name, memory_region_name(as->root));
2976 if (as->root->alias) {
2977 p(f, ", alias %s", memory_region_name(as->root->alias));
2979 p(f, "\n");
2982 p(f, " Root memory region: %s\n",
2983 view->root ? memory_region_name(view->root) : "(none)");
2985 if (n <= 0) {
2986 p(f, MTREE_INDENT "No rendered FlatView\n\n");
2987 return;
2990 while (n--) {
2991 mr = range->mr;
2992 if (range->offset_in_region) {
2993 p(f, MTREE_INDENT TARGET_FMT_plx "-"
2994 TARGET_FMT_plx " (prio %d, %s%s): %s @" TARGET_FMT_plx,
2995 int128_get64(range->addr.start),
2996 int128_get64(range->addr.start) + MR_SIZE(range->addr.size),
2997 mr->priority,
2998 range->nonvolatile ? "nv-" : "",
2999 range->readonly ? "rom" : memory_region_type(mr),
3000 memory_region_name(mr),
3001 range->offset_in_region);
3002 } else {
3003 p(f, MTREE_INDENT TARGET_FMT_plx "-"
3004 TARGET_FMT_plx " (prio %d, %s%s): %s",
3005 int128_get64(range->addr.start),
3006 int128_get64(range->addr.start) + MR_SIZE(range->addr.size),
3007 mr->priority,
3008 range->nonvolatile ? "nv-" : "",
3009 range->readonly ? "rom" : memory_region_type(mr),
3010 memory_region_name(mr));
3012 if (fvi->owner) {
3013 mtree_print_mr_owner(p, f, mr);
3015 p(f, "\n");
3016 range++;
3019 #if !defined(CONFIG_USER_ONLY)
3020 if (fvi->dispatch_tree && view->root) {
3021 mtree_print_dispatch(p, f, view->dispatch, view->root);
3023 #endif
3025 p(f, "\n");
3028 static gboolean mtree_info_flatview_free(gpointer key, gpointer value,
3029 gpointer user_data)
3031 FlatView *view = key;
3032 GArray *fv_address_spaces = value;
3034 g_array_unref(fv_address_spaces);
3035 flatview_unref(view);
3037 return true;
3040 void mtree_info(fprintf_function mon_printf, void *f, bool flatview,
3041 bool dispatch_tree, bool owner)
3043 MemoryRegionListHead ml_head;
3044 MemoryRegionList *ml, *ml2;
3045 AddressSpace *as;
3047 if (flatview) {
3048 FlatView *view;
3049 struct FlatViewInfo fvi = {
3050 .mon_printf = mon_printf,
3051 .f = f,
3052 .counter = 0,
3053 .dispatch_tree = dispatch_tree,
3054 .owner = owner,
3056 GArray *fv_address_spaces;
3057 GHashTable *views = g_hash_table_new(g_direct_hash, g_direct_equal);
3059 /* Gather all FVs in one table */
3060 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
3061 view = address_space_get_flatview(as);
3063 fv_address_spaces = g_hash_table_lookup(views, view);
3064 if (!fv_address_spaces) {
3065 fv_address_spaces = g_array_new(false, false, sizeof(as));
3066 g_hash_table_insert(views, view, fv_address_spaces);
3069 g_array_append_val(fv_address_spaces, as);
3072 /* Print */
3073 g_hash_table_foreach(views, mtree_print_flatview, &fvi);
3075 /* Free */
3076 g_hash_table_foreach_remove(views, mtree_info_flatview_free, 0);
3077 g_hash_table_unref(views);
3079 return;
3082 QTAILQ_INIT(&ml_head);
3084 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
3085 mon_printf(f, "address-space: %s\n", as->name);
3086 mtree_print_mr(mon_printf, f, as->root, 1, 0, &ml_head, owner);
3087 mon_printf(f, "\n");
3090 /* print aliased regions */
3091 QTAILQ_FOREACH(ml, &ml_head, mrqueue) {
3092 mon_printf(f, "memory-region: %s\n", memory_region_name(ml->mr));
3093 mtree_print_mr(mon_printf, f, ml->mr, 1, 0, &ml_head, owner);
3094 mon_printf(f, "\n");
3097 QTAILQ_FOREACH_SAFE(ml, &ml_head, mrqueue, ml2) {
3098 g_free(ml);
3102 void memory_region_init_ram(MemoryRegion *mr,
3103 struct Object *owner,
3104 const char *name,
3105 uint64_t size,
3106 Error **errp)
3108 DeviceState *owner_dev;
3109 Error *err = NULL;
3111 memory_region_init_ram_nomigrate(mr, owner, name, size, &err);
3112 if (err) {
3113 error_propagate(errp, err);
3114 return;
3116 /* This will assert if owner is neither NULL nor a DeviceState.
3117 * We only want the owner here for the purposes of defining a
3118 * unique name for migration. TODO: Ideally we should implement
3119 * a naming scheme for Objects which are not DeviceStates, in
3120 * which case we can relax this restriction.
3122 owner_dev = DEVICE(owner);
3123 vmstate_register_ram(mr, owner_dev);
3126 void memory_region_init_rom(MemoryRegion *mr,
3127 struct Object *owner,
3128 const char *name,
3129 uint64_t size,
3130 Error **errp)
3132 DeviceState *owner_dev;
3133 Error *err = NULL;
3135 memory_region_init_rom_nomigrate(mr, owner, name, size, &err);
3136 if (err) {
3137 error_propagate(errp, err);
3138 return;
3140 /* This will assert if owner is neither NULL nor a DeviceState.
3141 * We only want the owner here for the purposes of defining a
3142 * unique name for migration. TODO: Ideally we should implement
3143 * a naming scheme for Objects which are not DeviceStates, in
3144 * which case we can relax this restriction.
3146 owner_dev = DEVICE(owner);
3147 vmstate_register_ram(mr, owner_dev);
3150 void memory_region_init_rom_device(MemoryRegion *mr,
3151 struct Object *owner,
3152 const MemoryRegionOps *ops,
3153 void *opaque,
3154 const char *name,
3155 uint64_t size,
3156 Error **errp)
3158 DeviceState *owner_dev;
3159 Error *err = NULL;
3161 memory_region_init_rom_device_nomigrate(mr, owner, ops, opaque,
3162 name, size, &err);
3163 if (err) {
3164 error_propagate(errp, err);
3165 return;
3167 /* This will assert if owner is neither NULL nor a DeviceState.
3168 * We only want the owner here for the purposes of defining a
3169 * unique name for migration. TODO: Ideally we should implement
3170 * a naming scheme for Objects which are not DeviceStates, in
3171 * which case we can relax this restriction.
3173 owner_dev = DEVICE(owner);
3174 vmstate_register_ram(mr, owner_dev);
3177 static const TypeInfo memory_region_info = {
3178 .parent = TYPE_OBJECT,
3179 .name = TYPE_MEMORY_REGION,
3180 .instance_size = sizeof(MemoryRegion),
3181 .instance_init = memory_region_initfn,
3182 .instance_finalize = memory_region_finalize,
3185 static const TypeInfo iommu_memory_region_info = {
3186 .parent = TYPE_MEMORY_REGION,
3187 .name = TYPE_IOMMU_MEMORY_REGION,
3188 .class_size = sizeof(IOMMUMemoryRegionClass),
3189 .instance_size = sizeof(IOMMUMemoryRegion),
3190 .instance_init = iommu_memory_region_initfn,
3191 .abstract = true,
3194 static void memory_register_types(void)
3196 type_register_static(&memory_region_info);
3197 type_register_static(&iommu_memory_region_info);
3200 type_init(memory_register_types)