xref: /openbmc/qemu/system/memory_mapping.c (revision 13c59a0e)
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 "qemu/range.h"
16 #include "qapi/error.h"
17 
18 #include "sysemu/memory_mapping.h"
19 #include "exec/memory.h"
20 #include "exec/address-spaces.h"
21 #include "hw/core/cpu.h"
22 
23 //#define DEBUG_GUEST_PHYS_REGION_ADD
24 
memory_mapping_list_add_mapping_sorted(MemoryMappingList * list,MemoryMapping * mapping)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 
create_new_memory_mapping(MemoryMappingList * list,hwaddr phys_addr,hwaddr virt_addr,ram_addr_t length)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_new(MemoryMapping, 1);
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 
mapping_contiguous(MemoryMapping * map,hwaddr phys_addr,hwaddr virt_addr)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  */
mapping_have_same_region(MemoryMapping * map,hwaddr phys_addr,ram_addr_t length)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  */
mapping_conflict(MemoryMapping * map,hwaddr phys_addr,hwaddr virt_addr)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  */
mapping_merge(MemoryMapping * map,hwaddr virt_addr,ram_addr_t length)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 
memory_mapping_list_add_merge_sorted(MemoryMappingList * list,hwaddr phys_addr,hwaddr virt_addr,ram_addr_t length)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 
memory_mapping_list_free(MemoryMappingList * list)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 
memory_mapping_list_init(MemoryMappingList * list)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 
guest_phys_blocks_free(GuestPhysBlockList * list)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         memory_region_unref(p->mr);
182         g_free(p);
183     }
184     list->num = 0;
185 }
186 
guest_phys_blocks_init(GuestPhysBlockList * list)187 void guest_phys_blocks_init(GuestPhysBlockList *list)
188 {
189     list->num = 0;
190     QTAILQ_INIT(&list->head);
191 }
192 
193 typedef struct GuestPhysListener {
194     GuestPhysBlockList *list;
195     MemoryListener listener;
196 } GuestPhysListener;
197 
guest_phys_block_add_section(GuestPhysListener * g,MemoryRegionSection * section)198 static void guest_phys_block_add_section(GuestPhysListener *g,
199                                          MemoryRegionSection *section)
200 {
201     const hwaddr target_start = section->offset_within_address_space;
202     const hwaddr target_end = target_start + int128_get64(section->size);
203     uint8_t *host_addr = memory_region_get_ram_ptr(section->mr) +
204                          section->offset_within_region;
205     GuestPhysBlock *predecessor = NULL;
206 
207     /* find continuity in guest physical address space */
208     if (!QTAILQ_EMPTY(&g->list->head)) {
209         hwaddr predecessor_size;
210 
211         predecessor = QTAILQ_LAST(&g->list->head);
212         predecessor_size = predecessor->target_end - predecessor->target_start;
213 
214         /* the memory API guarantees monotonically increasing traversal */
215         g_assert(predecessor->target_end <= target_start);
216 
217         /* we want continuity in both guest-physical and host-virtual memory */
218         if (predecessor->target_end < target_start ||
219             predecessor->host_addr + predecessor_size != host_addr ||
220             predecessor->mr != section->mr) {
221             predecessor = NULL;
222         }
223     }
224 
225     if (predecessor == NULL) {
226         /* isolated mapping, allocate it and add it to the list */
227         GuestPhysBlock *block = g_malloc0(sizeof *block);
228 
229         block->target_start = target_start;
230         block->target_end   = target_end;
231         block->host_addr    = host_addr;
232         block->mr           = section->mr;
233         memory_region_ref(section->mr);
234 
235         QTAILQ_INSERT_TAIL(&g->list->head, block, next);
236         ++g->list->num;
237     } else {
238         /* expand predecessor until @target_end; predecessor's start doesn't
239          * change
240          */
241         predecessor->target_end = target_end;
242     }
243 
244 #ifdef DEBUG_GUEST_PHYS_REGION_ADD
245     fprintf(stderr, "%s: target_start=" HWADDR_FMT_plx " target_end="
246             HWADDR_FMT_plx ": %s (count: %u)\n", __func__, target_start,
247             target_end, predecessor ? "joined" : "added", g->list->num);
248 #endif
249 }
250 
guest_phys_ram_populate_cb(MemoryRegionSection * section,void * opaque)251 static int guest_phys_ram_populate_cb(MemoryRegionSection *section,
252                                       void *opaque)
253 {
254     GuestPhysListener *g = opaque;
255 
256     guest_phys_block_add_section(g, section);
257     return 0;
258 }
259 
guest_phys_blocks_region_add(MemoryListener * listener,MemoryRegionSection * section)260 static void guest_phys_blocks_region_add(MemoryListener *listener,
261                                          MemoryRegionSection *section)
262 {
263     GuestPhysListener *g = container_of(listener, GuestPhysListener, listener);
264 
265     /* we only care about RAM */
266     if (!memory_region_is_ram(section->mr) ||
267         memory_region_is_ram_device(section->mr) ||
268         memory_region_is_nonvolatile(section->mr)) {
269         return;
270     }
271 
272     /* for special sparse regions, only add populated parts */
273     if (memory_region_has_ram_discard_manager(section->mr)) {
274         RamDiscardManager *rdm;
275 
276         rdm = memory_region_get_ram_discard_manager(section->mr);
277         ram_discard_manager_replay_populated(rdm, section,
278                                              guest_phys_ram_populate_cb, g);
279         return;
280     }
281 
282     guest_phys_block_add_section(g, section);
283 }
284 
guest_phys_blocks_append(GuestPhysBlockList * list)285 void guest_phys_blocks_append(GuestPhysBlockList *list)
286 {
287     GuestPhysListener g = { 0 };
288 
289     g.list = list;
290     g.listener.region_add = &guest_phys_blocks_region_add;
291     memory_listener_register(&g.listener, &address_space_memory);
292     memory_listener_unregister(&g.listener);
293 }
294 
find_paging_enabled_cpu(void)295 static CPUState *find_paging_enabled_cpu(void)
296 {
297     CPUState *cpu;
298 
299     CPU_FOREACH(cpu) {
300         if (cpu_paging_enabled(cpu)) {
301             return cpu;
302         }
303     }
304 
305     return NULL;
306 }
307 
qemu_get_guest_memory_mapping(MemoryMappingList * list,const GuestPhysBlockList * guest_phys_blocks,Error ** errp)308 bool qemu_get_guest_memory_mapping(MemoryMappingList *list,
309                                    const GuestPhysBlockList *guest_phys_blocks,
310                                    Error **errp)
311 {
312     ERRP_GUARD();
313     CPUState *cpu, *first_paging_enabled_cpu;
314     GuestPhysBlock *block;
315     ram_addr_t offset, length;
316 
317     first_paging_enabled_cpu = find_paging_enabled_cpu();
318     if (first_paging_enabled_cpu) {
319         for (cpu = first_paging_enabled_cpu; cpu != NULL;
320              cpu = CPU_NEXT(cpu)) {
321             if (!cpu_get_memory_mapping(cpu, list, errp)) {
322                 return false;
323             }
324         }
325         return true;
326     }
327 
328     /*
329      * If the guest doesn't use paging, the virtual address is equal to physical
330      * address.
331      */
332     QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
333         offset = block->target_start;
334         length = block->target_end - block->target_start;
335         create_new_memory_mapping(list, offset, offset, length);
336     }
337     return true;
338 }
339 
qemu_get_guest_simple_memory_mapping(MemoryMappingList * list,const GuestPhysBlockList * guest_phys_blocks)340 void qemu_get_guest_simple_memory_mapping(MemoryMappingList *list,
341                                    const GuestPhysBlockList *guest_phys_blocks)
342 {
343     GuestPhysBlock *block;
344 
345     QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
346         create_new_memory_mapping(list, block->target_start, 0,
347                                   block->target_end - block->target_start);
348     }
349 }
350 
memory_mapping_filter(MemoryMappingList * list,int64_t begin,int64_t length)351 void memory_mapping_filter(MemoryMappingList *list, int64_t begin,
352                            int64_t length)
353 {
354     MemoryMapping *cur, *next;
355 
356     QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) {
357         if (!ranges_overlap(cur->phys_addr, cur->length, begin, length)) {
358             QTAILQ_REMOVE(&list->head, cur, next);
359             g_free(cur);
360             list->num--;
361             continue;
362         }
363 
364         if (cur->phys_addr < begin) {
365             cur->length -= begin - cur->phys_addr;
366             if (cur->virt_addr) {
367                 cur->virt_addr += begin - cur->phys_addr;
368             }
369             cur->phys_addr = begin;
370         }
371 
372         if (cur->phys_addr + cur->length > begin + length) {
373             cur->length -= cur->phys_addr + cur->length - begin - length;
374         }
375     }
376 }
377