4 * Copyright Fujitsu, Corp. 2011, 2012
7 * Wen Congyang <wency@cn.fujitsu.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
17 #include "exec/cpu-all.h"
18 #include "sysemu/memory_mapping.h"
19 #include "exec/memory.h"
20 #include "exec/address-spaces.h"
22 //#define DEBUG_GUEST_PHYS_REGION_ADD
24 static void memory_mapping_list_add_mapping_sorted(MemoryMappingList
*list
,
25 MemoryMapping
*mapping
)
29 QTAILQ_FOREACH(p
, &list
->head
, next
) {
30 if (p
->phys_addr
>= mapping
->phys_addr
) {
31 QTAILQ_INSERT_BEFORE(p
, mapping
, next
);
35 QTAILQ_INSERT_TAIL(&list
->head
, mapping
, next
);
38 static void create_new_memory_mapping(MemoryMappingList
*list
,
43 MemoryMapping
*memory_mapping
;
45 memory_mapping
= g_malloc(sizeof(MemoryMapping
));
46 memory_mapping
->phys_addr
= phys_addr
;
47 memory_mapping
->virt_addr
= virt_addr
;
48 memory_mapping
->length
= length
;
49 list
->last_mapping
= memory_mapping
;
51 memory_mapping_list_add_mapping_sorted(list
, memory_mapping
);
54 static inline bool mapping_contiguous(MemoryMapping
*map
,
58 return phys_addr
== map
->phys_addr
+ map
->length
&&
59 virt_addr
== map
->virt_addr
+ map
->length
;
63 * [map->phys_addr, map->phys_addr + map->length) and
64 * [phys_addr, phys_addr + length) have intersection?
66 static inline bool mapping_have_same_region(MemoryMapping
*map
,
70 return !(phys_addr
+ length
< map
->phys_addr
||
71 phys_addr
>= map
->phys_addr
+ map
->length
);
75 * [map->phys_addr, map->phys_addr + map->length) and
76 * [phys_addr, phys_addr + length) have intersection. The virtual address in the
77 * intersection are the same?
79 static inline bool mapping_conflict(MemoryMapping
*map
,
83 return virt_addr
- map
->virt_addr
!= phys_addr
- map
->phys_addr
;
87 * [map->virt_addr, map->virt_addr + map->length) and
88 * [virt_addr, virt_addr + length) have intersection. And the physical address
89 * in the intersection are the same.
91 static inline void mapping_merge(MemoryMapping
*map
,
95 if (virt_addr
< map
->virt_addr
) {
96 map
->length
+= map
->virt_addr
- virt_addr
;
97 map
->virt_addr
= virt_addr
;
100 if ((virt_addr
+ length
) >
101 (map
->virt_addr
+ map
->length
)) {
102 map
->length
= virt_addr
+ length
- map
->virt_addr
;
106 void memory_mapping_list_add_merge_sorted(MemoryMappingList
*list
,
111 MemoryMapping
*memory_mapping
, *last_mapping
;
113 if (QTAILQ_EMPTY(&list
->head
)) {
114 create_new_memory_mapping(list
, phys_addr
, virt_addr
, length
);
118 last_mapping
= list
->last_mapping
;
120 if (mapping_contiguous(last_mapping
, phys_addr
, virt_addr
)) {
121 last_mapping
->length
+= length
;
126 QTAILQ_FOREACH(memory_mapping
, &list
->head
, next
) {
127 if (mapping_contiguous(memory_mapping
, phys_addr
, virt_addr
)) {
128 memory_mapping
->length
+= length
;
129 list
->last_mapping
= memory_mapping
;
133 if (phys_addr
+ length
< memory_mapping
->phys_addr
) {
134 /* create a new region before memory_mapping */
138 if (mapping_have_same_region(memory_mapping
, phys_addr
, length
)) {
139 if (mapping_conflict(memory_mapping
, phys_addr
, virt_addr
)) {
143 /* merge this region into memory_mapping */
144 mapping_merge(memory_mapping
, virt_addr
, length
);
145 list
->last_mapping
= memory_mapping
;
150 /* this region can not be merged into any existed memory mapping. */
151 create_new_memory_mapping(list
, phys_addr
, virt_addr
, length
);
154 void memory_mapping_list_free(MemoryMappingList
*list
)
156 MemoryMapping
*p
, *q
;
158 QTAILQ_FOREACH_SAFE(p
, &list
->head
, next
, q
) {
159 QTAILQ_REMOVE(&list
->head
, p
, next
);
164 list
->last_mapping
= NULL
;
167 void memory_mapping_list_init(MemoryMappingList
*list
)
170 list
->last_mapping
= NULL
;
171 QTAILQ_INIT(&list
->head
);
174 void guest_phys_blocks_free(GuestPhysBlockList
*list
)
176 GuestPhysBlock
*p
, *q
;
178 QTAILQ_FOREACH_SAFE(p
, &list
->head
, next
, q
) {
179 QTAILQ_REMOVE(&list
->head
, p
, next
);
185 void guest_phys_blocks_init(GuestPhysBlockList
*list
)
188 QTAILQ_INIT(&list
->head
);
191 typedef struct GuestPhysListener
{
192 GuestPhysBlockList
*list
;
193 MemoryListener listener
;
196 static void guest_phys_blocks_region_add(MemoryListener
*listener
,
197 MemoryRegionSection
*section
)
199 GuestPhysListener
*g
;
200 uint64_t section_size
;
201 hwaddr target_start
, target_end
;
203 GuestPhysBlock
*predecessor
;
205 /* we only care about RAM */
206 if (!memory_region_is_ram(section
->mr
)) {
210 g
= container_of(listener
, GuestPhysListener
, listener
);
211 section_size
= int128_get64(section
->size
);
212 target_start
= section
->offset_within_address_space
;
213 target_end
= target_start
+ section_size
;
214 host_addr
= memory_region_get_ram_ptr(section
->mr
) +
215 section
->offset_within_region
;
218 /* find continuity in guest physical address space */
219 if (!QTAILQ_EMPTY(&g
->list
->head
)) {
220 hwaddr predecessor_size
;
222 predecessor
= QTAILQ_LAST(&g
->list
->head
, GuestPhysBlockHead
);
223 predecessor_size
= predecessor
->target_end
- predecessor
->target_start
;
225 /* the memory API guarantees monotonically increasing traversal */
226 g_assert(predecessor
->target_end
<= target_start
);
228 /* we want continuity in both guest-physical and host-virtual memory */
229 if (predecessor
->target_end
< target_start
||
230 predecessor
->host_addr
+ predecessor_size
!= host_addr
) {
235 if (predecessor
== NULL
) {
236 /* isolated mapping, allocate it and add it to the list */
237 GuestPhysBlock
*block
= g_malloc0(sizeof *block
);
239 block
->target_start
= target_start
;
240 block
->target_end
= target_end
;
241 block
->host_addr
= host_addr
;
243 QTAILQ_INSERT_TAIL(&g
->list
->head
, block
, next
);
246 /* expand predecessor until @target_end; predecessor's start doesn't
249 predecessor
->target_end
= target_end
;
252 #ifdef DEBUG_GUEST_PHYS_REGION_ADD
253 fprintf(stderr
, "%s: target_start=" TARGET_FMT_plx
" target_end="
254 TARGET_FMT_plx
": %s (count: %u)\n", __FUNCTION__
, target_start
,
255 target_end
, predecessor
? "joined" : "added", g
->list
->num
);
259 void guest_phys_blocks_append(GuestPhysBlockList
*list
)
261 GuestPhysListener g
= { 0 };
264 g
.listener
.region_add
= &guest_phys_blocks_region_add
;
265 memory_listener_register(&g
.listener
, &address_space_memory
);
266 memory_listener_unregister(&g
.listener
);
269 static CPUState
*find_paging_enabled_cpu(CPUState
*start_cpu
)
273 for (cpu
= start_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
274 if (cpu_paging_enabled(cpu
)) {
282 void qemu_get_guest_memory_mapping(MemoryMappingList
*list
,
283 const GuestPhysBlockList
*guest_phys_blocks
,
286 CPUState
*cpu
, *first_paging_enabled_cpu
;
287 GuestPhysBlock
*block
;
288 ram_addr_t offset
, length
;
290 first_paging_enabled_cpu
= find_paging_enabled_cpu(first_cpu
);
291 if (first_paging_enabled_cpu
) {
292 for (cpu
= first_paging_enabled_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
294 cpu_get_memory_mapping(cpu
, list
, &err
);
296 error_propagate(errp
, err
);
304 * If the guest doesn't use paging, the virtual address is equal to physical
307 QTAILQ_FOREACH(block
, &guest_phys_blocks
->head
, next
) {
308 offset
= block
->target_start
;
309 length
= block
->target_end
- block
->target_start
;
310 create_new_memory_mapping(list
, offset
, offset
, length
);
314 void qemu_get_guest_simple_memory_mapping(MemoryMappingList
*list
,
315 const GuestPhysBlockList
*guest_phys_blocks
)
317 GuestPhysBlock
*block
;
319 QTAILQ_FOREACH(block
, &guest_phys_blocks
->head
, next
) {
320 create_new_memory_mapping(list
, block
->target_start
, 0,
321 block
->target_end
- block
->target_start
);
325 void memory_mapping_filter(MemoryMappingList
*list
, int64_t begin
,
328 MemoryMapping
*cur
, *next
;
330 QTAILQ_FOREACH_SAFE(cur
, &list
->head
, next
, next
) {
331 if (cur
->phys_addr
>= begin
+ length
||
332 cur
->phys_addr
+ cur
->length
<= begin
) {
333 QTAILQ_REMOVE(&list
->head
, cur
, next
);
338 if (cur
->phys_addr
< begin
) {
339 cur
->length
-= begin
- cur
->phys_addr
;
340 if (cur
->virt_addr
) {
341 cur
->virt_addr
+= begin
- cur
->phys_addr
;
343 cur
->phys_addr
= begin
;
346 if (cur
->phys_addr
+ cur
->length
> begin
+ length
) {
347 cur
->length
-= cur
->phys_addr
+ cur
->length
- begin
- length
;