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.
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.
17 #include "exec-memory.h"
23 #include "memory-internal.h"
25 unsigned memory_region_transaction_depth
= 0;
26 static bool global_dirty_log
= false;
28 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
29 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
31 static QTAILQ_HEAD(, AddressSpace
) address_spaces
32 = QTAILQ_HEAD_INITIALIZER(address_spaces
);
34 typedef struct AddrRange AddrRange
;
37 * Note using signed integers limits us to physical addresses at most
38 * 63 bits wide. They are needed for negative offsetting in aliases
39 * (large MemoryRegion::alias_offset).
46 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
48 return (AddrRange
) { start
, size
};
51 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
53 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
56 static Int128
addrrange_end(AddrRange r
)
58 return int128_add(r
.start
, r
.size
);
61 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
63 int128_addto(&range
.start
, delta
);
67 static bool addrrange_contains(AddrRange range
, Int128 addr
)
69 return int128_ge(addr
, range
.start
)
70 && int128_lt(addr
, addrrange_end(range
));
73 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
75 return addrrange_contains(r1
, r2
.start
)
76 || addrrange_contains(r2
, r1
.start
);
79 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
81 Int128 start
= int128_max(r1
.start
, r2
.start
);
82 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
83 return addrrange_make(start
, int128_sub(end
, start
));
86 enum ListenerDirection
{ Forward
, Reverse
};
88 static bool memory_listener_match(MemoryListener
*listener
,
89 MemoryRegionSection
*section
)
91 return !listener
->address_space_filter
92 || listener
->address_space_filter
== section
->address_space
;
95 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
97 MemoryListener *_listener; \
99 switch (_direction) { \
101 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
102 if (_listener->_callback) { \
103 _listener->_callback(_listener, ##_args); \
108 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
109 memory_listeners, link) { \
110 if (_listener->_callback) { \
111 _listener->_callback(_listener, ##_args); \
120 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
122 MemoryListener *_listener; \
124 switch (_direction) { \
126 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
127 if (_listener->_callback \
128 && memory_listener_match(_listener, _section)) { \
129 _listener->_callback(_listener, _section, ##_args); \
134 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
135 memory_listeners, link) { \
136 if (_listener->_callback \
137 && memory_listener_match(_listener, _section)) { \
138 _listener->_callback(_listener, _section, ##_args); \
147 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
148 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
150 .address_space = (as), \
151 .offset_within_region = (fr)->offset_in_region, \
152 .size = int128_get64((fr)->addr.size), \
153 .offset_within_address_space = int128_get64((fr)->addr.start), \
154 .readonly = (fr)->readonly, \
157 struct CoalescedMemoryRange
{
159 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
162 struct MemoryRegionIoeventfd
{
169 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
170 MemoryRegionIoeventfd b
)
172 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
174 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
176 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
178 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
180 } else if (a
.match_data
< b
.match_data
) {
182 } else if (a
.match_data
> b
.match_data
) {
184 } else if (a
.match_data
) {
185 if (a
.data
< b
.data
) {
187 } else if (a
.data
> b
.data
) {
193 } else if (a
.e
> b
.e
) {
199 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
200 MemoryRegionIoeventfd b
)
202 return !memory_region_ioeventfd_before(a
, b
)
203 && !memory_region_ioeventfd_before(b
, a
);
206 typedef struct FlatRange FlatRange
;
207 typedef struct FlatView FlatView
;
209 /* Range of memory in the global map. Addresses are absolute. */
212 hwaddr offset_in_region
;
214 uint8_t dirty_log_mask
;
219 /* Flattened global view of current active memory hierarchy. Kept in sorted
225 unsigned nr_allocated
;
228 typedef struct AddressSpaceOps AddressSpaceOps
;
230 #define FOR_EACH_FLAT_RANGE(var, view) \
231 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
233 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
235 return a
->mr
== b
->mr
236 && addrrange_equal(a
->addr
, b
->addr
)
237 && a
->offset_in_region
== b
->offset_in_region
238 && a
->readable
== b
->readable
239 && a
->readonly
== b
->readonly
;
242 static void flatview_init(FlatView
*view
)
246 view
->nr_allocated
= 0;
249 /* Insert a range into a given position. Caller is responsible for maintaining
252 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
254 if (view
->nr
== view
->nr_allocated
) {
255 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
256 view
->ranges
= g_realloc(view
->ranges
,
257 view
->nr_allocated
* sizeof(*view
->ranges
));
259 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
260 (view
->nr
- pos
) * sizeof(FlatRange
));
261 view
->ranges
[pos
] = *range
;
265 static void flatview_destroy(FlatView
*view
)
267 g_free(view
->ranges
);
270 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
272 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
274 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
276 int128_make64(r2
->offset_in_region
))
277 && r1
->dirty_log_mask
== r2
->dirty_log_mask
278 && r1
->readable
== r2
->readable
279 && r1
->readonly
== r2
->readonly
;
282 /* Attempt to simplify a view by merging ajacent ranges */
283 static void flatview_simplify(FlatView
*view
)
288 while (i
< view
->nr
) {
291 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
292 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
296 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
297 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
302 static void memory_region_read_accessor(void *opaque
,
309 MemoryRegion
*mr
= opaque
;
312 if (mr
->flush_coalesced_mmio
) {
313 qemu_flush_coalesced_mmio_buffer();
315 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
316 *value
|= (tmp
& mask
) << shift
;
319 static void memory_region_write_accessor(void *opaque
,
326 MemoryRegion
*mr
= opaque
;
329 if (mr
->flush_coalesced_mmio
) {
330 qemu_flush_coalesced_mmio_buffer();
332 tmp
= (*value
>> shift
) & mask
;
333 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
336 static void access_with_adjusted_size(hwaddr addr
,
339 unsigned access_size_min
,
340 unsigned access_size_max
,
341 void (*access
)(void *opaque
,
349 uint64_t access_mask
;
350 unsigned access_size
;
353 if (!access_size_min
) {
356 if (!access_size_max
) {
359 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
360 access_mask
= -1ULL >> (64 - access_size
* 8);
361 for (i
= 0; i
< size
; i
+= access_size
) {
362 /* FIXME: big-endian support */
363 access(opaque
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
367 static const MemoryRegionPortio
*find_portio(MemoryRegion
*mr
, uint64_t offset
,
368 unsigned width
, bool write
)
370 const MemoryRegionPortio
*mrp
;
372 for (mrp
= mr
->ops
->old_portio
; mrp
->size
; ++mrp
) {
373 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
374 && width
== mrp
->size
375 && (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
382 static void memory_region_iorange_read(IORange
*iorange
,
387 MemoryRegionIORange
*mrio
388 = container_of(iorange
, MemoryRegionIORange
, iorange
);
389 MemoryRegion
*mr
= mrio
->mr
;
391 offset
+= mrio
->offset
;
392 if (mr
->ops
->old_portio
) {
393 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
- mrio
->offset
,
396 *data
= ((uint64_t)1 << (width
* 8)) - 1;
398 *data
= mrp
->read(mr
->opaque
, offset
);
399 } else if (width
== 2) {
400 mrp
= find_portio(mr
, offset
- mrio
->offset
, 1, false);
402 *data
= mrp
->read(mr
->opaque
, offset
) |
403 (mrp
->read(mr
->opaque
, offset
+ 1) << 8);
408 access_with_adjusted_size(offset
, data
, width
,
409 mr
->ops
->impl
.min_access_size
,
410 mr
->ops
->impl
.max_access_size
,
411 memory_region_read_accessor
, mr
);
414 static void memory_region_iorange_write(IORange
*iorange
,
419 MemoryRegionIORange
*mrio
420 = container_of(iorange
, MemoryRegionIORange
, iorange
);
421 MemoryRegion
*mr
= mrio
->mr
;
423 offset
+= mrio
->offset
;
424 if (mr
->ops
->old_portio
) {
425 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
- mrio
->offset
,
429 mrp
->write(mr
->opaque
, offset
, data
);
430 } else if (width
== 2) {
431 mrp
= find_portio(mr
, offset
- mrio
->offset
, 1, true);
433 mrp
->write(mr
->opaque
, offset
, data
& 0xff);
434 mrp
->write(mr
->opaque
, offset
+ 1, data
>> 8);
438 access_with_adjusted_size(offset
, &data
, width
,
439 mr
->ops
->impl
.min_access_size
,
440 mr
->ops
->impl
.max_access_size
,
441 memory_region_write_accessor
, mr
);
444 static void memory_region_iorange_destructor(IORange
*iorange
)
446 g_free(container_of(iorange
, MemoryRegionIORange
, iorange
));
449 const IORangeOps memory_region_iorange_ops
= {
450 .read
= memory_region_iorange_read
,
451 .write
= memory_region_iorange_write
,
452 .destructor
= memory_region_iorange_destructor
,
455 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
462 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
463 if (mr
== as
->root
) {
470 /* Render a memory region into the global view. Ranges in @view obscure
473 static void render_memory_region(FlatView
*view
,
479 MemoryRegion
*subregion
;
481 hwaddr offset_in_region
;
491 int128_addto(&base
, int128_make64(mr
->addr
));
492 readonly
|= mr
->readonly
;
494 tmp
= addrrange_make(base
, mr
->size
);
496 if (!addrrange_intersects(tmp
, clip
)) {
500 clip
= addrrange_intersection(tmp
, clip
);
503 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
504 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
505 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
509 /* Render subregions in priority order. */
510 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
511 render_memory_region(view
, subregion
, base
, clip
, readonly
);
514 if (!mr
->terminates
) {
518 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
522 /* Render the region itself into any gaps left by the current view. */
523 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
524 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
527 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
528 now
= int128_min(remain
,
529 int128_sub(view
->ranges
[i
].addr
.start
, base
));
531 fr
.offset_in_region
= offset_in_region
;
532 fr
.addr
= addrrange_make(base
, now
);
533 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
534 fr
.readable
= mr
->readable
;
535 fr
.readonly
= readonly
;
536 flatview_insert(view
, i
, &fr
);
538 int128_addto(&base
, now
);
539 offset_in_region
+= int128_get64(now
);
540 int128_subfrom(&remain
, now
);
542 if (int128_eq(base
, view
->ranges
[i
].addr
.start
)) {
543 now
= int128_min(remain
, view
->ranges
[i
].addr
.size
);
544 int128_addto(&base
, now
);
545 offset_in_region
+= int128_get64(now
);
546 int128_subfrom(&remain
, now
);
549 if (int128_nz(remain
)) {
551 fr
.offset_in_region
= offset_in_region
;
552 fr
.addr
= addrrange_make(base
, remain
);
553 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
554 fr
.readable
= mr
->readable
;
555 fr
.readonly
= readonly
;
556 flatview_insert(view
, i
, &fr
);
560 /* Render a memory topology into a list of disjoint absolute ranges. */
561 static FlatView
generate_memory_topology(MemoryRegion
*mr
)
565 flatview_init(&view
);
568 render_memory_region(&view
, mr
, int128_zero(),
569 addrrange_make(int128_zero(), int128_2_64()), false);
571 flatview_simplify(&view
);
576 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
577 MemoryRegionIoeventfd
*fds_new
,
579 MemoryRegionIoeventfd
*fds_old
,
583 MemoryRegionIoeventfd
*fd
;
584 MemoryRegionSection section
;
586 /* Generate a symmetric difference of the old and new fd sets, adding
587 * and deleting as necessary.
591 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
592 if (iold
< fds_old_nb
593 && (inew
== fds_new_nb
594 || memory_region_ioeventfd_before(fds_old
[iold
],
597 section
= (MemoryRegionSection
) {
599 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
600 .size
= int128_get64(fd
->addr
.size
),
602 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
603 fd
->match_data
, fd
->data
, fd
->e
);
605 } else if (inew
< fds_new_nb
606 && (iold
== fds_old_nb
607 || memory_region_ioeventfd_before(fds_new
[inew
],
610 section
= (MemoryRegionSection
) {
612 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
613 .size
= int128_get64(fd
->addr
.size
),
615 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
616 fd
->match_data
, fd
->data
, fd
->e
);
625 static void address_space_update_ioeventfds(AddressSpace
*as
)
628 unsigned ioeventfd_nb
= 0;
629 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
633 FOR_EACH_FLAT_RANGE(fr
, as
->current_map
) {
634 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
635 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
636 int128_sub(fr
->addr
.start
,
637 int128_make64(fr
->offset_in_region
)));
638 if (addrrange_intersects(fr
->addr
, tmp
)) {
640 ioeventfds
= g_realloc(ioeventfds
,
641 ioeventfd_nb
* sizeof(*ioeventfds
));
642 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
643 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
648 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
649 as
->ioeventfds
, as
->ioeventfd_nb
);
651 g_free(as
->ioeventfds
);
652 as
->ioeventfds
= ioeventfds
;
653 as
->ioeventfd_nb
= ioeventfd_nb
;
656 static void address_space_update_topology_pass(AddressSpace
*as
,
662 FlatRange
*frold
, *frnew
;
664 /* Generate a symmetric difference of the old and new memory maps.
665 * Kill ranges in the old map, and instantiate ranges in the new map.
668 while (iold
< old_view
.nr
|| inew
< new_view
.nr
) {
669 if (iold
< old_view
.nr
) {
670 frold
= &old_view
.ranges
[iold
];
674 if (inew
< new_view
.nr
) {
675 frnew
= &new_view
.ranges
[inew
];
682 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
683 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
684 && !flatrange_equal(frold
, frnew
)))) {
685 /* In old, but (not in new, or in new but attributes changed). */
688 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
692 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
693 /* In both (logging may have changed) */
696 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
697 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
698 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
699 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
700 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
710 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
719 static void address_space_update_topology(AddressSpace
*as
)
721 FlatView old_view
= *as
->current_map
;
722 FlatView new_view
= generate_memory_topology(as
->root
);
724 address_space_update_topology_pass(as
, old_view
, new_view
, false);
725 address_space_update_topology_pass(as
, old_view
, new_view
, true);
727 *as
->current_map
= new_view
;
728 flatview_destroy(&old_view
);
729 address_space_update_ioeventfds(as
);
732 void memory_region_transaction_begin(void)
734 qemu_flush_coalesced_mmio_buffer();
735 ++memory_region_transaction_depth
;
738 void memory_region_transaction_commit(void)
742 assert(memory_region_transaction_depth
);
743 --memory_region_transaction_depth
;
744 if (!memory_region_transaction_depth
) {
745 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
747 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
748 address_space_update_topology(as
);
751 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
755 static void memory_region_destructor_none(MemoryRegion
*mr
)
759 static void memory_region_destructor_ram(MemoryRegion
*mr
)
761 qemu_ram_free(mr
->ram_addr
);
764 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
766 qemu_ram_free_from_ptr(mr
->ram_addr
);
769 static void memory_region_destructor_iomem(MemoryRegion
*mr
)
773 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
775 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
778 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
780 #ifdef TARGET_WORDS_BIGENDIAN
781 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
783 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
787 void memory_region_init(MemoryRegion
*mr
,
793 mr
->size
= int128_make64(size
);
794 if (size
== UINT64_MAX
) {
795 mr
->size
= int128_2_64();
800 mr
->terminates
= false;
803 mr
->readonly
= false;
804 mr
->rom_device
= false;
805 mr
->destructor
= memory_region_destructor_none
;
807 mr
->may_overlap
= false;
809 QTAILQ_INIT(&mr
->subregions
);
810 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
811 QTAILQ_INIT(&mr
->coalesced
);
812 mr
->name
= g_strdup(name
);
813 mr
->dirty_log_mask
= 0;
814 mr
->ioeventfd_nb
= 0;
815 mr
->ioeventfds
= NULL
;
816 mr
->flush_coalesced_mmio
= false;
819 static bool memory_region_access_valid(MemoryRegion
*mr
,
824 if (mr
->ops
->valid
.accepts
825 && !mr
->ops
->valid
.accepts(mr
->opaque
, addr
, size
, is_write
)) {
829 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
833 /* Treat zero as compatibility all valid */
834 if (!mr
->ops
->valid
.max_access_size
) {
838 if (size
> mr
->ops
->valid
.max_access_size
839 || size
< mr
->ops
->valid
.min_access_size
) {
845 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
851 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
852 return -1U; /* FIXME: better signalling */
855 if (!mr
->ops
->read
) {
856 return mr
->ops
->old_mmio
.read
[bitops_ffsl(size
)](mr
->opaque
, addr
);
859 /* FIXME: support unaligned access */
860 access_with_adjusted_size(addr
, &data
, size
,
861 mr
->ops
->impl
.min_access_size
,
862 mr
->ops
->impl
.max_access_size
,
863 memory_region_read_accessor
, mr
);
868 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
870 if (memory_region_wrong_endianness(mr
)) {
875 *data
= bswap16(*data
);
878 *data
= bswap32(*data
);
886 static uint64_t memory_region_dispatch_read(MemoryRegion
*mr
,
892 ret
= memory_region_dispatch_read1(mr
, addr
, size
);
893 adjust_endianness(mr
, &ret
, size
);
897 static void memory_region_dispatch_write(MemoryRegion
*mr
,
902 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
903 return; /* FIXME: better signalling */
906 adjust_endianness(mr
, &data
, size
);
908 if (!mr
->ops
->write
) {
909 mr
->ops
->old_mmio
.write
[bitops_ffsl(size
)](mr
->opaque
, addr
, data
);
913 /* FIXME: support unaligned access */
914 access_with_adjusted_size(addr
, &data
, size
,
915 mr
->ops
->impl
.min_access_size
,
916 mr
->ops
->impl
.max_access_size
,
917 memory_region_write_accessor
, mr
);
920 void memory_region_init_io(MemoryRegion
*mr
,
921 const MemoryRegionOps
*ops
,
926 memory_region_init(mr
, name
, size
);
929 mr
->terminates
= true;
930 mr
->destructor
= memory_region_destructor_iomem
;
931 mr
->ram_addr
= ~(ram_addr_t
)0;
934 void memory_region_init_ram(MemoryRegion
*mr
,
938 memory_region_init(mr
, name
, size
);
940 mr
->terminates
= true;
941 mr
->destructor
= memory_region_destructor_ram
;
942 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
945 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
950 memory_region_init(mr
, name
, size
);
952 mr
->terminates
= true;
953 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
954 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
957 void memory_region_init_alias(MemoryRegion
*mr
,
963 memory_region_init(mr
, name
, size
);
965 mr
->alias_offset
= offset
;
968 void memory_region_init_rom_device(MemoryRegion
*mr
,
969 const MemoryRegionOps
*ops
,
974 memory_region_init(mr
, name
, size
);
977 mr
->terminates
= true;
978 mr
->rom_device
= true;
979 mr
->destructor
= memory_region_destructor_rom_device
;
980 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
983 static uint64_t invalid_read(void *opaque
, hwaddr addr
,
986 MemoryRegion
*mr
= opaque
;
988 if (!mr
->warning_printed
) {
989 fprintf(stderr
, "Invalid read from memory region %s\n", mr
->name
);
990 mr
->warning_printed
= true;
995 static void invalid_write(void *opaque
, hwaddr addr
, uint64_t data
,
998 MemoryRegion
*mr
= opaque
;
1000 if (!mr
->warning_printed
) {
1001 fprintf(stderr
, "Invalid write to memory region %s\n", mr
->name
);
1002 mr
->warning_printed
= true;
1006 static const MemoryRegionOps reservation_ops
= {
1007 .read
= invalid_read
,
1008 .write
= invalid_write
,
1009 .endianness
= DEVICE_NATIVE_ENDIAN
,
1012 void memory_region_init_reservation(MemoryRegion
*mr
,
1016 memory_region_init_io(mr
, &reservation_ops
, mr
, name
, size
);
1019 void memory_region_destroy(MemoryRegion
*mr
)
1021 assert(QTAILQ_EMPTY(&mr
->subregions
));
1022 assert(memory_region_transaction_depth
== 0);
1024 memory_region_clear_coalescing(mr
);
1025 g_free((char *)mr
->name
);
1026 g_free(mr
->ioeventfds
);
1029 uint64_t memory_region_size(MemoryRegion
*mr
)
1031 if (int128_eq(mr
->size
, int128_2_64())) {
1034 return int128_get64(mr
->size
);
1037 const char *memory_region_name(MemoryRegion
*mr
)
1042 bool memory_region_is_ram(MemoryRegion
*mr
)
1047 bool memory_region_is_logging(MemoryRegion
*mr
)
1049 return mr
->dirty_log_mask
;
1052 bool memory_region_is_rom(MemoryRegion
*mr
)
1054 return mr
->ram
&& mr
->readonly
;
1057 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1059 uint8_t mask
= 1 << client
;
1061 memory_region_transaction_begin();
1062 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1063 memory_region_transaction_commit();
1066 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1067 hwaddr size
, unsigned client
)
1069 assert(mr
->terminates
);
1070 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1074 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1077 assert(mr
->terminates
);
1078 return cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
, -1);
1081 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1086 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1087 FOR_EACH_FLAT_RANGE(fr
, as
->current_map
) {
1089 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1095 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1097 if (mr
->readonly
!= readonly
) {
1098 memory_region_transaction_begin();
1099 mr
->readonly
= readonly
;
1100 memory_region_transaction_commit();
1104 void memory_region_rom_device_set_readable(MemoryRegion
*mr
, bool readable
)
1106 if (mr
->readable
!= readable
) {
1107 memory_region_transaction_begin();
1108 mr
->readable
= readable
;
1109 memory_region_transaction_commit();
1113 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1114 hwaddr size
, unsigned client
)
1116 assert(mr
->terminates
);
1117 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1118 mr
->ram_addr
+ addr
+ size
,
1122 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1125 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1128 assert(mr
->terminates
);
1130 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1133 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1136 CoalescedMemoryRange
*cmr
;
1138 MemoryRegionSection section
;
1140 FOR_EACH_FLAT_RANGE(fr
, as
->current_map
) {
1142 section
= (MemoryRegionSection
) {
1143 .address_space
= as
,
1144 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1145 .size
= int128_get64(fr
->addr
.size
),
1148 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1149 int128_get64(fr
->addr
.start
),
1150 int128_get64(fr
->addr
.size
));
1151 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1152 tmp
= addrrange_shift(cmr
->addr
,
1153 int128_sub(fr
->addr
.start
,
1154 int128_make64(fr
->offset_in_region
)));
1155 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1158 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1159 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1160 int128_get64(tmp
.start
),
1161 int128_get64(tmp
.size
));
1167 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1171 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1172 memory_region_update_coalesced_range_as(mr
, as
);
1176 void memory_region_set_coalescing(MemoryRegion
*mr
)
1178 memory_region_clear_coalescing(mr
);
1179 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1182 void memory_region_add_coalescing(MemoryRegion
*mr
,
1186 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1188 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1189 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1190 memory_region_update_coalesced_range(mr
);
1191 memory_region_set_flush_coalesced(mr
);
1194 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1196 CoalescedMemoryRange
*cmr
;
1198 qemu_flush_coalesced_mmio_buffer();
1199 mr
->flush_coalesced_mmio
= false;
1201 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1202 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1203 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1206 memory_region_update_coalesced_range(mr
);
1209 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1211 mr
->flush_coalesced_mmio
= true;
1214 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1216 qemu_flush_coalesced_mmio_buffer();
1217 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1218 mr
->flush_coalesced_mmio
= false;
1222 void memory_region_add_eventfd(MemoryRegion
*mr
,
1229 MemoryRegionIoeventfd mrfd
= {
1230 .addr
.start
= int128_make64(addr
),
1231 .addr
.size
= int128_make64(size
),
1232 .match_data
= match_data
,
1238 adjust_endianness(mr
, &mrfd
.data
, size
);
1239 memory_region_transaction_begin();
1240 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1241 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1246 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1247 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1248 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1249 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1250 mr
->ioeventfds
[i
] = mrfd
;
1251 memory_region_transaction_commit();
1254 void memory_region_del_eventfd(MemoryRegion
*mr
,
1261 MemoryRegionIoeventfd mrfd
= {
1262 .addr
.start
= int128_make64(addr
),
1263 .addr
.size
= int128_make64(size
),
1264 .match_data
= match_data
,
1270 adjust_endianness(mr
, &mrfd
.data
, size
);
1271 memory_region_transaction_begin();
1272 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1273 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1277 assert(i
!= mr
->ioeventfd_nb
);
1278 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1279 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1281 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1282 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1283 memory_region_transaction_commit();
1286 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1288 MemoryRegion
*subregion
)
1290 MemoryRegion
*other
;
1292 memory_region_transaction_begin();
1294 assert(!subregion
->parent
);
1295 subregion
->parent
= mr
;
1296 subregion
->addr
= offset
;
1297 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1298 if (subregion
->may_overlap
|| other
->may_overlap
) {
1301 if (int128_gt(int128_make64(offset
),
1302 int128_add(int128_make64(other
->addr
), other
->size
))
1303 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1304 int128_make64(other
->addr
))) {
1308 printf("warning: subregion collision %llx/%llx (%s) "
1309 "vs %llx/%llx (%s)\n",
1310 (unsigned long long)offset
,
1311 (unsigned long long)int128_get64(subregion
->size
),
1313 (unsigned long long)other
->addr
,
1314 (unsigned long long)int128_get64(other
->size
),
1318 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1319 if (subregion
->priority
>= other
->priority
) {
1320 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1324 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1326 memory_region_transaction_commit();
1330 void memory_region_add_subregion(MemoryRegion
*mr
,
1332 MemoryRegion
*subregion
)
1334 subregion
->may_overlap
= false;
1335 subregion
->priority
= 0;
1336 memory_region_add_subregion_common(mr
, offset
, subregion
);
1339 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1341 MemoryRegion
*subregion
,
1344 subregion
->may_overlap
= true;
1345 subregion
->priority
= priority
;
1346 memory_region_add_subregion_common(mr
, offset
, subregion
);
1349 void memory_region_del_subregion(MemoryRegion
*mr
,
1350 MemoryRegion
*subregion
)
1352 memory_region_transaction_begin();
1353 assert(subregion
->parent
== mr
);
1354 subregion
->parent
= NULL
;
1355 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1356 memory_region_transaction_commit();
1359 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1361 if (enabled
== mr
->enabled
) {
1364 memory_region_transaction_begin();
1365 mr
->enabled
= enabled
;
1366 memory_region_transaction_commit();
1369 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
)
1371 MemoryRegion
*parent
= mr
->parent
;
1372 unsigned priority
= mr
->priority
;
1373 bool may_overlap
= mr
->may_overlap
;
1375 if (addr
== mr
->addr
|| !parent
) {
1380 memory_region_transaction_begin();
1381 memory_region_del_subregion(parent
, mr
);
1383 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1385 memory_region_add_subregion(parent
, addr
, mr
);
1387 memory_region_transaction_commit();
1390 void memory_region_set_alias_offset(MemoryRegion
*mr
, hwaddr offset
)
1394 if (offset
== mr
->alias_offset
) {
1398 memory_region_transaction_begin();
1399 mr
->alias_offset
= offset
;
1400 memory_region_transaction_commit();
1403 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1405 return mr
->ram_addr
;
1408 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1410 const AddrRange
*addr
= addr_
;
1411 const FlatRange
*fr
= fr_
;
1413 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1415 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1421 static FlatRange
*address_space_lookup(AddressSpace
*as
, AddrRange addr
)
1423 return bsearch(&addr
, as
->current_map
->ranges
, as
->current_map
->nr
,
1424 sizeof(FlatRange
), cmp_flatrange_addr
);
1427 MemoryRegionSection
memory_region_find(MemoryRegion
*address_space
,
1428 hwaddr addr
, uint64_t size
)
1430 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1431 AddrRange range
= addrrange_make(int128_make64(addr
),
1432 int128_make64(size
));
1433 FlatRange
*fr
= address_space_lookup(as
, range
);
1434 MemoryRegionSection ret
= { .mr
= NULL
, .size
= 0 };
1440 while (fr
> as
->current_map
->ranges
1441 && addrrange_intersects(fr
[-1].addr
, range
)) {
1446 range
= addrrange_intersection(range
, fr
->addr
);
1447 ret
.offset_within_region
= fr
->offset_in_region
;
1448 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1450 ret
.size
= int128_get64(range
.size
);
1451 ret
.offset_within_address_space
= int128_get64(range
.start
);
1452 ret
.readonly
= fr
->readonly
;
1456 void memory_global_sync_dirty_bitmap(MemoryRegion
*address_space
)
1458 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1461 FOR_EACH_FLAT_RANGE(fr
, as
->current_map
) {
1462 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1466 void memory_global_dirty_log_start(void)
1468 global_dirty_log
= true;
1469 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1472 void memory_global_dirty_log_stop(void)
1474 global_dirty_log
= false;
1475 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1478 static void listener_add_address_space(MemoryListener
*listener
,
1483 if (listener
->address_space_filter
1484 && listener
->address_space_filter
!= as
) {
1488 if (global_dirty_log
) {
1489 if (listener
->log_global_start
) {
1490 listener
->log_global_start(listener
);
1494 FOR_EACH_FLAT_RANGE(fr
, as
->current_map
) {
1495 MemoryRegionSection section
= {
1497 .address_space
= as
,
1498 .offset_within_region
= fr
->offset_in_region
,
1499 .size
= int128_get64(fr
->addr
.size
),
1500 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1501 .readonly
= fr
->readonly
,
1503 if (listener
->region_add
) {
1504 listener
->region_add(listener
, §ion
);
1509 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1511 MemoryListener
*other
= NULL
;
1514 listener
->address_space_filter
= filter
;
1515 if (QTAILQ_EMPTY(&memory_listeners
)
1516 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1517 memory_listeners
)->priority
) {
1518 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1520 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1521 if (listener
->priority
< other
->priority
) {
1525 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1528 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1529 listener_add_address_space(listener
, as
);
1533 void memory_listener_unregister(MemoryListener
*listener
)
1535 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1538 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
)
1540 memory_region_transaction_begin();
1542 as
->current_map
= g_new(FlatView
, 1);
1543 flatview_init(as
->current_map
);
1544 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1546 memory_region_transaction_commit();
1547 address_space_init_dispatch(as
);
1550 void address_space_destroy(AddressSpace
*as
)
1552 /* Flush out anything from MemoryListeners listening in on this */
1553 memory_region_transaction_begin();
1555 memory_region_transaction_commit();
1556 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1557 address_space_destroy_dispatch(as
);
1558 flatview_destroy(as
->current_map
);
1559 g_free(as
->current_map
);
1562 uint64_t io_mem_read(MemoryRegion
*mr
, hwaddr addr
, unsigned size
)
1564 return memory_region_dispatch_read(mr
, addr
, size
);
1567 void io_mem_write(MemoryRegion
*mr
, hwaddr addr
,
1568 uint64_t val
, unsigned size
)
1570 memory_region_dispatch_write(mr
, addr
, val
, size
);
1573 typedef struct MemoryRegionList MemoryRegionList
;
1575 struct MemoryRegionList
{
1576 const MemoryRegion
*mr
;
1578 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1581 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1583 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1584 const MemoryRegion
*mr
, unsigned int level
,
1586 MemoryRegionListHead
*alias_print_queue
)
1588 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1589 MemoryRegionListHead submr_print_queue
;
1590 const MemoryRegion
*submr
;
1597 for (i
= 0; i
< level
; i
++) {
1602 MemoryRegionList
*ml
;
1605 /* check if the alias is already in the queue */
1606 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1607 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1613 ml
= g_new(MemoryRegionList
, 1);
1615 ml
->printed
= false;
1616 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1618 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1619 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1620 "-" TARGET_FMT_plx
"\n",
1623 + (hwaddr
)int128_get64(mr
->size
) - 1,
1625 mr
->readable
? 'R' : '-',
1626 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1632 + (hwaddr
)int128_get64(mr
->size
) - 1);
1635 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1638 + (hwaddr
)int128_get64(mr
->size
) - 1,
1640 mr
->readable
? 'R' : '-',
1641 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1646 QTAILQ_INIT(&submr_print_queue
);
1648 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1649 new_ml
= g_new(MemoryRegionList
, 1);
1651 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1652 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1653 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1654 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1655 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1661 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1665 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1666 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1670 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1675 void mtree_info(fprintf_function mon_printf
, void *f
)
1677 MemoryRegionListHead ml_head
;
1678 MemoryRegionList
*ml
, *ml2
;
1681 QTAILQ_INIT(&ml_head
);
1683 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1687 mon_printf(f
, "%s\n", as
->name
);
1688 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
1691 mon_printf(f
, "aliases\n");
1692 /* print aliased regions */
1693 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1695 mon_printf(f
, "%s\n", ml
->mr
->name
);
1696 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1700 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {