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.
15 #include "exec-memory.h"
21 #define WANT_EXEC_OBSOLETE
22 #include "exec-obsolete.h"
24 unsigned memory_region_transaction_depth
= 0;
25 static bool memory_region_update_pending
= false;
26 static bool global_dirty_log
= false;
28 static QLIST_HEAD(, MemoryListener
) memory_listeners
29 = QLIST_HEAD_INITIALIZER(memory_listeners
);
31 typedef struct AddrRange AddrRange
;
34 * Note using signed integers limits us to physical addresses at most
35 * 63 bits wide. They are needed for negative offsetting in aliases
36 * (large MemoryRegion::alias_offset).
43 static AddrRange
addrrange_make(Int128 start
, Int128 size
)
45 return (AddrRange
) { start
, size
};
48 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
50 return int128_eq(r1
.start
, r2
.start
) && int128_eq(r1
.size
, r2
.size
);
53 static Int128
addrrange_end(AddrRange r
)
55 return int128_add(r
.start
, r
.size
);
58 static AddrRange
addrrange_shift(AddrRange range
, Int128 delta
)
60 int128_addto(&range
.start
, delta
);
64 static bool addrrange_contains(AddrRange range
, Int128 addr
)
66 return int128_ge(addr
, range
.start
)
67 && int128_lt(addr
, addrrange_end(range
));
70 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
72 return addrrange_contains(r1
, r2
.start
)
73 || addrrange_contains(r2
, r1
.start
);
76 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
78 Int128 start
= int128_max(r1
.start
, r2
.start
);
79 Int128 end
= int128_min(addrrange_end(r1
), addrrange_end(r2
));
80 return addrrange_make(start
, int128_sub(end
, start
));
83 struct CoalescedMemoryRange
{
85 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
88 struct MemoryRegionIoeventfd
{
95 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
96 MemoryRegionIoeventfd b
)
98 if (int128_lt(a
.addr
.start
, b
.addr
.start
)) {
100 } else if (int128_gt(a
.addr
.start
, b
.addr
.start
)) {
102 } else if (int128_lt(a
.addr
.size
, b
.addr
.size
)) {
104 } else if (int128_gt(a
.addr
.size
, b
.addr
.size
)) {
106 } else if (a
.match_data
< b
.match_data
) {
108 } else if (a
.match_data
> b
.match_data
) {
110 } else if (a
.match_data
) {
111 if (a
.data
< b
.data
) {
113 } else if (a
.data
> b
.data
) {
119 } else if (a
.fd
> b
.fd
) {
125 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
126 MemoryRegionIoeventfd b
)
128 return !memory_region_ioeventfd_before(a
, b
)
129 && !memory_region_ioeventfd_before(b
, a
);
132 typedef struct FlatRange FlatRange
;
133 typedef struct FlatView FlatView
;
135 /* Range of memory in the global map. Addresses are absolute. */
138 target_phys_addr_t offset_in_region
;
140 uint8_t dirty_log_mask
;
145 /* Flattened global view of current active memory hierarchy. Kept in sorted
151 unsigned nr_allocated
;
154 typedef struct AddressSpace AddressSpace
;
155 typedef struct AddressSpaceOps AddressSpaceOps
;
157 /* A system address space - I/O, memory, etc. */
158 struct AddressSpace
{
159 const AddressSpaceOps
*ops
;
161 FlatView current_map
;
163 MemoryRegionIoeventfd
*ioeventfds
;
166 struct AddressSpaceOps
{
167 void (*range_add
)(AddressSpace
*as
, FlatRange
*fr
);
168 void (*range_del
)(AddressSpace
*as
, FlatRange
*fr
);
169 void (*log_start
)(AddressSpace
*as
, FlatRange
*fr
);
170 void (*log_stop
)(AddressSpace
*as
, FlatRange
*fr
);
171 void (*ioeventfd_add
)(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
);
172 void (*ioeventfd_del
)(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
);
175 #define FOR_EACH_FLAT_RANGE(var, view) \
176 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
178 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
180 return a
->mr
== b
->mr
181 && addrrange_equal(a
->addr
, b
->addr
)
182 && a
->offset_in_region
== b
->offset_in_region
183 && a
->readable
== b
->readable
184 && a
->readonly
== b
->readonly
;
187 static void flatview_init(FlatView
*view
)
191 view
->nr_allocated
= 0;
194 /* Insert a range into a given position. Caller is responsible for maintaining
197 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
199 if (view
->nr
== view
->nr_allocated
) {
200 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
201 view
->ranges
= g_realloc(view
->ranges
,
202 view
->nr_allocated
* sizeof(*view
->ranges
));
204 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
205 (view
->nr
- pos
) * sizeof(FlatRange
));
206 view
->ranges
[pos
] = *range
;
210 static void flatview_destroy(FlatView
*view
)
212 g_free(view
->ranges
);
215 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
217 return int128_eq(addrrange_end(r1
->addr
), r2
->addr
.start
)
219 && int128_eq(int128_add(int128_make64(r1
->offset_in_region
),
221 int128_make64(r2
->offset_in_region
))
222 && r1
->dirty_log_mask
== r2
->dirty_log_mask
223 && r1
->readable
== r2
->readable
224 && r1
->readonly
== r2
->readonly
;
227 /* Attempt to simplify a view by merging ajacent ranges */
228 static void flatview_simplify(FlatView
*view
)
233 while (i
< view
->nr
) {
236 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
237 int128_addto(&view
->ranges
[i
].addr
.size
, view
->ranges
[j
].addr
.size
);
241 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
242 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
247 static void memory_region_read_accessor(void *opaque
,
248 target_phys_addr_t addr
,
254 MemoryRegion
*mr
= opaque
;
257 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
258 *value
|= (tmp
& mask
) << shift
;
261 static void memory_region_write_accessor(void *opaque
,
262 target_phys_addr_t addr
,
268 MemoryRegion
*mr
= opaque
;
271 tmp
= (*value
>> shift
) & mask
;
272 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
275 static void access_with_adjusted_size(target_phys_addr_t addr
,
278 unsigned access_size_min
,
279 unsigned access_size_max
,
280 void (*access
)(void *opaque
,
281 target_phys_addr_t addr
,
288 uint64_t access_mask
;
289 unsigned access_size
;
292 if (!access_size_min
) {
295 if (!access_size_max
) {
298 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
299 access_mask
= -1ULL >> (64 - access_size
* 8);
300 for (i
= 0; i
< size
; i
+= access_size
) {
301 /* FIXME: big-endian support */
302 access(opaque
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
306 static void as_memory_range_add(AddressSpace
*as
, FlatRange
*fr
)
308 MemoryRegionSection section
= {
310 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
311 .offset_within_region
= fr
->offset_in_region
,
312 .size
= int128_get64(fr
->addr
.size
),
315 cpu_register_physical_memory_log(§ion
, fr
->readable
, fr
->readonly
);
318 static void as_memory_range_del(AddressSpace
*as
, FlatRange
*fr
)
320 MemoryRegionSection section
= {
321 .mr
= &io_mem_unassigned
,
322 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
323 .offset_within_region
= int128_get64(fr
->addr
.start
),
324 .size
= int128_get64(fr
->addr
.size
),
327 cpu_register_physical_memory_log(§ion
, true, false);
330 static void as_memory_log_start(AddressSpace
*as
, FlatRange
*fr
)
334 static void as_memory_log_stop(AddressSpace
*as
, FlatRange
*fr
)
338 static void as_memory_ioeventfd_add(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
342 assert(fd
->match_data
&& int128_get64(fd
->addr
.size
) == 4);
344 r
= kvm_set_ioeventfd_mmio_long(fd
->fd
, int128_get64(fd
->addr
.start
),
351 static void as_memory_ioeventfd_del(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
355 r
= kvm_set_ioeventfd_mmio_long(fd
->fd
, int128_get64(fd
->addr
.start
),
362 static const AddressSpaceOps address_space_ops_memory
= {
363 .range_add
= as_memory_range_add
,
364 .range_del
= as_memory_range_del
,
365 .log_start
= as_memory_log_start
,
366 .log_stop
= as_memory_log_stop
,
367 .ioeventfd_add
= as_memory_ioeventfd_add
,
368 .ioeventfd_del
= as_memory_ioeventfd_del
,
371 static AddressSpace address_space_memory
= {
372 .ops
= &address_space_ops_memory
,
375 static const MemoryRegionPortio
*find_portio(MemoryRegion
*mr
, uint64_t offset
,
376 unsigned width
, bool write
)
378 const MemoryRegionPortio
*mrp
;
380 for (mrp
= mr
->ops
->old_portio
; mrp
->size
; ++mrp
) {
381 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
382 && width
== mrp
->size
383 && (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
390 static void memory_region_iorange_read(IORange
*iorange
,
395 MemoryRegion
*mr
= container_of(iorange
, MemoryRegion
, iorange
);
397 if (mr
->ops
->old_portio
) {
398 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
, width
, false);
400 *data
= ((uint64_t)1 << (width
* 8)) - 1;
402 *data
= mrp
->read(mr
->opaque
, offset
+ mr
->offset
);
403 } else if (width
== 2) {
404 mrp
= find_portio(mr
, offset
, 1, false);
406 *data
= mrp
->read(mr
->opaque
, offset
+ mr
->offset
) |
407 (mrp
->read(mr
->opaque
, offset
+ mr
->offset
+ 1) << 8);
412 access_with_adjusted_size(offset
+ mr
->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 MemoryRegion
*mr
= container_of(iorange
, MemoryRegion
, iorange
);
425 if (mr
->ops
->old_portio
) {
426 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
, width
, true);
429 mrp
->write(mr
->opaque
, offset
+ mr
->offset
, data
);
430 } else if (width
== 2) {
431 mrp
= find_portio(mr
, offset
, 1, false);
433 mrp
->write(mr
->opaque
, offset
+ mr
->offset
, data
& 0xff);
434 mrp
->write(mr
->opaque
, offset
+ mr
->offset
+ 1, data
>> 8);
438 access_with_adjusted_size(offset
+ mr
->offset
, &data
, width
,
439 mr
->ops
->impl
.min_access_size
,
440 mr
->ops
->impl
.max_access_size
,
441 memory_region_write_accessor
, mr
);
444 static const IORangeOps memory_region_iorange_ops
= {
445 .read
= memory_region_iorange_read
,
446 .write
= memory_region_iorange_write
,
449 static void as_io_range_add(AddressSpace
*as
, FlatRange
*fr
)
451 iorange_init(&fr
->mr
->iorange
, &memory_region_iorange_ops
,
452 int128_get64(fr
->addr
.start
), int128_get64(fr
->addr
.size
));
453 ioport_register(&fr
->mr
->iorange
);
456 static void as_io_range_del(AddressSpace
*as
, FlatRange
*fr
)
458 isa_unassign_ioport(int128_get64(fr
->addr
.start
),
459 int128_get64(fr
->addr
.size
));
462 static void as_io_ioeventfd_add(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
466 assert(fd
->match_data
&& int128_get64(fd
->addr
.size
) == 2);
468 r
= kvm_set_ioeventfd_pio_word(fd
->fd
, int128_get64(fd
->addr
.start
),
475 static void as_io_ioeventfd_del(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
479 r
= kvm_set_ioeventfd_pio_word(fd
->fd
, int128_get64(fd
->addr
.start
),
486 static const AddressSpaceOps address_space_ops_io
= {
487 .range_add
= as_io_range_add
,
488 .range_del
= as_io_range_del
,
489 .ioeventfd_add
= as_io_ioeventfd_add
,
490 .ioeventfd_del
= as_io_ioeventfd_del
,
493 static AddressSpace address_space_io
= {
494 .ops
= &address_space_ops_io
,
497 static AddressSpace
*memory_region_to_address_space(MemoryRegion
*mr
)
502 if (mr
== address_space_memory
.root
) {
503 return &address_space_memory
;
505 if (mr
== address_space_io
.root
) {
506 return &address_space_io
;
511 /* Render a memory region into the global view. Ranges in @view obscure
514 static void render_memory_region(FlatView
*view
,
520 MemoryRegion
*subregion
;
522 target_phys_addr_t offset_in_region
;
532 int128_addto(&base
, int128_make64(mr
->addr
));
533 readonly
|= mr
->readonly
;
535 tmp
= addrrange_make(base
, mr
->size
);
537 if (!addrrange_intersects(tmp
, clip
)) {
541 clip
= addrrange_intersection(tmp
, clip
);
544 int128_subfrom(&base
, int128_make64(mr
->alias
->addr
));
545 int128_subfrom(&base
, int128_make64(mr
->alias_offset
));
546 render_memory_region(view
, mr
->alias
, base
, clip
, readonly
);
550 /* Render subregions in priority order. */
551 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
552 render_memory_region(view
, subregion
, base
, clip
, readonly
);
555 if (!mr
->terminates
) {
559 offset_in_region
= int128_get64(int128_sub(clip
.start
, base
));
563 /* Render the region itself into any gaps left by the current view. */
564 for (i
= 0; i
< view
->nr
&& int128_nz(remain
); ++i
) {
565 if (int128_ge(base
, addrrange_end(view
->ranges
[i
].addr
))) {
568 if (int128_lt(base
, view
->ranges
[i
].addr
.start
)) {
569 now
= int128_min(remain
,
570 int128_sub(view
->ranges
[i
].addr
.start
, base
));
572 fr
.offset_in_region
= offset_in_region
;
573 fr
.addr
= addrrange_make(base
, now
);
574 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
575 fr
.readable
= mr
->readable
;
576 fr
.readonly
= readonly
;
577 flatview_insert(view
, i
, &fr
);
579 int128_addto(&base
, now
);
580 offset_in_region
+= int128_get64(now
);
581 int128_subfrom(&remain
, now
);
583 if (int128_eq(base
, view
->ranges
[i
].addr
.start
)) {
584 now
= int128_min(remain
, view
->ranges
[i
].addr
.size
);
585 int128_addto(&base
, now
);
586 offset_in_region
+= int128_get64(now
);
587 int128_subfrom(&remain
, now
);
590 if (int128_nz(remain
)) {
592 fr
.offset_in_region
= offset_in_region
;
593 fr
.addr
= addrrange_make(base
, remain
);
594 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
595 fr
.readable
= mr
->readable
;
596 fr
.readonly
= readonly
;
597 flatview_insert(view
, i
, &fr
);
601 /* Render a memory topology into a list of disjoint absolute ranges. */
602 static FlatView
generate_memory_topology(MemoryRegion
*mr
)
606 flatview_init(&view
);
608 render_memory_region(&view
, mr
, int128_zero(),
609 addrrange_make(int128_zero(), int128_2_64()), false);
610 flatview_simplify(&view
);
615 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
616 MemoryRegionIoeventfd
*fds_new
,
618 MemoryRegionIoeventfd
*fds_old
,
623 /* Generate a symmetric difference of the old and new fd sets, adding
624 * and deleting as necessary.
628 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
629 if (iold
< fds_old_nb
630 && (inew
== fds_new_nb
631 || memory_region_ioeventfd_before(fds_old
[iold
],
633 as
->ops
->ioeventfd_del(as
, &fds_old
[iold
]);
635 } else if (inew
< fds_new_nb
636 && (iold
== fds_old_nb
637 || memory_region_ioeventfd_before(fds_new
[inew
],
639 as
->ops
->ioeventfd_add(as
, &fds_new
[inew
]);
648 static void address_space_update_ioeventfds(AddressSpace
*as
)
651 unsigned ioeventfd_nb
= 0;
652 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
656 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
657 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
658 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
659 int128_sub(fr
->addr
.start
,
660 int128_make64(fr
->offset_in_region
)));
661 if (addrrange_intersects(fr
->addr
, tmp
)) {
663 ioeventfds
= g_realloc(ioeventfds
,
664 ioeventfd_nb
* sizeof(*ioeventfds
));
665 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
666 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
671 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
672 as
->ioeventfds
, as
->ioeventfd_nb
);
674 g_free(as
->ioeventfds
);
675 as
->ioeventfds
= ioeventfds
;
676 as
->ioeventfd_nb
= ioeventfd_nb
;
679 typedef void ListenerCallback(MemoryListener
*listener
,
680 MemoryRegionSection
*mrs
);
682 /* Want "void (&MemoryListener::*callback)(const MemoryRegionSection& s)" */
683 static void memory_listener_update_region(FlatRange
*fr
, AddressSpace
*as
,
684 size_t callback_offset
)
686 MemoryRegionSection section
= {
688 .address_space
= as
->root
,
689 .offset_within_region
= fr
->offset_in_region
,
690 .size
= int128_get64(fr
->addr
.size
),
691 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
693 MemoryListener
*listener
;
695 QLIST_FOREACH(listener
, &memory_listeners
, link
) {
696 ListenerCallback
*callback
697 = *(ListenerCallback
**)((void *)listener
+ callback_offset
);
698 callback(listener
, §ion
);
702 #define MEMORY_LISTENER_UPDATE_REGION(fr, as, callback) \
703 memory_listener_update_region(fr, as, offsetof(MemoryListener, callback))
705 static void address_space_update_topology_pass(AddressSpace
*as
,
711 FlatRange
*frold
, *frnew
;
713 /* Generate a symmetric difference of the old and new memory maps.
714 * Kill ranges in the old map, and instantiate ranges in the new map.
717 while (iold
< old_view
.nr
|| inew
< new_view
.nr
) {
718 if (iold
< old_view
.nr
) {
719 frold
= &old_view
.ranges
[iold
];
723 if (inew
< new_view
.nr
) {
724 frnew
= &new_view
.ranges
[inew
];
731 || int128_lt(frold
->addr
.start
, frnew
->addr
.start
)
732 || (int128_eq(frold
->addr
.start
, frnew
->addr
.start
)
733 && !flatrange_equal(frold
, frnew
)))) {
734 /* In old, but (not in new, or in new but attributes changed). */
737 MEMORY_LISTENER_UPDATE_REGION(frold
, as
, region_del
);
738 as
->ops
->range_del(as
, frold
);
742 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
743 /* In both (logging may have changed) */
746 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
747 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, log_stop
);
748 as
->ops
->log_stop(as
, frnew
);
749 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
750 as
->ops
->log_start(as
, frnew
);
751 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, log_start
);
761 as
->ops
->range_add(as
, frnew
);
762 MEMORY_LISTENER_UPDATE_REGION(frnew
, as
, region_add
);
771 static void address_space_update_topology(AddressSpace
*as
)
773 FlatView old_view
= as
->current_map
;
774 FlatView new_view
= generate_memory_topology(as
->root
);
776 address_space_update_topology_pass(as
, old_view
, new_view
, false);
777 address_space_update_topology_pass(as
, old_view
, new_view
, true);
779 as
->current_map
= new_view
;
780 flatview_destroy(&old_view
);
781 address_space_update_ioeventfds(as
);
784 static void memory_region_update_topology(MemoryRegion
*mr
)
786 if (memory_region_transaction_depth
) {
787 memory_region_update_pending
|= !mr
|| mr
->enabled
;
791 if (mr
&& !mr
->enabled
) {
795 if (address_space_memory
.root
) {
796 address_space_update_topology(&address_space_memory
);
798 if (address_space_io
.root
) {
799 address_space_update_topology(&address_space_io
);
802 memory_region_update_pending
= false;
805 void memory_region_transaction_begin(void)
807 ++memory_region_transaction_depth
;
810 void memory_region_transaction_commit(void)
812 assert(memory_region_transaction_depth
);
813 --memory_region_transaction_depth
;
814 if (!memory_region_transaction_depth
&& memory_region_update_pending
) {
815 memory_region_update_topology(NULL
);
819 static void memory_region_destructor_none(MemoryRegion
*mr
)
823 static void memory_region_destructor_ram(MemoryRegion
*mr
)
825 qemu_ram_free(mr
->ram_addr
);
828 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
830 qemu_ram_free_from_ptr(mr
->ram_addr
);
833 static void memory_region_destructor_iomem(MemoryRegion
*mr
)
835 cpu_unregister_io_memory(mr
->ram_addr
);
838 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
840 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
841 cpu_unregister_io_memory(mr
->ram_addr
& ~TARGET_PAGE_MASK
);
844 static bool memory_region_wrong_endianness(MemoryRegion
*mr
)
846 #ifdef TARGET_WORDS_BIGENDIAN
847 return mr
->ops
->endianness
== DEVICE_LITTLE_ENDIAN
;
849 return mr
->ops
->endianness
== DEVICE_BIG_ENDIAN
;
853 void memory_region_init(MemoryRegion
*mr
,
859 mr
->size
= int128_make64(size
);
860 if (size
== UINT64_MAX
) {
861 mr
->size
= int128_2_64();
867 mr
->terminates
= false;
870 mr
->readonly
= false;
871 mr
->rom_device
= false;
872 mr
->destructor
= memory_region_destructor_none
;
874 mr
->may_overlap
= false;
876 QTAILQ_INIT(&mr
->subregions
);
877 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
878 QTAILQ_INIT(&mr
->coalesced
);
879 mr
->name
= g_strdup(name
);
880 mr
->dirty_log_mask
= 0;
881 mr
->ioeventfd_nb
= 0;
882 mr
->ioeventfds
= NULL
;
885 static bool memory_region_access_valid(MemoryRegion
*mr
,
886 target_phys_addr_t addr
,
890 if (mr
->ops
->valid
.accepts
891 && !mr
->ops
->valid
.accepts(mr
->opaque
, addr
, size
, is_write
)) {
895 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
899 /* Treat zero as compatibility all valid */
900 if (!mr
->ops
->valid
.max_access_size
) {
904 if (size
> mr
->ops
->valid
.max_access_size
905 || size
< mr
->ops
->valid
.min_access_size
) {
911 static uint64_t memory_region_dispatch_read1(MemoryRegion
*mr
,
912 target_phys_addr_t addr
,
917 if (!memory_region_access_valid(mr
, addr
, size
, false)) {
918 return -1U; /* FIXME: better signalling */
921 if (!mr
->ops
->read
) {
922 return mr
->ops
->old_mmio
.read
[bitops_ffsl(size
)](mr
->opaque
, addr
);
925 /* FIXME: support unaligned access */
926 access_with_adjusted_size(addr
+ mr
->offset
, &data
, size
,
927 mr
->ops
->impl
.min_access_size
,
928 mr
->ops
->impl
.max_access_size
,
929 memory_region_read_accessor
, mr
);
934 static void adjust_endianness(MemoryRegion
*mr
, uint64_t *data
, unsigned size
)
936 if (memory_region_wrong_endianness(mr
)) {
941 *data
= bswap16(*data
);
944 *data
= bswap32(*data
);
952 static uint64_t memory_region_dispatch_read(MemoryRegion
*mr
,
953 target_phys_addr_t addr
,
958 ret
= memory_region_dispatch_read1(mr
, addr
, size
);
959 adjust_endianness(mr
, &ret
, size
);
963 static void memory_region_dispatch_write(MemoryRegion
*mr
,
964 target_phys_addr_t addr
,
968 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
969 return; /* FIXME: better signalling */
972 adjust_endianness(mr
, &data
, size
);
974 if (!mr
->ops
->write
) {
975 mr
->ops
->old_mmio
.write
[bitops_ffsl(size
)](mr
->opaque
, addr
, data
);
979 /* FIXME: support unaligned access */
980 access_with_adjusted_size(addr
+ mr
->offset
, &data
, size
,
981 mr
->ops
->impl
.min_access_size
,
982 mr
->ops
->impl
.max_access_size
,
983 memory_region_write_accessor
, mr
);
986 void memory_region_init_io(MemoryRegion
*mr
,
987 const MemoryRegionOps
*ops
,
992 memory_region_init(mr
, name
, size
);
995 mr
->terminates
= true;
996 mr
->destructor
= memory_region_destructor_iomem
;
997 mr
->ram_addr
= cpu_register_io_memory(mr
);
1000 void memory_region_init_ram(MemoryRegion
*mr
,
1004 memory_region_init(mr
, name
, size
);
1006 mr
->terminates
= true;
1007 mr
->destructor
= memory_region_destructor_ram
;
1008 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1011 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1016 memory_region_init(mr
, name
, size
);
1018 mr
->terminates
= true;
1019 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1020 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
1023 void memory_region_init_alias(MemoryRegion
*mr
,
1026 target_phys_addr_t offset
,
1029 memory_region_init(mr
, name
, size
);
1031 mr
->alias_offset
= offset
;
1034 void memory_region_init_rom_device(MemoryRegion
*mr
,
1035 const MemoryRegionOps
*ops
,
1040 memory_region_init(mr
, name
, size
);
1042 mr
->opaque
= opaque
;
1043 mr
->terminates
= true;
1044 mr
->rom_device
= true;
1045 mr
->destructor
= memory_region_destructor_rom_device
;
1046 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1047 mr
->ram_addr
|= cpu_register_io_memory(mr
);
1050 void memory_region_destroy(MemoryRegion
*mr
)
1052 assert(QTAILQ_EMPTY(&mr
->subregions
));
1054 memory_region_clear_coalescing(mr
);
1055 g_free((char *)mr
->name
);
1056 g_free(mr
->ioeventfds
);
1059 uint64_t memory_region_size(MemoryRegion
*mr
)
1061 if (int128_eq(mr
->size
, int128_2_64())) {
1064 return int128_get64(mr
->size
);
1067 const char *memory_region_name(MemoryRegion
*mr
)
1072 bool memory_region_is_ram(MemoryRegion
*mr
)
1077 bool memory_region_is_logging(MemoryRegion
*mr
)
1079 return mr
->dirty_log_mask
;
1082 bool memory_region_is_rom(MemoryRegion
*mr
)
1084 return mr
->ram
&& mr
->readonly
;
1087 void memory_region_set_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1089 mr
->offset
= offset
;
1092 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1094 uint8_t mask
= 1 << client
;
1096 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1097 memory_region_update_topology(mr
);
1100 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1103 assert(mr
->terminates
);
1104 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, 1 << client
);
1107 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
)
1109 assert(mr
->terminates
);
1110 return cpu_physical_memory_set_dirty(mr
->ram_addr
+ addr
);
1113 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1117 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1119 MEMORY_LISTENER_UPDATE_REGION(fr
, &address_space_memory
, log_sync
);
1124 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1126 if (mr
->readonly
!= readonly
) {
1127 mr
->readonly
= readonly
;
1128 memory_region_update_topology(mr
);
1132 void memory_region_rom_device_set_readable(MemoryRegion
*mr
, bool readable
)
1134 if (mr
->readable
!= readable
) {
1135 mr
->readable
= readable
;
1136 memory_region_update_topology(mr
);
1140 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1141 target_phys_addr_t size
, unsigned client
)
1143 assert(mr
->terminates
);
1144 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1145 mr
->ram_addr
+ addr
+ size
,
1149 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1152 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1155 assert(mr
->terminates
);
1157 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1160 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1163 CoalescedMemoryRange
*cmr
;
1166 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1168 qemu_unregister_coalesced_mmio(int128_get64(fr
->addr
.start
),
1169 int128_get64(fr
->addr
.size
));
1170 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1171 tmp
= addrrange_shift(cmr
->addr
,
1172 int128_sub(fr
->addr
.start
,
1173 int128_make64(fr
->offset_in_region
)));
1174 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1177 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1178 qemu_register_coalesced_mmio(int128_get64(tmp
.start
),
1179 int128_get64(tmp
.size
));
1185 void memory_region_set_coalescing(MemoryRegion
*mr
)
1187 memory_region_clear_coalescing(mr
);
1188 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1191 void memory_region_add_coalescing(MemoryRegion
*mr
,
1192 target_phys_addr_t offset
,
1195 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1197 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1198 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1199 memory_region_update_coalesced_range(mr
);
1202 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1204 CoalescedMemoryRange
*cmr
;
1206 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1207 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1208 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1211 memory_region_update_coalesced_range(mr
);
1214 void memory_region_add_eventfd(MemoryRegion
*mr
,
1215 target_phys_addr_t addr
,
1221 MemoryRegionIoeventfd mrfd
= {
1222 .addr
.start
= int128_make64(addr
),
1223 .addr
.size
= int128_make64(size
),
1224 .match_data
= match_data
,
1230 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1231 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1236 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1237 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1238 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1239 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1240 mr
->ioeventfds
[i
] = mrfd
;
1241 memory_region_update_topology(mr
);
1244 void memory_region_del_eventfd(MemoryRegion
*mr
,
1245 target_phys_addr_t addr
,
1251 MemoryRegionIoeventfd mrfd
= {
1252 .addr
.start
= int128_make64(addr
),
1253 .addr
.size
= int128_make64(size
),
1254 .match_data
= match_data
,
1260 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1261 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1265 assert(i
!= mr
->ioeventfd_nb
);
1266 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1267 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1269 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1270 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1271 memory_region_update_topology(mr
);
1274 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1275 target_phys_addr_t offset
,
1276 MemoryRegion
*subregion
)
1278 MemoryRegion
*other
;
1280 assert(!subregion
->parent
);
1281 subregion
->parent
= mr
;
1282 subregion
->addr
= offset
;
1283 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1284 if (subregion
->may_overlap
|| other
->may_overlap
) {
1287 if (int128_gt(int128_make64(offset
),
1288 int128_add(int128_make64(other
->addr
), other
->size
))
1289 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1290 int128_make64(other
->addr
))) {
1294 printf("warning: subregion collision %llx/%llx (%s) "
1295 "vs %llx/%llx (%s)\n",
1296 (unsigned long long)offset
,
1297 (unsigned long long)int128_get64(subregion
->size
),
1299 (unsigned long long)other
->addr
,
1300 (unsigned long long)int128_get64(other
->size
),
1304 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1305 if (subregion
->priority
>= other
->priority
) {
1306 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1310 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1312 memory_region_update_topology(mr
);
1316 void memory_region_add_subregion(MemoryRegion
*mr
,
1317 target_phys_addr_t offset
,
1318 MemoryRegion
*subregion
)
1320 subregion
->may_overlap
= false;
1321 subregion
->priority
= 0;
1322 memory_region_add_subregion_common(mr
, offset
, subregion
);
1325 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1326 target_phys_addr_t offset
,
1327 MemoryRegion
*subregion
,
1330 subregion
->may_overlap
= true;
1331 subregion
->priority
= priority
;
1332 memory_region_add_subregion_common(mr
, offset
, subregion
);
1335 void memory_region_del_subregion(MemoryRegion
*mr
,
1336 MemoryRegion
*subregion
)
1338 assert(subregion
->parent
== mr
);
1339 subregion
->parent
= NULL
;
1340 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1341 memory_region_update_topology(mr
);
1344 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1346 if (enabled
== mr
->enabled
) {
1349 mr
->enabled
= enabled
;
1350 memory_region_update_topology(NULL
);
1353 void memory_region_set_address(MemoryRegion
*mr
, target_phys_addr_t addr
)
1355 MemoryRegion
*parent
= mr
->parent
;
1356 unsigned priority
= mr
->priority
;
1357 bool may_overlap
= mr
->may_overlap
;
1359 if (addr
== mr
->addr
|| !parent
) {
1364 memory_region_transaction_begin();
1365 memory_region_del_subregion(parent
, mr
);
1367 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1369 memory_region_add_subregion(parent
, addr
, mr
);
1371 memory_region_transaction_commit();
1374 void memory_region_set_alias_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1376 target_phys_addr_t old_offset
= mr
->alias_offset
;
1379 mr
->alias_offset
= offset
;
1381 if (offset
== old_offset
|| !mr
->parent
) {
1385 memory_region_update_topology(mr
);
1388 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1390 return mr
->ram_addr
;
1393 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1395 const AddrRange
*addr
= addr_
;
1396 const FlatRange
*fr
= fr_
;
1398 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1400 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1406 static FlatRange
*address_space_lookup(AddressSpace
*as
, AddrRange addr
)
1408 return bsearch(&addr
, as
->current_map
.ranges
, as
->current_map
.nr
,
1409 sizeof(FlatRange
), cmp_flatrange_addr
);
1412 MemoryRegionSection
memory_region_find(MemoryRegion
*address_space
,
1413 target_phys_addr_t addr
, uint64_t size
)
1415 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1416 AddrRange range
= addrrange_make(int128_make64(addr
),
1417 int128_make64(size
));
1418 FlatRange
*fr
= address_space_lookup(as
, range
);
1419 MemoryRegionSection ret
= { .mr
= NULL
, .size
= 0 };
1425 while (fr
> as
->current_map
.ranges
1426 && addrrange_intersects(fr
[-1].addr
, range
)) {
1431 range
= addrrange_intersection(range
, fr
->addr
);
1432 ret
.offset_within_region
= fr
->offset_in_region
;
1433 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1435 ret
.size
= int128_get64(range
.size
);
1436 ret
.offset_within_address_space
= int128_get64(range
.start
);
1440 void memory_global_sync_dirty_bitmap(MemoryRegion
*address_space
)
1442 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1445 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1446 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, log_sync
);
1450 void memory_global_dirty_log_start(void)
1452 MemoryListener
*listener
;
1454 cpu_physical_memory_set_dirty_tracking(1);
1455 global_dirty_log
= true;
1456 QLIST_FOREACH(listener
, &memory_listeners
, link
) {
1457 listener
->log_global_start(listener
);
1461 void memory_global_dirty_log_stop(void)
1463 MemoryListener
*listener
;
1465 global_dirty_log
= false;
1466 QLIST_FOREACH(listener
, &memory_listeners
, link
) {
1467 listener
->log_global_stop(listener
);
1469 cpu_physical_memory_set_dirty_tracking(0);
1472 static void listener_add_address_space(MemoryListener
*listener
,
1477 if (global_dirty_log
) {
1478 listener
->log_global_start(listener
);
1480 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1481 MemoryRegionSection section
= {
1483 .address_space
= as
->root
,
1484 .offset_within_region
= fr
->offset_in_region
,
1485 .size
= int128_get64(fr
->addr
.size
),
1486 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1488 listener
->region_add(listener
, §ion
);
1492 void memory_listener_register(MemoryListener
*listener
)
1494 QLIST_INSERT_HEAD(&memory_listeners
, listener
, link
);
1495 listener_add_address_space(listener
, &address_space_memory
);
1496 listener_add_address_space(listener
, &address_space_io
);
1499 void memory_listener_unregister(MemoryListener
*listener
)
1501 QLIST_REMOVE(listener
, link
);
1504 void set_system_memory_map(MemoryRegion
*mr
)
1506 address_space_memory
.root
= mr
;
1507 memory_region_update_topology(NULL
);
1510 void set_system_io_map(MemoryRegion
*mr
)
1512 address_space_io
.root
= mr
;
1513 memory_region_update_topology(NULL
);
1516 uint64_t io_mem_read(int io_index
, target_phys_addr_t addr
, unsigned size
)
1518 return memory_region_dispatch_read(io_mem_region
[io_index
], addr
, size
);
1521 void io_mem_write(int io_index
, target_phys_addr_t addr
,
1522 uint64_t val
, unsigned size
)
1524 memory_region_dispatch_write(io_mem_region
[io_index
], addr
, val
, size
);
1527 typedef struct MemoryRegionList MemoryRegionList
;
1529 struct MemoryRegionList
{
1530 const MemoryRegion
*mr
;
1532 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1535 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1537 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1538 const MemoryRegion
*mr
, unsigned int level
,
1539 target_phys_addr_t base
,
1540 MemoryRegionListHead
*alias_print_queue
)
1542 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1543 MemoryRegionListHead submr_print_queue
;
1544 const MemoryRegion
*submr
;
1551 for (i
= 0; i
< level
; i
++) {
1556 MemoryRegionList
*ml
;
1559 /* check if the alias is already in the queue */
1560 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1561 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1567 ml
= g_new(MemoryRegionList
, 1);
1569 ml
->printed
= false;
1570 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1572 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d): alias %s @%s "
1573 TARGET_FMT_plx
"-" TARGET_FMT_plx
"\n",
1576 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1582 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1);
1584 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d): %s\n",
1587 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1592 QTAILQ_INIT(&submr_print_queue
);
1594 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1595 new_ml
= g_new(MemoryRegionList
, 1);
1597 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1598 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1599 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1600 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1601 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1607 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1611 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1612 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1616 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1621 void mtree_info(fprintf_function mon_printf
, void *f
)
1623 MemoryRegionListHead ml_head
;
1624 MemoryRegionList
*ml
, *ml2
;
1626 QTAILQ_INIT(&ml_head
);
1628 mon_printf(f
, "memory\n");
1629 mtree_print_mr(mon_printf
, f
, address_space_memory
.root
, 0, 0, &ml_head
);
1631 /* print aliased regions */
1632 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1634 mon_printf(f
, "%s\n", ml
->mr
->name
);
1635 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1639 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
1643 if (address_space_io
.root
&&
1644 !QTAILQ_EMPTY(&address_space_io
.root
->subregions
)) {
1645 QTAILQ_INIT(&ml_head
);
1646 mon_printf(f
, "I/O\n");
1647 mtree_print_mr(mon_printf
, f
, address_space_io
.root
, 0, 0, &ml_head
);