]> git.proxmox.com Git - mirror_qemu.git/blob - memory_mapping.c
all: Clean up includes
[mirror_qemu.git] / memory_mapping.c
1 /*
2 * QEMU memory mapping
3 *
4 * Copyright Fujitsu, Corp. 2011, 2012
5 *
6 * Authors:
7 * Wen Congyang <wency@cn.fujitsu.com>
8 *
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.
11 *
12 */
13
14 #include "qemu/osdep.h"
15 #include <glib.h>
16
17 #include "qemu-common.h"
18 #include "cpu.h"
19 #include "sysemu/memory_mapping.h"
20 #include "exec/memory.h"
21 #include "exec/address-spaces.h"
22
23 //#define DEBUG_GUEST_PHYS_REGION_ADD
24
25 static void memory_mapping_list_add_mapping_sorted(MemoryMappingList *list,
26 MemoryMapping *mapping)
27 {
28 MemoryMapping *p;
29
30 QTAILQ_FOREACH(p, &list->head, next) {
31 if (p->phys_addr >= mapping->phys_addr) {
32 QTAILQ_INSERT_BEFORE(p, mapping, next);
33 return;
34 }
35 }
36 QTAILQ_INSERT_TAIL(&list->head, mapping, next);
37 }
38
39 static void create_new_memory_mapping(MemoryMappingList *list,
40 hwaddr phys_addr,
41 hwaddr virt_addr,
42 ram_addr_t length)
43 {
44 MemoryMapping *memory_mapping;
45
46 memory_mapping = g_malloc(sizeof(MemoryMapping));
47 memory_mapping->phys_addr = phys_addr;
48 memory_mapping->virt_addr = virt_addr;
49 memory_mapping->length = length;
50 list->last_mapping = memory_mapping;
51 list->num++;
52 memory_mapping_list_add_mapping_sorted(list, memory_mapping);
53 }
54
55 static inline bool mapping_contiguous(MemoryMapping *map,
56 hwaddr phys_addr,
57 hwaddr virt_addr)
58 {
59 return phys_addr == map->phys_addr + map->length &&
60 virt_addr == map->virt_addr + map->length;
61 }
62
63 /*
64 * [map->phys_addr, map->phys_addr + map->length) and
65 * [phys_addr, phys_addr + length) have intersection?
66 */
67 static inline bool mapping_have_same_region(MemoryMapping *map,
68 hwaddr phys_addr,
69 ram_addr_t length)
70 {
71 return !(phys_addr + length < map->phys_addr ||
72 phys_addr >= map->phys_addr + map->length);
73 }
74
75 /*
76 * [map->phys_addr, map->phys_addr + map->length) and
77 * [phys_addr, phys_addr + length) have intersection. The virtual address in the
78 * intersection are the same?
79 */
80 static inline bool mapping_conflict(MemoryMapping *map,
81 hwaddr phys_addr,
82 hwaddr virt_addr)
83 {
84 return virt_addr - map->virt_addr != phys_addr - map->phys_addr;
85 }
86
87 /*
88 * [map->virt_addr, map->virt_addr + map->length) and
89 * [virt_addr, virt_addr + length) have intersection. And the physical address
90 * in the intersection are the same.
91 */
92 static inline void mapping_merge(MemoryMapping *map,
93 hwaddr virt_addr,
94 ram_addr_t length)
95 {
96 if (virt_addr < map->virt_addr) {
97 map->length += map->virt_addr - virt_addr;
98 map->virt_addr = virt_addr;
99 }
100
101 if ((virt_addr + length) >
102 (map->virt_addr + map->length)) {
103 map->length = virt_addr + length - map->virt_addr;
104 }
105 }
106
107 void memory_mapping_list_add_merge_sorted(MemoryMappingList *list,
108 hwaddr phys_addr,
109 hwaddr virt_addr,
110 ram_addr_t length)
111 {
112 MemoryMapping *memory_mapping, *last_mapping;
113
114 if (QTAILQ_EMPTY(&list->head)) {
115 create_new_memory_mapping(list, phys_addr, virt_addr, length);
116 return;
117 }
118
119 last_mapping = list->last_mapping;
120 if (last_mapping) {
121 if (mapping_contiguous(last_mapping, phys_addr, virt_addr)) {
122 last_mapping->length += length;
123 return;
124 }
125 }
126
127 QTAILQ_FOREACH(memory_mapping, &list->head, next) {
128 if (mapping_contiguous(memory_mapping, phys_addr, virt_addr)) {
129 memory_mapping->length += length;
130 list->last_mapping = memory_mapping;
131 return;
132 }
133
134 if (phys_addr + length < memory_mapping->phys_addr) {
135 /* create a new region before memory_mapping */
136 break;
137 }
138
139 if (mapping_have_same_region(memory_mapping, phys_addr, length)) {
140 if (mapping_conflict(memory_mapping, phys_addr, virt_addr)) {
141 continue;
142 }
143
144 /* merge this region into memory_mapping */
145 mapping_merge(memory_mapping, virt_addr, length);
146 list->last_mapping = memory_mapping;
147 return;
148 }
149 }
150
151 /* this region can not be merged into any existed memory mapping. */
152 create_new_memory_mapping(list, phys_addr, virt_addr, length);
153 }
154
155 void memory_mapping_list_free(MemoryMappingList *list)
156 {
157 MemoryMapping *p, *q;
158
159 QTAILQ_FOREACH_SAFE(p, &list->head, next, q) {
160 QTAILQ_REMOVE(&list->head, p, next);
161 g_free(p);
162 }
163
164 list->num = 0;
165 list->last_mapping = NULL;
166 }
167
168 void memory_mapping_list_init(MemoryMappingList *list)
169 {
170 list->num = 0;
171 list->last_mapping = NULL;
172 QTAILQ_INIT(&list->head);
173 }
174
175 void guest_phys_blocks_free(GuestPhysBlockList *list)
176 {
177 GuestPhysBlock *p, *q;
178
179 QTAILQ_FOREACH_SAFE(p, &list->head, next, q) {
180 QTAILQ_REMOVE(&list->head, p, next);
181 g_free(p);
182 }
183 list->num = 0;
184 }
185
186 void guest_phys_blocks_init(GuestPhysBlockList *list)
187 {
188 list->num = 0;
189 QTAILQ_INIT(&list->head);
190 }
191
192 typedef struct GuestPhysListener {
193 GuestPhysBlockList *list;
194 MemoryListener listener;
195 } GuestPhysListener;
196
197 static void guest_phys_blocks_region_add(MemoryListener *listener,
198 MemoryRegionSection *section)
199 {
200 GuestPhysListener *g;
201 uint64_t section_size;
202 hwaddr target_start, target_end;
203 uint8_t *host_addr;
204 GuestPhysBlock *predecessor;
205
206 /* we only care about RAM */
207 if (!memory_region_is_ram(section->mr) ||
208 memory_region_is_skip_dump(section->mr)) {
209 return;
210 }
211
212 g = container_of(listener, GuestPhysListener, listener);
213 section_size = int128_get64(section->size);
214 target_start = section->offset_within_address_space;
215 target_end = target_start + section_size;
216 host_addr = memory_region_get_ram_ptr(section->mr) +
217 section->offset_within_region;
218 predecessor = NULL;
219
220 /* find continuity in guest physical address space */
221 if (!QTAILQ_EMPTY(&g->list->head)) {
222 hwaddr predecessor_size;
223
224 predecessor = QTAILQ_LAST(&g->list->head, GuestPhysBlockHead);
225 predecessor_size = predecessor->target_end - predecessor->target_start;
226
227 /* the memory API guarantees monotonically increasing traversal */
228 g_assert(predecessor->target_end <= target_start);
229
230 /* we want continuity in both guest-physical and host-virtual memory */
231 if (predecessor->target_end < target_start ||
232 predecessor->host_addr + predecessor_size != host_addr) {
233 predecessor = NULL;
234 }
235 }
236
237 if (predecessor == NULL) {
238 /* isolated mapping, allocate it and add it to the list */
239 GuestPhysBlock *block = g_malloc0(sizeof *block);
240
241 block->target_start = target_start;
242 block->target_end = target_end;
243 block->host_addr = host_addr;
244
245 QTAILQ_INSERT_TAIL(&g->list->head, block, next);
246 ++g->list->num;
247 } else {
248 /* expand predecessor until @target_end; predecessor's start doesn't
249 * change
250 */
251 predecessor->target_end = target_end;
252 }
253
254 #ifdef DEBUG_GUEST_PHYS_REGION_ADD
255 fprintf(stderr, "%s: target_start=" TARGET_FMT_plx " target_end="
256 TARGET_FMT_plx ": %s (count: %u)\n", __FUNCTION__, target_start,
257 target_end, predecessor ? "joined" : "added", g->list->num);
258 #endif
259 }
260
261 void guest_phys_blocks_append(GuestPhysBlockList *list)
262 {
263 GuestPhysListener g = { 0 };
264
265 g.list = list;
266 g.listener.region_add = &guest_phys_blocks_region_add;
267 memory_listener_register(&g.listener, &address_space_memory);
268 memory_listener_unregister(&g.listener);
269 }
270
271 static CPUState *find_paging_enabled_cpu(CPUState *start_cpu)
272 {
273 CPUState *cpu;
274
275 CPU_FOREACH(cpu) {
276 if (cpu_paging_enabled(cpu)) {
277 return cpu;
278 }
279 }
280
281 return NULL;
282 }
283
284 void qemu_get_guest_memory_mapping(MemoryMappingList *list,
285 const GuestPhysBlockList *guest_phys_blocks,
286 Error **errp)
287 {
288 CPUState *cpu, *first_paging_enabled_cpu;
289 GuestPhysBlock *block;
290 ram_addr_t offset, length;
291
292 first_paging_enabled_cpu = find_paging_enabled_cpu(first_cpu);
293 if (first_paging_enabled_cpu) {
294 for (cpu = first_paging_enabled_cpu; cpu != NULL;
295 cpu = CPU_NEXT(cpu)) {
296 Error *err = NULL;
297 cpu_get_memory_mapping(cpu, list, &err);
298 if (err) {
299 error_propagate(errp, err);
300 return;
301 }
302 }
303 return;
304 }
305
306 /*
307 * If the guest doesn't use paging, the virtual address is equal to physical
308 * address.
309 */
310 QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
311 offset = block->target_start;
312 length = block->target_end - block->target_start;
313 create_new_memory_mapping(list, offset, offset, length);
314 }
315 }
316
317 void qemu_get_guest_simple_memory_mapping(MemoryMappingList *list,
318 const GuestPhysBlockList *guest_phys_blocks)
319 {
320 GuestPhysBlock *block;
321
322 QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
323 create_new_memory_mapping(list, block->target_start, 0,
324 block->target_end - block->target_start);
325 }
326 }
327
328 void memory_mapping_filter(MemoryMappingList *list, int64_t begin,
329 int64_t length)
330 {
331 MemoryMapping *cur, *next;
332
333 QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) {
334 if (cur->phys_addr >= begin + length ||
335 cur->phys_addr + cur->length <= begin) {
336 QTAILQ_REMOVE(&list->head, cur, next);
337 list->num--;
338 continue;
339 }
340
341 if (cur->phys_addr < begin) {
342 cur->length -= begin - cur->phys_addr;
343 if (cur->virt_addr) {
344 cur->virt_addr += begin - cur->phys_addr;
345 }
346 cur->phys_addr = begin;
347 }
348
349 if (cur->phys_addr + cur->length > begin + length) {
350 cur->length -= cur->phys_addr + cur->length - begin - length;
351 }
352 }
353 }