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 #define WANT_EXEC_OBSOLETE
24 #include "exec-obsolete.h"
26 unsigned memory_region_transaction_depth
= 0;
27 static bool global_dirty_log
= false;
29 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
30 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
32 typedef struct AddrRange AddrRange
;
35 * Note using signed integers limits us to physical addresses at most
36 * 63 bits wide. They are needed for negative offsetting in aliases
37 * (large MemoryRegion::alias_offset).
44 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
46 return (AddrRange
) { start
, size
};
49 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
51 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
54 static Int128
addrrange_end(AddrRange r
)
56 return int128_add(r
.start
, r
.size
);
59 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
61 int128_addto(&range
.start
, delta
);
65 static bool addrrange_contains(AddrRange range
, Int128 addr
)
67 return int128_ge(addr
, range
.start
)
68 && int128_lt(addr
, addrrange_end(range
));
71 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
73 return addrrange_contains(r1
, r2
.start
)
74 || addrrange_contains(r2
, r1
.start
);
77 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
79 Int128 start
= int128_max(r1
.start
, r2
.start
);
80 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
81 return addrrange_make(start
, int128_sub(end
, start
));
84 enum ListenerDirection
{ Forward
, Reverse
};
86 static bool memory_listener_match(MemoryListener
*listener
,
87 MemoryRegionSection
*section
)
89 return !listener
->address_space_filter
90 || listener
->address_space_filter
== section
->address_space
;
93 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
95 MemoryListener *_listener; \
97 switch (_direction) { \
99 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
100 _listener->_callback(_listener, ##_args); \
104 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
105 memory_listeners, link) { \
106 _listener->_callback(_listener, ##_args); \
114 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
116 MemoryListener *_listener; \
118 switch (_direction) { \
120 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
121 if (memory_listener_match(_listener, _section)) { \
122 _listener->_callback(_listener, _section, ##_args); \
127 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
128 memory_listeners, link) { \
129 if (memory_listener_match(_listener, _section)) { \
130 _listener->_callback(_listener, _section, ##_args); \
139 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
140 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
142 .address_space = (as)->root, \
143 .offset_within_region = (fr)->offset_in_region, \
144 .size = int128_get64((fr)->addr.size), \
145 .offset_within_address_space = int128_get64((fr)->addr.start), \
146 .readonly = (fr)->readonly, \
149 struct CoalescedMemoryRange
{
151 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
154 struct MemoryRegionIoeventfd
{
161 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
162 MemoryRegionIoeventfd b
)
164 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
166 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
168 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
170 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
172 } else if (a
.match_data
< b
.match_data
) {
174 } else if (a
.match_data
> b
.match_data
) {
176 } else if (a
.match_data
) {
177 if (a
.data
< b
.data
) {
179 } else if (a
.data
> b
.data
) {
185 } else if (a
.e
> b
.e
) {
191 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
192 MemoryRegionIoeventfd b
)
194 return !memory_region_ioeventfd_before(a
, b
)
195 && !memory_region_ioeventfd_before(b
, a
);
198 typedef struct FlatRange FlatRange
;
199 typedef struct FlatView FlatView
;
201 /* Range of memory in the global map. Addresses are absolute. */
204 target_phys_addr_t offset_in_region
;
206 uint8_t dirty_log_mask
;
211 /* Flattened global view of current active memory hierarchy. Kept in sorted
217 unsigned nr_allocated
;
220 typedef struct AddressSpace AddressSpace
;
221 typedef struct AddressSpaceOps AddressSpaceOps
;
223 /* A system address space - I/O, memory, etc. */
224 struct AddressSpace
{
226 FlatView current_map
;
228 MemoryRegionIoeventfd
*ioeventfds
;
231 #define FOR_EACH_FLAT_RANGE(var, view) \
232 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
234 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
236 return a
->mr
== b
->mr
237 && addrrange_equal(a
->addr
, b
->addr
)
238 && a
->offset_in_region
== b
->offset_in_region
239 && a
->readable
== b
->readable
240 && a
->readonly
== b
->readonly
;
243 static void flatview_init(FlatView
*view
)
247 view
->nr_allocated
= 0;
250 /* Insert a range into a given position. Caller is responsible for maintaining
253 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
255 if (view
->nr
== view
->nr_allocated
) {
256 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
257 view
->ranges
= g_realloc(view
->ranges
,
258 view
->nr_allocated
* sizeof(*view
->ranges
));
260 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
261 (view
->nr
- pos
) * sizeof(FlatRange
));
262 view
->ranges
[pos
] = *range
;
266 static void flatview_destroy(FlatView
*view
)
268 g_free(view
->ranges
);
271 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
273 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
275 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
277 int128_make64(r2
->offset_in_region
))
278 && r1
->dirty_log_mask
== r2
->dirty_log_mask
279 && r1
->readable
== r2
->readable
280 && r1
->readonly
== r2
->readonly
;
283 /* Attempt to simplify a view by merging ajacent ranges */
284 static void flatview_simplify(FlatView
*view
)
289 while (i
< view
->nr
) {
292 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
293 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
297 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
298 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
303 static void memory_region_read_accessor(void *opaque
,
304 target_phys_addr_t addr
,
310 MemoryRegion
*mr
= opaque
;
313 if (mr
->flush_coalesced_mmio
) {
314 qemu_flush_coalesced_mmio_buffer();
316 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
317 *value
|= (tmp
& mask
) << shift
;
320 static void memory_region_write_accessor(void *opaque
,
321 target_phys_addr_t addr
,
327 MemoryRegion
*mr
= opaque
;
330 if (mr
->flush_coalesced_mmio
) {
331 qemu_flush_coalesced_mmio_buffer();
333 tmp
= (*value
>> shift
) & mask
;
334 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
337 static void access_with_adjusted_size(target_phys_addr_t addr
,
340 unsigned access_size_min
,
341 unsigned access_size_max
,
342 void (*access
)(void *opaque
,
343 target_phys_addr_t addr
,
350 uint64_t access_mask
;
351 unsigned access_size
;
354 if (!access_size_min
) {
357 if (!access_size_max
) {
360 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
361 access_mask
= -1ULL >> (64 - access_size
* 8);
362 for (i
= 0; i
< size
; i
+= access_size
) {
363 /* FIXME: big-endian support */
364 access(opaque
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
368 static AddressSpace address_space_memory
;
370 static const MemoryRegionPortio
*find_portio(MemoryRegion
*mr
, uint64_t offset
,
371 unsigned width
, bool write
)
373 const MemoryRegionPortio
*mrp
;
375 for (mrp
= mr
->ops
->old_portio
; mrp
->size
; ++mrp
) {
376 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
377 && width
== mrp
->size
378 && (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
385 static void memory_region_iorange_read(IORange
*iorange
,
390 MemoryRegionIORange
*mrio
391 = container_of(iorange
, MemoryRegionIORange
, iorange
);
392 MemoryRegion
*mr
= mrio
->mr
;
394 offset
+= mrio
->offset
;
395 if (mr
->ops
->old_portio
) {
396 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
- mrio
->offset
,
399 *data
= ((uint64_t)1 << (width
* 8)) - 1;
401 *data
= mrp
->read(mr
->opaque
, offset
);
402 } else if (width
== 2) {
403 mrp
= find_portio(mr
, offset
- mrio
->offset
, 1, false);
405 *data
= mrp
->read(mr
->opaque
, offset
) |
406 (mrp
->read(mr
->opaque
, offset
+ 1) << 8);
411 access_with_adjusted_size(offset
, data
, width
,
412 mr
->ops
->impl
.min_access_size
,
413 mr
->ops
->impl
.max_access_size
,
414 memory_region_read_accessor
, mr
);
417 static void memory_region_iorange_write(IORange
*iorange
,
422 MemoryRegionIORange
*mrio
423 = container_of(iorange
, MemoryRegionIORange
, iorange
);
424 MemoryRegion
*mr
= mrio
->mr
;
426 offset
+= mrio
->offset
;
427 if (mr
->ops
->old_portio
) {
428 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
- mrio
->offset
,
432 mrp
->write(mr
->opaque
, offset
, data
);
433 } else if (width
== 2) {
434 mrp
= find_portio(mr
, offset
- mrio
->offset
, 1, true);
436 mrp
->write(mr
->opaque
, offset
, data
& 0xff);
437 mrp
->write(mr
->opaque
, offset
+ 1, data
>> 8);
441 access_with_adjusted_size(offset
, &data
, width
,
442 mr
->ops
->impl
.min_access_size
,
443 mr
->ops
->impl
.max_access_size
,
444 memory_region_write_accessor
, mr
);
447 static void memory_region_iorange_destructor(IORange
*iorange
)
449 g_free(container_of(iorange
, MemoryRegionIORange
, iorange
));
452 const IORangeOps memory_region_iorange_ops
= {
453 .read
= memory_region_iorange_read
,
454 .write
= memory_region_iorange_write
,
455 .destructor
= memory_region_iorange_destructor
,
458 static AddressSpace address_space_io
;
460 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
465 if (mr
== address_space_memory
.root
) {
466 return &address_space_memory
;
468 if (mr
== address_space_io
.root
) {
469 return &address_space_io
;
474 /* Render a memory region into the global view. Ranges in @view obscure
477 static void render_memory_region(FlatView
*view
,
483 MemoryRegion
*subregion
;
485 target_phys_addr_t offset_in_region
;
495 int128_addto(&base
, int128_make64(mr
->addr
));
496 readonly
|= mr
->readonly
;
498 tmp
= addrrange_make(base
, mr
->size
);
500 if (!addrrange_intersects(tmp
, clip
)) {
504 clip
= addrrange_intersection(tmp
, clip
);
507 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
508 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
509 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
513 /* Render subregions in priority order. */
514 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
515 render_memory_region(view
, subregion
, base
, clip
, readonly
);
518 if (!mr
->terminates
) {
522 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
526 /* Render the region itself into any gaps left by the current view. */
527 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
528 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
531 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
532 now
= int128_min(remain
,
533 int128_sub(view
->ranges
[i
].addr
.start
, base
));
535 fr
.offset_in_region
= offset_in_region
;
536 fr
.addr
= addrrange_make(base
, now
);
537 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
538 fr
.readable
= mr
->readable
;
539 fr
.readonly
= readonly
;
540 flatview_insert(view
, i
, &fr
);
542 int128_addto(&base
, now
);
543 offset_in_region
+= int128_get64(now
);
544 int128_subfrom(&remain
, now
);
546 if (int128_eq(base
, view
->ranges
[i
].addr
.start
)) {
547 now
= int128_min(remain
, view
->ranges
[i
].addr
.size
);
548 int128_addto(&base
, now
);
549 offset_in_region
+= int128_get64(now
);
550 int128_subfrom(&remain
, now
);
553 if (int128_nz(remain
)) {
555 fr
.offset_in_region
= offset_in_region
;
556 fr
.addr
= addrrange_make(base
, remain
);
557 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
558 fr
.readable
= mr
->readable
;
559 fr
.readonly
= readonly
;
560 flatview_insert(view
, i
, &fr
);
564 /* Render a memory topology into a list of disjoint absolute ranges. */
565 static FlatView
generate_memory_topology(MemoryRegion
*mr
)
569 flatview_init(&view
);
571 render_memory_region(&view
, mr
, int128_zero(),
572 addrrange_make(int128_zero(), int128_2_64()), false);
573 flatview_simplify(&view
);
578 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
579 MemoryRegionIoeventfd
*fds_new
,
581 MemoryRegionIoeventfd
*fds_old
,
585 MemoryRegionIoeventfd
*fd
;
586 MemoryRegionSection section
;
588 /* Generate a symmetric difference of the old and new fd sets, adding
589 * and deleting as necessary.
593 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
594 if (iold
< fds_old_nb
595 && (inew
== fds_new_nb
596 || memory_region_ioeventfd_before(fds_old
[iold
],
599 section
= (MemoryRegionSection
) {
600 .address_space
= as
->root
,
601 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
602 .size
= int128_get64(fd
->addr
.size
),
604 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
605 fd
->match_data
, fd
->data
, fd
->e
);
607 } else if (inew
< fds_new_nb
608 && (iold
== fds_old_nb
609 || memory_region_ioeventfd_before(fds_new
[inew
],
612 section
= (MemoryRegionSection
) {
613 .address_space
= as
->root
,
614 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
615 .size
= int128_get64(fd
->addr
.size
),
617 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
618 fd
->match_data
, fd
->data
, fd
->e
);
627 static void address_space_update_ioeventfds(AddressSpace
*as
)
630 unsigned ioeventfd_nb
= 0;
631 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
635 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
636 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
637 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
638 int128_sub(fr
->addr
.start
,
639 int128_make64(fr
->offset_in_region
)));
640 if (addrrange_intersects(fr
->addr
, tmp
)) {
642 ioeventfds
= g_realloc(ioeventfds
,
643 ioeventfd_nb
* sizeof(*ioeventfds
));
644 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
645 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
650 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
651 as
->ioeventfds
, as
->ioeventfd_nb
);
653 g_free(as
->ioeventfds
);
654 as
->ioeventfds
= ioeventfds
;
655 as
->ioeventfd_nb
= ioeventfd_nb
;
658 static void address_space_update_topology_pass(AddressSpace
*as
,
664 FlatRange
*frold
, *frnew
;
666 /* Generate a symmetric difference of the old and new memory maps.
667 * Kill ranges in the old map, and instantiate ranges in the new map.
670 while (iold
< old_view
.nr
|| inew
< new_view
.nr
) {
671 if (iold
< old_view
.nr
) {
672 frold
= &old_view
.ranges
[iold
];
676 if (inew
< new_view
.nr
) {
677 frnew
= &new_view
.ranges
[inew
];
684 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
685 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
686 && !flatrange_equal(frold
, frnew
)))) {
687 /* In old, but (not in new, or in new but attributes changed). */
690 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
694 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
695 /* In both (logging may have changed) */
698 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
699 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
700 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
701 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
702 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
712 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
721 static void address_space_update_topology(AddressSpace
*as
)
723 FlatView old_view
= as
->current_map
;
724 FlatView new_view
= generate_memory_topology(as
->root
);
726 address_space_update_topology_pass(as
, old_view
, new_view
, false);
727 address_space_update_topology_pass(as
, old_view
, new_view
, true);
729 as
->current_map
= new_view
;
730 flatview_destroy(&old_view
);
731 address_space_update_ioeventfds(as
);
734 void memory_region_transaction_begin(void)
736 qemu_flush_coalesced_mmio_buffer();
737 ++memory_region_transaction_depth
;
740 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 if (address_space_memory
.root
) {
748 address_space_update_topology(&address_space_memory
);
750 if (address_space_io
.root
) {
751 address_space_update_topology(&address_space_io
);
754 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
758 static void memory_region_destructor_none(MemoryRegion
*mr
)
762 static void memory_region_destructor_ram(MemoryRegion
*mr
)
764 qemu_ram_free(mr
->ram_addr
);
767 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
769 qemu_ram_free_from_ptr(mr
->ram_addr
);
772 static void memory_region_destructor_iomem(MemoryRegion
*mr
)
776 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
778 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
781 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
783 #ifdef TARGET_WORDS_BIGENDIAN
784 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
786 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
790 void memory_region_init(MemoryRegion
*mr
,
796 mr
->size
= int128_make64(size
);
797 if (size
== UINT64_MAX
) {
798 mr
->size
= int128_2_64();
803 mr
->terminates
= false;
806 mr
->readonly
= false;
807 mr
->rom_device
= false;
808 mr
->destructor
= memory_region_destructor_none
;
810 mr
->may_overlap
= false;
812 QTAILQ_INIT(&mr
->subregions
);
813 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
814 QTAILQ_INIT(&mr
->coalesced
);
815 mr
->name
= g_strdup(name
);
816 mr
->dirty_log_mask
= 0;
817 mr
->ioeventfd_nb
= 0;
818 mr
->ioeventfds
= NULL
;
819 mr
->flush_coalesced_mmio
= false;
822 static bool memory_region_access_valid(MemoryRegion
*mr
,
823 target_phys_addr_t addr
,
827 if (mr
->ops
->valid
.accepts
828 && !mr
->ops
->valid
.accepts(mr
->opaque
, addr
, size
, is_write
)) {
832 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
836 /* Treat zero as compatibility all valid */
837 if (!mr
->ops
->valid
.max_access_size
) {
841 if (size
> mr
->ops
->valid
.max_access_size
842 || size
< mr
->ops
->valid
.min_access_size
) {
848 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
849 target_phys_addr_t addr
,
854 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
855 return -1U; /* FIXME: better signalling */
858 if (!mr
->ops
->read
) {
859 return mr
->ops
->old_mmio
.read
[bitops_ffsl(size
)](mr
->opaque
, addr
);
862 /* FIXME: support unaligned access */
863 access_with_adjusted_size(addr
, &data
, size
,
864 mr
->ops
->impl
.min_access_size
,
865 mr
->ops
->impl
.max_access_size
,
866 memory_region_read_accessor
, mr
);
871 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
873 if (memory_region_wrong_endianness(mr
)) {
878 *data
= bswap16(*data
);
881 *data
= bswap32(*data
);
889 static uint64_t memory_region_dispatch_read(MemoryRegion
*mr
,
890 target_phys_addr_t addr
,
895 ret
= memory_region_dispatch_read1(mr
, addr
, size
);
896 adjust_endianness(mr
, &ret
, size
);
900 static void memory_region_dispatch_write(MemoryRegion
*mr
,
901 target_phys_addr_t addr
,
905 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
906 return; /* FIXME: better signalling */
909 adjust_endianness(mr
, &data
, size
);
911 if (!mr
->ops
->write
) {
912 mr
->ops
->old_mmio
.write
[bitops_ffsl(size
)](mr
->opaque
, addr
, data
);
916 /* FIXME: support unaligned access */
917 access_with_adjusted_size(addr
, &data
, size
,
918 mr
->ops
->impl
.min_access_size
,
919 mr
->ops
->impl
.max_access_size
,
920 memory_region_write_accessor
, mr
);
923 void memory_region_init_io(MemoryRegion
*mr
,
924 const MemoryRegionOps
*ops
,
929 memory_region_init(mr
, name
, size
);
932 mr
->terminates
= true;
933 mr
->destructor
= memory_region_destructor_iomem
;
934 mr
->ram_addr
= ~(ram_addr_t
)0;
937 void memory_region_init_ram(MemoryRegion
*mr
,
941 memory_region_init(mr
, name
, size
);
943 mr
->terminates
= true;
944 mr
->destructor
= memory_region_destructor_ram
;
945 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
948 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
953 memory_region_init(mr
, name
, size
);
955 mr
->terminates
= true;
956 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
957 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
960 void memory_region_init_alias(MemoryRegion
*mr
,
963 target_phys_addr_t offset
,
966 memory_region_init(mr
, name
, size
);
968 mr
->alias_offset
= offset
;
971 void memory_region_init_rom_device(MemoryRegion
*mr
,
972 const MemoryRegionOps
*ops
,
977 memory_region_init(mr
, name
, size
);
980 mr
->terminates
= true;
981 mr
->rom_device
= true;
982 mr
->destructor
= memory_region_destructor_rom_device
;
983 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
986 static uint64_t invalid_read(void *opaque
, target_phys_addr_t addr
,
989 MemoryRegion
*mr
= opaque
;
991 if (!mr
->warning_printed
) {
992 fprintf(stderr
, "Invalid read from memory region %s\n", mr
->name
);
993 mr
->warning_printed
= true;
998 static void invalid_write(void *opaque
, target_phys_addr_t addr
, uint64_t data
,
1001 MemoryRegion
*mr
= opaque
;
1003 if (!mr
->warning_printed
) {
1004 fprintf(stderr
, "Invalid write to memory region %s\n", mr
->name
);
1005 mr
->warning_printed
= true;
1009 static const MemoryRegionOps reservation_ops
= {
1010 .read
= invalid_read
,
1011 .write
= invalid_write
,
1012 .endianness
= DEVICE_NATIVE_ENDIAN
,
1015 void memory_region_init_reservation(MemoryRegion
*mr
,
1019 memory_region_init_io(mr
, &reservation_ops
, mr
, name
, size
);
1022 void memory_region_destroy(MemoryRegion
*mr
)
1024 assert(QTAILQ_EMPTY(&mr
->subregions
));
1026 memory_region_clear_coalescing(mr
);
1027 g_free((char *)mr
->name
);
1028 g_free(mr
->ioeventfds
);
1031 uint64_t memory_region_size(MemoryRegion
*mr
)
1033 if (int128_eq(mr
->size
, int128_2_64())) {
1036 return int128_get64(mr
->size
);
1039 const char *memory_region_name(MemoryRegion
*mr
)
1044 bool memory_region_is_ram(MemoryRegion
*mr
)
1049 bool memory_region_is_logging(MemoryRegion
*mr
)
1051 return mr
->dirty_log_mask
;
1054 bool memory_region_is_rom(MemoryRegion
*mr
)
1056 return mr
->ram
&& mr
->readonly
;
1059 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1061 uint8_t mask
= 1 << client
;
1063 memory_region_transaction_begin();
1064 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1065 memory_region_transaction_commit();
1068 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1069 target_phys_addr_t size
, unsigned client
)
1071 assert(mr
->terminates
);
1072 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1076 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1077 target_phys_addr_t size
)
1079 assert(mr
->terminates
);
1080 return cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
, -1);
1083 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1087 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1089 MEMORY_LISTENER_UPDATE_REGION(fr
, &address_space_memory
,
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
, target_phys_addr_t addr
,
1114 target_phys_addr_t 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(MemoryRegion
*mr
)
1136 CoalescedMemoryRange
*cmr
;
1139 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1141 qemu_unregister_coalesced_mmio(int128_get64(fr
->addr
.start
),
1142 int128_get64(fr
->addr
.size
));
1143 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1144 tmp
= addrrange_shift(cmr
->addr
,
1145 int128_sub(fr
->addr
.start
,
1146 int128_make64(fr
->offset_in_region
)));
1147 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1150 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1151 qemu_register_coalesced_mmio(int128_get64(tmp
.start
),
1152 int128_get64(tmp
.size
));
1158 void memory_region_set_coalescing(MemoryRegion
*mr
)
1160 memory_region_clear_coalescing(mr
);
1161 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1164 void memory_region_add_coalescing(MemoryRegion
*mr
,
1165 target_phys_addr_t offset
,
1168 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1170 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1171 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1172 memory_region_update_coalesced_range(mr
);
1173 memory_region_set_flush_coalesced(mr
);
1176 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1178 CoalescedMemoryRange
*cmr
;
1180 qemu_flush_coalesced_mmio_buffer();
1181 mr
->flush_coalesced_mmio
= false;
1183 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1184 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1185 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1188 memory_region_update_coalesced_range(mr
);
1191 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1193 mr
->flush_coalesced_mmio
= true;
1196 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1198 qemu_flush_coalesced_mmio_buffer();
1199 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1200 mr
->flush_coalesced_mmio
= false;
1204 void memory_region_add_eventfd(MemoryRegion
*mr
,
1205 target_phys_addr_t addr
,
1211 MemoryRegionIoeventfd mrfd
= {
1212 .addr
.start
= int128_make64(addr
),
1213 .addr
.size
= int128_make64(size
),
1214 .match_data
= match_data
,
1220 memory_region_transaction_begin();
1221 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1222 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1227 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1228 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1229 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1230 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1231 mr
->ioeventfds
[i
] = mrfd
;
1232 memory_region_transaction_commit();
1235 void memory_region_del_eventfd(MemoryRegion
*mr
,
1236 target_phys_addr_t addr
,
1242 MemoryRegionIoeventfd mrfd
= {
1243 .addr
.start
= int128_make64(addr
),
1244 .addr
.size
= int128_make64(size
),
1245 .match_data
= match_data
,
1251 memory_region_transaction_begin();
1252 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1253 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1257 assert(i
!= mr
->ioeventfd_nb
);
1258 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1259 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1261 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1262 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1263 memory_region_transaction_commit();
1266 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1267 target_phys_addr_t offset
,
1268 MemoryRegion
*subregion
)
1270 MemoryRegion
*other
;
1272 memory_region_transaction_begin();
1274 assert(!subregion
->parent
);
1275 subregion
->parent
= mr
;
1276 subregion
->addr
= offset
;
1277 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1278 if (subregion
->may_overlap
|| other
->may_overlap
) {
1281 if (int128_gt(int128_make64(offset
),
1282 int128_add(int128_make64(other
->addr
), other
->size
))
1283 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1284 int128_make64(other
->addr
))) {
1288 printf("warning: subregion collision %llx/%llx (%s) "
1289 "vs %llx/%llx (%s)\n",
1290 (unsigned long long)offset
,
1291 (unsigned long long)int128_get64(subregion
->size
),
1293 (unsigned long long)other
->addr
,
1294 (unsigned long long)int128_get64(other
->size
),
1298 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1299 if (subregion
->priority
>= other
->priority
) {
1300 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1304 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1306 memory_region_transaction_commit();
1310 void memory_region_add_subregion(MemoryRegion
*mr
,
1311 target_phys_addr_t offset
,
1312 MemoryRegion
*subregion
)
1314 subregion
->may_overlap
= false;
1315 subregion
->priority
= 0;
1316 memory_region_add_subregion_common(mr
, offset
, subregion
);
1319 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1320 target_phys_addr_t offset
,
1321 MemoryRegion
*subregion
,
1324 subregion
->may_overlap
= true;
1325 subregion
->priority
= priority
;
1326 memory_region_add_subregion_common(mr
, offset
, subregion
);
1329 void memory_region_del_subregion(MemoryRegion
*mr
,
1330 MemoryRegion
*subregion
)
1332 memory_region_transaction_begin();
1333 assert(subregion
->parent
== mr
);
1334 subregion
->parent
= NULL
;
1335 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1336 memory_region_transaction_commit();
1339 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1341 if (enabled
== mr
->enabled
) {
1344 memory_region_transaction_begin();
1345 mr
->enabled
= enabled
;
1346 memory_region_transaction_commit();
1349 void memory_region_set_address(MemoryRegion
*mr
, target_phys_addr_t addr
)
1351 MemoryRegion
*parent
= mr
->parent
;
1352 unsigned priority
= mr
->priority
;
1353 bool may_overlap
= mr
->may_overlap
;
1355 if (addr
== mr
->addr
|| !parent
) {
1360 memory_region_transaction_begin();
1361 memory_region_del_subregion(parent
, mr
);
1363 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1365 memory_region_add_subregion(parent
, addr
, mr
);
1367 memory_region_transaction_commit();
1370 void memory_region_set_alias_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1374 if (offset
== mr
->alias_offset
) {
1378 memory_region_transaction_begin();
1379 mr
->alias_offset
= offset
;
1380 memory_region_transaction_commit();
1383 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1385 return mr
->ram_addr
;
1388 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1390 const AddrRange
*addr
= addr_
;
1391 const FlatRange
*fr
= fr_
;
1393 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1395 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1401 static FlatRange
*address_space_lookup(AddressSpace
*as
, AddrRange addr
)
1403 return bsearch(&addr
, as
->current_map
.ranges
, as
->current_map
.nr
,
1404 sizeof(FlatRange
), cmp_flatrange_addr
);
1407 MemoryRegionSection
memory_region_find(MemoryRegion
*address_space
,
1408 target_phys_addr_t addr
, uint64_t size
)
1410 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1411 AddrRange range
= addrrange_make(int128_make64(addr
),
1412 int128_make64(size
));
1413 FlatRange
*fr
= address_space_lookup(as
, range
);
1414 MemoryRegionSection ret
= { .mr
= NULL
, .size
= 0 };
1420 while (fr
> as
->current_map
.ranges
1421 && addrrange_intersects(fr
[-1].addr
, range
)) {
1426 range
= addrrange_intersection(range
, fr
->addr
);
1427 ret
.offset_within_region
= fr
->offset_in_region
;
1428 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1430 ret
.size
= int128_get64(range
.size
);
1431 ret
.offset_within_address_space
= int128_get64(range
.start
);
1432 ret
.readonly
= fr
->readonly
;
1436 void memory_global_sync_dirty_bitmap(MemoryRegion
*address_space
)
1438 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1441 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1442 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1446 void memory_global_dirty_log_start(void)
1448 global_dirty_log
= true;
1449 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1452 void memory_global_dirty_log_stop(void)
1454 global_dirty_log
= false;
1455 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1458 static void listener_add_address_space(MemoryListener
*listener
,
1463 if (listener
->address_space_filter
1464 && listener
->address_space_filter
!= as
->root
) {
1468 if (global_dirty_log
) {
1469 listener
->log_global_start(listener
);
1471 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1472 MemoryRegionSection section
= {
1474 .address_space
= as
->root
,
1475 .offset_within_region
= fr
->offset_in_region
,
1476 .size
= int128_get64(fr
->addr
.size
),
1477 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1478 .readonly
= fr
->readonly
,
1480 listener
->region_add(listener
, §ion
);
1484 void memory_listener_register(MemoryListener
*listener
, MemoryRegion
*filter
)
1486 MemoryListener
*other
= NULL
;
1488 listener
->address_space_filter
= filter
;
1489 if (QTAILQ_EMPTY(&memory_listeners
)
1490 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1491 memory_listeners
)->priority
) {
1492 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1494 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1495 if (listener
->priority
< other
->priority
) {
1499 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1501 listener_add_address_space(listener
, &address_space_memory
);
1502 listener_add_address_space(listener
, &address_space_io
);
1505 void memory_listener_unregister(MemoryListener
*listener
)
1507 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1510 void set_system_memory_map(MemoryRegion
*mr
)
1512 memory_region_transaction_begin();
1513 address_space_memory
.root
= mr
;
1514 memory_region_transaction_commit();
1517 void set_system_io_map(MemoryRegion
*mr
)
1519 memory_region_transaction_begin();
1520 address_space_io
.root
= mr
;
1521 memory_region_transaction_commit();
1524 uint64_t io_mem_read(MemoryRegion
*mr
, target_phys_addr_t addr
, unsigned size
)
1526 return memory_region_dispatch_read(mr
, addr
, size
);
1529 void io_mem_write(MemoryRegion
*mr
, target_phys_addr_t addr
,
1530 uint64_t val
, unsigned size
)
1532 memory_region_dispatch_write(mr
, addr
, val
, size
);
1535 typedef struct MemoryRegionList MemoryRegionList
;
1537 struct MemoryRegionList
{
1538 const MemoryRegion
*mr
;
1540 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1543 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1545 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1546 const MemoryRegion
*mr
, unsigned int level
,
1547 target_phys_addr_t base
,
1548 MemoryRegionListHead
*alias_print_queue
)
1550 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1551 MemoryRegionListHead submr_print_queue
;
1552 const MemoryRegion
*submr
;
1559 for (i
= 0; i
< level
; i
++) {
1564 MemoryRegionList
*ml
;
1567 /* check if the alias is already in the queue */
1568 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1569 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1575 ml
= g_new(MemoryRegionList
, 1);
1577 ml
->printed
= false;
1578 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1580 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1581 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1582 "-" TARGET_FMT_plx
"\n",
1585 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1587 mr
->readable
? 'R' : '-',
1588 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1594 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1);
1597 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1600 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1602 mr
->readable
? 'R' : '-',
1603 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1608 QTAILQ_INIT(&submr_print_queue
);
1610 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1611 new_ml
= g_new(MemoryRegionList
, 1);
1613 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1614 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1615 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1616 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1617 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1623 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1627 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1628 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1632 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1637 void mtree_info(fprintf_function mon_printf
, void *f
)
1639 MemoryRegionListHead ml_head
;
1640 MemoryRegionList
*ml
, *ml2
;
1642 QTAILQ_INIT(&ml_head
);
1644 mon_printf(f
, "memory\n");
1645 mtree_print_mr(mon_printf
, f
, address_space_memory
.root
, 0, 0, &ml_head
);
1647 if (address_space_io
.root
&&
1648 !QTAILQ_EMPTY(&address_space_io
.root
->subregions
)) {
1649 mon_printf(f
, "I/O\n");
1650 mtree_print_mr(mon_printf
, f
, address_space_io
.root
, 0, 0, &ml_head
);
1653 mon_printf(f
, "aliases\n");
1654 /* print aliased regions */
1655 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1657 mon_printf(f
, "%s\n", ml
->mr
->name
);
1658 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1662 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {