target-xtensa: avoid double-stopping at breakpoints
[qemu.git] / memory.c
blob1494e955d4db7fffde85115b30afd825ef056fe1
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 "exec/memory.h"
17 #include "exec/address-spaces.h"
18 #include "exec/ioport.h"
19 #include "qemu/bitops.h"
20 #include "qom/object.h"
21 #include "sysemu/kvm.h"
22 #include <assert.h>
24 #include "exec/memory-internal.h"
26 //#define DEBUG_UNASSIGNED
28 static unsigned memory_region_transaction_depth;
29 static bool memory_region_update_pending;
30 static bool global_dirty_log = false;
32 /* flat_view_mutex is taken around reading as->current_map; the critical
33 * section is extremely short, so I'm using a single mutex for every AS.
34 * We could also RCU for the read-side.
36 * The BQL is taken around transaction commits, hence both locks are taken
37 * while writing to as->current_map (with the BQL taken outside).
39 static QemuMutex flat_view_mutex;
41 static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners
42 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
44 static QTAILQ_HEAD(, AddressSpace) address_spaces
45 = QTAILQ_HEAD_INITIALIZER(address_spaces);
47 static void memory_init(void)
49 qemu_mutex_init(&flat_view_mutex);
52 typedef struct AddrRange AddrRange;
55 * Note using signed integers limits us to physical addresses at most
56 * 63 bits wide. They are needed for negative offsetting in aliases
57 * (large MemoryRegion::alias_offset).
59 struct AddrRange {
60 Int128 start;
61 Int128 size;
64 static AddrRange addrrange_make(Int128 start, Int128 size)
66 return (AddrRange) { start, size };
69 static bool addrrange_equal(AddrRange r1, AddrRange r2)
71 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
74 static Int128 addrrange_end(AddrRange r)
76 return int128_add(r.start, r.size);
79 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
81 int128_addto(&range.start, delta);
82 return range;
85 static bool addrrange_contains(AddrRange range, Int128 addr)
87 return int128_ge(addr, range.start)
88 && int128_lt(addr, addrrange_end(range));
91 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
93 return addrrange_contains(r1, r2.start)
94 || addrrange_contains(r2, r1.start);
97 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
99 Int128 start = int128_max(r1.start, r2.start);
100 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
101 return addrrange_make(start, int128_sub(end, start));
104 enum ListenerDirection { Forward, Reverse };
106 static bool memory_listener_match(MemoryListener *listener,
107 MemoryRegionSection *section)
109 return !listener->address_space_filter
110 || listener->address_space_filter == section->address_space;
113 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
114 do { \
115 MemoryListener *_listener; \
117 switch (_direction) { \
118 case Forward: \
119 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
120 if (_listener->_callback) { \
121 _listener->_callback(_listener, ##_args); \
124 break; \
125 case Reverse: \
126 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
127 memory_listeners, link) { \
128 if (_listener->_callback) { \
129 _listener->_callback(_listener, ##_args); \
132 break; \
133 default: \
134 abort(); \
136 } while (0)
138 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
139 do { \
140 MemoryListener *_listener; \
142 switch (_direction) { \
143 case Forward: \
144 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
145 if (_listener->_callback \
146 && memory_listener_match(_listener, _section)) { \
147 _listener->_callback(_listener, _section, ##_args); \
150 break; \
151 case Reverse: \
152 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
153 memory_listeners, link) { \
154 if (_listener->_callback \
155 && memory_listener_match(_listener, _section)) { \
156 _listener->_callback(_listener, _section, ##_args); \
159 break; \
160 default: \
161 abort(); \
163 } while (0)
165 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
166 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
167 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
168 .mr = (fr)->mr, \
169 .address_space = (as), \
170 .offset_within_region = (fr)->offset_in_region, \
171 .size = (fr)->addr.size, \
172 .offset_within_address_space = int128_get64((fr)->addr.start), \
173 .readonly = (fr)->readonly, \
176 struct CoalescedMemoryRange {
177 AddrRange addr;
178 QTAILQ_ENTRY(CoalescedMemoryRange) link;
181 struct MemoryRegionIoeventfd {
182 AddrRange addr;
183 bool match_data;
184 uint64_t data;
185 EventNotifier *e;
188 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
189 MemoryRegionIoeventfd b)
191 if (int128_lt(a.addr.start, b.addr.start)) {
192 return true;
193 } else if (int128_gt(a.addr.start, b.addr.start)) {
194 return false;
195 } else if (int128_lt(a.addr.size, b.addr.size)) {
196 return true;
197 } else if (int128_gt(a.addr.size, b.addr.size)) {
198 return false;
199 } else if (a.match_data < b.match_data) {
200 return true;
201 } else if (a.match_data > b.match_data) {
202 return false;
203 } else if (a.match_data) {
204 if (a.data < b.data) {
205 return true;
206 } else if (a.data > b.data) {
207 return false;
210 if (a.e < b.e) {
211 return true;
212 } else if (a.e > b.e) {
213 return false;
215 return false;
218 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
219 MemoryRegionIoeventfd b)
221 return !memory_region_ioeventfd_before(a, b)
222 && !memory_region_ioeventfd_before(b, a);
225 typedef struct FlatRange FlatRange;
226 typedef struct FlatView FlatView;
228 /* Range of memory in the global map. Addresses are absolute. */
229 struct FlatRange {
230 MemoryRegion *mr;
231 hwaddr offset_in_region;
232 AddrRange addr;
233 uint8_t dirty_log_mask;
234 bool romd_mode;
235 bool readonly;
238 /* Flattened global view of current active memory hierarchy. Kept in sorted
239 * order.
241 struct FlatView {
242 unsigned ref;
243 FlatRange *ranges;
244 unsigned nr;
245 unsigned nr_allocated;
248 typedef struct AddressSpaceOps AddressSpaceOps;
250 #define FOR_EACH_FLAT_RANGE(var, view) \
251 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
253 static bool flatrange_equal(FlatRange *a, FlatRange *b)
255 return a->mr == b->mr
256 && addrrange_equal(a->addr, b->addr)
257 && a->offset_in_region == b->offset_in_region
258 && a->romd_mode == b->romd_mode
259 && a->readonly == b->readonly;
262 static void flatview_init(FlatView *view)
264 view->ref = 1;
265 view->ranges = NULL;
266 view->nr = 0;
267 view->nr_allocated = 0;
270 /* Insert a range into a given position. Caller is responsible for maintaining
271 * sorting order.
273 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
275 if (view->nr == view->nr_allocated) {
276 view->nr_allocated = MAX(2 * view->nr, 10);
277 view->ranges = g_realloc(view->ranges,
278 view->nr_allocated * sizeof(*view->ranges));
280 memmove(view->ranges + pos + 1, view->ranges + pos,
281 (view->nr - pos) * sizeof(FlatRange));
282 view->ranges[pos] = *range;
283 memory_region_ref(range->mr);
284 ++view->nr;
287 static void flatview_destroy(FlatView *view)
289 int i;
291 for (i = 0; i < view->nr; i++) {
292 memory_region_unref(view->ranges[i].mr);
294 g_free(view->ranges);
295 g_free(view);
298 static void flatview_ref(FlatView *view)
300 atomic_inc(&view->ref);
303 static void flatview_unref(FlatView *view)
305 if (atomic_fetch_dec(&view->ref) == 1) {
306 flatview_destroy(view);
310 static bool can_merge(FlatRange *r1, FlatRange *r2)
312 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
313 && r1->mr == r2->mr
314 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
315 r1->addr.size),
316 int128_make64(r2->offset_in_region))
317 && r1->dirty_log_mask == r2->dirty_log_mask
318 && r1->romd_mode == r2->romd_mode
319 && r1->readonly == r2->readonly;
322 /* Attempt to simplify a view by merging adjacent ranges */
323 static void flatview_simplify(FlatView *view)
325 unsigned i, j;
327 i = 0;
328 while (i < view->nr) {
329 j = i + 1;
330 while (j < view->nr
331 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
332 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
333 ++j;
335 ++i;
336 memmove(&view->ranges[i], &view->ranges[j],
337 (view->nr - j) * sizeof(view->ranges[j]));
338 view->nr -= j - i;
342 static bool memory_region_big_endian(MemoryRegion *mr)
344 #ifdef TARGET_WORDS_BIGENDIAN
345 return mr->ops->endianness != DEVICE_LITTLE_ENDIAN;
346 #else
347 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
348 #endif
351 static bool memory_region_wrong_endianness(MemoryRegion *mr)
353 #ifdef TARGET_WORDS_BIGENDIAN
354 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
355 #else
356 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
357 #endif
360 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
362 if (memory_region_wrong_endianness(mr)) {
363 switch (size) {
364 case 1:
365 break;
366 case 2:
367 *data = bswap16(*data);
368 break;
369 case 4:
370 *data = bswap32(*data);
371 break;
372 case 8:
373 *data = bswap64(*data);
374 break;
375 default:
376 abort();
381 static void memory_region_oldmmio_read_accessor(MemoryRegion *mr,
382 hwaddr addr,
383 uint64_t *value,
384 unsigned size,
385 unsigned shift,
386 uint64_t mask)
388 uint64_t tmp;
390 tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);
391 *value |= (tmp & mask) << shift;
394 static void memory_region_read_accessor(MemoryRegion *mr,
395 hwaddr addr,
396 uint64_t *value,
397 unsigned size,
398 unsigned shift,
399 uint64_t mask)
401 uint64_t tmp;
403 if (mr->flush_coalesced_mmio) {
404 qemu_flush_coalesced_mmio_buffer();
406 tmp = mr->ops->read(mr->opaque, addr, size);
407 *value |= (tmp & mask) << shift;
410 static void memory_region_oldmmio_write_accessor(MemoryRegion *mr,
411 hwaddr addr,
412 uint64_t *value,
413 unsigned size,
414 unsigned shift,
415 uint64_t mask)
417 uint64_t tmp;
419 tmp = (*value >> shift) & mask;
420 mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp);
423 static void memory_region_write_accessor(MemoryRegion *mr,
424 hwaddr addr,
425 uint64_t *value,
426 unsigned size,
427 unsigned shift,
428 uint64_t mask)
430 uint64_t tmp;
432 if (mr->flush_coalesced_mmio) {
433 qemu_flush_coalesced_mmio_buffer();
435 tmp = (*value >> shift) & mask;
436 mr->ops->write(mr->opaque, addr, tmp, size);
439 static void access_with_adjusted_size(hwaddr addr,
440 uint64_t *value,
441 unsigned size,
442 unsigned access_size_min,
443 unsigned access_size_max,
444 void (*access)(MemoryRegion *mr,
445 hwaddr addr,
446 uint64_t *value,
447 unsigned size,
448 unsigned shift,
449 uint64_t mask),
450 MemoryRegion *mr)
452 uint64_t access_mask;
453 unsigned access_size;
454 unsigned i;
456 if (!access_size_min) {
457 access_size_min = 1;
459 if (!access_size_max) {
460 access_size_max = 4;
463 /* FIXME: support unaligned access? */
464 access_size = MAX(MIN(size, access_size_max), access_size_min);
465 access_mask = -1ULL >> (64 - access_size * 8);
466 if (memory_region_big_endian(mr)) {
467 for (i = 0; i < size; i += access_size) {
468 access(mr, addr + i, value, access_size,
469 (size - access_size - i) * 8, access_mask);
471 } else {
472 for (i = 0; i < size; i += access_size) {
473 access(mr, addr + i, value, access_size, i * 8, access_mask);
478 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
480 AddressSpace *as;
482 while (mr->parent) {
483 mr = mr->parent;
485 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
486 if (mr == as->root) {
487 return as;
490 abort();
493 /* Render a memory region into the global view. Ranges in @view obscure
494 * ranges in @mr.
496 static void render_memory_region(FlatView *view,
497 MemoryRegion *mr,
498 Int128 base,
499 AddrRange clip,
500 bool readonly)
502 MemoryRegion *subregion;
503 unsigned i;
504 hwaddr offset_in_region;
505 Int128 remain;
506 Int128 now;
507 FlatRange fr;
508 AddrRange tmp;
510 if (!mr->enabled) {
511 return;
514 int128_addto(&base, int128_make64(mr->addr));
515 readonly |= mr->readonly;
517 tmp = addrrange_make(base, mr->size);
519 if (!addrrange_intersects(tmp, clip)) {
520 return;
523 clip = addrrange_intersection(tmp, clip);
525 if (mr->alias) {
526 int128_subfrom(&base, int128_make64(mr->alias->addr));
527 int128_subfrom(&base, int128_make64(mr->alias_offset));
528 render_memory_region(view, mr->alias, base, clip, readonly);
529 return;
532 /* Render subregions in priority order. */
533 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
534 render_memory_region(view, subregion, base, clip, readonly);
537 if (!mr->terminates) {
538 return;
541 offset_in_region = int128_get64(int128_sub(clip.start, base));
542 base = clip.start;
543 remain = clip.size;
545 fr.mr = mr;
546 fr.dirty_log_mask = mr->dirty_log_mask;
547 fr.romd_mode = mr->romd_mode;
548 fr.readonly = readonly;
550 /* Render the region itself into any gaps left by the current view. */
551 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
552 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
553 continue;
555 if (int128_lt(base, view->ranges[i].addr.start)) {
556 now = int128_min(remain,
557 int128_sub(view->ranges[i].addr.start, base));
558 fr.offset_in_region = offset_in_region;
559 fr.addr = addrrange_make(base, now);
560 flatview_insert(view, i, &fr);
561 ++i;
562 int128_addto(&base, now);
563 offset_in_region += int128_get64(now);
564 int128_subfrom(&remain, now);
566 now = int128_sub(int128_min(int128_add(base, remain),
567 addrrange_end(view->ranges[i].addr)),
568 base);
569 int128_addto(&base, now);
570 offset_in_region += int128_get64(now);
571 int128_subfrom(&remain, now);
573 if (int128_nz(remain)) {
574 fr.offset_in_region = offset_in_region;
575 fr.addr = addrrange_make(base, remain);
576 flatview_insert(view, i, &fr);
580 /* Render a memory topology into a list of disjoint absolute ranges. */
581 static FlatView *generate_memory_topology(MemoryRegion *mr)
583 FlatView *view;
585 view = g_new(FlatView, 1);
586 flatview_init(view);
588 if (mr) {
589 render_memory_region(view, mr, int128_zero(),
590 addrrange_make(int128_zero(), int128_2_64()), false);
592 flatview_simplify(view);
594 return view;
597 static void address_space_add_del_ioeventfds(AddressSpace *as,
598 MemoryRegionIoeventfd *fds_new,
599 unsigned fds_new_nb,
600 MemoryRegionIoeventfd *fds_old,
601 unsigned fds_old_nb)
603 unsigned iold, inew;
604 MemoryRegionIoeventfd *fd;
605 MemoryRegionSection section;
607 /* Generate a symmetric difference of the old and new fd sets, adding
608 * and deleting as necessary.
611 iold = inew = 0;
612 while (iold < fds_old_nb || inew < fds_new_nb) {
613 if (iold < fds_old_nb
614 && (inew == fds_new_nb
615 || memory_region_ioeventfd_before(fds_old[iold],
616 fds_new[inew]))) {
617 fd = &fds_old[iold];
618 section = (MemoryRegionSection) {
619 .address_space = as,
620 .offset_within_address_space = int128_get64(fd->addr.start),
621 .size = fd->addr.size,
623 MEMORY_LISTENER_CALL(eventfd_del, Forward, &section,
624 fd->match_data, fd->data, fd->e);
625 ++iold;
626 } else if (inew < fds_new_nb
627 && (iold == fds_old_nb
628 || memory_region_ioeventfd_before(fds_new[inew],
629 fds_old[iold]))) {
630 fd = &fds_new[inew];
631 section = (MemoryRegionSection) {
632 .address_space = as,
633 .offset_within_address_space = int128_get64(fd->addr.start),
634 .size = fd->addr.size,
636 MEMORY_LISTENER_CALL(eventfd_add, Reverse, &section,
637 fd->match_data, fd->data, fd->e);
638 ++inew;
639 } else {
640 ++iold;
641 ++inew;
646 static FlatView *address_space_get_flatview(AddressSpace *as)
648 FlatView *view;
650 qemu_mutex_lock(&flat_view_mutex);
651 view = as->current_map;
652 flatview_ref(view);
653 qemu_mutex_unlock(&flat_view_mutex);
654 return view;
657 static void address_space_update_ioeventfds(AddressSpace *as)
659 FlatView *view;
660 FlatRange *fr;
661 unsigned ioeventfd_nb = 0;
662 MemoryRegionIoeventfd *ioeventfds = NULL;
663 AddrRange tmp;
664 unsigned i;
666 view = address_space_get_flatview(as);
667 FOR_EACH_FLAT_RANGE(fr, view) {
668 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
669 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
670 int128_sub(fr->addr.start,
671 int128_make64(fr->offset_in_region)));
672 if (addrrange_intersects(fr->addr, tmp)) {
673 ++ioeventfd_nb;
674 ioeventfds = g_realloc(ioeventfds,
675 ioeventfd_nb * sizeof(*ioeventfds));
676 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
677 ioeventfds[ioeventfd_nb-1].addr = tmp;
682 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
683 as->ioeventfds, as->ioeventfd_nb);
685 g_free(as->ioeventfds);
686 as->ioeventfds = ioeventfds;
687 as->ioeventfd_nb = ioeventfd_nb;
688 flatview_unref(view);
691 static void address_space_update_topology_pass(AddressSpace *as,
692 const FlatView *old_view,
693 const FlatView *new_view,
694 bool adding)
696 unsigned iold, inew;
697 FlatRange *frold, *frnew;
699 /* Generate a symmetric difference of the old and new memory maps.
700 * Kill ranges in the old map, and instantiate ranges in the new map.
702 iold = inew = 0;
703 while (iold < old_view->nr || inew < new_view->nr) {
704 if (iold < old_view->nr) {
705 frold = &old_view->ranges[iold];
706 } else {
707 frold = NULL;
709 if (inew < new_view->nr) {
710 frnew = &new_view->ranges[inew];
711 } else {
712 frnew = NULL;
715 if (frold
716 && (!frnew
717 || int128_lt(frold->addr.start, frnew->addr.start)
718 || (int128_eq(frold->addr.start, frnew->addr.start)
719 && !flatrange_equal(frold, frnew)))) {
720 /* In old but not in new, or in both but attributes changed. */
722 if (!adding) {
723 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
726 ++iold;
727 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
728 /* In both and unchanged (except logging may have changed) */
730 if (adding) {
731 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
732 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
733 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop);
734 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
735 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start);
739 ++iold;
740 ++inew;
741 } else {
742 /* In new */
744 if (adding) {
745 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
748 ++inew;
754 static void address_space_update_topology(AddressSpace *as)
756 FlatView *old_view = address_space_get_flatview(as);
757 FlatView *new_view = generate_memory_topology(as->root);
759 address_space_update_topology_pass(as, old_view, new_view, false);
760 address_space_update_topology_pass(as, old_view, new_view, true);
762 qemu_mutex_lock(&flat_view_mutex);
763 flatview_unref(as->current_map);
764 as->current_map = new_view;
765 qemu_mutex_unlock(&flat_view_mutex);
767 /* Note that all the old MemoryRegions are still alive up to this
768 * point. This relieves most MemoryListeners from the need to
769 * ref/unref the MemoryRegions they get---unless they use them
770 * outside the iothread mutex, in which case precise reference
771 * counting is necessary.
773 flatview_unref(old_view);
775 address_space_update_ioeventfds(as);
778 void memory_region_transaction_begin(void)
780 qemu_flush_coalesced_mmio_buffer();
781 ++memory_region_transaction_depth;
784 void memory_region_transaction_commit(void)
786 AddressSpace *as;
788 assert(memory_region_transaction_depth);
789 --memory_region_transaction_depth;
790 if (!memory_region_transaction_depth && memory_region_update_pending) {
791 memory_region_update_pending = false;
792 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
794 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
795 address_space_update_topology(as);
798 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
802 static void memory_region_destructor_none(MemoryRegion *mr)
806 static void memory_region_destructor_ram(MemoryRegion *mr)
808 qemu_ram_free(mr->ram_addr);
811 static void memory_region_destructor_alias(MemoryRegion *mr)
813 memory_region_unref(mr->alias);
816 static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
818 qemu_ram_free_from_ptr(mr->ram_addr);
821 static void memory_region_destructor_rom_device(MemoryRegion *mr)
823 qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
826 void memory_region_init(MemoryRegion *mr,
827 Object *owner,
828 const char *name,
829 uint64_t size)
831 mr->ops = &unassigned_mem_ops;
832 mr->opaque = NULL;
833 mr->owner = owner;
834 mr->iommu_ops = NULL;
835 mr->parent = NULL;
836 mr->size = int128_make64(size);
837 if (size == UINT64_MAX) {
838 mr->size = int128_2_64();
840 mr->addr = 0;
841 mr->subpage = false;
842 mr->enabled = true;
843 mr->terminates = false;
844 mr->ram = false;
845 mr->romd_mode = true;
846 mr->readonly = false;
847 mr->rom_device = false;
848 mr->destructor = memory_region_destructor_none;
849 mr->priority = 0;
850 mr->may_overlap = false;
851 mr->alias = NULL;
852 QTAILQ_INIT(&mr->subregions);
853 memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
854 QTAILQ_INIT(&mr->coalesced);
855 mr->name = g_strdup(name);
856 mr->dirty_log_mask = 0;
857 mr->ioeventfd_nb = 0;
858 mr->ioeventfds = NULL;
859 mr->flush_coalesced_mmio = false;
862 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
863 unsigned size)
865 #ifdef DEBUG_UNASSIGNED
866 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
867 #endif
868 if (current_cpu != NULL) {
869 cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
871 return -1ULL;
874 static void unassigned_mem_write(void *opaque, hwaddr addr,
875 uint64_t val, unsigned size)
877 #ifdef DEBUG_UNASSIGNED
878 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
879 #endif
880 if (current_cpu != NULL) {
881 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
885 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
886 unsigned size, bool is_write)
888 return false;
891 const MemoryRegionOps unassigned_mem_ops = {
892 .valid.accepts = unassigned_mem_accepts,
893 .endianness = DEVICE_NATIVE_ENDIAN,
896 bool memory_region_access_valid(MemoryRegion *mr,
897 hwaddr addr,
898 unsigned size,
899 bool is_write)
901 int access_size_min, access_size_max;
902 int access_size, i;
904 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
905 return false;
908 if (!mr->ops->valid.accepts) {
909 return true;
912 access_size_min = mr->ops->valid.min_access_size;
913 if (!mr->ops->valid.min_access_size) {
914 access_size_min = 1;
917 access_size_max = mr->ops->valid.max_access_size;
918 if (!mr->ops->valid.max_access_size) {
919 access_size_max = 4;
922 access_size = MAX(MIN(size, access_size_max), access_size_min);
923 for (i = 0; i < size; i += access_size) {
924 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
925 is_write)) {
926 return false;
930 return true;
933 static uint64_t memory_region_dispatch_read1(MemoryRegion *mr,
934 hwaddr addr,
935 unsigned size)
937 uint64_t data = 0;
939 if (mr->ops->read) {
940 access_with_adjusted_size(addr, &data, size,
941 mr->ops->impl.min_access_size,
942 mr->ops->impl.max_access_size,
943 memory_region_read_accessor, mr);
944 } else {
945 access_with_adjusted_size(addr, &data, size, 1, 4,
946 memory_region_oldmmio_read_accessor, mr);
949 return data;
952 static bool memory_region_dispatch_read(MemoryRegion *mr,
953 hwaddr addr,
954 uint64_t *pval,
955 unsigned size)
957 if (!memory_region_access_valid(mr, addr, size, false)) {
958 *pval = unassigned_mem_read(mr, addr, size);
959 return true;
962 *pval = memory_region_dispatch_read1(mr, addr, size);
963 adjust_endianness(mr, pval, size);
964 return false;
967 static bool memory_region_dispatch_write(MemoryRegion *mr,
968 hwaddr addr,
969 uint64_t data,
970 unsigned size)
972 if (!memory_region_access_valid(mr, addr, size, true)) {
973 unassigned_mem_write(mr, addr, data, size);
974 return true;
977 adjust_endianness(mr, &data, size);
979 if (mr->ops->write) {
980 access_with_adjusted_size(addr, &data, size,
981 mr->ops->impl.min_access_size,
982 mr->ops->impl.max_access_size,
983 memory_region_write_accessor, mr);
984 } else {
985 access_with_adjusted_size(addr, &data, size, 1, 4,
986 memory_region_oldmmio_write_accessor, mr);
988 return false;
991 void memory_region_init_io(MemoryRegion *mr,
992 Object *owner,
993 const MemoryRegionOps *ops,
994 void *opaque,
995 const char *name,
996 uint64_t size)
998 memory_region_init(mr, owner, name, size);
999 mr->ops = ops;
1000 mr->opaque = opaque;
1001 mr->terminates = true;
1002 mr->ram_addr = ~(ram_addr_t)0;
1005 void memory_region_init_ram(MemoryRegion *mr,
1006 Object *owner,
1007 const char *name,
1008 uint64_t size)
1010 memory_region_init(mr, owner, name, size);
1011 mr->ram = true;
1012 mr->terminates = true;
1013 mr->destructor = memory_region_destructor_ram;
1014 mr->ram_addr = qemu_ram_alloc(size, mr);
1017 void memory_region_init_ram_ptr(MemoryRegion *mr,
1018 Object *owner,
1019 const char *name,
1020 uint64_t size,
1021 void *ptr)
1023 memory_region_init(mr, owner, name, size);
1024 mr->ram = true;
1025 mr->terminates = true;
1026 mr->destructor = memory_region_destructor_ram_from_ptr;
1027 mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr);
1030 void memory_region_init_alias(MemoryRegion *mr,
1031 Object *owner,
1032 const char *name,
1033 MemoryRegion *orig,
1034 hwaddr offset,
1035 uint64_t size)
1037 memory_region_init(mr, owner, name, size);
1038 memory_region_ref(orig);
1039 mr->destructor = memory_region_destructor_alias;
1040 mr->alias = orig;
1041 mr->alias_offset = offset;
1044 void memory_region_init_rom_device(MemoryRegion *mr,
1045 Object *owner,
1046 const MemoryRegionOps *ops,
1047 void *opaque,
1048 const char *name,
1049 uint64_t size)
1051 memory_region_init(mr, owner, name, size);
1052 mr->ops = ops;
1053 mr->opaque = opaque;
1054 mr->terminates = true;
1055 mr->rom_device = true;
1056 mr->destructor = memory_region_destructor_rom_device;
1057 mr->ram_addr = qemu_ram_alloc(size, mr);
1060 void memory_region_init_iommu(MemoryRegion *mr,
1061 Object *owner,
1062 const MemoryRegionIOMMUOps *ops,
1063 const char *name,
1064 uint64_t size)
1066 memory_region_init(mr, owner, name, size);
1067 mr->iommu_ops = ops,
1068 mr->terminates = true; /* then re-forwards */
1069 notifier_list_init(&mr->iommu_notify);
1072 void memory_region_init_reservation(MemoryRegion *mr,
1073 Object *owner,
1074 const char *name,
1075 uint64_t size)
1077 memory_region_init_io(mr, owner, &unassigned_mem_ops, mr, name, size);
1080 void memory_region_destroy(MemoryRegion *mr)
1082 assert(QTAILQ_EMPTY(&mr->subregions));
1083 assert(memory_region_transaction_depth == 0);
1084 mr->destructor(mr);
1085 memory_region_clear_coalescing(mr);
1086 g_free((char *)mr->name);
1087 g_free(mr->ioeventfds);
1090 Object *memory_region_owner(MemoryRegion *mr)
1092 return mr->owner;
1095 void memory_region_ref(MemoryRegion *mr)
1097 if (mr && mr->owner) {
1098 object_ref(mr->owner);
1102 void memory_region_unref(MemoryRegion *mr)
1104 if (mr && mr->owner) {
1105 object_unref(mr->owner);
1109 uint64_t memory_region_size(MemoryRegion *mr)
1111 if (int128_eq(mr->size, int128_2_64())) {
1112 return UINT64_MAX;
1114 return int128_get64(mr->size);
1117 const char *memory_region_name(MemoryRegion *mr)
1119 return mr->name;
1122 bool memory_region_is_ram(MemoryRegion *mr)
1124 return mr->ram;
1127 bool memory_region_is_logging(MemoryRegion *mr)
1129 return mr->dirty_log_mask;
1132 bool memory_region_is_rom(MemoryRegion *mr)
1134 return mr->ram && mr->readonly;
1137 bool memory_region_is_iommu(MemoryRegion *mr)
1139 return mr->iommu_ops;
1142 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n)
1144 notifier_list_add(&mr->iommu_notify, n);
1147 void memory_region_unregister_iommu_notifier(Notifier *n)
1149 notifier_remove(n);
1152 void memory_region_notify_iommu(MemoryRegion *mr,
1153 IOMMUTLBEntry entry)
1155 assert(memory_region_is_iommu(mr));
1156 notifier_list_notify(&mr->iommu_notify, &entry);
1159 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1161 uint8_t mask = 1 << client;
1163 memory_region_transaction_begin();
1164 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1165 memory_region_update_pending |= mr->enabled;
1166 memory_region_transaction_commit();
1169 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1170 hwaddr size, unsigned client)
1172 assert(mr->terminates);
1173 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, size,
1174 1 << client);
1177 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1178 hwaddr size)
1180 assert(mr->terminates);
1181 return cpu_physical_memory_set_dirty_range(mr->ram_addr + addr, size, -1);
1184 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
1185 hwaddr size, unsigned client)
1187 bool ret;
1188 assert(mr->terminates);
1189 ret = cpu_physical_memory_get_dirty(mr->ram_addr + addr, size,
1190 1 << client);
1191 if (ret) {
1192 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1193 mr->ram_addr + addr + size,
1194 1 << client);
1196 return ret;
1200 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1202 AddressSpace *as;
1203 FlatRange *fr;
1205 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1206 FlatView *view = address_space_get_flatview(as);
1207 FOR_EACH_FLAT_RANGE(fr, view) {
1208 if (fr->mr == mr) {
1209 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1212 flatview_unref(view);
1216 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1218 if (mr->readonly != readonly) {
1219 memory_region_transaction_begin();
1220 mr->readonly = readonly;
1221 memory_region_update_pending |= mr->enabled;
1222 memory_region_transaction_commit();
1226 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
1228 if (mr->romd_mode != romd_mode) {
1229 memory_region_transaction_begin();
1230 mr->romd_mode = romd_mode;
1231 memory_region_update_pending |= mr->enabled;
1232 memory_region_transaction_commit();
1236 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1237 hwaddr size, unsigned client)
1239 assert(mr->terminates);
1240 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1241 mr->ram_addr + addr + size,
1242 1 << client);
1245 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1247 if (mr->alias) {
1248 return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset;
1251 assert(mr->terminates);
1253 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
1256 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
1258 FlatView *view;
1259 FlatRange *fr;
1260 CoalescedMemoryRange *cmr;
1261 AddrRange tmp;
1262 MemoryRegionSection section;
1264 view = address_space_get_flatview(as);
1265 FOR_EACH_FLAT_RANGE(fr, view) {
1266 if (fr->mr == mr) {
1267 section = (MemoryRegionSection) {
1268 .address_space = as,
1269 .offset_within_address_space = int128_get64(fr->addr.start),
1270 .size = fr->addr.size,
1273 MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, &section,
1274 int128_get64(fr->addr.start),
1275 int128_get64(fr->addr.size));
1276 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1277 tmp = addrrange_shift(cmr->addr,
1278 int128_sub(fr->addr.start,
1279 int128_make64(fr->offset_in_region)));
1280 if (!addrrange_intersects(tmp, fr->addr)) {
1281 continue;
1283 tmp = addrrange_intersection(tmp, fr->addr);
1284 MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, &section,
1285 int128_get64(tmp.start),
1286 int128_get64(tmp.size));
1290 flatview_unref(view);
1293 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1295 AddressSpace *as;
1297 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1298 memory_region_update_coalesced_range_as(mr, as);
1302 void memory_region_set_coalescing(MemoryRegion *mr)
1304 memory_region_clear_coalescing(mr);
1305 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1308 void memory_region_add_coalescing(MemoryRegion *mr,
1309 hwaddr offset,
1310 uint64_t size)
1312 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1314 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1315 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1316 memory_region_update_coalesced_range(mr);
1317 memory_region_set_flush_coalesced(mr);
1320 void memory_region_clear_coalescing(MemoryRegion *mr)
1322 CoalescedMemoryRange *cmr;
1324 qemu_flush_coalesced_mmio_buffer();
1325 mr->flush_coalesced_mmio = false;
1327 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1328 cmr = QTAILQ_FIRST(&mr->coalesced);
1329 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1330 g_free(cmr);
1332 memory_region_update_coalesced_range(mr);
1335 void memory_region_set_flush_coalesced(MemoryRegion *mr)
1337 mr->flush_coalesced_mmio = true;
1340 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
1342 qemu_flush_coalesced_mmio_buffer();
1343 if (QTAILQ_EMPTY(&mr->coalesced)) {
1344 mr->flush_coalesced_mmio = false;
1348 void memory_region_add_eventfd(MemoryRegion *mr,
1349 hwaddr addr,
1350 unsigned size,
1351 bool match_data,
1352 uint64_t data,
1353 EventNotifier *e)
1355 MemoryRegionIoeventfd mrfd = {
1356 .addr.start = int128_make64(addr),
1357 .addr.size = int128_make64(size),
1358 .match_data = match_data,
1359 .data = data,
1360 .e = e,
1362 unsigned i;
1364 adjust_endianness(mr, &mrfd.data, size);
1365 memory_region_transaction_begin();
1366 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1367 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1368 break;
1371 ++mr->ioeventfd_nb;
1372 mr->ioeventfds = g_realloc(mr->ioeventfds,
1373 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1374 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1375 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1376 mr->ioeventfds[i] = mrfd;
1377 memory_region_update_pending |= mr->enabled;
1378 memory_region_transaction_commit();
1381 void memory_region_del_eventfd(MemoryRegion *mr,
1382 hwaddr addr,
1383 unsigned size,
1384 bool match_data,
1385 uint64_t data,
1386 EventNotifier *e)
1388 MemoryRegionIoeventfd mrfd = {
1389 .addr.start = int128_make64(addr),
1390 .addr.size = int128_make64(size),
1391 .match_data = match_data,
1392 .data = data,
1393 .e = e,
1395 unsigned i;
1397 adjust_endianness(mr, &mrfd.data, size);
1398 memory_region_transaction_begin();
1399 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1400 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1401 break;
1404 assert(i != mr->ioeventfd_nb);
1405 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1406 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1407 --mr->ioeventfd_nb;
1408 mr->ioeventfds = g_realloc(mr->ioeventfds,
1409 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1410 memory_region_update_pending |= mr->enabled;
1411 memory_region_transaction_commit();
1414 static void memory_region_add_subregion_common(MemoryRegion *mr,
1415 hwaddr offset,
1416 MemoryRegion *subregion)
1418 MemoryRegion *other;
1420 memory_region_transaction_begin();
1422 assert(!subregion->parent);
1423 memory_region_ref(subregion);
1424 subregion->parent = mr;
1425 subregion->addr = offset;
1426 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1427 if (subregion->may_overlap || other->may_overlap) {
1428 continue;
1430 if (int128_ge(int128_make64(offset),
1431 int128_add(int128_make64(other->addr), other->size))
1432 || int128_le(int128_add(int128_make64(offset), subregion->size),
1433 int128_make64(other->addr))) {
1434 continue;
1436 #if 0
1437 printf("warning: subregion collision %llx/%llx (%s) "
1438 "vs %llx/%llx (%s)\n",
1439 (unsigned long long)offset,
1440 (unsigned long long)int128_get64(subregion->size),
1441 subregion->name,
1442 (unsigned long long)other->addr,
1443 (unsigned long long)int128_get64(other->size),
1444 other->name);
1445 #endif
1447 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1448 if (subregion->priority >= other->priority) {
1449 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1450 goto done;
1453 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1454 done:
1455 memory_region_update_pending |= mr->enabled && subregion->enabled;
1456 memory_region_transaction_commit();
1460 void memory_region_add_subregion(MemoryRegion *mr,
1461 hwaddr offset,
1462 MemoryRegion *subregion)
1464 subregion->may_overlap = false;
1465 subregion->priority = 0;
1466 memory_region_add_subregion_common(mr, offset, subregion);
1469 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1470 hwaddr offset,
1471 MemoryRegion *subregion,
1472 unsigned priority)
1474 subregion->may_overlap = true;
1475 subregion->priority = priority;
1476 memory_region_add_subregion_common(mr, offset, subregion);
1479 void memory_region_del_subregion(MemoryRegion *mr,
1480 MemoryRegion *subregion)
1482 memory_region_transaction_begin();
1483 assert(subregion->parent == mr);
1484 subregion->parent = NULL;
1485 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1486 memory_region_unref(subregion);
1487 memory_region_update_pending |= mr->enabled && subregion->enabled;
1488 memory_region_transaction_commit();
1491 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1493 if (enabled == mr->enabled) {
1494 return;
1496 memory_region_transaction_begin();
1497 mr->enabled = enabled;
1498 memory_region_update_pending = true;
1499 memory_region_transaction_commit();
1502 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
1504 MemoryRegion *parent = mr->parent;
1505 unsigned priority = mr->priority;
1506 bool may_overlap = mr->may_overlap;
1508 if (addr == mr->addr || !parent) {
1509 mr->addr = addr;
1510 return;
1513 memory_region_transaction_begin();
1514 memory_region_ref(mr);
1515 memory_region_del_subregion(parent, mr);
1516 if (may_overlap) {
1517 memory_region_add_subregion_overlap(parent, addr, mr, priority);
1518 } else {
1519 memory_region_add_subregion(parent, addr, mr);
1521 memory_region_unref(mr);
1522 memory_region_transaction_commit();
1525 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
1527 assert(mr->alias);
1529 if (offset == mr->alias_offset) {
1530 return;
1533 memory_region_transaction_begin();
1534 mr->alias_offset = offset;
1535 memory_region_update_pending |= mr->enabled;
1536 memory_region_transaction_commit();
1539 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1541 return mr->ram_addr;
1544 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1546 const AddrRange *addr = addr_;
1547 const FlatRange *fr = fr_;
1549 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1550 return -1;
1551 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1552 return 1;
1554 return 0;
1557 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
1559 return bsearch(&addr, view->ranges, view->nr,
1560 sizeof(FlatRange), cmp_flatrange_addr);
1563 bool memory_region_present(MemoryRegion *parent, hwaddr addr)
1565 MemoryRegion *mr = memory_region_find(parent, addr, 1).mr;
1566 if (!mr) {
1567 return false;
1569 memory_region_unref(mr);
1570 return true;
1573 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1574 hwaddr addr, uint64_t size)
1576 MemoryRegionSection ret = { .mr = NULL };
1577 MemoryRegion *root;
1578 AddressSpace *as;
1579 AddrRange range;
1580 FlatView *view;
1581 FlatRange *fr;
1583 addr += mr->addr;
1584 for (root = mr; root->parent; ) {
1585 root = root->parent;
1586 addr += root->addr;
1589 as = memory_region_to_address_space(root);
1590 range = addrrange_make(int128_make64(addr), int128_make64(size));
1592 view = address_space_get_flatview(as);
1593 fr = flatview_lookup(view, range);
1594 if (!fr) {
1595 return ret;
1598 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
1599 --fr;
1602 ret.mr = fr->mr;
1603 ret.address_space = as;
1604 range = addrrange_intersection(range, fr->addr);
1605 ret.offset_within_region = fr->offset_in_region;
1606 ret.offset_within_region += int128_get64(int128_sub(range.start,
1607 fr->addr.start));
1608 ret.size = range.size;
1609 ret.offset_within_address_space = int128_get64(range.start);
1610 ret.readonly = fr->readonly;
1611 memory_region_ref(ret.mr);
1613 flatview_unref(view);
1614 return ret;
1617 void address_space_sync_dirty_bitmap(AddressSpace *as)
1619 FlatView *view;
1620 FlatRange *fr;
1622 view = address_space_get_flatview(as);
1623 FOR_EACH_FLAT_RANGE(fr, view) {
1624 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1626 flatview_unref(view);
1629 void memory_global_dirty_log_start(void)
1631 global_dirty_log = true;
1632 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
1635 void memory_global_dirty_log_stop(void)
1637 global_dirty_log = false;
1638 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
1641 static void listener_add_address_space(MemoryListener *listener,
1642 AddressSpace *as)
1644 FlatView *view;
1645 FlatRange *fr;
1647 if (listener->address_space_filter
1648 && listener->address_space_filter != as) {
1649 return;
1652 if (global_dirty_log) {
1653 if (listener->log_global_start) {
1654 listener->log_global_start(listener);
1658 view = address_space_get_flatview(as);
1659 FOR_EACH_FLAT_RANGE(fr, view) {
1660 MemoryRegionSection section = {
1661 .mr = fr->mr,
1662 .address_space = as,
1663 .offset_within_region = fr->offset_in_region,
1664 .size = fr->addr.size,
1665 .offset_within_address_space = int128_get64(fr->addr.start),
1666 .readonly = fr->readonly,
1668 if (listener->region_add) {
1669 listener->region_add(listener, &section);
1672 flatview_unref(view);
1675 void memory_listener_register(MemoryListener *listener, AddressSpace *filter)
1677 MemoryListener *other = NULL;
1678 AddressSpace *as;
1680 listener->address_space_filter = filter;
1681 if (QTAILQ_EMPTY(&memory_listeners)
1682 || listener->priority >= QTAILQ_LAST(&memory_listeners,
1683 memory_listeners)->priority) {
1684 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
1685 } else {
1686 QTAILQ_FOREACH(other, &memory_listeners, link) {
1687 if (listener->priority < other->priority) {
1688 break;
1691 QTAILQ_INSERT_BEFORE(other, listener, link);
1694 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1695 listener_add_address_space(listener, as);
1699 void memory_listener_unregister(MemoryListener *listener)
1701 QTAILQ_REMOVE(&memory_listeners, listener, link);
1704 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
1706 if (QTAILQ_EMPTY(&address_spaces)) {
1707 memory_init();
1710 memory_region_transaction_begin();
1711 as->root = root;
1712 as->current_map = g_new(FlatView, 1);
1713 flatview_init(as->current_map);
1714 as->ioeventfd_nb = 0;
1715 as->ioeventfds = NULL;
1716 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
1717 as->name = g_strdup(name ? name : "anonymous");
1718 address_space_init_dispatch(as);
1719 memory_region_update_pending |= root->enabled;
1720 memory_region_transaction_commit();
1723 void address_space_destroy(AddressSpace *as)
1725 /* Flush out anything from MemoryListeners listening in on this */
1726 memory_region_transaction_begin();
1727 as->root = NULL;
1728 memory_region_transaction_commit();
1729 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
1730 address_space_destroy_dispatch(as);
1731 flatview_unref(as->current_map);
1732 g_free(as->name);
1733 g_free(as->ioeventfds);
1736 bool io_mem_read(MemoryRegion *mr, hwaddr addr, uint64_t *pval, unsigned size)
1738 return memory_region_dispatch_read(mr, addr, pval, size);
1741 bool io_mem_write(MemoryRegion *mr, hwaddr addr,
1742 uint64_t val, unsigned size)
1744 return memory_region_dispatch_write(mr, addr, val, size);
1747 typedef struct MemoryRegionList MemoryRegionList;
1749 struct MemoryRegionList {
1750 const MemoryRegion *mr;
1751 bool printed;
1752 QTAILQ_ENTRY(MemoryRegionList) queue;
1755 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1757 static void mtree_print_mr(fprintf_function mon_printf, void *f,
1758 const MemoryRegion *mr, unsigned int level,
1759 hwaddr base,
1760 MemoryRegionListHead *alias_print_queue)
1762 MemoryRegionList *new_ml, *ml, *next_ml;
1763 MemoryRegionListHead submr_print_queue;
1764 const MemoryRegion *submr;
1765 unsigned int i;
1767 if (!mr || !mr->enabled) {
1768 return;
1771 for (i = 0; i < level; i++) {
1772 mon_printf(f, " ");
1775 if (mr->alias) {
1776 MemoryRegionList *ml;
1777 bool found = false;
1779 /* check if the alias is already in the queue */
1780 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
1781 if (ml->mr == mr->alias && !ml->printed) {
1782 found = true;
1786 if (!found) {
1787 ml = g_new(MemoryRegionList, 1);
1788 ml->mr = mr->alias;
1789 ml->printed = false;
1790 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
1792 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
1793 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1794 "-" TARGET_FMT_plx "\n",
1795 base + mr->addr,
1796 base + mr->addr
1797 + (int128_nz(mr->size) ?
1798 (hwaddr)int128_get64(int128_sub(mr->size,
1799 int128_one())) : 0),
1800 mr->priority,
1801 mr->romd_mode ? 'R' : '-',
1802 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
1803 : '-',
1804 mr->name,
1805 mr->alias->name,
1806 mr->alias_offset,
1807 mr->alias_offset
1808 + (hwaddr)int128_get64(mr->size) - 1);
1809 } else {
1810 mon_printf(f,
1811 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %c%c): %s\n",
1812 base + mr->addr,
1813 base + mr->addr
1814 + (int128_nz(mr->size) ?
1815 (hwaddr)int128_get64(int128_sub(mr->size,
1816 int128_one())) : 0),
1817 mr->priority,
1818 mr->romd_mode ? 'R' : '-',
1819 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
1820 : '-',
1821 mr->name);
1824 QTAILQ_INIT(&submr_print_queue);
1826 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
1827 new_ml = g_new(MemoryRegionList, 1);
1828 new_ml->mr = submr;
1829 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1830 if (new_ml->mr->addr < ml->mr->addr ||
1831 (new_ml->mr->addr == ml->mr->addr &&
1832 new_ml->mr->priority > ml->mr->priority)) {
1833 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
1834 new_ml = NULL;
1835 break;
1838 if (new_ml) {
1839 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
1843 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1844 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
1845 alias_print_queue);
1848 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
1849 g_free(ml);
1853 void mtree_info(fprintf_function mon_printf, void *f)
1855 MemoryRegionListHead ml_head;
1856 MemoryRegionList *ml, *ml2;
1857 AddressSpace *as;
1859 QTAILQ_INIT(&ml_head);
1861 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1862 mon_printf(f, "%s\n", as->name);
1863 mtree_print_mr(mon_printf, f, as->root, 0, 0, &ml_head);
1866 mon_printf(f, "aliases\n");
1867 /* print aliased regions */
1868 QTAILQ_FOREACH(ml, &ml_head, queue) {
1869 if (!ml->printed) {
1870 mon_printf(f, "%s\n", ml->mr->name);
1871 mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head);
1875 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
1876 g_free(ml);