memory: MemoryRegion: Add container and addr props
[qemu/cris-port.git] / memory.c
blob04dc9336038580f3775c9cd02bdbdf08b646f8b5
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 "qapi/visitor.h"
20 #include "qemu/bitops.h"
21 #include "qom/object.h"
22 #include "trace.h"
23 #include <assert.h>
25 #include "exec/memory-internal.h"
26 #include "exec/ram_addr.h"
27 #include "sysemu/sysemu.h"
29 //#define DEBUG_UNASSIGNED
31 static unsigned memory_region_transaction_depth;
32 static bool memory_region_update_pending;
33 static bool ioeventfd_update_pending;
34 static bool global_dirty_log = false;
36 /* flat_view_mutex is taken around reading as->current_map; the critical
37 * section is extremely short, so I'm using a single mutex for every AS.
38 * We could also RCU for the read-side.
40 * The BQL is taken around transaction commits, hence both locks are taken
41 * while writing to as->current_map (with the BQL taken outside).
43 static QemuMutex flat_view_mutex;
45 static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners
46 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
48 static QTAILQ_HEAD(, AddressSpace) address_spaces
49 = QTAILQ_HEAD_INITIALIZER(address_spaces);
51 static void memory_init(void)
53 qemu_mutex_init(&flat_view_mutex);
56 typedef struct AddrRange AddrRange;
59 * Note using signed integers limits us to physical addresses at most
60 * 63 bits wide. They are needed for negative offsetting in aliases
61 * (large MemoryRegion::alias_offset).
63 struct AddrRange {
64 Int128 start;
65 Int128 size;
68 static AddrRange addrrange_make(Int128 start, Int128 size)
70 return (AddrRange) { start, size };
73 static bool addrrange_equal(AddrRange r1, AddrRange r2)
75 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
78 static Int128 addrrange_end(AddrRange r)
80 return int128_add(r.start, r.size);
83 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
85 int128_addto(&range.start, delta);
86 return range;
89 static bool addrrange_contains(AddrRange range, Int128 addr)
91 return int128_ge(addr, range.start)
92 && int128_lt(addr, addrrange_end(range));
95 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
97 return addrrange_contains(r1, r2.start)
98 || addrrange_contains(r2, r1.start);
101 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
103 Int128 start = int128_max(r1.start, r2.start);
104 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
105 return addrrange_make(start, int128_sub(end, start));
108 enum ListenerDirection { Forward, Reverse };
110 static bool memory_listener_match(MemoryListener *listener,
111 MemoryRegionSection *section)
113 return !listener->address_space_filter
114 || listener->address_space_filter == section->address_space;
117 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
118 do { \
119 MemoryListener *_listener; \
121 switch (_direction) { \
122 case Forward: \
123 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
124 if (_listener->_callback) { \
125 _listener->_callback(_listener, ##_args); \
128 break; \
129 case Reverse: \
130 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
131 memory_listeners, link) { \
132 if (_listener->_callback) { \
133 _listener->_callback(_listener, ##_args); \
136 break; \
137 default: \
138 abort(); \
140 } while (0)
142 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
143 do { \
144 MemoryListener *_listener; \
146 switch (_direction) { \
147 case Forward: \
148 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
149 if (_listener->_callback \
150 && memory_listener_match(_listener, _section)) { \
151 _listener->_callback(_listener, _section, ##_args); \
154 break; \
155 case Reverse: \
156 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
157 memory_listeners, link) { \
158 if (_listener->_callback \
159 && memory_listener_match(_listener, _section)) { \
160 _listener->_callback(_listener, _section, ##_args); \
163 break; \
164 default: \
165 abort(); \
167 } while (0)
169 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
170 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
171 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
172 .mr = (fr)->mr, \
173 .address_space = (as), \
174 .offset_within_region = (fr)->offset_in_region, \
175 .size = (fr)->addr.size, \
176 .offset_within_address_space = int128_get64((fr)->addr.start), \
177 .readonly = (fr)->readonly, \
180 struct CoalescedMemoryRange {
181 AddrRange addr;
182 QTAILQ_ENTRY(CoalescedMemoryRange) link;
185 struct MemoryRegionIoeventfd {
186 AddrRange addr;
187 bool match_data;
188 uint64_t data;
189 EventNotifier *e;
192 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
193 MemoryRegionIoeventfd b)
195 if (int128_lt(a.addr.start, b.addr.start)) {
196 return true;
197 } else if (int128_gt(a.addr.start, b.addr.start)) {
198 return false;
199 } else if (int128_lt(a.addr.size, b.addr.size)) {
200 return true;
201 } else if (int128_gt(a.addr.size, b.addr.size)) {
202 return false;
203 } else if (a.match_data < b.match_data) {
204 return true;
205 } else if (a.match_data > b.match_data) {
206 return false;
207 } else if (a.match_data) {
208 if (a.data < b.data) {
209 return true;
210 } else if (a.data > b.data) {
211 return false;
214 if (a.e < b.e) {
215 return true;
216 } else if (a.e > b.e) {
217 return false;
219 return false;
222 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
223 MemoryRegionIoeventfd b)
225 return !memory_region_ioeventfd_before(a, b)
226 && !memory_region_ioeventfd_before(b, a);
229 typedef struct FlatRange FlatRange;
230 typedef struct FlatView FlatView;
232 /* Range of memory in the global map. Addresses are absolute. */
233 struct FlatRange {
234 MemoryRegion *mr;
235 hwaddr offset_in_region;
236 AddrRange addr;
237 uint8_t dirty_log_mask;
238 bool romd_mode;
239 bool readonly;
242 /* Flattened global view of current active memory hierarchy. Kept in sorted
243 * order.
245 struct FlatView {
246 unsigned ref;
247 FlatRange *ranges;
248 unsigned nr;
249 unsigned nr_allocated;
252 typedef struct AddressSpaceOps AddressSpaceOps;
254 #define FOR_EACH_FLAT_RANGE(var, view) \
255 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
257 static bool flatrange_equal(FlatRange *a, FlatRange *b)
259 return a->mr == b->mr
260 && addrrange_equal(a->addr, b->addr)
261 && a->offset_in_region == b->offset_in_region
262 && a->romd_mode == b->romd_mode
263 && a->readonly == b->readonly;
266 static void flatview_init(FlatView *view)
268 view->ref = 1;
269 view->ranges = NULL;
270 view->nr = 0;
271 view->nr_allocated = 0;
274 /* Insert a range into a given position. Caller is responsible for maintaining
275 * sorting order.
277 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
279 if (view->nr == view->nr_allocated) {
280 view->nr_allocated = MAX(2 * view->nr, 10);
281 view->ranges = g_realloc(view->ranges,
282 view->nr_allocated * sizeof(*view->ranges));
284 memmove(view->ranges + pos + 1, view->ranges + pos,
285 (view->nr - pos) * sizeof(FlatRange));
286 view->ranges[pos] = *range;
287 memory_region_ref(range->mr);
288 ++view->nr;
291 static void flatview_destroy(FlatView *view)
293 int i;
295 for (i = 0; i < view->nr; i++) {
296 memory_region_unref(view->ranges[i].mr);
298 g_free(view->ranges);
299 g_free(view);
302 static void flatview_ref(FlatView *view)
304 atomic_inc(&view->ref);
307 static void flatview_unref(FlatView *view)
309 if (atomic_fetch_dec(&view->ref) == 1) {
310 flatview_destroy(view);
314 static bool can_merge(FlatRange *r1, FlatRange *r2)
316 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
317 && r1->mr == r2->mr
318 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
319 r1->addr.size),
320 int128_make64(r2->offset_in_region))
321 && r1->dirty_log_mask == r2->dirty_log_mask
322 && r1->romd_mode == r2->romd_mode
323 && r1->readonly == r2->readonly;
326 /* Attempt to simplify a view by merging adjacent ranges */
327 static void flatview_simplify(FlatView *view)
329 unsigned i, j;
331 i = 0;
332 while (i < view->nr) {
333 j = i + 1;
334 while (j < view->nr
335 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
336 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
337 ++j;
339 ++i;
340 memmove(&view->ranges[i], &view->ranges[j],
341 (view->nr - j) * sizeof(view->ranges[j]));
342 view->nr -= j - i;
346 static bool memory_region_big_endian(MemoryRegion *mr)
348 #ifdef TARGET_WORDS_BIGENDIAN
349 return mr->ops->endianness != DEVICE_LITTLE_ENDIAN;
350 #else
351 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
352 #endif
355 static bool memory_region_wrong_endianness(MemoryRegion *mr)
357 #ifdef TARGET_WORDS_BIGENDIAN
358 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
359 #else
360 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
361 #endif
364 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
366 if (memory_region_wrong_endianness(mr)) {
367 switch (size) {
368 case 1:
369 break;
370 case 2:
371 *data = bswap16(*data);
372 break;
373 case 4:
374 *data = bswap32(*data);
375 break;
376 case 8:
377 *data = bswap64(*data);
378 break;
379 default:
380 abort();
385 static void memory_region_oldmmio_read_accessor(MemoryRegion *mr,
386 hwaddr addr,
387 uint64_t *value,
388 unsigned size,
389 unsigned shift,
390 uint64_t mask)
392 uint64_t tmp;
394 tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);
395 trace_memory_region_ops_read(mr, addr, tmp, size);
396 *value |= (tmp & mask) << shift;
399 static void memory_region_read_accessor(MemoryRegion *mr,
400 hwaddr addr,
401 uint64_t *value,
402 unsigned size,
403 unsigned shift,
404 uint64_t mask)
406 uint64_t tmp;
408 if (mr->flush_coalesced_mmio) {
409 qemu_flush_coalesced_mmio_buffer();
411 tmp = mr->ops->read(mr->opaque, addr, size);
412 trace_memory_region_ops_read(mr, addr, tmp, size);
413 *value |= (tmp & mask) << shift;
416 static void memory_region_oldmmio_write_accessor(MemoryRegion *mr,
417 hwaddr addr,
418 uint64_t *value,
419 unsigned size,
420 unsigned shift,
421 uint64_t mask)
423 uint64_t tmp;
425 tmp = (*value >> shift) & mask;
426 trace_memory_region_ops_write(mr, addr, tmp, size);
427 mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp);
430 static void memory_region_write_accessor(MemoryRegion *mr,
431 hwaddr addr,
432 uint64_t *value,
433 unsigned size,
434 unsigned shift,
435 uint64_t mask)
437 uint64_t tmp;
439 if (mr->flush_coalesced_mmio) {
440 qemu_flush_coalesced_mmio_buffer();
442 tmp = (*value >> shift) & mask;
443 trace_memory_region_ops_write(mr, addr, tmp, size);
444 mr->ops->write(mr->opaque, addr, tmp, size);
447 static void access_with_adjusted_size(hwaddr addr,
448 uint64_t *value,
449 unsigned size,
450 unsigned access_size_min,
451 unsigned access_size_max,
452 void (*access)(MemoryRegion *mr,
453 hwaddr addr,
454 uint64_t *value,
455 unsigned size,
456 unsigned shift,
457 uint64_t mask),
458 MemoryRegion *mr)
460 uint64_t access_mask;
461 unsigned access_size;
462 unsigned i;
464 if (!access_size_min) {
465 access_size_min = 1;
467 if (!access_size_max) {
468 access_size_max = 4;
471 /* FIXME: support unaligned access? */
472 access_size = MAX(MIN(size, access_size_max), access_size_min);
473 access_mask = -1ULL >> (64 - access_size * 8);
474 if (memory_region_big_endian(mr)) {
475 for (i = 0; i < size; i += access_size) {
476 access(mr, addr + i, value, access_size,
477 (size - access_size - i) * 8, access_mask);
479 } else {
480 for (i = 0; i < size; i += access_size) {
481 access(mr, addr + i, value, access_size, i * 8, access_mask);
486 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
488 AddressSpace *as;
490 while (mr->container) {
491 mr = mr->container;
493 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
494 if (mr == as->root) {
495 return as;
498 return NULL;
501 /* Render a memory region into the global view. Ranges in @view obscure
502 * ranges in @mr.
504 static void render_memory_region(FlatView *view,
505 MemoryRegion *mr,
506 Int128 base,
507 AddrRange clip,
508 bool readonly)
510 MemoryRegion *subregion;
511 unsigned i;
512 hwaddr offset_in_region;
513 Int128 remain;
514 Int128 now;
515 FlatRange fr;
516 AddrRange tmp;
518 if (!mr->enabled) {
519 return;
522 int128_addto(&base, int128_make64(mr->addr));
523 readonly |= mr->readonly;
525 tmp = addrrange_make(base, mr->size);
527 if (!addrrange_intersects(tmp, clip)) {
528 return;
531 clip = addrrange_intersection(tmp, clip);
533 if (mr->alias) {
534 int128_subfrom(&base, int128_make64(mr->alias->addr));
535 int128_subfrom(&base, int128_make64(mr->alias_offset));
536 render_memory_region(view, mr->alias, base, clip, readonly);
537 return;
540 /* Render subregions in priority order. */
541 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
542 render_memory_region(view, subregion, base, clip, readonly);
545 if (!mr->terminates) {
546 return;
549 offset_in_region = int128_get64(int128_sub(clip.start, base));
550 base = clip.start;
551 remain = clip.size;
553 fr.mr = mr;
554 fr.dirty_log_mask = mr->dirty_log_mask;
555 fr.romd_mode = mr->romd_mode;
556 fr.readonly = readonly;
558 /* Render the region itself into any gaps left by the current view. */
559 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
560 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
561 continue;
563 if (int128_lt(base, view->ranges[i].addr.start)) {
564 now = int128_min(remain,
565 int128_sub(view->ranges[i].addr.start, base));
566 fr.offset_in_region = offset_in_region;
567 fr.addr = addrrange_make(base, now);
568 flatview_insert(view, i, &fr);
569 ++i;
570 int128_addto(&base, now);
571 offset_in_region += int128_get64(now);
572 int128_subfrom(&remain, now);
574 now = int128_sub(int128_min(int128_add(base, remain),
575 addrrange_end(view->ranges[i].addr)),
576 base);
577 int128_addto(&base, now);
578 offset_in_region += int128_get64(now);
579 int128_subfrom(&remain, now);
581 if (int128_nz(remain)) {
582 fr.offset_in_region = offset_in_region;
583 fr.addr = addrrange_make(base, remain);
584 flatview_insert(view, i, &fr);
588 /* Render a memory topology into a list of disjoint absolute ranges. */
589 static FlatView *generate_memory_topology(MemoryRegion *mr)
591 FlatView *view;
593 view = g_new(FlatView, 1);
594 flatview_init(view);
596 if (mr) {
597 render_memory_region(view, mr, int128_zero(),
598 addrrange_make(int128_zero(), int128_2_64()), false);
600 flatview_simplify(view);
602 return view;
605 static void address_space_add_del_ioeventfds(AddressSpace *as,
606 MemoryRegionIoeventfd *fds_new,
607 unsigned fds_new_nb,
608 MemoryRegionIoeventfd *fds_old,
609 unsigned fds_old_nb)
611 unsigned iold, inew;
612 MemoryRegionIoeventfd *fd;
613 MemoryRegionSection section;
615 /* Generate a symmetric difference of the old and new fd sets, adding
616 * and deleting as necessary.
619 iold = inew = 0;
620 while (iold < fds_old_nb || inew < fds_new_nb) {
621 if (iold < fds_old_nb
622 && (inew == fds_new_nb
623 || memory_region_ioeventfd_before(fds_old[iold],
624 fds_new[inew]))) {
625 fd = &fds_old[iold];
626 section = (MemoryRegionSection) {
627 .address_space = as,
628 .offset_within_address_space = int128_get64(fd->addr.start),
629 .size = fd->addr.size,
631 MEMORY_LISTENER_CALL(eventfd_del, Forward, &section,
632 fd->match_data, fd->data, fd->e);
633 ++iold;
634 } else if (inew < fds_new_nb
635 && (iold == fds_old_nb
636 || memory_region_ioeventfd_before(fds_new[inew],
637 fds_old[iold]))) {
638 fd = &fds_new[inew];
639 section = (MemoryRegionSection) {
640 .address_space = as,
641 .offset_within_address_space = int128_get64(fd->addr.start),
642 .size = fd->addr.size,
644 MEMORY_LISTENER_CALL(eventfd_add, Reverse, &section,
645 fd->match_data, fd->data, fd->e);
646 ++inew;
647 } else {
648 ++iold;
649 ++inew;
654 static FlatView *address_space_get_flatview(AddressSpace *as)
656 FlatView *view;
658 qemu_mutex_lock(&flat_view_mutex);
659 view = as->current_map;
660 flatview_ref(view);
661 qemu_mutex_unlock(&flat_view_mutex);
662 return view;
665 static void address_space_update_ioeventfds(AddressSpace *as)
667 FlatView *view;
668 FlatRange *fr;
669 unsigned ioeventfd_nb = 0;
670 MemoryRegionIoeventfd *ioeventfds = NULL;
671 AddrRange tmp;
672 unsigned i;
674 view = address_space_get_flatview(as);
675 FOR_EACH_FLAT_RANGE(fr, view) {
676 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
677 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
678 int128_sub(fr->addr.start,
679 int128_make64(fr->offset_in_region)));
680 if (addrrange_intersects(fr->addr, tmp)) {
681 ++ioeventfd_nb;
682 ioeventfds = g_realloc(ioeventfds,
683 ioeventfd_nb * sizeof(*ioeventfds));
684 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
685 ioeventfds[ioeventfd_nb-1].addr = tmp;
690 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
691 as->ioeventfds, as->ioeventfd_nb);
693 g_free(as->ioeventfds);
694 as->ioeventfds = ioeventfds;
695 as->ioeventfd_nb = ioeventfd_nb;
696 flatview_unref(view);
699 static void address_space_update_topology_pass(AddressSpace *as,
700 const FlatView *old_view,
701 const FlatView *new_view,
702 bool adding)
704 unsigned iold, inew;
705 FlatRange *frold, *frnew;
707 /* Generate a symmetric difference of the old and new memory maps.
708 * Kill ranges in the old map, and instantiate ranges in the new map.
710 iold = inew = 0;
711 while (iold < old_view->nr || inew < new_view->nr) {
712 if (iold < old_view->nr) {
713 frold = &old_view->ranges[iold];
714 } else {
715 frold = NULL;
717 if (inew < new_view->nr) {
718 frnew = &new_view->ranges[inew];
719 } else {
720 frnew = NULL;
723 if (frold
724 && (!frnew
725 || int128_lt(frold->addr.start, frnew->addr.start)
726 || (int128_eq(frold->addr.start, frnew->addr.start)
727 && !flatrange_equal(frold, frnew)))) {
728 /* In old but not in new, or in both but attributes changed. */
730 if (!adding) {
731 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
734 ++iold;
735 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
736 /* In both and unchanged (except logging may have changed) */
738 if (adding) {
739 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
740 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
741 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop);
742 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
743 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start);
747 ++iold;
748 ++inew;
749 } else {
750 /* In new */
752 if (adding) {
753 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
756 ++inew;
762 static void address_space_update_topology(AddressSpace *as)
764 FlatView *old_view = address_space_get_flatview(as);
765 FlatView *new_view = generate_memory_topology(as->root);
767 address_space_update_topology_pass(as, old_view, new_view, false);
768 address_space_update_topology_pass(as, old_view, new_view, true);
770 qemu_mutex_lock(&flat_view_mutex);
771 flatview_unref(as->current_map);
772 as->current_map = new_view;
773 qemu_mutex_unlock(&flat_view_mutex);
775 /* Note that all the old MemoryRegions are still alive up to this
776 * point. This relieves most MemoryListeners from the need to
777 * ref/unref the MemoryRegions they get---unless they use them
778 * outside the iothread mutex, in which case precise reference
779 * counting is necessary.
781 flatview_unref(old_view);
783 address_space_update_ioeventfds(as);
786 void memory_region_transaction_begin(void)
788 qemu_flush_coalesced_mmio_buffer();
789 ++memory_region_transaction_depth;
792 static void memory_region_clear_pending(void)
794 memory_region_update_pending = false;
795 ioeventfd_update_pending = false;
798 void memory_region_transaction_commit(void)
800 AddressSpace *as;
802 assert(memory_region_transaction_depth);
803 --memory_region_transaction_depth;
804 if (!memory_region_transaction_depth) {
805 if (memory_region_update_pending) {
806 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
808 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
809 address_space_update_topology(as);
812 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
813 } else if (ioeventfd_update_pending) {
814 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
815 address_space_update_ioeventfds(as);
818 memory_region_clear_pending();
822 static void memory_region_destructor_none(MemoryRegion *mr)
826 static void memory_region_destructor_ram(MemoryRegion *mr)
828 qemu_ram_free(mr->ram_addr);
831 static void memory_region_destructor_alias(MemoryRegion *mr)
833 memory_region_unref(mr->alias);
836 static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
838 qemu_ram_free_from_ptr(mr->ram_addr);
841 static void memory_region_destructor_rom_device(MemoryRegion *mr)
843 qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
846 static bool memory_region_need_escape(char c)
848 return c == '/' || c == '[' || c == '\\' || c == ']';
851 static char *memory_region_escape_name(const char *name)
853 const char *p;
854 char *escaped, *q;
855 uint8_t c;
856 size_t bytes = 0;
858 for (p = name; *p; p++) {
859 bytes += memory_region_need_escape(*p) ? 4 : 1;
861 if (bytes == p - name) {
862 return g_memdup(name, bytes + 1);
865 escaped = g_malloc(bytes + 1);
866 for (p = name, q = escaped; *p; p++) {
867 c = *p;
868 if (unlikely(memory_region_need_escape(c))) {
869 *q++ = '\\';
870 *q++ = 'x';
871 *q++ = "0123456789abcdef"[c >> 4];
872 c = "0123456789abcdef"[c & 15];
874 *q++ = c;
876 *q = 0;
877 return escaped;
880 static void object_property_add_child_array(Object *owner,
881 const char *name,
882 Object *child)
884 int i;
885 char *base_name = memory_region_escape_name(name);
887 for (i = 0; ; i++) {
888 char *full_name = g_strdup_printf("%s[%d]", base_name, i);
889 Error *local_err = NULL;
891 object_property_add_child(owner, full_name, child, &local_err);
892 g_free(full_name);
893 if (!local_err) {
894 break;
897 error_free(local_err);
900 g_free(base_name);
904 void memory_region_init(MemoryRegion *mr,
905 Object *owner,
906 const char *name,
907 uint64_t size)
909 if (!owner) {
910 owner = qdev_get_machine();
913 object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION);
914 mr->size = int128_make64(size);
915 if (size == UINT64_MAX) {
916 mr->size = int128_2_64();
918 mr->name = g_strdup(name);
920 if (name) {
921 object_property_add_child_array(owner, name, OBJECT(mr));
922 object_unref(OBJECT(mr));
926 static void memory_region_get_addr(Object *obj, Visitor *v, void *opaque,
927 const char *name, Error **errp)
929 MemoryRegion *mr = MEMORY_REGION(obj);
930 uint64_t value = mr->addr;
932 visit_type_uint64(v, &value, name, errp);
935 static void memory_region_get_container(Object *obj, Visitor *v, void *opaque,
936 const char *name, Error **errp)
938 MemoryRegion *mr = MEMORY_REGION(obj);
939 gchar *path = (gchar *)"";
941 if (mr->container) {
942 path = object_get_canonical_path(OBJECT(mr->container));
944 visit_type_str(v, &path, name, errp);
945 if (mr->container) {
946 g_free(path);
950 static Object *memory_region_resolve_container(Object *obj, void *opaque,
951 const char *part)
953 MemoryRegion *mr = MEMORY_REGION(obj);
955 return OBJECT(mr->container);
958 static void memory_region_initfn(Object *obj)
960 MemoryRegion *mr = MEMORY_REGION(obj);
961 ObjectProperty *op;
963 mr->ops = &unassigned_mem_ops;
964 mr->enabled = true;
965 mr->romd_mode = true;
966 mr->destructor = memory_region_destructor_none;
967 QTAILQ_INIT(&mr->subregions);
968 QTAILQ_INIT(&mr->coalesced);
970 op = object_property_add(OBJECT(mr), "container",
971 "link<" TYPE_MEMORY_REGION ">",
972 memory_region_get_container,
973 NULL, /* memory_region_set_container */
974 NULL, NULL, &error_abort);
975 op->resolve = memory_region_resolve_container;
977 object_property_add(OBJECT(mr), "addr", "uint64",
978 memory_region_get_addr,
979 NULL, /* memory_region_set_addr */
980 NULL, NULL, &error_abort);
983 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
984 unsigned size)
986 #ifdef DEBUG_UNASSIGNED
987 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
988 #endif
989 if (current_cpu != NULL) {
990 cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
992 return 0;
995 static void unassigned_mem_write(void *opaque, hwaddr addr,
996 uint64_t val, unsigned size)
998 #ifdef DEBUG_UNASSIGNED
999 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
1000 #endif
1001 if (current_cpu != NULL) {
1002 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
1006 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
1007 unsigned size, bool is_write)
1009 return false;
1012 const MemoryRegionOps unassigned_mem_ops = {
1013 .valid.accepts = unassigned_mem_accepts,
1014 .endianness = DEVICE_NATIVE_ENDIAN,
1017 bool memory_region_access_valid(MemoryRegion *mr,
1018 hwaddr addr,
1019 unsigned size,
1020 bool is_write)
1022 int access_size_min, access_size_max;
1023 int access_size, i;
1025 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
1026 return false;
1029 if (!mr->ops->valid.accepts) {
1030 return true;
1033 access_size_min = mr->ops->valid.min_access_size;
1034 if (!mr->ops->valid.min_access_size) {
1035 access_size_min = 1;
1038 access_size_max = mr->ops->valid.max_access_size;
1039 if (!mr->ops->valid.max_access_size) {
1040 access_size_max = 4;
1043 access_size = MAX(MIN(size, access_size_max), access_size_min);
1044 for (i = 0; i < size; i += access_size) {
1045 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
1046 is_write)) {
1047 return false;
1051 return true;
1054 static uint64_t memory_region_dispatch_read1(MemoryRegion *mr,
1055 hwaddr addr,
1056 unsigned size)
1058 uint64_t data = 0;
1060 if (mr->ops->read) {
1061 access_with_adjusted_size(addr, &data, size,
1062 mr->ops->impl.min_access_size,
1063 mr->ops->impl.max_access_size,
1064 memory_region_read_accessor, mr);
1065 } else {
1066 access_with_adjusted_size(addr, &data, size, 1, 4,
1067 memory_region_oldmmio_read_accessor, mr);
1070 return data;
1073 static bool memory_region_dispatch_read(MemoryRegion *mr,
1074 hwaddr addr,
1075 uint64_t *pval,
1076 unsigned size)
1078 if (!memory_region_access_valid(mr, addr, size, false)) {
1079 *pval = unassigned_mem_read(mr, addr, size);
1080 return true;
1083 *pval = memory_region_dispatch_read1(mr, addr, size);
1084 adjust_endianness(mr, pval, size);
1085 return false;
1088 static bool memory_region_dispatch_write(MemoryRegion *mr,
1089 hwaddr addr,
1090 uint64_t data,
1091 unsigned size)
1093 if (!memory_region_access_valid(mr, addr, size, true)) {
1094 unassigned_mem_write(mr, addr, data, size);
1095 return true;
1098 adjust_endianness(mr, &data, size);
1100 if (mr->ops->write) {
1101 access_with_adjusted_size(addr, &data, size,
1102 mr->ops->impl.min_access_size,
1103 mr->ops->impl.max_access_size,
1104 memory_region_write_accessor, mr);
1105 } else {
1106 access_with_adjusted_size(addr, &data, size, 1, 4,
1107 memory_region_oldmmio_write_accessor, mr);
1109 return false;
1112 void memory_region_init_io(MemoryRegion *mr,
1113 Object *owner,
1114 const MemoryRegionOps *ops,
1115 void *opaque,
1116 const char *name,
1117 uint64_t size)
1119 memory_region_init(mr, owner, name, size);
1120 mr->ops = ops;
1121 mr->opaque = opaque;
1122 mr->terminates = true;
1123 mr->ram_addr = ~(ram_addr_t)0;
1126 void memory_region_init_ram(MemoryRegion *mr,
1127 Object *owner,
1128 const char *name,
1129 uint64_t size)
1131 memory_region_init(mr, owner, name, size);
1132 mr->ram = true;
1133 mr->terminates = true;
1134 mr->destructor = memory_region_destructor_ram;
1135 mr->ram_addr = qemu_ram_alloc(size, mr);
1138 #ifdef __linux__
1139 void memory_region_init_ram_from_file(MemoryRegion *mr,
1140 struct Object *owner,
1141 const char *name,
1142 uint64_t size,
1143 bool share,
1144 const char *path,
1145 Error **errp)
1147 memory_region_init(mr, owner, name, size);
1148 mr->ram = true;
1149 mr->terminates = true;
1150 mr->destructor = memory_region_destructor_ram;
1151 mr->ram_addr = qemu_ram_alloc_from_file(size, mr, share, path, errp);
1153 #endif
1155 void memory_region_init_ram_ptr(MemoryRegion *mr,
1156 Object *owner,
1157 const char *name,
1158 uint64_t size,
1159 void *ptr)
1161 memory_region_init(mr, owner, name, size);
1162 mr->ram = true;
1163 mr->terminates = true;
1164 mr->destructor = memory_region_destructor_ram_from_ptr;
1165 mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr);
1168 void memory_region_init_alias(MemoryRegion *mr,
1169 Object *owner,
1170 const char *name,
1171 MemoryRegion *orig,
1172 hwaddr offset,
1173 uint64_t size)
1175 memory_region_init(mr, owner, name, size);
1176 memory_region_ref(orig);
1177 mr->destructor = memory_region_destructor_alias;
1178 mr->alias = orig;
1179 mr->alias_offset = offset;
1182 void memory_region_init_rom_device(MemoryRegion *mr,
1183 Object *owner,
1184 const MemoryRegionOps *ops,
1185 void *opaque,
1186 const char *name,
1187 uint64_t size)
1189 memory_region_init(mr, owner, name, size);
1190 mr->ops = ops;
1191 mr->opaque = opaque;
1192 mr->terminates = true;
1193 mr->rom_device = true;
1194 mr->destructor = memory_region_destructor_rom_device;
1195 mr->ram_addr = qemu_ram_alloc(size, mr);
1198 void memory_region_init_iommu(MemoryRegion *mr,
1199 Object *owner,
1200 const MemoryRegionIOMMUOps *ops,
1201 const char *name,
1202 uint64_t size)
1204 memory_region_init(mr, owner, name, size);
1205 mr->iommu_ops = ops,
1206 mr->terminates = true; /* then re-forwards */
1207 notifier_list_init(&mr->iommu_notify);
1210 void memory_region_init_reservation(MemoryRegion *mr,
1211 Object *owner,
1212 const char *name,
1213 uint64_t size)
1215 memory_region_init_io(mr, owner, &unassigned_mem_ops, mr, name, size);
1218 static void memory_region_finalize(Object *obj)
1220 MemoryRegion *mr = MEMORY_REGION(obj);
1222 assert(QTAILQ_EMPTY(&mr->subregions));
1223 assert(memory_region_transaction_depth == 0);
1224 mr->destructor(mr);
1225 memory_region_clear_coalescing(mr);
1226 g_free((char *)mr->name);
1227 g_free(mr->ioeventfds);
1230 void memory_region_destroy(MemoryRegion *mr)
1232 object_unparent(OBJECT(mr));
1236 Object *memory_region_owner(MemoryRegion *mr)
1238 Object *obj = OBJECT(mr);
1239 return obj->parent;
1242 void memory_region_ref(MemoryRegion *mr)
1244 /* MMIO callbacks most likely will access data that belongs
1245 * to the owner, hence the need to ref/unref the owner whenever
1246 * the memory region is in use.
1248 * The memory region is a child of its owner. As long as the
1249 * owner doesn't call unparent itself on the memory region,
1250 * ref-ing the owner will also keep the memory region alive.
1251 * Memory regions without an owner are supposed to never go away,
1252 * but we still ref/unref them for debugging purposes.
1254 Object *obj = OBJECT(mr);
1255 if (obj && obj->parent) {
1256 object_ref(obj->parent);
1257 } else {
1258 object_ref(obj);
1262 void memory_region_unref(MemoryRegion *mr)
1264 Object *obj = OBJECT(mr);
1265 if (obj && obj->parent) {
1266 object_unref(obj->parent);
1267 } else {
1268 object_unref(obj);
1272 uint64_t memory_region_size(MemoryRegion *mr)
1274 if (int128_eq(mr->size, int128_2_64())) {
1275 return UINT64_MAX;
1277 return int128_get64(mr->size);
1280 const char *memory_region_name(MemoryRegion *mr)
1282 return mr->name;
1285 bool memory_region_is_ram(MemoryRegion *mr)
1287 return mr->ram;
1290 bool memory_region_is_logging(MemoryRegion *mr)
1292 return mr->dirty_log_mask;
1295 bool memory_region_is_rom(MemoryRegion *mr)
1297 return mr->ram && mr->readonly;
1300 bool memory_region_is_iommu(MemoryRegion *mr)
1302 return mr->iommu_ops;
1305 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n)
1307 notifier_list_add(&mr->iommu_notify, n);
1310 void memory_region_unregister_iommu_notifier(Notifier *n)
1312 notifier_remove(n);
1315 void memory_region_notify_iommu(MemoryRegion *mr,
1316 IOMMUTLBEntry entry)
1318 assert(memory_region_is_iommu(mr));
1319 notifier_list_notify(&mr->iommu_notify, &entry);
1322 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1324 uint8_t mask = 1 << client;
1326 memory_region_transaction_begin();
1327 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1328 memory_region_update_pending |= mr->enabled;
1329 memory_region_transaction_commit();
1332 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1333 hwaddr size, unsigned client)
1335 assert(mr->terminates);
1336 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, size, client);
1339 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1340 hwaddr size)
1342 assert(mr->terminates);
1343 cpu_physical_memory_set_dirty_range(mr->ram_addr + addr, size);
1346 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
1347 hwaddr size, unsigned client)
1349 bool ret;
1350 assert(mr->terminates);
1351 ret = cpu_physical_memory_get_dirty(mr->ram_addr + addr, size, client);
1352 if (ret) {
1353 cpu_physical_memory_reset_dirty(mr->ram_addr + addr, size, client);
1355 return ret;
1359 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1361 AddressSpace *as;
1362 FlatRange *fr;
1364 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1365 FlatView *view = address_space_get_flatview(as);
1366 FOR_EACH_FLAT_RANGE(fr, view) {
1367 if (fr->mr == mr) {
1368 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1371 flatview_unref(view);
1375 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1377 if (mr->readonly != readonly) {
1378 memory_region_transaction_begin();
1379 mr->readonly = readonly;
1380 memory_region_update_pending |= mr->enabled;
1381 memory_region_transaction_commit();
1385 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
1387 if (mr->romd_mode != romd_mode) {
1388 memory_region_transaction_begin();
1389 mr->romd_mode = romd_mode;
1390 memory_region_update_pending |= mr->enabled;
1391 memory_region_transaction_commit();
1395 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1396 hwaddr size, unsigned client)
1398 assert(mr->terminates);
1399 cpu_physical_memory_reset_dirty(mr->ram_addr + addr, size, client);
1402 int memory_region_get_fd(MemoryRegion *mr)
1404 if (mr->alias) {
1405 return memory_region_get_fd(mr->alias);
1408 assert(mr->terminates);
1410 return qemu_get_ram_fd(mr->ram_addr & TARGET_PAGE_MASK);
1413 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1415 if (mr->alias) {
1416 return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset;
1419 assert(mr->terminates);
1421 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
1424 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
1426 FlatView *view;
1427 FlatRange *fr;
1428 CoalescedMemoryRange *cmr;
1429 AddrRange tmp;
1430 MemoryRegionSection section;
1432 view = address_space_get_flatview(as);
1433 FOR_EACH_FLAT_RANGE(fr, view) {
1434 if (fr->mr == mr) {
1435 section = (MemoryRegionSection) {
1436 .address_space = as,
1437 .offset_within_address_space = int128_get64(fr->addr.start),
1438 .size = fr->addr.size,
1441 MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, &section,
1442 int128_get64(fr->addr.start),
1443 int128_get64(fr->addr.size));
1444 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1445 tmp = addrrange_shift(cmr->addr,
1446 int128_sub(fr->addr.start,
1447 int128_make64(fr->offset_in_region)));
1448 if (!addrrange_intersects(tmp, fr->addr)) {
1449 continue;
1451 tmp = addrrange_intersection(tmp, fr->addr);
1452 MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, &section,
1453 int128_get64(tmp.start),
1454 int128_get64(tmp.size));
1458 flatview_unref(view);
1461 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1463 AddressSpace *as;
1465 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1466 memory_region_update_coalesced_range_as(mr, as);
1470 void memory_region_set_coalescing(MemoryRegion *mr)
1472 memory_region_clear_coalescing(mr);
1473 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1476 void memory_region_add_coalescing(MemoryRegion *mr,
1477 hwaddr offset,
1478 uint64_t size)
1480 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1482 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1483 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1484 memory_region_update_coalesced_range(mr);
1485 memory_region_set_flush_coalesced(mr);
1488 void memory_region_clear_coalescing(MemoryRegion *mr)
1490 CoalescedMemoryRange *cmr;
1491 bool updated = false;
1493 qemu_flush_coalesced_mmio_buffer();
1494 mr->flush_coalesced_mmio = false;
1496 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1497 cmr = QTAILQ_FIRST(&mr->coalesced);
1498 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1499 g_free(cmr);
1500 updated = true;
1503 if (updated) {
1504 memory_region_update_coalesced_range(mr);
1508 void memory_region_set_flush_coalesced(MemoryRegion *mr)
1510 mr->flush_coalesced_mmio = true;
1513 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
1515 qemu_flush_coalesced_mmio_buffer();
1516 if (QTAILQ_EMPTY(&mr->coalesced)) {
1517 mr->flush_coalesced_mmio = false;
1521 void memory_region_add_eventfd(MemoryRegion *mr,
1522 hwaddr addr,
1523 unsigned size,
1524 bool match_data,
1525 uint64_t data,
1526 EventNotifier *e)
1528 MemoryRegionIoeventfd mrfd = {
1529 .addr.start = int128_make64(addr),
1530 .addr.size = int128_make64(size),
1531 .match_data = match_data,
1532 .data = data,
1533 .e = e,
1535 unsigned i;
1537 adjust_endianness(mr, &mrfd.data, size);
1538 memory_region_transaction_begin();
1539 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1540 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1541 break;
1544 ++mr->ioeventfd_nb;
1545 mr->ioeventfds = g_realloc(mr->ioeventfds,
1546 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1547 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1548 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1549 mr->ioeventfds[i] = mrfd;
1550 ioeventfd_update_pending |= mr->enabled;
1551 memory_region_transaction_commit();
1554 void memory_region_del_eventfd(MemoryRegion *mr,
1555 hwaddr addr,
1556 unsigned size,
1557 bool match_data,
1558 uint64_t data,
1559 EventNotifier *e)
1561 MemoryRegionIoeventfd mrfd = {
1562 .addr.start = int128_make64(addr),
1563 .addr.size = int128_make64(size),
1564 .match_data = match_data,
1565 .data = data,
1566 .e = e,
1568 unsigned i;
1570 adjust_endianness(mr, &mrfd.data, size);
1571 memory_region_transaction_begin();
1572 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1573 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1574 break;
1577 assert(i != mr->ioeventfd_nb);
1578 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1579 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1580 --mr->ioeventfd_nb;
1581 mr->ioeventfds = g_realloc(mr->ioeventfds,
1582 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1583 ioeventfd_update_pending |= mr->enabled;
1584 memory_region_transaction_commit();
1587 static void memory_region_update_container_subregions(MemoryRegion *subregion)
1589 hwaddr offset = subregion->addr;
1590 MemoryRegion *mr = subregion->container;
1591 MemoryRegion *other;
1593 memory_region_transaction_begin();
1595 memory_region_ref(subregion);
1596 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1597 if (subregion->may_overlap || other->may_overlap) {
1598 continue;
1600 if (int128_ge(int128_make64(offset),
1601 int128_add(int128_make64(other->addr), other->size))
1602 || int128_le(int128_add(int128_make64(offset), subregion->size),
1603 int128_make64(other->addr))) {
1604 continue;
1606 #if 0
1607 printf("warning: subregion collision %llx/%llx (%s) "
1608 "vs %llx/%llx (%s)\n",
1609 (unsigned long long)offset,
1610 (unsigned long long)int128_get64(subregion->size),
1611 subregion->name,
1612 (unsigned long long)other->addr,
1613 (unsigned long long)int128_get64(other->size),
1614 other->name);
1615 #endif
1617 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1618 if (subregion->priority >= other->priority) {
1619 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1620 goto done;
1623 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1624 done:
1625 memory_region_update_pending |= mr->enabled && subregion->enabled;
1626 memory_region_transaction_commit();
1629 static void memory_region_add_subregion_common(MemoryRegion *mr,
1630 hwaddr offset,
1631 MemoryRegion *subregion)
1633 assert(!subregion->container);
1634 subregion->container = mr;
1635 subregion->addr = offset;
1636 memory_region_update_container_subregions(subregion);
1639 void memory_region_add_subregion(MemoryRegion *mr,
1640 hwaddr offset,
1641 MemoryRegion *subregion)
1643 subregion->may_overlap = false;
1644 subregion->priority = 0;
1645 memory_region_add_subregion_common(mr, offset, subregion);
1648 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1649 hwaddr offset,
1650 MemoryRegion *subregion,
1651 int priority)
1653 subregion->may_overlap = true;
1654 subregion->priority = priority;
1655 memory_region_add_subregion_common(mr, offset, subregion);
1658 void memory_region_del_subregion(MemoryRegion *mr,
1659 MemoryRegion *subregion)
1661 memory_region_transaction_begin();
1662 assert(subregion->container == mr);
1663 subregion->container = NULL;
1664 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1665 memory_region_unref(subregion);
1666 memory_region_update_pending |= mr->enabled && subregion->enabled;
1667 memory_region_transaction_commit();
1670 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1672 if (enabled == mr->enabled) {
1673 return;
1675 memory_region_transaction_begin();
1676 mr->enabled = enabled;
1677 memory_region_update_pending = true;
1678 memory_region_transaction_commit();
1681 static void memory_region_readd_subregion(MemoryRegion *mr)
1683 MemoryRegion *container = mr->container;
1685 if (container) {
1686 memory_region_transaction_begin();
1687 memory_region_ref(mr);
1688 memory_region_del_subregion(container, mr);
1689 mr->container = container;
1690 memory_region_update_container_subregions(mr);
1691 memory_region_unref(mr);
1692 memory_region_transaction_commit();
1696 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
1698 if (addr != mr->addr) {
1699 mr->addr = addr;
1700 memory_region_readd_subregion(mr);
1704 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
1706 assert(mr->alias);
1708 if (offset == mr->alias_offset) {
1709 return;
1712 memory_region_transaction_begin();
1713 mr->alias_offset = offset;
1714 memory_region_update_pending |= mr->enabled;
1715 memory_region_transaction_commit();
1718 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1720 return mr->ram_addr;
1723 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1725 const AddrRange *addr = addr_;
1726 const FlatRange *fr = fr_;
1728 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1729 return -1;
1730 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1731 return 1;
1733 return 0;
1736 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
1738 return bsearch(&addr, view->ranges, view->nr,
1739 sizeof(FlatRange), cmp_flatrange_addr);
1742 bool memory_region_present(MemoryRegion *container, hwaddr addr)
1744 MemoryRegion *mr = memory_region_find(container, addr, 1).mr;
1745 if (!mr || (mr == container)) {
1746 return false;
1748 memory_region_unref(mr);
1749 return true;
1752 bool memory_region_is_mapped(MemoryRegion *mr)
1754 return mr->container ? true : false;
1757 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1758 hwaddr addr, uint64_t size)
1760 MemoryRegionSection ret = { .mr = NULL };
1761 MemoryRegion *root;
1762 AddressSpace *as;
1763 AddrRange range;
1764 FlatView *view;
1765 FlatRange *fr;
1767 addr += mr->addr;
1768 for (root = mr; root->container; ) {
1769 root = root->container;
1770 addr += root->addr;
1773 as = memory_region_to_address_space(root);
1774 if (!as) {
1775 return ret;
1777 range = addrrange_make(int128_make64(addr), int128_make64(size));
1779 view = address_space_get_flatview(as);
1780 fr = flatview_lookup(view, range);
1781 if (!fr) {
1782 flatview_unref(view);
1783 return ret;
1786 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
1787 --fr;
1790 ret.mr = fr->mr;
1791 ret.address_space = as;
1792 range = addrrange_intersection(range, fr->addr);
1793 ret.offset_within_region = fr->offset_in_region;
1794 ret.offset_within_region += int128_get64(int128_sub(range.start,
1795 fr->addr.start));
1796 ret.size = range.size;
1797 ret.offset_within_address_space = int128_get64(range.start);
1798 ret.readonly = fr->readonly;
1799 memory_region_ref(ret.mr);
1801 flatview_unref(view);
1802 return ret;
1805 void address_space_sync_dirty_bitmap(AddressSpace *as)
1807 FlatView *view;
1808 FlatRange *fr;
1810 view = address_space_get_flatview(as);
1811 FOR_EACH_FLAT_RANGE(fr, view) {
1812 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1814 flatview_unref(view);
1817 void memory_global_dirty_log_start(void)
1819 global_dirty_log = true;
1820 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
1823 void memory_global_dirty_log_stop(void)
1825 global_dirty_log = false;
1826 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
1829 static void listener_add_address_space(MemoryListener *listener,
1830 AddressSpace *as)
1832 FlatView *view;
1833 FlatRange *fr;
1835 if (listener->address_space_filter
1836 && listener->address_space_filter != as) {
1837 return;
1840 if (global_dirty_log) {
1841 if (listener->log_global_start) {
1842 listener->log_global_start(listener);
1846 view = address_space_get_flatview(as);
1847 FOR_EACH_FLAT_RANGE(fr, view) {
1848 MemoryRegionSection section = {
1849 .mr = fr->mr,
1850 .address_space = as,
1851 .offset_within_region = fr->offset_in_region,
1852 .size = fr->addr.size,
1853 .offset_within_address_space = int128_get64(fr->addr.start),
1854 .readonly = fr->readonly,
1856 if (listener->region_add) {
1857 listener->region_add(listener, &section);
1860 flatview_unref(view);
1863 void memory_listener_register(MemoryListener *listener, AddressSpace *filter)
1865 MemoryListener *other = NULL;
1866 AddressSpace *as;
1868 listener->address_space_filter = filter;
1869 if (QTAILQ_EMPTY(&memory_listeners)
1870 || listener->priority >= QTAILQ_LAST(&memory_listeners,
1871 memory_listeners)->priority) {
1872 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
1873 } else {
1874 QTAILQ_FOREACH(other, &memory_listeners, link) {
1875 if (listener->priority < other->priority) {
1876 break;
1879 QTAILQ_INSERT_BEFORE(other, listener, link);
1882 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1883 listener_add_address_space(listener, as);
1887 void memory_listener_unregister(MemoryListener *listener)
1889 QTAILQ_REMOVE(&memory_listeners, listener, link);
1892 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
1894 if (QTAILQ_EMPTY(&address_spaces)) {
1895 memory_init();
1898 memory_region_transaction_begin();
1899 as->root = root;
1900 as->current_map = g_new(FlatView, 1);
1901 flatview_init(as->current_map);
1902 as->ioeventfd_nb = 0;
1903 as->ioeventfds = NULL;
1904 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
1905 as->name = g_strdup(name ? name : "anonymous");
1906 address_space_init_dispatch(as);
1907 memory_region_update_pending |= root->enabled;
1908 memory_region_transaction_commit();
1911 void address_space_destroy(AddressSpace *as)
1913 MemoryListener *listener;
1915 /* Flush out anything from MemoryListeners listening in on this */
1916 memory_region_transaction_begin();
1917 as->root = NULL;
1918 memory_region_transaction_commit();
1919 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
1920 address_space_destroy_dispatch(as);
1922 QTAILQ_FOREACH(listener, &memory_listeners, link) {
1923 assert(listener->address_space_filter != as);
1926 flatview_unref(as->current_map);
1927 g_free(as->name);
1928 g_free(as->ioeventfds);
1931 bool io_mem_read(MemoryRegion *mr, hwaddr addr, uint64_t *pval, unsigned size)
1933 return memory_region_dispatch_read(mr, addr, pval, size);
1936 bool io_mem_write(MemoryRegion *mr, hwaddr addr,
1937 uint64_t val, unsigned size)
1939 return memory_region_dispatch_write(mr, addr, val, size);
1942 typedef struct MemoryRegionList MemoryRegionList;
1944 struct MemoryRegionList {
1945 const MemoryRegion *mr;
1946 bool printed;
1947 QTAILQ_ENTRY(MemoryRegionList) queue;
1950 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1952 static void mtree_print_mr(fprintf_function mon_printf, void *f,
1953 const MemoryRegion *mr, unsigned int level,
1954 hwaddr base,
1955 MemoryRegionListHead *alias_print_queue)
1957 MemoryRegionList *new_ml, *ml, *next_ml;
1958 MemoryRegionListHead submr_print_queue;
1959 const MemoryRegion *submr;
1960 unsigned int i;
1962 if (!mr || !mr->enabled) {
1963 return;
1966 for (i = 0; i < level; i++) {
1967 mon_printf(f, " ");
1970 if (mr->alias) {
1971 MemoryRegionList *ml;
1972 bool found = false;
1974 /* check if the alias is already in the queue */
1975 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
1976 if (ml->mr == mr->alias && !ml->printed) {
1977 found = true;
1981 if (!found) {
1982 ml = g_new(MemoryRegionList, 1);
1983 ml->mr = mr->alias;
1984 ml->printed = false;
1985 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
1987 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
1988 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1989 "-" TARGET_FMT_plx "\n",
1990 base + mr->addr,
1991 base + mr->addr
1992 + (int128_nz(mr->size) ?
1993 (hwaddr)int128_get64(int128_sub(mr->size,
1994 int128_one())) : 0),
1995 mr->priority,
1996 mr->romd_mode ? 'R' : '-',
1997 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
1998 : '-',
1999 mr->name,
2000 mr->alias->name,
2001 mr->alias_offset,
2002 mr->alias_offset
2003 + (int128_nz(mr->size) ?
2004 (hwaddr)int128_get64(int128_sub(mr->size,
2005 int128_one())) : 0));
2006 } else {
2007 mon_printf(f,
2008 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %c%c): %s\n",
2009 base + mr->addr,
2010 base + mr->addr
2011 + (int128_nz(mr->size) ?
2012 (hwaddr)int128_get64(int128_sub(mr->size,
2013 int128_one())) : 0),
2014 mr->priority,
2015 mr->romd_mode ? 'R' : '-',
2016 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
2017 : '-',
2018 mr->name);
2021 QTAILQ_INIT(&submr_print_queue);
2023 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
2024 new_ml = g_new(MemoryRegionList, 1);
2025 new_ml->mr = submr;
2026 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
2027 if (new_ml->mr->addr < ml->mr->addr ||
2028 (new_ml->mr->addr == ml->mr->addr &&
2029 new_ml->mr->priority > ml->mr->priority)) {
2030 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
2031 new_ml = NULL;
2032 break;
2035 if (new_ml) {
2036 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
2040 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
2041 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
2042 alias_print_queue);
2045 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
2046 g_free(ml);
2050 void mtree_info(fprintf_function mon_printf, void *f)
2052 MemoryRegionListHead ml_head;
2053 MemoryRegionList *ml, *ml2;
2054 AddressSpace *as;
2056 QTAILQ_INIT(&ml_head);
2058 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2059 mon_printf(f, "%s\n", as->name);
2060 mtree_print_mr(mon_printf, f, as->root, 0, 0, &ml_head);
2063 mon_printf(f, "aliases\n");
2064 /* print aliased regions */
2065 QTAILQ_FOREACH(ml, &ml_head, queue) {
2066 if (!ml->printed) {
2067 mon_printf(f, "%s\n", ml->mr->name);
2068 mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head);
2072 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
2073 g_free(ml);
2077 static const TypeInfo memory_region_info = {
2078 .parent = TYPE_OBJECT,
2079 .name = TYPE_MEMORY_REGION,
2080 .instance_size = sizeof(MemoryRegion),
2081 .instance_init = memory_region_initfn,
2082 .instance_finalize = memory_region_finalize,
2085 static void memory_register_types(void)
2087 type_register_static(&memory_region_info);
2090 type_init(memory_register_types)