exec: introduce memory_access_size
[qemu.git] / memory.c
blobf2135d1de0b883941d561a5305cbf48005c9aff8
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 "sysemu/kvm.h"
21 #include <assert.h>
23 #include "exec/memory-internal.h"
25 //#define DEBUG_UNASSIGNED
27 static unsigned memory_region_transaction_depth;
28 static bool memory_region_update_pending;
29 static bool global_dirty_log = false;
31 static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners
32 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
34 static QTAILQ_HEAD(, AddressSpace) address_spaces
35 = QTAILQ_HEAD_INITIALIZER(address_spaces);
37 typedef struct AddrRange AddrRange;
40 * Note using signed integers limits us to physical addresses at most
41 * 63 bits wide. They are needed for negative offsetting in aliases
42 * (large MemoryRegion::alias_offset).
44 struct AddrRange {
45 Int128 start;
46 Int128 size;
49 static AddrRange addrrange_make(Int128 start, Int128 size)
51 return (AddrRange) { start, size };
54 static bool addrrange_equal(AddrRange r1, AddrRange r2)
56 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
59 static Int128 addrrange_end(AddrRange r)
61 return int128_add(r.start, r.size);
64 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
66 int128_addto(&range.start, delta);
67 return range;
70 static bool addrrange_contains(AddrRange range, Int128 addr)
72 return int128_ge(addr, range.start)
73 && int128_lt(addr, addrrange_end(range));
76 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
78 return addrrange_contains(r1, r2.start)
79 || addrrange_contains(r2, r1.start);
82 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
84 Int128 start = int128_max(r1.start, r2.start);
85 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
86 return addrrange_make(start, int128_sub(end, start));
89 enum ListenerDirection { Forward, Reverse };
91 static bool memory_listener_match(MemoryListener *listener,
92 MemoryRegionSection *section)
94 return !listener->address_space_filter
95 || listener->address_space_filter == section->address_space;
98 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
99 do { \
100 MemoryListener *_listener; \
102 switch (_direction) { \
103 case Forward: \
104 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
105 if (_listener->_callback) { \
106 _listener->_callback(_listener, ##_args); \
109 break; \
110 case Reverse: \
111 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
112 memory_listeners, link) { \
113 if (_listener->_callback) { \
114 _listener->_callback(_listener, ##_args); \
117 break; \
118 default: \
119 abort(); \
121 } while (0)
123 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
124 do { \
125 MemoryListener *_listener; \
127 switch (_direction) { \
128 case Forward: \
129 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
130 if (_listener->_callback \
131 && memory_listener_match(_listener, _section)) { \
132 _listener->_callback(_listener, _section, ##_args); \
135 break; \
136 case Reverse: \
137 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
138 memory_listeners, link) { \
139 if (_listener->_callback \
140 && memory_listener_match(_listener, _section)) { \
141 _listener->_callback(_listener, _section, ##_args); \
144 break; \
145 default: \
146 abort(); \
148 } while (0)
150 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
151 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
152 .mr = (fr)->mr, \
153 .address_space = (as), \
154 .offset_within_region = (fr)->offset_in_region, \
155 .size = int128_get64((fr)->addr.size), \
156 .offset_within_address_space = int128_get64((fr)->addr.start), \
157 .readonly = (fr)->readonly, \
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 FlatRange *ranges;
227 unsigned nr;
228 unsigned nr_allocated;
231 typedef struct AddressSpaceOps AddressSpaceOps;
233 #define FOR_EACH_FLAT_RANGE(var, view) \
234 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
236 static bool flatrange_equal(FlatRange *a, FlatRange *b)
238 return a->mr == b->mr
239 && addrrange_equal(a->addr, b->addr)
240 && a->offset_in_region == b->offset_in_region
241 && a->romd_mode == b->romd_mode
242 && a->readonly == b->readonly;
245 static void flatview_init(FlatView *view)
247 view->ranges = NULL;
248 view->nr = 0;
249 view->nr_allocated = 0;
252 /* Insert a range into a given position. Caller is responsible for maintaining
253 * sorting order.
255 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
257 if (view->nr == view->nr_allocated) {
258 view->nr_allocated = MAX(2 * view->nr, 10);
259 view->ranges = g_realloc(view->ranges,
260 view->nr_allocated * sizeof(*view->ranges));
262 memmove(view->ranges + pos + 1, view->ranges + pos,
263 (view->nr - pos) * sizeof(FlatRange));
264 view->ranges[pos] = *range;
265 ++view->nr;
268 static void flatview_destroy(FlatView *view)
270 g_free(view->ranges);
273 static bool can_merge(FlatRange *r1, FlatRange *r2)
275 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
276 && r1->mr == r2->mr
277 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
278 r1->addr.size),
279 int128_make64(r2->offset_in_region))
280 && r1->dirty_log_mask == r2->dirty_log_mask
281 && r1->romd_mode == r2->romd_mode
282 && r1->readonly == r2->readonly;
285 /* Attempt to simplify a view by merging ajacent ranges */
286 static void flatview_simplify(FlatView *view)
288 unsigned i, j;
290 i = 0;
291 while (i < view->nr) {
292 j = i + 1;
293 while (j < view->nr
294 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
295 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
296 ++j;
298 ++i;
299 memmove(&view->ranges[i], &view->ranges[j],
300 (view->nr - j) * sizeof(view->ranges[j]));
301 view->nr -= j - i;
305 static void memory_region_read_accessor(void *opaque,
306 hwaddr addr,
307 uint64_t *value,
308 unsigned size,
309 unsigned shift,
310 uint64_t mask)
312 MemoryRegion *mr = opaque;
313 uint64_t tmp;
315 if (mr->flush_coalesced_mmio) {
316 qemu_flush_coalesced_mmio_buffer();
318 tmp = mr->ops->read(mr->opaque, addr, size);
319 *value |= (tmp & mask) << shift;
322 static void memory_region_write_accessor(void *opaque,
323 hwaddr addr,
324 uint64_t *value,
325 unsigned size,
326 unsigned shift,
327 uint64_t mask)
329 MemoryRegion *mr = opaque;
330 uint64_t tmp;
332 if (mr->flush_coalesced_mmio) {
333 qemu_flush_coalesced_mmio_buffer();
335 tmp = (*value >> shift) & mask;
336 mr->ops->write(mr->opaque, addr, tmp, size);
339 static void access_with_adjusted_size(hwaddr addr,
340 uint64_t *value,
341 unsigned size,
342 unsigned access_size_min,
343 unsigned access_size_max,
344 void (*access)(void *opaque,
345 hwaddr addr,
346 uint64_t *value,
347 unsigned size,
348 unsigned shift,
349 uint64_t mask),
350 void *opaque)
352 uint64_t access_mask;
353 unsigned access_size;
354 unsigned i;
356 if (!access_size_min) {
357 access_size_min = 1;
359 if (!access_size_max) {
360 access_size_max = 4;
362 access_size = MAX(MIN(size, access_size_max), access_size_min);
363 access_mask = -1ULL >> (64 - access_size * 8);
364 for (i = 0; i < size; i += access_size) {
365 /* FIXME: big-endian support */
366 access(opaque, addr + i, value, access_size, i * 8, access_mask);
370 static const MemoryRegionPortio *find_portio(MemoryRegion *mr, uint64_t offset,
371 unsigned width, bool write)
373 const MemoryRegionPortio *mrp;
375 for (mrp = mr->ops->old_portio; mrp->size; ++mrp) {
376 if (offset >= mrp->offset && offset < mrp->offset + mrp->len
377 && width == mrp->size
378 && (write ? (bool)mrp->write : (bool)mrp->read)) {
379 return mrp;
382 return NULL;
385 static void memory_region_iorange_read(IORange *iorange,
386 uint64_t offset,
387 unsigned width,
388 uint64_t *data)
390 MemoryRegionIORange *mrio
391 = container_of(iorange, MemoryRegionIORange, iorange);
392 MemoryRegion *mr = mrio->mr;
394 offset += mrio->offset;
395 if (mr->ops->old_portio) {
396 const MemoryRegionPortio *mrp = find_portio(mr, offset - mrio->offset,
397 width, false);
399 *data = ((uint64_t)1 << (width * 8)) - 1;
400 if (mrp) {
401 *data = mrp->read(mr->opaque, offset);
402 } else if (width == 2) {
403 mrp = find_portio(mr, offset - mrio->offset, 1, false);
404 assert(mrp);
405 *data = mrp->read(mr->opaque, offset) |
406 (mrp->read(mr->opaque, offset + 1) << 8);
408 return;
410 *data = 0;
411 access_with_adjusted_size(offset, data, width,
412 mr->ops->impl.min_access_size,
413 mr->ops->impl.max_access_size,
414 memory_region_read_accessor, mr);
417 static void memory_region_iorange_write(IORange *iorange,
418 uint64_t offset,
419 unsigned width,
420 uint64_t data)
422 MemoryRegionIORange *mrio
423 = container_of(iorange, MemoryRegionIORange, iorange);
424 MemoryRegion *mr = mrio->mr;
426 offset += mrio->offset;
427 if (mr->ops->old_portio) {
428 const MemoryRegionPortio *mrp = find_portio(mr, offset - mrio->offset,
429 width, true);
431 if (mrp) {
432 mrp->write(mr->opaque, offset, data);
433 } else if (width == 2) {
434 mrp = find_portio(mr, offset - mrio->offset, 1, true);
435 assert(mrp);
436 mrp->write(mr->opaque, offset, data & 0xff);
437 mrp->write(mr->opaque, offset + 1, data >> 8);
439 return;
441 access_with_adjusted_size(offset, &data, width,
442 mr->ops->impl.min_access_size,
443 mr->ops->impl.max_access_size,
444 memory_region_write_accessor, mr);
447 static void memory_region_iorange_destructor(IORange *iorange)
449 g_free(container_of(iorange, MemoryRegionIORange, iorange));
452 const IORangeOps memory_region_iorange_ops = {
453 .read = memory_region_iorange_read,
454 .write = memory_region_iorange_write,
455 .destructor = memory_region_iorange_destructor,
458 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
460 AddressSpace *as;
462 while (mr->parent) {
463 mr = mr->parent;
465 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
466 if (mr == as->root) {
467 return as;
470 abort();
473 /* Render a memory region into the global view. Ranges in @view obscure
474 * ranges in @mr.
476 static void render_memory_region(FlatView *view,
477 MemoryRegion *mr,
478 Int128 base,
479 AddrRange clip,
480 bool readonly)
482 MemoryRegion *subregion;
483 unsigned i;
484 hwaddr offset_in_region;
485 Int128 remain;
486 Int128 now;
487 FlatRange fr;
488 AddrRange tmp;
490 if (!mr->enabled) {
491 return;
494 int128_addto(&base, int128_make64(mr->addr));
495 readonly |= mr->readonly;
497 tmp = addrrange_make(base, mr->size);
499 if (!addrrange_intersects(tmp, clip)) {
500 return;
503 clip = addrrange_intersection(tmp, clip);
505 if (mr->alias) {
506 int128_subfrom(&base, int128_make64(mr->alias->addr));
507 int128_subfrom(&base, int128_make64(mr->alias_offset));
508 render_memory_region(view, mr->alias, base, clip, readonly);
509 return;
512 /* Render subregions in priority order. */
513 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
514 render_memory_region(view, subregion, base, clip, readonly);
517 if (!mr->terminates) {
518 return;
521 offset_in_region = int128_get64(int128_sub(clip.start, base));
522 base = clip.start;
523 remain = clip.size;
525 /* Render the region itself into any gaps left by the current view. */
526 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
527 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
528 continue;
530 if (int128_lt(base, view->ranges[i].addr.start)) {
531 now = int128_min(remain,
532 int128_sub(view->ranges[i].addr.start, base));
533 fr.mr = mr;
534 fr.offset_in_region = offset_in_region;
535 fr.addr = addrrange_make(base, now);
536 fr.dirty_log_mask = mr->dirty_log_mask;
537 fr.romd_mode = mr->romd_mode;
538 fr.readonly = readonly;
539 flatview_insert(view, i, &fr);
540 ++i;
541 int128_addto(&base, now);
542 offset_in_region += int128_get64(now);
543 int128_subfrom(&remain, now);
545 now = int128_sub(int128_min(int128_add(base, remain),
546 addrrange_end(view->ranges[i].addr)),
547 base);
548 int128_addto(&base, now);
549 offset_in_region += int128_get64(now);
550 int128_subfrom(&remain, now);
552 if (int128_nz(remain)) {
553 fr.mr = mr;
554 fr.offset_in_region = offset_in_region;
555 fr.addr = addrrange_make(base, remain);
556 fr.dirty_log_mask = mr->dirty_log_mask;
557 fr.romd_mode = mr->romd_mode;
558 fr.readonly = readonly;
559 flatview_insert(view, i, &fr);
563 /* Render a memory topology into a list of disjoint absolute ranges. */
564 static FlatView generate_memory_topology(MemoryRegion *mr)
566 FlatView view;
568 flatview_init(&view);
570 if (mr) {
571 render_memory_region(&view, mr, int128_zero(),
572 addrrange_make(int128_zero(), int128_2_64()), false);
574 flatview_simplify(&view);
576 return view;
579 static void address_space_add_del_ioeventfds(AddressSpace *as,
580 MemoryRegionIoeventfd *fds_new,
581 unsigned fds_new_nb,
582 MemoryRegionIoeventfd *fds_old,
583 unsigned fds_old_nb)
585 unsigned iold, inew;
586 MemoryRegionIoeventfd *fd;
587 MemoryRegionSection section;
589 /* Generate a symmetric difference of the old and new fd sets, adding
590 * and deleting as necessary.
593 iold = inew = 0;
594 while (iold < fds_old_nb || inew < fds_new_nb) {
595 if (iold < fds_old_nb
596 && (inew == fds_new_nb
597 || memory_region_ioeventfd_before(fds_old[iold],
598 fds_new[inew]))) {
599 fd = &fds_old[iold];
600 section = (MemoryRegionSection) {
601 .address_space = as,
602 .offset_within_address_space = int128_get64(fd->addr.start),
603 .size = int128_get64(fd->addr.size),
605 MEMORY_LISTENER_CALL(eventfd_del, Forward, &section,
606 fd->match_data, fd->data, fd->e);
607 ++iold;
608 } else if (inew < fds_new_nb
609 && (iold == fds_old_nb
610 || memory_region_ioeventfd_before(fds_new[inew],
611 fds_old[iold]))) {
612 fd = &fds_new[inew];
613 section = (MemoryRegionSection) {
614 .address_space = as,
615 .offset_within_address_space = int128_get64(fd->addr.start),
616 .size = int128_get64(fd->addr.size),
618 MEMORY_LISTENER_CALL(eventfd_add, Reverse, &section,
619 fd->match_data, fd->data, fd->e);
620 ++inew;
621 } else {
622 ++iold;
623 ++inew;
628 static void address_space_update_ioeventfds(AddressSpace *as)
630 FlatRange *fr;
631 unsigned ioeventfd_nb = 0;
632 MemoryRegionIoeventfd *ioeventfds = NULL;
633 AddrRange tmp;
634 unsigned i;
636 FOR_EACH_FLAT_RANGE(fr, as->current_map) {
637 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
638 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
639 int128_sub(fr->addr.start,
640 int128_make64(fr->offset_in_region)));
641 if (addrrange_intersects(fr->addr, tmp)) {
642 ++ioeventfd_nb;
643 ioeventfds = g_realloc(ioeventfds,
644 ioeventfd_nb * sizeof(*ioeventfds));
645 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
646 ioeventfds[ioeventfd_nb-1].addr = tmp;
651 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
652 as->ioeventfds, as->ioeventfd_nb);
654 g_free(as->ioeventfds);
655 as->ioeventfds = ioeventfds;
656 as->ioeventfd_nb = ioeventfd_nb;
659 static void address_space_update_topology_pass(AddressSpace *as,
660 FlatView old_view,
661 FlatView new_view,
662 bool adding)
664 unsigned iold, inew;
665 FlatRange *frold, *frnew;
667 /* Generate a symmetric difference of the old and new memory maps.
668 * Kill ranges in the old map, and instantiate ranges in the new map.
670 iold = inew = 0;
671 while (iold < old_view.nr || inew < new_view.nr) {
672 if (iold < old_view.nr) {
673 frold = &old_view.ranges[iold];
674 } else {
675 frold = NULL;
677 if (inew < new_view.nr) {
678 frnew = &new_view.ranges[inew];
679 } else {
680 frnew = NULL;
683 if (frold
684 && (!frnew
685 || int128_lt(frold->addr.start, frnew->addr.start)
686 || (int128_eq(frold->addr.start, frnew->addr.start)
687 && !flatrange_equal(frold, frnew)))) {
688 /* In old, but (not in new, or in new but attributes changed). */
690 if (!adding) {
691 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
694 ++iold;
695 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
696 /* In both (logging may have changed) */
698 if (adding) {
699 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
700 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
701 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop);
702 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
703 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start);
707 ++iold;
708 ++inew;
709 } else {
710 /* In new */
712 if (adding) {
713 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
716 ++inew;
722 static void address_space_update_topology(AddressSpace *as)
724 FlatView old_view = *as->current_map;
725 FlatView new_view = generate_memory_topology(as->root);
727 address_space_update_topology_pass(as, old_view, new_view, false);
728 address_space_update_topology_pass(as, old_view, new_view, true);
730 *as->current_map = new_view;
731 flatview_destroy(&old_view);
732 address_space_update_ioeventfds(as);
735 void memory_region_transaction_begin(void)
737 qemu_flush_coalesced_mmio_buffer();
738 ++memory_region_transaction_depth;
741 void memory_region_transaction_commit(void)
743 AddressSpace *as;
745 assert(memory_region_transaction_depth);
746 --memory_region_transaction_depth;
747 if (!memory_region_transaction_depth && memory_region_update_pending) {
748 memory_region_update_pending = false;
749 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
751 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
752 address_space_update_topology(as);
755 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
759 static void memory_region_destructor_none(MemoryRegion *mr)
763 static void memory_region_destructor_ram(MemoryRegion *mr)
765 qemu_ram_free(mr->ram_addr);
768 static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
770 qemu_ram_free_from_ptr(mr->ram_addr);
773 static void memory_region_destructor_rom_device(MemoryRegion *mr)
775 qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
778 static bool memory_region_wrong_endianness(MemoryRegion *mr)
780 #ifdef TARGET_WORDS_BIGENDIAN
781 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
782 #else
783 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
784 #endif
787 void memory_region_init(MemoryRegion *mr,
788 const char *name,
789 uint64_t size)
791 mr->ops = &unassigned_mem_ops;
792 mr->opaque = NULL;
793 mr->parent = NULL;
794 mr->size = int128_make64(size);
795 if (size == UINT64_MAX) {
796 mr->size = int128_2_64();
798 mr->addr = 0;
799 mr->subpage = false;
800 mr->enabled = true;
801 mr->terminates = false;
802 mr->ram = false;
803 mr->romd_mode = true;
804 mr->readonly = false;
805 mr->rom_device = false;
806 mr->destructor = memory_region_destructor_none;
807 mr->priority = 0;
808 mr->may_overlap = false;
809 mr->alias = NULL;
810 QTAILQ_INIT(&mr->subregions);
811 memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
812 QTAILQ_INIT(&mr->coalesced);
813 mr->name = g_strdup(name);
814 mr->dirty_log_mask = 0;
815 mr->ioeventfd_nb = 0;
816 mr->ioeventfds = NULL;
817 mr->flush_coalesced_mmio = false;
820 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
821 unsigned size)
823 #ifdef DEBUG_UNASSIGNED
824 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
825 #endif
826 #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
827 cpu_unassigned_access(cpu_single_env, addr, 0, 0, 0, size);
828 #endif
829 return 0;
832 static void unassigned_mem_write(void *opaque, hwaddr addr,
833 uint64_t val, unsigned size)
835 #ifdef DEBUG_UNASSIGNED
836 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
837 #endif
838 #if defined(TARGET_ALPHA) || defined(TARGET_SPARC) || defined(TARGET_MICROBLAZE)
839 cpu_unassigned_access(cpu_single_env, addr, 1, 0, 0, size);
840 #endif
843 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
844 unsigned size, bool is_write)
846 return false;
849 const MemoryRegionOps unassigned_mem_ops = {
850 .valid.accepts = unassigned_mem_accepts,
851 .endianness = DEVICE_NATIVE_ENDIAN,
854 static bool memory_region_access_valid(MemoryRegion *mr,
855 hwaddr addr,
856 unsigned size,
857 bool is_write)
859 if (mr->ops->valid.accepts
860 && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write)) {
861 return false;
864 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
865 return false;
868 /* Treat zero as compatibility all valid */
869 if (!mr->ops->valid.max_access_size) {
870 return true;
873 if (size > mr->ops->valid.max_access_size
874 || size < mr->ops->valid.min_access_size) {
875 return false;
877 return true;
880 static uint64_t memory_region_dispatch_read1(MemoryRegion *mr,
881 hwaddr addr,
882 unsigned size)
884 uint64_t data = 0;
886 if (!memory_region_access_valid(mr, addr, size, false)) {
887 return unassigned_mem_read(mr, addr, size);
890 if (!mr->ops->read) {
891 return mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);
894 /* FIXME: support unaligned access */
895 access_with_adjusted_size(addr, &data, size,
896 mr->ops->impl.min_access_size,
897 mr->ops->impl.max_access_size,
898 memory_region_read_accessor, mr);
900 return data;
903 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
905 if (memory_region_wrong_endianness(mr)) {
906 switch (size) {
907 case 1:
908 break;
909 case 2:
910 *data = bswap16(*data);
911 break;
912 case 4:
913 *data = bswap32(*data);
914 break;
915 default:
916 abort();
921 static uint64_t memory_region_dispatch_read(MemoryRegion *mr,
922 hwaddr addr,
923 unsigned size)
925 uint64_t ret;
927 ret = memory_region_dispatch_read1(mr, addr, size);
928 adjust_endianness(mr, &ret, size);
929 return ret;
932 static void memory_region_dispatch_write(MemoryRegion *mr,
933 hwaddr addr,
934 uint64_t data,
935 unsigned size)
937 if (!memory_region_access_valid(mr, addr, size, true)) {
938 unassigned_mem_write(mr, addr, data, size);
939 return;
942 adjust_endianness(mr, &data, size);
944 if (!mr->ops->write) {
945 mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, data);
946 return;
949 /* FIXME: support unaligned access */
950 access_with_adjusted_size(addr, &data, size,
951 mr->ops->impl.min_access_size,
952 mr->ops->impl.max_access_size,
953 memory_region_write_accessor, mr);
956 void memory_region_init_io(MemoryRegion *mr,
957 const MemoryRegionOps *ops,
958 void *opaque,
959 const char *name,
960 uint64_t size)
962 memory_region_init(mr, name, size);
963 mr->ops = ops;
964 mr->opaque = opaque;
965 mr->terminates = true;
966 mr->ram_addr = ~(ram_addr_t)0;
969 void memory_region_init_ram(MemoryRegion *mr,
970 const char *name,
971 uint64_t size)
973 memory_region_init(mr, name, size);
974 mr->ram = true;
975 mr->terminates = true;
976 mr->destructor = memory_region_destructor_ram;
977 mr->ram_addr = qemu_ram_alloc(size, mr);
980 void memory_region_init_ram_ptr(MemoryRegion *mr,
981 const char *name,
982 uint64_t size,
983 void *ptr)
985 memory_region_init(mr, name, size);
986 mr->ram = true;
987 mr->terminates = true;
988 mr->destructor = memory_region_destructor_ram_from_ptr;
989 mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr);
992 void memory_region_init_alias(MemoryRegion *mr,
993 const char *name,
994 MemoryRegion *orig,
995 hwaddr offset,
996 uint64_t size)
998 memory_region_init(mr, name, size);
999 mr->alias = orig;
1000 mr->alias_offset = offset;
1003 void memory_region_init_rom_device(MemoryRegion *mr,
1004 const MemoryRegionOps *ops,
1005 void *opaque,
1006 const char *name,
1007 uint64_t size)
1009 memory_region_init(mr, name, size);
1010 mr->ops = ops;
1011 mr->opaque = opaque;
1012 mr->terminates = true;
1013 mr->rom_device = true;
1014 mr->destructor = memory_region_destructor_rom_device;
1015 mr->ram_addr = qemu_ram_alloc(size, mr);
1018 void memory_region_init_reservation(MemoryRegion *mr,
1019 const char *name,
1020 uint64_t size)
1022 memory_region_init_io(mr, &unassigned_mem_ops, mr, name, size);
1025 void memory_region_destroy(MemoryRegion *mr)
1027 assert(QTAILQ_EMPTY(&mr->subregions));
1028 assert(memory_region_transaction_depth == 0);
1029 mr->destructor(mr);
1030 memory_region_clear_coalescing(mr);
1031 g_free((char *)mr->name);
1032 g_free(mr->ioeventfds);
1035 uint64_t memory_region_size(MemoryRegion *mr)
1037 if (int128_eq(mr->size, int128_2_64())) {
1038 return UINT64_MAX;
1040 return int128_get64(mr->size);
1043 const char *memory_region_name(MemoryRegion *mr)
1045 return mr->name;
1048 bool memory_region_is_ram(MemoryRegion *mr)
1050 return mr->ram;
1053 bool memory_region_is_logging(MemoryRegion *mr)
1055 return mr->dirty_log_mask;
1058 bool memory_region_is_rom(MemoryRegion *mr)
1060 return mr->ram && mr->readonly;
1063 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1065 uint8_t mask = 1 << client;
1067 memory_region_transaction_begin();
1068 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1069 memory_region_update_pending |= mr->enabled;
1070 memory_region_transaction_commit();
1073 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1074 hwaddr size, unsigned client)
1076 assert(mr->terminates);
1077 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, size,
1078 1 << client);
1081 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1082 hwaddr size)
1084 assert(mr->terminates);
1085 return cpu_physical_memory_set_dirty_range(mr->ram_addr + addr, size, -1);
1088 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
1089 hwaddr size, unsigned client)
1091 bool ret;
1092 assert(mr->terminates);
1093 ret = cpu_physical_memory_get_dirty(mr->ram_addr + addr, size,
1094 1 << client);
1095 if (ret) {
1096 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1097 mr->ram_addr + addr + size,
1098 1 << client);
1100 return ret;
1104 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1106 AddressSpace *as;
1107 FlatRange *fr;
1109 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1110 FOR_EACH_FLAT_RANGE(fr, as->current_map) {
1111 if (fr->mr == mr) {
1112 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1118 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1120 if (mr->readonly != readonly) {
1121 memory_region_transaction_begin();
1122 mr->readonly = readonly;
1123 memory_region_update_pending |= mr->enabled;
1124 memory_region_transaction_commit();
1128 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
1130 if (mr->romd_mode != romd_mode) {
1131 memory_region_transaction_begin();
1132 mr->romd_mode = romd_mode;
1133 memory_region_update_pending |= mr->enabled;
1134 memory_region_transaction_commit();
1138 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1139 hwaddr size, unsigned client)
1141 assert(mr->terminates);
1142 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1143 mr->ram_addr + addr + size,
1144 1 << client);
1147 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1149 if (mr->alias) {
1150 return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset;
1153 assert(mr->terminates);
1155 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
1158 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
1160 FlatRange *fr;
1161 CoalescedMemoryRange *cmr;
1162 AddrRange tmp;
1163 MemoryRegionSection section;
1165 FOR_EACH_FLAT_RANGE(fr, as->current_map) {
1166 if (fr->mr == mr) {
1167 section = (MemoryRegionSection) {
1168 .address_space = as,
1169 .offset_within_address_space = int128_get64(fr->addr.start),
1170 .size = int128_get64(fr->addr.size),
1173 MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, &section,
1174 int128_get64(fr->addr.start),
1175 int128_get64(fr->addr.size));
1176 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1177 tmp = addrrange_shift(cmr->addr,
1178 int128_sub(fr->addr.start,
1179 int128_make64(fr->offset_in_region)));
1180 if (!addrrange_intersects(tmp, fr->addr)) {
1181 continue;
1183 tmp = addrrange_intersection(tmp, fr->addr);
1184 MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, &section,
1185 int128_get64(tmp.start),
1186 int128_get64(tmp.size));
1192 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1194 AddressSpace *as;
1196 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1197 memory_region_update_coalesced_range_as(mr, as);
1201 void memory_region_set_coalescing(MemoryRegion *mr)
1203 memory_region_clear_coalescing(mr);
1204 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1207 void memory_region_add_coalescing(MemoryRegion *mr,
1208 hwaddr offset,
1209 uint64_t size)
1211 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1213 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1214 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1215 memory_region_update_coalesced_range(mr);
1216 memory_region_set_flush_coalesced(mr);
1219 void memory_region_clear_coalescing(MemoryRegion *mr)
1221 CoalescedMemoryRange *cmr;
1223 qemu_flush_coalesced_mmio_buffer();
1224 mr->flush_coalesced_mmio = false;
1226 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1227 cmr = QTAILQ_FIRST(&mr->coalesced);
1228 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1229 g_free(cmr);
1231 memory_region_update_coalesced_range(mr);
1234 void memory_region_set_flush_coalesced(MemoryRegion *mr)
1236 mr->flush_coalesced_mmio = true;
1239 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
1241 qemu_flush_coalesced_mmio_buffer();
1242 if (QTAILQ_EMPTY(&mr->coalesced)) {
1243 mr->flush_coalesced_mmio = false;
1247 void memory_region_add_eventfd(MemoryRegion *mr,
1248 hwaddr addr,
1249 unsigned size,
1250 bool match_data,
1251 uint64_t data,
1252 EventNotifier *e)
1254 MemoryRegionIoeventfd mrfd = {
1255 .addr.start = int128_make64(addr),
1256 .addr.size = int128_make64(size),
1257 .match_data = match_data,
1258 .data = data,
1259 .e = e,
1261 unsigned i;
1263 adjust_endianness(mr, &mrfd.data, size);
1264 memory_region_transaction_begin();
1265 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1266 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1267 break;
1270 ++mr->ioeventfd_nb;
1271 mr->ioeventfds = g_realloc(mr->ioeventfds,
1272 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1273 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1274 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1275 mr->ioeventfds[i] = mrfd;
1276 memory_region_update_pending |= mr->enabled;
1277 memory_region_transaction_commit();
1280 void memory_region_del_eventfd(MemoryRegion *mr,
1281 hwaddr addr,
1282 unsigned size,
1283 bool match_data,
1284 uint64_t data,
1285 EventNotifier *e)
1287 MemoryRegionIoeventfd mrfd = {
1288 .addr.start = int128_make64(addr),
1289 .addr.size = int128_make64(size),
1290 .match_data = match_data,
1291 .data = data,
1292 .e = e,
1294 unsigned i;
1296 adjust_endianness(mr, &mrfd.data, size);
1297 memory_region_transaction_begin();
1298 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1299 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1300 break;
1303 assert(i != mr->ioeventfd_nb);
1304 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1305 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1306 --mr->ioeventfd_nb;
1307 mr->ioeventfds = g_realloc(mr->ioeventfds,
1308 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1309 memory_region_update_pending |= mr->enabled;
1310 memory_region_transaction_commit();
1313 static void memory_region_add_subregion_common(MemoryRegion *mr,
1314 hwaddr offset,
1315 MemoryRegion *subregion)
1317 MemoryRegion *other;
1319 memory_region_transaction_begin();
1321 assert(!subregion->parent);
1322 subregion->parent = mr;
1323 subregion->addr = offset;
1324 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1325 if (subregion->may_overlap || other->may_overlap) {
1326 continue;
1328 if (int128_ge(int128_make64(offset),
1329 int128_add(int128_make64(other->addr), other->size))
1330 || int128_le(int128_add(int128_make64(offset), subregion->size),
1331 int128_make64(other->addr))) {
1332 continue;
1334 #if 0
1335 printf("warning: subregion collision %llx/%llx (%s) "
1336 "vs %llx/%llx (%s)\n",
1337 (unsigned long long)offset,
1338 (unsigned long long)int128_get64(subregion->size),
1339 subregion->name,
1340 (unsigned long long)other->addr,
1341 (unsigned long long)int128_get64(other->size),
1342 other->name);
1343 #endif
1345 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1346 if (subregion->priority >= other->priority) {
1347 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1348 goto done;
1351 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1352 done:
1353 memory_region_update_pending |= mr->enabled && subregion->enabled;
1354 memory_region_transaction_commit();
1358 void memory_region_add_subregion(MemoryRegion *mr,
1359 hwaddr offset,
1360 MemoryRegion *subregion)
1362 subregion->may_overlap = false;
1363 subregion->priority = 0;
1364 memory_region_add_subregion_common(mr, offset, subregion);
1367 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1368 hwaddr offset,
1369 MemoryRegion *subregion,
1370 unsigned priority)
1372 subregion->may_overlap = true;
1373 subregion->priority = priority;
1374 memory_region_add_subregion_common(mr, offset, subregion);
1377 void memory_region_del_subregion(MemoryRegion *mr,
1378 MemoryRegion *subregion)
1380 memory_region_transaction_begin();
1381 assert(subregion->parent == mr);
1382 subregion->parent = NULL;
1383 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1384 memory_region_update_pending |= mr->enabled && subregion->enabled;
1385 memory_region_transaction_commit();
1388 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1390 if (enabled == mr->enabled) {
1391 return;
1393 memory_region_transaction_begin();
1394 mr->enabled = enabled;
1395 memory_region_update_pending = true;
1396 memory_region_transaction_commit();
1399 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
1401 MemoryRegion *parent = mr->parent;
1402 unsigned priority = mr->priority;
1403 bool may_overlap = mr->may_overlap;
1405 if (addr == mr->addr || !parent) {
1406 mr->addr = addr;
1407 return;
1410 memory_region_transaction_begin();
1411 memory_region_del_subregion(parent, mr);
1412 if (may_overlap) {
1413 memory_region_add_subregion_overlap(parent, addr, mr, priority);
1414 } else {
1415 memory_region_add_subregion(parent, addr, mr);
1417 memory_region_transaction_commit();
1420 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
1422 assert(mr->alias);
1424 if (offset == mr->alias_offset) {
1425 return;
1428 memory_region_transaction_begin();
1429 mr->alias_offset = offset;
1430 memory_region_update_pending |= mr->enabled;
1431 memory_region_transaction_commit();
1434 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1436 return mr->ram_addr;
1439 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1441 const AddrRange *addr = addr_;
1442 const FlatRange *fr = fr_;
1444 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1445 return -1;
1446 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1447 return 1;
1449 return 0;
1452 static FlatRange *address_space_lookup(AddressSpace *as, AddrRange addr)
1454 return bsearch(&addr, as->current_map->ranges, as->current_map->nr,
1455 sizeof(FlatRange), cmp_flatrange_addr);
1458 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1459 hwaddr addr, uint64_t size)
1461 MemoryRegionSection ret = { .mr = NULL, .size = 0 };
1462 MemoryRegion *root;
1463 AddressSpace *as;
1464 AddrRange range;
1465 FlatRange *fr;
1467 addr += mr->addr;
1468 for (root = mr; root->parent; ) {
1469 root = root->parent;
1470 addr += root->addr;
1473 as = memory_region_to_address_space(root);
1474 range = addrrange_make(int128_make64(addr), int128_make64(size));
1475 fr = address_space_lookup(as, range);
1476 if (!fr) {
1477 return ret;
1480 while (fr > as->current_map->ranges
1481 && addrrange_intersects(fr[-1].addr, range)) {
1482 --fr;
1485 ret.mr = fr->mr;
1486 ret.address_space = as;
1487 range = addrrange_intersection(range, fr->addr);
1488 ret.offset_within_region = fr->offset_in_region;
1489 ret.offset_within_region += int128_get64(int128_sub(range.start,
1490 fr->addr.start));
1491 ret.size = int128_get64(range.size);
1492 ret.offset_within_address_space = int128_get64(range.start);
1493 ret.readonly = fr->readonly;
1494 return ret;
1497 void address_space_sync_dirty_bitmap(AddressSpace *as)
1499 FlatRange *fr;
1501 FOR_EACH_FLAT_RANGE(fr, as->current_map) {
1502 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1506 void memory_global_dirty_log_start(void)
1508 global_dirty_log = true;
1509 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
1512 void memory_global_dirty_log_stop(void)
1514 global_dirty_log = false;
1515 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
1518 static void listener_add_address_space(MemoryListener *listener,
1519 AddressSpace *as)
1521 FlatRange *fr;
1523 if (listener->address_space_filter
1524 && listener->address_space_filter != as) {
1525 return;
1528 if (global_dirty_log) {
1529 if (listener->log_global_start) {
1530 listener->log_global_start(listener);
1534 FOR_EACH_FLAT_RANGE(fr, as->current_map) {
1535 MemoryRegionSection section = {
1536 .mr = fr->mr,
1537 .address_space = as,
1538 .offset_within_region = fr->offset_in_region,
1539 .size = int128_get64(fr->addr.size),
1540 .offset_within_address_space = int128_get64(fr->addr.start),
1541 .readonly = fr->readonly,
1543 if (listener->region_add) {
1544 listener->region_add(listener, &section);
1549 void memory_listener_register(MemoryListener *listener, AddressSpace *filter)
1551 MemoryListener *other = NULL;
1552 AddressSpace *as;
1554 listener->address_space_filter = filter;
1555 if (QTAILQ_EMPTY(&memory_listeners)
1556 || listener->priority >= QTAILQ_LAST(&memory_listeners,
1557 memory_listeners)->priority) {
1558 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
1559 } else {
1560 QTAILQ_FOREACH(other, &memory_listeners, link) {
1561 if (listener->priority < other->priority) {
1562 break;
1565 QTAILQ_INSERT_BEFORE(other, listener, link);
1568 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1569 listener_add_address_space(listener, as);
1573 void memory_listener_unregister(MemoryListener *listener)
1575 QTAILQ_REMOVE(&memory_listeners, listener, link);
1578 void address_space_init(AddressSpace *as, MemoryRegion *root)
1580 memory_region_transaction_begin();
1581 as->root = root;
1582 as->current_map = g_new(FlatView, 1);
1583 flatview_init(as->current_map);
1584 as->ioeventfd_nb = 0;
1585 as->ioeventfds = NULL;
1586 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
1587 as->name = NULL;
1588 address_space_init_dispatch(as);
1589 memory_region_update_pending |= root->enabled;
1590 memory_region_transaction_commit();
1593 void address_space_destroy(AddressSpace *as)
1595 /* Flush out anything from MemoryListeners listening in on this */
1596 memory_region_transaction_begin();
1597 as->root = NULL;
1598 memory_region_transaction_commit();
1599 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
1600 address_space_destroy_dispatch(as);
1601 flatview_destroy(as->current_map);
1602 g_free(as->current_map);
1603 g_free(as->ioeventfds);
1606 uint64_t io_mem_read(MemoryRegion *mr, hwaddr addr, unsigned size)
1608 return memory_region_dispatch_read(mr, addr, size);
1611 void io_mem_write(MemoryRegion *mr, hwaddr addr,
1612 uint64_t val, unsigned size)
1614 memory_region_dispatch_write(mr, addr, val, size);
1617 typedef struct MemoryRegionList MemoryRegionList;
1619 struct MemoryRegionList {
1620 const MemoryRegion *mr;
1621 bool printed;
1622 QTAILQ_ENTRY(MemoryRegionList) queue;
1625 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1627 static void mtree_print_mr(fprintf_function mon_printf, void *f,
1628 const MemoryRegion *mr, unsigned int level,
1629 hwaddr base,
1630 MemoryRegionListHead *alias_print_queue)
1632 MemoryRegionList *new_ml, *ml, *next_ml;
1633 MemoryRegionListHead submr_print_queue;
1634 const MemoryRegion *submr;
1635 unsigned int i;
1637 if (!mr || !mr->enabled) {
1638 return;
1641 for (i = 0; i < level; i++) {
1642 mon_printf(f, " ");
1645 if (mr->alias) {
1646 MemoryRegionList *ml;
1647 bool found = false;
1649 /* check if the alias is already in the queue */
1650 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
1651 if (ml->mr == mr->alias && !ml->printed) {
1652 found = true;
1656 if (!found) {
1657 ml = g_new(MemoryRegionList, 1);
1658 ml->mr = mr->alias;
1659 ml->printed = false;
1660 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
1662 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
1663 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1664 "-" TARGET_FMT_plx "\n",
1665 base + mr->addr,
1666 base + mr->addr
1667 + (hwaddr)int128_get64(mr->size) - 1,
1668 mr->priority,
1669 mr->romd_mode ? 'R' : '-',
1670 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
1671 : '-',
1672 mr->name,
1673 mr->alias->name,
1674 mr->alias_offset,
1675 mr->alias_offset
1676 + (hwaddr)int128_get64(mr->size) - 1);
1677 } else {
1678 mon_printf(f,
1679 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %c%c): %s\n",
1680 base + mr->addr,
1681 base + mr->addr
1682 + (hwaddr)int128_get64(mr->size) - 1,
1683 mr->priority,
1684 mr->romd_mode ? 'R' : '-',
1685 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
1686 : '-',
1687 mr->name);
1690 QTAILQ_INIT(&submr_print_queue);
1692 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
1693 new_ml = g_new(MemoryRegionList, 1);
1694 new_ml->mr = submr;
1695 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1696 if (new_ml->mr->addr < ml->mr->addr ||
1697 (new_ml->mr->addr == ml->mr->addr &&
1698 new_ml->mr->priority > ml->mr->priority)) {
1699 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
1700 new_ml = NULL;
1701 break;
1704 if (new_ml) {
1705 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
1709 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1710 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
1711 alias_print_queue);
1714 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
1715 g_free(ml);
1719 void mtree_info(fprintf_function mon_printf, void *f)
1721 MemoryRegionListHead ml_head;
1722 MemoryRegionList *ml, *ml2;
1723 AddressSpace *as;
1725 QTAILQ_INIT(&ml_head);
1727 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1728 if (!as->name) {
1729 continue;
1731 mon_printf(f, "%s\n", as->name);
1732 mtree_print_mr(mon_printf, f, as->root, 0, 0, &ml_head);
1735 mon_printf(f, "aliases\n");
1736 /* print aliased regions */
1737 QTAILQ_FOREACH(ml, &ml_head, queue) {
1738 if (!ml->printed) {
1739 mon_printf(f, "%s\n", ml->mr->name);
1740 mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head);
1744 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
1745 g_free(ml);