exynos4210_i2c: QOM cast cleanup
[qemu/qmp-unstable.git] / memory.c
blobac6f3c6a190c30b376c1b1b94a7f0d1783143085
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 "trace.h"
23 #include <assert.h>
25 #include "exec/memory-internal.h"
27 //#define DEBUG_UNASSIGNED
29 static unsigned memory_region_transaction_depth;
30 static bool memory_region_update_pending;
31 static bool global_dirty_log = false;
33 /* flat_view_mutex is taken around reading as->current_map; the critical
34 * section is extremely short, so I'm using a single mutex for every AS.
35 * We could also RCU for the read-side.
37 * The BQL is taken around transaction commits, hence both locks are taken
38 * while writing to as->current_map (with the BQL taken outside).
40 static QemuMutex flat_view_mutex;
42 static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners
43 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
45 static QTAILQ_HEAD(, AddressSpace) address_spaces
46 = QTAILQ_HEAD_INITIALIZER(address_spaces);
48 static void memory_init(void)
50 qemu_mutex_init(&flat_view_mutex);
53 typedef struct AddrRange AddrRange;
56 * Note using signed integers limits us to physical addresses at most
57 * 63 bits wide. They are needed for negative offsetting in aliases
58 * (large MemoryRegion::alias_offset).
60 struct AddrRange {
61 Int128 start;
62 Int128 size;
65 static AddrRange addrrange_make(Int128 start, Int128 size)
67 return (AddrRange) { start, size };
70 static bool addrrange_equal(AddrRange r1, AddrRange r2)
72 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
75 static Int128 addrrange_end(AddrRange r)
77 return int128_add(r.start, r.size);
80 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
82 int128_addto(&range.start, delta);
83 return range;
86 static bool addrrange_contains(AddrRange range, Int128 addr)
88 return int128_ge(addr, range.start)
89 && int128_lt(addr, addrrange_end(range));
92 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
94 return addrrange_contains(r1, r2.start)
95 || addrrange_contains(r2, r1.start);
98 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
100 Int128 start = int128_max(r1.start, r2.start);
101 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
102 return addrrange_make(start, int128_sub(end, start));
105 enum ListenerDirection { Forward, Reverse };
107 static bool memory_listener_match(MemoryListener *listener,
108 MemoryRegionSection *section)
110 return !listener->address_space_filter
111 || listener->address_space_filter == section->address_space;
114 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
115 do { \
116 MemoryListener *_listener; \
118 switch (_direction) { \
119 case Forward: \
120 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
121 if (_listener->_callback) { \
122 _listener->_callback(_listener, ##_args); \
125 break; \
126 case Reverse: \
127 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
128 memory_listeners, link) { \
129 if (_listener->_callback) { \
130 _listener->_callback(_listener, ##_args); \
133 break; \
134 default: \
135 abort(); \
137 } while (0)
139 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
140 do { \
141 MemoryListener *_listener; \
143 switch (_direction) { \
144 case Forward: \
145 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
146 if (_listener->_callback \
147 && memory_listener_match(_listener, _section)) { \
148 _listener->_callback(_listener, _section, ##_args); \
151 break; \
152 case Reverse: \
153 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
154 memory_listeners, link) { \
155 if (_listener->_callback \
156 && memory_listener_match(_listener, _section)) { \
157 _listener->_callback(_listener, _section, ##_args); \
160 break; \
161 default: \
162 abort(); \
164 } while (0)
166 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
167 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
168 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
169 .mr = (fr)->mr, \
170 .address_space = (as), \
171 .offset_within_region = (fr)->offset_in_region, \
172 .size = (fr)->addr.size, \
173 .offset_within_address_space = int128_get64((fr)->addr.start), \
174 .readonly = (fr)->readonly, \
177 struct CoalescedMemoryRange {
178 AddrRange addr;
179 QTAILQ_ENTRY(CoalescedMemoryRange) link;
182 struct MemoryRegionIoeventfd {
183 AddrRange addr;
184 bool match_data;
185 uint64_t data;
186 EventNotifier *e;
189 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
190 MemoryRegionIoeventfd b)
192 if (int128_lt(a.addr.start, b.addr.start)) {
193 return true;
194 } else if (int128_gt(a.addr.start, b.addr.start)) {
195 return false;
196 } else if (int128_lt(a.addr.size, b.addr.size)) {
197 return true;
198 } else if (int128_gt(a.addr.size, b.addr.size)) {
199 return false;
200 } else if (a.match_data < b.match_data) {
201 return true;
202 } else if (a.match_data > b.match_data) {
203 return false;
204 } else if (a.match_data) {
205 if (a.data < b.data) {
206 return true;
207 } else if (a.data > b.data) {
208 return false;
211 if (a.e < b.e) {
212 return true;
213 } else if (a.e > b.e) {
214 return false;
216 return false;
219 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
220 MemoryRegionIoeventfd b)
222 return !memory_region_ioeventfd_before(a, b)
223 && !memory_region_ioeventfd_before(b, a);
226 typedef struct FlatRange FlatRange;
227 typedef struct FlatView FlatView;
229 /* Range of memory in the global map. Addresses are absolute. */
230 struct FlatRange {
231 MemoryRegion *mr;
232 hwaddr offset_in_region;
233 AddrRange addr;
234 uint8_t dirty_log_mask;
235 bool romd_mode;
236 bool readonly;
239 /* Flattened global view of current active memory hierarchy. Kept in sorted
240 * order.
242 struct FlatView {
243 unsigned ref;
244 FlatRange *ranges;
245 unsigned nr;
246 unsigned nr_allocated;
249 typedef struct AddressSpaceOps AddressSpaceOps;
251 #define FOR_EACH_FLAT_RANGE(var, view) \
252 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
254 static bool flatrange_equal(FlatRange *a, FlatRange *b)
256 return a->mr == b->mr
257 && addrrange_equal(a->addr, b->addr)
258 && a->offset_in_region == b->offset_in_region
259 && a->romd_mode == b->romd_mode
260 && a->readonly == b->readonly;
263 static void flatview_init(FlatView *view)
265 view->ref = 1;
266 view->ranges = NULL;
267 view->nr = 0;
268 view->nr_allocated = 0;
271 /* Insert a range into a given position. Caller is responsible for maintaining
272 * sorting order.
274 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
276 if (view->nr == view->nr_allocated) {
277 view->nr_allocated = MAX(2 * view->nr, 10);
278 view->ranges = g_realloc(view->ranges,
279 view->nr_allocated * sizeof(*view->ranges));
281 memmove(view->ranges + pos + 1, view->ranges + pos,
282 (view->nr - pos) * sizeof(FlatRange));
283 view->ranges[pos] = *range;
284 memory_region_ref(range->mr);
285 ++view->nr;
288 static void flatview_destroy(FlatView *view)
290 int i;
292 for (i = 0; i < view->nr; i++) {
293 memory_region_unref(view->ranges[i].mr);
295 g_free(view->ranges);
296 g_free(view);
299 static void flatview_ref(FlatView *view)
301 atomic_inc(&view->ref);
304 static void flatview_unref(FlatView *view)
306 if (atomic_fetch_dec(&view->ref) == 1) {
307 flatview_destroy(view);
311 static bool can_merge(FlatRange *r1, FlatRange *r2)
313 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
314 && r1->mr == r2->mr
315 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
316 r1->addr.size),
317 int128_make64(r2->offset_in_region))
318 && r1->dirty_log_mask == r2->dirty_log_mask
319 && r1->romd_mode == r2->romd_mode
320 && r1->readonly == r2->readonly;
323 /* Attempt to simplify a view by merging adjacent ranges */
324 static void flatview_simplify(FlatView *view)
326 unsigned i, j;
328 i = 0;
329 while (i < view->nr) {
330 j = i + 1;
331 while (j < view->nr
332 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
333 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
334 ++j;
336 ++i;
337 memmove(&view->ranges[i], &view->ranges[j],
338 (view->nr - j) * sizeof(view->ranges[j]));
339 view->nr -= j - i;
343 static bool memory_region_big_endian(MemoryRegion *mr)
345 #ifdef TARGET_WORDS_BIGENDIAN
346 return mr->ops->endianness != DEVICE_LITTLE_ENDIAN;
347 #else
348 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
349 #endif
352 static bool memory_region_wrong_endianness(MemoryRegion *mr)
354 #ifdef TARGET_WORDS_BIGENDIAN
355 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
356 #else
357 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
358 #endif
361 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
363 if (memory_region_wrong_endianness(mr)) {
364 switch (size) {
365 case 1:
366 break;
367 case 2:
368 *data = bswap16(*data);
369 break;
370 case 4:
371 *data = bswap32(*data);
372 break;
373 case 8:
374 *data = bswap64(*data);
375 break;
376 default:
377 abort();
382 static void memory_region_oldmmio_read_accessor(MemoryRegion *mr,
383 hwaddr addr,
384 uint64_t *value,
385 unsigned size,
386 unsigned shift,
387 uint64_t mask)
389 uint64_t tmp;
391 tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);
392 trace_memory_region_ops_read(mr, addr, tmp, size);
393 *value |= (tmp & mask) << shift;
396 static void memory_region_read_accessor(MemoryRegion *mr,
397 hwaddr addr,
398 uint64_t *value,
399 unsigned size,
400 unsigned shift,
401 uint64_t mask)
403 uint64_t tmp;
405 if (mr->flush_coalesced_mmio) {
406 qemu_flush_coalesced_mmio_buffer();
408 tmp = mr->ops->read(mr->opaque, addr, size);
409 trace_memory_region_ops_read(mr, addr, tmp, size);
410 *value |= (tmp & mask) << shift;
413 static void memory_region_oldmmio_write_accessor(MemoryRegion *mr,
414 hwaddr addr,
415 uint64_t *value,
416 unsigned size,
417 unsigned shift,
418 uint64_t mask)
420 uint64_t tmp;
422 tmp = (*value >> shift) & mask;
423 trace_memory_region_ops_write(mr, addr, tmp, size);
424 mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp);
427 static void memory_region_write_accessor(MemoryRegion *mr,
428 hwaddr addr,
429 uint64_t *value,
430 unsigned size,
431 unsigned shift,
432 uint64_t mask)
434 uint64_t tmp;
436 if (mr->flush_coalesced_mmio) {
437 qemu_flush_coalesced_mmio_buffer();
439 tmp = (*value >> shift) & mask;
440 trace_memory_region_ops_write(mr, addr, tmp, size);
441 mr->ops->write(mr->opaque, addr, tmp, size);
444 static void access_with_adjusted_size(hwaddr addr,
445 uint64_t *value,
446 unsigned size,
447 unsigned access_size_min,
448 unsigned access_size_max,
449 void (*access)(MemoryRegion *mr,
450 hwaddr addr,
451 uint64_t *value,
452 unsigned size,
453 unsigned shift,
454 uint64_t mask),
455 MemoryRegion *mr)
457 uint64_t access_mask;
458 unsigned access_size;
459 unsigned i;
461 if (!access_size_min) {
462 access_size_min = 1;
464 if (!access_size_max) {
465 access_size_max = 4;
468 /* FIXME: support unaligned access? */
469 access_size = MAX(MIN(size, access_size_max), access_size_min);
470 access_mask = -1ULL >> (64 - access_size * 8);
471 if (memory_region_big_endian(mr)) {
472 for (i = 0; i < size; i += access_size) {
473 access(mr, addr + i, value, access_size,
474 (size - access_size - i) * 8, access_mask);
476 } else {
477 for (i = 0; i < size; i += access_size) {
478 access(mr, addr + i, value, access_size, i * 8, access_mask);
483 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
485 AddressSpace *as;
487 while (mr->parent) {
488 mr = mr->parent;
490 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
491 if (mr == as->root) {
492 return as;
495 abort();
498 /* Render a memory region into the global view. Ranges in @view obscure
499 * ranges in @mr.
501 static void render_memory_region(FlatView *view,
502 MemoryRegion *mr,
503 Int128 base,
504 AddrRange clip,
505 bool readonly)
507 MemoryRegion *subregion;
508 unsigned i;
509 hwaddr offset_in_region;
510 Int128 remain;
511 Int128 now;
512 FlatRange fr;
513 AddrRange tmp;
515 if (!mr->enabled) {
516 return;
519 int128_addto(&base, int128_make64(mr->addr));
520 readonly |= mr->readonly;
522 tmp = addrrange_make(base, mr->size);
524 if (!addrrange_intersects(tmp, clip)) {
525 return;
528 clip = addrrange_intersection(tmp, clip);
530 if (mr->alias) {
531 int128_subfrom(&base, int128_make64(mr->alias->addr));
532 int128_subfrom(&base, int128_make64(mr->alias_offset));
533 render_memory_region(view, mr->alias, base, clip, readonly);
534 return;
537 /* Render subregions in priority order. */
538 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
539 render_memory_region(view, subregion, base, clip, readonly);
542 if (!mr->terminates) {
543 return;
546 offset_in_region = int128_get64(int128_sub(clip.start, base));
547 base = clip.start;
548 remain = clip.size;
550 fr.mr = mr;
551 fr.dirty_log_mask = mr->dirty_log_mask;
552 fr.romd_mode = mr->romd_mode;
553 fr.readonly = readonly;
555 /* Render the region itself into any gaps left by the current view. */
556 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
557 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
558 continue;
560 if (int128_lt(base, view->ranges[i].addr.start)) {
561 now = int128_min(remain,
562 int128_sub(view->ranges[i].addr.start, base));
563 fr.offset_in_region = offset_in_region;
564 fr.addr = addrrange_make(base, now);
565 flatview_insert(view, i, &fr);
566 ++i;
567 int128_addto(&base, now);
568 offset_in_region += int128_get64(now);
569 int128_subfrom(&remain, now);
571 now = int128_sub(int128_min(int128_add(base, remain),
572 addrrange_end(view->ranges[i].addr)),
573 base);
574 int128_addto(&base, now);
575 offset_in_region += int128_get64(now);
576 int128_subfrom(&remain, now);
578 if (int128_nz(remain)) {
579 fr.offset_in_region = offset_in_region;
580 fr.addr = addrrange_make(base, remain);
581 flatview_insert(view, i, &fr);
585 /* Render a memory topology into a list of disjoint absolute ranges. */
586 static FlatView *generate_memory_topology(MemoryRegion *mr)
588 FlatView *view;
590 view = g_new(FlatView, 1);
591 flatview_init(view);
593 if (mr) {
594 render_memory_region(view, mr, int128_zero(),
595 addrrange_make(int128_zero(), int128_2_64()), false);
597 flatview_simplify(view);
599 return view;
602 static void address_space_add_del_ioeventfds(AddressSpace *as,
603 MemoryRegionIoeventfd *fds_new,
604 unsigned fds_new_nb,
605 MemoryRegionIoeventfd *fds_old,
606 unsigned fds_old_nb)
608 unsigned iold, inew;
609 MemoryRegionIoeventfd *fd;
610 MemoryRegionSection section;
612 /* Generate a symmetric difference of the old and new fd sets, adding
613 * and deleting as necessary.
616 iold = inew = 0;
617 while (iold < fds_old_nb || inew < fds_new_nb) {
618 if (iold < fds_old_nb
619 && (inew == fds_new_nb
620 || memory_region_ioeventfd_before(fds_old[iold],
621 fds_new[inew]))) {
622 fd = &fds_old[iold];
623 section = (MemoryRegionSection) {
624 .address_space = as,
625 .offset_within_address_space = int128_get64(fd->addr.start),
626 .size = fd->addr.size,
628 MEMORY_LISTENER_CALL(eventfd_del, Forward, &section,
629 fd->match_data, fd->data, fd->e);
630 ++iold;
631 } else if (inew < fds_new_nb
632 && (iold == fds_old_nb
633 || memory_region_ioeventfd_before(fds_new[inew],
634 fds_old[iold]))) {
635 fd = &fds_new[inew];
636 section = (MemoryRegionSection) {
637 .address_space = as,
638 .offset_within_address_space = int128_get64(fd->addr.start),
639 .size = fd->addr.size,
641 MEMORY_LISTENER_CALL(eventfd_add, Reverse, &section,
642 fd->match_data, fd->data, fd->e);
643 ++inew;
644 } else {
645 ++iold;
646 ++inew;
651 static FlatView *address_space_get_flatview(AddressSpace *as)
653 FlatView *view;
655 qemu_mutex_lock(&flat_view_mutex);
656 view = as->current_map;
657 flatview_ref(view);
658 qemu_mutex_unlock(&flat_view_mutex);
659 return view;
662 static void address_space_update_ioeventfds(AddressSpace *as)
664 FlatView *view;
665 FlatRange *fr;
666 unsigned ioeventfd_nb = 0;
667 MemoryRegionIoeventfd *ioeventfds = NULL;
668 AddrRange tmp;
669 unsigned i;
671 view = address_space_get_flatview(as);
672 FOR_EACH_FLAT_RANGE(fr, view) {
673 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
674 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
675 int128_sub(fr->addr.start,
676 int128_make64(fr->offset_in_region)));
677 if (addrrange_intersects(fr->addr, tmp)) {
678 ++ioeventfd_nb;
679 ioeventfds = g_realloc(ioeventfds,
680 ioeventfd_nb * sizeof(*ioeventfds));
681 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
682 ioeventfds[ioeventfd_nb-1].addr = tmp;
687 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
688 as->ioeventfds, as->ioeventfd_nb);
690 g_free(as->ioeventfds);
691 as->ioeventfds = ioeventfds;
692 as->ioeventfd_nb = ioeventfd_nb;
693 flatview_unref(view);
696 static void address_space_update_topology_pass(AddressSpace *as,
697 const FlatView *old_view,
698 const FlatView *new_view,
699 bool adding)
701 unsigned iold, inew;
702 FlatRange *frold, *frnew;
704 /* Generate a symmetric difference of the old and new memory maps.
705 * Kill ranges in the old map, and instantiate ranges in the new map.
707 iold = inew = 0;
708 while (iold < old_view->nr || inew < new_view->nr) {
709 if (iold < old_view->nr) {
710 frold = &old_view->ranges[iold];
711 } else {
712 frold = NULL;
714 if (inew < new_view->nr) {
715 frnew = &new_view->ranges[inew];
716 } else {
717 frnew = NULL;
720 if (frold
721 && (!frnew
722 || int128_lt(frold->addr.start, frnew->addr.start)
723 || (int128_eq(frold->addr.start, frnew->addr.start)
724 && !flatrange_equal(frold, frnew)))) {
725 /* In old but not in new, or in both but attributes changed. */
727 if (!adding) {
728 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
731 ++iold;
732 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
733 /* In both and unchanged (except logging may have changed) */
735 if (adding) {
736 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
737 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
738 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop);
739 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
740 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start);
744 ++iold;
745 ++inew;
746 } else {
747 /* In new */
749 if (adding) {
750 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
753 ++inew;
759 static void address_space_update_topology(AddressSpace *as)
761 FlatView *old_view = address_space_get_flatview(as);
762 FlatView *new_view = generate_memory_topology(as->root);
764 address_space_update_topology_pass(as, old_view, new_view, false);
765 address_space_update_topology_pass(as, old_view, new_view, true);
767 qemu_mutex_lock(&flat_view_mutex);
768 flatview_unref(as->current_map);
769 as->current_map = new_view;
770 qemu_mutex_unlock(&flat_view_mutex);
772 /* Note that all the old MemoryRegions are still alive up to this
773 * point. This relieves most MemoryListeners from the need to
774 * ref/unref the MemoryRegions they get---unless they use them
775 * outside the iothread mutex, in which case precise reference
776 * counting is necessary.
778 flatview_unref(old_view);
780 address_space_update_ioeventfds(as);
783 void memory_region_transaction_begin(void)
785 qemu_flush_coalesced_mmio_buffer();
786 ++memory_region_transaction_depth;
789 void memory_region_transaction_commit(void)
791 AddressSpace *as;
793 assert(memory_region_transaction_depth);
794 --memory_region_transaction_depth;
795 if (!memory_region_transaction_depth && memory_region_update_pending) {
796 memory_region_update_pending = false;
797 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
799 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
800 address_space_update_topology(as);
803 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
807 static void memory_region_destructor_none(MemoryRegion *mr)
811 static void memory_region_destructor_ram(MemoryRegion *mr)
813 qemu_ram_free(mr->ram_addr);
816 static void memory_region_destructor_alias(MemoryRegion *mr)
818 memory_region_unref(mr->alias);
821 static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
823 qemu_ram_free_from_ptr(mr->ram_addr);
826 static void memory_region_destructor_rom_device(MemoryRegion *mr)
828 qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
831 void memory_region_init(MemoryRegion *mr,
832 Object *owner,
833 const char *name,
834 uint64_t size)
836 mr->ops = &unassigned_mem_ops;
837 mr->opaque = NULL;
838 mr->owner = owner;
839 mr->iommu_ops = NULL;
840 mr->parent = NULL;
841 mr->size = int128_make64(size);
842 if (size == UINT64_MAX) {
843 mr->size = int128_2_64();
845 mr->addr = 0;
846 mr->subpage = false;
847 mr->enabled = true;
848 mr->terminates = false;
849 mr->ram = false;
850 mr->romd_mode = true;
851 mr->readonly = false;
852 mr->rom_device = false;
853 mr->destructor = memory_region_destructor_none;
854 mr->priority = 0;
855 mr->may_overlap = false;
856 mr->alias = NULL;
857 QTAILQ_INIT(&mr->subregions);
858 memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
859 QTAILQ_INIT(&mr->coalesced);
860 mr->name = g_strdup(name);
861 mr->dirty_log_mask = 0;
862 mr->ioeventfd_nb = 0;
863 mr->ioeventfds = NULL;
864 mr->flush_coalesced_mmio = false;
867 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
868 unsigned size)
870 #ifdef DEBUG_UNASSIGNED
871 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
872 #endif
873 if (current_cpu != NULL) {
874 cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
876 return -1ULL;
879 static void unassigned_mem_write(void *opaque, hwaddr addr,
880 uint64_t val, unsigned size)
882 #ifdef DEBUG_UNASSIGNED
883 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
884 #endif
885 if (current_cpu != NULL) {
886 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
890 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
891 unsigned size, bool is_write)
893 return false;
896 const MemoryRegionOps unassigned_mem_ops = {
897 .valid.accepts = unassigned_mem_accepts,
898 .endianness = DEVICE_NATIVE_ENDIAN,
901 bool memory_region_access_valid(MemoryRegion *mr,
902 hwaddr addr,
903 unsigned size,
904 bool is_write)
906 int access_size_min, access_size_max;
907 int access_size, i;
909 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
910 return false;
913 if (!mr->ops->valid.accepts) {
914 return true;
917 access_size_min = mr->ops->valid.min_access_size;
918 if (!mr->ops->valid.min_access_size) {
919 access_size_min = 1;
922 access_size_max = mr->ops->valid.max_access_size;
923 if (!mr->ops->valid.max_access_size) {
924 access_size_max = 4;
927 access_size = MAX(MIN(size, access_size_max), access_size_min);
928 for (i = 0; i < size; i += access_size) {
929 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
930 is_write)) {
931 return false;
935 return true;
938 static uint64_t memory_region_dispatch_read1(MemoryRegion *mr,
939 hwaddr addr,
940 unsigned size)
942 uint64_t data = 0;
944 if (mr->ops->read) {
945 access_with_adjusted_size(addr, &data, size,
946 mr->ops->impl.min_access_size,
947 mr->ops->impl.max_access_size,
948 memory_region_read_accessor, mr);
949 } else {
950 access_with_adjusted_size(addr, &data, size, 1, 4,
951 memory_region_oldmmio_read_accessor, mr);
954 return data;
957 static bool memory_region_dispatch_read(MemoryRegion *mr,
958 hwaddr addr,
959 uint64_t *pval,
960 unsigned size)
962 if (!memory_region_access_valid(mr, addr, size, false)) {
963 *pval = unassigned_mem_read(mr, addr, size);
964 return true;
967 *pval = memory_region_dispatch_read1(mr, addr, size);
968 adjust_endianness(mr, pval, size);
969 return false;
972 static bool memory_region_dispatch_write(MemoryRegion *mr,
973 hwaddr addr,
974 uint64_t data,
975 unsigned size)
977 if (!memory_region_access_valid(mr, addr, size, true)) {
978 unassigned_mem_write(mr, addr, data, size);
979 return true;
982 adjust_endianness(mr, &data, size);
984 if (mr->ops->write) {
985 access_with_adjusted_size(addr, &data, size,
986 mr->ops->impl.min_access_size,
987 mr->ops->impl.max_access_size,
988 memory_region_write_accessor, mr);
989 } else {
990 access_with_adjusted_size(addr, &data, size, 1, 4,
991 memory_region_oldmmio_write_accessor, mr);
993 return false;
996 void memory_region_init_io(MemoryRegion *mr,
997 Object *owner,
998 const MemoryRegionOps *ops,
999 void *opaque,
1000 const char *name,
1001 uint64_t size)
1003 memory_region_init(mr, owner, name, size);
1004 mr->ops = ops;
1005 mr->opaque = opaque;
1006 mr->terminates = true;
1007 mr->ram_addr = ~(ram_addr_t)0;
1010 void memory_region_init_ram(MemoryRegion *mr,
1011 Object *owner,
1012 const char *name,
1013 uint64_t size)
1015 memory_region_init(mr, owner, name, size);
1016 mr->ram = true;
1017 mr->terminates = true;
1018 mr->destructor = memory_region_destructor_ram;
1019 mr->ram_addr = qemu_ram_alloc(size, mr);
1022 void memory_region_init_ram_ptr(MemoryRegion *mr,
1023 Object *owner,
1024 const char *name,
1025 uint64_t size,
1026 void *ptr)
1028 memory_region_init(mr, owner, name, size);
1029 mr->ram = true;
1030 mr->terminates = true;
1031 mr->destructor = memory_region_destructor_ram_from_ptr;
1032 mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr);
1035 void memory_region_init_alias(MemoryRegion *mr,
1036 Object *owner,
1037 const char *name,
1038 MemoryRegion *orig,
1039 hwaddr offset,
1040 uint64_t size)
1042 memory_region_init(mr, owner, name, size);
1043 memory_region_ref(orig);
1044 mr->destructor = memory_region_destructor_alias;
1045 mr->alias = orig;
1046 mr->alias_offset = offset;
1049 void memory_region_init_rom_device(MemoryRegion *mr,
1050 Object *owner,
1051 const MemoryRegionOps *ops,
1052 void *opaque,
1053 const char *name,
1054 uint64_t size)
1056 memory_region_init(mr, owner, name, size);
1057 mr->ops = ops;
1058 mr->opaque = opaque;
1059 mr->terminates = true;
1060 mr->rom_device = true;
1061 mr->destructor = memory_region_destructor_rom_device;
1062 mr->ram_addr = qemu_ram_alloc(size, mr);
1065 void memory_region_init_iommu(MemoryRegion *mr,
1066 Object *owner,
1067 const MemoryRegionIOMMUOps *ops,
1068 const char *name,
1069 uint64_t size)
1071 memory_region_init(mr, owner, name, size);
1072 mr->iommu_ops = ops,
1073 mr->terminates = true; /* then re-forwards */
1074 notifier_list_init(&mr->iommu_notify);
1077 void memory_region_init_reservation(MemoryRegion *mr,
1078 Object *owner,
1079 const char *name,
1080 uint64_t size)
1082 memory_region_init_io(mr, owner, &unassigned_mem_ops, mr, name, size);
1085 void memory_region_destroy(MemoryRegion *mr)
1087 assert(QTAILQ_EMPTY(&mr->subregions));
1088 assert(memory_region_transaction_depth == 0);
1089 mr->destructor(mr);
1090 memory_region_clear_coalescing(mr);
1091 g_free((char *)mr->name);
1092 g_free(mr->ioeventfds);
1095 Object *memory_region_owner(MemoryRegion *mr)
1097 return mr->owner;
1100 void memory_region_ref(MemoryRegion *mr)
1102 if (mr && mr->owner) {
1103 object_ref(mr->owner);
1107 void memory_region_unref(MemoryRegion *mr)
1109 if (mr && mr->owner) {
1110 object_unref(mr->owner);
1114 uint64_t memory_region_size(MemoryRegion *mr)
1116 if (int128_eq(mr->size, int128_2_64())) {
1117 return UINT64_MAX;
1119 return int128_get64(mr->size);
1122 const char *memory_region_name(MemoryRegion *mr)
1124 return mr->name;
1127 bool memory_region_is_ram(MemoryRegion *mr)
1129 return mr->ram;
1132 bool memory_region_is_logging(MemoryRegion *mr)
1134 return mr->dirty_log_mask;
1137 bool memory_region_is_rom(MemoryRegion *mr)
1139 return mr->ram && mr->readonly;
1142 bool memory_region_is_iommu(MemoryRegion *mr)
1144 return mr->iommu_ops;
1147 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n)
1149 notifier_list_add(&mr->iommu_notify, n);
1152 void memory_region_unregister_iommu_notifier(Notifier *n)
1154 notifier_remove(n);
1157 void memory_region_notify_iommu(MemoryRegion *mr,
1158 IOMMUTLBEntry entry)
1160 assert(memory_region_is_iommu(mr));
1161 notifier_list_notify(&mr->iommu_notify, &entry);
1164 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1166 uint8_t mask = 1 << client;
1168 memory_region_transaction_begin();
1169 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1170 memory_region_update_pending |= mr->enabled;
1171 memory_region_transaction_commit();
1174 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1175 hwaddr size, unsigned client)
1177 assert(mr->terminates);
1178 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, size,
1179 1 << client);
1182 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1183 hwaddr size)
1185 assert(mr->terminates);
1186 return cpu_physical_memory_set_dirty_range(mr->ram_addr + addr, size, -1);
1189 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
1190 hwaddr size, unsigned client)
1192 bool ret;
1193 assert(mr->terminates);
1194 ret = cpu_physical_memory_get_dirty(mr->ram_addr + addr, size,
1195 1 << client);
1196 if (ret) {
1197 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1198 mr->ram_addr + addr + size,
1199 1 << client);
1201 return ret;
1205 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1207 AddressSpace *as;
1208 FlatRange *fr;
1210 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1211 FlatView *view = address_space_get_flatview(as);
1212 FOR_EACH_FLAT_RANGE(fr, view) {
1213 if (fr->mr == mr) {
1214 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1217 flatview_unref(view);
1221 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1223 if (mr->readonly != readonly) {
1224 memory_region_transaction_begin();
1225 mr->readonly = readonly;
1226 memory_region_update_pending |= mr->enabled;
1227 memory_region_transaction_commit();
1231 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
1233 if (mr->romd_mode != romd_mode) {
1234 memory_region_transaction_begin();
1235 mr->romd_mode = romd_mode;
1236 memory_region_update_pending |= mr->enabled;
1237 memory_region_transaction_commit();
1241 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1242 hwaddr size, unsigned client)
1244 assert(mr->terminates);
1245 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1246 mr->ram_addr + addr + size,
1247 1 << client);
1250 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1252 if (mr->alias) {
1253 return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset;
1256 assert(mr->terminates);
1258 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
1261 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
1263 FlatView *view;
1264 FlatRange *fr;
1265 CoalescedMemoryRange *cmr;
1266 AddrRange tmp;
1267 MemoryRegionSection section;
1269 view = address_space_get_flatview(as);
1270 FOR_EACH_FLAT_RANGE(fr, view) {
1271 if (fr->mr == mr) {
1272 section = (MemoryRegionSection) {
1273 .address_space = as,
1274 .offset_within_address_space = int128_get64(fr->addr.start),
1275 .size = fr->addr.size,
1278 MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, &section,
1279 int128_get64(fr->addr.start),
1280 int128_get64(fr->addr.size));
1281 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1282 tmp = addrrange_shift(cmr->addr,
1283 int128_sub(fr->addr.start,
1284 int128_make64(fr->offset_in_region)));
1285 if (!addrrange_intersects(tmp, fr->addr)) {
1286 continue;
1288 tmp = addrrange_intersection(tmp, fr->addr);
1289 MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, &section,
1290 int128_get64(tmp.start),
1291 int128_get64(tmp.size));
1295 flatview_unref(view);
1298 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1300 AddressSpace *as;
1302 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1303 memory_region_update_coalesced_range_as(mr, as);
1307 void memory_region_set_coalescing(MemoryRegion *mr)
1309 memory_region_clear_coalescing(mr);
1310 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1313 void memory_region_add_coalescing(MemoryRegion *mr,
1314 hwaddr offset,
1315 uint64_t size)
1317 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1319 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1320 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1321 memory_region_update_coalesced_range(mr);
1322 memory_region_set_flush_coalesced(mr);
1325 void memory_region_clear_coalescing(MemoryRegion *mr)
1327 CoalescedMemoryRange *cmr;
1329 qemu_flush_coalesced_mmio_buffer();
1330 mr->flush_coalesced_mmio = false;
1332 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1333 cmr = QTAILQ_FIRST(&mr->coalesced);
1334 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1335 g_free(cmr);
1337 memory_region_update_coalesced_range(mr);
1340 void memory_region_set_flush_coalesced(MemoryRegion *mr)
1342 mr->flush_coalesced_mmio = true;
1345 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
1347 qemu_flush_coalesced_mmio_buffer();
1348 if (QTAILQ_EMPTY(&mr->coalesced)) {
1349 mr->flush_coalesced_mmio = false;
1353 void memory_region_add_eventfd(MemoryRegion *mr,
1354 hwaddr addr,
1355 unsigned size,
1356 bool match_data,
1357 uint64_t data,
1358 EventNotifier *e)
1360 MemoryRegionIoeventfd mrfd = {
1361 .addr.start = int128_make64(addr),
1362 .addr.size = int128_make64(size),
1363 .match_data = match_data,
1364 .data = data,
1365 .e = e,
1367 unsigned i;
1369 adjust_endianness(mr, &mrfd.data, size);
1370 memory_region_transaction_begin();
1371 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1372 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1373 break;
1376 ++mr->ioeventfd_nb;
1377 mr->ioeventfds = g_realloc(mr->ioeventfds,
1378 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1379 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1380 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1381 mr->ioeventfds[i] = mrfd;
1382 memory_region_update_pending |= mr->enabled;
1383 memory_region_transaction_commit();
1386 void memory_region_del_eventfd(MemoryRegion *mr,
1387 hwaddr addr,
1388 unsigned size,
1389 bool match_data,
1390 uint64_t data,
1391 EventNotifier *e)
1393 MemoryRegionIoeventfd mrfd = {
1394 .addr.start = int128_make64(addr),
1395 .addr.size = int128_make64(size),
1396 .match_data = match_data,
1397 .data = data,
1398 .e = e,
1400 unsigned i;
1402 adjust_endianness(mr, &mrfd.data, size);
1403 memory_region_transaction_begin();
1404 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1405 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1406 break;
1409 assert(i != mr->ioeventfd_nb);
1410 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1411 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1412 --mr->ioeventfd_nb;
1413 mr->ioeventfds = g_realloc(mr->ioeventfds,
1414 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1415 memory_region_update_pending |= mr->enabled;
1416 memory_region_transaction_commit();
1419 static void memory_region_add_subregion_common(MemoryRegion *mr,
1420 hwaddr offset,
1421 MemoryRegion *subregion)
1423 MemoryRegion *other;
1425 memory_region_transaction_begin();
1427 assert(!subregion->parent);
1428 memory_region_ref(subregion);
1429 subregion->parent = mr;
1430 subregion->addr = offset;
1431 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1432 if (subregion->may_overlap || other->may_overlap) {
1433 continue;
1435 if (int128_ge(int128_make64(offset),
1436 int128_add(int128_make64(other->addr), other->size))
1437 || int128_le(int128_add(int128_make64(offset), subregion->size),
1438 int128_make64(other->addr))) {
1439 continue;
1441 #if 0
1442 printf("warning: subregion collision %llx/%llx (%s) "
1443 "vs %llx/%llx (%s)\n",
1444 (unsigned long long)offset,
1445 (unsigned long long)int128_get64(subregion->size),
1446 subregion->name,
1447 (unsigned long long)other->addr,
1448 (unsigned long long)int128_get64(other->size),
1449 other->name);
1450 #endif
1452 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1453 if (subregion->priority >= other->priority) {
1454 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1455 goto done;
1458 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1459 done:
1460 memory_region_update_pending |= mr->enabled && subregion->enabled;
1461 memory_region_transaction_commit();
1465 void memory_region_add_subregion(MemoryRegion *mr,
1466 hwaddr offset,
1467 MemoryRegion *subregion)
1469 subregion->may_overlap = false;
1470 subregion->priority = 0;
1471 memory_region_add_subregion_common(mr, offset, subregion);
1474 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1475 hwaddr offset,
1476 MemoryRegion *subregion,
1477 unsigned priority)
1479 subregion->may_overlap = true;
1480 subregion->priority = priority;
1481 memory_region_add_subregion_common(mr, offset, subregion);
1484 void memory_region_del_subregion(MemoryRegion *mr,
1485 MemoryRegion *subregion)
1487 memory_region_transaction_begin();
1488 assert(subregion->parent == mr);
1489 subregion->parent = NULL;
1490 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1491 memory_region_unref(subregion);
1492 memory_region_update_pending |= mr->enabled && subregion->enabled;
1493 memory_region_transaction_commit();
1496 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1498 if (enabled == mr->enabled) {
1499 return;
1501 memory_region_transaction_begin();
1502 mr->enabled = enabled;
1503 memory_region_update_pending = true;
1504 memory_region_transaction_commit();
1507 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
1509 MemoryRegion *parent = mr->parent;
1510 unsigned priority = mr->priority;
1511 bool may_overlap = mr->may_overlap;
1513 if (addr == mr->addr || !parent) {
1514 mr->addr = addr;
1515 return;
1518 memory_region_transaction_begin();
1519 memory_region_ref(mr);
1520 memory_region_del_subregion(parent, mr);
1521 if (may_overlap) {
1522 memory_region_add_subregion_overlap(parent, addr, mr, priority);
1523 } else {
1524 memory_region_add_subregion(parent, addr, mr);
1526 memory_region_unref(mr);
1527 memory_region_transaction_commit();
1530 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
1532 assert(mr->alias);
1534 if (offset == mr->alias_offset) {
1535 return;
1538 memory_region_transaction_begin();
1539 mr->alias_offset = offset;
1540 memory_region_update_pending |= mr->enabled;
1541 memory_region_transaction_commit();
1544 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1546 return mr->ram_addr;
1549 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1551 const AddrRange *addr = addr_;
1552 const FlatRange *fr = fr_;
1554 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1555 return -1;
1556 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1557 return 1;
1559 return 0;
1562 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
1564 return bsearch(&addr, view->ranges, view->nr,
1565 sizeof(FlatRange), cmp_flatrange_addr);
1568 bool memory_region_present(MemoryRegion *parent, hwaddr addr)
1570 MemoryRegion *mr = memory_region_find(parent, addr, 1).mr;
1571 if (!mr) {
1572 return false;
1574 memory_region_unref(mr);
1575 return true;
1578 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1579 hwaddr addr, uint64_t size)
1581 MemoryRegionSection ret = { .mr = NULL };
1582 MemoryRegion *root;
1583 AddressSpace *as;
1584 AddrRange range;
1585 FlatView *view;
1586 FlatRange *fr;
1588 addr += mr->addr;
1589 for (root = mr; root->parent; ) {
1590 root = root->parent;
1591 addr += root->addr;
1594 as = memory_region_to_address_space(root);
1595 range = addrrange_make(int128_make64(addr), int128_make64(size));
1597 view = address_space_get_flatview(as);
1598 fr = flatview_lookup(view, range);
1599 if (!fr) {
1600 return ret;
1603 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
1604 --fr;
1607 ret.mr = fr->mr;
1608 ret.address_space = as;
1609 range = addrrange_intersection(range, fr->addr);
1610 ret.offset_within_region = fr->offset_in_region;
1611 ret.offset_within_region += int128_get64(int128_sub(range.start,
1612 fr->addr.start));
1613 ret.size = range.size;
1614 ret.offset_within_address_space = int128_get64(range.start);
1615 ret.readonly = fr->readonly;
1616 memory_region_ref(ret.mr);
1618 flatview_unref(view);
1619 return ret;
1622 void address_space_sync_dirty_bitmap(AddressSpace *as)
1624 FlatView *view;
1625 FlatRange *fr;
1627 view = address_space_get_flatview(as);
1628 FOR_EACH_FLAT_RANGE(fr, view) {
1629 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1631 flatview_unref(view);
1634 void memory_global_dirty_log_start(void)
1636 global_dirty_log = true;
1637 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
1640 void memory_global_dirty_log_stop(void)
1642 global_dirty_log = false;
1643 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
1646 static void listener_add_address_space(MemoryListener *listener,
1647 AddressSpace *as)
1649 FlatView *view;
1650 FlatRange *fr;
1652 if (listener->address_space_filter
1653 && listener->address_space_filter != as) {
1654 return;
1657 if (global_dirty_log) {
1658 if (listener->log_global_start) {
1659 listener->log_global_start(listener);
1663 view = address_space_get_flatview(as);
1664 FOR_EACH_FLAT_RANGE(fr, view) {
1665 MemoryRegionSection section = {
1666 .mr = fr->mr,
1667 .address_space = as,
1668 .offset_within_region = fr->offset_in_region,
1669 .size = fr->addr.size,
1670 .offset_within_address_space = int128_get64(fr->addr.start),
1671 .readonly = fr->readonly,
1673 if (listener->region_add) {
1674 listener->region_add(listener, &section);
1677 flatview_unref(view);
1680 void memory_listener_register(MemoryListener *listener, AddressSpace *filter)
1682 MemoryListener *other = NULL;
1683 AddressSpace *as;
1685 listener->address_space_filter = filter;
1686 if (QTAILQ_EMPTY(&memory_listeners)
1687 || listener->priority >= QTAILQ_LAST(&memory_listeners,
1688 memory_listeners)->priority) {
1689 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
1690 } else {
1691 QTAILQ_FOREACH(other, &memory_listeners, link) {
1692 if (listener->priority < other->priority) {
1693 break;
1696 QTAILQ_INSERT_BEFORE(other, listener, link);
1699 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1700 listener_add_address_space(listener, as);
1704 void memory_listener_unregister(MemoryListener *listener)
1706 QTAILQ_REMOVE(&memory_listeners, listener, link);
1709 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
1711 if (QTAILQ_EMPTY(&address_spaces)) {
1712 memory_init();
1715 memory_region_transaction_begin();
1716 as->root = root;
1717 as->current_map = g_new(FlatView, 1);
1718 flatview_init(as->current_map);
1719 as->ioeventfd_nb = 0;
1720 as->ioeventfds = NULL;
1721 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
1722 as->name = g_strdup(name ? name : "anonymous");
1723 address_space_init_dispatch(as);
1724 memory_region_update_pending |= root->enabled;
1725 memory_region_transaction_commit();
1728 void address_space_destroy(AddressSpace *as)
1730 /* Flush out anything from MemoryListeners listening in on this */
1731 memory_region_transaction_begin();
1732 as->root = NULL;
1733 memory_region_transaction_commit();
1734 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
1735 address_space_destroy_dispatch(as);
1736 flatview_unref(as->current_map);
1737 g_free(as->name);
1738 g_free(as->ioeventfds);
1741 bool io_mem_read(MemoryRegion *mr, hwaddr addr, uint64_t *pval, unsigned size)
1743 return memory_region_dispatch_read(mr, addr, pval, size);
1746 bool io_mem_write(MemoryRegion *mr, hwaddr addr,
1747 uint64_t val, unsigned size)
1749 return memory_region_dispatch_write(mr, addr, val, size);
1752 typedef struct MemoryRegionList MemoryRegionList;
1754 struct MemoryRegionList {
1755 const MemoryRegion *mr;
1756 bool printed;
1757 QTAILQ_ENTRY(MemoryRegionList) queue;
1760 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1762 static void mtree_print_mr(fprintf_function mon_printf, void *f,
1763 const MemoryRegion *mr, unsigned int level,
1764 hwaddr base,
1765 MemoryRegionListHead *alias_print_queue)
1767 MemoryRegionList *new_ml, *ml, *next_ml;
1768 MemoryRegionListHead submr_print_queue;
1769 const MemoryRegion *submr;
1770 unsigned int i;
1772 if (!mr || !mr->enabled) {
1773 return;
1776 for (i = 0; i < level; i++) {
1777 mon_printf(f, " ");
1780 if (mr->alias) {
1781 MemoryRegionList *ml;
1782 bool found = false;
1784 /* check if the alias is already in the queue */
1785 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
1786 if (ml->mr == mr->alias && !ml->printed) {
1787 found = true;
1791 if (!found) {
1792 ml = g_new(MemoryRegionList, 1);
1793 ml->mr = mr->alias;
1794 ml->printed = false;
1795 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
1797 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
1798 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1799 "-" TARGET_FMT_plx "\n",
1800 base + mr->addr,
1801 base + mr->addr
1802 + (int128_nz(mr->size) ?
1803 (hwaddr)int128_get64(int128_sub(mr->size,
1804 int128_one())) : 0),
1805 mr->priority,
1806 mr->romd_mode ? 'R' : '-',
1807 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
1808 : '-',
1809 mr->name,
1810 mr->alias->name,
1811 mr->alias_offset,
1812 mr->alias_offset
1813 + (hwaddr)int128_get64(mr->size) - 1);
1814 } else {
1815 mon_printf(f,
1816 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %c%c): %s\n",
1817 base + mr->addr,
1818 base + mr->addr
1819 + (int128_nz(mr->size) ?
1820 (hwaddr)int128_get64(int128_sub(mr->size,
1821 int128_one())) : 0),
1822 mr->priority,
1823 mr->romd_mode ? 'R' : '-',
1824 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
1825 : '-',
1826 mr->name);
1829 QTAILQ_INIT(&submr_print_queue);
1831 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
1832 new_ml = g_new(MemoryRegionList, 1);
1833 new_ml->mr = submr;
1834 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1835 if (new_ml->mr->addr < ml->mr->addr ||
1836 (new_ml->mr->addr == ml->mr->addr &&
1837 new_ml->mr->priority > ml->mr->priority)) {
1838 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
1839 new_ml = NULL;
1840 break;
1843 if (new_ml) {
1844 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
1848 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1849 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
1850 alias_print_queue);
1853 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
1854 g_free(ml);
1858 void mtree_info(fprintf_function mon_printf, void *f)
1860 MemoryRegionListHead ml_head;
1861 MemoryRegionList *ml, *ml2;
1862 AddressSpace *as;
1864 QTAILQ_INIT(&ml_head);
1866 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1867 mon_printf(f, "%s\n", as->name);
1868 mtree_print_mr(mon_printf, f, as->root, 0, 0, &ml_head);
1871 mon_printf(f, "aliases\n");
1872 /* print aliased regions */
1873 QTAILQ_FOREACH(ml, &ml_head, queue) {
1874 if (!ml->printed) {
1875 mon_printf(f, "%s\n", ml->mr->name);
1876 mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head);
1880 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
1881 g_free(ml);