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.
15 #include "exec/cpu-all.h"
16 #include "sysemu/memory_mapping.h"
18 static void memory_mapping_list_add_mapping_sorted(MemoryMappingList
*list
,
19 MemoryMapping
*mapping
)
23 QTAILQ_FOREACH(p
, &list
->head
, next
) {
24 if (p
->phys_addr
>= mapping
->phys_addr
) {
25 QTAILQ_INSERT_BEFORE(p
, mapping
, next
);
29 QTAILQ_INSERT_TAIL(&list
->head
, mapping
, next
);
32 static void create_new_memory_mapping(MemoryMappingList
*list
,
37 MemoryMapping
*memory_mapping
;
39 memory_mapping
= g_malloc(sizeof(MemoryMapping
));
40 memory_mapping
->phys_addr
= phys_addr
;
41 memory_mapping
->virt_addr
= virt_addr
;
42 memory_mapping
->length
= length
;
43 list
->last_mapping
= memory_mapping
;
45 memory_mapping_list_add_mapping_sorted(list
, memory_mapping
);
48 static inline bool mapping_contiguous(MemoryMapping
*map
,
52 return phys_addr
== map
->phys_addr
+ map
->length
&&
53 virt_addr
== map
->virt_addr
+ map
->length
;
57 * [map->phys_addr, map->phys_addr + map->length) and
58 * [phys_addr, phys_addr + length) have intersection?
60 static inline bool mapping_have_same_region(MemoryMapping
*map
,
64 return !(phys_addr
+ length
< map
->phys_addr
||
65 phys_addr
>= map
->phys_addr
+ map
->length
);
69 * [map->phys_addr, map->phys_addr + map->length) and
70 * [phys_addr, phys_addr + length) have intersection. The virtual address in the
71 * intersection are the same?
73 static inline bool mapping_conflict(MemoryMapping
*map
,
77 return virt_addr
- map
->virt_addr
!= phys_addr
- map
->phys_addr
;
81 * [map->virt_addr, map->virt_addr + map->length) and
82 * [virt_addr, virt_addr + length) have intersection. And the physical address
83 * in the intersection are the same.
85 static inline void mapping_merge(MemoryMapping
*map
,
89 if (virt_addr
< map
->virt_addr
) {
90 map
->length
+= map
->virt_addr
- virt_addr
;
91 map
->virt_addr
= virt_addr
;
94 if ((virt_addr
+ length
) >
95 (map
->virt_addr
+ map
->length
)) {
96 map
->length
= virt_addr
+ length
- map
->virt_addr
;
100 void memory_mapping_list_add_merge_sorted(MemoryMappingList
*list
,
105 MemoryMapping
*memory_mapping
, *last_mapping
;
107 if (QTAILQ_EMPTY(&list
->head
)) {
108 create_new_memory_mapping(list
, phys_addr
, virt_addr
, length
);
112 last_mapping
= list
->last_mapping
;
114 if (mapping_contiguous(last_mapping
, phys_addr
, virt_addr
)) {
115 last_mapping
->length
+= length
;
120 QTAILQ_FOREACH(memory_mapping
, &list
->head
, next
) {
121 if (mapping_contiguous(memory_mapping
, phys_addr
, virt_addr
)) {
122 memory_mapping
->length
+= length
;
123 list
->last_mapping
= memory_mapping
;
127 if (phys_addr
+ length
< memory_mapping
->phys_addr
) {
128 /* create a new region before memory_mapping */
132 if (mapping_have_same_region(memory_mapping
, phys_addr
, length
)) {
133 if (mapping_conflict(memory_mapping
, phys_addr
, virt_addr
)) {
137 /* merge this region into memory_mapping */
138 mapping_merge(memory_mapping
, virt_addr
, length
);
139 list
->last_mapping
= memory_mapping
;
144 /* this region can not be merged into any existed memory mapping. */
145 create_new_memory_mapping(list
, phys_addr
, virt_addr
, length
);
148 void memory_mapping_list_free(MemoryMappingList
*list
)
150 MemoryMapping
*p
, *q
;
152 QTAILQ_FOREACH_SAFE(p
, &list
->head
, next
, q
) {
153 QTAILQ_REMOVE(&list
->head
, p
, next
);
158 list
->last_mapping
= NULL
;
161 void memory_mapping_list_init(MemoryMappingList
*list
)
164 list
->last_mapping
= NULL
;
165 QTAILQ_INIT(&list
->head
);
168 static CPUArchState
*find_paging_enabled_cpu(CPUArchState
*start_cpu
)
172 for (env
= start_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
173 if (cpu_paging_enabled(env
)) {
181 int qemu_get_guest_memory_mapping(MemoryMappingList
*list
)
183 CPUArchState
*env
, *first_paging_enabled_cpu
;
185 ram_addr_t offset
, length
;
188 first_paging_enabled_cpu
= find_paging_enabled_cpu(first_cpu
);
189 if (first_paging_enabled_cpu
) {
190 for (env
= first_paging_enabled_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
191 ret
= cpu_get_memory_mapping(list
, env
);
200 * If the guest doesn't use paging, the virtual address is equal to physical
203 QTAILQ_FOREACH(block
, &ram_list
.blocks
, next
) {
204 offset
= block
->offset
;
205 length
= block
->length
;
206 create_new_memory_mapping(list
, offset
, offset
, length
);
212 void qemu_get_guest_simple_memory_mapping(MemoryMappingList
*list
)
216 QTAILQ_FOREACH(block
, &ram_list
.blocks
, next
) {
217 create_new_memory_mapping(list
, block
->offset
, 0, block
->length
);
221 void memory_mapping_filter(MemoryMappingList
*list
, int64_t begin
,
224 MemoryMapping
*cur
, *next
;
226 QTAILQ_FOREACH_SAFE(cur
, &list
->head
, next
, next
) {
227 if (cur
->phys_addr
>= begin
+ length
||
228 cur
->phys_addr
+ cur
->length
<= begin
) {
229 QTAILQ_REMOVE(&list
->head
, cur
, next
);
234 if (cur
->phys_addr
< begin
) {
235 cur
->length
-= begin
- cur
->phys_addr
;
236 if (cur
->virt_addr
) {
237 cur
->virt_addr
+= begin
- cur
->phys_addr
;
239 cur
->phys_addr
= begin
;
242 if (cur
->phys_addr
+ cur
->length
> begin
+ length
) {
243 cur
->length
-= cur
->phys_addr
+ cur
->length
- begin
- length
;