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
.e
> b
.e
) {
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 if (mr
->flush_coalesced_mmio
) {
315 qemu_flush_coalesced_mmio_buffer();
317 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
318 *value
|= (tmp
& mask
) << shift
;
321 static void memory_region_write_accessor(void *opaque
,
322 target_phys_addr_t addr
,
328 MemoryRegion
*mr
= opaque
;
331 if (mr
->flush_coalesced_mmio
) {
332 qemu_flush_coalesced_mmio_buffer();
334 tmp
= (*value
>> shift
) & mask
;
335 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
338 static void access_with_adjusted_size(target_phys_addr_t addr
,
341 unsigned access_size_min
,
342 unsigned access_size_max
,
343 void (*access
)(void *opaque
,
344 target_phys_addr_t addr
,
351 uint64_t access_mask
;
352 unsigned access_size
;
355 if (!access_size_min
) {
358 if (!access_size_max
) {
361 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
362 access_mask
= -1ULL >> (64 - access_size
* 8);
363 for (i
= 0; i
< size
; i
+= access_size
) {
364 /* FIXME: big-endian support */
365 access(opaque
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
369 static AddressSpace address_space_memory
;
371 static const MemoryRegionPortio
*find_portio(MemoryRegion
*mr
, uint64_t offset
,
372 unsigned width
, bool write
)
374 const MemoryRegionPortio
*mrp
;
376 for (mrp
= mr
->ops
->old_portio
; mrp
->size
; ++mrp
) {
377 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
378 && width
== mrp
->size
379 && (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
386 static void memory_region_iorange_read(IORange
*iorange
,
391 MemoryRegionIORange
*mrio
392 = container_of(iorange
, MemoryRegionIORange
, iorange
);
393 MemoryRegion
*mr
= mrio
->mr
;
395 offset
+= mrio
->offset
;
396 if (mr
->ops
->old_portio
) {
397 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
- mrio
->offset
,
400 *data
= ((uint64_t)1 << (width
* 8)) - 1;
402 *data
= mrp
->read(mr
->opaque
, offset
);
403 } else if (width
== 2) {
404 mrp
= find_portio(mr
, offset
- mrio
->offset
, 1, false);
406 *data
= mrp
->read(mr
->opaque
, offset
) |
407 (mrp
->read(mr
->opaque
, offset
+ 1) << 8);
412 access_with_adjusted_size(offset
, data
, width
,
413 mr
->ops
->impl
.min_access_size
,
414 mr
->ops
->impl
.max_access_size
,
415 memory_region_read_accessor
, mr
);
418 static void memory_region_iorange_write(IORange
*iorange
,
423 MemoryRegionIORange
*mrio
424 = container_of(iorange
, MemoryRegionIORange
, iorange
);
425 MemoryRegion
*mr
= mrio
->mr
;
427 offset
+= mrio
->offset
;
428 if (mr
->ops
->old_portio
) {
429 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
- mrio
->offset
,
433 mrp
->write(mr
->opaque
, offset
, data
);
434 } else if (width
== 2) {
435 mrp
= find_portio(mr
, offset
- mrio
->offset
, 1, true);
437 mrp
->write(mr
->opaque
, offset
, data
& 0xff);
438 mrp
->write(mr
->opaque
, offset
+ 1, data
>> 8);
442 access_with_adjusted_size(offset
, &data
, width
,
443 mr
->ops
->impl
.min_access_size
,
444 mr
->ops
->impl
.max_access_size
,
445 memory_region_write_accessor
, mr
);
448 static void memory_region_iorange_destructor(IORange
*iorange
)
450 g_free(container_of(iorange
, MemoryRegionIORange
, iorange
));
453 const IORangeOps memory_region_iorange_ops
= {
454 .read
= memory_region_iorange_read
,
455 .write
= memory_region_iorange_write
,
456 .destructor
= memory_region_iorange_destructor
,
459 static AddressSpace address_space_io
;
461 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
466 if (mr
== address_space_memory
.root
) {
467 return &address_space_memory
;
469 if (mr
== address_space_io
.root
) {
470 return &address_space_io
;
475 /* Render a memory region into the global view. Ranges in @view obscure
478 static void render_memory_region(FlatView
*view
,
484 MemoryRegion
*subregion
;
486 target_phys_addr_t offset_in_region
;
496 int128_addto(&base
, int128_make64(mr
->addr
));
497 readonly
|= mr
->readonly
;
499 tmp
= addrrange_make(base
, mr
->size
);
501 if (!addrrange_intersects(tmp
, clip
)) {
505 clip
= addrrange_intersection(tmp
, clip
);
508 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
509 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
510 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
514 /* Render subregions in priority order. */
515 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
516 render_memory_region(view
, subregion
, base
, clip
, readonly
);
519 if (!mr
->terminates
) {
523 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
527 /* Render the region itself into any gaps left by the current view. */
528 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
529 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
532 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
533 now
= int128_min(remain
,
534 int128_sub(view
->ranges
[i
].addr
.start
, base
));
536 fr
.offset_in_region
= offset_in_region
;
537 fr
.addr
= addrrange_make(base
, now
);
538 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
539 fr
.readable
= mr
->readable
;
540 fr
.readonly
= readonly
;
541 flatview_insert(view
, i
, &fr
);
543 int128_addto(&base
, now
);
544 offset_in_region
+= int128_get64(now
);
545 int128_subfrom(&remain
, now
);
547 if (int128_eq(base
, view
->ranges
[i
].addr
.start
)) {
548 now
= int128_min(remain
, view
->ranges
[i
].addr
.size
);
549 int128_addto(&base
, now
);
550 offset_in_region
+= int128_get64(now
);
551 int128_subfrom(&remain
, now
);
554 if (int128_nz(remain
)) {
556 fr
.offset_in_region
= offset_in_region
;
557 fr
.addr
= addrrange_make(base
, remain
);
558 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
559 fr
.readable
= mr
->readable
;
560 fr
.readonly
= readonly
;
561 flatview_insert(view
, i
, &fr
);
565 /* Render a memory topology into a list of disjoint absolute ranges. */
566 static FlatView
generate_memory_topology(MemoryRegion
*mr
)
570 flatview_init(&view
);
572 render_memory_region(&view
, mr
, int128_zero(),
573 addrrange_make(int128_zero(), int128_2_64()), false);
574 flatview_simplify(&view
);
579 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
580 MemoryRegionIoeventfd
*fds_new
,
582 MemoryRegionIoeventfd
*fds_old
,
586 MemoryRegionIoeventfd
*fd
;
587 MemoryRegionSection section
;
589 /* Generate a symmetric difference of the old and new fd sets, adding
590 * and deleting as necessary.
594 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
595 if (iold
< fds_old_nb
596 && (inew
== fds_new_nb
597 || memory_region_ioeventfd_before(fds_old
[iold
],
600 section
= (MemoryRegionSection
) {
601 .address_space
= as
->root
,
602 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
603 .size
= int128_get64(fd
->addr
.size
),
605 MEMORY_LISTENER_CALL(eventfd_del
, Forward
, §ion
,
606 fd
->match_data
, fd
->data
, fd
->e
);
608 } else if (inew
< fds_new_nb
609 && (iold
== fds_old_nb
610 || memory_region_ioeventfd_before(fds_new
[inew
],
613 section
= (MemoryRegionSection
) {
614 .address_space
= as
->root
,
615 .offset_within_address_space
= int128_get64(fd
->addr
.start
),
616 .size
= int128_get64(fd
->addr
.size
),
618 MEMORY_LISTENER_CALL(eventfd_add
, Reverse
, §ion
,
619 fd
->match_data
, fd
->data
, fd
->e
);
628 static void address_space_update_ioeventfds(AddressSpace
*as
)
631 unsigned ioeventfd_nb
= 0;
632 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
636 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
637 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
638 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
639 int128_sub(fr
->addr
.start
,
640 int128_make64(fr
->offset_in_region
)));
641 if (addrrange_intersects(fr
->addr
, tmp
)) {
643 ioeventfds
= g_realloc(ioeventfds
,
644 ioeventfd_nb
* sizeof(*ioeventfds
));
645 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
646 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
651 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
652 as
->ioeventfds
, as
->ioeventfd_nb
);
654 g_free(as
->ioeventfds
);
655 as
->ioeventfds
= ioeventfds
;
656 as
->ioeventfd_nb
= ioeventfd_nb
;
659 static void address_space_update_topology_pass(AddressSpace
*as
,
665 FlatRange
*frold
, *frnew
;
667 /* Generate a symmetric difference of the old and new memory maps.
668 * Kill ranges in the old map, and instantiate ranges in the new map.
671 while (iold
< old_view
.nr
|| inew
< new_view
.nr
) {
672 if (iold
< old_view
.nr
) {
673 frold
= &old_view
.ranges
[iold
];
677 if (inew
< new_view
.nr
) {
678 frnew
= &new_view
.ranges
[inew
];
685 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
686 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
687 && !flatrange_equal(frold
, frnew
)))) {
688 /* In old, but (not in new, or in new but attributes changed). */
691 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, Reverse
, region_del
);
695 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
696 /* In both (logging may have changed) */
699 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_nop
);
700 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
701 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Reverse
, log_stop
);
702 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
703 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, log_start
);
713 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, Forward
, region_add
);
722 static void address_space_update_topology(AddressSpace
*as
)
724 FlatView old_view
= as
->current_map
;
725 FlatView new_view
= generate_memory_topology(as
->root
);
727 address_space_update_topology_pass(as
, old_view
, new_view
, false);
728 address_space_update_topology_pass(as
, old_view
, new_view
, true);
730 as
->current_map
= new_view
;
731 flatview_destroy(&old_view
);
732 address_space_update_ioeventfds(as
);
735 static void memory_region_update_topology(MemoryRegion
*mr
)
737 if (memory_region_transaction_depth
) {
738 memory_region_update_pending
|= !mr
|| mr
->enabled
;
742 if (mr
&& !mr
->enabled
) {
746 MEMORY_LISTENER_CALL_GLOBAL(begin
, Forward
);
748 if (address_space_memory
.root
) {
749 address_space_update_topology(&address_space_memory
);
751 if (address_space_io
.root
) {
752 address_space_update_topology(&address_space_io
);
755 MEMORY_LISTENER_CALL_GLOBAL(commit
, Forward
);
757 memory_region_update_pending
= false;
760 void memory_region_transaction_begin(void)
762 ++memory_region_transaction_depth
;
765 void memory_region_transaction_commit(void)
767 assert(memory_region_transaction_depth
);
768 --memory_region_transaction_depth
;
769 if (!memory_region_transaction_depth
&& memory_region_update_pending
) {
770 memory_region_update_topology(NULL
);
774 static void memory_region_destructor_none(MemoryRegion
*mr
)
778 static void memory_region_destructor_ram(MemoryRegion
*mr
)
780 qemu_ram_free(mr
->ram_addr
);
783 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
785 qemu_ram_free_from_ptr(mr
->ram_addr
);
788 static void memory_region_destructor_iomem(MemoryRegion
*mr
)
792 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
794 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
797 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
799 #ifdef TARGET_WORDS_BIGENDIAN
800 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
802 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
806 void memory_region_init(MemoryRegion
*mr
,
812 mr
->size
= int128_make64(size
);
813 if (size
== UINT64_MAX
) {
814 mr
->size
= int128_2_64();
819 mr
->terminates
= false;
822 mr
->readonly
= false;
823 mr
->rom_device
= false;
824 mr
->destructor
= memory_region_destructor_none
;
826 mr
->may_overlap
= false;
828 QTAILQ_INIT(&mr
->subregions
);
829 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
830 QTAILQ_INIT(&mr
->coalesced
);
831 mr
->name
= g_strdup(name
);
832 mr
->dirty_log_mask
= 0;
833 mr
->ioeventfd_nb
= 0;
834 mr
->ioeventfds
= NULL
;
835 mr
->flush_coalesced_mmio
= false;
838 static bool memory_region_access_valid(MemoryRegion
*mr
,
839 target_phys_addr_t addr
,
843 if (mr
->ops
->valid
.accepts
844 && !mr
->ops
->valid
.accepts(mr
->opaque
, addr
, size
, is_write
)) {
848 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
852 /* Treat zero as compatibility all valid */
853 if (!mr
->ops
->valid
.max_access_size
) {
857 if (size
> mr
->ops
->valid
.max_access_size
858 || size
< mr
->ops
->valid
.min_access_size
) {
864 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
865 target_phys_addr_t addr
,
870 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
871 return -1U; /* FIXME: better signalling */
874 if (!mr
->ops
->read
) {
875 return mr
->ops
->old_mmio
.read
[bitops_ffsl(size
)](mr
->opaque
, addr
);
878 /* FIXME: support unaligned access */
879 access_with_adjusted_size(addr
, &data
, size
,
880 mr
->ops
->impl
.min_access_size
,
881 mr
->ops
->impl
.max_access_size
,
882 memory_region_read_accessor
, mr
);
887 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
889 if (memory_region_wrong_endianness(mr
)) {
894 *data
= bswap16(*data
);
897 *data
= bswap32(*data
);
905 static uint64_t memory_region_dispatch_read(MemoryRegion
*mr
,
906 target_phys_addr_t addr
,
911 ret
= memory_region_dispatch_read1(mr
, addr
, size
);
912 adjust_endianness(mr
, &ret
, size
);
916 static void memory_region_dispatch_write(MemoryRegion
*mr
,
917 target_phys_addr_t addr
,
921 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
922 return; /* FIXME: better signalling */
925 adjust_endianness(mr
, &data
, size
);
927 if (!mr
->ops
->write
) {
928 mr
->ops
->old_mmio
.write
[bitops_ffsl(size
)](mr
->opaque
, addr
, data
);
932 /* FIXME: support unaligned access */
933 access_with_adjusted_size(addr
, &data
, size
,
934 mr
->ops
->impl
.min_access_size
,
935 mr
->ops
->impl
.max_access_size
,
936 memory_region_write_accessor
, mr
);
939 void memory_region_init_io(MemoryRegion
*mr
,
940 const MemoryRegionOps
*ops
,
945 memory_region_init(mr
, name
, size
);
948 mr
->terminates
= true;
949 mr
->destructor
= memory_region_destructor_iomem
;
950 mr
->ram_addr
= ~(ram_addr_t
)0;
953 void memory_region_init_ram(MemoryRegion
*mr
,
957 memory_region_init(mr
, name
, size
);
959 mr
->terminates
= true;
960 mr
->destructor
= memory_region_destructor_ram
;
961 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
964 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
969 memory_region_init(mr
, name
, size
);
971 mr
->terminates
= true;
972 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
973 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
976 void memory_region_init_alias(MemoryRegion
*mr
,
979 target_phys_addr_t offset
,
982 memory_region_init(mr
, name
, size
);
984 mr
->alias_offset
= offset
;
987 void memory_region_init_rom_device(MemoryRegion
*mr
,
988 const MemoryRegionOps
*ops
,
993 memory_region_init(mr
, name
, size
);
996 mr
->terminates
= true;
997 mr
->rom_device
= true;
998 mr
->destructor
= memory_region_destructor_rom_device
;
999 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1002 static uint64_t invalid_read(void *opaque
, target_phys_addr_t addr
,
1005 MemoryRegion
*mr
= opaque
;
1007 if (!mr
->warning_printed
) {
1008 fprintf(stderr
, "Invalid read from memory region %s\n", mr
->name
);
1009 mr
->warning_printed
= true;
1014 static void invalid_write(void *opaque
, target_phys_addr_t addr
, uint64_t data
,
1017 MemoryRegion
*mr
= opaque
;
1019 if (!mr
->warning_printed
) {
1020 fprintf(stderr
, "Invalid write to memory region %s\n", mr
->name
);
1021 mr
->warning_printed
= true;
1025 static const MemoryRegionOps reservation_ops
= {
1026 .read
= invalid_read
,
1027 .write
= invalid_write
,
1028 .endianness
= DEVICE_NATIVE_ENDIAN
,
1031 void memory_region_init_reservation(MemoryRegion
*mr
,
1035 memory_region_init_io(mr
, &reservation_ops
, mr
, name
, size
);
1038 void memory_region_destroy(MemoryRegion
*mr
)
1040 assert(QTAILQ_EMPTY(&mr
->subregions
));
1042 memory_region_clear_coalescing(mr
);
1043 g_free((char *)mr
->name
);
1044 g_free(mr
->ioeventfds
);
1047 uint64_t memory_region_size(MemoryRegion
*mr
)
1049 if (int128_eq(mr
->size
, int128_2_64())) {
1052 return int128_get64(mr
->size
);
1055 const char *memory_region_name(MemoryRegion
*mr
)
1060 bool memory_region_is_ram(MemoryRegion
*mr
)
1065 bool memory_region_is_logging(MemoryRegion
*mr
)
1067 return mr
->dirty_log_mask
;
1070 bool memory_region_is_rom(MemoryRegion
*mr
)
1072 return mr
->ram
&& mr
->readonly
;
1075 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1077 uint8_t mask
= 1 << client
;
1079 memory_region_transaction_begin();
1080 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1081 memory_region_transaction_commit();
1084 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1085 target_phys_addr_t size
, unsigned client
)
1087 assert(mr
->terminates
);
1088 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, size
,
1092 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1093 target_phys_addr_t size
)
1095 assert(mr
->terminates
);
1096 return cpu_physical_memory_set_dirty_range(mr
->ram_addr
+ addr
, size
, -1);
1099 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1103 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1105 MEMORY_LISTENER_UPDATE_REGION(fr
, &address_space_memory
,
1111 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1113 if (mr
->readonly
!= readonly
) {
1114 memory_region_transaction_begin();
1115 mr
->readonly
= readonly
;
1116 memory_region_transaction_commit();
1120 void memory_region_rom_device_set_readable(MemoryRegion
*mr
, bool readable
)
1122 if (mr
->readable
!= readable
) {
1123 memory_region_transaction_begin();
1124 mr
->readable
= readable
;
1125 memory_region_transaction_commit();
1129 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1130 target_phys_addr_t size
, unsigned client
)
1132 assert(mr
->terminates
);
1133 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1134 mr
->ram_addr
+ addr
+ size
,
1138 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1141 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1144 assert(mr
->terminates
);
1146 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1149 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1152 CoalescedMemoryRange
*cmr
;
1155 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1157 qemu_unregister_coalesced_mmio(int128_get64(fr
->addr
.start
),
1158 int128_get64(fr
->addr
.size
));
1159 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1160 tmp
= addrrange_shift(cmr
->addr
,
1161 int128_sub(fr
->addr
.start
,
1162 int128_make64(fr
->offset_in_region
)));
1163 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1166 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1167 qemu_register_coalesced_mmio(int128_get64(tmp
.start
),
1168 int128_get64(tmp
.size
));
1174 void memory_region_set_coalescing(MemoryRegion
*mr
)
1176 memory_region_clear_coalescing(mr
);
1177 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1180 void memory_region_add_coalescing(MemoryRegion
*mr
,
1181 target_phys_addr_t offset
,
1184 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1186 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1187 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1188 memory_region_update_coalesced_range(mr
);
1189 memory_region_set_flush_coalesced(mr
);
1192 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1194 CoalescedMemoryRange
*cmr
;
1196 qemu_flush_coalesced_mmio_buffer();
1197 mr
->flush_coalesced_mmio
= false;
1199 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1200 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1201 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1204 memory_region_update_coalesced_range(mr
);
1207 void memory_region_set_flush_coalesced(MemoryRegion
*mr
)
1209 mr
->flush_coalesced_mmio
= true;
1212 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
)
1214 qemu_flush_coalesced_mmio_buffer();
1215 if (QTAILQ_EMPTY(&mr
->coalesced
)) {
1216 mr
->flush_coalesced_mmio
= false;
1220 void memory_region_add_eventfd(MemoryRegion
*mr
,
1221 target_phys_addr_t addr
,
1227 MemoryRegionIoeventfd mrfd
= {
1228 .addr
.start
= int128_make64(addr
),
1229 .addr
.size
= int128_make64(size
),
1230 .match_data
= match_data
,
1236 memory_region_transaction_begin();
1237 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1238 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1243 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1244 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1245 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1246 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1247 mr
->ioeventfds
[i
] = mrfd
;
1248 memory_region_transaction_commit();
1251 void memory_region_del_eventfd(MemoryRegion
*mr
,
1252 target_phys_addr_t addr
,
1258 MemoryRegionIoeventfd mrfd
= {
1259 .addr
.start
= int128_make64(addr
),
1260 .addr
.size
= int128_make64(size
),
1261 .match_data
= match_data
,
1267 memory_region_transaction_begin();
1268 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1269 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1273 assert(i
!= mr
->ioeventfd_nb
);
1274 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1275 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1277 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1278 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1279 memory_region_transaction_commit();
1282 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1283 target_phys_addr_t offset
,
1284 MemoryRegion
*subregion
)
1286 MemoryRegion
*other
;
1288 memory_region_transaction_begin();
1290 assert(!subregion
->parent
);
1291 subregion
->parent
= mr
;
1292 subregion
->addr
= offset
;
1293 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1294 if (subregion
->may_overlap
|| other
->may_overlap
) {
1297 if (int128_gt(int128_make64(offset
),
1298 int128_add(int128_make64(other
->addr
), other
->size
))
1299 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1300 int128_make64(other
->addr
))) {
1304 printf("warning: subregion collision %llx/%llx (%s) "
1305 "vs %llx/%llx (%s)\n",
1306 (unsigned long long)offset
,
1307 (unsigned long long)int128_get64(subregion
->size
),
1309 (unsigned long long)other
->addr
,
1310 (unsigned long long)int128_get64(other
->size
),
1314 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1315 if (subregion
->priority
>= other
->priority
) {
1316 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1320 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1322 memory_region_transaction_commit();
1326 void memory_region_add_subregion(MemoryRegion
*mr
,
1327 target_phys_addr_t offset
,
1328 MemoryRegion
*subregion
)
1330 subregion
->may_overlap
= false;
1331 subregion
->priority
= 0;
1332 memory_region_add_subregion_common(mr
, offset
, subregion
);
1335 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1336 target_phys_addr_t offset
,
1337 MemoryRegion
*subregion
,
1340 subregion
->may_overlap
= true;
1341 subregion
->priority
= priority
;
1342 memory_region_add_subregion_common(mr
, offset
, subregion
);
1345 void memory_region_del_subregion(MemoryRegion
*mr
,
1346 MemoryRegion
*subregion
)
1348 memory_region_transaction_begin();
1349 assert(subregion
->parent
== mr
);
1350 subregion
->parent
= NULL
;
1351 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1352 memory_region_transaction_commit();
1355 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1357 if (enabled
== mr
->enabled
) {
1360 memory_region_transaction_begin();
1361 mr
->enabled
= enabled
;
1362 memory_region_transaction_commit();
1365 void memory_region_set_address(MemoryRegion
*mr
, target_phys_addr_t addr
)
1367 MemoryRegion
*parent
= mr
->parent
;
1368 unsigned priority
= mr
->priority
;
1369 bool may_overlap
= mr
->may_overlap
;
1371 if (addr
== mr
->addr
|| !parent
) {
1376 memory_region_transaction_begin();
1377 memory_region_del_subregion(parent
, mr
);
1379 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1381 memory_region_add_subregion(parent
, addr
, mr
);
1383 memory_region_transaction_commit();
1386 void memory_region_set_alias_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1390 if (offset
== mr
->alias_offset
) {
1394 memory_region_transaction_begin();
1395 mr
->alias_offset
= offset
;
1396 memory_region_transaction_commit();
1399 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1401 return mr
->ram_addr
;
1404 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1406 const AddrRange
*addr
= addr_
;
1407 const FlatRange
*fr
= fr_
;
1409 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1411 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1417 static FlatRange
*address_space_lookup(AddressSpace
*as
, AddrRange addr
)
1419 return bsearch(&addr
, as
->current_map
.ranges
, as
->current_map
.nr
,
1420 sizeof(FlatRange
), cmp_flatrange_addr
);
1423 MemoryRegionSection
memory_region_find(MemoryRegion
*address_space
,
1424 target_phys_addr_t addr
, uint64_t size
)
1426 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1427 AddrRange range
= addrrange_make(int128_make64(addr
),
1428 int128_make64(size
));
1429 FlatRange
*fr
= address_space_lookup(as
, range
);
1430 MemoryRegionSection ret
= { .mr
= NULL
, .size
= 0 };
1436 while (fr
> as
->current_map
.ranges
1437 && addrrange_intersects(fr
[-1].addr
, range
)) {
1442 range
= addrrange_intersection(range
, fr
->addr
);
1443 ret
.offset_within_region
= fr
->offset_in_region
;
1444 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1446 ret
.size
= int128_get64(range
.size
);
1447 ret
.offset_within_address_space
= int128_get64(range
.start
);
1448 ret
.readonly
= fr
->readonly
;
1452 void memory_global_sync_dirty_bitmap(MemoryRegion
*address_space
)
1454 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1457 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1458 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, Forward
, log_sync
);
1462 void memory_global_dirty_log_start(void)
1464 global_dirty_log
= true;
1465 MEMORY_LISTENER_CALL_GLOBAL(log_global_start
, Forward
);
1468 void memory_global_dirty_log_stop(void)
1470 global_dirty_log
= false;
1471 MEMORY_LISTENER_CALL_GLOBAL(log_global_stop
, Reverse
);
1474 static void listener_add_address_space(MemoryListener
*listener
,
1479 if (listener
->address_space_filter
1480 && listener
->address_space_filter
!= as
->root
) {
1484 if (global_dirty_log
) {
1485 listener
->log_global_start(listener
);
1487 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1488 MemoryRegionSection section
= {
1490 .address_space
= as
->root
,
1491 .offset_within_region
= fr
->offset_in_region
,
1492 .size
= int128_get64(fr
->addr
.size
),
1493 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1494 .readonly
= fr
->readonly
,
1496 listener
->region_add(listener
, §ion
);
1500 void memory_listener_register(MemoryListener
*listener
, MemoryRegion
*filter
)
1502 MemoryListener
*other
= NULL
;
1504 listener
->address_space_filter
= filter
;
1505 if (QTAILQ_EMPTY(&memory_listeners
)
1506 || listener
->priority
>= QTAILQ_LAST(&memory_listeners
,
1507 memory_listeners
)->priority
) {
1508 QTAILQ_INSERT_TAIL(&memory_listeners
, listener
, link
);
1510 QTAILQ_FOREACH(other
, &memory_listeners
, link
) {
1511 if (listener
->priority
< other
->priority
) {
1515 QTAILQ_INSERT_BEFORE(other
, listener
, link
);
1517 listener_add_address_space(listener
, &address_space_memory
);
1518 listener_add_address_space(listener
, &address_space_io
);
1521 void memory_listener_unregister(MemoryListener
*listener
)
1523 QTAILQ_REMOVE(&memory_listeners
, listener
, link
);
1526 void set_system_memory_map(MemoryRegion
*mr
)
1528 memory_region_transaction_begin();
1529 address_space_memory
.root
= mr
;
1530 memory_region_transaction_commit();
1533 void set_system_io_map(MemoryRegion
*mr
)
1535 memory_region_transaction_begin();
1536 address_space_io
.root
= mr
;
1537 memory_region_transaction_commit();
1540 uint64_t io_mem_read(MemoryRegion
*mr
, target_phys_addr_t addr
, unsigned size
)
1542 return memory_region_dispatch_read(mr
, addr
, size
);
1545 void io_mem_write(MemoryRegion
*mr
, target_phys_addr_t addr
,
1546 uint64_t val
, unsigned size
)
1548 memory_region_dispatch_write(mr
, addr
, val
, size
);
1551 typedef struct MemoryRegionList MemoryRegionList
;
1553 struct MemoryRegionList
{
1554 const MemoryRegion
*mr
;
1556 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1559 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1561 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1562 const MemoryRegion
*mr
, unsigned int level
,
1563 target_phys_addr_t base
,
1564 MemoryRegionListHead
*alias_print_queue
)
1566 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1567 MemoryRegionListHead submr_print_queue
;
1568 const MemoryRegion
*submr
;
1575 for (i
= 0; i
< level
; i
++) {
1580 MemoryRegionList
*ml
;
1583 /* check if the alias is already in the queue */
1584 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1585 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1591 ml
= g_new(MemoryRegionList
, 1);
1593 ml
->printed
= false;
1594 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1596 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
1597 " (prio %d, %c%c): alias %s @%s " TARGET_FMT_plx
1598 "-" TARGET_FMT_plx
"\n",
1601 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1603 mr
->readable
? 'R' : '-',
1604 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1610 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1);
1613 TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d, %c%c): %s\n",
1616 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1618 mr
->readable
? 'R' : '-',
1619 !mr
->readonly
&& !(mr
->rom_device
&& mr
->readable
) ? 'W'
1624 QTAILQ_INIT(&submr_print_queue
);
1626 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1627 new_ml
= g_new(MemoryRegionList
, 1);
1629 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1630 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1631 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1632 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1633 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1639 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1643 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1644 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1648 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1653 void mtree_info(fprintf_function mon_printf
, void *f
)
1655 MemoryRegionListHead ml_head
;
1656 MemoryRegionList
*ml
, *ml2
;
1658 QTAILQ_INIT(&ml_head
);
1660 mon_printf(f
, "memory\n");
1661 mtree_print_mr(mon_printf
, f
, address_space_memory
.root
, 0, 0, &ml_head
);
1663 if (address_space_io
.root
&&
1664 !QTAILQ_EMPTY(&address_space_io
.root
->subregions
)) {
1665 mon_printf(f
, "I/O\n");
1666 mtree_print_mr(mon_printf
, f
, address_space_io
.root
, 0, 0, &ml_head
);
1669 mon_printf(f
, "aliases\n");
1670 /* print aliased regions */
1671 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1673 mon_printf(f
, "%s\n", ml
->mr
->name
);
1674 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1678 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {