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 MemoryRegion
*mr
= container_of(iorange
, MemoryRegion
, iorange
);
387 if (mr
->ops
->old_portio
) {
388 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
, width
, false);
390 *data
= ((uint64_t)1 << (width
* 8)) - 1;
392 *data
= mrp
->read(mr
->opaque
, offset
);
393 } else if (width
== 2) {
394 mrp
= find_portio(mr
, offset
, 1, false);
396 *data
= mrp
->read(mr
->opaque
, offset
) |
397 (mrp
->read(mr
->opaque
, offset
+ 1) << 8);
402 access_with_adjusted_size(offset
, data
, width
,
403 mr
->ops
->impl
.min_access_size
,
404 mr
->ops
->impl
.max_access_size
,
405 memory_region_read_accessor
, mr
);
408 static void memory_region_iorange_write(IORange
*iorange
,
413 MemoryRegion
*mr
= container_of(iorange
, MemoryRegion
, iorange
);
415 if (mr
->ops
->old_portio
) {
416 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
, width
, true);
419 mrp
->write(mr
->opaque
, offset
, data
);
420 } else if (width
== 2) {
421 mrp
= find_portio(mr
, offset
, 1, false);
423 mrp
->write(mr
->opaque
, offset
, data
& 0xff);
424 mrp
->write(mr
->opaque
, offset
+ 1, data
>> 8);
428 access_with_adjusted_size(offset
, &data
, width
,
429 mr
->ops
->impl
.min_access_size
,
430 mr
->ops
->impl
.max_access_size
,
431 memory_region_write_accessor
, mr
);
434 const IORangeOps memory_region_iorange_ops
= {
435 .read
= memory_region_iorange_read
,
436 .write
= memory_region_iorange_write
,
439 static AddressSpace address_space_io
;
441 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
446 if (mr
== address_space_memory
.root
) {
447 return &address_space_memory
;
449 if (mr
== address_space_io
.root
) {
450 return &address_space_io
;
455 /* Render a memory region into the global view. Ranges in @view obscure
458 static void render_memory_region(FlatView
*view
,
464 MemoryRegion
*subregion
;
466 target_phys_addr_t offset_in_region
;
476 int128_addto(&base
, int128_make64(mr
->addr
));
477 readonly
|= mr
->readonly
;
479 tmp
= addrrange_make(base
, mr
->size
);
481 if (!addrrange_intersects(tmp
, clip
)) {
485 clip
= addrrange_intersection(tmp
, clip
);
488 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
489 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
490 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
494 /* Render subregions in priority order. */
495 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
496 render_memory_region(view
, subregion
, base
, clip
, readonly
);
499 if (!mr
->terminates
) {
503 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
507 /* Render the region itself into any gaps left by the current view. */
508 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
509 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
512 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
513 now
= int128_min(remain
,
514 int128_sub(view
->ranges
[i
].addr
.start
, base
));
516 fr
.offset_in_region
= offset_in_region
;
517 fr
.addr
= addrrange_make(base
, now
);
518 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
519 fr
.readable
= mr
->readable
;
520 fr
.readonly
= readonly
;
521 flatview_insert(view
, i
, &fr
);
523 int128_addto(&base
, now
);
524 offset_in_region
+= int128_get64(now
);
525 int128_subfrom(&remain
, now
);
527 if (int128_eq(base
, view
->ranges
[i
].addr
.start
)) {
528 now
= int128_min(remain
, view
->ranges
[i
].addr
.size
);
529 int128_addto(&base
, now
);
530 offset_in_region
+= int128_get64(now
);
531 int128_subfrom(&remain
, now
);
534 if (int128_nz(remain
)) {
536 fr
.offset_in_region
= offset_in_region
;
537 fr
.addr
= addrrange_make(base
, remain
);
538 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
539 fr
.readable
= mr
->readable
;
540 fr
.readonly
= readonly
;
541 flatview_insert(view
, i
, &fr
);
545 /* Render a memory topology into a list of disjoint absolute ranges. */
546 static FlatView
generate_memory_topology(MemoryRegion
*mr
)
550 flatview_init(&view
);
552 render_memory_region(&view
, mr
, int128_zero(),
553 addrrange_make(int128_zero(), int128_2_64()), false);
554 flatview_simplify(&view
);
559 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
560 MemoryRegionIoeventfd
*fds_new
,
562 MemoryRegionIoeventfd
*fds_old
,
566 MemoryRegionIoeventfd
*fd
;
567 MemoryRegionSection section
;
569 /* Generate a symmetric difference of the old and new fd sets, adding
570 * and deleting as necessary.
574 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
575 if (iold
< fds_old_nb
576 && (inew
== fds_new_nb
577 || memory_region_ioeventfd_before(fds_old
[iold
],
580 section
= (MemoryRegionSection
) {
581 .address_space
= as
->root
,
582 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
583 .size
= int128_get64(fd
->addr
.size
),
585 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
586 fd
->match_data
, fd
->data
, fd
->fd
);
588 } else if (inew
< fds_new_nb
589 && (iold
== fds_old_nb
590 || memory_region_ioeventfd_before(fds_new
[inew
],
593 section
= (MemoryRegionSection
) {
594 .address_space
= as
->root
,
595 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
596 .size
= int128_get64(fd
->addr
.size
),
598 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
599 fd
->match_data
, fd
->data
, fd
->fd
);
608 static void address_space_update_ioeventfds(AddressSpace
*as
)
611 unsigned ioeventfd_nb
= 0;
612 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
616 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
617 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
618 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
619 int128_sub(fr
->addr
.start
,
620 int128_make64(fr
->offset_in_region
)));
621 if (addrrange_intersects(fr
->addr
, tmp
)) {
623 ioeventfds
= g_realloc(ioeventfds
,
624 ioeventfd_nb
* sizeof(*ioeventfds
));
625 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
626 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
631 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
632 as
->ioeventfds
, as
->ioeventfd_nb
);
634 g_free(as
->ioeventfds
);
635 as
->ioeventfds
= ioeventfds
;
636 as
->ioeventfd_nb
= ioeventfd_nb
;
639 static void address_space_update_topology_pass(AddressSpace
*as
,
645 FlatRange
*frold
, *frnew
;
647 /* Generate a symmetric difference of the old and new memory maps.
648 * Kill ranges in the old map, and instantiate ranges in the new map.
651 while (iold
< old_view
.nr
|| inew
< new_view
.nr
) {
652 if (iold
< old_view
.nr
) {
653 frold
= &old_view
.ranges
[iold
];
657 if (inew
< new_view
.nr
) {
658 frnew
= &new_view
.ranges
[inew
];
665 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
666 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
667 && !flatrange_equal(frold
, frnew
)))) {
668 /* In old, but (not in new, or in new but attributes changed). */
671 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
675 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
676 /* In both (logging may have changed) */
679 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
680 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
681 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
682 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
683 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
693 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
702 static void address_space_update_topology(AddressSpace
*as
)
704 FlatView old_view
= as
->current_map
;
705 FlatView new_view
= generate_memory_topology(as
->root
);
707 address_space_update_topology_pass(as
, old_view
, new_view
, false);
708 address_space_update_topology_pass(as
, old_view
, new_view
, true);
710 as
->current_map
= new_view
;
711 flatview_destroy(&old_view
);
712 address_space_update_ioeventfds(as
);
715 static void memory_region_update_topology(MemoryRegion
*mr
)
717 if (memory_region_transaction_depth
) {
718 memory_region_update_pending
|= !mr
|| mr
->enabled
;
722 if (mr
&& !mr
->enabled
) {
726 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
728 if (address_space_memory
.root
) {
729 address_space_update_topology(&address_space_memory
);
731 if (address_space_io
.root
) {
732 address_space_update_topology(&address_space_io
);
735 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
737 memory_region_update_pending
= false;
740 void memory_region_transaction_begin(void)
742 ++memory_region_transaction_depth
;
745 void memory_region_transaction_commit(void)
747 assert(memory_region_transaction_depth
);
748 --memory_region_transaction_depth
;
749 if (!memory_region_transaction_depth
&& memory_region_update_pending
) {
750 memory_region_update_topology(NULL
);
754 static void memory_region_destructor_none(MemoryRegion
*mr
)
758 static void memory_region_destructor_ram(MemoryRegion
*mr
)
760 qemu_ram_free(mr
->ram_addr
);
763 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
765 qemu_ram_free_from_ptr(mr
->ram_addr
);
768 static void memory_region_destructor_iomem(MemoryRegion
*mr
)
770 cpu_unregister_io_memory(mr
->ram_addr
);
773 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
775 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
776 cpu_unregister_io_memory(mr
->ram_addr
& ~TARGET_PAGE_MASK
);
779 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
781 #ifdef TARGET_WORDS_BIGENDIAN
782 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
784 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
788 void memory_region_init(MemoryRegion
*mr
,
794 mr
->size
= int128_make64(size
);
795 if (size
== UINT64_MAX
) {
796 mr
->size
= int128_2_64();
801 mr
->terminates
= false;
804 mr
->readonly
= false;
805 mr
->rom_device
= false;
806 mr
->destructor
= memory_region_destructor_none
;
808 mr
->may_overlap
= false;
810 QTAILQ_INIT(&mr
->subregions
);
811 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
812 QTAILQ_INIT(&mr
->coalesced
);
813 mr
->name
= g_strdup(name
);
814 mr
->dirty_log_mask
= 0;
815 mr
->ioeventfd_nb
= 0;
816 mr
->ioeventfds
= NULL
;
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
= cpu_register_io_memory(mr
);
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
);
981 mr
->ram_addr
|= cpu_register_io_memory(mr
);
984 static uint64_t invalid_read(void *opaque
, target_phys_addr_t addr
,
987 MemoryRegion
*mr
= opaque
;
989 if (!mr
->warning_printed
) {
990 fprintf(stderr
, "Invalid read from memory region %s\n", mr
->name
);
991 mr
->warning_printed
= true;
996 static void invalid_write(void *opaque
, target_phys_addr_t addr
, uint64_t data
,
999 MemoryRegion
*mr
= opaque
;
1001 if (!mr
->warning_printed
) {
1002 fprintf(stderr
, "Invalid write to memory region %s\n", mr
->name
);
1003 mr
->warning_printed
= true;
1007 static const MemoryRegionOps reservation_ops
= {
1008 .read
= invalid_read
,
1009 .write
= invalid_write
,
1010 .endianness
= DEVICE_NATIVE_ENDIAN
,
1013 void memory_region_init_reservation(MemoryRegion
*mr
,
1017 memory_region_init_io(mr
, &reservation_ops
, mr
, name
, size
);
1020 void memory_region_destroy(MemoryRegion
*mr
)
1022 assert(QTAILQ_EMPTY(&mr
->subregions
));
1024 memory_region_clear_coalescing(mr
);
1025 g_free((char *)mr
->name
);
1026 g_free(mr
->ioeventfds
);
1029 uint64_t memory_region_size(MemoryRegion
*mr
)
1031 if (int128_eq(mr
->size
, int128_2_64())) {
1034 return int128_get64(mr
->size
);
1037 const char *memory_region_name(MemoryRegion
*mr
)
1042 bool memory_region_is_ram(MemoryRegion
*mr
)
1047 bool memory_region_is_logging(MemoryRegion
*mr
)
1049 return mr
->dirty_log_mask
;
1052 bool memory_region_is_rom(MemoryRegion
*mr
)
1054 return mr
->ram
&& mr
->readonly
;
1057 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1059 uint8_t mask
= 1 << client
;
1061 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1062 memory_region_update_topology(mr
);
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
)
1084 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1086 MEMORY_LISTENER_UPDATE_REGION(fr
, &address_space_memory
,
1092 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1094 if (mr
->readonly
!= readonly
) {
1095 mr
->readonly
= readonly
;
1096 memory_region_update_topology(mr
);
1100 void memory_region_rom_device_set_readable(MemoryRegion
*mr
, bool readable
)
1102 if (mr
->readable
!= readable
) {
1103 mr
->readable
= readable
;
1104 memory_region_update_topology(mr
);
1108 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1109 target_phys_addr_t size
, unsigned client
)
1111 assert(mr
->terminates
);
1112 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1113 mr
->ram_addr
+ addr
+ size
,
1117 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1120 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1123 assert(mr
->terminates
);
1125 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1128 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1131 CoalescedMemoryRange
*cmr
;
1134 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1136 qemu_unregister_coalesced_mmio(int128_get64(fr
->addr
.start
),
1137 int128_get64(fr
->addr
.size
));
1138 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1139 tmp
= addrrange_shift(cmr
->addr
,
1140 int128_sub(fr
->addr
.start
,
1141 int128_make64(fr
->offset_in_region
)));
1142 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1145 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1146 qemu_register_coalesced_mmio(int128_get64(tmp
.start
),
1147 int128_get64(tmp
.size
));
1153 void memory_region_set_coalescing(MemoryRegion
*mr
)
1155 memory_region_clear_coalescing(mr
);
1156 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1159 void memory_region_add_coalescing(MemoryRegion
*mr
,
1160 target_phys_addr_t offset
,
1163 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1165 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1166 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1167 memory_region_update_coalesced_range(mr
);
1170 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1172 CoalescedMemoryRange
*cmr
;
1174 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1175 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1176 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1179 memory_region_update_coalesced_range(mr
);
1182 void memory_region_add_eventfd(MemoryRegion
*mr
,
1183 target_phys_addr_t addr
,
1189 MemoryRegionIoeventfd mrfd
= {
1190 .addr
.start
= int128_make64(addr
),
1191 .addr
.size
= int128_make64(size
),
1192 .match_data
= match_data
,
1198 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1199 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1204 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1205 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1206 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1207 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1208 mr
->ioeventfds
[i
] = mrfd
;
1209 memory_region_update_topology(mr
);
1212 void memory_region_del_eventfd(MemoryRegion
*mr
,
1213 target_phys_addr_t addr
,
1219 MemoryRegionIoeventfd mrfd
= {
1220 .addr
.start
= int128_make64(addr
),
1221 .addr
.size
= int128_make64(size
),
1222 .match_data
= match_data
,
1228 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1229 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1233 assert(i
!= mr
->ioeventfd_nb
);
1234 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1235 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1237 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1238 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1239 memory_region_update_topology(mr
);
1242 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1243 target_phys_addr_t offset
,
1244 MemoryRegion
*subregion
)
1246 MemoryRegion
*other
;
1248 assert(!subregion
->parent
);
1249 subregion
->parent
= mr
;
1250 subregion
->addr
= offset
;
1251 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1252 if (subregion
->may_overlap
|| other
->may_overlap
) {
1255 if (int128_gt(int128_make64(offset
),
1256 int128_add(int128_make64(other
->addr
), other
->size
))
1257 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1258 int128_make64(other
->addr
))) {
1262 printf("warning: subregion collision %llx/%llx (%s) "
1263 "vs %llx/%llx (%s)\n",
1264 (unsigned long long)offset
,
1265 (unsigned long long)int128_get64(subregion
->size
),
1267 (unsigned long long)other
->addr
,
1268 (unsigned long long)int128_get64(other
->size
),
1272 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1273 if (subregion
->priority
>= other
->priority
) {
1274 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1278 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1280 memory_region_update_topology(mr
);
1284 void memory_region_add_subregion(MemoryRegion
*mr
,
1285 target_phys_addr_t offset
,
1286 MemoryRegion
*subregion
)
1288 subregion
->may_overlap
= false;
1289 subregion
->priority
= 0;
1290 memory_region_add_subregion_common(mr
, offset
, subregion
);
1293 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1294 target_phys_addr_t offset
,
1295 MemoryRegion
*subregion
,
1298 subregion
->may_overlap
= true;
1299 subregion
->priority
= priority
;
1300 memory_region_add_subregion_common(mr
, offset
, subregion
);
1303 void memory_region_del_subregion(MemoryRegion
*mr
,
1304 MemoryRegion
*subregion
)
1306 assert(subregion
->parent
== mr
);
1307 subregion
->parent
= NULL
;
1308 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1309 memory_region_update_topology(mr
);
1312 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1314 if (enabled
== mr
->enabled
) {
1317 mr
->enabled
= enabled
;
1318 memory_region_update_topology(NULL
);
1321 void memory_region_set_address(MemoryRegion
*mr
, target_phys_addr_t addr
)
1323 MemoryRegion
*parent
= mr
->parent
;
1324 unsigned priority
= mr
->priority
;
1325 bool may_overlap
= mr
->may_overlap
;
1327 if (addr
== mr
->addr
|| !parent
) {
1332 memory_region_transaction_begin();
1333 memory_region_del_subregion(parent
, mr
);
1335 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1337 memory_region_add_subregion(parent
, addr
, mr
);
1339 memory_region_transaction_commit();
1342 void memory_region_set_alias_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1344 target_phys_addr_t old_offset
= mr
->alias_offset
;
1347 mr
->alias_offset
= offset
;
1349 if (offset
== old_offset
|| !mr
->parent
) {
1353 memory_region_update_topology(mr
);
1356 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1358 return mr
->ram_addr
;
1361 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1363 const AddrRange
*addr
= addr_
;
1364 const FlatRange
*fr
= fr_
;
1366 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1368 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1374 static FlatRange
*address_space_lookup(AddressSpace
*as
, AddrRange addr
)
1376 return bsearch(&addr
, as
->current_map
.ranges
, as
->current_map
.nr
,
1377 sizeof(FlatRange
), cmp_flatrange_addr
);
1380 MemoryRegionSection
memory_region_find(MemoryRegion
*address_space
,
1381 target_phys_addr_t addr
, uint64_t size
)
1383 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1384 AddrRange range
= addrrange_make(int128_make64(addr
),
1385 int128_make64(size
));
1386 FlatRange
*fr
= address_space_lookup(as
, range
);
1387 MemoryRegionSection ret
= { .mr
= NULL
, .size
= 0 };
1393 while (fr
> as
->current_map
.ranges
1394 && addrrange_intersects(fr
[-1].addr
, range
)) {
1399 range
= addrrange_intersection(range
, fr
->addr
);
1400 ret
.offset_within_region
= fr
->offset_in_region
;
1401 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1403 ret
.size
= int128_get64(range
.size
);
1404 ret
.offset_within_address_space
= int128_get64(range
.start
);
1405 ret
.readonly
= fr
->readonly
;
1409 void memory_global_sync_dirty_bitmap(MemoryRegion
*address_space
)
1411 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1414 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1415 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1419 void memory_global_dirty_log_start(void)
1421 global_dirty_log
= true;
1422 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1425 void memory_global_dirty_log_stop(void)
1427 global_dirty_log
= false;
1428 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1431 static void listener_add_address_space(MemoryListener
*listener
,
1436 if (global_dirty_log
) {
1437 listener
->log_global_start(listener
);
1439 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1440 MemoryRegionSection section
= {
1442 .address_space
= as
->root
,
1443 .offset_within_region
= fr
->offset_in_region
,
1444 .size
= int128_get64(fr
->addr
.size
),
1445 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1446 .readonly
= fr
->readonly
,
1448 listener
->region_add(listener
, §ion
);
1452 void memory_listener_register(MemoryListener
*listener
, MemoryRegion
*filter
)
1454 MemoryListener
*other
= NULL
;
1456 listener
->address_space_filter
= filter
;
1457 if (QTAILQ_EMPTY(&memory_listeners
)
1458 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1459 memory_listeners
)->priority
) {
1460 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1462 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1463 if (listener
->priority
< other
->priority
) {
1467 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1469 listener_add_address_space(listener
, &address_space_memory
);
1470 listener_add_address_space(listener
, &address_space_io
);
1473 void memory_listener_unregister(MemoryListener
*listener
)
1475 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1478 void set_system_memory_map(MemoryRegion
*mr
)
1480 address_space_memory
.root
= mr
;
1481 memory_region_update_topology(NULL
);
1484 void set_system_io_map(MemoryRegion
*mr
)
1486 address_space_io
.root
= mr
;
1487 memory_region_update_topology(NULL
);
1490 uint64_t io_mem_read(int io_index
, target_phys_addr_t addr
, unsigned size
)
1492 return memory_region_dispatch_read(io_mem_region
[io_index
], addr
, size
);
1495 void io_mem_write(int io_index
, target_phys_addr_t addr
,
1496 uint64_t val
, unsigned size
)
1498 memory_region_dispatch_write(io_mem_region
[io_index
], addr
, val
, size
);
1501 typedef struct MemoryRegionList MemoryRegionList
;
1503 struct MemoryRegionList
{
1504 const MemoryRegion
*mr
;
1506 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1509 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1511 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1512 const MemoryRegion
*mr
, unsigned int level
,
1513 target_phys_addr_t base
,
1514 MemoryRegionListHead
*alias_print_queue
)
1516 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1517 MemoryRegionListHead submr_print_queue
;
1518 const MemoryRegion
*submr
;
1525 for (i
= 0; i
< level
; i
++) {
1530 MemoryRegionList
*ml
;
1533 /* check if the alias is already in the queue */
1534 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1535 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1541 ml
= g_new(MemoryRegionList
, 1);
1543 ml
->printed
= false;
1544 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1546 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1547 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1548 "-" TARGET_FMT_plx
"\n",
1551 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1553 mr
->readable
? 'R' : '-',
1554 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1560 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1);
1563 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1566 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1568 mr
->readable
? 'R' : '-',
1569 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1574 QTAILQ_INIT(&submr_print_queue
);
1576 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1577 new_ml
= g_new(MemoryRegionList
, 1);
1579 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1580 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1581 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1582 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1583 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1589 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1593 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1594 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1598 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1603 void mtree_info(fprintf_function mon_printf
, void *f
)
1605 MemoryRegionListHead ml_head
;
1606 MemoryRegionList
*ml
, *ml2
;
1608 QTAILQ_INIT(&ml_head
);
1610 mon_printf(f
, "memory\n");
1611 mtree_print_mr(mon_printf
, f
, address_space_memory
.root
, 0, 0, &ml_head
);
1613 /* print aliased regions */
1614 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1616 mon_printf(f
, "%s\n", ml
->mr
->name
);
1617 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1621 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
1625 if (address_space_io
.root
&&
1626 !QTAILQ_EMPTY(&address_space_io
.root
->subregions
)) {
1627 QTAILQ_INIT(&ml_head
);
1628 mon_printf(f
, "I/O\n");
1629 mtree_print_mr(mon_printf
, f
, address_space_io
.root
, 0, 0, &ml_head
);