nbd: Switch to byte-based block access
[qemu/ar7.git] / memory.c
blobf76f85df95eea6a31ccf99ff168b91ad12842820
1 /*
2 * Physical memory management
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "exec/memory.h"
19 #include "exec/address-spaces.h"
20 #include "exec/ioport.h"
21 #include "qapi/visitor.h"
22 #include "qemu/bitops.h"
23 #include "qemu/error-report.h"
24 #include "qom/object.h"
25 #include "trace.h"
27 #include "exec/memory-internal.h"
28 #include "exec/ram_addr.h"
29 #include "sysemu/kvm.h"
30 #include "sysemu/sysemu.h"
32 //#define DEBUG_UNASSIGNED
34 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
36 static unsigned memory_region_transaction_depth;
37 static bool memory_region_update_pending;
38 static bool ioeventfd_update_pending;
39 static bool global_dirty_log = false;
41 static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners
42 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
44 static QTAILQ_HEAD(, AddressSpace) address_spaces
45 = QTAILQ_HEAD_INITIALIZER(address_spaces);
47 typedef struct AddrRange AddrRange;
50 * Note that signed integers are needed for negative offsetting in aliases
51 * (large MemoryRegion::alias_offset).
53 struct AddrRange {
54 Int128 start;
55 Int128 size;
58 static AddrRange addrrange_make(Int128 start, Int128 size)
60 return (AddrRange) { start, size };
63 static bool addrrange_equal(AddrRange r1, AddrRange r2)
65 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
68 static Int128 addrrange_end(AddrRange r)
70 return int128_add(r.start, r.size);
73 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
75 int128_addto(&range.start, delta);
76 return range;
79 static bool addrrange_contains(AddrRange range, Int128 addr)
81 return int128_ge(addr, range.start)
82 && int128_lt(addr, addrrange_end(range));
85 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
87 return addrrange_contains(r1, r2.start)
88 || addrrange_contains(r2, r1.start);
91 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
93 Int128 start = int128_max(r1.start, r2.start);
94 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
95 return addrrange_make(start, int128_sub(end, start));
98 enum ListenerDirection { Forward, Reverse };
100 static bool memory_listener_match(MemoryListener *listener,
101 MemoryRegionSection *section)
103 return !listener->address_space_filter
104 || listener->address_space_filter == section->address_space;
107 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
108 do { \
109 MemoryListener *_listener; \
111 switch (_direction) { \
112 case Forward: \
113 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
114 if (_listener->_callback) { \
115 _listener->_callback(_listener, ##_args); \
118 break; \
119 case Reverse: \
120 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
121 memory_listeners, link) { \
122 if (_listener->_callback) { \
123 _listener->_callback(_listener, ##_args); \
126 break; \
127 default: \
128 abort(); \
130 } while (0)
132 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
133 do { \
134 MemoryListener *_listener; \
136 switch (_direction) { \
137 case Forward: \
138 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
139 if (_listener->_callback \
140 && memory_listener_match(_listener, _section)) { \
141 _listener->_callback(_listener, _section, ##_args); \
144 break; \
145 case Reverse: \
146 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
147 memory_listeners, link) { \
148 if (_listener->_callback \
149 && memory_listener_match(_listener, _section)) { \
150 _listener->_callback(_listener, _section, ##_args); \
153 break; \
154 default: \
155 abort(); \
157 } while (0)
159 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
160 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback, _args...) \
161 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
162 .mr = (fr)->mr, \
163 .address_space = (as), \
164 .offset_within_region = (fr)->offset_in_region, \
165 .size = (fr)->addr.size, \
166 .offset_within_address_space = int128_get64((fr)->addr.start), \
167 .readonly = (fr)->readonly, \
168 }), ##_args)
170 struct CoalescedMemoryRange {
171 AddrRange addr;
172 QTAILQ_ENTRY(CoalescedMemoryRange) link;
175 struct MemoryRegionIoeventfd {
176 AddrRange addr;
177 bool match_data;
178 uint64_t data;
179 EventNotifier *e;
182 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
183 MemoryRegionIoeventfd b)
185 if (int128_lt(a.addr.start, b.addr.start)) {
186 return true;
187 } else if (int128_gt(a.addr.start, b.addr.start)) {
188 return false;
189 } else if (int128_lt(a.addr.size, b.addr.size)) {
190 return true;
191 } else if (int128_gt(a.addr.size, b.addr.size)) {
192 return false;
193 } else if (a.match_data < b.match_data) {
194 return true;
195 } else if (a.match_data > b.match_data) {
196 return false;
197 } else if (a.match_data) {
198 if (a.data < b.data) {
199 return true;
200 } else if (a.data > b.data) {
201 return false;
204 if (a.e < b.e) {
205 return true;
206 } else if (a.e > b.e) {
207 return false;
209 return false;
212 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
213 MemoryRegionIoeventfd b)
215 return !memory_region_ioeventfd_before(a, b)
216 && !memory_region_ioeventfd_before(b, a);
219 typedef struct FlatRange FlatRange;
220 typedef struct FlatView FlatView;
222 /* Range of memory in the global map. Addresses are absolute. */
223 struct FlatRange {
224 MemoryRegion *mr;
225 hwaddr offset_in_region;
226 AddrRange addr;
227 uint8_t dirty_log_mask;
228 bool romd_mode;
229 bool readonly;
232 /* Flattened global view of current active memory hierarchy. Kept in sorted
233 * order.
235 struct FlatView {
236 struct rcu_head rcu;
237 unsigned ref;
238 FlatRange *ranges;
239 unsigned nr;
240 unsigned nr_allocated;
243 typedef struct AddressSpaceOps AddressSpaceOps;
245 #define FOR_EACH_FLAT_RANGE(var, view) \
246 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
248 static bool flatrange_equal(FlatRange *a, FlatRange *b)
250 return a->mr == b->mr
251 && addrrange_equal(a->addr, b->addr)
252 && a->offset_in_region == b->offset_in_region
253 && a->romd_mode == b->romd_mode
254 && a->readonly == b->readonly;
257 static void flatview_init(FlatView *view)
259 view->ref = 1;
260 view->ranges = NULL;
261 view->nr = 0;
262 view->nr_allocated = 0;
265 /* Insert a range into a given position. Caller is responsible for maintaining
266 * sorting order.
268 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
270 if (view->nr == view->nr_allocated) {
271 view->nr_allocated = MAX(2 * view->nr, 10);
272 view->ranges = g_realloc(view->ranges,
273 view->nr_allocated * sizeof(*view->ranges));
275 memmove(view->ranges + pos + 1, view->ranges + pos,
276 (view->nr - pos) * sizeof(FlatRange));
277 view->ranges[pos] = *range;
278 memory_region_ref(range->mr);
279 ++view->nr;
282 static void flatview_destroy(FlatView *view)
284 int i;
286 for (i = 0; i < view->nr; i++) {
287 memory_region_unref(view->ranges[i].mr);
289 g_free(view->ranges);
290 g_free(view);
293 static void flatview_ref(FlatView *view)
295 atomic_inc(&view->ref);
298 static void flatview_unref(FlatView *view)
300 if (atomic_fetch_dec(&view->ref) == 1) {
301 flatview_destroy(view);
305 static bool can_merge(FlatRange *r1, FlatRange *r2)
307 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
308 && r1->mr == r2->mr
309 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
310 r1->addr.size),
311 int128_make64(r2->offset_in_region))
312 && r1->dirty_log_mask == r2->dirty_log_mask
313 && r1->romd_mode == r2->romd_mode
314 && r1->readonly == r2->readonly;
317 /* Attempt to simplify a view by merging adjacent ranges */
318 static void flatview_simplify(FlatView *view)
320 unsigned i, j;
322 i = 0;
323 while (i < view->nr) {
324 j = i + 1;
325 while (j < view->nr
326 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
327 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
328 ++j;
330 ++i;
331 memmove(&view->ranges[i], &view->ranges[j],
332 (view->nr - j) * sizeof(view->ranges[j]));
333 view->nr -= j - i;
337 static bool memory_region_big_endian(MemoryRegion *mr)
339 #ifdef TARGET_WORDS_BIGENDIAN
340 return mr->ops->endianness != DEVICE_LITTLE_ENDIAN;
341 #else
342 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
343 #endif
346 static bool memory_region_wrong_endianness(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 void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
357 if (memory_region_wrong_endianness(mr)) {
358 switch (size) {
359 case 1:
360 break;
361 case 2:
362 *data = bswap16(*data);
363 break;
364 case 4:
365 *data = bswap32(*data);
366 break;
367 case 8:
368 *data = bswap64(*data);
369 break;
370 default:
371 abort();
376 static hwaddr memory_region_to_absolute_addr(MemoryRegion *mr, hwaddr offset)
378 MemoryRegion *root;
379 hwaddr abs_addr = offset;
381 abs_addr += mr->addr;
382 for (root = mr; root->container; ) {
383 root = root->container;
384 abs_addr += root->addr;
387 return abs_addr;
390 static int get_cpu_index(void)
392 if (current_cpu) {
393 return current_cpu->cpu_index;
395 return -1;
398 static MemTxResult memory_region_oldmmio_read_accessor(MemoryRegion *mr,
399 hwaddr addr,
400 uint64_t *value,
401 unsigned size,
402 unsigned shift,
403 uint64_t mask,
404 MemTxAttrs attrs)
406 uint64_t tmp;
408 tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);
409 if (mr->subpage) {
410 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
411 } else if (mr == &io_mem_notdirty) {
412 /* Accesses to code which has previously been translated into a TB show
413 * up in the MMIO path, as accesses to the io_mem_notdirty
414 * MemoryRegion. */
415 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
416 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
417 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
418 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
420 *value |= (tmp & mask) << shift;
421 return MEMTX_OK;
424 static MemTxResult memory_region_read_accessor(MemoryRegion *mr,
425 hwaddr addr,
426 uint64_t *value,
427 unsigned size,
428 unsigned shift,
429 uint64_t mask,
430 MemTxAttrs attrs)
432 uint64_t tmp;
434 tmp = mr->ops->read(mr->opaque, addr, size);
435 if (mr->subpage) {
436 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
437 } else if (mr == &io_mem_notdirty) {
438 /* Accesses to code which has previously been translated into a TB show
439 * up in the MMIO path, as accesses to the io_mem_notdirty
440 * MemoryRegion. */
441 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
442 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
443 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
444 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
446 *value |= (tmp & mask) << shift;
447 return MEMTX_OK;
450 static MemTxResult memory_region_read_with_attrs_accessor(MemoryRegion *mr,
451 hwaddr addr,
452 uint64_t *value,
453 unsigned size,
454 unsigned shift,
455 uint64_t mask,
456 MemTxAttrs attrs)
458 uint64_t tmp = 0;
459 MemTxResult r;
461 r = mr->ops->read_with_attrs(mr->opaque, addr, &tmp, size, attrs);
462 if (mr->subpage) {
463 trace_memory_region_subpage_read(get_cpu_index(), mr, addr, tmp, size);
464 } else if (mr == &io_mem_notdirty) {
465 /* Accesses to code which has previously been translated into a TB show
466 * up in the MMIO path, as accesses to the io_mem_notdirty
467 * MemoryRegion. */
468 trace_memory_region_tb_read(get_cpu_index(), addr, tmp, size);
469 } else if (TRACE_MEMORY_REGION_OPS_READ_ENABLED) {
470 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
471 trace_memory_region_ops_read(get_cpu_index(), mr, abs_addr, tmp, size);
473 *value |= (tmp & mask) << shift;
474 return r;
477 static MemTxResult memory_region_oldmmio_write_accessor(MemoryRegion *mr,
478 hwaddr addr,
479 uint64_t *value,
480 unsigned size,
481 unsigned shift,
482 uint64_t mask,
483 MemTxAttrs attrs)
485 uint64_t tmp;
487 tmp = (*value >> shift) & mask;
488 if (mr->subpage) {
489 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
490 } else if (mr == &io_mem_notdirty) {
491 /* Accesses to code which has previously been translated into a TB show
492 * up in the MMIO path, as accesses to the io_mem_notdirty
493 * MemoryRegion. */
494 trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
495 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
496 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
497 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
499 mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp);
500 return MEMTX_OK;
503 static MemTxResult memory_region_write_accessor(MemoryRegion *mr,
504 hwaddr addr,
505 uint64_t *value,
506 unsigned size,
507 unsigned shift,
508 uint64_t mask,
509 MemTxAttrs attrs)
511 uint64_t tmp;
513 tmp = (*value >> shift) & mask;
514 if (mr->subpage) {
515 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
516 } else if (mr == &io_mem_notdirty) {
517 /* Accesses to code which has previously been translated into a TB show
518 * up in the MMIO path, as accesses to the io_mem_notdirty
519 * MemoryRegion. */
520 trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
521 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
522 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
523 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
525 mr->ops->write(mr->opaque, addr, tmp, size);
526 return MEMTX_OK;
529 static MemTxResult memory_region_write_with_attrs_accessor(MemoryRegion *mr,
530 hwaddr addr,
531 uint64_t *value,
532 unsigned size,
533 unsigned shift,
534 uint64_t mask,
535 MemTxAttrs attrs)
537 uint64_t tmp;
539 tmp = (*value >> shift) & mask;
540 if (mr->subpage) {
541 trace_memory_region_subpage_write(get_cpu_index(), mr, addr, tmp, size);
542 } else if (mr == &io_mem_notdirty) {
543 /* Accesses to code which has previously been translated into a TB show
544 * up in the MMIO path, as accesses to the io_mem_notdirty
545 * MemoryRegion. */
546 trace_memory_region_tb_write(get_cpu_index(), addr, tmp, size);
547 } else if (TRACE_MEMORY_REGION_OPS_WRITE_ENABLED) {
548 hwaddr abs_addr = memory_region_to_absolute_addr(mr, addr);
549 trace_memory_region_ops_write(get_cpu_index(), mr, abs_addr, tmp, size);
551 return mr->ops->write_with_attrs(mr->opaque, addr, tmp, size, attrs);
554 static MemTxResult access_with_adjusted_size(hwaddr addr,
555 uint64_t *value,
556 unsigned size,
557 unsigned access_size_min,
558 unsigned access_size_max,
559 MemTxResult (*access)(MemoryRegion *mr,
560 hwaddr addr,
561 uint64_t *value,
562 unsigned size,
563 unsigned shift,
564 uint64_t mask,
565 MemTxAttrs attrs),
566 MemoryRegion *mr,
567 MemTxAttrs attrs)
569 uint64_t access_mask;
570 unsigned access_size;
571 unsigned i;
572 MemTxResult r = MEMTX_OK;
574 if (!access_size_min) {
575 access_size_min = 1;
577 if (!access_size_max) {
578 access_size_max = 4;
581 /* FIXME: support unaligned access? */
582 access_size = MAX(MIN(size, access_size_max), access_size_min);
583 access_mask = -1ULL >> (64 - access_size * 8);
584 if (memory_region_big_endian(mr)) {
585 for (i = 0; i < size; i += access_size) {
586 r |= access(mr, addr + i, value, access_size,
587 (size - access_size - i) * 8, access_mask, attrs);
589 } else {
590 for (i = 0; i < size; i += access_size) {
591 r |= access(mr, addr + i, value, access_size, i * 8,
592 access_mask, attrs);
595 return r;
598 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
600 AddressSpace *as;
602 while (mr->container) {
603 mr = mr->container;
605 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
606 if (mr == as->root) {
607 return as;
610 return NULL;
613 /* Render a memory region into the global view. Ranges in @view obscure
614 * ranges in @mr.
616 static void render_memory_region(FlatView *view,
617 MemoryRegion *mr,
618 Int128 base,
619 AddrRange clip,
620 bool readonly)
622 MemoryRegion *subregion;
623 unsigned i;
624 hwaddr offset_in_region;
625 Int128 remain;
626 Int128 now;
627 FlatRange fr;
628 AddrRange tmp;
630 if (!mr->enabled) {
631 return;
634 int128_addto(&base, int128_make64(mr->addr));
635 readonly |= mr->readonly;
637 tmp = addrrange_make(base, mr->size);
639 if (!addrrange_intersects(tmp, clip)) {
640 return;
643 clip = addrrange_intersection(tmp, clip);
645 if (mr->alias) {
646 int128_subfrom(&base, int128_make64(mr->alias->addr));
647 int128_subfrom(&base, int128_make64(mr->alias_offset));
648 render_memory_region(view, mr->alias, base, clip, readonly);
649 return;
652 /* Render subregions in priority order. */
653 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
654 render_memory_region(view, subregion, base, clip, readonly);
657 if (!mr->terminates) {
658 return;
661 offset_in_region = int128_get64(int128_sub(clip.start, base));
662 base = clip.start;
663 remain = clip.size;
665 fr.mr = mr;
666 fr.dirty_log_mask = memory_region_get_dirty_log_mask(mr);
667 fr.romd_mode = mr->romd_mode;
668 fr.readonly = readonly;
670 /* Render the region itself into any gaps left by the current view. */
671 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
672 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
673 continue;
675 if (int128_lt(base, view->ranges[i].addr.start)) {
676 now = int128_min(remain,
677 int128_sub(view->ranges[i].addr.start, base));
678 fr.offset_in_region = offset_in_region;
679 fr.addr = addrrange_make(base, now);
680 flatview_insert(view, i, &fr);
681 ++i;
682 int128_addto(&base, now);
683 offset_in_region += int128_get64(now);
684 int128_subfrom(&remain, now);
686 now = int128_sub(int128_min(int128_add(base, remain),
687 addrrange_end(view->ranges[i].addr)),
688 base);
689 int128_addto(&base, now);
690 offset_in_region += int128_get64(now);
691 int128_subfrom(&remain, now);
693 if (int128_nz(remain)) {
694 fr.offset_in_region = offset_in_region;
695 fr.addr = addrrange_make(base, remain);
696 flatview_insert(view, i, &fr);
700 /* Render a memory topology into a list of disjoint absolute ranges. */
701 static FlatView *generate_memory_topology(MemoryRegion *mr)
703 FlatView *view;
705 view = g_new(FlatView, 1);
706 flatview_init(view);
708 if (mr) {
709 render_memory_region(view, mr, int128_zero(),
710 addrrange_make(int128_zero(), int128_2_64()), false);
712 flatview_simplify(view);
714 return view;
717 static void address_space_add_del_ioeventfds(AddressSpace *as,
718 MemoryRegionIoeventfd *fds_new,
719 unsigned fds_new_nb,
720 MemoryRegionIoeventfd *fds_old,
721 unsigned fds_old_nb)
723 unsigned iold, inew;
724 MemoryRegionIoeventfd *fd;
725 MemoryRegionSection section;
727 /* Generate a symmetric difference of the old and new fd sets, adding
728 * and deleting as necessary.
731 iold = inew = 0;
732 while (iold < fds_old_nb || inew < fds_new_nb) {
733 if (iold < fds_old_nb
734 && (inew == fds_new_nb
735 || memory_region_ioeventfd_before(fds_old[iold],
736 fds_new[inew]))) {
737 fd = &fds_old[iold];
738 section = (MemoryRegionSection) {
739 .address_space = as,
740 .offset_within_address_space = int128_get64(fd->addr.start),
741 .size = fd->addr.size,
743 MEMORY_LISTENER_CALL(eventfd_del, Forward, &section,
744 fd->match_data, fd->data, fd->e);
745 ++iold;
746 } else if (inew < fds_new_nb
747 && (iold == fds_old_nb
748 || memory_region_ioeventfd_before(fds_new[inew],
749 fds_old[iold]))) {
750 fd = &fds_new[inew];
751 section = (MemoryRegionSection) {
752 .address_space = as,
753 .offset_within_address_space = int128_get64(fd->addr.start),
754 .size = fd->addr.size,
756 MEMORY_LISTENER_CALL(eventfd_add, Reverse, &section,
757 fd->match_data, fd->data, fd->e);
758 ++inew;
759 } else {
760 ++iold;
761 ++inew;
766 static FlatView *address_space_get_flatview(AddressSpace *as)
768 FlatView *view;
770 rcu_read_lock();
771 view = atomic_rcu_read(&as->current_map);
772 flatview_ref(view);
773 rcu_read_unlock();
774 return view;
777 static void address_space_update_ioeventfds(AddressSpace *as)
779 FlatView *view;
780 FlatRange *fr;
781 unsigned ioeventfd_nb = 0;
782 MemoryRegionIoeventfd *ioeventfds = NULL;
783 AddrRange tmp;
784 unsigned i;
786 view = address_space_get_flatview(as);
787 FOR_EACH_FLAT_RANGE(fr, view) {
788 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
789 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
790 int128_sub(fr->addr.start,
791 int128_make64(fr->offset_in_region)));
792 if (addrrange_intersects(fr->addr, tmp)) {
793 ++ioeventfd_nb;
794 ioeventfds = g_realloc(ioeventfds,
795 ioeventfd_nb * sizeof(*ioeventfds));
796 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
797 ioeventfds[ioeventfd_nb-1].addr = tmp;
802 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
803 as->ioeventfds, as->ioeventfd_nb);
805 g_free(as->ioeventfds);
806 as->ioeventfds = ioeventfds;
807 as->ioeventfd_nb = ioeventfd_nb;
808 flatview_unref(view);
811 static void address_space_update_topology_pass(AddressSpace *as,
812 const FlatView *old_view,
813 const FlatView *new_view,
814 bool adding)
816 unsigned iold, inew;
817 FlatRange *frold, *frnew;
819 /* Generate a symmetric difference of the old and new memory maps.
820 * Kill ranges in the old map, and instantiate ranges in the new map.
822 iold = inew = 0;
823 while (iold < old_view->nr || inew < new_view->nr) {
824 if (iold < old_view->nr) {
825 frold = &old_view->ranges[iold];
826 } else {
827 frold = NULL;
829 if (inew < new_view->nr) {
830 frnew = &new_view->ranges[inew];
831 } else {
832 frnew = NULL;
835 if (frold
836 && (!frnew
837 || int128_lt(frold->addr.start, frnew->addr.start)
838 || (int128_eq(frold->addr.start, frnew->addr.start)
839 && !flatrange_equal(frold, frnew)))) {
840 /* In old but not in new, or in both but attributes changed. */
842 if (!adding) {
843 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
846 ++iold;
847 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
848 /* In both and unchanged (except logging may have changed) */
850 if (adding) {
851 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
852 if (frnew->dirty_log_mask & ~frold->dirty_log_mask) {
853 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start,
854 frold->dirty_log_mask,
855 frnew->dirty_log_mask);
857 if (frold->dirty_log_mask & ~frnew->dirty_log_mask) {
858 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop,
859 frold->dirty_log_mask,
860 frnew->dirty_log_mask);
864 ++iold;
865 ++inew;
866 } else {
867 /* In new */
869 if (adding) {
870 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
873 ++inew;
879 static void address_space_update_topology(AddressSpace *as)
881 FlatView *old_view = address_space_get_flatview(as);
882 FlatView *new_view = generate_memory_topology(as->root);
884 address_space_update_topology_pass(as, old_view, new_view, false);
885 address_space_update_topology_pass(as, old_view, new_view, true);
887 /* Writes are protected by the BQL. */
888 atomic_rcu_set(&as->current_map, new_view);
889 call_rcu(old_view, flatview_unref, rcu);
891 /* Note that all the old MemoryRegions are still alive up to this
892 * point. This relieves most MemoryListeners from the need to
893 * ref/unref the MemoryRegions they get---unless they use them
894 * outside the iothread mutex, in which case precise reference
895 * counting is necessary.
897 flatview_unref(old_view);
899 address_space_update_ioeventfds(as);
902 void memory_region_transaction_begin(void)
904 qemu_flush_coalesced_mmio_buffer();
905 ++memory_region_transaction_depth;
908 static void memory_region_clear_pending(void)
910 memory_region_update_pending = false;
911 ioeventfd_update_pending = false;
914 void memory_region_transaction_commit(void)
916 AddressSpace *as;
918 assert(memory_region_transaction_depth);
919 --memory_region_transaction_depth;
920 if (!memory_region_transaction_depth) {
921 if (memory_region_update_pending) {
922 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
924 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
925 address_space_update_topology(as);
928 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
929 } else if (ioeventfd_update_pending) {
930 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
931 address_space_update_ioeventfds(as);
934 memory_region_clear_pending();
938 static void memory_region_destructor_none(MemoryRegion *mr)
942 static void memory_region_destructor_ram(MemoryRegion *mr)
944 qemu_ram_free(mr->ram_block);
947 static void memory_region_destructor_rom_device(MemoryRegion *mr)
949 qemu_ram_free(mr->ram_block);
952 static bool memory_region_need_escape(char c)
954 return c == '/' || c == '[' || c == '\\' || c == ']';
957 static char *memory_region_escape_name(const char *name)
959 const char *p;
960 char *escaped, *q;
961 uint8_t c;
962 size_t bytes = 0;
964 for (p = name; *p; p++) {
965 bytes += memory_region_need_escape(*p) ? 4 : 1;
967 if (bytes == p - name) {
968 return g_memdup(name, bytes + 1);
971 escaped = g_malloc(bytes + 1);
972 for (p = name, q = escaped; *p; p++) {
973 c = *p;
974 if (unlikely(memory_region_need_escape(c))) {
975 *q++ = '\\';
976 *q++ = 'x';
977 *q++ = "0123456789abcdef"[c >> 4];
978 c = "0123456789abcdef"[c & 15];
980 *q++ = c;
982 *q = 0;
983 return escaped;
986 void memory_region_init(MemoryRegion *mr,
987 Object *owner,
988 const char *name,
989 uint64_t size)
991 object_initialize(mr, sizeof(*mr), TYPE_MEMORY_REGION);
992 mr->size = int128_make64(size);
993 if (size == UINT64_MAX) {
994 mr->size = int128_2_64();
996 mr->name = g_strdup(name);
997 mr->owner = owner;
998 mr->ram_block = NULL;
1000 if (name) {
1001 char *escaped_name = memory_region_escape_name(name);
1002 char *name_array = g_strdup_printf("%s[*]", escaped_name);
1004 if (!owner) {
1005 owner = container_get(qdev_get_machine(), "/unattached");
1008 object_property_add_child(owner, name_array, OBJECT(mr), &error_abort);
1009 object_unref(OBJECT(mr));
1010 g_free(name_array);
1011 g_free(escaped_name);
1015 static void memory_region_get_addr(Object *obj, Visitor *v, const char *name,
1016 void *opaque, Error **errp)
1018 MemoryRegion *mr = MEMORY_REGION(obj);
1019 uint64_t value = mr->addr;
1021 visit_type_uint64(v, name, &value, errp);
1024 static void memory_region_get_container(Object *obj, Visitor *v,
1025 const char *name, void *opaque,
1026 Error **errp)
1028 MemoryRegion *mr = MEMORY_REGION(obj);
1029 gchar *path = (gchar *)"";
1031 if (mr->container) {
1032 path = object_get_canonical_path(OBJECT(mr->container));
1034 visit_type_str(v, name, &path, errp);
1035 if (mr->container) {
1036 g_free(path);
1040 static Object *memory_region_resolve_container(Object *obj, void *opaque,
1041 const char *part)
1043 MemoryRegion *mr = MEMORY_REGION(obj);
1045 return OBJECT(mr->container);
1048 static void memory_region_get_priority(Object *obj, Visitor *v,
1049 const char *name, void *opaque,
1050 Error **errp)
1052 MemoryRegion *mr = MEMORY_REGION(obj);
1053 int32_t value = mr->priority;
1055 visit_type_int32(v, name, &value, errp);
1058 static bool memory_region_get_may_overlap(Object *obj, Error **errp)
1060 MemoryRegion *mr = MEMORY_REGION(obj);
1062 return mr->may_overlap;
1065 static void memory_region_get_size(Object *obj, Visitor *v, const char *name,
1066 void *opaque, Error **errp)
1068 MemoryRegion *mr = MEMORY_REGION(obj);
1069 uint64_t value = memory_region_size(mr);
1071 visit_type_uint64(v, name, &value, errp);
1074 static void memory_region_initfn(Object *obj)
1076 MemoryRegion *mr = MEMORY_REGION(obj);
1077 ObjectProperty *op;
1079 mr->ops = &unassigned_mem_ops;
1080 mr->enabled = true;
1081 mr->romd_mode = true;
1082 mr->global_locking = true;
1083 mr->destructor = memory_region_destructor_none;
1084 QTAILQ_INIT(&mr->subregions);
1085 QTAILQ_INIT(&mr->coalesced);
1087 op = object_property_add(OBJECT(mr), "container",
1088 "link<" TYPE_MEMORY_REGION ">",
1089 memory_region_get_container,
1090 NULL, /* memory_region_set_container */
1091 NULL, NULL, &error_abort);
1092 op->resolve = memory_region_resolve_container;
1094 object_property_add(OBJECT(mr), "addr", "uint64",
1095 memory_region_get_addr,
1096 NULL, /* memory_region_set_addr */
1097 NULL, NULL, &error_abort);
1098 object_property_add(OBJECT(mr), "priority", "uint32",
1099 memory_region_get_priority,
1100 NULL, /* memory_region_set_priority */
1101 NULL, NULL, &error_abort);
1102 object_property_add_bool(OBJECT(mr), "may-overlap",
1103 memory_region_get_may_overlap,
1104 NULL, /* memory_region_set_may_overlap */
1105 &error_abort);
1106 object_property_add(OBJECT(mr), "size", "uint64",
1107 memory_region_get_size,
1108 NULL, /* memory_region_set_size, */
1109 NULL, NULL, &error_abort);
1112 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
1113 unsigned size)
1115 #ifdef DEBUG_UNASSIGNED
1116 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
1117 #endif
1118 if (current_cpu != NULL) {
1119 cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
1121 return 0;
1124 static void unassigned_mem_write(void *opaque, hwaddr addr,
1125 uint64_t val, unsigned size)
1127 #ifdef DEBUG_UNASSIGNED
1128 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
1129 #endif
1130 if (current_cpu != NULL) {
1131 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
1135 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
1136 unsigned size, bool is_write)
1138 return false;
1141 const MemoryRegionOps unassigned_mem_ops = {
1142 .valid.accepts = unassigned_mem_accepts,
1143 .endianness = DEVICE_NATIVE_ENDIAN,
1146 bool memory_region_access_valid(MemoryRegion *mr,
1147 hwaddr addr,
1148 unsigned size,
1149 bool is_write)
1151 int access_size_min, access_size_max;
1152 int access_size, i;
1154 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
1155 return false;
1158 if (!mr->ops->valid.accepts) {
1159 return true;
1162 access_size_min = mr->ops->valid.min_access_size;
1163 if (!mr->ops->valid.min_access_size) {
1164 access_size_min = 1;
1167 access_size_max = mr->ops->valid.max_access_size;
1168 if (!mr->ops->valid.max_access_size) {
1169 access_size_max = 4;
1172 access_size = MAX(MIN(size, access_size_max), access_size_min);
1173 for (i = 0; i < size; i += access_size) {
1174 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
1175 is_write)) {
1176 return false;
1180 return true;
1183 static MemTxResult memory_region_dispatch_read1(MemoryRegion *mr,
1184 hwaddr addr,
1185 uint64_t *pval,
1186 unsigned size,
1187 MemTxAttrs attrs)
1189 *pval = 0;
1191 if (mr->ops->read) {
1192 return access_with_adjusted_size(addr, pval, size,
1193 mr->ops->impl.min_access_size,
1194 mr->ops->impl.max_access_size,
1195 memory_region_read_accessor,
1196 mr, attrs);
1197 } else if (mr->ops->read_with_attrs) {
1198 return access_with_adjusted_size(addr, pval, size,
1199 mr->ops->impl.min_access_size,
1200 mr->ops->impl.max_access_size,
1201 memory_region_read_with_attrs_accessor,
1202 mr, attrs);
1203 } else {
1204 return access_with_adjusted_size(addr, pval, size, 1, 4,
1205 memory_region_oldmmio_read_accessor,
1206 mr, attrs);
1210 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1211 hwaddr addr,
1212 uint64_t *pval,
1213 unsigned size,
1214 MemTxAttrs attrs)
1216 MemTxResult r;
1218 if (!memory_region_access_valid(mr, addr, size, false)) {
1219 *pval = unassigned_mem_read(mr, addr, size);
1220 return MEMTX_DECODE_ERROR;
1223 r = memory_region_dispatch_read1(mr, addr, pval, size, attrs);
1224 adjust_endianness(mr, pval, size);
1225 return r;
1228 /* Return true if an eventfd was signalled */
1229 static bool memory_region_dispatch_write_eventfds(MemoryRegion *mr,
1230 hwaddr addr,
1231 uint64_t data,
1232 unsigned size,
1233 MemTxAttrs attrs)
1235 MemoryRegionIoeventfd ioeventfd = {
1236 .addr = addrrange_make(int128_make64(addr), int128_make64(size)),
1237 .data = data,
1239 unsigned i;
1241 for (i = 0; i < mr->ioeventfd_nb; i++) {
1242 ioeventfd.match_data = mr->ioeventfds[i].match_data;
1243 ioeventfd.e = mr->ioeventfds[i].e;
1245 if (memory_region_ioeventfd_equal(ioeventfd, mr->ioeventfds[i])) {
1246 event_notifier_set(ioeventfd.e);
1247 return true;
1251 return false;
1254 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1255 hwaddr addr,
1256 uint64_t data,
1257 unsigned size,
1258 MemTxAttrs attrs)
1260 if (!memory_region_access_valid(mr, addr, size, true)) {
1261 unassigned_mem_write(mr, addr, data, size);
1262 return MEMTX_DECODE_ERROR;
1265 adjust_endianness(mr, &data, size);
1267 if ((!kvm_eventfds_enabled()) &&
1268 memory_region_dispatch_write_eventfds(mr, addr, data, size, attrs)) {
1269 return MEMTX_OK;
1272 if (mr->ops->write) {
1273 return access_with_adjusted_size(addr, &data, size,
1274 mr->ops->impl.min_access_size,
1275 mr->ops->impl.max_access_size,
1276 memory_region_write_accessor, mr,
1277 attrs);
1278 } else if (mr->ops->write_with_attrs) {
1279 return
1280 access_with_adjusted_size(addr, &data, size,
1281 mr->ops->impl.min_access_size,
1282 mr->ops->impl.max_access_size,
1283 memory_region_write_with_attrs_accessor,
1284 mr, attrs);
1285 } else {
1286 return access_with_adjusted_size(addr, &data, size, 1, 4,
1287 memory_region_oldmmio_write_accessor,
1288 mr, attrs);
1292 void memory_region_init_io(MemoryRegion *mr,
1293 Object *owner,
1294 const MemoryRegionOps *ops,
1295 void *opaque,
1296 const char *name,
1297 uint64_t size)
1299 memory_region_init(mr, owner, name, size);
1300 mr->ops = ops ? ops : &unassigned_mem_ops;
1301 mr->opaque = opaque;
1302 mr->terminates = true;
1305 void memory_region_init_ram(MemoryRegion *mr,
1306 Object *owner,
1307 const char *name,
1308 uint64_t size,
1309 Error **errp)
1311 memory_region_init(mr, owner, name, size);
1312 mr->ram = true;
1313 mr->terminates = true;
1314 mr->destructor = memory_region_destructor_ram;
1315 mr->ram_block = qemu_ram_alloc(size, mr, errp);
1316 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1319 void memory_region_init_resizeable_ram(MemoryRegion *mr,
1320 Object *owner,
1321 const char *name,
1322 uint64_t size,
1323 uint64_t max_size,
1324 void (*resized)(const char*,
1325 uint64_t length,
1326 void *host),
1327 Error **errp)
1329 memory_region_init(mr, owner, name, size);
1330 mr->ram = true;
1331 mr->terminates = true;
1332 mr->destructor = memory_region_destructor_ram;
1333 mr->ram_block = qemu_ram_alloc_resizeable(size, max_size, resized,
1334 mr, errp);
1335 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1338 #ifdef __linux__
1339 void memory_region_init_ram_from_file(MemoryRegion *mr,
1340 struct Object *owner,
1341 const char *name,
1342 uint64_t size,
1343 bool share,
1344 const char *path,
1345 Error **errp)
1347 memory_region_init(mr, owner, name, size);
1348 mr->ram = true;
1349 mr->terminates = true;
1350 mr->destructor = memory_region_destructor_ram;
1351 mr->ram_block = qemu_ram_alloc_from_file(size, mr, share, path, errp);
1352 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1354 #endif
1356 void memory_region_init_ram_ptr(MemoryRegion *mr,
1357 Object *owner,
1358 const char *name,
1359 uint64_t size,
1360 void *ptr)
1362 memory_region_init(mr, owner, name, size);
1363 mr->ram = true;
1364 mr->terminates = true;
1365 mr->destructor = memory_region_destructor_ram;
1366 mr->dirty_log_mask = tcg_enabled() ? (1 << DIRTY_MEMORY_CODE) : 0;
1368 /* qemu_ram_alloc_from_ptr cannot fail with ptr != NULL. */
1369 assert(ptr != NULL);
1370 mr->ram_block = qemu_ram_alloc_from_ptr(size, ptr, mr, &error_fatal);
1373 void memory_region_set_skip_dump(MemoryRegion *mr)
1375 mr->skip_dump = true;
1378 void memory_region_init_alias(MemoryRegion *mr,
1379 Object *owner,
1380 const char *name,
1381 MemoryRegion *orig,
1382 hwaddr offset,
1383 uint64_t size)
1385 memory_region_init(mr, owner, name, size);
1386 mr->alias = orig;
1387 mr->alias_offset = offset;
1390 void memory_region_init_rom_device(MemoryRegion *mr,
1391 Object *owner,
1392 const MemoryRegionOps *ops,
1393 void *opaque,
1394 const char *name,
1395 uint64_t size,
1396 Error **errp)
1398 memory_region_init(mr, owner, name, size);
1399 mr->ops = ops;
1400 mr->opaque = opaque;
1401 mr->terminates = true;
1402 mr->rom_device = true;
1403 mr->destructor = memory_region_destructor_rom_device;
1404 mr->ram_block = qemu_ram_alloc(size, mr, errp);
1407 void memory_region_init_iommu(MemoryRegion *mr,
1408 Object *owner,
1409 const MemoryRegionIOMMUOps *ops,
1410 const char *name,
1411 uint64_t size)
1413 memory_region_init(mr, owner, name, size);
1414 mr->iommu_ops = ops,
1415 mr->terminates = true; /* then re-forwards */
1416 notifier_list_init(&mr->iommu_notify);
1419 static void memory_region_finalize(Object *obj)
1421 MemoryRegion *mr = MEMORY_REGION(obj);
1423 assert(!mr->container);
1425 /* We know the region is not visible in any address space (it
1426 * does not have a container and cannot be a root either because
1427 * it has no references, so we can blindly clear mr->enabled.
1428 * memory_region_set_enabled instead could trigger a transaction
1429 * and cause an infinite loop.
1431 mr->enabled = false;
1432 memory_region_transaction_begin();
1433 while (!QTAILQ_EMPTY(&mr->subregions)) {
1434 MemoryRegion *subregion = QTAILQ_FIRST(&mr->subregions);
1435 memory_region_del_subregion(mr, subregion);
1437 memory_region_transaction_commit();
1439 mr->destructor(mr);
1440 memory_region_clear_coalescing(mr);
1441 g_free((char *)mr->name);
1442 g_free(mr->ioeventfds);
1445 Object *memory_region_owner(MemoryRegion *mr)
1447 Object *obj = OBJECT(mr);
1448 return obj->parent;
1451 void memory_region_ref(MemoryRegion *mr)
1453 /* MMIO callbacks most likely will access data that belongs
1454 * to the owner, hence the need to ref/unref the owner whenever
1455 * the memory region is in use.
1457 * The memory region is a child of its owner. As long as the
1458 * owner doesn't call unparent itself on the memory region,
1459 * ref-ing the owner will also keep the memory region alive.
1460 * Memory regions without an owner are supposed to never go away;
1461 * we do not ref/unref them because it slows down DMA sensibly.
1463 if (mr && mr->owner) {
1464 object_ref(mr->owner);
1468 void memory_region_unref(MemoryRegion *mr)
1470 if (mr && mr->owner) {
1471 object_unref(mr->owner);
1475 uint64_t memory_region_size(MemoryRegion *mr)
1477 if (int128_eq(mr->size, int128_2_64())) {
1478 return UINT64_MAX;
1480 return int128_get64(mr->size);
1483 const char *memory_region_name(const MemoryRegion *mr)
1485 if (!mr->name) {
1486 ((MemoryRegion *)mr)->name =
1487 object_get_canonical_path_component(OBJECT(mr));
1489 return mr->name;
1492 bool memory_region_is_skip_dump(MemoryRegion *mr)
1494 return mr->skip_dump;
1497 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr)
1499 uint8_t mask = mr->dirty_log_mask;
1500 if (global_dirty_log) {
1501 mask |= (1 << DIRTY_MEMORY_MIGRATION);
1503 return mask;
1506 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client)
1508 return memory_region_get_dirty_log_mask(mr) & (1 << client);
1511 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n)
1513 notifier_list_add(&mr->iommu_notify, n);
1516 void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n,
1517 hwaddr granularity, bool is_write)
1519 hwaddr addr;
1520 IOMMUTLBEntry iotlb;
1522 for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
1523 iotlb = mr->iommu_ops->translate(mr, addr, is_write);
1524 if (iotlb.perm != IOMMU_NONE) {
1525 n->notify(n, &iotlb);
1528 /* if (2^64 - MR size) < granularity, it's possible to get an
1529 * infinite loop here. This should catch such a wraparound */
1530 if ((addr + granularity) < addr) {
1531 break;
1536 void memory_region_unregister_iommu_notifier(Notifier *n)
1538 notifier_remove(n);
1541 void memory_region_notify_iommu(MemoryRegion *mr,
1542 IOMMUTLBEntry entry)
1544 assert(memory_region_is_iommu(mr));
1545 notifier_list_notify(&mr->iommu_notify, &entry);
1548 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1550 uint8_t mask = 1 << client;
1551 uint8_t old_logging;
1553 assert(client == DIRTY_MEMORY_VGA);
1554 old_logging = mr->vga_logging_count;
1555 mr->vga_logging_count += log ? 1 : -1;
1556 if (!!old_logging == !!mr->vga_logging_count) {
1557 return;
1560 memory_region_transaction_begin();
1561 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1562 memory_region_update_pending |= mr->enabled;
1563 memory_region_transaction_commit();
1566 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1567 hwaddr size, unsigned client)
1569 assert(mr->ram_block);
1570 return cpu_physical_memory_get_dirty(memory_region_get_ram_addr(mr) + addr,
1571 size, client);
1574 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1575 hwaddr size)
1577 assert(mr->ram_block);
1578 cpu_physical_memory_set_dirty_range(memory_region_get_ram_addr(mr) + addr,
1579 size,
1580 memory_region_get_dirty_log_mask(mr));
1583 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
1584 hwaddr size, unsigned client)
1586 assert(mr->ram_block);
1587 return cpu_physical_memory_test_and_clear_dirty(
1588 memory_region_get_ram_addr(mr) + addr, size, client);
1592 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1594 AddressSpace *as;
1595 FlatRange *fr;
1597 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1598 FlatView *view = address_space_get_flatview(as);
1599 FOR_EACH_FLAT_RANGE(fr, view) {
1600 if (fr->mr == mr) {
1601 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1604 flatview_unref(view);
1608 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1610 if (mr->readonly != readonly) {
1611 memory_region_transaction_begin();
1612 mr->readonly = readonly;
1613 memory_region_update_pending |= mr->enabled;
1614 memory_region_transaction_commit();
1618 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
1620 if (mr->romd_mode != romd_mode) {
1621 memory_region_transaction_begin();
1622 mr->romd_mode = romd_mode;
1623 memory_region_update_pending |= mr->enabled;
1624 memory_region_transaction_commit();
1628 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1629 hwaddr size, unsigned client)
1631 assert(mr->ram_block);
1632 cpu_physical_memory_test_and_clear_dirty(
1633 memory_region_get_ram_addr(mr) + addr, size, client);
1636 int memory_region_get_fd(MemoryRegion *mr)
1638 if (mr->alias) {
1639 return memory_region_get_fd(mr->alias);
1642 assert(mr->ram_block);
1644 return qemu_get_ram_fd(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK);
1647 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1649 void *ptr;
1650 uint64_t offset = 0;
1652 rcu_read_lock();
1653 while (mr->alias) {
1654 offset += mr->alias_offset;
1655 mr = mr->alias;
1657 assert(mr->ram_block);
1658 ptr = qemu_get_ram_ptr(mr->ram_block,
1659 memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK);
1660 rcu_read_unlock();
1662 return ptr + offset;
1665 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1667 return mr->ram_block ? mr->ram_block->offset : RAM_ADDR_INVALID;
1670 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, Error **errp)
1672 assert(mr->ram_block);
1674 qemu_ram_resize(memory_region_get_ram_addr(mr), newsize, errp);
1677 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
1679 FlatView *view;
1680 FlatRange *fr;
1681 CoalescedMemoryRange *cmr;
1682 AddrRange tmp;
1683 MemoryRegionSection section;
1685 view = address_space_get_flatview(as);
1686 FOR_EACH_FLAT_RANGE(fr, view) {
1687 if (fr->mr == mr) {
1688 section = (MemoryRegionSection) {
1689 .address_space = as,
1690 .offset_within_address_space = int128_get64(fr->addr.start),
1691 .size = fr->addr.size,
1694 MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, &section,
1695 int128_get64(fr->addr.start),
1696 int128_get64(fr->addr.size));
1697 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1698 tmp = addrrange_shift(cmr->addr,
1699 int128_sub(fr->addr.start,
1700 int128_make64(fr->offset_in_region)));
1701 if (!addrrange_intersects(tmp, fr->addr)) {
1702 continue;
1704 tmp = addrrange_intersection(tmp, fr->addr);
1705 MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, &section,
1706 int128_get64(tmp.start),
1707 int128_get64(tmp.size));
1711 flatview_unref(view);
1714 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1716 AddressSpace *as;
1718 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1719 memory_region_update_coalesced_range_as(mr, as);
1723 void memory_region_set_coalescing(MemoryRegion *mr)
1725 memory_region_clear_coalescing(mr);
1726 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1729 void memory_region_add_coalescing(MemoryRegion *mr,
1730 hwaddr offset,
1731 uint64_t size)
1733 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1735 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1736 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1737 memory_region_update_coalesced_range(mr);
1738 memory_region_set_flush_coalesced(mr);
1741 void memory_region_clear_coalescing(MemoryRegion *mr)
1743 CoalescedMemoryRange *cmr;
1744 bool updated = false;
1746 qemu_flush_coalesced_mmio_buffer();
1747 mr->flush_coalesced_mmio = false;
1749 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1750 cmr = QTAILQ_FIRST(&mr->coalesced);
1751 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1752 g_free(cmr);
1753 updated = true;
1756 if (updated) {
1757 memory_region_update_coalesced_range(mr);
1761 void memory_region_set_flush_coalesced(MemoryRegion *mr)
1763 mr->flush_coalesced_mmio = true;
1766 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
1768 qemu_flush_coalesced_mmio_buffer();
1769 if (QTAILQ_EMPTY(&mr->coalesced)) {
1770 mr->flush_coalesced_mmio = false;
1774 void memory_region_set_global_locking(MemoryRegion *mr)
1776 mr->global_locking = true;
1779 void memory_region_clear_global_locking(MemoryRegion *mr)
1781 mr->global_locking = false;
1784 static bool userspace_eventfd_warning;
1786 void memory_region_add_eventfd(MemoryRegion *mr,
1787 hwaddr addr,
1788 unsigned size,
1789 bool match_data,
1790 uint64_t data,
1791 EventNotifier *e)
1793 MemoryRegionIoeventfd mrfd = {
1794 .addr.start = int128_make64(addr),
1795 .addr.size = int128_make64(size),
1796 .match_data = match_data,
1797 .data = data,
1798 .e = e,
1800 unsigned i;
1802 if (kvm_enabled() && (!(kvm_eventfds_enabled() ||
1803 userspace_eventfd_warning))) {
1804 userspace_eventfd_warning = true;
1805 error_report("Using eventfd without MMIO binding in KVM. "
1806 "Suboptimal performance expected");
1809 if (size) {
1810 adjust_endianness(mr, &mrfd.data, size);
1812 memory_region_transaction_begin();
1813 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1814 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1815 break;
1818 ++mr->ioeventfd_nb;
1819 mr->ioeventfds = g_realloc(mr->ioeventfds,
1820 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1821 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1822 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1823 mr->ioeventfds[i] = mrfd;
1824 ioeventfd_update_pending |= mr->enabled;
1825 memory_region_transaction_commit();
1828 void memory_region_del_eventfd(MemoryRegion *mr,
1829 hwaddr addr,
1830 unsigned size,
1831 bool match_data,
1832 uint64_t data,
1833 EventNotifier *e)
1835 MemoryRegionIoeventfd mrfd = {
1836 .addr.start = int128_make64(addr),
1837 .addr.size = int128_make64(size),
1838 .match_data = match_data,
1839 .data = data,
1840 .e = e,
1842 unsigned i;
1844 if (size) {
1845 adjust_endianness(mr, &mrfd.data, size);
1847 memory_region_transaction_begin();
1848 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1849 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1850 break;
1853 assert(i != mr->ioeventfd_nb);
1854 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1855 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1856 --mr->ioeventfd_nb;
1857 mr->ioeventfds = g_realloc(mr->ioeventfds,
1858 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1859 ioeventfd_update_pending |= mr->enabled;
1860 memory_region_transaction_commit();
1863 static void memory_region_update_container_subregions(MemoryRegion *subregion)
1865 hwaddr offset = subregion->addr;
1866 MemoryRegion *mr = subregion->container;
1867 MemoryRegion *other;
1869 memory_region_transaction_begin();
1871 memory_region_ref(subregion);
1872 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1873 if (subregion->may_overlap || other->may_overlap) {
1874 continue;
1876 if (int128_ge(int128_make64(offset),
1877 int128_add(int128_make64(other->addr), other->size))
1878 || int128_le(int128_add(int128_make64(offset), subregion->size),
1879 int128_make64(other->addr))) {
1880 continue;
1882 #if 0
1883 printf("warning: subregion collision %llx/%llx (%s) "
1884 "vs %llx/%llx (%s)\n",
1885 (unsigned long long)offset,
1886 (unsigned long long)int128_get64(subregion->size),
1887 subregion->name,
1888 (unsigned long long)other->addr,
1889 (unsigned long long)int128_get64(other->size),
1890 other->name);
1891 #endif
1893 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1894 if (subregion->priority >= other->priority) {
1895 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1896 goto done;
1899 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1900 done:
1901 memory_region_update_pending |= mr->enabled && subregion->enabled;
1902 memory_region_transaction_commit();
1905 static void memory_region_add_subregion_common(MemoryRegion *mr,
1906 hwaddr offset,
1907 MemoryRegion *subregion)
1909 assert(!subregion->container);
1910 subregion->container = mr;
1911 subregion->addr = offset;
1912 memory_region_update_container_subregions(subregion);
1915 void memory_region_add_subregion(MemoryRegion *mr,
1916 hwaddr offset,
1917 MemoryRegion *subregion)
1919 subregion->may_overlap = false;
1920 subregion->priority = 0;
1921 memory_region_add_subregion_common(mr, offset, subregion);
1924 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1925 hwaddr offset,
1926 MemoryRegion *subregion,
1927 int priority)
1929 subregion->may_overlap = true;
1930 subregion->priority = priority;
1931 memory_region_add_subregion_common(mr, offset, subregion);
1934 void memory_region_del_subregion(MemoryRegion *mr,
1935 MemoryRegion *subregion)
1937 memory_region_transaction_begin();
1938 assert(subregion->container == mr);
1939 subregion->container = NULL;
1940 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1941 memory_region_unref(subregion);
1942 memory_region_update_pending |= mr->enabled && subregion->enabled;
1943 memory_region_transaction_commit();
1946 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1948 if (enabled == mr->enabled) {
1949 return;
1951 memory_region_transaction_begin();
1952 mr->enabled = enabled;
1953 memory_region_update_pending = true;
1954 memory_region_transaction_commit();
1957 void memory_region_set_size(MemoryRegion *mr, uint64_t size)
1959 Int128 s = int128_make64(size);
1961 if (size == UINT64_MAX) {
1962 s = int128_2_64();
1964 if (int128_eq(s, mr->size)) {
1965 return;
1967 memory_region_transaction_begin();
1968 mr->size = s;
1969 memory_region_update_pending = true;
1970 memory_region_transaction_commit();
1973 static void memory_region_readd_subregion(MemoryRegion *mr)
1975 MemoryRegion *container = mr->container;
1977 if (container) {
1978 memory_region_transaction_begin();
1979 memory_region_ref(mr);
1980 memory_region_del_subregion(container, mr);
1981 mr->container = container;
1982 memory_region_update_container_subregions(mr);
1983 memory_region_unref(mr);
1984 memory_region_transaction_commit();
1988 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
1990 if (addr != mr->addr) {
1991 mr->addr = addr;
1992 memory_region_readd_subregion(mr);
1996 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
1998 assert(mr->alias);
2000 if (offset == mr->alias_offset) {
2001 return;
2004 memory_region_transaction_begin();
2005 mr->alias_offset = offset;
2006 memory_region_update_pending |= mr->enabled;
2007 memory_region_transaction_commit();
2010 uint64_t memory_region_get_alignment(const MemoryRegion *mr)
2012 return mr->align;
2015 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
2017 const AddrRange *addr = addr_;
2018 const FlatRange *fr = fr_;
2020 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
2021 return -1;
2022 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
2023 return 1;
2025 return 0;
2028 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
2030 return bsearch(&addr, view->ranges, view->nr,
2031 sizeof(FlatRange), cmp_flatrange_addr);
2034 bool memory_region_is_mapped(MemoryRegion *mr)
2036 return mr->container ? true : false;
2039 /* Same as memory_region_find, but it does not add a reference to the
2040 * returned region. It must be called from an RCU critical section.
2042 static MemoryRegionSection memory_region_find_rcu(MemoryRegion *mr,
2043 hwaddr addr, uint64_t size)
2045 MemoryRegionSection ret = { .mr = NULL };
2046 MemoryRegion *root;
2047 AddressSpace *as;
2048 AddrRange range;
2049 FlatView *view;
2050 FlatRange *fr;
2052 addr += mr->addr;
2053 for (root = mr; root->container; ) {
2054 root = root->container;
2055 addr += root->addr;
2058 as = memory_region_to_address_space(root);
2059 if (!as) {
2060 return ret;
2062 range = addrrange_make(int128_make64(addr), int128_make64(size));
2064 view = atomic_rcu_read(&as->current_map);
2065 fr = flatview_lookup(view, range);
2066 if (!fr) {
2067 return ret;
2070 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
2071 --fr;
2074 ret.mr = fr->mr;
2075 ret.address_space = as;
2076 range = addrrange_intersection(range, fr->addr);
2077 ret.offset_within_region = fr->offset_in_region;
2078 ret.offset_within_region += int128_get64(int128_sub(range.start,
2079 fr->addr.start));
2080 ret.size = range.size;
2081 ret.offset_within_address_space = int128_get64(range.start);
2082 ret.readonly = fr->readonly;
2083 return ret;
2086 MemoryRegionSection memory_region_find(MemoryRegion *mr,
2087 hwaddr addr, uint64_t size)
2089 MemoryRegionSection ret;
2090 rcu_read_lock();
2091 ret = memory_region_find_rcu(mr, addr, size);
2092 if (ret.mr) {
2093 memory_region_ref(ret.mr);
2095 rcu_read_unlock();
2096 return ret;
2099 bool memory_region_present(MemoryRegion *container, hwaddr addr)
2101 MemoryRegion *mr;
2103 rcu_read_lock();
2104 mr = memory_region_find_rcu(container, addr, 1).mr;
2105 rcu_read_unlock();
2106 return mr && mr != container;
2109 void address_space_sync_dirty_bitmap(AddressSpace *as)
2111 FlatView *view;
2112 FlatRange *fr;
2114 view = address_space_get_flatview(as);
2115 FOR_EACH_FLAT_RANGE(fr, view) {
2116 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
2118 flatview_unref(view);
2121 void memory_global_dirty_log_start(void)
2123 global_dirty_log = true;
2125 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
2127 /* Refresh DIRTY_LOG_MIGRATION bit. */
2128 memory_region_transaction_begin();
2129 memory_region_update_pending = true;
2130 memory_region_transaction_commit();
2133 void memory_global_dirty_log_stop(void)
2135 global_dirty_log = false;
2137 /* Refresh DIRTY_LOG_MIGRATION bit. */
2138 memory_region_transaction_begin();
2139 memory_region_update_pending = true;
2140 memory_region_transaction_commit();
2142 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
2145 static void listener_add_address_space(MemoryListener *listener,
2146 AddressSpace *as)
2148 FlatView *view;
2149 FlatRange *fr;
2151 if (listener->address_space_filter
2152 && listener->address_space_filter != as) {
2153 return;
2156 if (listener->begin) {
2157 listener->begin(listener);
2159 if (global_dirty_log) {
2160 if (listener->log_global_start) {
2161 listener->log_global_start(listener);
2165 view = address_space_get_flatview(as);
2166 FOR_EACH_FLAT_RANGE(fr, view) {
2167 MemoryRegionSection section = {
2168 .mr = fr->mr,
2169 .address_space = as,
2170 .offset_within_region = fr->offset_in_region,
2171 .size = fr->addr.size,
2172 .offset_within_address_space = int128_get64(fr->addr.start),
2173 .readonly = fr->readonly,
2175 if (fr->dirty_log_mask && listener->log_start) {
2176 listener->log_start(listener, &section, 0, fr->dirty_log_mask);
2178 if (listener->region_add) {
2179 listener->region_add(listener, &section);
2182 if (listener->commit) {
2183 listener->commit(listener);
2185 flatview_unref(view);
2188 void memory_listener_register(MemoryListener *listener, AddressSpace *filter)
2190 MemoryListener *other = NULL;
2191 AddressSpace *as;
2193 listener->address_space_filter = filter;
2194 if (QTAILQ_EMPTY(&memory_listeners)
2195 || listener->priority >= QTAILQ_LAST(&memory_listeners,
2196 memory_listeners)->priority) {
2197 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
2198 } else {
2199 QTAILQ_FOREACH(other, &memory_listeners, link) {
2200 if (listener->priority < other->priority) {
2201 break;
2204 QTAILQ_INSERT_BEFORE(other, listener, link);
2207 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2208 listener_add_address_space(listener, as);
2212 void memory_listener_unregister(MemoryListener *listener)
2214 QTAILQ_REMOVE(&memory_listeners, listener, link);
2217 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
2219 memory_region_ref(root);
2220 memory_region_transaction_begin();
2221 as->ref_count = 1;
2222 as->root = root;
2223 as->malloced = false;
2224 as->current_map = g_new(FlatView, 1);
2225 flatview_init(as->current_map);
2226 as->ioeventfd_nb = 0;
2227 as->ioeventfds = NULL;
2228 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
2229 as->name = g_strdup(name ? name : "anonymous");
2230 address_space_init_dispatch(as);
2231 memory_region_update_pending |= root->enabled;
2232 memory_region_transaction_commit();
2235 static void do_address_space_destroy(AddressSpace *as)
2237 MemoryListener *listener;
2238 bool do_free = as->malloced;
2240 address_space_destroy_dispatch(as);
2242 QTAILQ_FOREACH(listener, &memory_listeners, link) {
2243 assert(listener->address_space_filter != as);
2246 flatview_unref(as->current_map);
2247 g_free(as->name);
2248 g_free(as->ioeventfds);
2249 memory_region_unref(as->root);
2250 if (do_free) {
2251 g_free(as);
2255 AddressSpace *address_space_init_shareable(MemoryRegion *root, const char *name)
2257 AddressSpace *as;
2259 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2260 if (root == as->root && as->malloced) {
2261 as->ref_count++;
2262 return as;
2266 as = g_malloc0(sizeof *as);
2267 address_space_init(as, root, name);
2268 as->malloced = true;
2269 return as;
2272 void address_space_destroy(AddressSpace *as)
2274 MemoryRegion *root = as->root;
2276 as->ref_count--;
2277 if (as->ref_count) {
2278 return;
2280 /* Flush out anything from MemoryListeners listening in on this */
2281 memory_region_transaction_begin();
2282 as->root = NULL;
2283 memory_region_transaction_commit();
2284 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
2285 address_space_unregister(as);
2287 /* At this point, as->dispatch and as->current_map are dummy
2288 * entries that the guest should never use. Wait for the old
2289 * values to expire before freeing the data.
2291 as->root = root;
2292 call_rcu(as, do_address_space_destroy, rcu);
2295 typedef struct MemoryRegionList MemoryRegionList;
2297 struct MemoryRegionList {
2298 const MemoryRegion *mr;
2299 QTAILQ_ENTRY(MemoryRegionList) queue;
2302 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
2304 static void mtree_print_mr(fprintf_function mon_printf, void *f,
2305 const MemoryRegion *mr, unsigned int level,
2306 hwaddr base,
2307 MemoryRegionListHead *alias_print_queue)
2309 MemoryRegionList *new_ml, *ml, *next_ml;
2310 MemoryRegionListHead submr_print_queue;
2311 const MemoryRegion *submr;
2312 unsigned int i;
2314 if (!mr) {
2315 return;
2318 for (i = 0; i < level; i++) {
2319 mon_printf(f, " ");
2322 if (mr->alias) {
2323 MemoryRegionList *ml;
2324 bool found = false;
2326 /* check if the alias is already in the queue */
2327 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
2328 if (ml->mr == mr->alias) {
2329 found = true;
2333 if (!found) {
2334 ml = g_new(MemoryRegionList, 1);
2335 ml->mr = mr->alias;
2336 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
2338 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
2339 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
2340 "-" TARGET_FMT_plx "%s\n",
2341 base + mr->addr,
2342 base + mr->addr
2343 + (int128_nz(mr->size) ?
2344 (hwaddr)int128_get64(int128_sub(mr->size,
2345 int128_one())) : 0),
2346 mr->priority,
2347 mr->romd_mode ? 'R' : '-',
2348 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
2349 : '-',
2350 memory_region_name(mr),
2351 memory_region_name(mr->alias),
2352 mr->alias_offset,
2353 mr->alias_offset
2354 + (int128_nz(mr->size) ?
2355 (hwaddr)int128_get64(int128_sub(mr->size,
2356 int128_one())) : 0),
2357 mr->enabled ? "" : " [disabled]");
2358 } else {
2359 mon_printf(f,
2360 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %c%c): %s%s\n",
2361 base + mr->addr,
2362 base + mr->addr
2363 + (int128_nz(mr->size) ?
2364 (hwaddr)int128_get64(int128_sub(mr->size,
2365 int128_one())) : 0),
2366 mr->priority,
2367 mr->romd_mode ? 'R' : '-',
2368 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
2369 : '-',
2370 memory_region_name(mr),
2371 mr->enabled ? "" : " [disabled]");
2374 QTAILQ_INIT(&submr_print_queue);
2376 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
2377 new_ml = g_new(MemoryRegionList, 1);
2378 new_ml->mr = submr;
2379 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
2380 if (new_ml->mr->addr < ml->mr->addr ||
2381 (new_ml->mr->addr == ml->mr->addr &&
2382 new_ml->mr->priority > ml->mr->priority)) {
2383 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
2384 new_ml = NULL;
2385 break;
2388 if (new_ml) {
2389 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
2393 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
2394 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
2395 alias_print_queue);
2398 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
2399 g_free(ml);
2403 void mtree_info(fprintf_function mon_printf, void *f)
2405 MemoryRegionListHead ml_head;
2406 MemoryRegionList *ml, *ml2;
2407 AddressSpace *as;
2409 QTAILQ_INIT(&ml_head);
2411 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
2412 mon_printf(f, "address-space: %s\n", as->name);
2413 mtree_print_mr(mon_printf, f, as->root, 1, 0, &ml_head);
2414 mon_printf(f, "\n");
2417 /* print aliased regions */
2418 QTAILQ_FOREACH(ml, &ml_head, queue) {
2419 mon_printf(f, "memory-region: %s\n", memory_region_name(ml->mr));
2420 mtree_print_mr(mon_printf, f, ml->mr, 1, 0, &ml_head);
2421 mon_printf(f, "\n");
2424 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
2425 g_free(ml);
2429 static const TypeInfo memory_region_info = {
2430 .parent = TYPE_OBJECT,
2431 .name = TYPE_MEMORY_REGION,
2432 .instance_size = sizeof(MemoryRegion),
2433 .instance_init = memory_region_initfn,
2434 .instance_finalize = memory_region_finalize,
2437 static void memory_register_types(void)
2439 type_register_static(&memory_region_info);
2442 type_init(memory_register_types)