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 unsigned memory_region_transaction_depth
= 0;
23 typedef struct AddrRange AddrRange
;
26 * Note using signed integers limits us to physical addresses at most
27 * 63 bits wide. They are needed for negative offsetting in aliases
28 * (large MemoryRegion::alias_offset).
35 static AddrRange
addrrange_make(int64_t start
, int64_t size
)
37 return (AddrRange
) { start
, size
};
40 static bool addrrange_equal(AddrRange r1
, AddrRange r2
)
42 return r1
.start
== r2
.start
&& r1
.size
== r2
.size
;
45 static int64_t addrrange_end(AddrRange r
)
47 return r
.start
+ r
.size
;
50 static AddrRange
addrrange_shift(AddrRange range
, int64_t delta
)
56 static bool addrrange_intersects(AddrRange r1
, AddrRange r2
)
58 return (r1
.start
>= r2
.start
&& r1
.start
< r2
.start
+ r2
.size
)
59 || (r2
.start
>= r1
.start
&& r2
.start
< r1
.start
+ r1
.size
);
62 static AddrRange
addrrange_intersection(AddrRange r1
, AddrRange r2
)
64 int64_t start
= MAX(r1
.start
, r2
.start
);
65 /* off-by-one arithmetic to prevent overflow */
66 int64_t end
= MIN(addrrange_end(r1
) - 1, addrrange_end(r2
) - 1);
67 return addrrange_make(start
, end
- start
+ 1);
70 struct CoalescedMemoryRange
{
72 QTAILQ_ENTRY(CoalescedMemoryRange
) link
;
75 struct MemoryRegionIoeventfd
{
82 static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd a
,
83 MemoryRegionIoeventfd b
)
85 if (a
.addr
.start
< b
.addr
.start
) {
87 } else if (a
.addr
.start
> b
.addr
.start
) {
89 } else if (a
.addr
.size
< b
.addr
.size
) {
91 } else if (a
.addr
.size
> b
.addr
.size
) {
93 } else if (a
.match_data
< b
.match_data
) {
95 } else if (a
.match_data
> b
.match_data
) {
97 } else if (a
.match_data
) {
98 if (a
.data
< b
.data
) {
100 } else if (a
.data
> b
.data
) {
106 } else if (a
.fd
> b
.fd
) {
112 static bool memory_region_ioeventfd_equal(MemoryRegionIoeventfd a
,
113 MemoryRegionIoeventfd b
)
115 return !memory_region_ioeventfd_before(a
, b
)
116 && !memory_region_ioeventfd_before(b
, a
);
119 typedef struct FlatRange FlatRange
;
120 typedef struct FlatView FlatView
;
122 /* Range of memory in the global map. Addresses are absolute. */
125 target_phys_addr_t offset_in_region
;
127 uint8_t dirty_log_mask
;
131 /* Flattened global view of current active memory hierarchy. Kept in sorted
137 unsigned nr_allocated
;
140 typedef struct AddressSpace AddressSpace
;
141 typedef struct AddressSpaceOps AddressSpaceOps
;
143 /* A system address space - I/O, memory, etc. */
144 struct AddressSpace
{
145 const AddressSpaceOps
*ops
;
147 FlatView current_map
;
149 MemoryRegionIoeventfd
*ioeventfds
;
152 struct AddressSpaceOps
{
153 void (*range_add
)(AddressSpace
*as
, FlatRange
*fr
);
154 void (*range_del
)(AddressSpace
*as
, FlatRange
*fr
);
155 void (*log_start
)(AddressSpace
*as
, FlatRange
*fr
);
156 void (*log_stop
)(AddressSpace
*as
, FlatRange
*fr
);
157 void (*ioeventfd_add
)(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
);
158 void (*ioeventfd_del
)(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
);
161 #define FOR_EACH_FLAT_RANGE(var, view) \
162 for (var = (view)->ranges; var < (view)->ranges + (view)->nr; ++var)
164 static bool flatrange_equal(FlatRange
*a
, FlatRange
*b
)
166 return a
->mr
== b
->mr
167 && addrrange_equal(a
->addr
, b
->addr
)
168 && a
->offset_in_region
== b
->offset_in_region
169 && a
->readable
== b
->readable
;
172 static void flatview_init(FlatView
*view
)
176 view
->nr_allocated
= 0;
179 /* Insert a range into a given position. Caller is responsible for maintaining
182 static void flatview_insert(FlatView
*view
, unsigned pos
, FlatRange
*range
)
184 if (view
->nr
== view
->nr_allocated
) {
185 view
->nr_allocated
= MAX(2 * view
->nr
, 10);
186 view
->ranges
= g_realloc(view
->ranges
,
187 view
->nr_allocated
* sizeof(*view
->ranges
));
189 memmove(view
->ranges
+ pos
+ 1, view
->ranges
+ pos
,
190 (view
->nr
- pos
) * sizeof(FlatRange
));
191 view
->ranges
[pos
] = *range
;
195 static void flatview_destroy(FlatView
*view
)
197 g_free(view
->ranges
);
200 static bool can_merge(FlatRange
*r1
, FlatRange
*r2
)
202 return addrrange_end(r1
->addr
) == r2
->addr
.start
204 && r1
->offset_in_region
+ r1
->addr
.size
== r2
->offset_in_region
205 && r1
->dirty_log_mask
== r2
->dirty_log_mask
206 && r1
->readable
== r2
->readable
;
209 /* Attempt to simplify a view by merging ajacent ranges */
210 static void flatview_simplify(FlatView
*view
)
215 while (i
< view
->nr
) {
218 && can_merge(&view
->ranges
[j
-1], &view
->ranges
[j
])) {
219 view
->ranges
[i
].addr
.size
+= view
->ranges
[j
].addr
.size
;
223 memmove(&view
->ranges
[i
], &view
->ranges
[j
],
224 (view
->nr
- j
) * sizeof(view
->ranges
[j
]));
229 static void memory_region_read_accessor(void *opaque
,
230 target_phys_addr_t addr
,
236 MemoryRegion
*mr
= opaque
;
239 tmp
= mr
->ops
->read(mr
->opaque
, addr
, size
);
240 *value
|= (tmp
& mask
) << shift
;
243 static void memory_region_write_accessor(void *opaque
,
244 target_phys_addr_t addr
,
250 MemoryRegion
*mr
= opaque
;
253 tmp
= (*value
>> shift
) & mask
;
254 mr
->ops
->write(mr
->opaque
, addr
, tmp
, size
);
257 static void access_with_adjusted_size(target_phys_addr_t addr
,
260 unsigned access_size_min
,
261 unsigned access_size_max
,
262 void (*access
)(void *opaque
,
263 target_phys_addr_t addr
,
270 uint64_t access_mask
;
271 unsigned access_size
;
274 if (!access_size_min
) {
277 if (!access_size_max
) {
280 access_size
= MAX(MIN(size
, access_size_max
), access_size_min
);
281 access_mask
= -1ULL >> (64 - access_size
* 8);
282 for (i
= 0; i
< size
; i
+= access_size
) {
283 /* FIXME: big-endian support */
284 access(opaque
, addr
+ i
, value
, access_size
, i
* 8, access_mask
);
288 static void memory_region_prepare_ram_addr(MemoryRegion
*mr
);
290 static void as_memory_range_add(AddressSpace
*as
, FlatRange
*fr
)
292 ram_addr_t phys_offset
, region_offset
;
294 memory_region_prepare_ram_addr(fr
->mr
);
296 phys_offset
= fr
->mr
->ram_addr
;
297 region_offset
= fr
->offset_in_region
;
298 /* cpu_register_physical_memory_log() wants region_offset for
299 * mmio, but prefers offseting phys_offset for RAM. Humour it.
301 if ((phys_offset
& ~TARGET_PAGE_MASK
) <= IO_MEM_ROM
) {
302 phys_offset
+= region_offset
;
307 phys_offset
&= TARGET_PAGE_MASK
;
310 cpu_register_physical_memory_log(fr
->addr
.start
,
317 static void as_memory_range_del(AddressSpace
*as
, FlatRange
*fr
)
319 if (fr
->dirty_log_mask
) {
320 cpu_physical_sync_dirty_bitmap(fr
->addr
.start
,
321 fr
->addr
.start
+ fr
->addr
.size
);
323 cpu_register_physical_memory(fr
->addr
.start
, fr
->addr
.size
,
327 static void as_memory_log_start(AddressSpace
*as
, FlatRange
*fr
)
329 cpu_physical_log_start(fr
->addr
.start
, fr
->addr
.size
);
332 static void as_memory_log_stop(AddressSpace
*as
, FlatRange
*fr
)
334 cpu_physical_log_stop(fr
->addr
.start
, fr
->addr
.size
);
337 static void as_memory_ioeventfd_add(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
341 assert(fd
->match_data
&& fd
->addr
.size
== 4);
343 r
= kvm_set_ioeventfd_mmio_long(fd
->fd
, fd
->addr
.start
, fd
->data
, true);
349 static void as_memory_ioeventfd_del(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
353 r
= kvm_set_ioeventfd_mmio_long(fd
->fd
, fd
->addr
.start
, fd
->data
, false);
359 static const AddressSpaceOps address_space_ops_memory
= {
360 .range_add
= as_memory_range_add
,
361 .range_del
= as_memory_range_del
,
362 .log_start
= as_memory_log_start
,
363 .log_stop
= as_memory_log_stop
,
364 .ioeventfd_add
= as_memory_ioeventfd_add
,
365 .ioeventfd_del
= as_memory_ioeventfd_del
,
368 static AddressSpace address_space_memory
= {
369 .ops
= &address_space_ops_memory
,
372 static const MemoryRegionPortio
*find_portio(MemoryRegion
*mr
, uint64_t offset
,
373 unsigned width
, bool write
)
375 const MemoryRegionPortio
*mrp
;
377 for (mrp
= mr
->ops
->old_portio
; mrp
->size
; ++mrp
) {
378 if (offset
>= mrp
->offset
&& offset
< mrp
->offset
+ mrp
->len
379 && width
== mrp
->size
380 && (write
? (bool)mrp
->write
: (bool)mrp
->read
)) {
387 static void memory_region_iorange_read(IORange
*iorange
,
392 MemoryRegion
*mr
= container_of(iorange
, MemoryRegion
, iorange
);
394 if (mr
->ops
->old_portio
) {
395 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
, width
, false);
397 *data
= ((uint64_t)1 << (width
* 8)) - 1;
399 *data
= mrp
->read(mr
->opaque
, offset
- mrp
->offset
);
404 access_with_adjusted_size(offset
, data
, width
,
405 mr
->ops
->impl
.min_access_size
,
406 mr
->ops
->impl
.max_access_size
,
407 memory_region_read_accessor
, mr
);
410 static void memory_region_iorange_write(IORange
*iorange
,
415 MemoryRegion
*mr
= container_of(iorange
, MemoryRegion
, iorange
);
417 if (mr
->ops
->old_portio
) {
418 const MemoryRegionPortio
*mrp
= find_portio(mr
, offset
, width
, true);
421 mrp
->write(mr
->opaque
, offset
- mrp
->offset
, data
);
425 access_with_adjusted_size(offset
, &data
, width
,
426 mr
->ops
->impl
.min_access_size
,
427 mr
->ops
->impl
.max_access_size
,
428 memory_region_write_accessor
, mr
);
431 static const IORangeOps memory_region_iorange_ops
= {
432 .read
= memory_region_iorange_read
,
433 .write
= memory_region_iorange_write
,
436 static void as_io_range_add(AddressSpace
*as
, FlatRange
*fr
)
438 iorange_init(&fr
->mr
->iorange
, &memory_region_iorange_ops
,
439 fr
->addr
.start
,fr
->addr
.size
);
440 ioport_register(&fr
->mr
->iorange
);
443 static void as_io_range_del(AddressSpace
*as
, FlatRange
*fr
)
445 isa_unassign_ioport(fr
->addr
.start
, fr
->addr
.size
);
448 static void as_io_ioeventfd_add(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
452 assert(fd
->match_data
&& fd
->addr
.size
== 2);
454 r
= kvm_set_ioeventfd_pio_word(fd
->fd
, fd
->addr
.start
, fd
->data
, true);
460 static void as_io_ioeventfd_del(AddressSpace
*as
, MemoryRegionIoeventfd
*fd
)
464 r
= kvm_set_ioeventfd_pio_word(fd
->fd
, fd
->addr
.start
, fd
->data
, false);
470 static const AddressSpaceOps address_space_ops_io
= {
471 .range_add
= as_io_range_add
,
472 .range_del
= as_io_range_del
,
473 .ioeventfd_add
= as_io_ioeventfd_add
,
474 .ioeventfd_del
= as_io_ioeventfd_del
,
477 static AddressSpace address_space_io
= {
478 .ops
= &address_space_ops_io
,
481 /* Render a memory region into the global view. Ranges in @view obscure
484 static void render_memory_region(FlatView
*view
,
486 target_phys_addr_t base
,
489 MemoryRegion
*subregion
;
491 target_phys_addr_t offset_in_region
;
499 tmp
= addrrange_make(base
, mr
->size
);
501 if (!addrrange_intersects(tmp
, clip
)) {
505 clip
= addrrange_intersection(tmp
, clip
);
508 base
-= mr
->alias
->addr
;
509 base
-= mr
->alias_offset
;
510 render_memory_region(view
, mr
->alias
, base
, clip
);
514 /* Render subregions in priority order. */
515 QTAILQ_FOREACH(subregion
, &mr
->subregions
, subregions_link
) {
516 render_memory_region(view
, subregion
, base
, clip
);
519 if (!mr
->terminates
) {
523 offset_in_region
= clip
.start
- base
;
527 /* Render the region itself into any gaps left by the current view. */
528 for (i
= 0; i
< view
->nr
&& remain
; ++i
) {
529 if (base
>= addrrange_end(view
->ranges
[i
].addr
)) {
532 if (base
< view
->ranges
[i
].addr
.start
) {
533 now
= MIN(remain
, view
->ranges
[i
].addr
.start
- base
);
535 fr
.offset_in_region
= offset_in_region
;
536 fr
.addr
= addrrange_make(base
, now
);
537 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
538 fr
.readable
= mr
->readable
;
539 flatview_insert(view
, i
, &fr
);
542 offset_in_region
+= now
;
545 if (base
== view
->ranges
[i
].addr
.start
) {
546 now
= MIN(remain
, view
->ranges
[i
].addr
.size
);
548 offset_in_region
+= now
;
554 fr
.offset_in_region
= offset_in_region
;
555 fr
.addr
= addrrange_make(base
, remain
);
556 fr
.dirty_log_mask
= mr
->dirty_log_mask
;
557 fr
.readable
= mr
->readable
;
558 flatview_insert(view
, i
, &fr
);
562 /* Render a memory topology into a list of disjoint absolute ranges. */
563 static FlatView
generate_memory_topology(MemoryRegion
*mr
)
567 flatview_init(&view
);
569 render_memory_region(&view
, mr
, 0, addrrange_make(0, INT64_MAX
));
570 flatview_simplify(&view
);
575 static void address_space_add_del_ioeventfds(AddressSpace
*as
,
576 MemoryRegionIoeventfd
*fds_new
,
578 MemoryRegionIoeventfd
*fds_old
,
583 /* Generate a symmetric difference of the old and new fd sets, adding
584 * and deleting as necessary.
588 while (iold
< fds_old_nb
|| inew
< fds_new_nb
) {
589 if (iold
< fds_old_nb
590 && (inew
== fds_new_nb
591 || memory_region_ioeventfd_before(fds_old
[iold
],
593 as
->ops
->ioeventfd_del(as
, &fds_old
[iold
]);
595 } else if (inew
< fds_new_nb
596 && (iold
== fds_old_nb
597 || memory_region_ioeventfd_before(fds_new
[inew
],
599 as
->ops
->ioeventfd_add(as
, &fds_new
[inew
]);
608 static void address_space_update_ioeventfds(AddressSpace
*as
)
611 unsigned ioeventfd_nb
= 0;
612 MemoryRegionIoeventfd
*ioeventfds
= NULL
;
616 FOR_EACH_FLAT_RANGE(fr
, &as
->current_map
) {
617 for (i
= 0; i
< fr
->mr
->ioeventfd_nb
; ++i
) {
618 tmp
= addrrange_shift(fr
->mr
->ioeventfds
[i
].addr
,
619 fr
->addr
.start
- fr
->offset_in_region
);
620 if (addrrange_intersects(fr
->addr
, tmp
)) {
622 ioeventfds
= g_realloc(ioeventfds
,
623 ioeventfd_nb
* sizeof(*ioeventfds
));
624 ioeventfds
[ioeventfd_nb
-1] = fr
->mr
->ioeventfds
[i
];
625 ioeventfds
[ioeventfd_nb
-1].addr
= tmp
;
630 address_space_add_del_ioeventfds(as
, ioeventfds
, ioeventfd_nb
,
631 as
->ioeventfds
, as
->ioeventfd_nb
);
633 g_free(as
->ioeventfds
);
634 as
->ioeventfds
= ioeventfds
;
635 as
->ioeventfd_nb
= ioeventfd_nb
;
638 static void address_space_update_topology_pass(AddressSpace
*as
,
644 FlatRange
*frold
, *frnew
;
646 /* Generate a symmetric difference of the old and new memory maps.
647 * Kill ranges in the old map, and instantiate ranges in the new map.
650 while (iold
< old_view
.nr
|| inew
< new_view
.nr
) {
651 if (iold
< old_view
.nr
) {
652 frold
= &old_view
.ranges
[iold
];
656 if (inew
< new_view
.nr
) {
657 frnew
= &new_view
.ranges
[inew
];
664 || frold
->addr
.start
< frnew
->addr
.start
665 || (frold
->addr
.start
== frnew
->addr
.start
666 && !flatrange_equal(frold
, frnew
)))) {
667 /* In old, but (not in new, or in new but attributes changed). */
670 as
->ops
->range_del(as
, frold
);
674 } else if (frold
&& frnew
&& flatrange_equal(frold
, frnew
)) {
675 /* In both (logging may have changed) */
678 if (frold
->dirty_log_mask
&& !frnew
->dirty_log_mask
) {
679 as
->ops
->log_stop(as
, frnew
);
680 } else if (frnew
->dirty_log_mask
&& !frold
->dirty_log_mask
) {
681 as
->ops
->log_start(as
, frnew
);
691 as
->ops
->range_add(as
, frnew
);
700 static void address_space_update_topology(AddressSpace
*as
)
702 FlatView old_view
= as
->current_map
;
703 FlatView new_view
= generate_memory_topology(as
->root
);
705 address_space_update_topology_pass(as
, old_view
, new_view
, false);
706 address_space_update_topology_pass(as
, old_view
, new_view
, true);
708 as
->current_map
= new_view
;
709 flatview_destroy(&old_view
);
710 address_space_update_ioeventfds(as
);
713 static void memory_region_update_topology(void)
715 if (memory_region_transaction_depth
) {
719 if (address_space_memory
.root
) {
720 address_space_update_topology(&address_space_memory
);
722 if (address_space_io
.root
) {
723 address_space_update_topology(&address_space_io
);
727 void memory_region_transaction_begin(void)
729 ++memory_region_transaction_depth
;
732 void memory_region_transaction_commit(void)
734 assert(memory_region_transaction_depth
);
735 --memory_region_transaction_depth
;
736 memory_region_update_topology();
739 static void memory_region_destructor_none(MemoryRegion
*mr
)
743 static void memory_region_destructor_ram(MemoryRegion
*mr
)
745 qemu_ram_free(mr
->ram_addr
);
748 static void memory_region_destructor_ram_from_ptr(MemoryRegion
*mr
)
750 qemu_ram_free_from_ptr(mr
->ram_addr
);
753 static void memory_region_destructor_iomem(MemoryRegion
*mr
)
755 cpu_unregister_io_memory(mr
->ram_addr
);
758 static void memory_region_destructor_rom_device(MemoryRegion
*mr
)
760 qemu_ram_free(mr
->ram_addr
& TARGET_PAGE_MASK
);
761 cpu_unregister_io_memory(mr
->ram_addr
& ~(TARGET_PAGE_MASK
| IO_MEM_ROMD
));
764 void memory_region_init(MemoryRegion
*mr
,
773 mr
->terminates
= false;
775 mr
->destructor
= memory_region_destructor_none
;
777 mr
->may_overlap
= false;
779 QTAILQ_INIT(&mr
->subregions
);
780 memset(&mr
->subregions_link
, 0, sizeof mr
->subregions_link
);
781 QTAILQ_INIT(&mr
->coalesced
);
782 mr
->name
= g_strdup(name
);
783 mr
->dirty_log_mask
= 0;
784 mr
->ioeventfd_nb
= 0;
785 mr
->ioeventfds
= NULL
;
788 static bool memory_region_access_valid(MemoryRegion
*mr
,
789 target_phys_addr_t addr
,
792 if (!mr
->ops
->valid
.unaligned
&& (addr
& (size
- 1))) {
796 /* Treat zero as compatibility all valid */
797 if (!mr
->ops
->valid
.max_access_size
) {
801 if (size
> mr
->ops
->valid
.max_access_size
802 || size
< mr
->ops
->valid
.min_access_size
) {
808 static uint32_t memory_region_read_thunk_n(void *_mr
,
809 target_phys_addr_t addr
,
812 MemoryRegion
*mr
= _mr
;
815 if (!memory_region_access_valid(mr
, addr
, size
)) {
816 return -1U; /* FIXME: better signalling */
819 if (!mr
->ops
->read
) {
820 return mr
->ops
->old_mmio
.read
[bitops_ffsl(size
)](mr
->opaque
, addr
);
823 /* FIXME: support unaligned access */
824 access_with_adjusted_size(addr
+ mr
->offset
, &data
, size
,
825 mr
->ops
->impl
.min_access_size
,
826 mr
->ops
->impl
.max_access_size
,
827 memory_region_read_accessor
, mr
);
832 static void memory_region_write_thunk_n(void *_mr
,
833 target_phys_addr_t addr
,
837 MemoryRegion
*mr
= _mr
;
839 if (!memory_region_access_valid(mr
, addr
, size
)) {
840 return; /* FIXME: better signalling */
843 if (!mr
->ops
->write
) {
844 mr
->ops
->old_mmio
.write
[bitops_ffsl(size
)](mr
->opaque
, addr
, data
);
848 /* FIXME: support unaligned access */
849 access_with_adjusted_size(addr
+ mr
->offset
, &data
, size
,
850 mr
->ops
->impl
.min_access_size
,
851 mr
->ops
->impl
.max_access_size
,
852 memory_region_write_accessor
, mr
);
855 static uint32_t memory_region_read_thunk_b(void *mr
, target_phys_addr_t addr
)
857 return memory_region_read_thunk_n(mr
, addr
, 1);
860 static uint32_t memory_region_read_thunk_w(void *mr
, target_phys_addr_t addr
)
862 return memory_region_read_thunk_n(mr
, addr
, 2);
865 static uint32_t memory_region_read_thunk_l(void *mr
, target_phys_addr_t addr
)
867 return memory_region_read_thunk_n(mr
, addr
, 4);
870 static void memory_region_write_thunk_b(void *mr
, target_phys_addr_t addr
,
873 memory_region_write_thunk_n(mr
, addr
, 1, data
);
876 static void memory_region_write_thunk_w(void *mr
, target_phys_addr_t addr
,
879 memory_region_write_thunk_n(mr
, addr
, 2, data
);
882 static void memory_region_write_thunk_l(void *mr
, target_phys_addr_t addr
,
885 memory_region_write_thunk_n(mr
, addr
, 4, data
);
888 static CPUReadMemoryFunc
* const memory_region_read_thunk
[] = {
889 memory_region_read_thunk_b
,
890 memory_region_read_thunk_w
,
891 memory_region_read_thunk_l
,
894 static CPUWriteMemoryFunc
* const memory_region_write_thunk
[] = {
895 memory_region_write_thunk_b
,
896 memory_region_write_thunk_w
,
897 memory_region_write_thunk_l
,
900 static void memory_region_prepare_ram_addr(MemoryRegion
*mr
)
902 if (mr
->backend_registered
) {
906 mr
->destructor
= memory_region_destructor_iomem
;
907 mr
->ram_addr
= cpu_register_io_memory(memory_region_read_thunk
,
908 memory_region_write_thunk
,
910 mr
->ops
->endianness
);
911 mr
->backend_registered
= true;
914 void memory_region_init_io(MemoryRegion
*mr
,
915 const MemoryRegionOps
*ops
,
920 memory_region_init(mr
, name
, size
);
923 mr
->terminates
= true;
924 mr
->backend_registered
= false;
927 void memory_region_init_ram(MemoryRegion
*mr
,
932 memory_region_init(mr
, name
, size
);
933 mr
->terminates
= true;
934 mr
->destructor
= memory_region_destructor_ram
;
935 mr
->ram_addr
= qemu_ram_alloc(dev
, name
, size
);
936 mr
->backend_registered
= true;
939 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
945 memory_region_init(mr
, name
, size
);
946 mr
->terminates
= true;
947 mr
->destructor
= memory_region_destructor_ram_from_ptr
;
948 mr
->ram_addr
= qemu_ram_alloc_from_ptr(dev
, name
, size
, ptr
);
949 mr
->backend_registered
= true;
952 void memory_region_init_alias(MemoryRegion
*mr
,
955 target_phys_addr_t offset
,
958 memory_region_init(mr
, name
, size
);
960 mr
->alias_offset
= offset
;
963 void memory_region_init_rom_device(MemoryRegion
*mr
,
964 const MemoryRegionOps
*ops
,
969 memory_region_init(mr
, name
, size
);
970 mr
->terminates
= true;
971 mr
->destructor
= memory_region_destructor_rom_device
;
972 mr
->ram_addr
= qemu_ram_alloc(dev
, name
, size
);
973 mr
->ram_addr
|= cpu_register_io_memory(memory_region_read_thunk
,
974 memory_region_write_thunk
,
976 mr
->ops
->endianness
);
977 mr
->ram_addr
|= IO_MEM_ROMD
;
978 mr
->backend_registered
= true;
981 void memory_region_destroy(MemoryRegion
*mr
)
983 assert(QTAILQ_EMPTY(&mr
->subregions
));
985 memory_region_clear_coalescing(mr
);
986 g_free((char *)mr
->name
);
987 g_free(mr
->ioeventfds
);
990 uint64_t memory_region_size(MemoryRegion
*mr
)
995 void memory_region_set_offset(MemoryRegion
*mr
, target_phys_addr_t offset
)
1000 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
)
1002 uint8_t mask
= 1 << client
;
1004 mr
->dirty_log_mask
= (mr
->dirty_log_mask
& ~mask
) | (log
* mask
);
1005 memory_region_update_topology();
1008 bool memory_region_get_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1011 assert(mr
->terminates
);
1012 return cpu_physical_memory_get_dirty(mr
->ram_addr
+ addr
, 1 << client
);
1015 void memory_region_set_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
)
1017 assert(mr
->terminates
);
1018 return cpu_physical_memory_set_dirty(mr
->ram_addr
+ addr
);
1021 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
)
1025 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1027 cpu_physical_sync_dirty_bitmap(fr
->addr
.start
,
1028 fr
->addr
.start
+ fr
->addr
.size
);
1033 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
)
1038 void memory_region_rom_device_set_readable(MemoryRegion
*mr
, bool readable
)
1040 if (mr
->readable
!= readable
) {
1041 mr
->readable
= readable
;
1042 memory_region_update_topology();
1046 void memory_region_reset_dirty(MemoryRegion
*mr
, target_phys_addr_t addr
,
1047 target_phys_addr_t size
, unsigned client
)
1049 assert(mr
->terminates
);
1050 cpu_physical_memory_reset_dirty(mr
->ram_addr
+ addr
,
1051 mr
->ram_addr
+ addr
+ size
,
1055 void *memory_region_get_ram_ptr(MemoryRegion
*mr
)
1058 return memory_region_get_ram_ptr(mr
->alias
) + mr
->alias_offset
;
1061 assert(mr
->terminates
);
1063 return qemu_get_ram_ptr(mr
->ram_addr
);
1066 static void memory_region_update_coalesced_range(MemoryRegion
*mr
)
1069 CoalescedMemoryRange
*cmr
;
1072 FOR_EACH_FLAT_RANGE(fr
, &address_space_memory
.current_map
) {
1074 qemu_unregister_coalesced_mmio(fr
->addr
.start
, fr
->addr
.size
);
1075 QTAILQ_FOREACH(cmr
, &mr
->coalesced
, link
) {
1076 tmp
= addrrange_shift(cmr
->addr
,
1077 fr
->addr
.start
- fr
->offset_in_region
);
1078 if (!addrrange_intersects(tmp
, fr
->addr
)) {
1081 tmp
= addrrange_intersection(tmp
, fr
->addr
);
1082 qemu_register_coalesced_mmio(tmp
.start
, tmp
.size
);
1088 void memory_region_set_coalescing(MemoryRegion
*mr
)
1090 memory_region_clear_coalescing(mr
);
1091 memory_region_add_coalescing(mr
, 0, mr
->size
);
1094 void memory_region_add_coalescing(MemoryRegion
*mr
,
1095 target_phys_addr_t offset
,
1098 CoalescedMemoryRange
*cmr
= g_malloc(sizeof(*cmr
));
1100 cmr
->addr
= addrrange_make(offset
, size
);
1101 QTAILQ_INSERT_TAIL(&mr
->coalesced
, cmr
, link
);
1102 memory_region_update_coalesced_range(mr
);
1105 void memory_region_clear_coalescing(MemoryRegion
*mr
)
1107 CoalescedMemoryRange
*cmr
;
1109 while (!QTAILQ_EMPTY(&mr
->coalesced
)) {
1110 cmr
= QTAILQ_FIRST(&mr
->coalesced
);
1111 QTAILQ_REMOVE(&mr
->coalesced
, cmr
, link
);
1114 memory_region_update_coalesced_range(mr
);
1117 void memory_region_add_eventfd(MemoryRegion
*mr
,
1118 target_phys_addr_t addr
,
1124 MemoryRegionIoeventfd mrfd
= {
1127 .match_data
= match_data
,
1133 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1134 if (memory_region_ioeventfd_before(mrfd
, mr
->ioeventfds
[i
])) {
1139 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1140 sizeof(*mr
->ioeventfds
) * mr
->ioeventfd_nb
);
1141 memmove(&mr
->ioeventfds
[i
+1], &mr
->ioeventfds
[i
],
1142 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
-1 - i
));
1143 mr
->ioeventfds
[i
] = mrfd
;
1144 memory_region_update_topology();
1147 void memory_region_del_eventfd(MemoryRegion
*mr
,
1148 target_phys_addr_t addr
,
1154 MemoryRegionIoeventfd mrfd
= {
1157 .match_data
= match_data
,
1163 for (i
= 0; i
< mr
->ioeventfd_nb
; ++i
) {
1164 if (memory_region_ioeventfd_equal(mrfd
, mr
->ioeventfds
[i
])) {
1168 assert(i
!= mr
->ioeventfd_nb
);
1169 memmove(&mr
->ioeventfds
[i
], &mr
->ioeventfds
[i
+1],
1170 sizeof(*mr
->ioeventfds
) * (mr
->ioeventfd_nb
- (i
+1)));
1172 mr
->ioeventfds
= g_realloc(mr
->ioeventfds
,
1173 sizeof(*mr
->ioeventfds
)*mr
->ioeventfd_nb
+ 1);
1174 memory_region_update_topology();
1177 static void memory_region_add_subregion_common(MemoryRegion
*mr
,
1178 target_phys_addr_t offset
,
1179 MemoryRegion
*subregion
)
1181 MemoryRegion
*other
;
1183 assert(!subregion
->parent
);
1184 subregion
->parent
= mr
;
1185 subregion
->addr
= offset
;
1186 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1187 if (subregion
->may_overlap
|| other
->may_overlap
) {
1190 if (offset
>= other
->offset
+ other
->size
1191 || offset
+ subregion
->size
<= other
->offset
) {
1194 printf("warning: subregion collision %llx/%llx vs %llx/%llx\n",
1195 (unsigned long long)offset
,
1196 (unsigned long long)subregion
->size
,
1197 (unsigned long long)other
->offset
,
1198 (unsigned long long)other
->size
);
1200 QTAILQ_FOREACH(other
, &mr
->subregions
, subregions_link
) {
1201 if (subregion
->priority
>= other
->priority
) {
1202 QTAILQ_INSERT_BEFORE(other
, subregion
, subregions_link
);
1206 QTAILQ_INSERT_TAIL(&mr
->subregions
, subregion
, subregions_link
);
1208 memory_region_update_topology();
1212 void memory_region_add_subregion(MemoryRegion
*mr
,
1213 target_phys_addr_t offset
,
1214 MemoryRegion
*subregion
)
1216 subregion
->may_overlap
= false;
1217 subregion
->priority
= 0;
1218 memory_region_add_subregion_common(mr
, offset
, subregion
);
1221 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1222 target_phys_addr_t offset
,
1223 MemoryRegion
*subregion
,
1226 subregion
->may_overlap
= true;
1227 subregion
->priority
= priority
;
1228 memory_region_add_subregion_common(mr
, offset
, subregion
);
1231 void memory_region_del_subregion(MemoryRegion
*mr
,
1232 MemoryRegion
*subregion
)
1234 assert(subregion
->parent
== mr
);
1235 subregion
->parent
= NULL
;
1236 QTAILQ_REMOVE(&mr
->subregions
, subregion
, subregions_link
);
1237 memory_region_update_topology();
1240 void set_system_memory_map(MemoryRegion
*mr
)
1242 address_space_memory
.root
= mr
;
1243 memory_region_update_topology();
1246 void set_system_io_map(MemoryRegion
*mr
)
1248 address_space_io
.root
= mr
;
1249 memory_region_update_topology();