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 target_phys_addr_t 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
,
303 target_phys_addr_t addr
,
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
,
320 target_phys_addr_t addr
,
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(target_phys_addr_t addr
,
339 unsigned access_size_min
,
340 unsigned access_size_max
,
341 void (*access
)(void *opaque
,
342 target_phys_addr_t addr
,
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 target_phys_addr_t 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
,
820 target_phys_addr_t addr
,
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
,
846 target_phys_addr_t addr
,
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
,
887 target_phys_addr_t addr
,
892 ret
= memory_region_dispatch_read1(mr
, addr
, size
);
893 adjust_endianness(mr
, &ret
, size
);
897 static void memory_region_dispatch_write(MemoryRegion
*mr
,
898 target_phys_addr_t addr
,
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
,
960 target_phys_addr_t offset
,
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
, target_phys_addr_t 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
, target_phys_addr_t 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
));
1023 memory_region_clear_coalescing(mr
);
1024 g_free((char *)mr
->name
);
1025 g_free(mr
->ioeventfds
);
1028 uint64_t memory_region_size(MemoryRegion
*mr
)
1030 if (int128_eq(mr
->size
, int128_2_64())) {
1033 return int128_get64(mr
->size
);
1036 const char *memory_region_name(MemoryRegion
*mr
)
1041 bool memory_region_is_ram(MemoryRegion
*mr
)
1046 bool memory_region_is_logging(MemoryRegion
*mr
)
1048 return mr
->dirty_log_mask
;
1051 bool memory_region_is_rom(MemoryRegion
*mr
)
1053 return mr
->ram
&& mr
->readonly
;
1056 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1058 uint8_t mask
= 1 << client
;
1060 memory_region_transaction_begin();
1061 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1062 memory_region_transaction_commit();
1065 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1066 target_phys_addr_t size
, unsigned client
)
1068 assert(mr
->terminates
);
1069 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1073 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1074 target_phys_addr_t size
)
1076 assert(mr
->terminates
);
1077 return cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
, -1);
1080 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1085 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1086 FOR_EACH_FLAT_RANGE(fr
, as
->current_map
) {
1088 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1094 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1096 if (mr
->readonly
!= readonly
) {
1097 memory_region_transaction_begin();
1098 mr
->readonly
= readonly
;
1099 memory_region_transaction_commit();
1103 void memory_region_rom_device_set_readable(MemoryRegion
*mr
, bool readable
)
1105 if (mr
->readable
!= readable
) {
1106 memory_region_transaction_begin();
1107 mr
->readable
= readable
;
1108 memory_region_transaction_commit();
1112 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1113 target_phys_addr_t size
, unsigned client
)
1115 assert(mr
->terminates
);
1116 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1117 mr
->ram_addr
+ addr
+ size
,
1121 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1124 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1127 assert(mr
->terminates
);
1129 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1132 static void memory_region_update_coalesced_range_as(MemoryRegion
*mr
, AddressSpace
*as
)
1135 CoalescedMemoryRange
*cmr
;
1137 MemoryRegionSection section
;
1139 FOR_EACH_FLAT_RANGE(fr
, as
->current_map
) {
1141 section
= (MemoryRegionSection
) {
1142 .address_space
= as
,
1143 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1144 .size
= int128_get64(fr
->addr
.size
),
1147 MEMORY_LISTENER_CALL(coalesced_mmio_del
, Reverse
, §ion
,
1148 int128_get64(fr
->addr
.start
),
1149 int128_get64(fr
->addr
.size
));
1150 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1151 tmp
= addrrange_shift(cmr
->addr
,
1152 int128_sub(fr
->addr
.start
,
1153 int128_make64(fr
->offset_in_region
)));
1154 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1157 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1158 MEMORY_LISTENER_CALL(coalesced_mmio_add
, Forward
, §ion
,
1159 int128_get64(tmp
.start
),
1160 int128_get64(tmp
.size
));
1166 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1170 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1171 memory_region_update_coalesced_range_as(mr
, as
);
1175 void memory_region_set_coalescing(MemoryRegion
*mr
)
1177 memory_region_clear_coalescing(mr
);
1178 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1181 void memory_region_add_coalescing(MemoryRegion
*mr
,
1182 target_phys_addr_t offset
,
1185 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1187 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1188 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1189 memory_region_update_coalesced_range(mr
);
1190 memory_region_set_flush_coalesced(mr
);
1193 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1195 CoalescedMemoryRange
*cmr
;
1197 qemu_flush_coalesced_mmio_buffer();
1198 mr
->flush_coalesced_mmio
= false;
1200 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1201 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1202 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1205 memory_region_update_coalesced_range(mr
);
1208 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1210 mr
->flush_coalesced_mmio
= true;
1213 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1215 qemu_flush_coalesced_mmio_buffer();
1216 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1217 mr
->flush_coalesced_mmio
= false;
1221 void memory_region_add_eventfd(MemoryRegion
*mr
,
1222 target_phys_addr_t addr
,
1228 MemoryRegionIoeventfd mrfd
= {
1229 .addr
.start
= int128_make64(addr
),
1230 .addr
.size
= int128_make64(size
),
1231 .match_data
= match_data
,
1237 memory_region_transaction_begin();
1238 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1239 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1244 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1245 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1246 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1247 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1248 mr
->ioeventfds
[i
] = mrfd
;
1249 memory_region_transaction_commit();
1252 void memory_region_del_eventfd(MemoryRegion
*mr
,
1253 target_phys_addr_t addr
,
1259 MemoryRegionIoeventfd mrfd
= {
1260 .addr
.start
= int128_make64(addr
),
1261 .addr
.size
= int128_make64(size
),
1262 .match_data
= match_data
,
1268 memory_region_transaction_begin();
1269 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1270 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1274 assert(i
!= mr
->ioeventfd_nb
);
1275 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1276 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1278 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1279 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1280 memory_region_transaction_commit();
1283 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1284 target_phys_addr_t offset
,
1285 MemoryRegion
*subregion
)
1287 MemoryRegion
*other
;
1289 memory_region_transaction_begin();
1291 assert(!subregion
->parent
);
1292 subregion
->parent
= mr
;
1293 subregion
->addr
= offset
;
1294 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1295 if (subregion
->may_overlap
|| other
->may_overlap
) {
1298 if (int128_gt(int128_make64(offset
),
1299 int128_add(int128_make64(other
->addr
), other
->size
))
1300 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1301 int128_make64(other
->addr
))) {
1305 printf("warning: subregion collision %llx/%llx (%s) "
1306 "vs %llx/%llx (%s)\n",
1307 (unsigned long long)offset
,
1308 (unsigned long long)int128_get64(subregion
->size
),
1310 (unsigned long long)other
->addr
,
1311 (unsigned long long)int128_get64(other
->size
),
1315 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1316 if (subregion
->priority
>= other
->priority
) {
1317 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1321 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1323 memory_region_transaction_commit();
1327 void memory_region_add_subregion(MemoryRegion
*mr
,
1328 target_phys_addr_t offset
,
1329 MemoryRegion
*subregion
)
1331 subregion
->may_overlap
= false;
1332 subregion
->priority
= 0;
1333 memory_region_add_subregion_common(mr
, offset
, subregion
);
1336 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1337 target_phys_addr_t offset
,
1338 MemoryRegion
*subregion
,
1341 subregion
->may_overlap
= true;
1342 subregion
->priority
= priority
;
1343 memory_region_add_subregion_common(mr
, offset
, subregion
);
1346 void memory_region_del_subregion(MemoryRegion
*mr
,
1347 MemoryRegion
*subregion
)
1349 memory_region_transaction_begin();
1350 assert(subregion
->parent
== mr
);
1351 subregion
->parent
= NULL
;
1352 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1353 memory_region_transaction_commit();
1356 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1358 if (enabled
== mr
->enabled
) {
1361 memory_region_transaction_begin();
1362 mr
->enabled
= enabled
;
1363 memory_region_transaction_commit();
1366 void memory_region_set_address(MemoryRegion
*mr
, target_phys_addr_t addr
)
1368 MemoryRegion
*parent
= mr
->parent
;
1369 unsigned priority
= mr
->priority
;
1370 bool may_overlap
= mr
->may_overlap
;
1372 if (addr
== mr
->addr
|| !parent
) {
1377 memory_region_transaction_begin();
1378 memory_region_del_subregion(parent
, mr
);
1380 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1382 memory_region_add_subregion(parent
, addr
, mr
);
1384 memory_region_transaction_commit();
1387 void memory_region_set_alias_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1391 if (offset
== mr
->alias_offset
) {
1395 memory_region_transaction_begin();
1396 mr
->alias_offset
= offset
;
1397 memory_region_transaction_commit();
1400 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1402 return mr
->ram_addr
;
1405 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1407 const AddrRange
*addr
= addr_
;
1408 const FlatRange
*fr
= fr_
;
1410 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1412 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1418 static FlatRange
*address_space_lookup(AddressSpace
*as
, AddrRange addr
)
1420 return bsearch(&addr
, as
->current_map
->ranges
, as
->current_map
->nr
,
1421 sizeof(FlatRange
), cmp_flatrange_addr
);
1424 MemoryRegionSection
memory_region_find(MemoryRegion
*address_space
,
1425 target_phys_addr_t addr
, uint64_t size
)
1427 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1428 AddrRange range
= addrrange_make(int128_make64(addr
),
1429 int128_make64(size
));
1430 FlatRange
*fr
= address_space_lookup(as
, range
);
1431 MemoryRegionSection ret
= { .mr
= NULL
, .size
= 0 };
1437 while (fr
> as
->current_map
->ranges
1438 && addrrange_intersects(fr
[-1].addr
, range
)) {
1443 range
= addrrange_intersection(range
, fr
->addr
);
1444 ret
.offset_within_region
= fr
->offset_in_region
;
1445 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1447 ret
.size
= int128_get64(range
.size
);
1448 ret
.offset_within_address_space
= int128_get64(range
.start
);
1449 ret
.readonly
= fr
->readonly
;
1453 void memory_global_sync_dirty_bitmap(MemoryRegion
*address_space
)
1455 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1458 FOR_EACH_FLAT_RANGE(fr
, as
->current_map
) {
1459 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1463 void memory_global_dirty_log_start(void)
1465 global_dirty_log
= true;
1466 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1469 void memory_global_dirty_log_stop(void)
1471 global_dirty_log
= false;
1472 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1475 static void listener_add_address_space(MemoryListener
*listener
,
1480 if (listener
->address_space_filter
1481 && listener
->address_space_filter
!= as
) {
1485 if (global_dirty_log
) {
1486 if (listener
->log_global_start
) {
1487 listener
->log_global_start(listener
);
1491 FOR_EACH_FLAT_RANGE(fr
, as
->current_map
) {
1492 MemoryRegionSection section
= {
1494 .address_space
= as
,
1495 .offset_within_region
= fr
->offset_in_region
,
1496 .size
= int128_get64(fr
->addr
.size
),
1497 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1498 .readonly
= fr
->readonly
,
1500 if (listener
->region_add
) {
1501 listener
->region_add(listener
, §ion
);
1506 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
)
1508 MemoryListener
*other
= NULL
;
1511 listener
->address_space_filter
= filter
;
1512 if (QTAILQ_EMPTY(&memory_listeners
)
1513 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1514 memory_listeners
)->priority
) {
1515 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1517 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1518 if (listener
->priority
< other
->priority
) {
1522 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1525 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1526 listener_add_address_space(listener
, as
);
1530 void memory_listener_unregister(MemoryListener
*listener
)
1532 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1535 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
)
1537 memory_region_transaction_begin();
1539 as
->current_map
= g_new(FlatView
, 1);
1540 flatview_init(as
->current_map
);
1541 QTAILQ_INSERT_TAIL(&address_spaces
, as
, address_spaces_link
);
1543 memory_region_transaction_commit();
1544 address_space_init_dispatch(as
);
1547 void address_space_destroy(AddressSpace
*as
)
1549 /* Flush out anything from MemoryListeners listening in on this */
1550 memory_region_transaction_begin();
1552 memory_region_transaction_commit();
1553 QTAILQ_REMOVE(&address_spaces
, as
, address_spaces_link
);
1554 address_space_destroy_dispatch(as
);
1555 flatview_destroy(as
->current_map
);
1556 g_free(as
->current_map
);
1559 uint64_t io_mem_read(MemoryRegion
*mr
, target_phys_addr_t addr
, unsigned size
)
1561 return memory_region_dispatch_read(mr
, addr
, size
);
1564 void io_mem_write(MemoryRegion
*mr
, target_phys_addr_t addr
,
1565 uint64_t val
, unsigned size
)
1567 memory_region_dispatch_write(mr
, addr
, val
, size
);
1570 typedef struct MemoryRegionList MemoryRegionList
;
1572 struct MemoryRegionList
{
1573 const MemoryRegion
*mr
;
1575 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1578 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1580 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1581 const MemoryRegion
*mr
, unsigned int level
,
1582 target_phys_addr_t base
,
1583 MemoryRegionListHead
*alias_print_queue
)
1585 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1586 MemoryRegionListHead submr_print_queue
;
1587 const MemoryRegion
*submr
;
1594 for (i
= 0; i
< level
; i
++) {
1599 MemoryRegionList
*ml
;
1602 /* check if the alias is already in the queue */
1603 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1604 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1610 ml
= g_new(MemoryRegionList
, 1);
1612 ml
->printed
= false;
1613 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1615 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1616 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1617 "-" TARGET_FMT_plx
"\n",
1620 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1622 mr
->readable
? 'R' : '-',
1623 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1629 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1);
1632 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1635 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1637 mr
->readable
? 'R' : '-',
1638 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1643 QTAILQ_INIT(&submr_print_queue
);
1645 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1646 new_ml
= g_new(MemoryRegionList
, 1);
1648 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1649 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1650 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1651 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1652 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1658 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1662 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1663 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1667 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1672 void mtree_info(fprintf_function mon_printf
, void *f
)
1674 MemoryRegionListHead ml_head
;
1675 MemoryRegionList
*ml
, *ml2
;
1678 QTAILQ_INIT(&ml_head
);
1680 QTAILQ_FOREACH(as
, &address_spaces
, address_spaces_link
) {
1684 mon_printf(f
, "%s\n", as
->name
);
1685 mtree_print_mr(mon_printf
, f
, as
->root
, 0, 0, &ml_head
);
1688 mon_printf(f
, "aliases\n");
1689 /* print aliased regions */
1690 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1692 mon_printf(f
, "%s\n", ml
->mr
->name
);
1693 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1697 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {