hyper-v: introduce Hyper-V support infrastructure.
[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 "memory.h"
17 #include "exec-memory.h"
18 #include "ioport.h"
19 #include "bitops.h"
20 #include "kvm.h"
21 #include <assert.h>
22
23 #define WANT_EXEC_OBSOLETE
24 #include "exec-obsolete.h"
25
26 unsigned memory_region_transaction_depth = 0;
27 static bool memory_region_update_pending = false;
28 static bool global_dirty_log = false;
29
30 static QLIST_HEAD(, MemoryListener) memory_listeners
31 = QLIST_HEAD_INITIALIZER(memory_listeners);
32
33 typedef struct AddrRange AddrRange;
34
35 /*
36 * Note using signed integers limits us to physical addresses at most
37 * 63 bits wide. They are needed for negative offsetting in aliases
38 * (large MemoryRegion::alias_offset).
39 */
40 struct AddrRange {
41 Int128 start;
42 Int128 size;
43 };
44
45 static AddrRange addrrange_make(Int128 start, Int128 size)
46 {
47 return (AddrRange) { start, size };
48 }
49
50 static bool addrrange_equal(AddrRange r1, AddrRange r2)
51 {
52 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
53 }
54
55 static Int128 addrrange_end(AddrRange r)
56 {
57 return int128_add(r.start, r.size);
58 }
59
60 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
61 {
62 int128_addto(&range.start, delta);
63 return range;
64 }
65
66 static bool addrrange_contains(AddrRange range, Int128 addr)
67 {
68 return int128_ge(addr, range.start)
69 && int128_lt(addr, addrrange_end(range));
70 }
71
72 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
73 {
74 return addrrange_contains(r1, r2.start)
75 || addrrange_contains(r2, r1.start);
76 }
77
78 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
79 {
80 Int128 start = int128_max(r1.start, r2.start);
81 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
82 return addrrange_make(start, int128_sub(end, start));
83 }
84
85 struct CoalescedMemoryRange {
86 AddrRange addr;
87 QTAILQ_ENTRY(CoalescedMemoryRange) link;
88 };
89
90 struct MemoryRegionIoeventfd {
91 AddrRange addr;
92 bool match_data;
93 uint64_t data;
94 int fd;
95 };
96
97 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
98 MemoryRegionIoeventfd b)
99 {
100 if (int128_lt(a.addr.start, b.addr.start)) {
101 return true;
102 } else if (int128_gt(a.addr.start, b.addr.start)) {
103 return false;
104 } else if (int128_lt(a.addr.size, b.addr.size)) {
105 return true;
106 } else if (int128_gt(a.addr.size, b.addr.size)) {
107 return false;
108 } else if (a.match_data < b.match_data) {
109 return true;
110 } else if (a.match_data > b.match_data) {
111 return false;
112 } else if (a.match_data) {
113 if (a.data < b.data) {
114 return true;
115 } else if (a.data > b.data) {
116 return false;
117 }
118 }
119 if (a.fd < b.fd) {
120 return true;
121 } else if (a.fd > b.fd) {
122 return false;
123 }
124 return false;
125 }
126
127 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
128 MemoryRegionIoeventfd b)
129 {
130 return !memory_region_ioeventfd_before(a, b)
131 && !memory_region_ioeventfd_before(b, a);
132 }
133
134 typedef struct FlatRange FlatRange;
135 typedef struct FlatView FlatView;
136
137 /* Range of memory in the global map. Addresses are absolute. */
138 struct FlatRange {
139 MemoryRegion *mr;
140 target_phys_addr_t offset_in_region;
141 AddrRange addr;
142 uint8_t dirty_log_mask;
143 bool readable;
144 bool readonly;
145 };
146
147 /* Flattened global view of current active memory hierarchy. Kept in sorted
148 * order.
149 */
150 struct FlatView {
151 FlatRange *ranges;
152 unsigned nr;
153 unsigned nr_allocated;
154 };
155
156 typedef struct AddressSpace AddressSpace;
157 typedef struct AddressSpaceOps AddressSpaceOps;
158
159 /* A system address space - I/O, memory, etc. */
160 struct AddressSpace {
161 const AddressSpaceOps *ops;
162 MemoryRegion *root;
163 FlatView current_map;
164 int ioeventfd_nb;
165 MemoryRegionIoeventfd *ioeventfds;
166 };
167
168 struct AddressSpaceOps {
169 void (*range_add)(AddressSpace *as, FlatRange *fr);
170 void (*range_del)(AddressSpace *as, FlatRange *fr);
171 void (*log_start)(AddressSpace *as, FlatRange *fr);
172 void (*log_stop)(AddressSpace *as, FlatRange *fr);
173 void (*ioeventfd_add)(AddressSpace *as, MemoryRegionIoeventfd *fd);
174 void (*ioeventfd_del)(AddressSpace *as, MemoryRegionIoeventfd *fd);
175 };
176
177 #define FOR_EACH_FLAT_RANGE(var, view) \
178 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
179
180 static bool flatrange_equal(FlatRange *a, FlatRange *b)
181 {
182 return a->mr == b->mr
183 && addrrange_equal(a->addr, b->addr)
184 && a->offset_in_region == b->offset_in_region
185 && a->readable == b->readable
186 && a->readonly == b->readonly;
187 }
188
189 static void flatview_init(FlatView *view)
190 {
191 view->ranges = NULL;
192 view->nr = 0;
193 view->nr_allocated = 0;
194 }
195
196 /* Insert a range into a given position. Caller is responsible for maintaining
197 * sorting order.
198 */
199 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
200 {
201 if (view->nr == view->nr_allocated) {
202 view->nr_allocated = MAX(2 * view->nr, 10);
203 view->ranges = g_realloc(view->ranges,
204 view->nr_allocated * sizeof(*view->ranges));
205 }
206 memmove(view->ranges + pos + 1, view->ranges + pos,
207 (view->nr - pos) * sizeof(FlatRange));
208 view->ranges[pos] = *range;
209 ++view->nr;
210 }
211
212 static void flatview_destroy(FlatView *view)
213 {
214 g_free(view->ranges);
215 }
216
217 static bool can_merge(FlatRange *r1, FlatRange *r2)
218 {
219 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
220 && r1->mr == r2->mr
221 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
222 r1->addr.size),
223 int128_make64(r2->offset_in_region))
224 && r1->dirty_log_mask == r2->dirty_log_mask
225 && r1->readable == r2->readable
226 && r1->readonly == r2->readonly;
227 }
228
229 /* Attempt to simplify a view by merging ajacent ranges */
230 static void flatview_simplify(FlatView *view)
231 {
232 unsigned i, j;
233
234 i = 0;
235 while (i < view->nr) {
236 j = i + 1;
237 while (j < view->nr
238 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
239 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
240 ++j;
241 }
242 ++i;
243 memmove(&view->ranges[i], &view->ranges[j],
244 (view->nr - j) * sizeof(view->ranges[j]));
245 view->nr -= j - i;
246 }
247 }
248
249 static void memory_region_read_accessor(void *opaque,
250 target_phys_addr_t addr,
251 uint64_t *value,
252 unsigned size,
253 unsigned shift,
254 uint64_t mask)
255 {
256 MemoryRegion *mr = opaque;
257 uint64_t tmp;
258
259 tmp = mr->ops->read(mr->opaque, addr, size);
260 *value |= (tmp & mask) << shift;
261 }
262
263 static void memory_region_write_accessor(void *opaque,
264 target_phys_addr_t addr,
265 uint64_t *value,
266 unsigned size,
267 unsigned shift,
268 uint64_t mask)
269 {
270 MemoryRegion *mr = opaque;
271 uint64_t tmp;
272
273 tmp = (*value >> shift) & mask;
274 mr->ops->write(mr->opaque, addr, tmp, size);
275 }
276
277 static void access_with_adjusted_size(target_phys_addr_t addr,
278 uint64_t *value,
279 unsigned size,
280 unsigned access_size_min,
281 unsigned access_size_max,
282 void (*access)(void *opaque,
283 target_phys_addr_t addr,
284 uint64_t *value,
285 unsigned size,
286 unsigned shift,
287 uint64_t mask),
288 void *opaque)
289 {
290 uint64_t access_mask;
291 unsigned access_size;
292 unsigned i;
293
294 if (!access_size_min) {
295 access_size_min = 1;
296 }
297 if (!access_size_max) {
298 access_size_max = 4;
299 }
300 access_size = MAX(MIN(size, access_size_max), access_size_min);
301 access_mask = -1ULL >> (64 - access_size * 8);
302 for (i = 0; i < size; i += access_size) {
303 /* FIXME: big-endian support */
304 access(opaque, addr + i, value, access_size, i * 8, access_mask);
305 }
306 }
307
308 static void as_memory_range_add(AddressSpace *as, FlatRange *fr)
309 {
310 MemoryRegionSection section = {
311 .mr = fr->mr,
312 .offset_within_address_space = int128_get64(fr->addr.start),
313 .offset_within_region = fr->offset_in_region,
314 .size = int128_get64(fr->addr.size),
315 };
316
317 cpu_register_physical_memory_log(&section, fr->readable, fr->readonly);
318 }
319
320 static void as_memory_range_del(AddressSpace *as, FlatRange *fr)
321 {
322 MemoryRegionSection section = {
323 .mr = &io_mem_unassigned,
324 .offset_within_address_space = int128_get64(fr->addr.start),
325 .offset_within_region = int128_get64(fr->addr.start),
326 .size = int128_get64(fr->addr.size),
327 };
328
329 cpu_register_physical_memory_log(&section, true, false);
330 }
331
332 static void as_memory_log_start(AddressSpace *as, FlatRange *fr)
333 {
334 }
335
336 static void as_memory_log_stop(AddressSpace *as, FlatRange *fr)
337 {
338 }
339
340 static void as_memory_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd)
341 {
342 int r;
343
344 assert(fd->match_data && int128_get64(fd->addr.size) == 4);
345
346 r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start),
347 fd->data, true);
348 if (r < 0) {
349 abort();
350 }
351 }
352
353 static void as_memory_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd)
354 {
355 int r;
356
357 r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start),
358 fd->data, false);
359 if (r < 0) {
360 abort();
361 }
362 }
363
364 static const AddressSpaceOps address_space_ops_memory = {
365 .range_add = as_memory_range_add,
366 .range_del = as_memory_range_del,
367 .log_start = as_memory_log_start,
368 .log_stop = as_memory_log_stop,
369 .ioeventfd_add = as_memory_ioeventfd_add,
370 .ioeventfd_del = as_memory_ioeventfd_del,
371 };
372
373 static AddressSpace address_space_memory = {
374 .ops = &address_space_ops_memory,
375 };
376
377 static const MemoryRegionPortio *find_portio(MemoryRegion *mr, uint64_t offset,
378 unsigned width, bool write)
379 {
380 const MemoryRegionPortio *mrp;
381
382 for (mrp = mr->ops->old_portio; mrp->size; ++mrp) {
383 if (offset >= mrp->offset && offset < mrp->offset + mrp->len
384 && width == mrp->size
385 && (write ? (bool)mrp->write : (bool)mrp->read)) {
386 return mrp;
387 }
388 }
389 return NULL;
390 }
391
392 static void memory_region_iorange_read(IORange *iorange,
393 uint64_t offset,
394 unsigned width,
395 uint64_t *data)
396 {
397 MemoryRegion *mr = container_of(iorange, MemoryRegion, iorange);
398
399 if (mr->ops->old_portio) {
400 const MemoryRegionPortio *mrp = find_portio(mr, offset, width, false);
401
402 *data = ((uint64_t)1 << (width * 8)) - 1;
403 if (mrp) {
404 *data = mrp->read(mr->opaque, offset + mr->offset);
405 } else if (width == 2) {
406 mrp = find_portio(mr, offset, 1, false);
407 assert(mrp);
408 *data = mrp->read(mr->opaque, offset + mr->offset) |
409 (mrp->read(mr->opaque, offset + mr->offset + 1) << 8);
410 }
411 return;
412 }
413 *data = 0;
414 access_with_adjusted_size(offset + mr->offset, data, width,
415 mr->ops->impl.min_access_size,
416 mr->ops->impl.max_access_size,
417 memory_region_read_accessor, mr);
418 }
419
420 static void memory_region_iorange_write(IORange *iorange,
421 uint64_t offset,
422 unsigned width,
423 uint64_t data)
424 {
425 MemoryRegion *mr = container_of(iorange, MemoryRegion, iorange);
426
427 if (mr->ops->old_portio) {
428 const MemoryRegionPortio *mrp = find_portio(mr, offset, width, true);
429
430 if (mrp) {
431 mrp->write(mr->opaque, offset + mr->offset, data);
432 } else if (width == 2) {
433 mrp = find_portio(mr, offset, 1, false);
434 assert(mrp);
435 mrp->write(mr->opaque, offset + mr->offset, data & 0xff);
436 mrp->write(mr->opaque, offset + mr->offset + 1, data >> 8);
437 }
438 return;
439 }
440 access_with_adjusted_size(offset + mr->offset, &data, width,
441 mr->ops->impl.min_access_size,
442 mr->ops->impl.max_access_size,
443 memory_region_write_accessor, mr);
444 }
445
446 static const IORangeOps memory_region_iorange_ops = {
447 .read = memory_region_iorange_read,
448 .write = memory_region_iorange_write,
449 };
450
451 static void as_io_range_add(AddressSpace *as, FlatRange *fr)
452 {
453 iorange_init(&fr->mr->iorange, &memory_region_iorange_ops,
454 int128_get64(fr->addr.start), int128_get64(fr->addr.size));
455 ioport_register(&fr->mr->iorange);
456 }
457
458 static void as_io_range_del(AddressSpace *as, FlatRange *fr)
459 {
460 isa_unassign_ioport(int128_get64(fr->addr.start),
461 int128_get64(fr->addr.size));
462 }
463
464 static void as_io_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd)
465 {
466 int r;
467
468 assert(fd->match_data && int128_get64(fd->addr.size) == 2);
469
470 r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start),
471 fd->data, true);
472 if (r < 0) {
473 abort();
474 }
475 }
476
477 static void as_io_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd)
478 {
479 int r;
480
481 r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start),
482 fd->data, false);
483 if (r < 0) {
484 abort();
485 }
486 }
487
488 static const AddressSpaceOps address_space_ops_io = {
489 .range_add = as_io_range_add,
490 .range_del = as_io_range_del,
491 .ioeventfd_add = as_io_ioeventfd_add,
492 .ioeventfd_del = as_io_ioeventfd_del,
493 };
494
495 static AddressSpace address_space_io = {
496 .ops = &address_space_ops_io,
497 };
498
499 static AddressSpace *memory_region_to_address_space(MemoryRegion *mr)
500 {
501 while (mr->parent) {
502 mr = mr->parent;
503 }
504 if (mr == address_space_memory.root) {
505 return &address_space_memory;
506 }
507 if (mr == address_space_io.root) {
508 return &address_space_io;
509 }
510 abort();
511 }
512
513 /* Render a memory region into the global view. Ranges in @view obscure
514 * ranges in @mr.
515 */
516 static void render_memory_region(FlatView *view,
517 MemoryRegion *mr,
518 Int128 base,
519 AddrRange clip,
520 bool readonly)
521 {
522 MemoryRegion *subregion;
523 unsigned i;
524 target_phys_addr_t offset_in_region;
525 Int128 remain;
526 Int128 now;
527 FlatRange fr;
528 AddrRange tmp;
529
530 if (!mr->enabled) {
531 return;
532 }
533
534 int128_addto(&base, int128_make64(mr->addr));
535 readonly |= mr->readonly;
536
537 tmp = addrrange_make(base, mr->size);
538
539 if (!addrrange_intersects(tmp, clip)) {
540 return;
541 }
542
543 clip = addrrange_intersection(tmp, clip);
544
545 if (mr->alias) {
546 int128_subfrom(&base, int128_make64(mr->alias->addr));
547 int128_subfrom(&base, int128_make64(mr->alias_offset));
548 render_memory_region(view, mr->alias, base, clip, readonly);
549 return;
550 }
551
552 /* Render subregions in priority order. */
553 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
554 render_memory_region(view, subregion, base, clip, readonly);
555 }
556
557 if (!mr->terminates) {
558 return;
559 }
560
561 offset_in_region = int128_get64(int128_sub(clip.start, base));
562 base = clip.start;
563 remain = clip.size;
564
565 /* Render the region itself into any gaps left by the current view. */
566 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
567 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
568 continue;
569 }
570 if (int128_lt(base, view->ranges[i].addr.start)) {
571 now = int128_min(remain,
572 int128_sub(view->ranges[i].addr.start, base));
573 fr.mr = mr;
574 fr.offset_in_region = offset_in_region;
575 fr.addr = addrrange_make(base, now);
576 fr.dirty_log_mask = mr->dirty_log_mask;
577 fr.readable = mr->readable;
578 fr.readonly = readonly;
579 flatview_insert(view, i, &fr);
580 ++i;
581 int128_addto(&base, now);
582 offset_in_region += int128_get64(now);
583 int128_subfrom(&remain, now);
584 }
585 if (int128_eq(base, view->ranges[i].addr.start)) {
586 now = int128_min(remain, view->ranges[i].addr.size);
587 int128_addto(&base, now);
588 offset_in_region += int128_get64(now);
589 int128_subfrom(&remain, now);
590 }
591 }
592 if (int128_nz(remain)) {
593 fr.mr = mr;
594 fr.offset_in_region = offset_in_region;
595 fr.addr = addrrange_make(base, remain);
596 fr.dirty_log_mask = mr->dirty_log_mask;
597 fr.readable = mr->readable;
598 fr.readonly = readonly;
599 flatview_insert(view, i, &fr);
600 }
601 }
602
603 /* Render a memory topology into a list of disjoint absolute ranges. */
604 static FlatView generate_memory_topology(MemoryRegion *mr)
605 {
606 FlatView view;
607
608 flatview_init(&view);
609
610 render_memory_region(&view, mr, int128_zero(),
611 addrrange_make(int128_zero(), int128_2_64()), false);
612 flatview_simplify(&view);
613
614 return view;
615 }
616
617 static void address_space_add_del_ioeventfds(AddressSpace *as,
618 MemoryRegionIoeventfd *fds_new,
619 unsigned fds_new_nb,
620 MemoryRegionIoeventfd *fds_old,
621 unsigned fds_old_nb)
622 {
623 unsigned iold, inew;
624
625 /* Generate a symmetric difference of the old and new fd sets, adding
626 * and deleting as necessary.
627 */
628
629 iold = inew = 0;
630 while (iold < fds_old_nb || inew < fds_new_nb) {
631 if (iold < fds_old_nb
632 && (inew == fds_new_nb
633 || memory_region_ioeventfd_before(fds_old[iold],
634 fds_new[inew]))) {
635 as->ops->ioeventfd_del(as, &fds_old[iold]);
636 ++iold;
637 } else if (inew < fds_new_nb
638 && (iold == fds_old_nb
639 || memory_region_ioeventfd_before(fds_new[inew],
640 fds_old[iold]))) {
641 as->ops->ioeventfd_add(as, &fds_new[inew]);
642 ++inew;
643 } else {
644 ++iold;
645 ++inew;
646 }
647 }
648 }
649
650 static void address_space_update_ioeventfds(AddressSpace *as)
651 {
652 FlatRange *fr;
653 unsigned ioeventfd_nb = 0;
654 MemoryRegionIoeventfd *ioeventfds = NULL;
655 AddrRange tmp;
656 unsigned i;
657
658 FOR_EACH_FLAT_RANGE(fr, &as->current_map) {
659 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
660 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
661 int128_sub(fr->addr.start,
662 int128_make64(fr->offset_in_region)));
663 if (addrrange_intersects(fr->addr, tmp)) {
664 ++ioeventfd_nb;
665 ioeventfds = g_realloc(ioeventfds,
666 ioeventfd_nb * sizeof(*ioeventfds));
667 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
668 ioeventfds[ioeventfd_nb-1].addr = tmp;
669 }
670 }
671 }
672
673 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
674 as->ioeventfds, as->ioeventfd_nb);
675
676 g_free(as->ioeventfds);
677 as->ioeventfds = ioeventfds;
678 as->ioeventfd_nb = ioeventfd_nb;
679 }
680
681 typedef void ListenerCallback(MemoryListener *listener,
682 MemoryRegionSection *mrs);
683
684 /* Want "void (&MemoryListener::*callback)(const MemoryRegionSection& s)" */
685 static void memory_listener_update_region(FlatRange *fr, AddressSpace *as,
686 size_t callback_offset)
687 {
688 MemoryRegionSection section = {
689 .mr = fr->mr,
690 .address_space = as->root,
691 .offset_within_region = fr->offset_in_region,
692 .size = int128_get64(fr->addr.size),
693 .offset_within_address_space = int128_get64(fr->addr.start),
694 };
695 MemoryListener *listener;
696
697 QLIST_FOREACH(listener, &memory_listeners, link) {
698 ListenerCallback *callback
699 = *(ListenerCallback **)((void *)listener + callback_offset);
700 callback(listener, &section);
701 }
702 }
703
704 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, callback) \
705 memory_listener_update_region(fr, as, offsetof(MemoryListener, callback))
706
707 static void address_space_update_topology_pass(AddressSpace *as,
708 FlatView old_view,
709 FlatView new_view,
710 bool adding)
711 {
712 unsigned iold, inew;
713 FlatRange *frold, *frnew;
714
715 /* Generate a symmetric difference of the old and new memory maps.
716 * Kill ranges in the old map, and instantiate ranges in the new map.
717 */
718 iold = inew = 0;
719 while (iold < old_view.nr || inew < new_view.nr) {
720 if (iold < old_view.nr) {
721 frold = &old_view.ranges[iold];
722 } else {
723 frold = NULL;
724 }
725 if (inew < new_view.nr) {
726 frnew = &new_view.ranges[inew];
727 } else {
728 frnew = NULL;
729 }
730
731 if (frold
732 && (!frnew
733 || int128_lt(frold->addr.start, frnew->addr.start)
734 || (int128_eq(frold->addr.start, frnew->addr.start)
735 && !flatrange_equal(frold, frnew)))) {
736 /* In old, but (not in new, or in new but attributes changed). */
737
738 if (!adding) {
739 MEMORY_LISTENER_UPDATE_REGION(frold, as, region_del);
740 as->ops->range_del(as, frold);
741 }
742
743 ++iold;
744 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
745 /* In both (logging may have changed) */
746
747 if (adding) {
748 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
749 MEMORY_LISTENER_UPDATE_REGION(frnew, as, log_stop);
750 as->ops->log_stop(as, frnew);
751 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
752 as->ops->log_start(as, frnew);
753 MEMORY_LISTENER_UPDATE_REGION(frnew, as, log_start);
754 }
755 }
756
757 ++iold;
758 ++inew;
759 } else {
760 /* In new */
761
762 if (adding) {
763 as->ops->range_add(as, frnew);
764 MEMORY_LISTENER_UPDATE_REGION(frnew, as, region_add);
765 }
766
767 ++inew;
768 }
769 }
770 }
771
772
773 static void address_space_update_topology(AddressSpace *as)
774 {
775 FlatView old_view = as->current_map;
776 FlatView new_view = generate_memory_topology(as->root);
777
778 address_space_update_topology_pass(as, old_view, new_view, false);
779 address_space_update_topology_pass(as, old_view, new_view, true);
780
781 as->current_map = new_view;
782 flatview_destroy(&old_view);
783 address_space_update_ioeventfds(as);
784 }
785
786 static void memory_region_update_topology(MemoryRegion *mr)
787 {
788 if (memory_region_transaction_depth) {
789 memory_region_update_pending |= !mr || mr->enabled;
790 return;
791 }
792
793 if (mr && !mr->enabled) {
794 return;
795 }
796
797 if (address_space_memory.root) {
798 address_space_update_topology(&address_space_memory);
799 }
800 if (address_space_io.root) {
801 address_space_update_topology(&address_space_io);
802 }
803
804 memory_region_update_pending = false;
805 }
806
807 void memory_region_transaction_begin(void)
808 {
809 ++memory_region_transaction_depth;
810 }
811
812 void memory_region_transaction_commit(void)
813 {
814 assert(memory_region_transaction_depth);
815 --memory_region_transaction_depth;
816 if (!memory_region_transaction_depth && memory_region_update_pending) {
817 memory_region_update_topology(NULL);
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_ram_from_ptr(MemoryRegion *mr)
831 {
832 qemu_ram_free_from_ptr(mr->ram_addr);
833 }
834
835 static void memory_region_destructor_iomem(MemoryRegion *mr)
836 {
837 cpu_unregister_io_memory(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 cpu_unregister_io_memory(mr->ram_addr & ~TARGET_PAGE_MASK);
844 }
845
846 static bool memory_region_wrong_endianness(MemoryRegion *mr)
847 {
848 #ifdef TARGET_WORDS_BIGENDIAN
849 return mr->ops->endianness == DEVICE_LITTLE_ENDIAN;
850 #else
851 return mr->ops->endianness == DEVICE_BIG_ENDIAN;
852 #endif
853 }
854
855 void memory_region_init(MemoryRegion *mr,
856 const char *name,
857 uint64_t size)
858 {
859 mr->ops = NULL;
860 mr->parent = NULL;
861 mr->size = int128_make64(size);
862 if (size == UINT64_MAX) {
863 mr->size = int128_2_64();
864 }
865 mr->addr = 0;
866 mr->offset = 0;
867 mr->subpage = false;
868 mr->enabled = true;
869 mr->terminates = false;
870 mr->ram = false;
871 mr->readable = true;
872 mr->readonly = false;
873 mr->rom_device = false;
874 mr->destructor = memory_region_destructor_none;
875 mr->priority = 0;
876 mr->may_overlap = false;
877 mr->alias = NULL;
878 QTAILQ_INIT(&mr->subregions);
879 memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
880 QTAILQ_INIT(&mr->coalesced);
881 mr->name = g_strdup(name);
882 mr->dirty_log_mask = 0;
883 mr->ioeventfd_nb = 0;
884 mr->ioeventfds = NULL;
885 }
886
887 static bool memory_region_access_valid(MemoryRegion *mr,
888 target_phys_addr_t addr,
889 unsigned size,
890 bool is_write)
891 {
892 if (mr->ops->valid.accepts
893 && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write)) {
894 return false;
895 }
896
897 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
898 return false;
899 }
900
901 /* Treat zero as compatibility all valid */
902 if (!mr->ops->valid.max_access_size) {
903 return true;
904 }
905
906 if (size > mr->ops->valid.max_access_size
907 || size < mr->ops->valid.min_access_size) {
908 return false;
909 }
910 return true;
911 }
912
913 static uint64_t memory_region_dispatch_read1(MemoryRegion *mr,
914 target_phys_addr_t addr,
915 unsigned size)
916 {
917 uint64_t data = 0;
918
919 if (!memory_region_access_valid(mr, addr, size, false)) {
920 return -1U; /* FIXME: better signalling */
921 }
922
923 if (!mr->ops->read) {
924 return mr->ops->old_mmio.read[bitops_ffsl(size)](mr->opaque, addr);
925 }
926
927 /* FIXME: support unaligned access */
928 access_with_adjusted_size(addr + mr->offset, &data, size,
929 mr->ops->impl.min_access_size,
930 mr->ops->impl.max_access_size,
931 memory_region_read_accessor, mr);
932
933 return data;
934 }
935
936 static void adjust_endianness(MemoryRegion *mr, uint64_t *data, unsigned size)
937 {
938 if (memory_region_wrong_endianness(mr)) {
939 switch (size) {
940 case 1:
941 break;
942 case 2:
943 *data = bswap16(*data);
944 break;
945 case 4:
946 *data = bswap32(*data);
947 break;
948 default:
949 abort();
950 }
951 }
952 }
953
954 static uint64_t memory_region_dispatch_read(MemoryRegion *mr,
955 target_phys_addr_t addr,
956 unsigned size)
957 {
958 uint64_t ret;
959
960 ret = memory_region_dispatch_read1(mr, addr, size);
961 adjust_endianness(mr, &ret, size);
962 return ret;
963 }
964
965 static void memory_region_dispatch_write(MemoryRegion *mr,
966 target_phys_addr_t addr,
967 uint64_t data,
968 unsigned size)
969 {
970 if (!memory_region_access_valid(mr, addr, size, true)) {
971 return; /* FIXME: better signalling */
972 }
973
974 adjust_endianness(mr, &data, size);
975
976 if (!mr->ops->write) {
977 mr->ops->old_mmio.write[bitops_ffsl(size)](mr->opaque, addr, data);
978 return;
979 }
980
981 /* FIXME: support unaligned access */
982 access_with_adjusted_size(addr + mr->offset, &data, size,
983 mr->ops->impl.min_access_size,
984 mr->ops->impl.max_access_size,
985 memory_region_write_accessor, mr);
986 }
987
988 void memory_region_init_io(MemoryRegion *mr,
989 const MemoryRegionOps *ops,
990 void *opaque,
991 const char *name,
992 uint64_t size)
993 {
994 memory_region_init(mr, name, size);
995 mr->ops = ops;
996 mr->opaque = opaque;
997 mr->terminates = true;
998 mr->destructor = memory_region_destructor_iomem;
999 mr->ram_addr = cpu_register_io_memory(mr);
1000 }
1001
1002 void memory_region_init_ram(MemoryRegion *mr,
1003 const char *name,
1004 uint64_t size)
1005 {
1006 memory_region_init(mr, name, size);
1007 mr->ram = true;
1008 mr->terminates = true;
1009 mr->destructor = memory_region_destructor_ram;
1010 mr->ram_addr = qemu_ram_alloc(size, mr);
1011 }
1012
1013 void memory_region_init_ram_ptr(MemoryRegion *mr,
1014 const char *name,
1015 uint64_t size,
1016 void *ptr)
1017 {
1018 memory_region_init(mr, name, size);
1019 mr->ram = true;
1020 mr->terminates = true;
1021 mr->destructor = memory_region_destructor_ram_from_ptr;
1022 mr->ram_addr = qemu_ram_alloc_from_ptr(size, ptr, mr);
1023 }
1024
1025 void memory_region_init_alias(MemoryRegion *mr,
1026 const char *name,
1027 MemoryRegion *orig,
1028 target_phys_addr_t offset,
1029 uint64_t size)
1030 {
1031 memory_region_init(mr, name, size);
1032 mr->alias = orig;
1033 mr->alias_offset = offset;
1034 }
1035
1036 void memory_region_init_rom_device(MemoryRegion *mr,
1037 const MemoryRegionOps *ops,
1038 void *opaque,
1039 const char *name,
1040 uint64_t size)
1041 {
1042 memory_region_init(mr, name, size);
1043 mr->ops = ops;
1044 mr->opaque = opaque;
1045 mr->terminates = true;
1046 mr->rom_device = true;
1047 mr->destructor = memory_region_destructor_rom_device;
1048 mr->ram_addr = qemu_ram_alloc(size, mr);
1049 mr->ram_addr |= cpu_register_io_memory(mr);
1050 }
1051
1052 void memory_region_destroy(MemoryRegion *mr)
1053 {
1054 assert(QTAILQ_EMPTY(&mr->subregions));
1055 mr->destructor(mr);
1056 memory_region_clear_coalescing(mr);
1057 g_free((char *)mr->name);
1058 g_free(mr->ioeventfds);
1059 }
1060
1061 uint64_t memory_region_size(MemoryRegion *mr)
1062 {
1063 if (int128_eq(mr->size, int128_2_64())) {
1064 return UINT64_MAX;
1065 }
1066 return int128_get64(mr->size);
1067 }
1068
1069 const char *memory_region_name(MemoryRegion *mr)
1070 {
1071 return mr->name;
1072 }
1073
1074 bool memory_region_is_ram(MemoryRegion *mr)
1075 {
1076 return mr->ram;
1077 }
1078
1079 bool memory_region_is_logging(MemoryRegion *mr)
1080 {
1081 return mr->dirty_log_mask;
1082 }
1083
1084 bool memory_region_is_rom(MemoryRegion *mr)
1085 {
1086 return mr->ram && mr->readonly;
1087 }
1088
1089 void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset)
1090 {
1091 mr->offset = offset;
1092 }
1093
1094 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1095 {
1096 uint8_t mask = 1 << client;
1097
1098 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1099 memory_region_update_topology(mr);
1100 }
1101
1102 bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1103 unsigned client)
1104 {
1105 assert(mr->terminates);
1106 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, 1 << client);
1107 }
1108
1109 void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr)
1110 {
1111 assert(mr->terminates);
1112 return cpu_physical_memory_set_dirty(mr->ram_addr + addr);
1113 }
1114
1115 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1116 {
1117 FlatRange *fr;
1118
1119 FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) {
1120 if (fr->mr == mr) {
1121 MEMORY_LISTENER_UPDATE_REGION(fr, &address_space_memory, log_sync);
1122 }
1123 }
1124 }
1125
1126 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1127 {
1128 if (mr->readonly != readonly) {
1129 mr->readonly = readonly;
1130 memory_region_update_topology(mr);
1131 }
1132 }
1133
1134 void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable)
1135 {
1136 if (mr->readable != readable) {
1137 mr->readable = readable;
1138 memory_region_update_topology(mr);
1139 }
1140 }
1141
1142 void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1143 target_phys_addr_t size, unsigned client)
1144 {
1145 assert(mr->terminates);
1146 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1147 mr->ram_addr + addr + size,
1148 1 << client);
1149 }
1150
1151 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1152 {
1153 if (mr->alias) {
1154 return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset;
1155 }
1156
1157 assert(mr->terminates);
1158
1159 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
1160 }
1161
1162 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1163 {
1164 FlatRange *fr;
1165 CoalescedMemoryRange *cmr;
1166 AddrRange tmp;
1167
1168 FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) {
1169 if (fr->mr == mr) {
1170 qemu_unregister_coalesced_mmio(int128_get64(fr->addr.start),
1171 int128_get64(fr->addr.size));
1172 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1173 tmp = addrrange_shift(cmr->addr,
1174 int128_sub(fr->addr.start,
1175 int128_make64(fr->offset_in_region)));
1176 if (!addrrange_intersects(tmp, fr->addr)) {
1177 continue;
1178 }
1179 tmp = addrrange_intersection(tmp, fr->addr);
1180 qemu_register_coalesced_mmio(int128_get64(tmp.start),
1181 int128_get64(tmp.size));
1182 }
1183 }
1184 }
1185 }
1186
1187 void memory_region_set_coalescing(MemoryRegion *mr)
1188 {
1189 memory_region_clear_coalescing(mr);
1190 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1191 }
1192
1193 void memory_region_add_coalescing(MemoryRegion *mr,
1194 target_phys_addr_t offset,
1195 uint64_t size)
1196 {
1197 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1198
1199 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1200 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1201 memory_region_update_coalesced_range(mr);
1202 }
1203
1204 void memory_region_clear_coalescing(MemoryRegion *mr)
1205 {
1206 CoalescedMemoryRange *cmr;
1207
1208 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1209 cmr = QTAILQ_FIRST(&mr->coalesced);
1210 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1211 g_free(cmr);
1212 }
1213 memory_region_update_coalesced_range(mr);
1214 }
1215
1216 void memory_region_add_eventfd(MemoryRegion *mr,
1217 target_phys_addr_t addr,
1218 unsigned size,
1219 bool match_data,
1220 uint64_t data,
1221 int fd)
1222 {
1223 MemoryRegionIoeventfd mrfd = {
1224 .addr.start = int128_make64(addr),
1225 .addr.size = int128_make64(size),
1226 .match_data = match_data,
1227 .data = data,
1228 .fd = fd,
1229 };
1230 unsigned i;
1231
1232 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1233 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1234 break;
1235 }
1236 }
1237 ++mr->ioeventfd_nb;
1238 mr->ioeventfds = g_realloc(mr->ioeventfds,
1239 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1240 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1241 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1242 mr->ioeventfds[i] = mrfd;
1243 memory_region_update_topology(mr);
1244 }
1245
1246 void memory_region_del_eventfd(MemoryRegion *mr,
1247 target_phys_addr_t addr,
1248 unsigned size,
1249 bool match_data,
1250 uint64_t data,
1251 int fd)
1252 {
1253 MemoryRegionIoeventfd mrfd = {
1254 .addr.start = int128_make64(addr),
1255 .addr.size = int128_make64(size),
1256 .match_data = match_data,
1257 .data = data,
1258 .fd = fd,
1259 };
1260 unsigned i;
1261
1262 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1263 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1264 break;
1265 }
1266 }
1267 assert(i != mr->ioeventfd_nb);
1268 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1269 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1270 --mr->ioeventfd_nb;
1271 mr->ioeventfds = g_realloc(mr->ioeventfds,
1272 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1273 memory_region_update_topology(mr);
1274 }
1275
1276 static void memory_region_add_subregion_common(MemoryRegion *mr,
1277 target_phys_addr_t offset,
1278 MemoryRegion *subregion)
1279 {
1280 MemoryRegion *other;
1281
1282 assert(!subregion->parent);
1283 subregion->parent = mr;
1284 subregion->addr = offset;
1285 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1286 if (subregion->may_overlap || other->may_overlap) {
1287 continue;
1288 }
1289 if (int128_gt(int128_make64(offset),
1290 int128_add(int128_make64(other->addr), other->size))
1291 || int128_le(int128_add(int128_make64(offset), subregion->size),
1292 int128_make64(other->addr))) {
1293 continue;
1294 }
1295 #if 0
1296 printf("warning: subregion collision %llx/%llx (%s) "
1297 "vs %llx/%llx (%s)\n",
1298 (unsigned long long)offset,
1299 (unsigned long long)int128_get64(subregion->size),
1300 subregion->name,
1301 (unsigned long long)other->addr,
1302 (unsigned long long)int128_get64(other->size),
1303 other->name);
1304 #endif
1305 }
1306 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1307 if (subregion->priority >= other->priority) {
1308 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1309 goto done;
1310 }
1311 }
1312 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1313 done:
1314 memory_region_update_topology(mr);
1315 }
1316
1317
1318 void memory_region_add_subregion(MemoryRegion *mr,
1319 target_phys_addr_t offset,
1320 MemoryRegion *subregion)
1321 {
1322 subregion->may_overlap = false;
1323 subregion->priority = 0;
1324 memory_region_add_subregion_common(mr, offset, subregion);
1325 }
1326
1327 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1328 target_phys_addr_t offset,
1329 MemoryRegion *subregion,
1330 unsigned priority)
1331 {
1332 subregion->may_overlap = true;
1333 subregion->priority = priority;
1334 memory_region_add_subregion_common(mr, offset, subregion);
1335 }
1336
1337 void memory_region_del_subregion(MemoryRegion *mr,
1338 MemoryRegion *subregion)
1339 {
1340 assert(subregion->parent == mr);
1341 subregion->parent = NULL;
1342 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1343 memory_region_update_topology(mr);
1344 }
1345
1346 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1347 {
1348 if (enabled == mr->enabled) {
1349 return;
1350 }
1351 mr->enabled = enabled;
1352 memory_region_update_topology(NULL);
1353 }
1354
1355 void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr)
1356 {
1357 MemoryRegion *parent = mr->parent;
1358 unsigned priority = mr->priority;
1359 bool may_overlap = mr->may_overlap;
1360
1361 if (addr == mr->addr || !parent) {
1362 mr->addr = addr;
1363 return;
1364 }
1365
1366 memory_region_transaction_begin();
1367 memory_region_del_subregion(parent, mr);
1368 if (may_overlap) {
1369 memory_region_add_subregion_overlap(parent, addr, mr, priority);
1370 } else {
1371 memory_region_add_subregion(parent, addr, mr);
1372 }
1373 memory_region_transaction_commit();
1374 }
1375
1376 void memory_region_set_alias_offset(MemoryRegion *mr, target_phys_addr_t offset)
1377 {
1378 target_phys_addr_t old_offset = mr->alias_offset;
1379
1380 assert(mr->alias);
1381 mr->alias_offset = offset;
1382
1383 if (offset == old_offset || !mr->parent) {
1384 return;
1385 }
1386
1387 memory_region_update_topology(mr);
1388 }
1389
1390 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr)
1391 {
1392 return mr->ram_addr;
1393 }
1394
1395 static int cmp_flatrange_addr(const void *addr_, const void *fr_)
1396 {
1397 const AddrRange *addr = addr_;
1398 const FlatRange *fr = fr_;
1399
1400 if (int128_le(addrrange_end(*addr), fr->addr.start)) {
1401 return -1;
1402 } else if (int128_ge(addr->start, addrrange_end(fr->addr))) {
1403 return 1;
1404 }
1405 return 0;
1406 }
1407
1408 static FlatRange *address_space_lookup(AddressSpace *as, AddrRange addr)
1409 {
1410 return bsearch(&addr, as->current_map.ranges, as->current_map.nr,
1411 sizeof(FlatRange), cmp_flatrange_addr);
1412 }
1413
1414 MemoryRegionSection memory_region_find(MemoryRegion *address_space,
1415 target_phys_addr_t addr, uint64_t size)
1416 {
1417 AddressSpace *as = memory_region_to_address_space(address_space);
1418 AddrRange range = addrrange_make(int128_make64(addr),
1419 int128_make64(size));
1420 FlatRange *fr = address_space_lookup(as, range);
1421 MemoryRegionSection ret = { .mr = NULL, .size = 0 };
1422
1423 if (!fr) {
1424 return ret;
1425 }
1426
1427 while (fr > as->current_map.ranges
1428 && addrrange_intersects(fr[-1].addr, range)) {
1429 --fr;
1430 }
1431
1432 ret.mr = fr->mr;
1433 range = addrrange_intersection(range, fr->addr);
1434 ret.offset_within_region = fr->offset_in_region;
1435 ret.offset_within_region += int128_get64(int128_sub(range.start,
1436 fr->addr.start));
1437 ret.size = int128_get64(range.size);
1438 ret.offset_within_address_space = int128_get64(range.start);
1439 return ret;
1440 }
1441
1442 void memory_global_sync_dirty_bitmap(MemoryRegion *address_space)
1443 {
1444 AddressSpace *as = memory_region_to_address_space(address_space);
1445 FlatRange *fr;
1446
1447 FOR_EACH_FLAT_RANGE(fr, &as->current_map) {
1448 MEMORY_LISTENER_UPDATE_REGION(fr, as, log_sync);
1449 }
1450 }
1451
1452 void memory_global_dirty_log_start(void)
1453 {
1454 MemoryListener *listener;
1455
1456 cpu_physical_memory_set_dirty_tracking(1);
1457 global_dirty_log = true;
1458 QLIST_FOREACH(listener, &memory_listeners, link) {
1459 listener->log_global_start(listener);
1460 }
1461 }
1462
1463 void memory_global_dirty_log_stop(void)
1464 {
1465 MemoryListener *listener;
1466
1467 global_dirty_log = false;
1468 QLIST_FOREACH(listener, &memory_listeners, link) {
1469 listener->log_global_stop(listener);
1470 }
1471 cpu_physical_memory_set_dirty_tracking(0);
1472 }
1473
1474 static void listener_add_address_space(MemoryListener *listener,
1475 AddressSpace *as)
1476 {
1477 FlatRange *fr;
1478
1479 if (global_dirty_log) {
1480 listener->log_global_start(listener);
1481 }
1482 FOR_EACH_FLAT_RANGE(fr, &as->current_map) {
1483 MemoryRegionSection section = {
1484 .mr = fr->mr,
1485 .address_space = as->root,
1486 .offset_within_region = fr->offset_in_region,
1487 .size = int128_get64(fr->addr.size),
1488 .offset_within_address_space = int128_get64(fr->addr.start),
1489 };
1490 listener->region_add(listener, &section);
1491 }
1492 }
1493
1494 void memory_listener_register(MemoryListener *listener)
1495 {
1496 QLIST_INSERT_HEAD(&memory_listeners, listener, link);
1497 listener_add_address_space(listener, &address_space_memory);
1498 listener_add_address_space(listener, &address_space_io);
1499 }
1500
1501 void memory_listener_unregister(MemoryListener *listener)
1502 {
1503 QLIST_REMOVE(listener, link);
1504 }
1505
1506 void set_system_memory_map(MemoryRegion *mr)
1507 {
1508 address_space_memory.root = mr;
1509 memory_region_update_topology(NULL);
1510 }
1511
1512 void set_system_io_map(MemoryRegion *mr)
1513 {
1514 address_space_io.root = mr;
1515 memory_region_update_topology(NULL);
1516 }
1517
1518 uint64_t io_mem_read(int io_index, target_phys_addr_t addr, unsigned size)
1519 {
1520 return memory_region_dispatch_read(io_mem_region[io_index], addr, size);
1521 }
1522
1523 void io_mem_write(int io_index, target_phys_addr_t addr,
1524 uint64_t val, unsigned size)
1525 {
1526 memory_region_dispatch_write(io_mem_region[io_index], addr, val, size);
1527 }
1528
1529 typedef struct MemoryRegionList MemoryRegionList;
1530
1531 struct MemoryRegionList {
1532 const MemoryRegion *mr;
1533 bool printed;
1534 QTAILQ_ENTRY(MemoryRegionList) queue;
1535 };
1536
1537 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1538
1539 static void mtree_print_mr(fprintf_function mon_printf, void *f,
1540 const MemoryRegion *mr, unsigned int level,
1541 target_phys_addr_t base,
1542 MemoryRegionListHead *alias_print_queue)
1543 {
1544 MemoryRegionList *new_ml, *ml, *next_ml;
1545 MemoryRegionListHead submr_print_queue;
1546 const MemoryRegion *submr;
1547 unsigned int i;
1548
1549 if (!mr) {
1550 return;
1551 }
1552
1553 for (i = 0; i < level; i++) {
1554 mon_printf(f, " ");
1555 }
1556
1557 if (mr->alias) {
1558 MemoryRegionList *ml;
1559 bool found = false;
1560
1561 /* check if the alias is already in the queue */
1562 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
1563 if (ml->mr == mr->alias && !ml->printed) {
1564 found = true;
1565 }
1566 }
1567
1568 if (!found) {
1569 ml = g_new(MemoryRegionList, 1);
1570 ml->mr = mr->alias;
1571 ml->printed = false;
1572 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
1573 }
1574 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): alias %s @%s "
1575 TARGET_FMT_plx "-" TARGET_FMT_plx "\n",
1576 base + mr->addr,
1577 base + mr->addr
1578 + (target_phys_addr_t)int128_get64(mr->size) - 1,
1579 mr->priority,
1580 mr->name,
1581 mr->alias->name,
1582 mr->alias_offset,
1583 mr->alias_offset
1584 + (target_phys_addr_t)int128_get64(mr->size) - 1);
1585 } else {
1586 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): %s\n",
1587 base + mr->addr,
1588 base + mr->addr
1589 + (target_phys_addr_t)int128_get64(mr->size) - 1,
1590 mr->priority,
1591 mr->name);
1592 }
1593
1594 QTAILQ_INIT(&submr_print_queue);
1595
1596 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
1597 new_ml = g_new(MemoryRegionList, 1);
1598 new_ml->mr = submr;
1599 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1600 if (new_ml->mr->addr < ml->mr->addr ||
1601 (new_ml->mr->addr == ml->mr->addr &&
1602 new_ml->mr->priority > ml->mr->priority)) {
1603 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
1604 new_ml = NULL;
1605 break;
1606 }
1607 }
1608 if (new_ml) {
1609 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
1610 }
1611 }
1612
1613 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1614 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
1615 alias_print_queue);
1616 }
1617
1618 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
1619 g_free(ml);
1620 }
1621 }
1622
1623 void mtree_info(fprintf_function mon_printf, void *f)
1624 {
1625 MemoryRegionListHead ml_head;
1626 MemoryRegionList *ml, *ml2;
1627
1628 QTAILQ_INIT(&ml_head);
1629
1630 mon_printf(f, "memory\n");
1631 mtree_print_mr(mon_printf, f, address_space_memory.root, 0, 0, &ml_head);
1632
1633 /* print aliased regions */
1634 QTAILQ_FOREACH(ml, &ml_head, queue) {
1635 if (!ml->printed) {
1636 mon_printf(f, "%s\n", ml->mr->name);
1637 mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head);
1638 }
1639 }
1640
1641 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
1642 g_free(ml);
1643 }
1644
1645 if (address_space_io.root &&
1646 !QTAILQ_EMPTY(&address_space_io.root->subregions)) {
1647 QTAILQ_INIT(&ml_head);
1648 mon_printf(f, "I/O\n");
1649 mtree_print_mr(mon_printf, f, address_space_io.root, 0, 0, &ml_head);
1650 }
1651 }