hostmem: allow preallocation of any memory region
[qemu.git] / memory.c
1 /*
2 * Physical memory management
3 *
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
5 *
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
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.
14 */
15
16 #include "exec/memory.h"
17 #include "exec/address-spaces.h"
18 #include "exec/ioport.h"
19 #include "qemu/bitops.h"
20 #include "qom/object.h"
21 #include "trace.h"
22 #include <assert.h>
23
24 #include "exec/memory-internal.h"
25 #include "exec/ram_addr.h"
26 #include "sysemu/sysemu.h"
27
28 //#define DEBUG_UNASSIGNED
29
30 static unsigned memory_region_transaction_depth;
31 static bool memory_region_update_pending;
32 static bool ioeventfd_update_pending;
33 static bool global_dirty_log = false;
34
35 /* flat_view_mutex is taken around reading as->current_map; the critical
36 * section is extremely short, so I'm using a single mutex for every AS.
37 * We could also RCU for the read-side.
38 *
39 * The BQL is taken around transaction commits, hence both locks are taken
40 * while writing to as->current_map (with the BQL taken outside).
41 */
42 static QemuMutex flat_view_mutex;
43
44 static QTAILQ_HEAD(memory_listeners, MemoryListener) memory_listeners
45 = QTAILQ_HEAD_INITIALIZER(memory_listeners);
46
47 static QTAILQ_HEAD(, AddressSpace) address_spaces
48 = QTAILQ_HEAD_INITIALIZER(address_spaces);
49
50 static void memory_init(void)
51 {
52 qemu_mutex_init(&flat_view_mutex);
53 }
54
55 typedef struct AddrRange AddrRange;
56
57 /*
58 * Note using signed integers limits us to physical addresses at most
59 * 63 bits wide. They are needed for negative offsetting in aliases
60 * (large MemoryRegion::alias_offset).
61 */
62 struct AddrRange {
63 Int128 start;
64 Int128 size;
65 };
66
67 static AddrRange addrrange_make(Int128 start, Int128 size)
68 {
69 return (AddrRange) { start, size };
70 }
71
72 static bool addrrange_equal(AddrRange r1, AddrRange r2)
73 {
74 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
75 }
76
77 static Int128 addrrange_end(AddrRange r)
78 {
79 return int128_add(r.start, r.size);
80 }
81
82 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
83 {
84 int128_addto(&range.start, delta);
85 return range;
86 }
87
88 static bool addrrange_contains(AddrRange range, Int128 addr)
89 {
90 return int128_ge(addr, range.start)
91 && int128_lt(addr, addrrange_end(range));
92 }
93
94 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
95 {
96 return addrrange_contains(r1, r2.start)
97 || addrrange_contains(r2, r1.start);
98 }
99
100 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
101 {
102 Int128 start = int128_max(r1.start, r2.start);
103 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
104 return addrrange_make(start, int128_sub(end, start));
105 }
106
107 enum ListenerDirection { Forward, Reverse };
108
109 static bool memory_listener_match(MemoryListener *listener,
110 MemoryRegionSection *section)
111 {
112 return !listener->address_space_filter
113 || listener->address_space_filter == section->address_space;
114 }
115
116 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
117 do { \
118 MemoryListener *_listener; \
119 \
120 switch (_direction) { \
121 case Forward: \
122 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
123 if (_listener->_callback) { \
124 _listener->_callback(_listener, ##_args); \
125 } \
126 } \
127 break; \
128 case Reverse: \
129 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
130 memory_listeners, link) { \
131 if (_listener->_callback) { \
132 _listener->_callback(_listener, ##_args); \
133 } \
134 } \
135 break; \
136 default: \
137 abort(); \
138 } \
139 } while (0)
140
141 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
142 do { \
143 MemoryListener *_listener; \
144 \
145 switch (_direction) { \
146 case Forward: \
147 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
148 if (_listener->_callback \
149 && memory_listener_match(_listener, _section)) { \
150 _listener->_callback(_listener, _section, ##_args); \
151 } \
152 } \
153 break; \
154 case Reverse: \
155 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
156 memory_listeners, link) { \
157 if (_listener->_callback \
158 && memory_listener_match(_listener, _section)) { \
159 _listener->_callback(_listener, _section, ##_args); \
160 } \
161 } \
162 break; \
163 default: \
164 abort(); \
165 } \
166 } while (0)
167
168 /* No need to ref/unref .mr, the FlatRange keeps it alive. */
169 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
170 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
171 .mr = (fr)->mr, \
172 .address_space = (as), \
173 .offset_within_region = (fr)->offset_in_region, \
174 .size = (fr)->addr.size, \
175 .offset_within_address_space = int128_get64((fr)->addr.start), \
176 .readonly = (fr)->readonly, \
177 }))
178
179 struct CoalescedMemoryRange {
180 AddrRange addr;
181 QTAILQ_ENTRY(CoalescedMemoryRange) link;
182 };
183
184 struct MemoryRegionIoeventfd {
185 AddrRange addr;
186 bool match_data;
187 uint64_t data;
188 EventNotifier *e;
189 };
190
191 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
192 MemoryRegionIoeventfd b)
193 {
194 if (int128_lt(a.addr.start, b.addr.start)) {
195 return true;
196 } else if (int128_gt(a.addr.start, b.addr.start)) {
197 return false;
198 } else if (int128_lt(a.addr.size, b.addr.size)) {
199 return true;
200 } else if (int128_gt(a.addr.size, b.addr.size)) {
201 return false;
202 } else if (a.match_data < b.match_data) {
203 return true;
204 } else if (a.match_data > b.match_data) {
205 return false;
206 } else if (a.match_data) {
207 if (a.data < b.data) {
208 return true;
209 } else if (a.data > b.data) {
210 return false;
211 }
212 }
213 if (a.e < b.e) {
214 return true;
215 } else if (a.e > b.e) {
216 return false;
217 }
218 return false;
219 }
220
221 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
222 MemoryRegionIoeventfd b)
223 {
224 return !memory_region_ioeventfd_before(a, b)
225 && !memory_region_ioeventfd_before(b, a);
226 }
227
228 typedef struct FlatRange FlatRange;
229 typedef struct FlatView FlatView;
230
231 /* Range of memory in the global map. Addresses are absolute. */
232 struct FlatRange {
233 MemoryRegion *mr;
234 hwaddr offset_in_region;
235 AddrRange addr;
236 uint8_t dirty_log_mask;
237 bool romd_mode;
238 bool readonly;
239 };
240
241 /* Flattened global view of current active memory hierarchy. Kept in sorted
242 * order.
243 */
244 struct FlatView {
245 unsigned ref;
246 FlatRange *ranges;
247 unsigned nr;
248 unsigned nr_allocated;
249 };
250
251 typedef struct AddressSpaceOps AddressSpaceOps;
252
253 #define FOR_EACH_FLAT_RANGE(var, view) \
254 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
255
256 static bool flatrange_equal(FlatRange *a, FlatRange *b)
257 {
258 return a->mr == b->mr
259 && addrrange_equal(a->addr, b->addr)
260 && a->offset_in_region == b->offset_in_region
261 && a->romd_mode == b->romd_mode
262 && a->readonly == b->readonly;
263 }
264
265 static void flatview_init(FlatView *view)
266 {
267 view->ref = 1;
268 view->ranges = NULL;
269 view->nr = 0;
270 view->nr_allocated = 0;
271 }
272
273 /* Insert a range into a given position. Caller is responsible for maintaining
274 * sorting order.
275 */
276 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
277 {
278 if (view->nr == view->nr_allocated) {
279 view->nr_allocated = MAX(2 * view->nr, 10);
280 view->ranges = g_realloc(view->ranges,
281 view->nr_allocated * sizeof(*view->ranges));
282 }
283 memmove(view->ranges + pos + 1, view->ranges + pos,
284 (view->nr - pos) * sizeof(FlatRange));
285 view->ranges[pos] = *range;
286 memory_region_ref(range->mr);
287 ++view->nr;
288 }
289
290 static void flatview_destroy(FlatView *view)
291 {
292 int i;
293
294 for (i = 0; i < view->nr; i++) {
295 memory_region_unref(view->ranges[i].mr);
296 }
297 g_free(view->ranges);
298 g_free(view);
299 }
300
301 static void flatview_ref(FlatView *view)
302 {
303 atomic_inc(&view->ref);
304 }
305
306 static void flatview_unref(FlatView *view)
307 {
308 if (atomic_fetch_dec(&view->ref) == 1) {
309 flatview_destroy(view);
310 }
311 }
312
313 static bool can_merge(FlatRange *r1, FlatRange *r2)
314 {
315 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
316 && r1->mr == r2->mr
317 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
318 r1->addr.size),
319 int128_make64(r2->offset_in_region))
320 && r1->dirty_log_mask == r2->dirty_log_mask
321 && r1->romd_mode == r2->romd_mode
322 && r1->readonly == r2->readonly;
323 }
324
325 /* Attempt to simplify a view by merging adjacent ranges */
326 static void flatview_simplify(FlatView *view)
327 {
328 unsigned i, j;
329
330 i = 0;
331 while (i < view->nr) {
332 j = i + 1;
333 while (j < view->nr
334 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
335 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
336 ++j;
337 }
338 ++i;
339 memmove(&view->ranges[i], &view->ranges[j],
340 (view->nr - j) * sizeof(view->ranges[j]));
341 view->nr -= j - i;
342 }
343 }
344
345 static bool memory_region_big_endian(MemoryRegion *mr)
346 {
347 #ifdef TARGET_WORDS_BIGENDIAN
348 return mr->ops->endianness != DEVICE_LITTLE_ENDIAN;
349 #else
350 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
351 #endif
352 }
353
354 static bool memory_region_wrong_endianness(MemoryRegion *mr)
355 {
356 #ifdef TARGET_WORDS_BIGENDIAN
357 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
358 #else
359 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
360 #endif
361 }
362
363 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
364 {
365 if (memory_region_wrong_endianness(mr)) {
366 switch (size) {
367 case 1:
368 break;
369 case 2:
370 *data = bswap16(*data);
371 break;
372 case 4:
373 *data = bswap32(*data);
374 break;
375 case 8:
376 *data = bswap64(*data);
377 break;
378 default:
379 abort();
380 }
381 }
382 }
383
384 static void memory_region_oldmmio_read_accessor(MemoryRegion *mr,
385 hwaddr addr,
386 uint64_t *value,
387 unsigned size,
388 unsigned shift,
389 uint64_t mask)
390 {
391 uint64_t tmp;
392
393 tmp = mr->ops->old_mmio.read[ctz32(size)](mr->opaque, addr);
394 trace_memory_region_ops_read(mr, addr, tmp, size);
395 *value |= (tmp & mask) << shift;
396 }
397
398 static void memory_region_read_accessor(MemoryRegion *mr,
399 hwaddr addr,
400 uint64_t *value,
401 unsigned size,
402 unsigned shift,
403 uint64_t mask)
404 {
405 uint64_t tmp;
406
407 if (mr->flush_coalesced_mmio) {
408 qemu_flush_coalesced_mmio_buffer();
409 }
410 tmp = mr->ops->read(mr->opaque, addr, size);
411 trace_memory_region_ops_read(mr, addr, tmp, size);
412 *value |= (tmp & mask) << shift;
413 }
414
415 static void memory_region_oldmmio_write_accessor(MemoryRegion *mr,
416 hwaddr addr,
417 uint64_t *value,
418 unsigned size,
419 unsigned shift,
420 uint64_t mask)
421 {
422 uint64_t tmp;
423
424 tmp = (*value >> shift) & mask;
425 trace_memory_region_ops_write(mr, addr, tmp, size);
426 mr->ops->old_mmio.write[ctz32(size)](mr->opaque, addr, tmp);
427 }
428
429 static void memory_region_write_accessor(MemoryRegion *mr,
430 hwaddr addr,
431 uint64_t *value,
432 unsigned size,
433 unsigned shift,
434 uint64_t mask)
435 {
436 uint64_t tmp;
437
438 if (mr->flush_coalesced_mmio) {
439 qemu_flush_coalesced_mmio_buffer();
440 }
441 tmp = (*value >> shift) & mask;
442 trace_memory_region_ops_write(mr, addr, tmp, size);
443 mr->ops->write(mr->opaque, addr, tmp, size);
444 }
445
446 static void access_with_adjusted_size(hwaddr addr,
447 uint64_t *value,
448 unsigned size,
449 unsigned access_size_min,
450 unsigned access_size_max,
451 void (*access)(MemoryRegion *mr,
452 hwaddr addr,
453 uint64_t *value,
454 unsigned size,
455 unsigned shift,
456 uint64_t mask),
457 MemoryRegion *mr)
458 {
459 uint64_t access_mask;
460 unsigned access_size;
461 unsigned i;
462
463 if (!access_size_min) {
464 access_size_min = 1;
465 }
466 if (!access_size_max) {
467 access_size_max = 4;
468 }
469
470 /* FIXME: support unaligned access? */
471 access_size = MAX(MIN(size, access_size_max), access_size_min);
472 access_mask = -1ULL >> (64 - access_size * 8);
473 if (memory_region_big_endian(mr)) {
474 for (i = 0; i < size; i += access_size) {
475 access(mr, addr + i, value, access_size,
476 (size - access_size - i) * 8, access_mask);
477 }
478 } else {
479 for (i = 0; i < size; i += access_size) {
480 access(mr, addr + i, value, access_size, i * 8, access_mask);
481 }
482 }
483 }
484
485 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
486 {
487 AddressSpace *as;
488
489 while (mr->container) {
490 mr = mr->container;
491 }
492 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
493 if (mr == as->root) {
494 return as;
495 }
496 }
497 return NULL;
498 }
499
500 /* Render a memory region into the global view. Ranges in @view obscure
501 * ranges in @mr.
502 */
503 static void render_memory_region(FlatView *view,
504 MemoryRegion *mr,
505 Int128 base,
506 AddrRange clip,
507 bool readonly)
508 {
509 MemoryRegion *subregion;
510 unsigned i;
511 hwaddr offset_in_region;
512 Int128 remain;
513 Int128 now;
514 FlatRange fr;
515 AddrRange tmp;
516
517 if (!mr->enabled) {
518 return;
519 }
520
521 int128_addto(&base, int128_make64(mr->addr));
522 readonly |= mr->readonly;
523
524 tmp = addrrange_make(base, mr->size);
525
526 if (!addrrange_intersects(tmp, clip)) {
527 return;
528 }
529
530 clip = addrrange_intersection(tmp, clip);
531
532 if (mr->alias) {
533 int128_subfrom(&base, int128_make64(mr->alias->addr));
534 int128_subfrom(&base, int128_make64(mr->alias_offset));
535 render_memory_region(view, mr->alias, base, clip, readonly);
536 return;
537 }
538
539 /* Render subregions in priority order. */
540 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
541 render_memory_region(view, subregion, base, clip, readonly);
542 }
543
544 if (!mr->terminates) {
545 return;
546 }
547
548 offset_in_region = int128_get64(int128_sub(clip.start, base));
549 base = clip.start;
550 remain = clip.size;
551
552 fr.mr = mr;
553 fr.dirty_log_mask = mr->dirty_log_mask;
554 fr.romd_mode = mr->romd_mode;
555 fr.readonly = readonly;
556
557 /* Render the region itself into any gaps left by the current view. */
558 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
559 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
560 continue;
561 }
562 if (int128_lt(base, view->ranges[i].addr.start)) {
563 now = int128_min(remain,
564 int128_sub(view->ranges[i].addr.start, base));
565 fr.offset_in_region = offset_in_region;
566 fr.addr = addrrange_make(base, now);
567 flatview_insert(view, i, &fr);
568 ++i;
569 int128_addto(&base, now);
570 offset_in_region += int128_get64(now);
571 int128_subfrom(&remain, now);
572 }
573 now = int128_sub(int128_min(int128_add(base, remain),
574 addrrange_end(view->ranges[i].addr)),
575 base);
576 int128_addto(&base, now);
577 offset_in_region += int128_get64(now);
578 int128_subfrom(&remain, now);
579 }
580 if (int128_nz(remain)) {
581 fr.offset_in_region = offset_in_region;
582 fr.addr = addrrange_make(base, remain);
583 flatview_insert(view, i, &fr);
584 }
585 }
586
587 /* Render a memory topology into a list of disjoint absolute ranges. */
588 static FlatView *generate_memory_topology(MemoryRegion *mr)
589 {
590 FlatView *view;
591
592 view = g_new(FlatView, 1);
593 flatview_init(view);
594
595 if (mr) {
596 render_memory_region(view, mr, int128_zero(),
597 addrrange_make(int128_zero(), int128_2_64()), false);
598 }
599 flatview_simplify(view);
600
601 return view;
602 }
603
604 static void address_space_add_del_ioeventfds(AddressSpace *as,
605 MemoryRegionIoeventfd *fds_new,
606 unsigned fds_new_nb,
607 MemoryRegionIoeventfd *fds_old,
608 unsigned fds_old_nb)
609 {
610 unsigned iold, inew;
611 MemoryRegionIoeventfd *fd;
612 MemoryRegionSection section;
613
614 /* Generate a symmetric difference of the old and new fd sets, adding
615 * and deleting as necessary.
616 */
617
618 iold = inew = 0;
619 while (iold < fds_old_nb || inew < fds_new_nb) {
620 if (iold < fds_old_nb
621 && (inew == fds_new_nb
622 || memory_region_ioeventfd_before(fds_old[iold],
623 fds_new[inew]))) {
624 fd = &fds_old[iold];
625 section = (MemoryRegionSection) {
626 .address_space = as,
627 .offset_within_address_space = int128_get64(fd->addr.start),
628 .size = fd->addr.size,
629 };
630 MEMORY_LISTENER_CALL(eventfd_del, Forward, &section,
631 fd->match_data, fd->data, fd->e);
632 ++iold;
633 } else if (inew < fds_new_nb
634 && (iold == fds_old_nb
635 || memory_region_ioeventfd_before(fds_new[inew],
636 fds_old[iold]))) {
637 fd = &fds_new[inew];
638 section = (MemoryRegionSection) {
639 .address_space = as,
640 .offset_within_address_space = int128_get64(fd->addr.start),
641 .size = fd->addr.size,
642 };
643 MEMORY_LISTENER_CALL(eventfd_add, Reverse, &section,
644 fd->match_data, fd->data, fd->e);
645 ++inew;
646 } else {
647 ++iold;
648 ++inew;
649 }
650 }
651 }
652
653 static FlatView *address_space_get_flatview(AddressSpace *as)
654 {
655 FlatView *view;
656
657 qemu_mutex_lock(&flat_view_mutex);
658 view = as->current_map;
659 flatview_ref(view);
660 qemu_mutex_unlock(&flat_view_mutex);
661 return view;
662 }
663
664 static void address_space_update_ioeventfds(AddressSpace *as)
665 {
666 FlatView *view;
667 FlatRange *fr;
668 unsigned ioeventfd_nb = 0;
669 MemoryRegionIoeventfd *ioeventfds = NULL;
670 AddrRange tmp;
671 unsigned i;
672
673 view = address_space_get_flatview(as);
674 FOR_EACH_FLAT_RANGE(fr, view) {
675 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
676 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
677 int128_sub(fr->addr.start,
678 int128_make64(fr->offset_in_region)));
679 if (addrrange_intersects(fr->addr, tmp)) {
680 ++ioeventfd_nb;
681 ioeventfds = g_realloc(ioeventfds,
682 ioeventfd_nb * sizeof(*ioeventfds));
683 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
684 ioeventfds[ioeventfd_nb-1].addr = tmp;
685 }
686 }
687 }
688
689 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
690 as->ioeventfds, as->ioeventfd_nb);
691
692 g_free(as->ioeventfds);
693 as->ioeventfds = ioeventfds;
694 as->ioeventfd_nb = ioeventfd_nb;
695 flatview_unref(view);
696 }
697
698 static void address_space_update_topology_pass(AddressSpace *as,
699 const FlatView *old_view,
700 const FlatView *new_view,
701 bool adding)
702 {
703 unsigned iold, inew;
704 FlatRange *frold, *frnew;
705
706 /* Generate a symmetric difference of the old and new memory maps.
707 * Kill ranges in the old map, and instantiate ranges in the new map.
708 */
709 iold = inew = 0;
710 while (iold < old_view->nr || inew < new_view->nr) {
711 if (iold < old_view->nr) {
712 frold = &old_view->ranges[iold];
713 } else {
714 frold = NULL;
715 }
716 if (inew < new_view->nr) {
717 frnew = &new_view->ranges[inew];
718 } else {
719 frnew = NULL;
720 }
721
722 if (frold
723 && (!frnew
724 || int128_lt(frold->addr.start, frnew->addr.start)
725 || (int128_eq(frold->addr.start, frnew->addr.start)
726 && !flatrange_equal(frold, frnew)))) {
727 /* In old but not in new, or in both but attributes changed. */
728
729 if (!adding) {
730 MEMORY_LISTENER_UPDATE_REGION(frold, as, Reverse, region_del);
731 }
732
733 ++iold;
734 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
735 /* In both and unchanged (except logging may have changed) */
736
737 if (adding) {
738 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_nop);
739 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
740 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Reverse, log_stop);
741 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
742 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, log_start);
743 }
744 }
745
746 ++iold;
747 ++inew;
748 } else {
749 /* In new */
750
751 if (adding) {
752 MEMORY_LISTENER_UPDATE_REGION(frnew, as, Forward, region_add);
753 }
754
755 ++inew;
756 }
757 }
758 }
759
760
761 static void address_space_update_topology(AddressSpace *as)
762 {
763 FlatView *old_view = address_space_get_flatview(as);
764 FlatView *new_view = generate_memory_topology(as->root);
765
766 address_space_update_topology_pass(as, old_view, new_view, false);
767 address_space_update_topology_pass(as, old_view, new_view, true);
768
769 qemu_mutex_lock(&flat_view_mutex);
770 flatview_unref(as->current_map);
771 as->current_map = new_view;
772 qemu_mutex_unlock(&flat_view_mutex);
773
774 /* Note that all the old MemoryRegions are still alive up to this
775 * point. This relieves most MemoryListeners from the need to
776 * ref/unref the MemoryRegions they get---unless they use them
777 * outside the iothread mutex, in which case precise reference
778 * counting is necessary.
779 */
780 flatview_unref(old_view);
781
782 address_space_update_ioeventfds(as);
783 }
784
785 void memory_region_transaction_begin(void)
786 {
787 qemu_flush_coalesced_mmio_buffer();
788 ++memory_region_transaction_depth;
789 }
790
791 static void memory_region_clear_pending(void)
792 {
793 memory_region_update_pending = false;
794 ioeventfd_update_pending = false;
795 }
796
797 void memory_region_transaction_commit(void)
798 {
799 AddressSpace *as;
800
801 assert(memory_region_transaction_depth);
802 --memory_region_transaction_depth;
803 if (!memory_region_transaction_depth) {
804 if (memory_region_update_pending) {
805 MEMORY_LISTENER_CALL_GLOBAL(begin, Forward);
806
807 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
808 address_space_update_topology(as);
809 }
810
811 MEMORY_LISTENER_CALL_GLOBAL(commit, Forward);
812 } else if (ioeventfd_update_pending) {
813 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
814 address_space_update_ioeventfds(as);
815 }
816 }
817 memory_region_clear_pending();
818 }
819 }
820
821 static void memory_region_destructor_none(MemoryRegion *mr)
822 {
823 }
824
825 static void memory_region_destructor_ram(MemoryRegion *mr)
826 {
827 qemu_ram_free(mr->ram_addr);
828 }
829
830 static void memory_region_destructor_alias(MemoryRegion *mr)
831 {
832 memory_region_unref(mr->alias);
833 }
834
835 static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
836 {
837 qemu_ram_free_from_ptr(mr->ram_addr);
838 }
839
840 static void memory_region_destructor_rom_device(MemoryRegion *mr)
841 {
842 qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
843 }
844
845 void memory_region_init(MemoryRegion *mr,
846 Object *owner,
847 const char *name,
848 uint64_t size)
849 {
850 mr->ops = &unassigned_mem_ops;
851 mr->opaque = NULL;
852 mr->owner = owner;
853 mr->iommu_ops = NULL;
854 mr->container = NULL;
855 mr->size = int128_make64(size);
856 if (size == UINT64_MAX) {
857 mr->size = int128_2_64();
858 }
859 mr->addr = 0;
860 mr->subpage = false;
861 mr->enabled = true;
862 mr->terminates = false;
863 mr->ram = false;
864 mr->romd_mode = true;
865 mr->readonly = false;
866 mr->rom_device = false;
867 mr->destructor = memory_region_destructor_none;
868 mr->priority = 0;
869 mr->may_overlap = false;
870 mr->alias = NULL;
871 QTAILQ_INIT(&mr->subregions);
872 memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
873 QTAILQ_INIT(&mr->coalesced);
874 mr->name = g_strdup(name);
875 mr->dirty_log_mask = 0;
876 mr->ioeventfd_nb = 0;
877 mr->ioeventfds = NULL;
878 mr->flush_coalesced_mmio = false;
879 }
880
881 static uint64_t unassigned_mem_read(void *opaque, hwaddr addr,
882 unsigned size)
883 {
884 #ifdef DEBUG_UNASSIGNED
885 printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
886 #endif
887 if (current_cpu != NULL) {
888 cpu_unassigned_access(current_cpu, addr, false, false, 0, size);
889 }
890 return 0;
891 }
892
893 static void unassigned_mem_write(void *opaque, hwaddr addr,
894 uint64_t val, unsigned size)
895 {
896 #ifdef DEBUG_UNASSIGNED
897 printf("Unassigned mem write " TARGET_FMT_plx " = 0x%"PRIx64"\n", addr, val);
898 #endif
899 if (current_cpu != NULL) {
900 cpu_unassigned_access(current_cpu, addr, true, false, 0, size);
901 }
902 }
903
904 static bool unassigned_mem_accepts(void *opaque, hwaddr addr,
905 unsigned size, bool is_write)
906 {
907 return false;
908 }
909
910 const MemoryRegionOps unassigned_mem_ops = {
911 .valid.accepts = unassigned_mem_accepts,
912 .endianness = DEVICE_NATIVE_ENDIAN,
913 };
914
915 bool memory_region_access_valid(MemoryRegion *mr,
916 hwaddr addr,
917 unsigned size,
918 bool is_write)
919 {
920 int access_size_min, access_size_max;
921 int access_size, i;
922
923 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
924 return false;
925 }
926
927 if (!mr->ops->valid.accepts) {
928 return true;
929 }
930
931 access_size_min = mr->ops->valid.min_access_size;
932 if (!mr->ops->valid.min_access_size) {
933 access_size_min = 1;
934 }
935
936 access_size_max = mr->ops->valid.max_access_size;
937 if (!mr->ops->valid.max_access_size) {
938 access_size_max = 4;
939 }
940
941 access_size = MAX(MIN(size, access_size_max), access_size_min);
942 for (i = 0; i < size; i += access_size) {
943 if (!mr->ops->valid.accepts(mr->opaque, addr + i, access_size,
944 is_write)) {
945 return false;
946 }
947 }
948
949 return true;
950 }
951
952 static uint64_t memory_region_dispatch_read1(MemoryRegion *mr,
953 hwaddr addr,
954 unsigned size)
955 {
956 uint64_t data = 0;
957
958 if (mr->ops->read) {
959 access_with_adjusted_size(addr, &data, size,
960 mr->ops->impl.min_access_size,
961 mr->ops->impl.max_access_size,
962 memory_region_read_accessor, mr);
963 } else {
964 access_with_adjusted_size(addr, &data, size, 1, 4,
965 memory_region_oldmmio_read_accessor, mr);
966 }
967
968 return data;
969 }
970
971 static bool memory_region_dispatch_read(MemoryRegion *mr,
972 hwaddr addr,
973 uint64_t *pval,
974 unsigned size)
975 {
976 if (!memory_region_access_valid(mr, addr, size, false)) {
977 *pval = unassigned_mem_read(mr, addr, size);
978 return true;
979 }
980
981 *pval = memory_region_dispatch_read1(mr, addr, size);
982 adjust_endianness(mr, pval, size);
983 return false;
984 }
985
986 static bool memory_region_dispatch_write(MemoryRegion *mr,
987 hwaddr addr,
988 uint64_t data,
989 unsigned size)
990 {
991 if (!memory_region_access_valid(mr, addr, size, true)) {
992 unassigned_mem_write(mr, addr, data, size);
993 return true;
994 }
995
996 adjust_endianness(mr, &data, size);
997
998 if (mr->ops->write) {
999 access_with_adjusted_size(addr, &data, size,
1000 mr->ops->impl.min_access_size,
1001 mr->ops->impl.max_access_size,
1002 memory_region_write_accessor, mr);
1003 } else {
1004 access_with_adjusted_size(addr, &data, size, 1, 4,
1005 memory_region_oldmmio_write_accessor, mr);
1006 }
1007 return false;
1008 }
1009
1010 void memory_region_init_io(MemoryRegion *mr,
1011 Object *owner,
1012 const MemoryRegionOps *ops,
1013 void *opaque,
1014 const char *name,
1015 uint64_t size)
1016 {
1017 memory_region_init(mr, owner, name, size);
1018 mr->ops = ops;
1019 mr->opaque = opaque;
1020 mr->terminates = true;
1021 mr->ram_addr = ~(ram_addr_t)0;
1022 }
1023
1024 void memory_region_init_ram(MemoryRegion *mr,
1025 Object *owner,
1026 const char *name,
1027 uint64_t size)
1028 {
1029 memory_region_init(mr, owner, name, size);
1030 mr->ram = true;
1031 mr->terminates = true;
1032 mr->destructor = memory_region_destructor_ram;
1033 mr->ram_addr = qemu_ram_alloc(size, mr);
1034 }
1035
1036 #ifdef __linux__
1037 void memory_region_init_ram_from_file(MemoryRegion *mr,
1038 struct Object *owner,
1039 const char *name,
1040 uint64_t size,
1041 const char *path,
1042 Error **errp)
1043 {
1044 memory_region_init(mr, owner, name, size);
1045 mr->ram = true;
1046 mr->terminates = true;
1047 mr->destructor = memory_region_destructor_ram;
1048 mr->ram_addr = qemu_ram_alloc_from_file(size, mr, path, errp);
1049 }
1050 #endif
1051
1052 void memory_region_init_ram_ptr(MemoryRegion *mr,
1053 Object *owner,
1054 const char *name,
1055 uint64_t size,
1056 void *ptr)
1057 {
1058 memory_region_init(mr, owner, name, size);
1059 mr->ram = true;
1060 mr->terminates = true;
1061 mr->destructor = memory_region_destructor_ram_from_ptr;
1062 mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr);
1063 }
1064
1065 void memory_region_init_alias(MemoryRegion *mr,
1066 Object *owner,
1067 const char *name,
1068 MemoryRegion *orig,
1069 hwaddr offset,
1070 uint64_t size)
1071 {
1072 memory_region_init(mr, owner, name, size);
1073 memory_region_ref(orig);
1074 mr->destructor = memory_region_destructor_alias;
1075 mr->alias = orig;
1076 mr->alias_offset = offset;
1077 }
1078
1079 void memory_region_init_rom_device(MemoryRegion *mr,
1080 Object *owner,
1081 const MemoryRegionOps *ops,
1082 void *opaque,
1083 const char *name,
1084 uint64_t size)
1085 {
1086 memory_region_init(mr, owner, name, size);
1087 mr->ops = ops;
1088 mr->opaque = opaque;
1089 mr->terminates = true;
1090 mr->rom_device = true;
1091 mr->destructor = memory_region_destructor_rom_device;
1092 mr->ram_addr = qemu_ram_alloc(size, mr);
1093 }
1094
1095 void memory_region_init_iommu(MemoryRegion *mr,
1096 Object *owner,
1097 const MemoryRegionIOMMUOps *ops,
1098 const char *name,
1099 uint64_t size)
1100 {
1101 memory_region_init(mr, owner, name, size);
1102 mr->iommu_ops = ops,
1103 mr->terminates = true; /* then re-forwards */
1104 notifier_list_init(&mr->iommu_notify);
1105 }
1106
1107 void memory_region_init_reservation(MemoryRegion *mr,
1108 Object *owner,
1109 const char *name,
1110 uint64_t size)
1111 {
1112 memory_region_init_io(mr, owner, &unassigned_mem_ops, mr, name, size);
1113 }
1114
1115 void memory_region_destroy(MemoryRegion *mr)
1116 {
1117 assert(QTAILQ_EMPTY(&mr->subregions));
1118 assert(memory_region_transaction_depth == 0);
1119 mr->destructor(mr);
1120 memory_region_clear_coalescing(mr);
1121 g_free((char *)mr->name);
1122 g_free(mr->ioeventfds);
1123 }
1124
1125 Object *memory_region_owner(MemoryRegion *mr)
1126 {
1127 return mr->owner;
1128 }
1129
1130 void memory_region_ref(MemoryRegion *mr)
1131 {
1132 if (mr && mr->owner) {
1133 object_ref(mr->owner);
1134 }
1135 }
1136
1137 void memory_region_unref(MemoryRegion *mr)
1138 {
1139 if (mr && mr->owner) {
1140 object_unref(mr->owner);
1141 }
1142 }
1143
1144 uint64_t memory_region_size(MemoryRegion *mr)
1145 {
1146 if (int128_eq(mr->size, int128_2_64())) {
1147 return UINT64_MAX;
1148 }
1149 return int128_get64(mr->size);
1150 }
1151
1152 const char *memory_region_name(MemoryRegion *mr)
1153 {
1154 return mr->name;
1155 }
1156
1157 bool memory_region_is_ram(MemoryRegion *mr)
1158 {
1159 return mr->ram;
1160 }
1161
1162 bool memory_region_is_logging(MemoryRegion *mr)
1163 {
1164 return mr->dirty_log_mask;
1165 }
1166
1167 bool memory_region_is_rom(MemoryRegion *mr)
1168 {
1169 return mr->ram && mr->readonly;
1170 }
1171
1172 bool memory_region_is_iommu(MemoryRegion *mr)
1173 {
1174 return mr->iommu_ops;
1175 }
1176
1177 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n)
1178 {
1179 notifier_list_add(&mr->iommu_notify, n);
1180 }
1181
1182 void memory_region_unregister_iommu_notifier(Notifier *n)
1183 {
1184 notifier_remove(n);
1185 }
1186
1187 void memory_region_notify_iommu(MemoryRegion *mr,
1188 IOMMUTLBEntry entry)
1189 {
1190 assert(memory_region_is_iommu(mr));
1191 notifier_list_notify(&mr->iommu_notify, &entry);
1192 }
1193
1194 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1195 {
1196 uint8_t mask = 1 << client;
1197
1198 memory_region_transaction_begin();
1199 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1200 memory_region_update_pending |= mr->enabled;
1201 memory_region_transaction_commit();
1202 }
1203
1204 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
1205 hwaddr size, unsigned client)
1206 {
1207 assert(mr->terminates);
1208 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, size, client);
1209 }
1210
1211 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1212 hwaddr size)
1213 {
1214 assert(mr->terminates);
1215 cpu_physical_memory_set_dirty_range(mr->ram_addr + addr, size);
1216 }
1217
1218 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
1219 hwaddr size, unsigned client)
1220 {
1221 bool ret;
1222 assert(mr->terminates);
1223 ret = cpu_physical_memory_get_dirty(mr->ram_addr + addr, size, client);
1224 if (ret) {
1225 cpu_physical_memory_reset_dirty(mr->ram_addr + addr, size, client);
1226 }
1227 return ret;
1228 }
1229
1230
1231 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1232 {
1233 AddressSpace *as;
1234 FlatRange *fr;
1235
1236 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1237 FlatView *view = address_space_get_flatview(as);
1238 FOR_EACH_FLAT_RANGE(fr, view) {
1239 if (fr->mr == mr) {
1240 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1241 }
1242 }
1243 flatview_unref(view);
1244 }
1245 }
1246
1247 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1248 {
1249 if (mr->readonly != readonly) {
1250 memory_region_transaction_begin();
1251 mr->readonly = readonly;
1252 memory_region_update_pending |= mr->enabled;
1253 memory_region_transaction_commit();
1254 }
1255 }
1256
1257 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode)
1258 {
1259 if (mr->romd_mode != romd_mode) {
1260 memory_region_transaction_begin();
1261 mr->romd_mode = romd_mode;
1262 memory_region_update_pending |= mr->enabled;
1263 memory_region_transaction_commit();
1264 }
1265 }
1266
1267 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1268 hwaddr size, unsigned client)
1269 {
1270 assert(mr->terminates);
1271 cpu_physical_memory_reset_dirty(mr->ram_addr + addr, size, client);
1272 }
1273
1274 int memory_region_get_fd(MemoryRegion *mr)
1275 {
1276 if (mr->alias) {
1277 return memory_region_get_fd(mr->alias);
1278 }
1279
1280 assert(mr->terminates);
1281
1282 return qemu_get_ram_fd(mr->ram_addr & TARGET_PAGE_MASK);
1283 }
1284
1285 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1286 {
1287 if (mr->alias) {
1288 return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset;
1289 }
1290
1291 assert(mr->terminates);
1292
1293 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
1294 }
1295
1296 static void memory_region_update_coalesced_range_as(MemoryRegion *mr, AddressSpace *as)
1297 {
1298 FlatView *view;
1299 FlatRange *fr;
1300 CoalescedMemoryRange *cmr;
1301 AddrRange tmp;
1302 MemoryRegionSection section;
1303
1304 view = address_space_get_flatview(as);
1305 FOR_EACH_FLAT_RANGE(fr, view) {
1306 if (fr->mr == mr) {
1307 section = (MemoryRegionSection) {
1308 .address_space = as,
1309 .offset_within_address_space = int128_get64(fr->addr.start),
1310 .size = fr->addr.size,
1311 };
1312
1313 MEMORY_LISTENER_CALL(coalesced_mmio_del, Reverse, &section,
1314 int128_get64(fr->addr.start),
1315 int128_get64(fr->addr.size));
1316 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1317 tmp = addrrange_shift(cmr->addr,
1318 int128_sub(fr->addr.start,
1319 int128_make64(fr->offset_in_region)));
1320 if (!addrrange_intersects(tmp, fr->addr)) {
1321 continue;
1322 }
1323 tmp = addrrange_intersection(tmp, fr->addr);
1324 MEMORY_LISTENER_CALL(coalesced_mmio_add, Forward, &section,
1325 int128_get64(tmp.start),
1326 int128_get64(tmp.size));
1327 }
1328 }
1329 }
1330 flatview_unref(view);
1331 }
1332
1333 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1334 {
1335 AddressSpace *as;
1336
1337 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1338 memory_region_update_coalesced_range_as(mr, as);
1339 }
1340 }
1341
1342 void memory_region_set_coalescing(MemoryRegion *mr)
1343 {
1344 memory_region_clear_coalescing(mr);
1345 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1346 }
1347
1348 void memory_region_add_coalescing(MemoryRegion *mr,
1349 hwaddr offset,
1350 uint64_t size)
1351 {
1352 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1353
1354 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1355 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1356 memory_region_update_coalesced_range(mr);
1357 memory_region_set_flush_coalesced(mr);
1358 }
1359
1360 void memory_region_clear_coalescing(MemoryRegion *mr)
1361 {
1362 CoalescedMemoryRange *cmr;
1363 bool updated = false;
1364
1365 qemu_flush_coalesced_mmio_buffer();
1366 mr->flush_coalesced_mmio = false;
1367
1368 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1369 cmr = QTAILQ_FIRST(&mr->coalesced);
1370 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1371 g_free(cmr);
1372 updated = true;
1373 }
1374
1375 if (updated) {
1376 memory_region_update_coalesced_range(mr);
1377 }
1378 }
1379
1380 void memory_region_set_flush_coalesced(MemoryRegion *mr)
1381 {
1382 mr->flush_coalesced_mmio = true;
1383 }
1384
1385 void memory_region_clear_flush_coalesced(MemoryRegion *mr)
1386 {
1387 qemu_flush_coalesced_mmio_buffer();
1388 if (QTAILQ_EMPTY(&mr->coalesced)) {
1389 mr->flush_coalesced_mmio = false;
1390 }
1391 }
1392
1393 void memory_region_add_eventfd(MemoryRegion *mr,
1394 hwaddr addr,
1395 unsigned size,
1396 bool match_data,
1397 uint64_t data,
1398 EventNotifier *e)
1399 {
1400 MemoryRegionIoeventfd mrfd = {
1401 .addr.start = int128_make64(addr),
1402 .addr.size = int128_make64(size),
1403 .match_data = match_data,
1404 .data = data,
1405 .e = e,
1406 };
1407 unsigned i;
1408
1409 adjust_endianness(mr, &mrfd.data, size);
1410 memory_region_transaction_begin();
1411 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1412 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1413 break;
1414 }
1415 }
1416 ++mr->ioeventfd_nb;
1417 mr->ioeventfds = g_realloc(mr->ioeventfds,
1418 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1419 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1420 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1421 mr->ioeventfds[i] = mrfd;
1422 ioeventfd_update_pending |= mr->enabled;
1423 memory_region_transaction_commit();
1424 }
1425
1426 void memory_region_del_eventfd(MemoryRegion *mr,
1427 hwaddr addr,
1428 unsigned size,
1429 bool match_data,
1430 uint64_t data,
1431 EventNotifier *e)
1432 {
1433 MemoryRegionIoeventfd mrfd = {
1434 .addr.start = int128_make64(addr),
1435 .addr.size = int128_make64(size),
1436 .match_data = match_data,
1437 .data = data,
1438 .e = e,
1439 };
1440 unsigned i;
1441
1442 adjust_endianness(mr, &mrfd.data, size);
1443 memory_region_transaction_begin();
1444 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1445 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1446 break;
1447 }
1448 }
1449 assert(i != mr->ioeventfd_nb);
1450 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1451 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1452 --mr->ioeventfd_nb;
1453 mr->ioeventfds = g_realloc(mr->ioeventfds,
1454 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1455 ioeventfd_update_pending |= mr->enabled;
1456 memory_region_transaction_commit();
1457 }
1458
1459 static void memory_region_update_container_subregions(MemoryRegion *subregion)
1460 {
1461 hwaddr offset = subregion->addr;
1462 MemoryRegion *mr = subregion->container;
1463 MemoryRegion *other;
1464
1465 memory_region_transaction_begin();
1466
1467 memory_region_ref(subregion);
1468 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1469 if (subregion->may_overlap || other->may_overlap) {
1470 continue;
1471 }
1472 if (int128_ge(int128_make64(offset),
1473 int128_add(int128_make64(other->addr), other->size))
1474 || int128_le(int128_add(int128_make64(offset), subregion->size),
1475 int128_make64(other->addr))) {
1476 continue;
1477 }
1478 #if 0
1479 printf("warning: subregion collision %llx/%llx (%s) "
1480 "vs %llx/%llx (%s)\n",
1481 (unsigned long long)offset,
1482 (unsigned long long)int128_get64(subregion->size),
1483 subregion->name,
1484 (unsigned long long)other->addr,
1485 (unsigned long long)int128_get64(other->size),
1486 other->name);
1487 #endif
1488 }
1489 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1490 if (subregion->priority >= other->priority) {
1491 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1492 goto done;
1493 }
1494 }
1495 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1496 done:
1497 memory_region_update_pending |= mr->enabled && subregion->enabled;
1498 memory_region_transaction_commit();
1499 }
1500
1501 static void memory_region_add_subregion_common(MemoryRegion *mr,
1502 hwaddr offset,
1503 MemoryRegion *subregion)
1504 {
1505 assert(!subregion->container);
1506 subregion->container = mr;
1507 subregion->addr = offset;
1508 memory_region_update_container_subregions(subregion);
1509 }
1510
1511 void memory_region_add_subregion(MemoryRegion *mr,
1512 hwaddr offset,
1513 MemoryRegion *subregion)
1514 {
1515 subregion->may_overlap = false;
1516 subregion->priority = 0;
1517 memory_region_add_subregion_common(mr, offset, subregion);
1518 }
1519
1520 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1521 hwaddr offset,
1522 MemoryRegion *subregion,
1523 int priority)
1524 {
1525 subregion->may_overlap = true;
1526 subregion->priority = priority;
1527 memory_region_add_subregion_common(mr, offset, subregion);
1528 }
1529
1530 void memory_region_del_subregion(MemoryRegion *mr,
1531 MemoryRegion *subregion)
1532 {
1533 memory_region_transaction_begin();
1534 assert(subregion->container == mr);
1535 subregion->container = NULL;
1536 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1537 memory_region_unref(subregion);
1538 memory_region_update_pending |= mr->enabled && subregion->enabled;
1539 memory_region_transaction_commit();
1540 }
1541
1542 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1543 {
1544 if (enabled == mr->enabled) {
1545 return;
1546 }
1547 memory_region_transaction_begin();
1548 mr->enabled = enabled;
1549 memory_region_update_pending = true;
1550 memory_region_transaction_commit();
1551 }
1552
1553 static void memory_region_readd_subregion(MemoryRegion *mr)
1554 {
1555 MemoryRegion *container = mr->container;
1556
1557 if (container) {
1558 memory_region_transaction_begin();
1559 memory_region_ref(mr);
1560 memory_region_del_subregion(container, mr);
1561 mr->container = container;
1562 memory_region_update_container_subregions(mr);
1563 memory_region_unref(mr);
1564 memory_region_transaction_commit();
1565 }
1566 }
1567
1568 void memory_region_set_address(MemoryRegion *mr, hwaddr addr)
1569 {
1570 if (addr != mr->addr) {
1571 mr->addr = addr;
1572 memory_region_readd_subregion(mr);
1573 }
1574 }
1575
1576 void memory_region_set_alias_offset(MemoryRegion *mr, hwaddr offset)
1577 {
1578 assert(mr->alias);
1579
1580 if (offset == mr->alias_offset) {
1581 return;
1582 }
1583
1584 memory_region_transaction_begin();
1585 mr->alias_offset = offset;
1586 memory_region_update_pending |= mr->enabled;
1587 memory_region_transaction_commit();
1588 }
1589
1590 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1591 {
1592 return mr->ram_addr;
1593 }
1594
1595 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1596 {
1597 const AddrRange *addr = addr_;
1598 const FlatRange *fr = fr_;
1599
1600 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1601 return -1;
1602 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1603 return 1;
1604 }
1605 return 0;
1606 }
1607
1608 static FlatRange *flatview_lookup(FlatView *view, AddrRange addr)
1609 {
1610 return bsearch(&addr, view->ranges, view->nr,
1611 sizeof(FlatRange), cmp_flatrange_addr);
1612 }
1613
1614 bool memory_region_present(MemoryRegion *container, hwaddr addr)
1615 {
1616 MemoryRegion *mr = memory_region_find(container, addr, 1).mr;
1617 if (!mr || (mr == container)) {
1618 return false;
1619 }
1620 memory_region_unref(mr);
1621 return true;
1622 }
1623
1624 bool memory_region_is_mapped(MemoryRegion *mr)
1625 {
1626 return mr->container ? true : false;
1627 }
1628
1629 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1630 hwaddr addr, uint64_t size)
1631 {
1632 MemoryRegionSection ret = { .mr = NULL };
1633 MemoryRegion *root;
1634 AddressSpace *as;
1635 AddrRange range;
1636 FlatView *view;
1637 FlatRange *fr;
1638
1639 addr += mr->addr;
1640 for (root = mr; root->container; ) {
1641 root = root->container;
1642 addr += root->addr;
1643 }
1644
1645 as = memory_region_to_address_space(root);
1646 if (!as) {
1647 return ret;
1648 }
1649 range = addrrange_make(int128_make64(addr), int128_make64(size));
1650
1651 view = address_space_get_flatview(as);
1652 fr = flatview_lookup(view, range);
1653 if (!fr) {
1654 flatview_unref(view);
1655 return ret;
1656 }
1657
1658 while (fr > view->ranges && addrrange_intersects(fr[-1].addr, range)) {
1659 --fr;
1660 }
1661
1662 ret.mr = fr->mr;
1663 ret.address_space = as;
1664 range = addrrange_intersection(range, fr->addr);
1665 ret.offset_within_region = fr->offset_in_region;
1666 ret.offset_within_region += int128_get64(int128_sub(range.start,
1667 fr->addr.start));
1668 ret.size = range.size;
1669 ret.offset_within_address_space = int128_get64(range.start);
1670 ret.readonly = fr->readonly;
1671 memory_region_ref(ret.mr);
1672
1673 flatview_unref(view);
1674 return ret;
1675 }
1676
1677 void address_space_sync_dirty_bitmap(AddressSpace *as)
1678 {
1679 FlatView *view;
1680 FlatRange *fr;
1681
1682 view = address_space_get_flatview(as);
1683 FOR_EACH_FLAT_RANGE(fr, view) {
1684 MEMORY_LISTENER_UPDATE_REGION(fr, as, Forward, log_sync);
1685 }
1686 flatview_unref(view);
1687 }
1688
1689 void memory_global_dirty_log_start(void)
1690 {
1691 global_dirty_log = true;
1692 MEMORY_LISTENER_CALL_GLOBAL(log_global_start, Forward);
1693 }
1694
1695 void memory_global_dirty_log_stop(void)
1696 {
1697 global_dirty_log = false;
1698 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop, Reverse);
1699 }
1700
1701 static void listener_add_address_space(MemoryListener *listener,
1702 AddressSpace *as)
1703 {
1704 FlatView *view;
1705 FlatRange *fr;
1706
1707 if (listener->address_space_filter
1708 && listener->address_space_filter != as) {
1709 return;
1710 }
1711
1712 if (global_dirty_log) {
1713 if (listener->log_global_start) {
1714 listener->log_global_start(listener);
1715 }
1716 }
1717
1718 view = address_space_get_flatview(as);
1719 FOR_EACH_FLAT_RANGE(fr, view) {
1720 MemoryRegionSection section = {
1721 .mr = fr->mr,
1722 .address_space = as,
1723 .offset_within_region = fr->offset_in_region,
1724 .size = fr->addr.size,
1725 .offset_within_address_space = int128_get64(fr->addr.start),
1726 .readonly = fr->readonly,
1727 };
1728 if (listener->region_add) {
1729 listener->region_add(listener, &section);
1730 }
1731 }
1732 flatview_unref(view);
1733 }
1734
1735 void memory_listener_register(MemoryListener *listener, AddressSpace *filter)
1736 {
1737 MemoryListener *other = NULL;
1738 AddressSpace *as;
1739
1740 listener->address_space_filter = filter;
1741 if (QTAILQ_EMPTY(&memory_listeners)
1742 || listener->priority >= QTAILQ_LAST(&memory_listeners,
1743 memory_listeners)->priority) {
1744 QTAILQ_INSERT_TAIL(&memory_listeners, listener, link);
1745 } else {
1746 QTAILQ_FOREACH(other, &memory_listeners, link) {
1747 if (listener->priority < other->priority) {
1748 break;
1749 }
1750 }
1751 QTAILQ_INSERT_BEFORE(other, listener, link);
1752 }
1753
1754 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1755 listener_add_address_space(listener, as);
1756 }
1757 }
1758
1759 void memory_listener_unregister(MemoryListener *listener)
1760 {
1761 QTAILQ_REMOVE(&memory_listeners, listener, link);
1762 }
1763
1764 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name)
1765 {
1766 if (QTAILQ_EMPTY(&address_spaces)) {
1767 memory_init();
1768 }
1769
1770 memory_region_transaction_begin();
1771 as->root = root;
1772 as->current_map = g_new(FlatView, 1);
1773 flatview_init(as->current_map);
1774 as->ioeventfd_nb = 0;
1775 as->ioeventfds = NULL;
1776 QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link);
1777 as->name = g_strdup(name ? name : "anonymous");
1778 address_space_init_dispatch(as);
1779 memory_region_update_pending |= root->enabled;
1780 memory_region_transaction_commit();
1781 }
1782
1783 void address_space_destroy(AddressSpace *as)
1784 {
1785 MemoryListener *listener;
1786
1787 /* Flush out anything from MemoryListeners listening in on this */
1788 memory_region_transaction_begin();
1789 as->root = NULL;
1790 memory_region_transaction_commit();
1791 QTAILQ_REMOVE(&address_spaces, as, address_spaces_link);
1792 address_space_destroy_dispatch(as);
1793
1794 QTAILQ_FOREACH(listener, &memory_listeners, link) {
1795 assert(listener->address_space_filter != as);
1796 }
1797
1798 flatview_unref(as->current_map);
1799 g_free(as->name);
1800 g_free(as->ioeventfds);
1801 }
1802
1803 bool io_mem_read(MemoryRegion *mr, hwaddr addr, uint64_t *pval, unsigned size)
1804 {
1805 return memory_region_dispatch_read(mr, addr, pval, size);
1806 }
1807
1808 bool io_mem_write(MemoryRegion *mr, hwaddr addr,
1809 uint64_t val, unsigned size)
1810 {
1811 return memory_region_dispatch_write(mr, addr, val, size);
1812 }
1813
1814 typedef struct MemoryRegionList MemoryRegionList;
1815
1816 struct MemoryRegionList {
1817 const MemoryRegion *mr;
1818 bool printed;
1819 QTAILQ_ENTRY(MemoryRegionList) queue;
1820 };
1821
1822 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1823
1824 static void mtree_print_mr(fprintf_function mon_printf, void *f,
1825 const MemoryRegion *mr, unsigned int level,
1826 hwaddr base,
1827 MemoryRegionListHead *alias_print_queue)
1828 {
1829 MemoryRegionList *new_ml, *ml, *next_ml;
1830 MemoryRegionListHead submr_print_queue;
1831 const MemoryRegion *submr;
1832 unsigned int i;
1833
1834 if (!mr || !mr->enabled) {
1835 return;
1836 }
1837
1838 for (i = 0; i < level; i++) {
1839 mon_printf(f, " ");
1840 }
1841
1842 if (mr->alias) {
1843 MemoryRegionList *ml;
1844 bool found = false;
1845
1846 /* check if the alias is already in the queue */
1847 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
1848 if (ml->mr == mr->alias && !ml->printed) {
1849 found = true;
1850 }
1851 }
1852
1853 if (!found) {
1854 ml = g_new(MemoryRegionList, 1);
1855 ml->mr = mr->alias;
1856 ml->printed = false;
1857 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
1858 }
1859 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx
1860 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1861 "-" TARGET_FMT_plx "\n",
1862 base + mr->addr,
1863 base + mr->addr
1864 + (int128_nz(mr->size) ?
1865 (hwaddr)int128_get64(int128_sub(mr->size,
1866 int128_one())) : 0),
1867 mr->priority,
1868 mr->romd_mode ? 'R' : '-',
1869 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
1870 : '-',
1871 mr->name,
1872 mr->alias->name,
1873 mr->alias_offset,
1874 mr->alias_offset
1875 + (int128_nz(mr->size) ?
1876 (hwaddr)int128_get64(int128_sub(mr->size,
1877 int128_one())) : 0));
1878 } else {
1879 mon_printf(f,
1880 TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d, %c%c): %s\n",
1881 base + mr->addr,
1882 base + mr->addr
1883 + (int128_nz(mr->size) ?
1884 (hwaddr)int128_get64(int128_sub(mr->size,
1885 int128_one())) : 0),
1886 mr->priority,
1887 mr->romd_mode ? 'R' : '-',
1888 !mr->readonly && !(mr->rom_device && mr->romd_mode) ? 'W'
1889 : '-',
1890 mr->name);
1891 }
1892
1893 QTAILQ_INIT(&submr_print_queue);
1894
1895 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
1896 new_ml = g_new(MemoryRegionList, 1);
1897 new_ml->mr = submr;
1898 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1899 if (new_ml->mr->addr < ml->mr->addr ||
1900 (new_ml->mr->addr == ml->mr->addr &&
1901 new_ml->mr->priority > ml->mr->priority)) {
1902 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
1903 new_ml = NULL;
1904 break;
1905 }
1906 }
1907 if (new_ml) {
1908 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
1909 }
1910 }
1911
1912 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1913 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
1914 alias_print_queue);
1915 }
1916
1917 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
1918 g_free(ml);
1919 }
1920 }
1921
1922 void mtree_info(fprintf_function mon_printf, void *f)
1923 {
1924 MemoryRegionListHead ml_head;
1925 MemoryRegionList *ml, *ml2;
1926 AddressSpace *as;
1927
1928 QTAILQ_INIT(&ml_head);
1929
1930 QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) {
1931 mon_printf(f, "%s\n", as->name);
1932 mtree_print_mr(mon_printf, f, as->root, 0, 0, &ml_head);
1933 }
1934
1935 mon_printf(f, "aliases\n");
1936 /* print aliased regions */
1937 QTAILQ_FOREACH(ml, &ml_head, queue) {
1938 if (!ml->printed) {
1939 mon_printf(f, "%s\n", ml->mr->name);
1940 mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head);
1941 }
1942 }
1943
1944 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
1945 g_free(ml);
1946 }
1947 }