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
);
951 static uint64_t memory_region_dispatch_read(MemoryRegion
*mr
,
952 target_phys_addr_t addr
,
957 ret
= memory_region_dispatch_read1(mr
, addr
, size
);
958 adjust_endianness(mr
, &ret
, size
);
962 static void memory_region_dispatch_write(MemoryRegion
*mr
,
963 target_phys_addr_t addr
,
967 if (!memory_region_access_valid(mr
, addr
, size
, true)) {
968 return; /* FIXME: better signalling */
971 adjust_endianness(mr
, &data
, size
);
973 if (!mr
->ops
->write
) {
974 mr
->ops
->old_mmio
.write
[bitops_ffsl(size
)](mr
->opaque
, addr
, data
);
978 /* FIXME: support unaligned access */
979 access_with_adjusted_size(addr
+ mr
->offset
, &data
, size
,
980 mr
->ops
->impl
.min_access_size
,
981 mr
->ops
->impl
.max_access_size
,
982 memory_region_write_accessor
, mr
);
985 void memory_region_init_io(MemoryRegion
*mr
,
986 const MemoryRegionOps
*ops
,
991 memory_region_init(mr
, name
, size
);
994 mr
->terminates
= true;
995 mr
->destructor
= memory_region_destructor_iomem
;
996 mr
->ram_addr
= cpu_register_io_memory(mr
);
999 void memory_region_init_ram(MemoryRegion
*mr
,
1003 memory_region_init(mr
, name
, size
);
1005 mr
->terminates
= true;
1006 mr
->destructor
= memory_region_destructor_ram
;
1007 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1010 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1015 memory_region_init(mr
, name
, size
);
1017 mr
->terminates
= true;
1018 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
1019 mr
->ram_addr
= qemu_ram_alloc_from_ptr(size
, ptr
, mr
);
1022 void memory_region_init_alias(MemoryRegion
*mr
,
1025 target_phys_addr_t offset
,
1028 memory_region_init(mr
, name
, size
);
1030 mr
->alias_offset
= offset
;
1033 void memory_region_init_rom_device(MemoryRegion
*mr
,
1034 const MemoryRegionOps
*ops
,
1039 memory_region_init(mr
, name
, size
);
1041 mr
->opaque
= opaque
;
1042 mr
->terminates
= true;
1043 mr
->rom_device
= true;
1044 mr
->destructor
= memory_region_destructor_rom_device
;
1045 mr
->ram_addr
= qemu_ram_alloc(size
, mr
);
1046 mr
->ram_addr
|= cpu_register_io_memory(mr
);
1049 void memory_region_destroy(MemoryRegion
*mr
)
1051 assert(QTAILQ_EMPTY(&mr
->subregions
));
1053 memory_region_clear_coalescing(mr
);
1054 g_free((char *)mr
->name
);
1055 g_free(mr
->ioeventfds
);
1058 uint64_t memory_region_size(MemoryRegion
*mr
)
1060 if (int128_eq(mr
->size
, int128_2_64())) {
1063 return int128_get64(mr
->size
);
1066 const char *memory_region_name(MemoryRegion
*mr
)
1071 bool memory_region_is_ram(MemoryRegion
*mr
)
1076 bool memory_region_is_logging(MemoryRegion
*mr
)
1078 return mr
->dirty_log_mask
;
1081 bool memory_region_is_rom(MemoryRegion
*mr
)
1083 return mr
->ram
&& mr
->readonly
;
1086 void memory_region_set_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1088 mr
->offset
= offset
;
1091 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1093 uint8_t mask
= 1 << client
;
1095 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1096 memory_region_update_topology(mr
);
1099 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1102 assert(mr
->terminates
);
1103 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, 1 << client
);
1106 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
)
1108 assert(mr
->terminates
);
1109 return cpu_physical_memory_set_dirty(mr
->ram_addr
+ addr
);
1112 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1116 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1118 MEMORY_LISTENER_UPDATE_REGION(fr
, &address_space_memory
, log_sync
);
1123 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1125 if (mr
->readonly
!= readonly
) {
1126 mr
->readonly
= readonly
;
1127 memory_region_update_topology(mr
);
1131 void memory_region_rom_device_set_readable(MemoryRegion
*mr
, bool readable
)
1133 if (mr
->readable
!= readable
) {
1134 mr
->readable
= readable
;
1135 memory_region_update_topology(mr
);
1139 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1140 target_phys_addr_t size
, unsigned client
)
1142 assert(mr
->terminates
);
1143 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1144 mr
->ram_addr
+ addr
+ size
,
1148 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1151 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1154 assert(mr
->terminates
);
1156 return qemu_get_ram_ptr(mr
->ram_addr
& TARGET_PAGE_MASK
);
1159 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1162 CoalescedMemoryRange
*cmr
;
1165 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1167 qemu_unregister_coalesced_mmio(int128_get64(fr
->addr
.start
),
1168 int128_get64(fr
->addr
.size
));
1169 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1170 tmp
= addrrange_shift(cmr
->addr
,
1171 int128_sub(fr
->addr
.start
,
1172 int128_make64(fr
->offset_in_region
)));
1173 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1176 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1177 qemu_register_coalesced_mmio(int128_get64(tmp
.start
),
1178 int128_get64(tmp
.size
));
1184 void memory_region_set_coalescing(MemoryRegion
*mr
)
1186 memory_region_clear_coalescing(mr
);
1187 memory_region_add_coalescing(mr
, 0, int128_get64(mr
->size
));
1190 void memory_region_add_coalescing(MemoryRegion
*mr
,
1191 target_phys_addr_t offset
,
1194 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1196 cmr
->addr
= addrrange_make(int128_make64(offset
), int128_make64(size
));
1197 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1198 memory_region_update_coalesced_range(mr
);
1201 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1203 CoalescedMemoryRange
*cmr
;
1205 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1206 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1207 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1210 memory_region_update_coalesced_range(mr
);
1213 void memory_region_add_eventfd(MemoryRegion
*mr
,
1214 target_phys_addr_t addr
,
1220 MemoryRegionIoeventfd mrfd
= {
1221 .addr
.start
= int128_make64(addr
),
1222 .addr
.size
= int128_make64(size
),
1223 .match_data
= match_data
,
1229 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1230 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1235 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1236 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1237 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1238 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1239 mr
->ioeventfds
[i
] = mrfd
;
1240 memory_region_update_topology(mr
);
1243 void memory_region_del_eventfd(MemoryRegion
*mr
,
1244 target_phys_addr_t addr
,
1250 MemoryRegionIoeventfd mrfd
= {
1251 .addr
.start
= int128_make64(addr
),
1252 .addr
.size
= int128_make64(size
),
1253 .match_data
= match_data
,
1259 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1260 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1264 assert(i
!= mr
->ioeventfd_nb
);
1265 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1266 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1268 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1269 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1270 memory_region_update_topology(mr
);
1273 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1274 target_phys_addr_t offset
,
1275 MemoryRegion
*subregion
)
1277 MemoryRegion
*other
;
1279 assert(!subregion
->parent
);
1280 subregion
->parent
= mr
;
1281 subregion
->addr
= offset
;
1282 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1283 if (subregion
->may_overlap
|| other
->may_overlap
) {
1286 if (int128_gt(int128_make64(offset
),
1287 int128_add(int128_make64(other
->addr
), other
->size
))
1288 || int128_le(int128_add(int128_make64(offset
), subregion
->size
),
1289 int128_make64(other
->addr
))) {
1293 printf("warning: subregion collision %llx/%llx (%s) "
1294 "vs %llx/%llx (%s)\n",
1295 (unsigned long long)offset
,
1296 (unsigned long long)int128_get64(subregion
->size
),
1298 (unsigned long long)other
->addr
,
1299 (unsigned long long)int128_get64(other
->size
),
1303 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1304 if (subregion
->priority
>= other
->priority
) {
1305 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1309 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1311 memory_region_update_topology(mr
);
1315 void memory_region_add_subregion(MemoryRegion
*mr
,
1316 target_phys_addr_t offset
,
1317 MemoryRegion
*subregion
)
1319 subregion
->may_overlap
= false;
1320 subregion
->priority
= 0;
1321 memory_region_add_subregion_common(mr
, offset
, subregion
);
1324 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1325 target_phys_addr_t offset
,
1326 MemoryRegion
*subregion
,
1329 subregion
->may_overlap
= true;
1330 subregion
->priority
= priority
;
1331 memory_region_add_subregion_common(mr
, offset
, subregion
);
1334 void memory_region_del_subregion(MemoryRegion
*mr
,
1335 MemoryRegion
*subregion
)
1337 assert(subregion
->parent
== mr
);
1338 subregion
->parent
= NULL
;
1339 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1340 memory_region_update_topology(mr
);
1343 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
)
1345 if (enabled
== mr
->enabled
) {
1348 mr
->enabled
= enabled
;
1349 memory_region_update_topology(NULL
);
1352 void memory_region_set_address(MemoryRegion
*mr
, target_phys_addr_t addr
)
1354 MemoryRegion
*parent
= mr
->parent
;
1355 unsigned priority
= mr
->priority
;
1356 bool may_overlap
= mr
->may_overlap
;
1358 if (addr
== mr
->addr
|| !parent
) {
1363 memory_region_transaction_begin();
1364 memory_region_del_subregion(parent
, mr
);
1366 memory_region_add_subregion_overlap(parent
, addr
, mr
, priority
);
1368 memory_region_add_subregion(parent
, addr
, mr
);
1370 memory_region_transaction_commit();
1373 void memory_region_set_alias_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1375 target_phys_addr_t old_offset
= mr
->alias_offset
;
1378 mr
->alias_offset
= offset
;
1380 if (offset
== old_offset
|| !mr
->parent
) {
1384 memory_region_update_topology(mr
);
1387 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
)
1389 return mr
->ram_addr
;
1392 static int cmp_flatrange_addr(const void *addr_
, const void *fr_
)
1394 const AddrRange
*addr
= addr_
;
1395 const FlatRange
*fr
= fr_
;
1397 if (int128_le(addrrange_end(*addr
), fr
->addr
.start
)) {
1399 } else if (int128_ge(addr
->start
, addrrange_end(fr
->addr
))) {
1405 static FlatRange
*address_space_lookup(AddressSpace
*as
, AddrRange addr
)
1407 return bsearch(&addr
, as
->current_map
.ranges
, as
->current_map
.nr
,
1408 sizeof(FlatRange
), cmp_flatrange_addr
);
1411 MemoryRegionSection
memory_region_find(MemoryRegion
*address_space
,
1412 target_phys_addr_t addr
, uint64_t size
)
1414 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1415 AddrRange range
= addrrange_make(int128_make64(addr
),
1416 int128_make64(size
));
1417 FlatRange
*fr
= address_space_lookup(as
, range
);
1418 MemoryRegionSection ret
= { .mr
= NULL
, .size
= 0 };
1424 while (fr
> as
->current_map
.ranges
1425 && addrrange_intersects(fr
[-1].addr
, range
)) {
1430 range
= addrrange_intersection(range
, fr
->addr
);
1431 ret
.offset_within_region
= fr
->offset_in_region
;
1432 ret
.offset_within_region
+= int128_get64(int128_sub(range
.start
,
1434 ret
.size
= int128_get64(range
.size
);
1435 ret
.offset_within_address_space
= int128_get64(range
.start
);
1439 void memory_global_sync_dirty_bitmap(MemoryRegion
*address_space
)
1441 AddressSpace
*as
= memory_region_to_address_space(address_space
);
1444 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1445 MEMORY_LISTENER_UPDATE_REGION(fr
, as
, log_sync
);
1449 void memory_global_dirty_log_start(void)
1451 MemoryListener
*listener
;
1453 cpu_physical_memory_set_dirty_tracking(1);
1454 global_dirty_log
= true;
1455 QLIST_FOREACH(listener
, &memory_listeners
, link
) {
1456 listener
->log_global_start(listener
);
1460 void memory_global_dirty_log_stop(void)
1462 MemoryListener
*listener
;
1464 global_dirty_log
= false;
1465 QLIST_FOREACH(listener
, &memory_listeners
, link
) {
1466 listener
->log_global_stop(listener
);
1468 cpu_physical_memory_set_dirty_tracking(0);
1471 static void listener_add_address_space(MemoryListener
*listener
,
1476 if (global_dirty_log
) {
1477 listener
->log_global_start(listener
);
1479 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
1480 MemoryRegionSection section
= {
1482 .address_space
= as
->root
,
1483 .offset_within_region
= fr
->offset_in_region
,
1484 .size
= int128_get64(fr
->addr
.size
),
1485 .offset_within_address_space
= int128_get64(fr
->addr
.start
),
1487 listener
->region_add(listener
, §ion
);
1491 void memory_listener_register(MemoryListener
*listener
)
1493 QLIST_INSERT_HEAD(&memory_listeners
, listener
, link
);
1494 listener_add_address_space(listener
, &address_space_memory
);
1495 listener_add_address_space(listener
, &address_space_io
);
1498 void memory_listener_unregister(MemoryListener
*listener
)
1500 QLIST_REMOVE(listener
, link
);
1503 void set_system_memory_map(MemoryRegion
*mr
)
1505 address_space_memory
.root
= mr
;
1506 memory_region_update_topology(NULL
);
1509 void set_system_io_map(MemoryRegion
*mr
)
1511 address_space_io
.root
= mr
;
1512 memory_region_update_topology(NULL
);
1515 uint64_t io_mem_read(int io_index
, target_phys_addr_t addr
, unsigned size
)
1517 return memory_region_dispatch_read(io_mem_region
[io_index
], addr
, size
);
1520 void io_mem_write(int io_index
, target_phys_addr_t addr
,
1521 uint64_t val
, unsigned size
)
1523 memory_region_dispatch_write(io_mem_region
[io_index
], addr
, val
, size
);
1526 typedef struct MemoryRegionList MemoryRegionList
;
1528 struct MemoryRegionList
{
1529 const MemoryRegion
*mr
;
1531 QTAILQ_ENTRY(MemoryRegionList
) queue
;
1534 typedef QTAILQ_HEAD(queue
, MemoryRegionList
) MemoryRegionListHead
;
1536 static void mtree_print_mr(fprintf_function mon_printf
, void *f
,
1537 const MemoryRegion
*mr
, unsigned int level
,
1538 target_phys_addr_t base
,
1539 MemoryRegionListHead
*alias_print_queue
)
1541 MemoryRegionList
*new_ml
, *ml
, *next_ml
;
1542 MemoryRegionListHead submr_print_queue
;
1543 const MemoryRegion
*submr
;
1550 for (i
= 0; i
< level
; i
++) {
1555 MemoryRegionList
*ml
;
1558 /* check if the alias is already in the queue */
1559 QTAILQ_FOREACH(ml
, alias_print_queue
, queue
) {
1560 if (ml
->mr
== mr
->alias
&& !ml
->printed
) {
1566 ml
= g_new(MemoryRegionList
, 1);
1568 ml
->printed
= false;
1569 QTAILQ_INSERT_TAIL(alias_print_queue
, ml
, queue
);
1571 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d): alias %s @%s "
1572 TARGET_FMT_plx
"-" TARGET_FMT_plx
"\n",
1575 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1581 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1);
1583 mon_printf(f
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" (prio %d): %s\n",
1586 + (target_phys_addr_t
)int128_get64(mr
->size
) - 1,
1591 QTAILQ_INIT(&submr_print_queue
);
1593 QTAILQ_FOREACH(submr
, &mr
->subregions
, subregions_link
) {
1594 new_ml
= g_new(MemoryRegionList
, 1);
1596 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1597 if (new_ml
->mr
->addr
< ml
->mr
->addr
||
1598 (new_ml
->mr
->addr
== ml
->mr
->addr
&&
1599 new_ml
->mr
->priority
> ml
->mr
->priority
)) {
1600 QTAILQ_INSERT_BEFORE(ml
, new_ml
, queue
);
1606 QTAILQ_INSERT_TAIL(&submr_print_queue
, new_ml
, queue
);
1610 QTAILQ_FOREACH(ml
, &submr_print_queue
, queue
) {
1611 mtree_print_mr(mon_printf
, f
, ml
->mr
, level
+ 1, base
+ mr
->addr
,
1615 QTAILQ_FOREACH_SAFE(ml
, &submr_print_queue
, queue
, next_ml
) {
1620 void mtree_info(fprintf_function mon_printf
, void *f
)
1622 MemoryRegionListHead ml_head
;
1623 MemoryRegionList
*ml
, *ml2
;
1625 QTAILQ_INIT(&ml_head
);
1627 mon_printf(f
, "memory\n");
1628 mtree_print_mr(mon_printf
, f
, address_space_memory
.root
, 0, 0, &ml_head
);
1630 /* print aliased regions */
1631 QTAILQ_FOREACH(ml
, &ml_head
, queue
) {
1633 mon_printf(f
, "%s\n", ml
->mr
->name
);
1634 mtree_print_mr(mon_printf
, f
, ml
->mr
, 0, 0, &ml_head
);
1638 QTAILQ_FOREACH_SAFE(ml
, &ml_head
, queue
, ml2
) {
1642 if (address_space_io
.root
&&
1643 !QTAILQ_EMPTY(&address_space_io
.root
->subregions
)) {
1644 QTAILQ_INIT(&ml_head
);
1645 mon_printf(f
, "I/O\n");
1646 mtree_print_mr(mon_printf
, f
, address_space_io
.root
, 0, 0, &ml_head
);