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
;
30 static AddrRange
addrrange_make(uint64_t start
, uint64_t size
)
32 return (AddrRange
) { start
, size
};
35 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
37 return r1
.start
== r2
.start
&& r1
.size
== r2
.size
;
40 static uint64_t addrrange_end(AddrRange r
)
42 return r
.start
+ r
.size
;
45 static AddrRange
addrrange_shift(AddrRange range
, int64_t delta
)
51 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
53 return (r1
.start
>= r2
.start
&& r1
.start
< r2
.start
+ r2
.size
)
54 || (r2
.start
>= r1
.start
&& r2
.start
< r1
.start
+ r1
.size
);
57 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
59 uint64_t start
= MAX(r1
.start
, r2
.start
);
60 /* off-by-one arithmetic to prevent overflow */
61 uint64_t end
= MIN(addrrange_end(r1
) - 1, addrrange_end(r2
) - 1);
62 return addrrange_make(start
, end
- start
+ 1);
65 struct CoalescedMemoryRange
{
67 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
70 struct MemoryRegionIoeventfd
{
77 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
78 MemoryRegionIoeventfd b
)
80 if (a
.addr
.start
< b
.addr
.start
) {
82 } else if (a
.addr
.start
> b
.addr
.start
) {
84 } else if (a
.addr
.size
< b
.addr
.size
) {
86 } else if (a
.addr
.size
> b
.addr
.size
) {
88 } else if (a
.match_data
< b
.match_data
) {
90 } else if (a
.match_data
> b
.match_data
) {
92 } else if (a
.match_data
) {
93 if (a
.data
< b
.data
) {
95 } else if (a
.data
> b
.data
) {
101 } else if (a
.fd
> b
.fd
) {
107 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
108 MemoryRegionIoeventfd b
)
110 return !memory_region_ioeventfd_before(a
, b
)
111 && !memory_region_ioeventfd_before(b
, a
);
114 typedef struct FlatRange FlatRange
;
115 typedef struct FlatView FlatView
;
117 /* Range of memory in the global map. Addresses are absolute. */
120 target_phys_addr_t offset_in_region
;
122 uint8_t dirty_log_mask
;
125 /* Flattened global view of current active memory hierarchy. Kept in sorted
131 unsigned nr_allocated
;
134 typedef struct AddressSpace AddressSpace
;
135 typedef struct AddressSpaceOps AddressSpaceOps
;
137 /* A system address space - I/O, memory, etc. */
138 struct AddressSpace
{
139 const AddressSpaceOps
*ops
;
141 FlatView current_map
;
143 MemoryRegionIoeventfd
*ioeventfds
;
146 struct AddressSpaceOps
{
147 void (*range_add
)(AddressSpace
*as
, FlatRange
*fr
);
148 void (*range_del
)(AddressSpace
*as
, FlatRange
*fr
);
149 void (*log_start
)(AddressSpace
*as
, FlatRange
*fr
);
150 void (*log_stop
)(AddressSpace
*as
, FlatRange
*fr
);
151 void (*ioeventfd_add
)(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
);
152 void (*ioeventfd_del
)(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
);
155 #define FOR_EACH_FLAT_RANGE(var, view) \
156 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
158 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
160 return a
->mr
== b
->mr
161 && addrrange_equal(a
->addr
, b
->addr
)
162 && a
->offset_in_region
== b
->offset_in_region
;
165 static void flatview_init(FlatView
*view
)
169 view
->nr_allocated
= 0;
172 /* Insert a range into a given position. Caller is responsible for maintaining
175 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
177 if (view
->nr
== view
->nr_allocated
) {
178 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
179 view
->ranges
= qemu_realloc(view
->ranges
,
180 view
->nr_allocated
* sizeof(*view
->ranges
));
182 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
183 (view
->nr
- pos
) * sizeof(FlatRange
));
184 view
->ranges
[pos
] = *range
;
188 static void flatview_destroy(FlatView
*view
)
190 qemu_free(view
->ranges
);
193 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
195 return addrrange_end(r1
->addr
) == r2
->addr
.start
197 && r1
->offset_in_region
+ r1
->addr
.size
== r2
->offset_in_region
198 && r1
->dirty_log_mask
== r2
->dirty_log_mask
;
201 /* Attempt to simplify a view by merging ajacent ranges */
202 static void flatview_simplify(FlatView
*view
)
207 while (i
< view
->nr
) {
210 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
211 view
->ranges
[i
].addr
.size
+= view
->ranges
[j
].addr
.size
;
215 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
216 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
221 static void memory_region_prepare_ram_addr(MemoryRegion
*mr
);
223 static void as_memory_range_add(AddressSpace
*as
, FlatRange
*fr
)
225 ram_addr_t phys_offset
, region_offset
;
227 memory_region_prepare_ram_addr(fr
->mr
);
229 phys_offset
= fr
->mr
->ram_addr
;
230 region_offset
= fr
->offset_in_region
;
231 /* cpu_register_physical_memory_log() wants region_offset for
232 * mmio, but prefers offseting phys_offset for RAM. Humour it.
234 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
) {
235 phys_offset
+= region_offset
;
239 cpu_register_physical_memory_log(fr
->addr
.start
,
246 static void as_memory_range_del(AddressSpace
*as
, FlatRange
*fr
)
248 cpu_register_physical_memory(fr
->addr
.start
, fr
->addr
.size
,
252 static void as_memory_log_start(AddressSpace
*as
, FlatRange
*fr
)
254 cpu_physical_log_start(fr
->addr
.start
, fr
->addr
.size
);
257 static void as_memory_log_stop(AddressSpace
*as
, FlatRange
*fr
)
259 cpu_physical_log_stop(fr
->addr
.start
, fr
->addr
.size
);
262 static void as_memory_ioeventfd_add(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
266 assert(fd
->match_data
&& fd
->addr
.size
== 4);
268 r
= kvm_set_ioeventfd_mmio_long(fd
->fd
, fd
->addr
.start
, fd
->data
, true);
274 static void as_memory_ioeventfd_del(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
278 r
= kvm_set_ioeventfd_mmio_long(fd
->fd
, fd
->addr
.start
, fd
->data
, false);
284 static const AddressSpaceOps address_space_ops_memory
= {
285 .range_add
= as_memory_range_add
,
286 .range_del
= as_memory_range_del
,
287 .log_start
= as_memory_log_start
,
288 .log_stop
= as_memory_log_stop
,
289 .ioeventfd_add
= as_memory_ioeventfd_add
,
290 .ioeventfd_del
= as_memory_ioeventfd_del
,
293 static AddressSpace address_space_memory
= {
294 .ops
= &address_space_ops_memory
,
297 static const MemoryRegionPortio
*find_portio(MemoryRegion
*mr
, uint64_t offset
,
298 unsigned width
, bool write
)
300 const MemoryRegionPortio
*mrp
;
302 for (mrp
= mr
->ops
->old_portio
; mrp
->size
; ++mrp
) {
303 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
304 && width
== mrp
->size
305 && (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
312 static void memory_region_iorange_read(IORange
*iorange
,
317 MemoryRegion
*mr
= container_of(iorange
, MemoryRegion
, iorange
);
319 if (mr
->ops
->old_portio
) {
320 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
, width
, false);
322 *data
= ((uint64_t)1 << (width
* 8)) - 1;
324 *data
= mrp
->read(mr
->opaque
, offset
- mrp
->offset
);
328 *data
= mr
->ops
->read(mr
->opaque
, offset
, width
);
331 static void memory_region_iorange_write(IORange
*iorange
,
336 MemoryRegion
*mr
= container_of(iorange
, MemoryRegion
, iorange
);
338 if (mr
->ops
->old_portio
) {
339 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
, width
, true);
342 mrp
->write(mr
->opaque
, offset
- mrp
->offset
, data
);
346 mr
->ops
->write(mr
->opaque
, offset
, data
, width
);
349 static const IORangeOps memory_region_iorange_ops
= {
350 .read
= memory_region_iorange_read
,
351 .write
= memory_region_iorange_write
,
354 static void as_io_range_add(AddressSpace
*as
, FlatRange
*fr
)
356 iorange_init(&fr
->mr
->iorange
, &memory_region_iorange_ops
,
357 fr
->addr
.start
,fr
->addr
.size
);
358 ioport_register(&fr
->mr
->iorange
);
361 static void as_io_range_del(AddressSpace
*as
, FlatRange
*fr
)
363 isa_unassign_ioport(fr
->addr
.start
, fr
->addr
.size
);
366 static void as_io_ioeventfd_add(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
370 assert(fd
->match_data
&& fd
->addr
.size
== 2);
372 r
= kvm_set_ioeventfd_pio_word(fd
->fd
, fd
->addr
.start
, fd
->data
, true);
378 static void as_io_ioeventfd_del(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
382 r
= kvm_set_ioeventfd_pio_word(fd
->fd
, fd
->addr
.start
, fd
->data
, false);
388 static const AddressSpaceOps address_space_ops_io
= {
389 .range_add
= as_io_range_add
,
390 .range_del
= as_io_range_del
,
391 .ioeventfd_add
= as_io_ioeventfd_add
,
392 .ioeventfd_del
= as_io_ioeventfd_del
,
395 static AddressSpace address_space_io
= {
396 .ops
= &address_space_ops_io
,
399 /* Render a memory region into the global view. Ranges in @view obscure
402 static void render_memory_region(FlatView
*view
,
404 target_phys_addr_t base
,
407 MemoryRegion
*subregion
;
409 target_phys_addr_t offset_in_region
;
417 tmp
= addrrange_make(base
, mr
->size
);
419 if (!addrrange_intersects(tmp
, clip
)) {
423 clip
= addrrange_intersection(tmp
, clip
);
426 base
-= mr
->alias
->addr
;
427 base
-= mr
->alias_offset
;
428 render_memory_region(view
, mr
->alias
, base
, clip
);
432 /* Render subregions in priority order. */
433 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
434 render_memory_region(view
, subregion
, base
, clip
);
437 if (!mr
->terminates
) {
441 offset_in_region
= clip
.start
- base
;
445 /* Render the region itself into any gaps left by the current view. */
446 for (i
= 0; i
< view
->nr
&& remain
; ++i
) {
447 if (base
>= addrrange_end(view
->ranges
[i
].addr
)) {
450 if (base
< view
->ranges
[i
].addr
.start
) {
451 now
= MIN(remain
, view
->ranges
[i
].addr
.start
- base
);
453 fr
.offset_in_region
= offset_in_region
;
454 fr
.addr
= addrrange_make(base
, now
);
455 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
456 flatview_insert(view
, i
, &fr
);
459 offset_in_region
+= now
;
462 if (base
== view
->ranges
[i
].addr
.start
) {
463 now
= MIN(remain
, view
->ranges
[i
].addr
.size
);
465 offset_in_region
+= now
;
471 fr
.offset_in_region
= offset_in_region
;
472 fr
.addr
= addrrange_make(base
, remain
);
473 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
474 flatview_insert(view
, i
, &fr
);
478 /* Render a memory topology into a list of disjoint absolute ranges. */
479 static FlatView
generate_memory_topology(MemoryRegion
*mr
)
483 flatview_init(&view
);
485 render_memory_region(&view
, mr
, 0, addrrange_make(0, UINT64_MAX
));
486 flatview_simplify(&view
);
491 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
492 MemoryRegionIoeventfd
*fds_new
,
494 MemoryRegionIoeventfd
*fds_old
,
499 /* Generate a symmetric difference of the old and new fd sets, adding
500 * and deleting as necessary.
504 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
505 if (iold
< fds_old_nb
506 && (inew
== fds_new_nb
507 || memory_region_ioeventfd_before(fds_old
[iold
],
509 as
->ops
->ioeventfd_del(as
, &fds_old
[iold
]);
511 } else if (inew
< fds_new_nb
512 && (iold
== fds_old_nb
513 || memory_region_ioeventfd_before(fds_new
[inew
],
515 as
->ops
->ioeventfd_add(as
, &fds_new
[inew
]);
524 static void address_space_update_ioeventfds(AddressSpace
*as
)
527 unsigned ioeventfd_nb
= 0;
528 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
532 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
533 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
534 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
535 fr
->addr
.start
- fr
->offset_in_region
);
536 if (addrrange_intersects(fr
->addr
, tmp
)) {
538 ioeventfds
= qemu_realloc(ioeventfds
,
539 ioeventfd_nb
* sizeof(*ioeventfds
));
540 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
541 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
546 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
547 as
->ioeventfds
, as
->ioeventfd_nb
);
549 qemu_free(as
->ioeventfds
);
550 as
->ioeventfds
= ioeventfds
;
551 as
->ioeventfd_nb
= ioeventfd_nb
;
554 static void address_space_update_topology_pass(AddressSpace
*as
,
560 FlatRange
*frold
, *frnew
;
562 /* Generate a symmetric difference of the old and new memory maps.
563 * Kill ranges in the old map, and instantiate ranges in the new map.
566 while (iold
< old_view
.nr
|| inew
< new_view
.nr
) {
567 if (iold
< old_view
.nr
) {
568 frold
= &old_view
.ranges
[iold
];
572 if (inew
< new_view
.nr
) {
573 frnew
= &new_view
.ranges
[inew
];
580 || frold
->addr
.start
< frnew
->addr
.start
581 || (frold
->addr
.start
== frnew
->addr
.start
582 && !flatrange_equal(frold
, frnew
)))) {
583 /* In old, but (not in new, or in new but attributes changed). */
586 as
->ops
->range_del(as
, frold
);
590 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
591 /* In both (logging may have changed) */
594 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
595 as
->ops
->log_stop(as
, frnew
);
596 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
597 as
->ops
->log_start(as
, frnew
);
607 as
->ops
->range_add(as
, frnew
);
616 static void address_space_update_topology(AddressSpace
*as
)
618 FlatView old_view
= as
->current_map
;
619 FlatView new_view
= generate_memory_topology(as
->root
);
621 address_space_update_topology_pass(as
, old_view
, new_view
, false);
622 address_space_update_topology_pass(as
, old_view
, new_view
, true);
624 as
->current_map
= new_view
;
625 flatview_destroy(&old_view
);
626 address_space_update_ioeventfds(as
);
629 static void memory_region_update_topology(void)
631 if (memory_region_transaction_depth
) {
635 if (address_space_memory
.root
) {
636 address_space_update_topology(&address_space_memory
);
638 if (address_space_io
.root
) {
639 address_space_update_topology(&address_space_io
);
643 void memory_region_transaction_begin(void)
645 ++memory_region_transaction_depth
;
648 void memory_region_transaction_commit(void)
650 assert(memory_region_transaction_depth
);
651 --memory_region_transaction_depth
;
652 memory_region_update_topology();
655 void memory_region_init(MemoryRegion
*mr
,
664 mr
->terminates
= false;
666 mr
->may_overlap
= false;
668 QTAILQ_INIT(&mr
->subregions
);
669 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
670 QTAILQ_INIT(&mr
->coalesced
);
671 mr
->name
= qemu_strdup(name
);
672 mr
->dirty_log_mask
= 0;
673 mr
->ioeventfd_nb
= 0;
674 mr
->ioeventfds
= NULL
;
677 static bool memory_region_access_valid(MemoryRegion
*mr
,
678 target_phys_addr_t addr
,
681 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
685 /* Treat zero as compatibility all valid */
686 if (!mr
->ops
->valid
.max_access_size
) {
690 if (size
> mr
->ops
->valid
.max_access_size
691 || size
< mr
->ops
->valid
.min_access_size
) {
697 static uint32_t memory_region_read_thunk_n(void *_mr
,
698 target_phys_addr_t addr
,
701 MemoryRegion
*mr
= _mr
;
702 unsigned access_size
, access_size_min
, access_size_max
;
703 uint64_t access_mask
;
704 uint32_t data
= 0, tmp
;
707 if (!memory_region_access_valid(mr
, addr
, size
)) {
708 return -1U; /* FIXME: better signalling */
711 if (!mr
->ops
->read
) {
712 return mr
->ops
->old_mmio
.read
[bitops_ffsl(size
)](mr
->opaque
, addr
);
715 /* FIXME: support unaligned access */
717 access_size_min
= mr
->ops
->impl
.min_access_size
;
718 if (!access_size_min
) {
721 access_size_max
= mr
->ops
->impl
.max_access_size
;
722 if (!access_size_max
) {
725 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
726 access_mask
= -1ULL >> (64 - access_size
* 8);
728 for (i
= 0; i
< size
; i
+= access_size
) {
729 /* FIXME: big-endian support */
730 tmp
= mr
->ops
->read(mr
->opaque
, addr
+ i
, access_size
);
731 data
|= (tmp
& access_mask
) << (i
* 8);
737 static void memory_region_write_thunk_n(void *_mr
,
738 target_phys_addr_t addr
,
742 MemoryRegion
*mr
= _mr
;
743 unsigned access_size
, access_size_min
, access_size_max
;
744 uint64_t access_mask
;
747 if (!memory_region_access_valid(mr
, addr
, size
)) {
748 return; /* FIXME: better signalling */
751 if (!mr
->ops
->write
) {
752 mr
->ops
->old_mmio
.write
[bitops_ffsl(size
)](mr
->opaque
, addr
, data
);
756 /* FIXME: support unaligned access */
758 access_size_min
= mr
->ops
->impl
.min_access_size
;
759 if (!access_size_min
) {
762 access_size_max
= mr
->ops
->impl
.max_access_size
;
763 if (!access_size_max
) {
766 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
767 access_mask
= -1ULL >> (64 - access_size
* 8);
769 for (i
= 0; i
< size
; i
+= access_size
) {
770 /* FIXME: big-endian support */
771 mr
->ops
->write(mr
->opaque
, addr
+ i
, (data
>> (i
* 8)) & access_mask
,
776 static uint32_t memory_region_read_thunk_b(void *mr
, target_phys_addr_t addr
)
778 return memory_region_read_thunk_n(mr
, addr
, 1);
781 static uint32_t memory_region_read_thunk_w(void *mr
, target_phys_addr_t addr
)
783 return memory_region_read_thunk_n(mr
, addr
, 2);
786 static uint32_t memory_region_read_thunk_l(void *mr
, target_phys_addr_t addr
)
788 return memory_region_read_thunk_n(mr
, addr
, 4);
791 static void memory_region_write_thunk_b(void *mr
, target_phys_addr_t addr
,
794 memory_region_write_thunk_n(mr
, addr
, 1, data
);
797 static void memory_region_write_thunk_w(void *mr
, target_phys_addr_t addr
,
800 memory_region_write_thunk_n(mr
, addr
, 2, data
);
803 static void memory_region_write_thunk_l(void *mr
, target_phys_addr_t addr
,
806 memory_region_write_thunk_n(mr
, addr
, 4, data
);
809 static CPUReadMemoryFunc
* const memory_region_read_thunk
[] = {
810 memory_region_read_thunk_b
,
811 memory_region_read_thunk_w
,
812 memory_region_read_thunk_l
,
815 static CPUWriteMemoryFunc
* const memory_region_write_thunk
[] = {
816 memory_region_write_thunk_b
,
817 memory_region_write_thunk_w
,
818 memory_region_write_thunk_l
,
821 static void memory_region_prepare_ram_addr(MemoryRegion
*mr
)
823 if (mr
->backend_registered
) {
827 mr
->ram_addr
= cpu_register_io_memory(memory_region_read_thunk
,
828 memory_region_write_thunk
,
830 mr
->ops
->endianness
);
831 mr
->backend_registered
= true;
834 void memory_region_init_io(MemoryRegion
*mr
,
835 const MemoryRegionOps
*ops
,
840 memory_region_init(mr
, name
, size
);
843 mr
->terminates
= true;
844 mr
->backend_registered
= false;
847 void memory_region_init_ram(MemoryRegion
*mr
,
852 memory_region_init(mr
, name
, size
);
853 mr
->terminates
= true;
854 mr
->ram_addr
= qemu_ram_alloc(dev
, name
, size
);
855 mr
->backend_registered
= true;
858 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
864 memory_region_init(mr
, name
, size
);
865 mr
->terminates
= true;
866 mr
->ram_addr
= qemu_ram_alloc_from_ptr(dev
, name
, size
, ptr
);
867 mr
->backend_registered
= true;
870 void memory_region_init_alias(MemoryRegion
*mr
,
873 target_phys_addr_t offset
,
876 memory_region_init(mr
, name
, size
);
878 mr
->alias_offset
= offset
;
881 void memory_region_destroy(MemoryRegion
*mr
)
883 assert(QTAILQ_EMPTY(&mr
->subregions
));
884 memory_region_clear_coalescing(mr
);
885 qemu_free((char *)mr
->name
);
886 qemu_free(mr
->ioeventfds
);
889 uint64_t memory_region_size(MemoryRegion
*mr
)
894 void memory_region_set_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
899 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
901 uint8_t mask
= 1 << client
;
903 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
904 memory_region_update_topology();
907 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
910 assert(mr
->terminates
);
911 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, 1 << client
);
914 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
)
916 assert(mr
->terminates
);
917 return cpu_physical_memory_set_dirty(mr
->ram_addr
+ addr
);
920 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
924 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
926 cpu_physical_sync_dirty_bitmap(fr
->addr
.start
,
927 fr
->addr
.start
+ fr
->addr
.size
);
932 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
937 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
938 target_phys_addr_t size
, unsigned client
)
940 assert(mr
->terminates
);
941 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
942 mr
->ram_addr
+ addr
+ size
,
946 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
949 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
952 assert(mr
->terminates
);
954 return qemu_get_ram_ptr(mr
->ram_addr
);
957 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
960 CoalescedMemoryRange
*cmr
;
963 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
965 qemu_unregister_coalesced_mmio(fr
->addr
.start
, fr
->addr
.size
);
966 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
967 tmp
= addrrange_shift(cmr
->addr
,
968 fr
->addr
.start
- fr
->offset_in_region
);
969 if (!addrrange_intersects(tmp
, fr
->addr
)) {
972 tmp
= addrrange_intersection(tmp
, fr
->addr
);
973 qemu_register_coalesced_mmio(tmp
.start
, tmp
.size
);
979 void memory_region_set_coalescing(MemoryRegion
*mr
)
981 memory_region_clear_coalescing(mr
);
982 memory_region_add_coalescing(mr
, 0, mr
->size
);
985 void memory_region_add_coalescing(MemoryRegion
*mr
,
986 target_phys_addr_t offset
,
989 CoalescedMemoryRange
*cmr
= qemu_malloc(sizeof(*cmr
));
991 cmr
->addr
= addrrange_make(offset
, size
);
992 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
993 memory_region_update_coalesced_range(mr
);
996 void memory_region_clear_coalescing(MemoryRegion
*mr
)
998 CoalescedMemoryRange
*cmr
;
1000 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1001 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1002 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1005 memory_region_update_coalesced_range(mr
);
1008 void memory_region_add_eventfd(MemoryRegion
*mr
,
1009 target_phys_addr_t addr
,
1015 MemoryRegionIoeventfd mrfd
= {
1018 .match_data
= match_data
,
1024 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1025 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1030 mr
->ioeventfds
= qemu_realloc(mr
->ioeventfds
,
1031 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1032 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1033 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1034 mr
->ioeventfds
[i
] = mrfd
;
1035 memory_region_update_topology();
1038 void memory_region_del_eventfd(MemoryRegion
*mr
,
1039 target_phys_addr_t addr
,
1045 MemoryRegionIoeventfd mrfd
= {
1048 .match_data
= match_data
,
1054 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1055 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1059 assert(i
!= mr
->ioeventfd_nb
);
1060 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1061 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1063 mr
->ioeventfds
= qemu_realloc(mr
->ioeventfds
,
1064 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1065 memory_region_update_topology();
1068 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1069 target_phys_addr_t offset
,
1070 MemoryRegion
*subregion
)
1072 MemoryRegion
*other
;
1074 assert(!subregion
->parent
);
1075 subregion
->parent
= mr
;
1076 subregion
->addr
= offset
;
1077 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1078 if (subregion
->may_overlap
|| other
->may_overlap
) {
1081 if (offset
>= other
->offset
+ other
->size
1082 || offset
+ subregion
->size
<= other
->offset
) {
1085 printf("warning: subregion collision %llx/%llx vs %llx/%llx\n",
1086 (unsigned long long)offset
,
1087 (unsigned long long)subregion
->size
,
1088 (unsigned long long)other
->offset
,
1089 (unsigned long long)other
->size
);
1091 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1092 if (subregion
->priority
>= other
->priority
) {
1093 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1097 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1099 memory_region_update_topology();
1103 void memory_region_add_subregion(MemoryRegion
*mr
,
1104 target_phys_addr_t offset
,
1105 MemoryRegion
*subregion
)
1107 subregion
->may_overlap
= false;
1108 subregion
->priority
= 0;
1109 memory_region_add_subregion_common(mr
, offset
, subregion
);
1112 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1113 target_phys_addr_t offset
,
1114 MemoryRegion
*subregion
,
1117 subregion
->may_overlap
= true;
1118 subregion
->priority
= priority
;
1119 memory_region_add_subregion_common(mr
, offset
, subregion
);
1122 void memory_region_del_subregion(MemoryRegion
*mr
,
1123 MemoryRegion
*subregion
)
1125 assert(subregion
->parent
== mr
);
1126 subregion
->parent
= NULL
;
1127 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1128 memory_region_update_topology();
1131 void set_system_memory_map(MemoryRegion
*mr
)
1133 address_space_memory
.root
= mr
;
1134 memory_region_update_topology();
1137 void set_system_io_map(MemoryRegion
*mr
)
1139 address_space_io
.root
= mr
;
1140 memory_region_update_topology();