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 unsigned memory_region_transaction_depth
= 0;
23 typedef struct AddrRange AddrRange
;
26 * Note using signed integers limits us to physical addresses at most
27 * 63 bits wide. They are needed for negative offsetting in aliases
28 * (large MemoryRegion::alias_offset).
35 static AddrRange
addrrange_make(int64_t start
, int64_t size
)
37 return (AddrRange
) { start
, size
};
40 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
42 return r1
.start
== r2
.start
&& r1
.size
== r2
.size
;
45 static int64_t addrrange_end(AddrRange r
)
47 return r
.start
+ r
.size
;
50 static AddrRange
addrrange_shift(AddrRange range
, int64_t delta
)
56 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
58 return (r1
.start
>= r2
.start
&& (r1
.start
- r2
.start
) < r2
.size
)
59 || (r2
.start
>= r1
.start
&& (r2
.start
- r1
.start
) < r1
.size
);
62 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
64 int64_t start
= MAX(r1
.start
, r2
.start
);
65 /* off-by-one arithmetic to prevent overflow */
66 int64_t end
= MIN(addrrange_end(r1
) - 1, addrrange_end(r2
) - 1);
67 return addrrange_make(start
, end
- start
+ 1);
70 struct CoalescedMemoryRange
{
72 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
75 struct MemoryRegionIoeventfd
{
82 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
83 MemoryRegionIoeventfd b
)
85 if (a
.addr
.start
< b
.addr
.start
) {
87 } else if (a
.addr
.start
> b
.addr
.start
) {
89 } else if (a
.addr
.size
< b
.addr
.size
) {
91 } else if (a
.addr
.size
> b
.addr
.size
) {
93 } else if (a
.match_data
< b
.match_data
) {
95 } else if (a
.match_data
> b
.match_data
) {
97 } else if (a
.match_data
) {
98 if (a
.data
< b
.data
) {
100 } else if (a
.data
> b
.data
) {
106 } else if (a
.fd
> b
.fd
) {
112 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
113 MemoryRegionIoeventfd b
)
115 return !memory_region_ioeventfd_before(a
, b
)
116 && !memory_region_ioeventfd_before(b
, a
);
119 typedef struct FlatRange FlatRange
;
120 typedef struct FlatView FlatView
;
122 /* Range of memory in the global map. Addresses are absolute. */
125 target_phys_addr_t offset_in_region
;
127 uint8_t dirty_log_mask
;
132 /* Flattened global view of current active memory hierarchy. Kept in sorted
138 unsigned nr_allocated
;
141 typedef struct AddressSpace AddressSpace
;
142 typedef struct AddressSpaceOps AddressSpaceOps
;
144 /* A system address space - I/O, memory, etc. */
145 struct AddressSpace
{
146 const AddressSpaceOps
*ops
;
148 FlatView current_map
;
150 MemoryRegionIoeventfd
*ioeventfds
;
153 struct AddressSpaceOps
{
154 void (*range_add
)(AddressSpace
*as
, FlatRange
*fr
);
155 void (*range_del
)(AddressSpace
*as
, FlatRange
*fr
);
156 void (*log_start
)(AddressSpace
*as
, FlatRange
*fr
);
157 void (*log_stop
)(AddressSpace
*as
, FlatRange
*fr
);
158 void (*ioeventfd_add
)(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
);
159 void (*ioeventfd_del
)(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
);
162 #define FOR_EACH_FLAT_RANGE(var, view) \
163 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
165 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
167 return a
->mr
== b
->mr
168 && addrrange_equal(a
->addr
, b
->addr
)
169 && a
->offset_in_region
== b
->offset_in_region
170 && a
->readable
== b
->readable
171 && a
->readonly
== b
->readonly
;
174 static void flatview_init(FlatView
*view
)
178 view
->nr_allocated
= 0;
181 /* Insert a range into a given position. Caller is responsible for maintaining
184 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
186 if (view
->nr
== view
->nr_allocated
) {
187 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
188 view
->ranges
= g_realloc(view
->ranges
,
189 view
->nr_allocated
* sizeof(*view
->ranges
));
191 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
192 (view
->nr
- pos
) * sizeof(FlatRange
));
193 view
->ranges
[pos
] = *range
;
197 static void flatview_destroy(FlatView
*view
)
199 g_free(view
->ranges
);
202 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
204 return addrrange_end(r1
->addr
) == r2
->addr
.start
206 && r1
->offset_in_region
+ r1
->addr
.size
== r2
->offset_in_region
207 && r1
->dirty_log_mask
== r2
->dirty_log_mask
208 && r1
->readable
== r2
->readable
209 && r1
->readonly
== r2
->readonly
;
212 /* Attempt to simplify a view by merging ajacent ranges */
213 static void flatview_simplify(FlatView
*view
)
218 while (i
< view
->nr
) {
221 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
222 view
->ranges
[i
].addr
.size
+= view
->ranges
[j
].addr
.size
;
226 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
227 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
232 static void memory_region_read_accessor(void *opaque
,
233 target_phys_addr_t addr
,
239 MemoryRegion
*mr
= opaque
;
242 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
243 *value
|= (tmp
& mask
) << shift
;
246 static void memory_region_write_accessor(void *opaque
,
247 target_phys_addr_t addr
,
253 MemoryRegion
*mr
= opaque
;
256 tmp
= (*value
>> shift
) & mask
;
257 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
260 static void access_with_adjusted_size(target_phys_addr_t addr
,
263 unsigned access_size_min
,
264 unsigned access_size_max
,
265 void (*access
)(void *opaque
,
266 target_phys_addr_t addr
,
273 uint64_t access_mask
;
274 unsigned access_size
;
277 if (!access_size_min
) {
280 if (!access_size_max
) {
283 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
284 access_mask
= -1ULL >> (64 - access_size
* 8);
285 for (i
= 0; i
< size
; i
+= access_size
) {
286 /* FIXME: big-endian support */
287 access(opaque
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
291 static void memory_region_prepare_ram_addr(MemoryRegion
*mr
);
293 static void as_memory_range_add(AddressSpace
*as
, FlatRange
*fr
)
295 ram_addr_t phys_offset
, region_offset
;
297 memory_region_prepare_ram_addr(fr
->mr
);
299 phys_offset
= fr
->mr
->ram_addr
;
300 region_offset
= fr
->offset_in_region
;
301 /* cpu_register_physical_memory_log() wants region_offset for
302 * mmio, but prefers offseting phys_offset for RAM. Humour it.
304 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
) {
305 phys_offset
+= region_offset
;
310 phys_offset
&= ~TARGET_PAGE_MASK
& ~IO_MEM_ROMD
;
314 phys_offset
|= IO_MEM_ROM
;
317 cpu_register_physical_memory_log(fr
->addr
.start
,
324 static void as_memory_range_del(AddressSpace
*as
, FlatRange
*fr
)
326 if (fr
->dirty_log_mask
) {
327 cpu_physical_sync_dirty_bitmap(fr
->addr
.start
,
328 fr
->addr
.start
+ fr
->addr
.size
);
330 cpu_register_physical_memory(fr
->addr
.start
, fr
->addr
.size
,
334 static void as_memory_log_start(AddressSpace
*as
, FlatRange
*fr
)
336 cpu_physical_log_start(fr
->addr
.start
, fr
->addr
.size
);
339 static void as_memory_log_stop(AddressSpace
*as
, FlatRange
*fr
)
341 cpu_physical_log_stop(fr
->addr
.start
, fr
->addr
.size
);
344 static void as_memory_ioeventfd_add(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
348 assert(fd
->match_data
&& fd
->addr
.size
== 4);
350 r
= kvm_set_ioeventfd_mmio_long(fd
->fd
, fd
->addr
.start
, fd
->data
, true);
356 static void as_memory_ioeventfd_del(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
360 r
= kvm_set_ioeventfd_mmio_long(fd
->fd
, fd
->addr
.start
, fd
->data
, false);
366 static const AddressSpaceOps address_space_ops_memory
= {
367 .range_add
= as_memory_range_add
,
368 .range_del
= as_memory_range_del
,
369 .log_start
= as_memory_log_start
,
370 .log_stop
= as_memory_log_stop
,
371 .ioeventfd_add
= as_memory_ioeventfd_add
,
372 .ioeventfd_del
= as_memory_ioeventfd_del
,
375 static AddressSpace address_space_memory
= {
376 .ops
= &address_space_ops_memory
,
379 static const MemoryRegionPortio
*find_portio(MemoryRegion
*mr
, uint64_t offset
,
380 unsigned width
, bool write
)
382 const MemoryRegionPortio
*mrp
;
384 for (mrp
= mr
->ops
->old_portio
; mrp
->size
; ++mrp
) {
385 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
386 && width
== mrp
->size
387 && (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
394 static void memory_region_iorange_read(IORange
*iorange
,
399 MemoryRegion
*mr
= container_of(iorange
, MemoryRegion
, iorange
);
401 if (mr
->ops
->old_portio
) {
402 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
, width
, false);
404 *data
= ((uint64_t)1 << (width
* 8)) - 1;
406 *data
= mrp
->read(mr
->opaque
, offset
);
411 access_with_adjusted_size(offset
, data
, width
,
412 mr
->ops
->impl
.min_access_size
,
413 mr
->ops
->impl
.max_access_size
,
414 memory_region_read_accessor
, mr
);
417 static void memory_region_iorange_write(IORange
*iorange
,
422 MemoryRegion
*mr
= container_of(iorange
, MemoryRegion
, iorange
);
424 if (mr
->ops
->old_portio
) {
425 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
, width
, true);
428 mrp
->write(mr
->opaque
, offset
, data
);
432 access_with_adjusted_size(offset
, &data
, width
,
433 mr
->ops
->impl
.min_access_size
,
434 mr
->ops
->impl
.max_access_size
,
435 memory_region_write_accessor
, mr
);
438 static const IORangeOps memory_region_iorange_ops
= {
439 .read
= memory_region_iorange_read
,
440 .write
= memory_region_iorange_write
,
443 static void as_io_range_add(AddressSpace
*as
, FlatRange
*fr
)
445 iorange_init(&fr
->mr
->iorange
, &memory_region_iorange_ops
,
446 fr
->addr
.start
,fr
->addr
.size
);
447 ioport_register(&fr
->mr
->iorange
);
450 static void as_io_range_del(AddressSpace
*as
, FlatRange
*fr
)
452 isa_unassign_ioport(fr
->addr
.start
, fr
->addr
.size
);
455 static void as_io_ioeventfd_add(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
459 assert(fd
->match_data
&& fd
->addr
.size
== 2);
461 r
= kvm_set_ioeventfd_pio_word(fd
->fd
, fd
->addr
.start
, fd
->data
, true);
467 static void as_io_ioeventfd_del(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
471 r
= kvm_set_ioeventfd_pio_word(fd
->fd
, fd
->addr
.start
, fd
->data
, false);
477 static const AddressSpaceOps address_space_ops_io
= {
478 .range_add
= as_io_range_add
,
479 .range_del
= as_io_range_del
,
480 .ioeventfd_add
= as_io_ioeventfd_add
,
481 .ioeventfd_del
= as_io_ioeventfd_del
,
484 static AddressSpace address_space_io
= {
485 .ops
= &address_space_ops_io
,
488 /* Render a memory region into the global view. Ranges in @view obscure
491 static void render_memory_region(FlatView
*view
,
493 target_phys_addr_t base
,
497 MemoryRegion
*subregion
;
499 target_phys_addr_t offset_in_region
;
506 readonly
|= mr
->readonly
;
508 tmp
= addrrange_make(base
, mr
->size
);
510 if (!addrrange_intersects(tmp
, clip
)) {
514 clip
= addrrange_intersection(tmp
, clip
);
517 base
-= mr
->alias
->addr
;
518 base
-= mr
->alias_offset
;
519 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
523 /* Render subregions in priority order. */
524 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
525 render_memory_region(view
, subregion
, base
, clip
, readonly
);
528 if (!mr
->terminates
) {
532 offset_in_region
= clip
.start
- base
;
536 /* Render the region itself into any gaps left by the current view. */
537 for (i
= 0; i
< view
->nr
&& remain
; ++i
) {
538 if (base
>= addrrange_end(view
->ranges
[i
].addr
)) {
541 if (base
< view
->ranges
[i
].addr
.start
) {
542 now
= MIN(remain
, view
->ranges
[i
].addr
.start
- base
);
544 fr
.offset_in_region
= offset_in_region
;
545 fr
.addr
= addrrange_make(base
, now
);
546 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
547 fr
.readable
= mr
->readable
;
548 fr
.readonly
= readonly
;
549 flatview_insert(view
, i
, &fr
);
552 offset_in_region
+= now
;
555 if (base
== view
->ranges
[i
].addr
.start
) {
556 now
= MIN(remain
, view
->ranges
[i
].addr
.size
);
558 offset_in_region
+= now
;
564 fr
.offset_in_region
= offset_in_region
;
565 fr
.addr
= addrrange_make(base
, remain
);
566 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
567 fr
.readable
= mr
->readable
;
568 fr
.readonly
= readonly
;
569 flatview_insert(view
, i
, &fr
);
573 /* Render a memory topology into a list of disjoint absolute ranges. */
574 static FlatView
generate_memory_topology(MemoryRegion
*mr
)
578 flatview_init(&view
);
580 render_memory_region(&view
, mr
, 0, addrrange_make(0, INT64_MAX
), false);
581 flatview_simplify(&view
);
586 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
587 MemoryRegionIoeventfd
*fds_new
,
589 MemoryRegionIoeventfd
*fds_old
,
594 /* Generate a symmetric difference of the old and new fd sets, adding
595 * and deleting as necessary.
599 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
600 if (iold
< fds_old_nb
601 && (inew
== fds_new_nb
602 || memory_region_ioeventfd_before(fds_old
[iold
],
604 as
->ops
->ioeventfd_del(as
, &fds_old
[iold
]);
606 } else if (inew
< fds_new_nb
607 && (iold
== fds_old_nb
608 || memory_region_ioeventfd_before(fds_new
[inew
],
610 as
->ops
->ioeventfd_add(as
, &fds_new
[inew
]);
619 static void address_space_update_ioeventfds(AddressSpace
*as
)
622 unsigned ioeventfd_nb
= 0;
623 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
627 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
628 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
629 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
630 fr
->addr
.start
- fr
->offset_in_region
);
631 if (addrrange_intersects(fr
->addr
, tmp
)) {
633 ioeventfds
= g_realloc(ioeventfds
,
634 ioeventfd_nb
* sizeof(*ioeventfds
));
635 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
636 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
641 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
642 as
->ioeventfds
, as
->ioeventfd_nb
);
644 g_free(as
->ioeventfds
);
645 as
->ioeventfds
= ioeventfds
;
646 as
->ioeventfd_nb
= ioeventfd_nb
;
649 static void address_space_update_topology_pass(AddressSpace
*as
,
655 FlatRange
*frold
, *frnew
;
657 /* Generate a symmetric difference of the old and new memory maps.
658 * Kill ranges in the old map, and instantiate ranges in the new map.
661 while (iold
< old_view
.nr
|| inew
< new_view
.nr
) {
662 if (iold
< old_view
.nr
) {
663 frold
= &old_view
.ranges
[iold
];
667 if (inew
< new_view
.nr
) {
668 frnew
= &new_view
.ranges
[inew
];
675 || frold
->addr
.start
< frnew
->addr
.start
676 || (frold
->addr
.start
== frnew
->addr
.start
677 && !flatrange_equal(frold
, frnew
)))) {
678 /* In old, but (not in new, or in new but attributes changed). */
681 as
->ops
->range_del(as
, frold
);
685 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
686 /* In both (logging may have changed) */
689 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
690 as
->ops
->log_stop(as
, frnew
);
691 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
692 as
->ops
->log_start(as
, frnew
);
702 as
->ops
->range_add(as
, frnew
);
711 static void address_space_update_topology(AddressSpace
*as
)
713 FlatView old_view
= as
->current_map
;
714 FlatView new_view
= generate_memory_topology(as
->root
);
716 address_space_update_topology_pass(as
, old_view
, new_view
, false);
717 address_space_update_topology_pass(as
, old_view
, new_view
, true);
719 as
->current_map
= new_view
;
720 flatview_destroy(&old_view
);
721 address_space_update_ioeventfds(as
);
724 static void memory_region_update_topology(void)
726 if (memory_region_transaction_depth
) {
730 if (address_space_memory
.root
) {
731 address_space_update_topology(&address_space_memory
);
733 if (address_space_io
.root
) {
734 address_space_update_topology(&address_space_io
);
738 void memory_region_transaction_begin(void)
740 ++memory_region_transaction_depth
;
743 void memory_region_transaction_commit(void)
745 assert(memory_region_transaction_depth
);
746 --memory_region_transaction_depth
;
747 memory_region_update_topology();
750 static void memory_region_destructor_none(MemoryRegion
*mr
)
754 static void memory_region_destructor_ram(MemoryRegion
*mr
)
756 qemu_ram_free(mr
->ram_addr
);
759 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
761 qemu_ram_free_from_ptr(mr
->ram_addr
);
764 static void memory_region_destructor_iomem(MemoryRegion
*mr
)
766 cpu_unregister_io_memory(mr
->ram_addr
);
769 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
771 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
772 cpu_unregister_io_memory(mr
->ram_addr
& ~(TARGET_PAGE_MASK
| IO_MEM_ROMD
));
775 void memory_region_init(MemoryRegion
*mr
,
784 mr
->terminates
= false;
786 mr
->readonly
= false;
787 mr
->destructor
= memory_region_destructor_none
;
789 mr
->may_overlap
= false;
791 QTAILQ_INIT(&mr
->subregions
);
792 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
793 QTAILQ_INIT(&mr
->coalesced
);
794 mr
->name
= g_strdup(name
);
795 mr
->dirty_log_mask
= 0;
796 mr
->ioeventfd_nb
= 0;
797 mr
->ioeventfds
= NULL
;
800 static bool memory_region_access_valid(MemoryRegion
*mr
,
801 target_phys_addr_t addr
,
804 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
808 /* Treat zero as compatibility all valid */
809 if (!mr
->ops
->valid
.max_access_size
) {
813 if (size
> mr
->ops
->valid
.max_access_size
814 || size
< mr
->ops
->valid
.min_access_size
) {
820 static uint32_t memory_region_read_thunk_n(void *_mr
,
821 target_phys_addr_t addr
,
824 MemoryRegion
*mr
= _mr
;
827 if (!memory_region_access_valid(mr
, addr
, size
)) {
828 return -1U; /* FIXME: better signalling */
831 if (!mr
->ops
->read
) {
832 return mr
->ops
->old_mmio
.read
[bitops_ffsl(size
)](mr
->opaque
, addr
);
835 /* FIXME: support unaligned access */
836 access_with_adjusted_size(addr
+ mr
->offset
, &data
, size
,
837 mr
->ops
->impl
.min_access_size
,
838 mr
->ops
->impl
.max_access_size
,
839 memory_region_read_accessor
, mr
);
844 static void memory_region_write_thunk_n(void *_mr
,
845 target_phys_addr_t addr
,
849 MemoryRegion
*mr
= _mr
;
851 if (!memory_region_access_valid(mr
, addr
, size
)) {
852 return; /* FIXME: better signalling */
855 if (!mr
->ops
->write
) {
856 mr
->ops
->old_mmio
.write
[bitops_ffsl(size
)](mr
->opaque
, addr
, data
);
860 /* FIXME: support unaligned access */
861 access_with_adjusted_size(addr
+ mr
->offset
, &data
, size
,
862 mr
->ops
->impl
.min_access_size
,
863 mr
->ops
->impl
.max_access_size
,
864 memory_region_write_accessor
, mr
);
867 static uint32_t memory_region_read_thunk_b(void *mr
, target_phys_addr_t addr
)
869 return memory_region_read_thunk_n(mr
, addr
, 1);
872 static uint32_t memory_region_read_thunk_w(void *mr
, target_phys_addr_t addr
)
874 return memory_region_read_thunk_n(mr
, addr
, 2);
877 static uint32_t memory_region_read_thunk_l(void *mr
, target_phys_addr_t addr
)
879 return memory_region_read_thunk_n(mr
, addr
, 4);
882 static void memory_region_write_thunk_b(void *mr
, target_phys_addr_t addr
,
885 memory_region_write_thunk_n(mr
, addr
, 1, data
);
888 static void memory_region_write_thunk_w(void *mr
, target_phys_addr_t addr
,
891 memory_region_write_thunk_n(mr
, addr
, 2, data
);
894 static void memory_region_write_thunk_l(void *mr
, target_phys_addr_t addr
,
897 memory_region_write_thunk_n(mr
, addr
, 4, data
);
900 static CPUReadMemoryFunc
* const memory_region_read_thunk
[] = {
901 memory_region_read_thunk_b
,
902 memory_region_read_thunk_w
,
903 memory_region_read_thunk_l
,
906 static CPUWriteMemoryFunc
* const memory_region_write_thunk
[] = {
907 memory_region_write_thunk_b
,
908 memory_region_write_thunk_w
,
909 memory_region_write_thunk_l
,
912 static void memory_region_prepare_ram_addr(MemoryRegion
*mr
)
914 if (mr
->backend_registered
) {
918 mr
->destructor
= memory_region_destructor_iomem
;
919 mr
->ram_addr
= cpu_register_io_memory(memory_region_read_thunk
,
920 memory_region_write_thunk
,
922 mr
->ops
->endianness
);
923 mr
->backend_registered
= true;
926 void memory_region_init_io(MemoryRegion
*mr
,
927 const MemoryRegionOps
*ops
,
932 memory_region_init(mr
, name
, size
);
935 mr
->terminates
= true;
936 mr
->backend_registered
= false;
939 void memory_region_init_ram(MemoryRegion
*mr
,
944 memory_region_init(mr
, name
, size
);
945 mr
->terminates
= true;
946 mr
->destructor
= memory_region_destructor_ram
;
947 mr
->ram_addr
= qemu_ram_alloc(dev
, name
, size
);
948 mr
->backend_registered
= true;
951 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
957 memory_region_init(mr
, name
, size
);
958 mr
->terminates
= true;
959 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
960 mr
->ram_addr
= qemu_ram_alloc_from_ptr(dev
, name
, size
, ptr
);
961 mr
->backend_registered
= true;
964 void memory_region_init_alias(MemoryRegion
*mr
,
967 target_phys_addr_t offset
,
970 memory_region_init(mr
, name
, size
);
972 mr
->alias_offset
= offset
;
975 void memory_region_init_rom_device(MemoryRegion
*mr
,
976 const MemoryRegionOps
*ops
,
982 memory_region_init(mr
, name
, size
);
985 mr
->terminates
= true;
986 mr
->destructor
= memory_region_destructor_rom_device
;
987 mr
->ram_addr
= qemu_ram_alloc(dev
, name
, size
);
988 mr
->ram_addr
|= cpu_register_io_memory(memory_region_read_thunk
,
989 memory_region_write_thunk
,
991 mr
->ops
->endianness
);
992 mr
->ram_addr
|= IO_MEM_ROMD
;
993 mr
->backend_registered
= true;
996 void memory_region_destroy(MemoryRegion
*mr
)
998 assert(QTAILQ_EMPTY(&mr
->subregions
));
1000 memory_region_clear_coalescing(mr
);
1001 g_free((char *)mr
->name
);
1002 g_free(mr
->ioeventfds
);
1005 uint64_t memory_region_size(MemoryRegion
*mr
)
1010 void memory_region_set_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1012 mr
->offset
= offset
;
1015 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1017 uint8_t mask
= 1 << client
;
1019 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1020 memory_region_update_topology();
1023 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1026 assert(mr
->terminates
);
1027 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, 1 << client
);
1030 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
)
1032 assert(mr
->terminates
);
1033 return cpu_physical_memory_set_dirty(mr
->ram_addr
+ addr
);
1036 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1040 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1042 cpu_physical_sync_dirty_bitmap(fr
->addr
.start
,
1043 fr
->addr
.start
+ fr
->addr
.size
);
1048 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1050 if (mr
->readonly
!= readonly
) {
1051 mr
->readonly
= readonly
;
1052 memory_region_update_topology();
1056 void memory_region_rom_device_set_readable(MemoryRegion
*mr
, bool readable
)
1058 if (mr
->readable
!= readable
) {
1059 mr
->readable
= readable
;
1060 memory_region_update_topology();
1064 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1065 target_phys_addr_t size
, unsigned client
)
1067 assert(mr
->terminates
);
1068 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1069 mr
->ram_addr
+ addr
+ size
,
1073 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1076 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1079 assert(mr
->terminates
);
1081 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1084 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1087 CoalescedMemoryRange
*cmr
;
1090 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1092 qemu_unregister_coalesced_mmio(fr
->addr
.start
, fr
->addr
.size
);
1093 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1094 tmp
= addrrange_shift(cmr
->addr
,
1095 fr
->addr
.start
- fr
->offset_in_region
);
1096 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1099 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1100 qemu_register_coalesced_mmio(tmp
.start
, tmp
.size
);
1106 void memory_region_set_coalescing(MemoryRegion
*mr
)
1108 memory_region_clear_coalescing(mr
);
1109 memory_region_add_coalescing(mr
, 0, mr
->size
);
1112 void memory_region_add_coalescing(MemoryRegion
*mr
,
1113 target_phys_addr_t offset
,
1116 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1118 cmr
->addr
= addrrange_make(offset
, size
);
1119 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1120 memory_region_update_coalesced_range(mr
);
1123 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1125 CoalescedMemoryRange
*cmr
;
1127 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1128 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1129 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1132 memory_region_update_coalesced_range(mr
);
1135 void memory_region_add_eventfd(MemoryRegion
*mr
,
1136 target_phys_addr_t addr
,
1142 MemoryRegionIoeventfd mrfd
= {
1145 .match_data
= match_data
,
1151 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1152 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1157 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1158 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1159 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1160 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1161 mr
->ioeventfds
[i
] = mrfd
;
1162 memory_region_update_topology();
1165 void memory_region_del_eventfd(MemoryRegion
*mr
,
1166 target_phys_addr_t addr
,
1172 MemoryRegionIoeventfd mrfd
= {
1175 .match_data
= match_data
,
1181 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1182 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1186 assert(i
!= mr
->ioeventfd_nb
);
1187 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1188 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1190 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1191 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1192 memory_region_update_topology();
1195 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1196 target_phys_addr_t offset
,
1197 MemoryRegion
*subregion
)
1199 MemoryRegion
*other
;
1201 assert(!subregion
->parent
);
1202 subregion
->parent
= mr
;
1203 subregion
->addr
= offset
;
1204 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1205 if (subregion
->may_overlap
|| other
->may_overlap
) {
1208 if (offset
>= other
->addr
+ other
->size
1209 || offset
+ subregion
->size
<= other
->addr
) {
1213 printf("warning: subregion collision %llx/%llx (%s) "
1214 "vs %llx/%llx (%s)\n",
1215 (unsigned long long)offset
,
1216 (unsigned long long)subregion
->size
,
1218 (unsigned long long)other
->addr
,
1219 (unsigned long long)other
->size
,
1223 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1224 if (subregion
->priority
>= other
->priority
) {
1225 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1229 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1231 memory_region_update_topology();
1235 void memory_region_add_subregion(MemoryRegion
*mr
,
1236 target_phys_addr_t offset
,
1237 MemoryRegion
*subregion
)
1239 subregion
->may_overlap
= false;
1240 subregion
->priority
= 0;
1241 memory_region_add_subregion_common(mr
, offset
, subregion
);
1244 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1245 target_phys_addr_t offset
,
1246 MemoryRegion
*subregion
,
1249 subregion
->may_overlap
= true;
1250 subregion
->priority
= priority
;
1251 memory_region_add_subregion_common(mr
, offset
, subregion
);
1254 void memory_region_del_subregion(MemoryRegion
*mr
,
1255 MemoryRegion
*subregion
)
1257 assert(subregion
->parent
== mr
);
1258 subregion
->parent
= NULL
;
1259 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1260 memory_region_update_topology();
1263 void set_system_memory_map(MemoryRegion
*mr
)
1265 address_space_memory
.root
= mr
;
1266 memory_region_update_topology();
1269 void set_system_io_map(MemoryRegion
*mr
)
1271 address_space_io
.root
= mr
;
1272 memory_region_update_topology();
1275 typedef struct MemoryRegionList MemoryRegionList
;
1277 struct MemoryRegionList
{
1278 const MemoryRegion
*mr
;
1280 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1283 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1285 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1286 const MemoryRegion
*mr
, unsigned int level
,
1287 target_phys_addr_t base
,
1288 MemoryRegionListHead
*print_queue
)
1290 const MemoryRegion
*submr
;
1298 for (i
= 0; i
< level
; i
++) {
1303 MemoryRegionList
*ml
;
1306 /* check if the alias is already in the queue */
1307 QTAILQ_FOREACH(ml
, print_queue
, queue
) {
1308 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1314 ml
= g_new(MemoryRegionList
, 1);
1316 ml
->printed
= false;
1317 QTAILQ_INSERT_TAIL(print_queue
, ml
, queue
);
1319 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d): alias %s @%s "
1320 TARGET_FMT_plx
"-" TARGET_FMT_plx
"\n",
1322 base
+ mr
->addr
+ (target_phys_addr_t
)mr
->size
- 1,
1327 mr
->alias_offset
+ (target_phys_addr_t
)mr
->size
- 1);
1329 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d): %s\n",
1331 base
+ mr
->addr
+ (target_phys_addr_t
)mr
->size
- 1,
1335 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1336 mtree_print_mr(mon_printf
, f
, submr
, level
+ 1, base
+ mr
->addr
,
1341 void mtree_info(fprintf_function mon_printf
, void *f
)
1343 MemoryRegionListHead ml_head
;
1344 MemoryRegionList
*ml
, *ml2
;
1346 QTAILQ_INIT(&ml_head
);
1348 mon_printf(f
, "memory\n");
1349 mtree_print_mr(mon_printf
, f
, address_space_memory
.root
, 0, 0, &ml_head
);
1351 /* print aliased regions */
1352 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1354 mon_printf(f
, "%s\n", ml
->mr
->name
);
1355 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1359 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
1363 if (address_space_io
.root
&&
1364 !QTAILQ_EMPTY(&address_space_io
.root
->subregions
)) {
1365 QTAILQ_INIT(&ml_head
);
1366 mon_printf(f
, "I/O\n");
1367 mtree_print_mr(mon_printf
, f
, address_space_io
.root
, 0, 0, &ml_head
);