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 memory_region_update_pending
= false;
28 static bool global_dirty_log
= false;
30 static QTAILQ_HEAD(memory_listeners
, MemoryListener
) memory_listeners
31 = QTAILQ_HEAD_INITIALIZER(memory_listeners
);
33 typedef struct AddrRange AddrRange
;
36 * Note using signed integers limits us to physical addresses at most
37 * 63 bits wide. They are needed for negative offsetting in aliases
38 * (large MemoryRegion::alias_offset).
45 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
47 return (AddrRange
) { start
, size
};
50 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
52 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
55 static Int128
addrrange_end(AddrRange r
)
57 return int128_add(r
.start
, r
.size
);
60 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
62 int128_addto(&range
.start
, delta
);
66 static bool addrrange_contains(AddrRange range
, Int128 addr
)
68 return int128_ge(addr
, range
.start
)
69 && int128_lt(addr
, addrrange_end(range
));
72 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
74 return addrrange_contains(r1
, r2
.start
)
75 || addrrange_contains(r2
, r1
.start
);
78 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
80 Int128 start
= int128_max(r1
.start
, r2
.start
);
81 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
82 return addrrange_make(start
, int128_sub(end
, start
));
85 enum ListenerDirection
{ Forward
, Reverse
};
87 static bool memory_listener_match(MemoryListener
*listener
,
88 MemoryRegionSection
*section
)
90 return !listener
->address_space_filter
91 || listener
->address_space_filter
== section
->address_space
;
94 #define MEMORY_LISTENER_CALL_GLOBAL(_callback, _direction, _args...) \
96 MemoryListener *_listener; \
98 switch (_direction) { \
100 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
101 _listener->_callback(_listener, ##_args); \
105 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
106 memory_listeners, link) { \
107 _listener->_callback(_listener, ##_args); \
115 #define MEMORY_LISTENER_CALL(_callback, _direction, _section, _args...) \
117 MemoryListener *_listener; \
119 switch (_direction) { \
121 QTAILQ_FOREACH(_listener, &memory_listeners, link) { \
122 if (memory_listener_match(_listener, _section)) { \
123 _listener->_callback(_listener, _section, ##_args); \
128 QTAILQ_FOREACH_REVERSE(_listener, &memory_listeners, \
129 memory_listeners, link) { \
130 if (memory_listener_match(_listener, _section)) { \
131 _listener->_callback(_listener, _section, ##_args); \
140 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, dir, callback) \
141 MEMORY_LISTENER_CALL(callback, dir, (&(MemoryRegionSection) { \
143 .address_space = (as)->root, \
144 .offset_within_region = (fr)->offset_in_region, \
145 .size = int128_get64((fr)->addr.size), \
146 .offset_within_address_space = int128_get64((fr)->addr.start), \
147 .readonly = (fr)->readonly, \
150 struct CoalescedMemoryRange
{
152 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
155 struct MemoryRegionIoeventfd
{
162 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
163 MemoryRegionIoeventfd b
)
165 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
167 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
169 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
171 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
173 } else if (a
.match_data
< b
.match_data
) {
175 } else if (a
.match_data
> b
.match_data
) {
177 } else if (a
.match_data
) {
178 if (a
.data
< b
.data
) {
180 } else if (a
.data
> b
.data
) {
186 } else if (a
.fd
> b
.fd
) {
192 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
193 MemoryRegionIoeventfd b
)
195 return !memory_region_ioeventfd_before(a
, b
)
196 && !memory_region_ioeventfd_before(b
, a
);
199 typedef struct FlatRange FlatRange
;
200 typedef struct FlatView FlatView
;
202 /* Range of memory in the global map. Addresses are absolute. */
205 target_phys_addr_t offset_in_region
;
207 uint8_t dirty_log_mask
;
212 /* Flattened global view of current active memory hierarchy. Kept in sorted
218 unsigned nr_allocated
;
221 typedef struct AddressSpace AddressSpace
;
222 typedef struct AddressSpaceOps AddressSpaceOps
;
224 /* A system address space - I/O, memory, etc. */
225 struct AddressSpace
{
227 FlatView current_map
;
229 MemoryRegionIoeventfd
*ioeventfds
;
232 #define FOR_EACH_FLAT_RANGE(var, view) \
233 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
235 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
237 return a
->mr
== b
->mr
238 && addrrange_equal(a
->addr
, b
->addr
)
239 && a
->offset_in_region
== b
->offset_in_region
240 && a
->readable
== b
->readable
241 && a
->readonly
== b
->readonly
;
244 static void flatview_init(FlatView
*view
)
248 view
->nr_allocated
= 0;
251 /* Insert a range into a given position. Caller is responsible for maintaining
254 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
256 if (view
->nr
== view
->nr_allocated
) {
257 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
258 view
->ranges
= g_realloc(view
->ranges
,
259 view
->nr_allocated
* sizeof(*view
->ranges
));
261 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
262 (view
->nr
- pos
) * sizeof(FlatRange
));
263 view
->ranges
[pos
] = *range
;
267 static void flatview_destroy(FlatView
*view
)
269 g_free(view
->ranges
);
272 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
274 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
276 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
278 int128_make64(r2
->offset_in_region
))
279 && r1
->dirty_log_mask
== r2
->dirty_log_mask
280 && r1
->readable
== r2
->readable
281 && r1
->readonly
== r2
->readonly
;
284 /* Attempt to simplify a view by merging ajacent ranges */
285 static void flatview_simplify(FlatView
*view
)
290 while (i
< view
->nr
) {
293 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
294 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
298 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
299 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
304 static void memory_region_read_accessor(void *opaque
,
305 target_phys_addr_t addr
,
311 MemoryRegion
*mr
= opaque
;
314 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
315 *value
|= (tmp
& mask
) << shift
;
318 static void memory_region_write_accessor(void *opaque
,
319 target_phys_addr_t addr
,
325 MemoryRegion
*mr
= opaque
;
328 tmp
= (*value
>> shift
) & mask
;
329 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
332 static void access_with_adjusted_size(target_phys_addr_t addr
,
335 unsigned access_size_min
,
336 unsigned access_size_max
,
337 void (*access
)(void *opaque
,
338 target_phys_addr_t addr
,
345 uint64_t access_mask
;
346 unsigned access_size
;
349 if (!access_size_min
) {
352 if (!access_size_max
) {
355 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
356 access_mask
= -1ULL >> (64 - access_size
* 8);
357 for (i
= 0; i
< size
; i
+= access_size
) {
358 /* FIXME: big-endian support */
359 access(opaque
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
363 static AddressSpace address_space_memory
;
365 static const MemoryRegionPortio
*find_portio(MemoryRegion
*mr
, uint64_t offset
,
366 unsigned width
, bool write
)
368 const MemoryRegionPortio
*mrp
;
370 for (mrp
= mr
->ops
->old_portio
; mrp
->size
; ++mrp
) {
371 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
372 && width
== mrp
->size
373 && (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
380 static void memory_region_iorange_read(IORange
*iorange
,
385 MemoryRegionIORange
*mrio
386 = container_of(iorange
, MemoryRegionIORange
, iorange
);
387 MemoryRegion
*mr
= mrio
->mr
;
389 offset
+= mrio
->offset
;
390 if (mr
->ops
->old_portio
) {
391 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
- mrio
->offset
,
394 *data
= ((uint64_t)1 << (width
* 8)) - 1;
396 *data
= mrp
->read(mr
->opaque
, offset
);
397 } else if (width
== 2) {
398 mrp
= find_portio(mr
, offset
- mrio
->offset
, 1, false);
400 *data
= mrp
->read(mr
->opaque
, offset
) |
401 (mrp
->read(mr
->opaque
, offset
+ 1) << 8);
406 access_with_adjusted_size(offset
, data
, width
,
407 mr
->ops
->impl
.min_access_size
,
408 mr
->ops
->impl
.max_access_size
,
409 memory_region_read_accessor
, mr
);
412 static void memory_region_iorange_write(IORange
*iorange
,
417 MemoryRegionIORange
*mrio
418 = container_of(iorange
, MemoryRegionIORange
, iorange
);
419 MemoryRegion
*mr
= mrio
->mr
;
421 offset
+= mrio
->offset
;
422 if (mr
->ops
->old_portio
) {
423 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
- mrio
->offset
,
427 mrp
->write(mr
->opaque
, offset
, data
);
428 } else if (width
== 2) {
429 mrp
= find_portio(mr
, offset
- mrio
->offset
, 1, false);
431 mrp
->write(mr
->opaque
, offset
, data
& 0xff);
432 mrp
->write(mr
->opaque
, offset
+ 1, data
>> 8);
436 access_with_adjusted_size(offset
, &data
, width
,
437 mr
->ops
->impl
.min_access_size
,
438 mr
->ops
->impl
.max_access_size
,
439 memory_region_write_accessor
, mr
);
442 static void memory_region_iorange_destructor(IORange
*iorange
)
444 g_free(container_of(iorange
, MemoryRegionIORange
, iorange
));
447 const IORangeOps memory_region_iorange_ops
= {
448 .read
= memory_region_iorange_read
,
449 .write
= memory_region_iorange_write
,
450 .destructor
= memory_region_iorange_destructor
,
453 static AddressSpace address_space_io
;
455 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
460 if (mr
== address_space_memory
.root
) {
461 return &address_space_memory
;
463 if (mr
== address_space_io
.root
) {
464 return &address_space_io
;
469 /* Render a memory region into the global view. Ranges in @view obscure
472 static void render_memory_region(FlatView
*view
,
478 MemoryRegion
*subregion
;
480 target_phys_addr_t offset_in_region
;
490 int128_addto(&base
, int128_make64(mr
->addr
));
491 readonly
|= mr
->readonly
;
493 tmp
= addrrange_make(base
, mr
->size
);
495 if (!addrrange_intersects(tmp
, clip
)) {
499 clip
= addrrange_intersection(tmp
, clip
);
502 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
503 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
504 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
508 /* Render subregions in priority order. */
509 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
510 render_memory_region(view
, subregion
, base
, clip
, readonly
);
513 if (!mr
->terminates
) {
517 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
521 /* Render the region itself into any gaps left by the current view. */
522 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
523 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
526 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
527 now
= int128_min(remain
,
528 int128_sub(view
->ranges
[i
].addr
.start
, base
));
530 fr
.offset_in_region
= offset_in_region
;
531 fr
.addr
= addrrange_make(base
, now
);
532 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
533 fr
.readable
= mr
->readable
;
534 fr
.readonly
= readonly
;
535 flatview_insert(view
, i
, &fr
);
537 int128_addto(&base
, now
);
538 offset_in_region
+= int128_get64(now
);
539 int128_subfrom(&remain
, now
);
541 if (int128_eq(base
, view
->ranges
[i
].addr
.start
)) {
542 now
= int128_min(remain
, view
->ranges
[i
].addr
.size
);
543 int128_addto(&base
, now
);
544 offset_in_region
+= int128_get64(now
);
545 int128_subfrom(&remain
, now
);
548 if (int128_nz(remain
)) {
550 fr
.offset_in_region
= offset_in_region
;
551 fr
.addr
= addrrange_make(base
, remain
);
552 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
553 fr
.readable
= mr
->readable
;
554 fr
.readonly
= readonly
;
555 flatview_insert(view
, i
, &fr
);
559 /* Render a memory topology into a list of disjoint absolute ranges. */
560 static FlatView
generate_memory_topology(MemoryRegion
*mr
)
564 flatview_init(&view
);
566 render_memory_region(&view
, mr
, int128_zero(),
567 addrrange_make(int128_zero(), int128_2_64()), false);
568 flatview_simplify(&view
);
573 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
574 MemoryRegionIoeventfd
*fds_new
,
576 MemoryRegionIoeventfd
*fds_old
,
580 MemoryRegionIoeventfd
*fd
;
581 MemoryRegionSection section
;
583 /* Generate a symmetric difference of the old and new fd sets, adding
584 * and deleting as necessary.
588 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
589 if (iold
< fds_old_nb
590 && (inew
== fds_new_nb
591 || memory_region_ioeventfd_before(fds_old
[iold
],
594 section
= (MemoryRegionSection
) {
595 .address_space
= as
->root
,
596 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
597 .size
= int128_get64(fd
->addr
.size
),
599 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
600 fd
->match_data
, fd
->data
, fd
->fd
);
602 } else if (inew
< fds_new_nb
603 && (iold
== fds_old_nb
604 || memory_region_ioeventfd_before(fds_new
[inew
],
607 section
= (MemoryRegionSection
) {
608 .address_space
= as
->root
,
609 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
610 .size
= int128_get64(fd
->addr
.size
),
612 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
613 fd
->match_data
, fd
->data
, fd
->fd
);
622 static void address_space_update_ioeventfds(AddressSpace
*as
)
625 unsigned ioeventfd_nb
= 0;
626 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
630 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
631 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
632 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
633 int128_sub(fr
->addr
.start
,
634 int128_make64(fr
->offset_in_region
)));
635 if (addrrange_intersects(fr
->addr
, tmp
)) {
637 ioeventfds
= g_realloc(ioeventfds
,
638 ioeventfd_nb
* sizeof(*ioeventfds
));
639 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
640 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
645 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
646 as
->ioeventfds
, as
->ioeventfd_nb
);
648 g_free(as
->ioeventfds
);
649 as
->ioeventfds
= ioeventfds
;
650 as
->ioeventfd_nb
= ioeventfd_nb
;
653 static void address_space_update_topology_pass(AddressSpace
*as
,
659 FlatRange
*frold
, *frnew
;
661 /* Generate a symmetric difference of the old and new memory maps.
662 * Kill ranges in the old map, and instantiate ranges in the new map.
665 while (iold
< old_view
.nr
|| inew
< new_view
.nr
) {
666 if (iold
< old_view
.nr
) {
667 frold
= &old_view
.ranges
[iold
];
671 if (inew
< new_view
.nr
) {
672 frnew
= &new_view
.ranges
[inew
];
679 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
680 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
681 && !flatrange_equal(frold
, frnew
)))) {
682 /* In old, but (not in new, or in new but attributes changed). */
685 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
689 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
690 /* In both (logging may have changed) */
693 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
694 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
695 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
696 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
697 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
707 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
716 static void address_space_update_topology(AddressSpace
*as
)
718 FlatView old_view
= as
->current_map
;
719 FlatView new_view
= generate_memory_topology(as
->root
);
721 address_space_update_topology_pass(as
, old_view
, new_view
, false);
722 address_space_update_topology_pass(as
, old_view
, new_view
, true);
724 as
->current_map
= new_view
;
725 flatview_destroy(&old_view
);
726 address_space_update_ioeventfds(as
);
729 static void memory_region_update_topology(MemoryRegion
*mr
)
731 if (memory_region_transaction_depth
) {
732 memory_region_update_pending
|= !mr
|| mr
->enabled
;
736 if (mr
&& !mr
->enabled
) {
740 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
742 if (address_space_memory
.root
) {
743 address_space_update_topology(&address_space_memory
);
745 if (address_space_io
.root
) {
746 address_space_update_topology(&address_space_io
);
749 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
751 memory_region_update_pending
= false;
754 void memory_region_transaction_begin(void)
756 ++memory_region_transaction_depth
;
759 void memory_region_transaction_commit(void)
761 assert(memory_region_transaction_depth
);
762 --memory_region_transaction_depth
;
763 if (!memory_region_transaction_depth
&& memory_region_update_pending
) {
764 memory_region_update_topology(NULL
);
768 static void memory_region_destructor_none(MemoryRegion
*mr
)
772 static void memory_region_destructor_ram(MemoryRegion
*mr
)
774 qemu_ram_free(mr
->ram_addr
);
777 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
779 qemu_ram_free_from_ptr(mr
->ram_addr
);
782 static void memory_region_destructor_iomem(MemoryRegion
*mr
)
786 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
788 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
791 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
793 #ifdef TARGET_WORDS_BIGENDIAN
794 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
796 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
800 void memory_region_init(MemoryRegion
*mr
,
806 mr
->size
= int128_make64(size
);
807 if (size
== UINT64_MAX
) {
808 mr
->size
= int128_2_64();
813 mr
->terminates
= false;
816 mr
->readonly
= false;
817 mr
->rom_device
= false;
818 mr
->destructor
= memory_region_destructor_none
;
820 mr
->may_overlap
= false;
822 QTAILQ_INIT(&mr
->subregions
);
823 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
824 QTAILQ_INIT(&mr
->coalesced
);
825 mr
->name
= g_strdup(name
);
826 mr
->dirty_log_mask
= 0;
827 mr
->ioeventfd_nb
= 0;
828 mr
->ioeventfds
= NULL
;
831 static bool memory_region_access_valid(MemoryRegion
*mr
,
832 target_phys_addr_t addr
,
836 if (mr
->ops
->valid
.accepts
837 && !mr
->ops
->valid
.accepts(mr
->opaque
, addr
, size
, is_write
)) {
841 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
845 /* Treat zero as compatibility all valid */
846 if (!mr
->ops
->valid
.max_access_size
) {
850 if (size
> mr
->ops
->valid
.max_access_size
851 || size
< mr
->ops
->valid
.min_access_size
) {
857 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
858 target_phys_addr_t addr
,
863 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
864 return -1U; /* FIXME: better signalling */
867 if (!mr
->ops
->read
) {
868 return mr
->ops
->old_mmio
.read
[bitops_ffsl(size
)](mr
->opaque
, addr
);
871 /* FIXME: support unaligned access */
872 access_with_adjusted_size(addr
, &data
, size
,
873 mr
->ops
->impl
.min_access_size
,
874 mr
->ops
->impl
.max_access_size
,
875 memory_region_read_accessor
, mr
);
880 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
882 if (memory_region_wrong_endianness(mr
)) {
887 *data
= bswap16(*data
);
890 *data
= bswap32(*data
);
898 static uint64_t memory_region_dispatch_read(MemoryRegion
*mr
,
899 target_phys_addr_t addr
,
904 ret
= memory_region_dispatch_read1(mr
, addr
, size
);
905 adjust_endianness(mr
, &ret
, size
);
909 static void memory_region_dispatch_write(MemoryRegion
*mr
,
910 target_phys_addr_t addr
,
914 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
915 return; /* FIXME: better signalling */
918 adjust_endianness(mr
, &data
, size
);
920 if (!mr
->ops
->write
) {
921 mr
->ops
->old_mmio
.write
[bitops_ffsl(size
)](mr
->opaque
, addr
, data
);
925 /* FIXME: support unaligned access */
926 access_with_adjusted_size(addr
, &data
, size
,
927 mr
->ops
->impl
.min_access_size
,
928 mr
->ops
->impl
.max_access_size
,
929 memory_region_write_accessor
, mr
);
932 void memory_region_init_io(MemoryRegion
*mr
,
933 const MemoryRegionOps
*ops
,
938 memory_region_init(mr
, name
, size
);
941 mr
->terminates
= true;
942 mr
->destructor
= memory_region_destructor_iomem
;
943 mr
->ram_addr
= ~(ram_addr_t
)0;
946 void memory_region_init_ram(MemoryRegion
*mr
,
950 memory_region_init(mr
, name
, size
);
952 mr
->terminates
= true;
953 mr
->destructor
= memory_region_destructor_ram
;
954 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
957 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
962 memory_region_init(mr
, name
, size
);
964 mr
->terminates
= true;
965 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
966 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
969 void memory_region_init_alias(MemoryRegion
*mr
,
972 target_phys_addr_t offset
,
975 memory_region_init(mr
, name
, size
);
977 mr
->alias_offset
= offset
;
980 void memory_region_init_rom_device(MemoryRegion
*mr
,
981 const MemoryRegionOps
*ops
,
986 memory_region_init(mr
, name
, size
);
989 mr
->terminates
= true;
990 mr
->rom_device
= true;
991 mr
->destructor
= memory_region_destructor_rom_device
;
992 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
995 static uint64_t invalid_read(void *opaque
, target_phys_addr_t addr
,
998 MemoryRegion
*mr
= opaque
;
1000 if (!mr
->warning_printed
) {
1001 fprintf(stderr
, "Invalid read from memory region %s\n", mr
->name
);
1002 mr
->warning_printed
= true;
1007 static void invalid_write(void *opaque
, target_phys_addr_t addr
, uint64_t data
,
1010 MemoryRegion
*mr
= opaque
;
1012 if (!mr
->warning_printed
) {
1013 fprintf(stderr
, "Invalid write to memory region %s\n", mr
->name
);
1014 mr
->warning_printed
= true;
1018 static const MemoryRegionOps reservation_ops
= {
1019 .read
= invalid_read
,
1020 .write
= invalid_write
,
1021 .endianness
= DEVICE_NATIVE_ENDIAN
,
1024 void memory_region_init_reservation(MemoryRegion
*mr
,
1028 memory_region_init_io(mr
, &reservation_ops
, mr
, name
, size
);
1031 void memory_region_destroy(MemoryRegion
*mr
)
1033 assert(QTAILQ_EMPTY(&mr
->subregions
));
1035 memory_region_clear_coalescing(mr
);
1036 g_free((char *)mr
->name
);
1037 g_free(mr
->ioeventfds
);
1040 uint64_t memory_region_size(MemoryRegion
*mr
)
1042 if (int128_eq(mr
->size
, int128_2_64())) {
1045 return int128_get64(mr
->size
);
1048 const char *memory_region_name(MemoryRegion
*mr
)
1053 bool memory_region_is_ram(MemoryRegion
*mr
)
1058 bool memory_region_is_logging(MemoryRegion
*mr
)
1060 return mr
->dirty_log_mask
;
1063 bool memory_region_is_rom(MemoryRegion
*mr
)
1065 return mr
->ram
&& mr
->readonly
;
1068 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1070 uint8_t mask
= 1 << client
;
1072 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1073 memory_region_update_topology(mr
);
1076 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1077 target_phys_addr_t size
, unsigned client
)
1079 assert(mr
->terminates
);
1080 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1084 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1085 target_phys_addr_t size
)
1087 assert(mr
->terminates
);
1088 return cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
, -1);
1091 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1095 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1097 MEMORY_LISTENER_UPDATE_REGION(fr
, &address_space_memory
,
1103 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1105 if (mr
->readonly
!= readonly
) {
1106 mr
->readonly
= readonly
;
1107 memory_region_update_topology(mr
);
1111 void memory_region_rom_device_set_readable(MemoryRegion
*mr
, bool readable
)
1113 if (mr
->readable
!= readable
) {
1114 mr
->readable
= readable
;
1115 memory_region_update_topology(mr
);
1119 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1120 target_phys_addr_t size
, unsigned client
)
1122 assert(mr
->terminates
);
1123 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1124 mr
->ram_addr
+ addr
+ size
,
1128 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1131 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1134 assert(mr
->terminates
);
1136 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1139 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1142 CoalescedMemoryRange
*cmr
;
1145 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1147 qemu_unregister_coalesced_mmio(int128_get64(fr
->addr
.start
),
1148 int128_get64(fr
->addr
.size
));
1149 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1150 tmp
= addrrange_shift(cmr
->addr
,
1151 int128_sub(fr
->addr
.start
,
1152 int128_make64(fr
->offset_in_region
)));
1153 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1156 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1157 qemu_register_coalesced_mmio(int128_get64(tmp
.start
),
1158 int128_get64(tmp
.size
));
1164 void memory_region_set_coalescing(MemoryRegion
*mr
)
1166 memory_region_clear_coalescing(mr
);
1167 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1170 void memory_region_add_coalescing(MemoryRegion
*mr
,
1171 target_phys_addr_t offset
,
1174 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1176 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1177 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1178 memory_region_update_coalesced_range(mr
);
1181 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1183 CoalescedMemoryRange
*cmr
;
1185 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1186 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1187 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1190 memory_region_update_coalesced_range(mr
);
1193 void memory_region_add_eventfd(MemoryRegion
*mr
,
1194 target_phys_addr_t addr
,
1200 MemoryRegionIoeventfd mrfd
= {
1201 .addr
.start
= int128_make64(addr
),
1202 .addr
.size
= int128_make64(size
),
1203 .match_data
= match_data
,
1209 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1210 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1215 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1216 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1217 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1218 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1219 mr
->ioeventfds
[i
] = mrfd
;
1220 memory_region_update_topology(mr
);
1223 void memory_region_del_eventfd(MemoryRegion
*mr
,
1224 target_phys_addr_t addr
,
1230 MemoryRegionIoeventfd mrfd
= {
1231 .addr
.start
= int128_make64(addr
),
1232 .addr
.size
= int128_make64(size
),
1233 .match_data
= match_data
,
1239 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1240 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1244 assert(i
!= mr
->ioeventfd_nb
);
1245 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1246 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1248 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1249 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1250 memory_region_update_topology(mr
);
1253 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1254 target_phys_addr_t offset
,
1255 MemoryRegion
*subregion
)
1257 MemoryRegion
*other
;
1259 assert(!subregion
->parent
);
1260 subregion
->parent
= mr
;
1261 subregion
->addr
= offset
;
1262 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1263 if (subregion
->may_overlap
|| other
->may_overlap
) {
1266 if (int128_gt(int128_make64(offset
),
1267 int128_add(int128_make64(other
->addr
), other
->size
))
1268 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1269 int128_make64(other
->addr
))) {
1273 printf("warning: subregion collision %llx/%llx (%s) "
1274 "vs %llx/%llx (%s)\n",
1275 (unsigned long long)offset
,
1276 (unsigned long long)int128_get64(subregion
->size
),
1278 (unsigned long long)other
->addr
,
1279 (unsigned long long)int128_get64(other
->size
),
1283 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1284 if (subregion
->priority
>= other
->priority
) {
1285 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1289 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1291 memory_region_update_topology(mr
);
1295 void memory_region_add_subregion(MemoryRegion
*mr
,
1296 target_phys_addr_t offset
,
1297 MemoryRegion
*subregion
)
1299 subregion
->may_overlap
= false;
1300 subregion
->priority
= 0;
1301 memory_region_add_subregion_common(mr
, offset
, subregion
);
1304 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1305 target_phys_addr_t offset
,
1306 MemoryRegion
*subregion
,
1309 subregion
->may_overlap
= true;
1310 subregion
->priority
= priority
;
1311 memory_region_add_subregion_common(mr
, offset
, subregion
);
1314 void memory_region_del_subregion(MemoryRegion
*mr
,
1315 MemoryRegion
*subregion
)
1317 assert(subregion
->parent
== mr
);
1318 subregion
->parent
= NULL
;
1319 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1320 memory_region_update_topology(mr
);
1323 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1325 if (enabled
== mr
->enabled
) {
1328 mr
->enabled
= enabled
;
1329 memory_region_update_topology(NULL
);
1332 void memory_region_set_address(MemoryRegion
*mr
, target_phys_addr_t addr
)
1334 MemoryRegion
*parent
= mr
->parent
;
1335 unsigned priority
= mr
->priority
;
1336 bool may_overlap
= mr
->may_overlap
;
1338 if (addr
== mr
->addr
|| !parent
) {
1343 memory_region_transaction_begin();
1344 memory_region_del_subregion(parent
, mr
);
1346 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1348 memory_region_add_subregion(parent
, addr
, mr
);
1350 memory_region_transaction_commit();
1353 void memory_region_set_alias_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1355 target_phys_addr_t old_offset
= mr
->alias_offset
;
1358 mr
->alias_offset
= offset
;
1360 if (offset
== old_offset
|| !mr
->parent
) {
1364 memory_region_update_topology(mr
);
1367 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1369 return mr
->ram_addr
;
1372 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1374 const AddrRange
*addr
= addr_
;
1375 const FlatRange
*fr
= fr_
;
1377 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1379 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1385 static FlatRange
*address_space_lookup(AddressSpace
*as
, AddrRange addr
)
1387 return bsearch(&addr
, as
->current_map
.ranges
, as
->current_map
.nr
,
1388 sizeof(FlatRange
), cmp_flatrange_addr
);
1391 MemoryRegionSection
memory_region_find(MemoryRegion
*address_space
,
1392 target_phys_addr_t addr
, uint64_t size
)
1394 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1395 AddrRange range
= addrrange_make(int128_make64(addr
),
1396 int128_make64(size
));
1397 FlatRange
*fr
= address_space_lookup(as
, range
);
1398 MemoryRegionSection ret
= { .mr
= NULL
, .size
= 0 };
1404 while (fr
> as
->current_map
.ranges
1405 && addrrange_intersects(fr
[-1].addr
, range
)) {
1410 range
= addrrange_intersection(range
, fr
->addr
);
1411 ret
.offset_within_region
= fr
->offset_in_region
;
1412 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1414 ret
.size
= int128_get64(range
.size
);
1415 ret
.offset_within_address_space
= int128_get64(range
.start
);
1416 ret
.readonly
= fr
->readonly
;
1420 void memory_global_sync_dirty_bitmap(MemoryRegion
*address_space
)
1422 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1425 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1426 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1430 void memory_global_dirty_log_start(void)
1432 global_dirty_log
= true;
1433 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1436 void memory_global_dirty_log_stop(void)
1438 global_dirty_log
= false;
1439 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1442 static void listener_add_address_space(MemoryListener
*listener
,
1447 if (global_dirty_log
) {
1448 listener
->log_global_start(listener
);
1450 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1451 MemoryRegionSection section
= {
1453 .address_space
= as
->root
,
1454 .offset_within_region
= fr
->offset_in_region
,
1455 .size
= int128_get64(fr
->addr
.size
),
1456 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1457 .readonly
= fr
->readonly
,
1459 listener
->region_add(listener
, §ion
);
1463 void memory_listener_register(MemoryListener
*listener
, MemoryRegion
*filter
)
1465 MemoryListener
*other
= NULL
;
1467 listener
->address_space_filter
= filter
;
1468 if (QTAILQ_EMPTY(&memory_listeners
)
1469 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1470 memory_listeners
)->priority
) {
1471 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1473 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1474 if (listener
->priority
< other
->priority
) {
1478 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1480 listener_add_address_space(listener
, &address_space_memory
);
1481 listener_add_address_space(listener
, &address_space_io
);
1484 void memory_listener_unregister(MemoryListener
*listener
)
1486 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1489 void set_system_memory_map(MemoryRegion
*mr
)
1491 address_space_memory
.root
= mr
;
1492 memory_region_update_topology(NULL
);
1495 void set_system_io_map(MemoryRegion
*mr
)
1497 address_space_io
.root
= mr
;
1498 memory_region_update_topology(NULL
);
1501 uint64_t io_mem_read(MemoryRegion
*mr
, target_phys_addr_t addr
, unsigned size
)
1503 return memory_region_dispatch_read(mr
, addr
, size
);
1506 void io_mem_write(MemoryRegion
*mr
, target_phys_addr_t addr
,
1507 uint64_t val
, unsigned size
)
1509 memory_region_dispatch_write(mr
, addr
, val
, size
);
1512 typedef struct MemoryRegionList MemoryRegionList
;
1514 struct MemoryRegionList
{
1515 const MemoryRegion
*mr
;
1517 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1520 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1522 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1523 const MemoryRegion
*mr
, unsigned int level
,
1524 target_phys_addr_t base
,
1525 MemoryRegionListHead
*alias_print_queue
)
1527 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1528 MemoryRegionListHead submr_print_queue
;
1529 const MemoryRegion
*submr
;
1536 for (i
= 0; i
< level
; i
++) {
1541 MemoryRegionList
*ml
;
1544 /* check if the alias is already in the queue */
1545 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1546 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1552 ml
= g_new(MemoryRegionList
, 1);
1554 ml
->printed
= false;
1555 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1557 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1558 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1559 "-" TARGET_FMT_plx
"\n",
1562 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1564 mr
->readable
? 'R' : '-',
1565 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1571 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1);
1574 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1577 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1579 mr
->readable
? 'R' : '-',
1580 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1585 QTAILQ_INIT(&submr_print_queue
);
1587 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1588 new_ml
= g_new(MemoryRegionList
, 1);
1590 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1591 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1592 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1593 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1594 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1600 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1604 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1605 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1609 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1614 void mtree_info(fprintf_function mon_printf
, void *f
)
1616 MemoryRegionListHead ml_head
;
1617 MemoryRegionList
*ml
, *ml2
;
1619 QTAILQ_INIT(&ml_head
);
1621 mon_printf(f
, "memory\n");
1622 mtree_print_mr(mon_printf
, f
, address_space_memory
.root
, 0, 0, &ml_head
);
1624 /* print aliased regions */
1625 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1627 mon_printf(f
, "%s\n", ml
->mr
->name
);
1628 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1632 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
1636 if (address_space_io
.root
&&
1637 !QTAILQ_EMPTY(&address_space_io
.root
->subregions
)) {
1638 QTAILQ_INIT(&ml_head
);
1639 mon_printf(f
, "I/O\n");
1640 mtree_print_mr(mon_printf
, f
, address_space_io
.root
, 0, 0, &ml_head
);