travis: add no-TCG build
[qemu/ericb.git] / memory.c
blob69f697c20e9633155fa5e069701ca75aa02a97b5
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 "exec/ioport.h"
23 #include "qapi/visitor.h"
24 #include "qemu/bitops.h"
25 #include "qemu/error-report.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/misc/mmio_interface.h"
34 #include "hw/qdev-properties.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(memory_listeners, MemoryListener) memory_listeners
44 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
46 static QTAILQ_HEAD(, AddressSpace) address_spaces
47 = QTAILQ_HEAD_INITIALIZER(address_spaces);
49 typedef struct AddrRange AddrRange;
52 * Note that signed integers are needed for negative offsetting in aliases
53 * (large MemoryRegion::alias_offset).
55 struct AddrRange {
56 Int128 start;
57 Int128 size;
60 static AddrRange addrrange_make(Int128 start, Int128 size)
62 return (AddrRange) { start, size };
65 static bool addrrange_equal(AddrRange r1, AddrRange r2)
67 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
70 static Int128 addrrange_end(AddrRange r)
72 return int128_add(r.start, r.size);
75 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
77 int128_addto(&range.start, delta);
78 return range;
81 static bool addrrange_contains(AddrRange range, Int128 addr)
83 return int128_ge(addr, range.start)
84 && int128_lt(addr, addrrange_end(range));
87 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
89 return addrrange_contains(r1, r2.start)
90 || addrrange_contains(r2, r1.start);
93 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
95 Int128 start = int128_max(r1.start, r2.start);
96 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
97 return addrrange_make(start, int128_sub(end, start));
100 enum ListenerDirection { Forward, Reverse };
102 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
103 do { \
104 MemoryListener *_listener; \
106 switch (_direction) { \
107 case Forward: \
108 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
109 if (_listener->_callback) { \
110 _listener->_callback(_listener, ##_args); \
113 break; \
114 case Reverse: \
115 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
116 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; \
130 struct memory_listeners_as *list = &(_as)->listeners; \
132 switch (_direction) { \
133 case Forward: \
134 QTAILQ_FOREACH(_listener, list, link_as) { \
135 if (_listener->_callback) { \
136 _listener->_callback(_listener, _section, ##_args); \
139 break; \
140 case Reverse: \
141 QTAILQ_FOREACH_REVERSE(_listener, list, memory_listeners_as, \
142 link_as) { \
143 if (_listener->_callback) { \
144 _listener->_callback(_listener, _section, ##_args); \
147 break; \
148 default: \
149 abort(); \
151 } while (0)
153 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
154 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
155 do { \
156 MemoryRegionSection mrs = section_from_flat_range(fr, 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 typedef struct FlatRange FlatRange;
210 typedef struct FlatView FlatView;
212 /* Range of memory in the global map. Addresses are absolute. */
213 struct FlatRange {
214 MemoryRegion *mr;
215 hwaddr offset_in_region;
216 AddrRange addr;
217 uint8_t dirty_log_mask;
218 bool romd_mode;
219 bool readonly;
222 /* Flattened global view of current active memory hierarchy. Kept in sorted
223 * order.
225 struct FlatView {
226 struct rcu_head rcu;
227 unsigned ref;
228 FlatRange *ranges;
229 unsigned nr;
230 unsigned nr_allocated;
233 typedef struct AddressSpaceOps AddressSpaceOps;
235 #define FOR_EACH_FLAT_RANGE(var, view) \
236 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
238 static inline MemoryRegionSection
239 section_from_flat_range(FlatRange *fr, AddressSpace *as)
241 return (MemoryRegionSection) {
242 .mr = fr->mr,
243 .address_space = as,
244 .offset_within_region = fr->offset_in_region,
245 .size = fr->addr.size,
246 .offset_within_address_space = int128_get64(fr->addr.start),
247 .readonly = fr->readonly,
251 static bool flatrange_equal(FlatRange *a, FlatRange *b)
253 return a->mr == b->mr
254 && addrrange_equal(a->addr, b->addr)
255 && a->offset_in_region == b->offset_in_region
256 && a->romd_mode == b->romd_mode
257 && a->readonly == b->readonly;
260 static void flatview_init(FlatView *view)
262 view->ref = 1;
263 view->ranges = NULL;
264 view->nr = 0;
265 view->nr_allocated = 0;
268 /* Insert a range into a given position. Caller is responsible for maintaining
269 * sorting order.
271 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
273 if (view->nr == view->nr_allocated) {
274 view->nr_allocated = MAX(2 * view->nr, 10);
275 view->ranges = g_realloc(view->ranges,
276 view->nr_allocated * sizeof(*view->ranges));
278 memmove(view->ranges + pos + 1, view->ranges + pos,
279 (view->nr - pos) * sizeof(FlatRange));
280 view->ranges[pos] = *range;
281 memory_region_ref(range->mr);
282 ++view->nr;
285 static void flatview_destroy(FlatView *view)
287 int i;
289 for (i = 0; i < view->nr; i++) {
290 memory_region_unref(view->ranges[i].mr);
292 g_free(view->ranges);
293 g_free(view);
296 static void flatview_ref(FlatView *view)
298 atomic_inc(&view->ref);
301 static void flatview_unref(FlatView *view)
303 if (atomic_fetch_dec(&view->ref) == 1) {
304 flatview_destroy(view);
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;
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 hwaddr memory_region_to_absolute_addr(MemoryRegion *mr, hwaddr offset)
381 MemoryRegion *root;
382 hwaddr abs_addr = offset;
384 abs_addr += mr->addr;
385 for (root = mr; root->container; ) {
386 root = root->container;
387 abs_addr += root->addr;
390 return abs_addr;
393 static int get_cpu_index(void)
395 if (current_cpu) {
396 return current_cpu->cpu_index;
398 return -1;
401 static MemTxResult memory_region_oldmmio_read_accessor(MemoryRegion *mr,
402 hwaddr addr,
403 uint64_t *value,
404 unsigned size,
405 unsigned shift,
406 uint64_t mask,
407 MemTxAttrs attrs)
409 uint64_t tmp;
411 tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);
412 if (mr->subpage) {
413 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
414 } else if (mr == &io_mem_notdirty) {
415 /* Accesses to code which has previously been translated into a TB show
416 * up in the MMIO path, as accesses to the io_mem_notdirty
417 * MemoryRegion. */
418 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
419 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
420 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
421 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
423 *value |= (tmp & mask) << shift;
424 return MEMTX_OK;
427 static MemTxResult memory_region_read_accessor(MemoryRegion *mr,
428 hwaddr addr,
429 uint64_t *value,
430 unsigned size,
431 unsigned shift,
432 uint64_t mask,
433 MemTxAttrs attrs)
435 uint64_t tmp;
437 tmp = mr->ops->read(mr->opaque, addr, size);
438 if (mr->subpage) {
439 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
440 } else if (mr == &io_mem_notdirty) {
441 /* Accesses to code which has previously been translated into a TB show
442 * up in the MMIO path, as accesses to the io_mem_notdirty
443 * MemoryRegion. */
444 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
445 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
446 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
447 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
449 *value |= (tmp & mask) << shift;
450 return MEMTX_OK;
453 static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr,
454 hwaddr addr,
455 uint64_t *value,
456 unsigned size,
457 unsigned shift,
458 uint64_t mask,
459 MemTxAttrs attrs)
461 uint64_t tmp = 0;
462 MemTxResult r;
464 r = mr->ops->read_with_attrs(mr->opaque, addr, &tmp, size, attrs);
465 if (mr->subpage) {
466 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
467 } else if (mr == &io_mem_notdirty) {
468 /* Accesses to code which has previously been translated into a TB show
469 * up in the MMIO path, as accesses to the io_mem_notdirty
470 * MemoryRegion. */
471 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
472 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
473 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
474 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
476 *value |= (tmp & mask) << shift;
477 return r;
480 static MemTxResult memory_region_oldmmio_write_accessor(MemoryRegion *mr,
481 hwaddr addr,
482 uint64_t *value,
483 unsigned size,
484 unsigned shift,
485 uint64_t mask,
486 MemTxAttrs attrs)
488 uint64_t tmp;
490 tmp = (*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->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp);
503 return MEMTX_OK;
506 static MemTxResult memory_region_write_accessor(MemoryRegion *mr,
507 hwaddr addr,
508 uint64_t *value,
509 unsigned size,
510 unsigned shift,
511 uint64_t mask,
512 MemTxAttrs attrs)
514 uint64_t tmp;
516 tmp = (*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 mr->ops->write(mr->opaque, addr, tmp, size);
529 return MEMTX_OK;
532 static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr,
533 hwaddr addr,
534 uint64_t *value,
535 unsigned size,
536 unsigned shift,
537 uint64_t mask,
538 MemTxAttrs attrs)
540 uint64_t tmp;
542 tmp = (*value >> shift) & mask;
543 if (mr->subpage) {
544 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
545 } else if (mr == &io_mem_notdirty) {
546 /* Accesses to code which has previously been translated into a TB show
547 * up in the MMIO path, as accesses to the io_mem_notdirty
548 * MemoryRegion. */
549 trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
550 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
551 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
552 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
554 return mr->ops->write_with_attrs(mr->opaque, addr, tmp, size, attrs);
557 static MemTxResult access_with_adjusted_size(hwaddr addr,
558 uint64_t *value,
559 unsigned size,
560 unsigned access_size_min,
561 unsigned access_size_max,
562 MemTxResult (*access)(MemoryRegion *mr,
563 hwaddr addr,
564 uint64_t *value,
565 unsigned size,
566 unsigned shift,
567 uint64_t mask,
568 MemTxAttrs attrs),
569 MemoryRegion *mr,
570 MemTxAttrs attrs)
572 uint64_t access_mask;
573 unsigned access_size;
574 unsigned i;
575 MemTxResult r = MEMTX_OK;
577 if (!access_size_min) {
578 access_size_min = 1;
580 if (!access_size_max) {
581 access_size_max = 4;
584 /* FIXME: support unaligned access? */
585 access_size = MAX(MIN(size, access_size_max), access_size_min);
586 access_mask = -1ULL >> (64 - access_size * 8);
587 if (memory_region_big_endian(mr)) {
588 for (i = 0; i < size; i += access_size) {
589 r |= access(mr, addr + i, value, access_size,
590 (size - access_size - i) * 8, access_mask, attrs);
592 } else {
593 for (i = 0; i < size; i += access_size) {
594 r |= access(mr, addr + i, value, access_size, i * 8,
595 access_mask, attrs);
598 return r;
601 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
603 AddressSpace *as;
605 while (mr->container) {
606 mr = mr->container;
608 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
609 if (mr == as->root) {
610 return as;
613 return NULL;
616 /* Render a memory region into the global view. Ranges in @view obscure
617 * ranges in @mr.
619 static void render_memory_region(FlatView *view,
620 MemoryRegion *mr,
621 Int128 base,
622 AddrRange clip,
623 bool readonly)
625 MemoryRegion *subregion;
626 unsigned i;
627 hwaddr offset_in_region;
628 Int128 remain;
629 Int128 now;
630 FlatRange fr;
631 AddrRange tmp;
633 if (!mr->enabled) {
634 return;
637 int128_addto(&base, int128_make64(mr->addr));
638 readonly |= mr->readonly;
640 tmp = addrrange_make(base, mr->size);
642 if (!addrrange_intersects(tmp, clip)) {
643 return;
646 clip = addrrange_intersection(tmp, clip);
648 if (mr->alias) {
649 int128_subfrom(&base, int128_make64(mr->alias->addr));
650 int128_subfrom(&base, int128_make64(mr->alias_offset));
651 render_memory_region(view, mr->alias, base, clip, readonly);
652 return;
655 /* Render subregions in priority order. */
656 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
657 render_memory_region(view, subregion, base, clip, readonly);
660 if (!mr->terminates) {
661 return;
664 offset_in_region = int128_get64(int128_sub(clip.start, base));
665 base = clip.start;
666 remain = clip.size;
668 fr.mr = mr;
669 fr.dirty_log_mask = memory_region_get_dirty_log_mask(mr);
670 fr.romd_mode = mr->romd_mode;
671 fr.readonly = readonly;
673 /* Render the region itself into any gaps left by the current view. */
674 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
675 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
676 continue;
678 if (int128_lt(base, view->ranges[i].addr.start)) {
679 now = int128_min(remain,
680 int128_sub(view->ranges[i].addr.start, base));
681 fr.offset_in_region = offset_in_region;
682 fr.addr = addrrange_make(base, now);
683 flatview_insert(view, i, &fr);
684 ++i;
685 int128_addto(&base, now);
686 offset_in_region += int128_get64(now);
687 int128_subfrom(&remain, now);
689 now = int128_sub(int128_min(int128_add(base, remain),
690 addrrange_end(view->ranges[i].addr)),
691 base);
692 int128_addto(&base, now);
693 offset_in_region += int128_get64(now);
694 int128_subfrom(&remain, now);
696 if (int128_nz(remain)) {
697 fr.offset_in_region = offset_in_region;
698 fr.addr = addrrange_make(base, remain);
699 flatview_insert(view, i, &fr);
703 /* Render a memory topology into a list of disjoint absolute ranges. */
704 static FlatView *generate_memory_topology(MemoryRegion *mr)
706 FlatView *view;
708 view = g_new(FlatView, 1);
709 flatview_init(view);
711 if (mr) {
712 render_memory_region(view, mr, int128_zero(),
713 addrrange_make(int128_zero(), int128_2_64()), false);
715 flatview_simplify(view);
717 return view;
720 static void address_space_add_del_ioeventfds(AddressSpace *as,
721 MemoryRegionIoeventfd *fds_new,
722 unsigned fds_new_nb,
723 MemoryRegionIoeventfd *fds_old,
724 unsigned fds_old_nb)
726 unsigned iold, inew;
727 MemoryRegionIoeventfd *fd;
728 MemoryRegionSection section;
730 /* Generate a symmetric difference of the old and new fd sets, adding
731 * and deleting as necessary.
734 iold = inew = 0;
735 while (iold < fds_old_nb || inew < fds_new_nb) {
736 if (iold < fds_old_nb
737 && (inew == fds_new_nb
738 || memory_region_ioeventfd_before(fds_old[iold],
739 fds_new[inew]))) {
740 fd = &fds_old[iold];
741 section = (MemoryRegionSection) {
742 .address_space = as,
743 .offset_within_address_space = int128_get64(fd->addr.start),
744 .size = fd->addr.size,
746 MEMORY_LISTENER_CALL(as, eventfd_del, Forward, &section,
747 fd->match_data, fd->data, fd->e);
748 ++iold;
749 } else if (inew < fds_new_nb
750 && (iold == fds_old_nb
751 || memory_region_ioeventfd_before(fds_new[inew],
752 fds_old[iold]))) {
753 fd = &fds_new[inew];
754 section = (MemoryRegionSection) {
755 .address_space = as,
756 .offset_within_address_space = int128_get64(fd->addr.start),
757 .size = fd->addr.size,
759 MEMORY_LISTENER_CALL(as, eventfd_add, Reverse, &section,
760 fd->match_data, fd->data, fd->e);
761 ++inew;
762 } else {
763 ++iold;
764 ++inew;
769 static FlatView *address_space_get_flatview(AddressSpace *as)
771 FlatView *view;
773 rcu_read_lock();
774 view = atomic_rcu_read(&as->current_map);
775 flatview_ref(view);
776 rcu_read_unlock();
777 return view;
780 static void address_space_update_ioeventfds(AddressSpace *as)
782 FlatView *view;
783 FlatRange *fr;
784 unsigned ioeventfd_nb = 0;
785 MemoryRegionIoeventfd *ioeventfds = NULL;
786 AddrRange tmp;
787 unsigned i;
789 view = address_space_get_flatview(as);
790 FOR_EACH_FLAT_RANGE(fr, view) {
791 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
792 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
793 int128_sub(fr->addr.start,
794 int128_make64(fr->offset_in_region)));
795 if (addrrange_intersects(fr->addr, tmp)) {
796 ++ioeventfd_nb;
797 ioeventfds = g_realloc(ioeventfds,
798 ioeventfd_nb * sizeof(*ioeventfds));
799 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
800 ioeventfds[ioeventfd_nb-1].addr = tmp;
805 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
806 as->ioeventfds, as->ioeventfd_nb);
808 g_free(as->ioeventfds);
809 as->ioeventfds = ioeventfds;
810 as->ioeventfd_nb = ioeventfd_nb;
811 flatview_unref(view);
814 static void address_space_update_topology_pass(AddressSpace *as,
815 const FlatView *old_view,
816 const FlatView *new_view,
817 bool adding)
819 unsigned iold, inew;
820 FlatRange *frold, *frnew;
822 /* Generate a symmetric difference of the old and new memory maps.
823 * Kill ranges in the old map, and instantiate ranges in the new map.
825 iold = inew = 0;
826 while (iold < old_view->nr || inew < new_view->nr) {
827 if (iold < old_view->nr) {
828 frold = &old_view->ranges[iold];
829 } else {
830 frold = NULL;
832 if (inew < new_view->nr) {
833 frnew = &new_view->ranges[inew];
834 } else {
835 frnew = NULL;
838 if (frold
839 && (!frnew
840 || int128_lt(frold->addr.start, frnew->addr.start)
841 || (int128_eq(frold->addr.start, frnew->addr.start)
842 && !flatrange_equal(frold, frnew)))) {
843 /* In old but not in new, or in both but attributes changed. */
845 if (!adding) {
846 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
849 ++iold;
850 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
851 /* In both and unchanged (except logging may have changed) */
853 if (adding) {
854 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
855 if (frnew->dirty_log_mask & ~frold->dirty_log_mask) {
856 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start,
857 frold->dirty_log_mask,
858 frnew->dirty_log_mask);
860 if (frold->dirty_log_mask & ~frnew->dirty_log_mask) {
861 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop,
862 frold->dirty_log_mask,
863 frnew->dirty_log_mask);
867 ++iold;
868 ++inew;
869 } else {
870 /* In new */
872 if (adding) {
873 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
876 ++inew;
882 static void address_space_update_topology(AddressSpace *as)
884 FlatView *old_view = address_space_get_flatview(as);
885 FlatView *new_view = generate_memory_topology(as->root);
887 address_space_update_topology_pass(as, old_view, new_view, false);
888 address_space_update_topology_pass(as, old_view, new_view, true);
890 /* Writes are protected by the BQL. */
891 atomic_rcu_set(&as->current_map, new_view);
892 call_rcu(old_view, flatview_unref, rcu);
894 /* Note that all the old MemoryRegions are still alive up to this
895 * point. This relieves most MemoryListeners from the need to
896 * ref/unref the MemoryRegions they get---unless they use them
897 * outside the iothread mutex, in which case precise reference
898 * counting is necessary.
900 flatview_unref(old_view);
902 address_space_update_ioeventfds(as);
905 void memory_region_transaction_begin(void)
907 qemu_flush_coalesced_mmio_buffer();
908 ++memory_region_transaction_depth;
911 void memory_region_transaction_commit(void)
913 AddressSpace *as;
915 assert(memory_region_transaction_depth);
916 assert(qemu_mutex_iothread_locked());
918 --memory_region_transaction_depth;
919 if (!memory_region_transaction_depth) {
920 if (memory_region_update_pending) {
921 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
923 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
924 address_space_update_topology(as);
926 memory_region_update_pending = false;
927 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
928 } else if (ioeventfd_update_pending) {
929 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
930 address_space_update_ioeventfds(as);
932 ioeventfd_update_pending = false;
937 static void memory_region_destructor_none(MemoryRegion *mr)
941 static void memory_region_destructor_ram(MemoryRegion *mr)
943 qemu_ram_free(mr->ram_block);
946 static bool memory_region_need_escape(char c)
948 return c == '/' || c == '[' || c == '\\' || c == ']';
951 static char *memory_region_escape_name(const char *name)
953 const char *p;
954 char *escaped, *q;
955 uint8_t c;
956 size_t bytes = 0;
958 for (p = name; *p; p++) {
959 bytes += memory_region_need_escape(*p) ? 4 : 1;
961 if (bytes == p - name) {
962 return g_memdup(name, bytes + 1);
965 escaped = g_malloc(bytes + 1);
966 for (p = name, q = escaped; *p; p++) {
967 c = *p;
968 if (unlikely(memory_region_need_escape(c))) {
969 *q++ = '\\';
970 *q++ = 'x';
971 *q++ = "0123456789abcdef"[c >> 4];
972 c = "0123456789abcdef"[c & 15];
974 *q++ = c;
976 *q = 0;
977 return escaped;
980 static void memory_region_do_init(MemoryRegion *mr,
981 Object *owner,
982 const char *name,
983 uint64_t size)
985 mr->size = int128_make64(size);
986 if (size == UINT64_MAX) {
987 mr->size = int128_2_64();
989 mr->name = g_strdup(name);
990 mr->owner = owner;
991 mr->ram_block = NULL;
993 if (name) {
994 char *escaped_name = memory_region_escape_name(name);
995 char *name_array = g_strdup_printf("%s[*]", escaped_name);
997 if (!owner) {
998 owner = container_get(qdev_get_machine(), "/unattached");
1001 object_property_add_child(owner, name_array, OBJECT(mr), &error_abort);
1002 object_unref(OBJECT(mr));
1003 g_free(name_array);
1004 g_free(escaped_name);
1008 void memory_region_init(MemoryRegion *mr,
1009 Object *owner,
1010 const char *name,
1011 uint64_t size)
1013 object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION);
1014 memory_region_do_init(mr, owner, name, size);
1017 static void memory_region_get_addr(Object *obj, Visitor *v, const char *name,
1018 void *opaque, Error **errp)
1020 MemoryRegion *mr = MEMORY_REGION(obj);
1021 uint64_t value = mr->addr;
1023 visit_type_uint64(v, name, &value, errp);
1026 static void memory_region_get_container(Object *obj, Visitor *v,
1027 const char *name, void *opaque,
1028 Error **errp)
1030 MemoryRegion *mr = MEMORY_REGION(obj);
1031 gchar *path = (gchar *)"";
1033 if (mr->container) {
1034 path = object_get_canonical_path(OBJECT(mr->container));
1036 visit_type_str(v, name, &path, errp);
1037 if (mr->container) {
1038 g_free(path);
1042 static Object *memory_region_resolve_container(Object *obj, void *opaque,
1043 const char *part)
1045 MemoryRegion *mr = MEMORY_REGION(obj);
1047 return OBJECT(mr->container);
1050 static void memory_region_get_priority(Object *obj, Visitor *v,
1051 const char *name, void *opaque,
1052 Error **errp)
1054 MemoryRegion *mr = MEMORY_REGION(obj);
1055 int32_t value = mr->priority;
1057 visit_type_int32(v, name, &value, errp);
1060 static void memory_region_get_size(Object *obj, Visitor *v, const char *name,
1061 void *opaque, Error **errp)
1063 MemoryRegion *mr = MEMORY_REGION(obj);
1064 uint64_t value = memory_region_size(mr);
1066 visit_type_uint64(v, name, &value, errp);
1069 static void memory_region_initfn(Object *obj)
1071 MemoryRegion *mr = MEMORY_REGION(obj);
1072 ObjectProperty *op;
1074 mr->ops = &unassigned_mem_ops;
1075 mr->enabled = true;
1076 mr->romd_mode = true;
1077 mr->global_locking = true;
1078 mr->destructor = memory_region_destructor_none;
1079 QTAILQ_INIT(&mr->subregions);
1080 QTAILQ_INIT(&mr->coalesced);
1082 op = object_property_add(OBJECT(mr), "container",
1083 "link<" TYPE_MEMORY_REGION ">",
1084 memory_region_get_container,
1085 NULL, /* memory_region_set_container */
1086 NULL, NULL, &error_abort);
1087 op->resolve = memory_region_resolve_container;
1089 object_property_add(OBJECT(mr), "addr", "uint64",
1090 memory_region_get_addr,
1091 NULL, /* memory_region_set_addr */
1092 NULL, NULL, &error_abort);
1093 object_property_add(OBJECT(mr), "priority", "uint32",
1094 memory_region_get_priority,
1095 NULL, /* memory_region_set_priority */
1096 NULL, NULL, &error_abort);
1097 object_property_add(OBJECT(mr), "size", "uint64",
1098 memory_region_get_size,
1099 NULL, /* memory_region_set_size, */
1100 NULL, NULL, &error_abort);
1103 static void iommu_memory_region_initfn(Object *obj)
1105 MemoryRegion *mr = MEMORY_REGION(obj);
1107 mr->is_iommu = true;
1110 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
1111 unsigned size)
1113 #ifdef DEBUG_UNASSIGNED
1114 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
1115 #endif
1116 if (current_cpu != NULL) {
1117 cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
1119 return 0;
1122 static void unassigned_mem_write(void *opaque, hwaddr addr,
1123 uint64_t val, unsigned size)
1125 #ifdef DEBUG_UNASSIGNED
1126 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
1127 #endif
1128 if (current_cpu != NULL) {
1129 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
1133 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
1134 unsigned size, bool is_write)
1136 return false;
1139 const MemoryRegionOps unassigned_mem_ops = {
1140 .valid.accepts = unassigned_mem_accepts,
1141 .endianness = DEVICE_NATIVE_ENDIAN,
1144 static uint64_t memory_region_ram_device_read(void *opaque,
1145 hwaddr addr, unsigned size)
1147 MemoryRegion *mr = opaque;
1148 uint64_t data = (uint64_t)~0;
1150 switch (size) {
1151 case 1:
1152 data = *(uint8_t *)(mr->ram_block->host + addr);
1153 break;
1154 case 2:
1155 data = *(uint16_t *)(mr->ram_block->host + addr);
1156 break;
1157 case 4:
1158 data = *(uint32_t *)(mr->ram_block->host + addr);
1159 break;
1160 case 8:
1161 data = *(uint64_t *)(mr->ram_block->host + addr);
1162 break;
1165 trace_memory_region_ram_device_read(get_cpu_index(), mr, addr, data, size);
1167 return data;
1170 static void memory_region_ram_device_write(void *opaque, hwaddr addr,
1171 uint64_t data, unsigned size)
1173 MemoryRegion *mr = opaque;
1175 trace_memory_region_ram_device_write(get_cpu_index(), mr, addr, data, size);
1177 switch (size) {
1178 case 1:
1179 *(uint8_t *)(mr->ram_block->host + addr) = (uint8_t)data;
1180 break;
1181 case 2:
1182 *(uint16_t *)(mr->ram_block->host + addr) = (uint16_t)data;
1183 break;
1184 case 4:
1185 *(uint32_t *)(mr->ram_block->host + addr) = (uint32_t)data;
1186 break;
1187 case 8:
1188 *(uint64_t *)(mr->ram_block->host + addr) = data;
1189 break;
1193 static const MemoryRegionOps ram_device_mem_ops = {
1194 .read = memory_region_ram_device_read,
1195 .write = memory_region_ram_device_write,
1196 .endianness = DEVICE_HOST_ENDIAN,
1197 .valid = {
1198 .min_access_size = 1,
1199 .max_access_size = 8,
1200 .unaligned = true,
1202 .impl = {
1203 .min_access_size = 1,
1204 .max_access_size = 8,
1205 .unaligned = true,
1209 bool memory_region_access_valid(MemoryRegion *mr,
1210 hwaddr addr,
1211 unsigned size,
1212 bool is_write)
1214 int access_size_min, access_size_max;
1215 int access_size, i;
1217 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
1218 return false;
1221 if (!mr->ops->valid.accepts) {
1222 return true;
1225 access_size_min = mr->ops->valid.min_access_size;
1226 if (!mr->ops->valid.min_access_size) {
1227 access_size_min = 1;
1230 access_size_max = mr->ops->valid.max_access_size;
1231 if (!mr->ops->valid.max_access_size) {
1232 access_size_max = 4;
1235 access_size = MAX(MIN(size, access_size_max), access_size_min);
1236 for (i = 0; i < size; i += access_size) {
1237 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
1238 is_write)) {
1239 return false;
1243 return true;
1246 static MemTxResult memory_region_dispatch_read1(MemoryRegion *mr,
1247 hwaddr addr,
1248 uint64_t *pval,
1249 unsigned size,
1250 MemTxAttrs attrs)
1252 *pval = 0;
1254 if (mr->ops->read) {
1255 return access_with_adjusted_size(addr, pval, size,
1256 mr->ops->impl.min_access_size,
1257 mr->ops->impl.max_access_size,
1258 memory_region_read_accessor,
1259 mr, attrs);
1260 } else if (mr->ops->read_with_attrs) {
1261 return access_with_adjusted_size(addr, pval, size,
1262 mr->ops->impl.min_access_size,
1263 mr->ops->impl.max_access_size,
1264 memory_region_read_with_attrs_accessor,
1265 mr, attrs);
1266 } else {
1267 return access_with_adjusted_size(addr, pval, size, 1, 4,
1268 memory_region_oldmmio_read_accessor,
1269 mr, attrs);
1273 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1274 hwaddr addr,
1275 uint64_t *pval,
1276 unsigned size,
1277 MemTxAttrs attrs)
1279 MemTxResult r;
1281 if (!memory_region_access_valid(mr, addr, size, false)) {
1282 *pval = unassigned_mem_read(mr, addr, size);
1283 return MEMTX_DECODE_ERROR;
1286 r = memory_region_dispatch_read1(mr, addr, pval, size, attrs);
1287 adjust_endianness(mr, pval, size);
1288 return r;
1291 /* Return true if an eventfd was signalled */
1292 static bool memory_region_dispatch_write_eventfds(MemoryRegion *mr,
1293 hwaddr addr,
1294 uint64_t data,
1295 unsigned size,
1296 MemTxAttrs attrs)
1298 MemoryRegionIoeventfd ioeventfd = {
1299 .addr = addrrange_make(int128_make64(addr), int128_make64(size)),
1300 .data = data,
1302 unsigned i;
1304 for (i = 0; i < mr->ioeventfd_nb; i++) {
1305 ioeventfd.match_data = mr->ioeventfds[i].match_data;
1306 ioeventfd.e = mr->ioeventfds[i].e;
1308 if (memory_region_ioeventfd_equal(ioeventfd, mr->ioeventfds[i])) {
1309 event_notifier_set(ioeventfd.e);
1310 return true;
1314 return false;
1317 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1318 hwaddr addr,
1319 uint64_t data,
1320 unsigned size,
1321 MemTxAttrs attrs)
1323 if (!memory_region_access_valid(mr, addr, size, true)) {
1324 unassigned_mem_write(mr, addr, data, size);
1325 return MEMTX_DECODE_ERROR;
1328 adjust_endianness(mr, &data, size);
1330 if ((!kvm_eventfds_enabled()) &&
1331 memory_region_dispatch_write_eventfds(mr, addr, data, size, attrs)) {
1332 return MEMTX_OK;
1335 if (mr->ops->write) {
1336 return access_with_adjusted_size(addr, &data, size,
1337 mr->ops->impl.min_access_size,
1338 mr->ops->impl.max_access_size,
1339 memory_region_write_accessor, mr,
1340 attrs);
1341 } else if (mr->ops->write_with_attrs) {
1342 return
1343 access_with_adjusted_size(addr, &data, size,
1344 mr->ops->impl.min_access_size,
1345 mr->ops->impl.max_access_size,
1346 memory_region_write_with_attrs_accessor,
1347 mr, attrs);
1348 } else {
1349 return access_with_adjusted_size(addr, &data, size, 1, 4,
1350 memory_region_oldmmio_write_accessor,
1351 mr, attrs);
1355 void memory_region_init_io(MemoryRegion *mr,
1356 Object *owner,
1357 const MemoryRegionOps *ops,
1358 void *opaque,
1359 const char *name,
1360 uint64_t size)
1362 memory_region_init(mr, owner, name, size);
1363 mr->ops = ops ? ops : &unassigned_mem_ops;
1364 mr->opaque = opaque;
1365 mr->terminates = true;
1368 void memory_region_init_ram(MemoryRegion *mr,
1369 Object *owner,
1370 const char *name,
1371 uint64_t size,
1372 Error **errp)
1374 memory_region_init(mr, owner, name, size);
1375 mr->ram = true;
1376 mr->terminates = true;
1377 mr->destructor = memory_region_destructor_ram;
1378 mr->ram_block = qemu_ram_alloc(size, mr, errp);
1379 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1382 void memory_region_init_resizeable_ram(MemoryRegion *mr,
1383 Object *owner,
1384 const char *name,
1385 uint64_t size,
1386 uint64_t max_size,
1387 void (*resized)(const char*,
1388 uint64_t length,
1389 void *host),
1390 Error **errp)
1392 memory_region_init(mr, owner, name, size);
1393 mr->ram = true;
1394 mr->terminates = true;
1395 mr->destructor = memory_region_destructor_ram;
1396 mr->ram_block = qemu_ram_alloc_resizeable(size, max_size, resized,
1397 mr, errp);
1398 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1401 #ifdef __linux__
1402 void memory_region_init_ram_from_file(MemoryRegion *mr,
1403 struct Object *owner,
1404 const char *name,
1405 uint64_t size,
1406 bool share,
1407 const char *path,
1408 Error **errp)
1410 memory_region_init(mr, owner, name, size);
1411 mr->ram = true;
1412 mr->terminates = true;
1413 mr->destructor = memory_region_destructor_ram;
1414 mr->ram_block = qemu_ram_alloc_from_file(size, mr, share, path, errp);
1415 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1418 void memory_region_init_ram_from_fd(MemoryRegion *mr,
1419 struct Object *owner,
1420 const char *name,
1421 uint64_t size,
1422 bool share,
1423 int fd,
1424 Error **errp)
1426 memory_region_init(mr, owner, name, size);
1427 mr->ram = true;
1428 mr->terminates = true;
1429 mr->destructor = memory_region_destructor_ram;
1430 mr->ram_block = qemu_ram_alloc_from_fd(size, mr, share, fd, errp);
1431 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1433 #endif
1435 void memory_region_init_ram_ptr(MemoryRegion *mr,
1436 Object *owner,
1437 const char *name,
1438 uint64_t size,
1439 void *ptr)
1441 memory_region_init(mr, owner, name, size);
1442 mr->ram = true;
1443 mr->terminates = true;
1444 mr->destructor = memory_region_destructor_ram;
1445 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1447 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1448 assert(ptr != NULL);
1449 mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
1452 void memory_region_init_ram_device_ptr(MemoryRegion *mr,
1453 Object *owner,
1454 const char *name,
1455 uint64_t size,
1456 void *ptr)
1458 memory_region_init_ram_ptr(mr, owner, name, size, ptr);
1459 mr->ram_device = true;
1460 mr->ops = &ram_device_mem_ops;
1461 mr->opaque = mr;
1464 void memory_region_init_alias(MemoryRegion *mr,
1465 Object *owner,
1466 const char *name,
1467 MemoryRegion *orig,
1468 hwaddr offset,
1469 uint64_t size)
1471 memory_region_init(mr, owner, name, size);
1472 mr->alias = orig;
1473 mr->alias_offset = offset;
1476 void memory_region_init_rom(MemoryRegion *mr,
1477 struct Object *owner,
1478 const char *name,
1479 uint64_t size,
1480 Error **errp)
1482 memory_region_init(mr, owner, name, size);
1483 mr->ram = true;
1484 mr->readonly = true;
1485 mr->terminates = true;
1486 mr->destructor = memory_region_destructor_ram;
1487 mr->ram_block = qemu_ram_alloc(size, mr, errp);
1488 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1491 void memory_region_init_rom_device(MemoryRegion *mr,
1492 Object *owner,
1493 const MemoryRegionOps *ops,
1494 void *opaque,
1495 const char *name,
1496 uint64_t size,
1497 Error **errp)
1499 assert(ops);
1500 memory_region_init(mr, owner, name, size);
1501 mr->ops = ops;
1502 mr->opaque = opaque;
1503 mr->terminates = true;
1504 mr->rom_device = true;
1505 mr->destructor = memory_region_destructor_ram;
1506 mr->ram_block = qemu_ram_alloc(size, mr, errp);
1509 void memory_region_init_iommu(void *_iommu_mr,
1510 size_t instance_size,
1511 const char *mrtypename,
1512 Object *owner,
1513 const char *name,
1514 uint64_t size)
1516 struct IOMMUMemoryRegion *iommu_mr;
1517 struct MemoryRegion *mr;
1519 object_initialize(_iommu_mr, instance_size, mrtypename);
1520 mr = MEMORY_REGION(_iommu_mr);
1521 memory_region_do_init(mr, owner, name, size);
1522 iommu_mr = IOMMU_MEMORY_REGION(mr);
1523 mr->terminates = true; /* then re-forwards */
1524 QLIST_INIT(&iommu_mr->iommu_notify);
1525 iommu_mr->iommu_notify_flags = IOMMU_NOTIFIER_NONE;
1528 static void memory_region_finalize(Object *obj)
1530 MemoryRegion *mr = MEMORY_REGION(obj);
1532 assert(!mr->container);
1534 /* We know the region is not visible in any address space (it
1535 * does not have a container and cannot be a root either because
1536 * it has no references, so we can blindly clear mr->enabled.
1537 * memory_region_set_enabled instead could trigger a transaction
1538 * and cause an infinite loop.
1540 mr->enabled = false;
1541 memory_region_transaction_begin();
1542 while (!QTAILQ_EMPTY(&mr->subregions)) {
1543 MemoryRegion *subregion = QTAILQ_FIRST(&mr->subregions);
1544 memory_region_del_subregion(mr, subregion);
1546 memory_region_transaction_commit();
1548 mr->destructor(mr);
1549 memory_region_clear_coalescing(mr);
1550 g_free((char *)mr->name);
1551 g_free(mr->ioeventfds);
1554 Object *memory_region_owner(MemoryRegion *mr)
1556 Object *obj = OBJECT(mr);
1557 return obj->parent;
1560 void memory_region_ref(MemoryRegion *mr)
1562 /* MMIO callbacks most likely will access data that belongs
1563 * to the owner, hence the need to ref/unref the owner whenever
1564 * the memory region is in use.
1566 * The memory region is a child of its owner. As long as the
1567 * owner doesn't call unparent itself on the memory region,
1568 * ref-ing the owner will also keep the memory region alive.
1569 * Memory regions without an owner are supposed to never go away;
1570 * we do not ref/unref them because it slows down DMA sensibly.
1572 if (mr && mr->owner) {
1573 object_ref(mr->owner);
1577 void memory_region_unref(MemoryRegion *mr)
1579 if (mr && mr->owner) {
1580 object_unref(mr->owner);
1584 uint64_t memory_region_size(MemoryRegion *mr)
1586 if (int128_eq(mr->size, int128_2_64())) {
1587 return UINT64_MAX;
1589 return int128_get64(mr->size);
1592 const char *memory_region_name(const MemoryRegion *mr)
1594 if (!mr->name) {
1595 ((MemoryRegion *)mr)->name =
1596 object_get_canonical_path_component(OBJECT(mr));
1598 return mr->name;
1601 bool memory_region_is_ram_device(MemoryRegion *mr)
1603 return mr->ram_device;
1606 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
1608 uint8_t mask = mr->dirty_log_mask;
1609 if (global_dirty_log && mr->ram_block) {
1610 mask |= (1 << DIRTY_MEMORY_MIGRATION);
1612 return mask;
1615 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client)
1617 return memory_region_get_dirty_log_mask(mr) & (1 << client);
1620 static void memory_region_update_iommu_notify_flags(IOMMUMemoryRegion *iommu_mr)
1622 IOMMUNotifierFlag flags = IOMMU_NOTIFIER_NONE;
1623 IOMMUNotifier *iommu_notifier;
1624 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1626 IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) {
1627 flags |= iommu_notifier->notifier_flags;
1630 if (flags != iommu_mr->iommu_notify_flags && imrc->notify_flag_changed) {
1631 imrc->notify_flag_changed(iommu_mr,
1632 iommu_mr->iommu_notify_flags,
1633 flags);
1636 iommu_mr->iommu_notify_flags = flags;
1639 void memory_region_register_iommu_notifier(MemoryRegion *mr,
1640 IOMMUNotifier *n)
1642 IOMMUMemoryRegion *iommu_mr;
1644 if (mr->alias) {
1645 memory_region_register_iommu_notifier(mr->alias, n);
1646 return;
1649 /* We need to register for at least one bitfield */
1650 iommu_mr = IOMMU_MEMORY_REGION(mr);
1651 assert(n->notifier_flags != IOMMU_NOTIFIER_NONE);
1652 assert(n->start <= n->end);
1653 QLIST_INSERT_HEAD(&iommu_mr->iommu_notify, n, node);
1654 memory_region_update_iommu_notify_flags(iommu_mr);
1657 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr)
1659 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1661 if (imrc->get_min_page_size) {
1662 return imrc->get_min_page_size(iommu_mr);
1664 return TARGET_PAGE_SIZE;
1667 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
1669 MemoryRegion *mr = MEMORY_REGION(iommu_mr);
1670 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
1671 hwaddr addr, granularity;
1672 IOMMUTLBEntry iotlb;
1674 /* If the IOMMU has its own replay callback, override */
1675 if (imrc->replay) {
1676 imrc->replay(iommu_mr, n);
1677 return;
1680 granularity = memory_region_iommu_get_min_page_size(iommu_mr);
1682 for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
1683 iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE);
1684 if (iotlb.perm != IOMMU_NONE) {
1685 n->notify(n, &iotlb);
1688 /* if (2^64 - MR size) < granularity, it's possible to get an
1689 * infinite loop here. This should catch such a wraparound */
1690 if ((addr + granularity) < addr) {
1691 break;
1696 void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr)
1698 IOMMUNotifier *notifier;
1700 IOMMU_NOTIFIER_FOREACH(notifier, iommu_mr) {
1701 memory_region_iommu_replay(iommu_mr, notifier);
1705 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1706 IOMMUNotifier *n)
1708 IOMMUMemoryRegion *iommu_mr;
1710 if (mr->alias) {
1711 memory_region_unregister_iommu_notifier(mr->alias, n);
1712 return;
1714 QLIST_REMOVE(n, node);
1715 iommu_mr = IOMMU_MEMORY_REGION(mr);
1716 memory_region_update_iommu_notify_flags(iommu_mr);
1719 void memory_region_notify_one(IOMMUNotifier *notifier,
1720 IOMMUTLBEntry *entry)
1722 IOMMUNotifierFlag request_flags;
1725 * Skip the notification if the notification does not overlap
1726 * with registered range.
1728 if (notifier->start > entry->iova + entry->addr_mask + 1 ||
1729 notifier->end < entry->iova) {
1730 return;
1733 if (entry->perm & IOMMU_RW) {
1734 request_flags = IOMMU_NOTIFIER_MAP;
1735 } else {
1736 request_flags = IOMMU_NOTIFIER_UNMAP;
1739 if (notifier->notifier_flags & request_flags) {
1740 notifier->notify(notifier, entry);
1744 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1745 IOMMUTLBEntry entry)
1747 IOMMUNotifier *iommu_notifier;
1749 assert(memory_region_is_iommu(MEMORY_REGION(iommu_mr)));
1751 IOMMU_NOTIFIER_FOREACH(iommu_notifier, iommu_mr) {
1752 memory_region_notify_one(iommu_notifier, &entry);
1756 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1758 uint8_t mask = 1 << client;
1759 uint8_t old_logging;
1761 assert(client == DIRTY_MEMORY_VGA);
1762 old_logging = mr->vga_logging_count;
1763 mr->vga_logging_count += log ? 1 : -1;
1764 if (!!old_logging == !!mr->vga_logging_count) {
1765 return;
1768 memory_region_transaction_begin();
1769 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1770 memory_region_update_pending |= mr->enabled;
1771 memory_region_transaction_commit();
1774 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1775 hwaddr size, unsigned client)
1777 assert(mr->ram_block);
1778 return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr) + addr,
1779 size, client);
1782 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1783 hwaddr size)
1785 assert(mr->ram_block);
1786 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr) + addr,
1787 size,
1788 memory_region_get_dirty_log_mask(mr));
1791 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
1792 hwaddr size, unsigned client)
1794 assert(mr->ram_block);
1795 return cpu_physical_memory_test_and_clear_dirty(
1796 memory_region_get_ram_addr(mr) + addr, size, client);
1799 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
1800 hwaddr addr,
1801 hwaddr size,
1802 unsigned client)
1804 assert(mr->ram_block);
1805 return cpu_physical_memory_snapshot_and_clear_dirty(
1806 memory_region_get_ram_addr(mr) + addr, size, client);
1809 bool memory_region_snapshot_get_dirty(MemoryRegion *mr, DirtyBitmapSnapshot *snap,
1810 hwaddr addr, hwaddr size)
1812 assert(mr->ram_block);
1813 return cpu_physical_memory_snapshot_get_dirty(snap,
1814 memory_region_get_ram_addr(mr) + addr, size);
1817 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1819 MemoryListener *listener;
1820 AddressSpace *as;
1821 FlatView *view;
1822 FlatRange *fr;
1824 /* If the same address space has multiple log_sync listeners, we
1825 * visit that address space's FlatView multiple times. But because
1826 * log_sync listeners are rare, it's still cheaper than walking each
1827 * address space once.
1829 QTAILQ_FOREACH(listener, &memory_listeners, link) {
1830 if (!listener->log_sync) {
1831 continue;
1833 as = listener->address_space;
1834 view = address_space_get_flatview(as);
1835 FOR_EACH_FLAT_RANGE(fr, view) {
1836 if (fr->mr == mr) {
1837 MemoryRegionSection mrs = section_from_flat_range(fr, as);
1838 listener->log_sync(listener, &mrs);
1841 flatview_unref(view);
1845 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1847 if (mr->readonly != readonly) {
1848 memory_region_transaction_begin();
1849 mr->readonly = readonly;
1850 memory_region_update_pending |= mr->enabled;
1851 memory_region_transaction_commit();
1855 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
1857 if (mr->romd_mode != romd_mode) {
1858 memory_region_transaction_begin();
1859 mr->romd_mode = romd_mode;
1860 memory_region_update_pending |= mr->enabled;
1861 memory_region_transaction_commit();
1865 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1866 hwaddr size, unsigned client)
1868 assert(mr->ram_block);
1869 cpu_physical_memory_test_and_clear_dirty(
1870 memory_region_get_ram_addr(mr) + addr, size, client);
1873 int memory_region_get_fd(MemoryRegion *mr)
1875 int fd;
1877 rcu_read_lock();
1878 while (mr->alias) {
1879 mr = mr->alias;
1881 fd = mr->ram_block->fd;
1882 rcu_read_unlock();
1884 return fd;
1887 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1889 void *ptr;
1890 uint64_t offset = 0;
1892 rcu_read_lock();
1893 while (mr->alias) {
1894 offset += mr->alias_offset;
1895 mr = mr->alias;
1897 assert(mr->ram_block);
1898 ptr = qemu_map_ram_ptr(mr->ram_block, offset);
1899 rcu_read_unlock();
1901 return ptr;
1904 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset)
1906 RAMBlock *block;
1908 block = qemu_ram_block_from_host(ptr, false, offset);
1909 if (!block) {
1910 return NULL;
1913 return block->mr;
1916 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1918 return mr->ram_block ? mr->ram_block->offset : RAM_ADDR_INVALID;
1921 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp)
1923 assert(mr->ram_block);
1925 qemu_ram_resize(mr->ram_block, newsize, errp);
1928 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
1930 FlatView *view;
1931 FlatRange *fr;
1932 CoalescedMemoryRange *cmr;
1933 AddrRange tmp;
1934 MemoryRegionSection section;
1936 view = address_space_get_flatview(as);
1937 FOR_EACH_FLAT_RANGE(fr, view) {
1938 if (fr->mr == mr) {
1939 section = (MemoryRegionSection) {
1940 .address_space = as,
1941 .offset_within_address_space = int128_get64(fr->addr.start),
1942 .size = fr->addr.size,
1945 MEMORY_LISTENER_CALL(as, coalesced_mmio_del, Reverse, &section,
1946 int128_get64(fr->addr.start),
1947 int128_get64(fr->addr.size));
1948 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1949 tmp = addrrange_shift(cmr->addr,
1950 int128_sub(fr->addr.start,
1951 int128_make64(fr->offset_in_region)));
1952 if (!addrrange_intersects(tmp, fr->addr)) {
1953 continue;
1955 tmp = addrrange_intersection(tmp, fr->addr);
1956 MEMORY_LISTENER_CALL(as, coalesced_mmio_add, Forward, &section,
1957 int128_get64(tmp.start),
1958 int128_get64(tmp.size));
1962 flatview_unref(view);
1965 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1967 AddressSpace *as;
1969 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1970 memory_region_update_coalesced_range_as(mr, as);
1974 void memory_region_set_coalescing(MemoryRegion *mr)
1976 memory_region_clear_coalescing(mr);
1977 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1980 void memory_region_add_coalescing(MemoryRegion *mr,
1981 hwaddr offset,
1982 uint64_t size)
1984 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1986 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1987 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1988 memory_region_update_coalesced_range(mr);
1989 memory_region_set_flush_coalesced(mr);
1992 void memory_region_clear_coalescing(MemoryRegion *mr)
1994 CoalescedMemoryRange *cmr;
1995 bool updated = false;
1997 qemu_flush_coalesced_mmio_buffer();
1998 mr->flush_coalesced_mmio = false;
2000 while (!QTAILQ_EMPTY(&mr->coalesced)) {
2001 cmr = QTAILQ_FIRST(&mr->coalesced);
2002 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
2003 g_free(cmr);
2004 updated = true;
2007 if (updated) {
2008 memory_region_update_coalesced_range(mr);
2012 void memory_region_set_flush_coalesced(MemoryRegion *mr)
2014 mr->flush_coalesced_mmio = true;
2017 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
2019 qemu_flush_coalesced_mmio_buffer();
2020 if (QTAILQ_EMPTY(&mr->coalesced)) {
2021 mr->flush_coalesced_mmio = false;
2025 void memory_region_set_global_locking(MemoryRegion *mr)
2027 mr->global_locking = true;
2030 void memory_region_clear_global_locking(MemoryRegion *mr)
2032 mr->global_locking = false;
2035 static bool userspace_eventfd_warning;
2037 void memory_region_add_eventfd(MemoryRegion *mr,
2038 hwaddr addr,
2039 unsigned size,
2040 bool match_data,
2041 uint64_t data,
2042 EventNotifier *e)
2044 MemoryRegionIoeventfd mrfd = {
2045 .addr.start = int128_make64(addr),
2046 .addr.size = int128_make64(size),
2047 .match_data = match_data,
2048 .data = data,
2049 .e = e,
2051 unsigned i;
2053 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
2054 userspace_eventfd_warning))) {
2055 userspace_eventfd_warning = true;
2056 error_report("Using eventfd without MMIO binding in KVM. "
2057 "Suboptimal performance expected");
2060 if (size) {
2061 adjust_endianness(mr, &mrfd.data, size);
2063 memory_region_transaction_begin();
2064 for (i = 0; i < mr->ioeventfd_nb; ++i) {
2065 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
2066 break;
2069 ++mr->ioeventfd_nb;
2070 mr->ioeventfds = g_realloc(mr->ioeventfds,
2071 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
2072 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
2073 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
2074 mr->ioeventfds[i] = mrfd;
2075 ioeventfd_update_pending |= mr->enabled;
2076 memory_region_transaction_commit();
2079 void memory_region_del_eventfd(MemoryRegion *mr,
2080 hwaddr addr,
2081 unsigned size,
2082 bool match_data,
2083 uint64_t data,
2084 EventNotifier *e)
2086 MemoryRegionIoeventfd mrfd = {
2087 .addr.start = int128_make64(addr),
2088 .addr.size = int128_make64(size),
2089 .match_data = match_data,
2090 .data = data,
2091 .e = e,
2093 unsigned i;
2095 if (size) {
2096 adjust_endianness(mr, &mrfd.data, size);
2098 memory_region_transaction_begin();
2099 for (i = 0; i < mr->ioeventfd_nb; ++i) {
2100 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
2101 break;
2104 assert(i != mr->ioeventfd_nb);
2105 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
2106 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
2107 --mr->ioeventfd_nb;
2108 mr->ioeventfds = g_realloc(mr->ioeventfds,
2109 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
2110 ioeventfd_update_pending |= mr->enabled;
2111 memory_region_transaction_commit();
2114 static void memory_region_update_container_subregions(MemoryRegion *subregion)
2116 MemoryRegion *mr = subregion->container;
2117 MemoryRegion *other;
2119 memory_region_transaction_begin();
2121 memory_region_ref(subregion);
2122 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
2123 if (subregion->priority >= other->priority) {
2124 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
2125 goto done;
2128 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
2129 done:
2130 memory_region_update_pending |= mr->enabled && subregion->enabled;
2131 memory_region_transaction_commit();
2134 static void memory_region_add_subregion_common(MemoryRegion *mr,
2135 hwaddr offset,
2136 MemoryRegion *subregion)
2138 assert(!subregion->container);
2139 subregion->container = mr;
2140 subregion->addr = offset;
2141 memory_region_update_container_subregions(subregion);
2144 void memory_region_add_subregion(MemoryRegion *mr,
2145 hwaddr offset,
2146 MemoryRegion *subregion)
2148 subregion->priority = 0;
2149 memory_region_add_subregion_common(mr, offset, subregion);
2152 void memory_region_add_subregion_overlap(MemoryRegion *mr,
2153 hwaddr offset,
2154 MemoryRegion *subregion,
2155 int priority)
2157 subregion->priority = priority;
2158 memory_region_add_subregion_common(mr, offset, subregion);
2161 void memory_region_del_subregion(MemoryRegion *mr,
2162 MemoryRegion *subregion)
2164 memory_region_transaction_begin();
2165 assert(subregion->container == mr);
2166 subregion->container = NULL;
2167 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
2168 memory_region_unref(subregion);
2169 memory_region_update_pending |= mr->enabled && subregion->enabled;
2170 memory_region_transaction_commit();
2173 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
2175 if (enabled == mr->enabled) {
2176 return;
2178 memory_region_transaction_begin();
2179 mr->enabled = enabled;
2180 memory_region_update_pending = true;
2181 memory_region_transaction_commit();
2184 void memory_region_set_size(MemoryRegion *mr, uint64_t size)
2186 Int128 s = int128_make64(size);
2188 if (size == UINT64_MAX) {
2189 s = int128_2_64();
2191 if (int128_eq(s, mr->size)) {
2192 return;
2194 memory_region_transaction_begin();
2195 mr->size = s;
2196 memory_region_update_pending = true;
2197 memory_region_transaction_commit();
2200 static void memory_region_readd_subregion(MemoryRegion *mr)
2202 MemoryRegion *container = mr->container;
2204 if (container) {
2205 memory_region_transaction_begin();
2206 memory_region_ref(mr);
2207 memory_region_del_subregion(container, mr);
2208 mr->container = container;
2209 memory_region_update_container_subregions(mr);
2210 memory_region_unref(mr);
2211 memory_region_transaction_commit();
2215 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
2217 if (addr != mr->addr) {
2218 mr->addr = addr;
2219 memory_region_readd_subregion(mr);
2223 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
2225 assert(mr->alias);
2227 if (offset == mr->alias_offset) {
2228 return;
2231 memory_region_transaction_begin();
2232 mr->alias_offset = offset;
2233 memory_region_update_pending |= mr->enabled;
2234 memory_region_transaction_commit();
2237 uint64_t memory_region_get_alignment(const MemoryRegion *mr)
2239 return mr->align;
2242 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
2244 const AddrRange *addr = addr_;
2245 const FlatRange *fr = fr_;
2247 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
2248 return -1;
2249 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
2250 return 1;
2252 return 0;
2255 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
2257 return bsearch(&addr, view->ranges, view->nr,
2258 sizeof(FlatRange), cmp_flatrange_addr);
2261 bool memory_region_is_mapped(MemoryRegion *mr)
2263 return mr->container ? true : false;
2266 /* Same as memory_region_find, but it does not add a reference to the
2267 * returned region. It must be called from an RCU critical section.
2269 static MemoryRegionSection memory_region_find_rcu(MemoryRegion *mr,
2270 hwaddr addr, uint64_t size)
2272 MemoryRegionSection ret = { .mr = NULL };
2273 MemoryRegion *root;
2274 AddressSpace *as;
2275 AddrRange range;
2276 FlatView *view;
2277 FlatRange *fr;
2279 addr += mr->addr;
2280 for (root = mr; root->container; ) {
2281 root = root->container;
2282 addr += root->addr;
2285 as = memory_region_to_address_space(root);
2286 if (!as) {
2287 return ret;
2289 range = addrrange_make(int128_make64(addr), int128_make64(size));
2291 view = atomic_rcu_read(&as->current_map);
2292 fr = flatview_lookup(view, range);
2293 if (!fr) {
2294 return ret;
2297 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
2298 --fr;
2301 ret.mr = fr->mr;
2302 ret.address_space = as;
2303 range = addrrange_intersection(range, fr->addr);
2304 ret.offset_within_region = fr->offset_in_region;
2305 ret.offset_within_region += int128_get64(int128_sub(range.start,
2306 fr->addr.start));
2307 ret.size = range.size;
2308 ret.offset_within_address_space = int128_get64(range.start);
2309 ret.readonly = fr->readonly;
2310 return ret;
2313 MemoryRegionSection memory_region_find(MemoryRegion *mr,
2314 hwaddr addr, uint64_t size)
2316 MemoryRegionSection ret;
2317 rcu_read_lock();
2318 ret = memory_region_find_rcu(mr, addr, size);
2319 if (ret.mr) {
2320 memory_region_ref(ret.mr);
2322 rcu_read_unlock();
2323 return ret;
2326 bool memory_region_present(MemoryRegion *container, hwaddr addr)
2328 MemoryRegion *mr;
2330 rcu_read_lock();
2331 mr = memory_region_find_rcu(container, addr, 1).mr;
2332 rcu_read_unlock();
2333 return mr && mr != container;
2336 void memory_global_dirty_log_sync(void)
2338 MemoryListener *listener;
2339 AddressSpace *as;
2340 FlatView *view;
2341 FlatRange *fr;
2343 QTAILQ_FOREACH(listener, &memory_listeners, link) {
2344 if (!listener->log_sync) {
2345 continue;
2347 as = listener->address_space;
2348 view = address_space_get_flatview(as);
2349 FOR_EACH_FLAT_RANGE(fr, view) {
2350 if (fr->dirty_log_mask) {
2351 MemoryRegionSection mrs = section_from_flat_range(fr, as);
2352 listener->log_sync(listener, &mrs);
2355 flatview_unref(view);
2359 void memory_global_dirty_log_start(void)
2361 global_dirty_log = true;
2363 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
2365 /* Refresh DIRTY_LOG_MIGRATION bit. */
2366 memory_region_transaction_begin();
2367 memory_region_update_pending = true;
2368 memory_region_transaction_commit();
2371 void memory_global_dirty_log_stop(void)
2373 global_dirty_log = false;
2375 /* Refresh DIRTY_LOG_MIGRATION bit. */
2376 memory_region_transaction_begin();
2377 memory_region_update_pending = true;
2378 memory_region_transaction_commit();
2380 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
2383 static void listener_add_address_space(MemoryListener *listener,
2384 AddressSpace *as)
2386 FlatView *view;
2387 FlatRange *fr;
2389 if (listener->begin) {
2390 listener->begin(listener);
2392 if (global_dirty_log) {
2393 if (listener->log_global_start) {
2394 listener->log_global_start(listener);
2398 view = address_space_get_flatview(as);
2399 FOR_EACH_FLAT_RANGE(fr, view) {
2400 MemoryRegionSection section = {
2401 .mr = fr->mr,
2402 .address_space = as,
2403 .offset_within_region = fr->offset_in_region,
2404 .size = fr->addr.size,
2405 .offset_within_address_space = int128_get64(fr->addr.start),
2406 .readonly = fr->readonly,
2408 if (fr->dirty_log_mask && listener->log_start) {
2409 listener->log_start(listener, &section, 0, fr->dirty_log_mask);
2411 if (listener->region_add) {
2412 listener->region_add(listener, &section);
2415 if (listener->commit) {
2416 listener->commit(listener);
2418 flatview_unref(view);
2421 void memory_listener_register(MemoryListener *listener, AddressSpace *as)
2423 MemoryListener *other = NULL;
2425 listener->address_space = as;
2426 if (QTAILQ_EMPTY(&memory_listeners)
2427 || listener->priority >= QTAILQ_LAST(&memory_listeners,
2428 memory_listeners)->priority) {
2429 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
2430 } else {
2431 QTAILQ_FOREACH(other, &memory_listeners, link) {
2432 if (listener->priority < other->priority) {
2433 break;
2436 QTAILQ_INSERT_BEFORE(other, listener, link);
2439 if (QTAILQ_EMPTY(&as->listeners)
2440 || listener->priority >= QTAILQ_LAST(&as->listeners,
2441 memory_listeners)->priority) {
2442 QTAILQ_INSERT_TAIL(&as->listeners, listener, link_as);
2443 } else {
2444 QTAILQ_FOREACH(other, &as->listeners, link_as) {
2445 if (listener->priority < other->priority) {
2446 break;
2449 QTAILQ_INSERT_BEFORE(other, listener, link_as);
2452 listener_add_address_space(listener, as);
2455 void memory_listener_unregister(MemoryListener *listener)
2457 if (!listener->address_space) {
2458 return;
2461 QTAILQ_REMOVE(&memory_listeners, listener, link);
2462 QTAILQ_REMOVE(&listener->address_space->listeners, listener, link_as);
2463 listener->address_space = NULL;
2466 bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr)
2468 void *host;
2469 unsigned size = 0;
2470 unsigned offset = 0;
2471 Object *new_interface;
2473 if (!mr || !mr->ops->request_ptr) {
2474 return false;
2478 * Avoid an update if the request_ptr call
2479 * memory_region_invalidate_mmio_ptr which seems to be likely when we use
2480 * a cache.
2482 memory_region_transaction_begin();
2484 host = mr->ops->request_ptr(mr->opaque, addr - mr->addr, &size, &offset);
2486 if (!host || !size) {
2487 memory_region_transaction_commit();
2488 return false;
2491 new_interface = object_new("mmio_interface");
2492 qdev_prop_set_uint64(DEVICE(new_interface), "start", offset);
2493 qdev_prop_set_uint64(DEVICE(new_interface), "end", offset + size - 1);
2494 qdev_prop_set_bit(DEVICE(new_interface), "ro", true);
2495 qdev_prop_set_ptr(DEVICE(new_interface), "host_ptr", host);
2496 qdev_prop_set_ptr(DEVICE(new_interface), "subregion", mr);
2497 object_property_set_bool(OBJECT(new_interface), true, "realized", NULL);
2499 memory_region_transaction_commit();
2500 return true;
2503 typedef struct MMIOPtrInvalidate {
2504 MemoryRegion *mr;
2505 hwaddr offset;
2506 unsigned size;
2507 int busy;
2508 int allocated;
2509 } MMIOPtrInvalidate;
2511 #define MAX_MMIO_INVALIDATE 10
2512 static MMIOPtrInvalidate mmio_ptr_invalidate_list[MAX_MMIO_INVALIDATE];
2514 static void memory_region_do_invalidate_mmio_ptr(CPUState *cpu,
2515 run_on_cpu_data data)
2517 MMIOPtrInvalidate *invalidate_data = (MMIOPtrInvalidate *)data.host_ptr;
2518 MemoryRegion *mr = invalidate_data->mr;
2519 hwaddr offset = invalidate_data->offset;
2520 unsigned size = invalidate_data->size;
2521 MemoryRegionSection section = memory_region_find(mr, offset, size);
2523 qemu_mutex_lock_iothread();
2525 /* Reset dirty so this doesn't happen later. */
2526 cpu_physical_memory_test_and_clear_dirty(offset, size, 1);
2528 if (section.mr != mr) {
2529 /* memory_region_find add a ref on section.mr */
2530 memory_region_unref(section.mr);
2531 if (MMIO_INTERFACE(section.mr->owner)) {
2532 /* We found the interface just drop it. */
2533 object_property_set_bool(section.mr->owner, false, "realized",
2534 NULL);
2535 object_unref(section.mr->owner);
2536 object_unparent(section.mr->owner);
2540 qemu_mutex_unlock_iothread();
2542 if (invalidate_data->allocated) {
2543 g_free(invalidate_data);
2544 } else {
2545 invalidate_data->busy = 0;
2549 void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset,
2550 unsigned size)
2552 size_t i;
2553 MMIOPtrInvalidate *invalidate_data = NULL;
2555 for (i = 0; i < MAX_MMIO_INVALIDATE; i++) {
2556 if (atomic_cmpxchg(&(mmio_ptr_invalidate_list[i].busy), 0, 1) == 0) {
2557 invalidate_data = &mmio_ptr_invalidate_list[i];
2558 break;
2562 if (!invalidate_data) {
2563 invalidate_data = g_malloc0(sizeof(MMIOPtrInvalidate));
2564 invalidate_data->allocated = 1;
2567 invalidate_data->mr = mr;
2568 invalidate_data->offset = offset;
2569 invalidate_data->size = size;
2571 async_safe_run_on_cpu(first_cpu, memory_region_do_invalidate_mmio_ptr,
2572 RUN_ON_CPU_HOST_PTR(invalidate_data));
2575 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
2577 memory_region_ref(root);
2578 memory_region_transaction_begin();
2579 as->ref_count = 1;
2580 as->root = root;
2581 as->malloced = false;
2582 as->current_map = g_new(FlatView, 1);
2583 flatview_init(as->current_map);
2584 as->ioeventfd_nb = 0;
2585 as->ioeventfds = NULL;
2586 QTAILQ_INIT(&as->listeners);
2587 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
2588 as->name = g_strdup(name ? name : "anonymous");
2589 address_space_init_dispatch(as);
2590 memory_region_update_pending |= root->enabled;
2591 memory_region_transaction_commit();
2594 static void do_address_space_destroy(AddressSpace *as)
2596 bool do_free = as->malloced;
2598 address_space_destroy_dispatch(as);
2599 assert(QTAILQ_EMPTY(&as->listeners));
2601 flatview_unref(as->current_map);
2602 g_free(as->name);
2603 g_free(as->ioeventfds);
2604 memory_region_unref(as->root);
2605 if (do_free) {
2606 g_free(as);
2610 AddressSpace *address_space_init_shareable(MemoryRegion *root, const char *name)
2612 AddressSpace *as;
2614 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2615 if (root == as->root && as->malloced) {
2616 as->ref_count++;
2617 return as;
2621 as = g_malloc0(sizeof *as);
2622 address_space_init(as, root, name);
2623 as->malloced = true;
2624 return as;
2627 void address_space_destroy(AddressSpace *as)
2629 MemoryRegion *root = as->root;
2631 as->ref_count--;
2632 if (as->ref_count) {
2633 return;
2635 /* Flush out anything from MemoryListeners listening in on this */
2636 memory_region_transaction_begin();
2637 as->root = NULL;
2638 memory_region_transaction_commit();
2639 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
2640 address_space_unregister(as);
2642 /* At this point, as->dispatch and as->current_map are dummy
2643 * entries that the guest should never use. Wait for the old
2644 * values to expire before freeing the data.
2646 as->root = root;
2647 call_rcu(as, do_address_space_destroy, rcu);
2650 static const char *memory_region_type(MemoryRegion *mr)
2652 if (memory_region_is_ram_device(mr)) {
2653 return "ramd";
2654 } else if (memory_region_is_romd(mr)) {
2655 return "romd";
2656 } else if (memory_region_is_rom(mr)) {
2657 return "rom";
2658 } else if (memory_region_is_ram(mr)) {
2659 return "ram";
2660 } else {
2661 return "i/o";
2665 typedef struct MemoryRegionList MemoryRegionList;
2667 struct MemoryRegionList {
2668 const MemoryRegion *mr;
2669 QTAILQ_ENTRY(MemoryRegionList) queue;
2672 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
2674 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
2675 int128_sub((size), int128_one())) : 0)
2676 #define MTREE_INDENT " "
2678 static void mtree_print_mr(fprintf_function mon_printf, void *f,
2679 const MemoryRegion *mr, unsigned int level,
2680 hwaddr base,
2681 MemoryRegionListHead *alias_print_queue)
2683 MemoryRegionList *new_ml, *ml, *next_ml;
2684 MemoryRegionListHead submr_print_queue;
2685 const MemoryRegion *submr;
2686 unsigned int i;
2687 hwaddr cur_start, cur_end;
2689 if (!mr) {
2690 return;
2693 for (i = 0; i < level; i++) {
2694 mon_printf(f, MTREE_INDENT);
2697 cur_start = base + mr->addr;
2698 cur_end = cur_start + MR_SIZE(mr->size);
2701 * Try to detect overflow of memory region. This should never
2702 * happen normally. When it happens, we dump something to warn the
2703 * user who is observing this.
2705 if (cur_start < base || cur_end < cur_start) {
2706 mon_printf(f, "[DETECTED OVERFLOW!] ");
2709 if (mr->alias) {
2710 MemoryRegionList *ml;
2711 bool found = false;
2713 /* check if the alias is already in the queue */
2714 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
2715 if (ml->mr == mr->alias) {
2716 found = true;
2720 if (!found) {
2721 ml = g_new(MemoryRegionList, 1);
2722 ml->mr = mr->alias;
2723 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
2725 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
2726 " (prio %d, %s): alias %s @%s " TARGET_FMT_plx
2727 "-" TARGET_FMT_plx "%s\n",
2728 cur_start, cur_end,
2729 mr->priority,
2730 memory_region_type((MemoryRegion *)mr),
2731 memory_region_name(mr),
2732 memory_region_name(mr->alias),
2733 mr->alias_offset,
2734 mr->alias_offset + MR_SIZE(mr->size),
2735 mr->enabled ? "" : " [disabled]");
2736 } else {
2737 mon_printf(f,
2738 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %s): %s%s\n",
2739 cur_start, cur_end,
2740 mr->priority,
2741 memory_region_type((MemoryRegion *)mr),
2742 memory_region_name(mr),
2743 mr->enabled ? "" : " [disabled]");
2746 QTAILQ_INIT(&submr_print_queue);
2748 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
2749 new_ml = g_new(MemoryRegionList, 1);
2750 new_ml->mr = submr;
2751 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
2752 if (new_ml->mr->addr < ml->mr->addr ||
2753 (new_ml->mr->addr == ml->mr->addr &&
2754 new_ml->mr->priority > ml->mr->priority)) {
2755 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
2756 new_ml = NULL;
2757 break;
2760 if (new_ml) {
2761 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
2765 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
2766 mtree_print_mr(mon_printf, f, ml->mr, level + 1, cur_start,
2767 alias_print_queue);
2770 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
2771 g_free(ml);
2775 static void mtree_print_flatview(fprintf_function p, void *f,
2776 AddressSpace *as)
2778 FlatView *view = address_space_get_flatview(as);
2779 FlatRange *range = &view->ranges[0];
2780 MemoryRegion *mr;
2781 int n = view->nr;
2783 if (n <= 0) {
2784 p(f, MTREE_INDENT "No rendered FlatView for "
2785 "address space '%s'\n", as->name);
2786 flatview_unref(view);
2787 return;
2790 while (n--) {
2791 mr = range->mr;
2792 if (range->offset_in_region) {
2793 p(f, MTREE_INDENT TARGET_FMT_plx "-"
2794 TARGET_FMT_plx " (prio %d, %s): %s @" TARGET_FMT_plx "\n",
2795 int128_get64(range->addr.start),
2796 int128_get64(range->addr.start) + MR_SIZE(range->addr.size),
2797 mr->priority,
2798 range->readonly ? "rom" : memory_region_type(mr),
2799 memory_region_name(mr),
2800 range->offset_in_region);
2801 } else {
2802 p(f, MTREE_INDENT TARGET_FMT_plx "-"
2803 TARGET_FMT_plx " (prio %d, %s): %s\n",
2804 int128_get64(range->addr.start),
2805 int128_get64(range->addr.start) + MR_SIZE(range->addr.size),
2806 mr->priority,
2807 range->readonly ? "rom" : memory_region_type(mr),
2808 memory_region_name(mr));
2810 range++;
2813 flatview_unref(view);
2816 void mtree_info(fprintf_function mon_printf, void *f, bool flatview)
2818 MemoryRegionListHead ml_head;
2819 MemoryRegionList *ml, *ml2;
2820 AddressSpace *as;
2822 if (flatview) {
2823 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2824 mon_printf(f, "address-space (flat view): %s\n", as->name);
2825 mtree_print_flatview(mon_printf, f, as);
2826 mon_printf(f, "\n");
2828 return;
2831 QTAILQ_INIT(&ml_head);
2833 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2834 mon_printf(f, "address-space: %s\n", as->name);
2835 mtree_print_mr(mon_printf, f, as->root, 1, 0, &ml_head);
2836 mon_printf(f, "\n");
2839 /* print aliased regions */
2840 QTAILQ_FOREACH(ml, &ml_head, queue) {
2841 mon_printf(f, "memory-region: %s\n", memory_region_name(ml->mr));
2842 mtree_print_mr(mon_printf, f, ml->mr, 1, 0, &ml_head);
2843 mon_printf(f, "\n");
2846 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
2847 g_free(ml);
2851 static const TypeInfo memory_region_info = {
2852 .parent = TYPE_OBJECT,
2853 .name = TYPE_MEMORY_REGION,
2854 .instance_size = sizeof(MemoryRegion),
2855 .instance_init = memory_region_initfn,
2856 .instance_finalize = memory_region_finalize,
2859 static const TypeInfo iommu_memory_region_info = {
2860 .parent = TYPE_MEMORY_REGION,
2861 .name = TYPE_IOMMU_MEMORY_REGION,
2862 .class_size = sizeof(IOMMUMemoryRegionClass),
2863 .instance_size = sizeof(IOMMUMemoryRegion),
2864 .instance_init = iommu_memory_region_initfn,
2865 .abstract = true,
2868 static void memory_register_types(void)
2870 type_register_static(&memory_region_info);
2871 type_register_static(&iommu_memory_region_info);
2874 type_init(memory_register_types)