2 * Physical memory management
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
15 #include "exec-memory.h"
21 #define WANT_EXEC_OBSOLETE
22 #include "exec-obsolete.h"
24 unsigned memory_region_transaction_depth = 0;
25 static bool memory_region_update_pending = false;
27 typedef struct AddrRange AddrRange;
30 * Note using signed integers limits us to physical addresses at most
31 * 63 bits wide. They are needed for negative offsetting in aliases
32 * (large MemoryRegion::alias_offset).
39 static AddrRange addrrange_make(Int128 start, Int128 size)
41 return (AddrRange) { start, size };
44 static bool addrrange_equal(AddrRange r1, AddrRange r2)
46 return int128_eq(r1.start, r2.start) && int128_eq(r1.size, r2.size);
49 static Int128 addrrange_end(AddrRange r)
51 return int128_add(r.start, r.size);
54 static AddrRange addrrange_shift(AddrRange range, Int128 delta)
56 int128_addto(&range.start, delta);
60 static bool addrrange_contains(AddrRange range, Int128 addr)
62 return int128_ge(addr, range.start)
63 && int128_lt(addr, addrrange_end(range));
66 static bool addrrange_intersects(AddrRange r1, AddrRange r2)
68 return addrrange_contains(r1, r2.start)
69 || addrrange_contains(r2, r1.start);
72 static AddrRange addrrange_intersection(AddrRange r1, AddrRange r2)
74 Int128 start = int128_max(r1.start, r2.start);
75 Int128 end = int128_min(addrrange_end(r1), addrrange_end(r2));
76 return addrrange_make(start, int128_sub(end, start));
79 struct CoalescedMemoryRange {
81 QTAILQ_ENTRY(CoalescedMemoryRange) link;
84 struct MemoryRegionIoeventfd {
91 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a,
92 MemoryRegionIoeventfd b)
94 if (int128_lt(a.addr.start, b.addr.start)) {
96 } else if (int128_gt(a.addr.start, b.addr.start)) {
98 } else if (int128_lt(a.addr.size, b.addr.size)) {
100 } else if (int128_gt(a.addr.size, b.addr.size)) {
102 } else if (a.match_data < b.match_data) {
104 } else if (a.match_data > b.match_data) {
106 } else if (a.match_data) {
107 if (a.data < b.data) {
109 } else if (a.data > b.data) {
115 } else if (a.fd > b.fd) {
121 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a,
122 MemoryRegionIoeventfd b)
124 return !memory_region_ioeventfd_before(a, b)
125 && !memory_region_ioeventfd_before(b, a);
128 typedef struct FlatRange FlatRange;
129 typedef struct FlatView FlatView;
131 /* Range of memory in the global map. Addresses are absolute. */
134 target_phys_addr_t offset_in_region;
136 uint8_t dirty_log_mask;
141 /* Flattened global view of current active memory hierarchy. Kept in sorted
147 unsigned nr_allocated;
150 typedef struct AddressSpace AddressSpace;
151 typedef struct AddressSpaceOps AddressSpaceOps;
153 /* A system address space - I/O, memory, etc. */
154 struct AddressSpace {
155 const AddressSpaceOps *ops;
157 FlatView current_map;
159 MemoryRegionIoeventfd *ioeventfds;
162 struct AddressSpaceOps {
163 void (*range_add)(AddressSpace *as, FlatRange *fr);
164 void (*range_del)(AddressSpace *as, FlatRange *fr);
165 void (*log_start)(AddressSpace *as, FlatRange *fr);
166 void (*log_stop)(AddressSpace *as, FlatRange *fr);
167 void (*ioeventfd_add)(AddressSpace *as, MemoryRegionIoeventfd *fd);
168 void (*ioeventfd_del)(AddressSpace *as, MemoryRegionIoeventfd *fd);
171 #define FOR_EACH_FLAT_RANGE(var, view) \
172 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
174 static bool flatrange_equal(FlatRange *a, FlatRange *b)
176 return a->mr == b->mr
177 && addrrange_equal(a->addr, b->addr)
178 && a->offset_in_region == b->offset_in_region
179 && a->readable == b->readable
180 && a->readonly == b->readonly;
183 static void flatview_init(FlatView *view)
187 view->nr_allocated = 0;
190 /* Insert a range into a given position. Caller is responsible for maintaining
193 static void flatview_insert(FlatView *view, unsigned pos, FlatRange *range)
195 if (view->nr == view->nr_allocated) {
196 view->nr_allocated = MAX(2 * view->nr, 10);
197 view->ranges = g_realloc(view->ranges,
198 view->nr_allocated * sizeof(*view->ranges));
200 memmove(view->ranges + pos + 1, view->ranges + pos,
201 (view->nr - pos) * sizeof(FlatRange));
202 view->ranges[pos] = *range;
206 static void flatview_destroy(FlatView *view)
208 g_free(view->ranges);
211 static bool can_merge(FlatRange *r1, FlatRange *r2)
213 return int128_eq(addrrange_end(r1->addr), r2->addr.start)
215 && int128_eq(int128_add(int128_make64(r1->offset_in_region),
217 int128_make64(r2->offset_in_region))
218 && r1->dirty_log_mask == r2->dirty_log_mask
219 && r1->readable == r2->readable
220 && r1->readonly == r2->readonly;
223 /* Attempt to simplify a view by merging ajacent ranges */
224 static void flatview_simplify(FlatView *view)
229 while (i < view->nr) {
232 && can_merge(&view->ranges[j-1], &view->ranges[j])) {
233 int128_addto(&view->ranges[i].addr.size, view->ranges[j].addr.size);
237 memmove(&view->ranges[i], &view->ranges[j],
238 (view->nr - j) * sizeof(view->ranges[j]));
243 static void memory_region_read_accessor(void *opaque,
244 target_phys_addr_t addr,
250 MemoryRegion *mr = opaque;
253 tmp = mr->ops->read(mr->opaque, addr, size);
254 *value |= (tmp & mask) << shift;
257 static void memory_region_write_accessor(void *opaque,
258 target_phys_addr_t addr,
264 MemoryRegion *mr = opaque;
267 tmp = (*value >> shift) & mask;
268 mr->ops->write(mr->opaque, addr, tmp, size);
271 static void access_with_adjusted_size(target_phys_addr_t addr,
274 unsigned access_size_min,
275 unsigned access_size_max,
276 void (*access)(void *opaque,
277 target_phys_addr_t addr,
284 uint64_t access_mask;
285 unsigned access_size;
288 if (!access_size_min) {
291 if (!access_size_max) {
294 access_size = MAX(MIN(size, access_size_max), access_size_min);
295 access_mask = -1ULL >> (64 - access_size * 8);
296 for (i = 0; i < size; i += access_size) {
297 /* FIXME: big-endian support */
298 access(opaque, addr + i, value, access_size, i * 8, access_mask);
302 static void memory_region_prepare_ram_addr(MemoryRegion *mr);
304 static void as_memory_range_add(AddressSpace *as, FlatRange *fr)
306 ram_addr_t phys_offset, region_offset;
308 memory_region_prepare_ram_addr(fr->mr);
310 phys_offset = fr->mr->ram_addr;
311 region_offset = fr->offset_in_region;
312 /* cpu_register_physical_memory_log() wants region_offset for
313 * mmio, but prefers offseting phys_offset for RAM. Humour it.
315 if ((phys_offset & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
316 phys_offset += region_offset;
321 phys_offset &= ~TARGET_PAGE_MASK & ~IO_MEM_ROMD;
325 phys_offset |= IO_MEM_ROM;
328 cpu_register_physical_memory_log(int128_get64(fr->addr.start),
329 int128_get64(fr->addr.size),
335 static void as_memory_range_del(AddressSpace *as, FlatRange *fr)
337 if (fr->dirty_log_mask) {
338 Int128 end = addrrange_end(fr->addr);
339 cpu_physical_sync_dirty_bitmap(int128_get64(fr->addr.start),
342 cpu_register_physical_memory(int128_get64(fr->addr.start),
343 int128_get64(fr->addr.size),
347 static void as_memory_log_start(AddressSpace *as, FlatRange *fr)
349 cpu_physical_log_start(int128_get64(fr->addr.start),
350 int128_get64(fr->addr.size));
353 static void as_memory_log_stop(AddressSpace *as, FlatRange *fr)
355 cpu_physical_log_stop(int128_get64(fr->addr.start),
356 int128_get64(fr->addr.size));
359 static void as_memory_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd)
363 assert(fd->match_data && int128_get64(fd->addr.size) == 4);
365 r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start),
372 static void as_memory_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd)
376 r = kvm_set_ioeventfd_mmio_long(fd->fd, int128_get64(fd->addr.start),
383 static const AddressSpaceOps address_space_ops_memory = {
384 .range_add = as_memory_range_add,
385 .range_del = as_memory_range_del,
386 .log_start = as_memory_log_start,
387 .log_stop = as_memory_log_stop,
388 .ioeventfd_add = as_memory_ioeventfd_add,
389 .ioeventfd_del = as_memory_ioeventfd_del,
392 static AddressSpace address_space_memory = {
393 .ops = &address_space_ops_memory,
396 static const MemoryRegionPortio *find_portio(MemoryRegion *mr, uint64_t offset,
397 unsigned width, bool write)
399 const MemoryRegionPortio *mrp;
401 for (mrp = mr->ops->old_portio; mrp->size; ++mrp) {
402 if (offset >= mrp->offset && offset < mrp->offset + mrp->len
403 && width == mrp->size
404 && (write ? (bool)mrp->write : (bool)mrp->read)) {
411 static void memory_region_iorange_read(IORange *iorange,
416 MemoryRegion *mr = container_of(iorange, MemoryRegion, iorange);
418 if (mr->ops->old_portio) {
419 const MemoryRegionPortio *mrp = find_portio(mr, offset, width, false);
421 *data = ((uint64_t)1 << (width * 8)) - 1;
423 *data = mrp->read(mr->opaque, offset + mr->offset);
424 } else if (width == 2) {
425 mrp = find_portio(mr, offset, 1, false);
427 *data = mrp->read(mr->opaque, offset + mr->offset) |
428 (mrp->read(mr->opaque, offset + mr->offset + 1) << 8);
433 access_with_adjusted_size(offset + mr->offset, data, width,
434 mr->ops->impl.min_access_size,
435 mr->ops->impl.max_access_size,
436 memory_region_read_accessor, mr);
439 static void memory_region_iorange_write(IORange *iorange,
444 MemoryRegion *mr = container_of(iorange, MemoryRegion, iorange);
446 if (mr->ops->old_portio) {
447 const MemoryRegionPortio *mrp = find_portio(mr, offset, width, true);
450 mrp->write(mr->opaque, offset + mr->offset, data);
451 } else if (width == 2) {
452 mrp = find_portio(mr, offset, 1, false);
454 mrp->write(mr->opaque, offset + mr->offset, data & 0xff);
455 mrp->write(mr->opaque, offset + mr->offset + 1, data >> 8);
459 access_with_adjusted_size(offset + mr->offset, &data, width,
460 mr->ops->impl.min_access_size,
461 mr->ops->impl.max_access_size,
462 memory_region_write_accessor, mr);
465 static const IORangeOps memory_region_iorange_ops = {
466 .read = memory_region_iorange_read,
467 .write = memory_region_iorange_write,
470 static void as_io_range_add(AddressSpace *as, FlatRange *fr)
472 iorange_init(&fr->mr->iorange, &memory_region_iorange_ops,
473 int128_get64(fr->addr.start), int128_get64(fr->addr.size));
474 ioport_register(&fr->mr->iorange);
477 static void as_io_range_del(AddressSpace *as, FlatRange *fr)
479 isa_unassign_ioport(int128_get64(fr->addr.start),
480 int128_get64(fr->addr.size));
483 static void as_io_ioeventfd_add(AddressSpace *as, MemoryRegionIoeventfd *fd)
487 assert(fd->match_data && int128_get64(fd->addr.size) == 2);
489 r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start),
496 static void as_io_ioeventfd_del(AddressSpace *as, MemoryRegionIoeventfd *fd)
500 r = kvm_set_ioeventfd_pio_word(fd->fd, int128_get64(fd->addr.start),
507 static const AddressSpaceOps address_space_ops_io = {
508 .range_add = as_io_range_add,
509 .range_del = as_io_range_del,
510 .ioeventfd_add = as_io_ioeventfd_add,
511 .ioeventfd_del = as_io_ioeventfd_del,
514 static AddressSpace address_space_io = {
515 .ops = &address_space_ops_io,
518 /* Render a memory region into the global view. Ranges in @view obscure
521 static void render_memory_region(FlatView *view,
527 MemoryRegion *subregion;
529 target_phys_addr_t offset_in_region;
539 int128_addto(&base, int128_make64(mr->addr));
540 readonly |= mr->readonly;
542 tmp = addrrange_make(base, mr->size);
544 if (!addrrange_intersects(tmp, clip)) {
548 clip = addrrange_intersection(tmp, clip);
551 int128_subfrom(&base, int128_make64(mr->alias->addr));
552 int128_subfrom(&base, int128_make64(mr->alias_offset));
553 render_memory_region(view, mr->alias, base, clip, readonly);
557 /* Render subregions in priority order. */
558 QTAILQ_FOREACH(subregion, &mr->subregions, subregions_link) {
559 render_memory_region(view, subregion, base, clip, readonly);
562 if (!mr->terminates) {
566 offset_in_region = int128_get64(int128_sub(clip.start, base));
570 /* Render the region itself into any gaps left by the current view. */
571 for (i = 0; i < view->nr && int128_nz(remain); ++i) {
572 if (int128_ge(base, addrrange_end(view->ranges[i].addr))) {
575 if (int128_lt(base, view->ranges[i].addr.start)) {
576 now = int128_min(remain,
577 int128_sub(view->ranges[i].addr.start, base));
579 fr.offset_in_region = offset_in_region;
580 fr.addr = addrrange_make(base, now);
581 fr.dirty_log_mask = mr->dirty_log_mask;
582 fr.readable = mr->readable;
583 fr.readonly = readonly;
584 flatview_insert(view, i, &fr);
586 int128_addto(&base, now);
587 offset_in_region += int128_get64(now);
588 int128_subfrom(&remain, now);
590 if (int128_eq(base, view->ranges[i].addr.start)) {
591 now = int128_min(remain, view->ranges[i].addr.size);
592 int128_addto(&base, now);
593 offset_in_region += int128_get64(now);
594 int128_subfrom(&remain, now);
597 if (int128_nz(remain)) {
599 fr.offset_in_region = offset_in_region;
600 fr.addr = addrrange_make(base, remain);
601 fr.dirty_log_mask = mr->dirty_log_mask;
602 fr.readable = mr->readable;
603 fr.readonly = readonly;
604 flatview_insert(view, i, &fr);
608 /* Render a memory topology into a list of disjoint absolute ranges. */
609 static FlatView generate_memory_topology(MemoryRegion *mr)
613 flatview_init(&view);
615 render_memory_region(&view, mr, int128_zero(),
616 addrrange_make(int128_zero(), int128_2_64()), false);
617 flatview_simplify(&view);
622 static void address_space_add_del_ioeventfds(AddressSpace *as,
623 MemoryRegionIoeventfd *fds_new,
625 MemoryRegionIoeventfd *fds_old,
630 /* Generate a symmetric difference of the old and new fd sets, adding
631 * and deleting as necessary.
635 while (iold < fds_old_nb || inew < fds_new_nb) {
636 if (iold < fds_old_nb
637 && (inew == fds_new_nb
638 || memory_region_ioeventfd_before(fds_old[iold],
640 as->ops->ioeventfd_del(as, &fds_old[iold]);
642 } else if (inew < fds_new_nb
643 && (iold == fds_old_nb
644 || memory_region_ioeventfd_before(fds_new[inew],
646 as->ops->ioeventfd_add(as, &fds_new[inew]);
655 static void address_space_update_ioeventfds(AddressSpace *as)
658 unsigned ioeventfd_nb = 0;
659 MemoryRegionIoeventfd *ioeventfds = NULL;
663 FOR_EACH_FLAT_RANGE(fr, &as->current_map) {
664 for (i = 0; i < fr->mr->ioeventfd_nb; ++i) {
665 tmp = addrrange_shift(fr->mr->ioeventfds[i].addr,
666 int128_sub(fr->addr.start,
667 int128_make64(fr->offset_in_region)));
668 if (addrrange_intersects(fr->addr, tmp)) {
670 ioeventfds = g_realloc(ioeventfds,
671 ioeventfd_nb * sizeof(*ioeventfds));
672 ioeventfds[ioeventfd_nb-1] = fr->mr->ioeventfds[i];
673 ioeventfds[ioeventfd_nb-1].addr = tmp;
678 address_space_add_del_ioeventfds(as, ioeventfds, ioeventfd_nb,
679 as->ioeventfds, as->ioeventfd_nb);
681 g_free(as->ioeventfds);
682 as->ioeventfds = ioeventfds;
683 as->ioeventfd_nb = ioeventfd_nb;
686 static void address_space_update_topology_pass(AddressSpace *as,
692 FlatRange *frold, *frnew;
694 /* Generate a symmetric difference of the old and new memory maps.
695 * Kill ranges in the old map, and instantiate ranges in the new map.
698 while (iold < old_view.nr || inew < new_view.nr) {
699 if (iold < old_view.nr) {
700 frold = &old_view.ranges[iold];
704 if (inew < new_view.nr) {
705 frnew = &new_view.ranges[inew];
712 || int128_lt(frold->addr.start, frnew->addr.start)
713 || (int128_eq(frold->addr.start, frnew->addr.start)
714 && !flatrange_equal(frold, frnew)))) {
715 /* In old, but (not in new, or in new but attributes changed). */
718 as->ops->range_del(as, frold);
722 } else if (frold && frnew && flatrange_equal(frold, frnew)) {
723 /* In both (logging may have changed) */
726 if (frold->dirty_log_mask && !frnew->dirty_log_mask) {
727 as->ops->log_stop(as, frnew);
728 } else if (frnew->dirty_log_mask && !frold->dirty_log_mask) {
729 as->ops->log_start(as, frnew);
739 as->ops->range_add(as, frnew);
748 static void address_space_update_topology(AddressSpace *as)
750 FlatView old_view = as->current_map;
751 FlatView new_view = generate_memory_topology(as->root);
753 address_space_update_topology_pass(as, old_view, new_view, false);
754 address_space_update_topology_pass(as, old_view, new_view, true);
756 as->current_map = new_view;
757 flatview_destroy(&old_view);
758 address_space_update_ioeventfds(as);
761 static void memory_region_update_topology(MemoryRegion *mr)
763 if (memory_region_transaction_depth) {
764 memory_region_update_pending |= !mr || mr->enabled;
768 if (mr && !mr->enabled) {
772 if (address_space_memory.root) {
773 address_space_update_topology(&address_space_memory);
775 if (address_space_io.root) {
776 address_space_update_topology(&address_space_io);
779 memory_region_update_pending = false;
782 void memory_region_transaction_begin(void)
784 ++memory_region_transaction_depth;
787 void memory_region_transaction_commit(void)
789 assert(memory_region_transaction_depth);
790 --memory_region_transaction_depth;
791 if (!memory_region_transaction_depth && memory_region_update_pending) {
792 memory_region_update_topology(NULL);
796 static void memory_region_destructor_none(MemoryRegion *mr)
800 static void memory_region_destructor_ram(MemoryRegion *mr)
802 qemu_ram_free(mr->ram_addr);
805 static void memory_region_destructor_ram_from_ptr(MemoryRegion *mr)
807 qemu_ram_free_from_ptr(mr->ram_addr);
810 static void memory_region_destructor_iomem(MemoryRegion *mr)
812 cpu_unregister_io_memory(mr->ram_addr);
815 static void memory_region_destructor_rom_device(MemoryRegion *mr)
817 qemu_ram_free(mr->ram_addr & TARGET_PAGE_MASK);
818 cpu_unregister_io_memory(mr->ram_addr & ~(TARGET_PAGE_MASK | IO_MEM_ROMD));
821 void memory_region_init(MemoryRegion *mr,
827 mr->size = int128_make64(size);
828 if (size == UINT64_MAX) {
829 mr->size = int128_2_64();
834 mr->terminates = false;
836 mr->readonly = false;
837 mr->destructor = memory_region_destructor_none;
839 mr->may_overlap = false;
841 QTAILQ_INIT(&mr->subregions);
842 memset(&mr->subregions_link, 0, sizeof mr->subregions_link);
843 QTAILQ_INIT(&mr->coalesced);
844 mr->name = g_strdup(name);
845 mr->dirty_log_mask = 0;
846 mr->ioeventfd_nb = 0;
847 mr->ioeventfds = NULL;
850 static bool memory_region_access_valid(MemoryRegion *mr,
851 target_phys_addr_t addr,
855 if (mr->ops->valid.accepts
856 && !mr->ops->valid.accepts(mr->opaque, addr, size, is_write)) {
860 if (!mr->ops->valid.unaligned && (addr & (size - 1))) {
864 /* Treat zero as compatibility all valid */
865 if (!mr->ops->valid.max_access_size) {
869 if (size > mr->ops->valid.max_access_size
870 || size < mr->ops->valid.min_access_size) {
876 static uint32_t memory_region_read_thunk_n(void *_mr,
877 target_phys_addr_t addr,
880 MemoryRegion *mr = _mr;
883 if (!memory_region_access_valid(mr, addr, size, false)) {
884 return -1U; /* FIXME: better signalling */
887 if (!mr->ops->read) {
888 return mr->ops->old_mmio.read[bitops_ffsl(size)](mr->opaque, addr);
891 /* FIXME: support unaligned access */
892 access_with_adjusted_size(addr + mr->offset, &data, size,
893 mr->ops->impl.min_access_size,
894 mr->ops->impl.max_access_size,
895 memory_region_read_accessor, mr);
900 static void memory_region_write_thunk_n(void *_mr,
901 target_phys_addr_t addr,
905 MemoryRegion *mr = _mr;
907 if (!memory_region_access_valid(mr, addr, size, true)) {
908 return; /* FIXME: better signalling */
911 if (!mr->ops->write) {
912 mr->ops->old_mmio.write[bitops_ffsl(size)](mr->opaque, addr, data);
916 /* FIXME: support unaligned access */
917 access_with_adjusted_size(addr + mr->offset, &data, size,
918 mr->ops->impl.min_access_size,
919 mr->ops->impl.max_access_size,
920 memory_region_write_accessor, mr);
923 static uint32_t memory_region_read_thunk_b(void *mr, target_phys_addr_t addr)
925 return memory_region_read_thunk_n(mr, addr, 1);
928 static uint32_t memory_region_read_thunk_w(void *mr, target_phys_addr_t addr)
930 return memory_region_read_thunk_n(mr, addr, 2);
933 static uint32_t memory_region_read_thunk_l(void *mr, target_phys_addr_t addr)
935 return memory_region_read_thunk_n(mr, addr, 4);
938 static void memory_region_write_thunk_b(void *mr, target_phys_addr_t addr,
941 memory_region_write_thunk_n(mr, addr, 1, data);
944 static void memory_region_write_thunk_w(void *mr, target_phys_addr_t addr,
947 memory_region_write_thunk_n(mr, addr, 2, data);
950 static void memory_region_write_thunk_l(void *mr, target_phys_addr_t addr,
953 memory_region_write_thunk_n(mr, addr, 4, data);
956 static CPUReadMemoryFunc * const memory_region_read_thunk[] = {
957 memory_region_read_thunk_b,
958 memory_region_read_thunk_w,
959 memory_region_read_thunk_l,
962 static CPUWriteMemoryFunc * const memory_region_write_thunk[] = {
963 memory_region_write_thunk_b,
964 memory_region_write_thunk_w,
965 memory_region_write_thunk_l,
968 static void memory_region_prepare_ram_addr(MemoryRegion *mr)
970 if (mr->backend_registered) {
974 mr->destructor = memory_region_destructor_iomem;
975 mr->ram_addr = cpu_register_io_memory(memory_region_read_thunk,
976 memory_region_write_thunk,
978 mr->ops->endianness);
979 mr->backend_registered = true;
982 void memory_region_init_io(MemoryRegion *mr,
983 const MemoryRegionOps *ops,
988 memory_region_init(mr, name, size);
991 mr->terminates = true;
992 mr->backend_registered = false;
995 void memory_region_init_ram(MemoryRegion *mr,
1000 memory_region_init(mr, name, size);
1001 mr->terminates = true;
1002 mr->destructor = memory_region_destructor_ram;
1003 mr->ram_addr = qemu_ram_alloc(dev, name, size, mr);
1004 mr->backend_registered = true;
1007 void memory_region_init_ram_ptr(MemoryRegion *mr,
1013 memory_region_init(mr, name, size);
1014 mr->terminates = true;
1015 mr->destructor = memory_region_destructor_ram_from_ptr;
1016 mr->ram_addr = qemu_ram_alloc_from_ptr(dev, name, size, ptr, mr);
1017 mr->backend_registered = true;
1020 void memory_region_init_alias(MemoryRegion *mr,
1023 target_phys_addr_t offset,
1026 memory_region_init(mr, name, size);
1028 mr->alias_offset = offset;
1031 void memory_region_init_rom_device(MemoryRegion *mr,
1032 const MemoryRegionOps *ops,
1038 memory_region_init(mr, name, size);
1040 mr->opaque = opaque;
1041 mr->terminates = true;
1042 mr->destructor = memory_region_destructor_rom_device;
1043 mr->ram_addr = qemu_ram_alloc(dev, name, size, mr);
1044 mr->ram_addr |= cpu_register_io_memory(memory_region_read_thunk,
1045 memory_region_write_thunk,
1047 mr->ops->endianness);
1048 mr->ram_addr |= IO_MEM_ROMD;
1049 mr->backend_registered = true;
1052 void memory_region_destroy(MemoryRegion *mr)
1054 assert(QTAILQ_EMPTY(&mr->subregions));
1056 memory_region_clear_coalescing(mr);
1057 g_free((char *)mr->name);
1058 g_free(mr->ioeventfds);
1061 uint64_t memory_region_size(MemoryRegion *mr)
1063 if (int128_eq(mr->size, int128_2_64())) {
1066 return int128_get64(mr->size);
1069 void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset)
1071 mr->offset = offset;
1074 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client)
1076 uint8_t mask = 1 << client;
1078 mr->dirty_log_mask = (mr->dirty_log_mask & ~mask) | (log * mask);
1079 memory_region_update_topology(mr);
1082 bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1085 assert(mr->terminates);
1086 return cpu_physical_memory_get_dirty(mr->ram_addr + addr, 1 << client);
1089 void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr)
1091 assert(mr->terminates);
1092 return cpu_physical_memory_set_dirty(mr->ram_addr + addr);
1095 void memory_region_sync_dirty_bitmap(MemoryRegion *mr)
1099 FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) {
1101 cpu_physical_sync_dirty_bitmap(int128_get64(fr->addr.start),
1102 int128_get64(addrrange_end(fr->addr)));
1107 void memory_region_set_readonly(MemoryRegion *mr, bool readonly)
1109 if (mr->readonly != readonly) {
1110 mr->readonly = readonly;
1111 memory_region_update_topology(mr);
1115 void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable)
1117 if (mr->readable != readable) {
1118 mr->readable = readable;
1119 memory_region_update_topology(mr);
1123 void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
1124 target_phys_addr_t size, unsigned client)
1126 assert(mr->terminates);
1127 cpu_physical_memory_reset_dirty(mr->ram_addr + addr,
1128 mr->ram_addr + addr + size,
1132 void *memory_region_get_ram_ptr(MemoryRegion *mr)
1135 return memory_region_get_ram_ptr(mr->alias) + mr->alias_offset;
1138 assert(mr->terminates);
1140 return qemu_get_ram_ptr(mr->ram_addr & TARGET_PAGE_MASK);
1143 static void memory_region_update_coalesced_range(MemoryRegion *mr)
1146 CoalescedMemoryRange *cmr;
1149 FOR_EACH_FLAT_RANGE(fr, &address_space_memory.current_map) {
1151 qemu_unregister_coalesced_mmio(int128_get64(fr->addr.start),
1152 int128_get64(fr->addr.size));
1153 QTAILQ_FOREACH(cmr, &mr->coalesced, link) {
1154 tmp = addrrange_shift(cmr->addr,
1155 int128_sub(fr->addr.start,
1156 int128_make64(fr->offset_in_region)));
1157 if (!addrrange_intersects(tmp, fr->addr)) {
1160 tmp = addrrange_intersection(tmp, fr->addr);
1161 qemu_register_coalesced_mmio(int128_get64(tmp.start),
1162 int128_get64(tmp.size));
1168 void memory_region_set_coalescing(MemoryRegion *mr)
1170 memory_region_clear_coalescing(mr);
1171 memory_region_add_coalescing(mr, 0, int128_get64(mr->size));
1174 void memory_region_add_coalescing(MemoryRegion *mr,
1175 target_phys_addr_t offset,
1178 CoalescedMemoryRange *cmr = g_malloc(sizeof(*cmr));
1180 cmr->addr = addrrange_make(int128_make64(offset), int128_make64(size));
1181 QTAILQ_INSERT_TAIL(&mr->coalesced, cmr, link);
1182 memory_region_update_coalesced_range(mr);
1185 void memory_region_clear_coalescing(MemoryRegion *mr)
1187 CoalescedMemoryRange *cmr;
1189 while (!QTAILQ_EMPTY(&mr->coalesced)) {
1190 cmr = QTAILQ_FIRST(&mr->coalesced);
1191 QTAILQ_REMOVE(&mr->coalesced, cmr, link);
1194 memory_region_update_coalesced_range(mr);
1197 void memory_region_add_eventfd(MemoryRegion *mr,
1198 target_phys_addr_t addr,
1204 MemoryRegionIoeventfd mrfd = {
1205 .addr.start = int128_make64(addr),
1206 .addr.size = int128_make64(size),
1207 .match_data = match_data,
1213 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1214 if (memory_region_ioeventfd_before(mrfd, mr->ioeventfds[i])) {
1219 mr->ioeventfds = g_realloc(mr->ioeventfds,
1220 sizeof(*mr->ioeventfds) * mr->ioeventfd_nb);
1221 memmove(&mr->ioeventfds[i+1], &mr->ioeventfds[i],
1222 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb-1 - i));
1223 mr->ioeventfds[i] = mrfd;
1224 memory_region_update_topology(mr);
1227 void memory_region_del_eventfd(MemoryRegion *mr,
1228 target_phys_addr_t addr,
1234 MemoryRegionIoeventfd mrfd = {
1235 .addr.start = int128_make64(addr),
1236 .addr.size = int128_make64(size),
1237 .match_data = match_data,
1243 for (i = 0; i < mr->ioeventfd_nb; ++i) {
1244 if (memory_region_ioeventfd_equal(mrfd, mr->ioeventfds[i])) {
1248 assert(i != mr->ioeventfd_nb);
1249 memmove(&mr->ioeventfds[i], &mr->ioeventfds[i+1],
1250 sizeof(*mr->ioeventfds) * (mr->ioeventfd_nb - (i+1)));
1252 mr->ioeventfds = g_realloc(mr->ioeventfds,
1253 sizeof(*mr->ioeventfds)*mr->ioeventfd_nb + 1);
1254 memory_region_update_topology(mr);
1257 static void memory_region_add_subregion_common(MemoryRegion *mr,
1258 target_phys_addr_t offset,
1259 MemoryRegion *subregion)
1261 MemoryRegion *other;
1263 assert(!subregion->parent);
1264 subregion->parent = mr;
1265 subregion->addr = offset;
1266 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1267 if (subregion->may_overlap || other->may_overlap) {
1270 if (int128_gt(int128_make64(offset),
1271 int128_add(int128_make64(other->addr), other->size))
1272 || int128_le(int128_add(int128_make64(offset), subregion->size),
1273 int128_make64(other->addr))) {
1277 printf("warning: subregion collision %llx/%llx (%s) "
1278 "vs %llx/%llx (%s)\n",
1279 (unsigned long long)offset,
1280 (unsigned long long)int128_get64(subregion->size),
1282 (unsigned long long)other->addr,
1283 (unsigned long long)int128_get64(other->size),
1287 QTAILQ_FOREACH(other, &mr->subregions, subregions_link) {
1288 if (subregion->priority >= other->priority) {
1289 QTAILQ_INSERT_BEFORE(other, subregion, subregions_link);
1293 QTAILQ_INSERT_TAIL(&mr->subregions, subregion, subregions_link);
1295 memory_region_update_topology(mr);
1299 void memory_region_add_subregion(MemoryRegion *mr,
1300 target_phys_addr_t offset,
1301 MemoryRegion *subregion)
1303 subregion->may_overlap = false;
1304 subregion->priority = 0;
1305 memory_region_add_subregion_common(mr, offset, subregion);
1308 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1309 target_phys_addr_t offset,
1310 MemoryRegion *subregion,
1313 subregion->may_overlap = true;
1314 subregion->priority = priority;
1315 memory_region_add_subregion_common(mr, offset, subregion);
1318 void memory_region_del_subregion(MemoryRegion *mr,
1319 MemoryRegion *subregion)
1321 assert(subregion->parent == mr);
1322 subregion->parent = NULL;
1323 QTAILQ_REMOVE(&mr->subregions, subregion, subregions_link);
1324 memory_region_update_topology(mr);
1327 void memory_region_set_enabled(MemoryRegion *mr, bool enabled)
1329 if (enabled == mr->enabled) {
1332 mr->enabled = enabled;
1333 memory_region_update_topology(NULL);
1336 void memory_region_set_address(MemoryRegion *mr, target_phys_addr_t addr)
1338 MemoryRegion *parent = mr->parent;
1339 unsigned priority = mr->priority;
1340 bool may_overlap = mr->may_overlap;
1342 if (addr == mr->addr || !parent) {
1347 memory_region_transaction_begin();
1348 memory_region_del_subregion(parent, mr);
1350 memory_region_add_subregion_overlap(parent, addr, mr, priority);
1352 memory_region_add_subregion(parent, addr, mr);
1354 memory_region_transaction_commit();
1357 void memory_region_set_alias_offset(MemoryRegion *mr, target_phys_addr_t offset)
1359 target_phys_addr_t old_offset = mr->alias_offset;
1362 mr->alias_offset = offset;
1364 if (offset == old_offset || !mr->parent) {
1368 memory_region_update_topology(mr);
1371 void set_system_memory_map(MemoryRegion *mr)
1373 address_space_memory.root = mr;
1374 memory_region_update_topology(NULL);
1377 void set_system_io_map(MemoryRegion *mr)
1379 address_space_io.root = mr;
1380 memory_region_update_topology(NULL);
1383 typedef struct MemoryRegionList MemoryRegionList;
1385 struct MemoryRegionList {
1386 const MemoryRegion *mr;
1388 QTAILQ_ENTRY(MemoryRegionList) queue;
1391 typedef QTAILQ_HEAD(queue, MemoryRegionList) MemoryRegionListHead;
1393 static void mtree_print_mr(fprintf_function mon_printf, void *f,
1394 const MemoryRegion *mr, unsigned int level,
1395 target_phys_addr_t base,
1396 MemoryRegionListHead *alias_print_queue)
1398 MemoryRegionList *new_ml, *ml, *next_ml;
1399 MemoryRegionListHead submr_print_queue;
1400 const MemoryRegion *submr;
1407 for (i = 0; i < level; i++) {
1412 MemoryRegionList *ml;
1415 /* check if the alias is already in the queue */
1416 QTAILQ_FOREACH(ml, alias_print_queue, queue) {
1417 if (ml->mr == mr->alias && !ml->printed) {
1423 ml = g_new(MemoryRegionList, 1);
1425 ml->printed = false;
1426 QTAILQ_INSERT_TAIL(alias_print_queue, ml, queue);
1428 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): alias %s @%s "
1429 TARGET_FMT_plx "-" TARGET_FMT_plx "\n",
1432 + (target_phys_addr_t)int128_get64(mr->size) - 1,
1438 + (target_phys_addr_t)int128_get64(mr->size) - 1);
1440 mon_printf(f, TARGET_FMT_plx "-" TARGET_FMT_plx " (prio %d): %s\n",
1443 + (target_phys_addr_t)int128_get64(mr->size) - 1,
1448 QTAILQ_INIT(&submr_print_queue);
1450 QTAILQ_FOREACH(submr, &mr->subregions, subregions_link) {
1451 new_ml = g_new(MemoryRegionList, 1);
1453 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1454 if (new_ml->mr->addr < ml->mr->addr ||
1455 (new_ml->mr->addr == ml->mr->addr &&
1456 new_ml->mr->priority > ml->mr->priority)) {
1457 QTAILQ_INSERT_BEFORE(ml, new_ml, queue);
1463 QTAILQ_INSERT_TAIL(&submr_print_queue, new_ml, queue);
1467 QTAILQ_FOREACH(ml, &submr_print_queue, queue) {
1468 mtree_print_mr(mon_printf, f, ml->mr, level + 1, base + mr->addr,
1472 QTAILQ_FOREACH_SAFE(ml, &submr_print_queue, queue, next_ml) {
1477 void mtree_info(fprintf_function mon_printf, void *f)
1479 MemoryRegionListHead ml_head;
1480 MemoryRegionList *ml, *ml2;
1482 QTAILQ_INIT(&ml_head);
1484 mon_printf(f, "memory\n");
1485 mtree_print_mr(mon_printf, f, address_space_memory.root, 0, 0, &ml_head);
1487 /* print aliased regions */
1488 QTAILQ_FOREACH(ml, &ml_head, queue) {
1490 mon_printf(f, "%s\n", ml->mr->name);
1491 mtree_print_mr(mon_printf, f, ml->mr, 0, 0, &ml_head);
1495 QTAILQ_FOREACH_SAFE(ml, &ml_head, queue, ml2) {
1499 if (address_space_io.root &&
1500 !QTAILQ_EMPTY(&address_space_io.root->subregions)) {
1501 QTAILQ_INIT(&ml_head);
1502 mon_printf(f, "I/O\n");
1503 mtree_print_mr(mon_printf, f, address_space_io.root, 0, 0, &ml_head);