blob: e3ec70624f871db4fc26494cad17b79034feaabb [file] [log] [blame]
Wen Congyang80167a82012-05-07 12:03:46 +08001/*
2 * QEMU memory mapping
3 *
4 * Copyright Fujitsu, Corp. 2011, 2012
5 *
6 * Authors:
7 * Wen Congyang <wency@cn.fujitsu.com>
8 *
Stefan Weilfc0608a2012-06-10 19:49:18 +00009 * 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.
Wen Congyang80167a82012-05-07 12:03:46 +080011 *
12 */
13
Peter Maydelld38ea872016-01-29 17:50:05 +000014#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010015#include "qapi/error.h"
Laszlo Ersekc5d7f602013-08-06 12:37:10 +020016
Peter Crosthwaite94beb662015-06-07 14:59:09 -070017#include "qemu-common.h"
Wen Congyang80167a82012-05-07 12:03:46 +080018#include "cpu.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010019#include "sysemu/memory_mapping.h"
Laszlo Ersekc5d7f602013-08-06 12:37:10 +020020#include "exec/memory.h"
21#include "exec/address-spaces.h"
22
23//#define DEBUG_GUEST_PHYS_REGION_ADD
Wen Congyang80167a82012-05-07 12:03:46 +080024
25static 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
39static void create_new_memory_mapping(MemoryMappingList *list,
Avi Kivitya8170e52012-10-23 12:30:10 +020040 hwaddr phys_addr,
41 hwaddr virt_addr,
Wen Congyang80167a82012-05-07 12:03:46 +080042 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
55static inline bool mapping_contiguous(MemoryMapping *map,
Avi Kivitya8170e52012-10-23 12:30:10 +020056 hwaddr phys_addr,
57 hwaddr virt_addr)
Wen Congyang80167a82012-05-07 12:03:46 +080058{
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 */
67static inline bool mapping_have_same_region(MemoryMapping *map,
Avi Kivitya8170e52012-10-23 12:30:10 +020068 hwaddr phys_addr,
Wen Congyang80167a82012-05-07 12:03:46 +080069 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 */
80static inline bool mapping_conflict(MemoryMapping *map,
Avi Kivitya8170e52012-10-23 12:30:10 +020081 hwaddr phys_addr,
82 hwaddr virt_addr)
Wen Congyang80167a82012-05-07 12:03:46 +080083{
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 */
92static inline void mapping_merge(MemoryMapping *map,
Avi Kivitya8170e52012-10-23 12:30:10 +020093 hwaddr virt_addr,
Wen Congyang80167a82012-05-07 12:03:46 +080094 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
107void memory_mapping_list_add_merge_sorted(MemoryMappingList *list,
Avi Kivitya8170e52012-10-23 12:30:10 +0200108 hwaddr phys_addr,
109 hwaddr virt_addr,
Wen Congyang80167a82012-05-07 12:03:46 +0800110 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
155void 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
168void memory_mapping_list_init(MemoryMappingList *list)
169{
170 list->num = 0;
171 list->last_mapping = NULL;
172 QTAILQ_INIT(&list->head);
173}
Wen Congyangc5170762012-05-07 12:06:40 +0800174
Laszlo Ersek5ee163e2013-08-06 12:37:09 +0200175void 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);
Peter Xu1fbeff72016-02-18 13:16:52 +0800181 memory_region_unref(p->mr);
Laszlo Ersek5ee163e2013-08-06 12:37:09 +0200182 g_free(p);
183 }
184 list->num = 0;
185}
186
187void guest_phys_blocks_init(GuestPhysBlockList *list)
188{
189 list->num = 0;
190 QTAILQ_INIT(&list->head);
191}
192
Laszlo Ersekc5d7f602013-08-06 12:37:10 +0200193typedef struct GuestPhysListener {
194 GuestPhysBlockList *list;
195 MemoryListener listener;
196} GuestPhysListener;
197
198static void guest_phys_blocks_region_add(MemoryListener *listener,
199 MemoryRegionSection *section)
200{
201 GuestPhysListener *g;
202 uint64_t section_size;
203 hwaddr target_start, target_end;
204 uint8_t *host_addr;
205 GuestPhysBlock *predecessor;
206
207 /* we only care about RAM */
Nikunj A Dadhaniae4dc3f52014-09-15 09:28:23 +0530208 if (!memory_region_is_ram(section->mr) ||
Marc-André Lureau17a6ddb2018-10-03 15:44:54 +0400209 memory_region_is_ram_device(section->mr) ||
210 memory_region_is_nonvolatile(section->mr)) {
Laszlo Ersekc5d7f602013-08-06 12:37:10 +0200211 return;
212 }
213
214 g = container_of(listener, GuestPhysListener, listener);
215 section_size = int128_get64(section->size);
216 target_start = section->offset_within_address_space;
217 target_end = target_start + section_size;
218 host_addr = memory_region_get_ram_ptr(section->mr) +
219 section->offset_within_region;
220 predecessor = NULL;
221
222 /* find continuity in guest physical address space */
223 if (!QTAILQ_EMPTY(&g->list->head)) {
224 hwaddr predecessor_size;
225
Paolo Bonzinieae3eb32018-12-06 13:10:34 +0100226 predecessor = QTAILQ_LAST(&g->list->head);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +0200227 predecessor_size = predecessor->target_end - predecessor->target_start;
228
229 /* the memory API guarantees monotonically increasing traversal */
230 g_assert(predecessor->target_end <= target_start);
231
232 /* we want continuity in both guest-physical and host-virtual memory */
233 if (predecessor->target_end < target_start ||
234 predecessor->host_addr + predecessor_size != host_addr) {
235 predecessor = NULL;
236 }
237 }
238
239 if (predecessor == NULL) {
240 /* isolated mapping, allocate it and add it to the list */
241 GuestPhysBlock *block = g_malloc0(sizeof *block);
242
243 block->target_start = target_start;
244 block->target_end = target_end;
245 block->host_addr = host_addr;
Peter Xu1fbeff72016-02-18 13:16:52 +0800246 block->mr = section->mr;
247 memory_region_ref(section->mr);
Laszlo Ersekc5d7f602013-08-06 12:37:10 +0200248
249 QTAILQ_INSERT_TAIL(&g->list->head, block, next);
250 ++g->list->num;
251 } else {
252 /* expand predecessor until @target_end; predecessor's start doesn't
253 * change
254 */
255 predecessor->target_end = target_end;
256 }
257
258#ifdef DEBUG_GUEST_PHYS_REGION_ADD
259 fprintf(stderr, "%s: target_start=" TARGET_FMT_plx " target_end="
Alistair Francisa89f3642017-11-08 14:56:31 -0800260 TARGET_FMT_plx ": %s (count: %u)\n", __func__, target_start,
Laszlo Ersekc5d7f602013-08-06 12:37:10 +0200261 target_end, predecessor ? "joined" : "added", g->list->num);
262#endif
263}
264
265void guest_phys_blocks_append(GuestPhysBlockList *list)
266{
267 GuestPhysListener g = { 0 };
268
269 g.list = list;
270 g.listener.region_add = &guest_phys_blocks_region_add;
271 memory_listener_register(&g.listener, &address_space_memory);
272 memory_listener_unregister(&g.listener);
273}
274
Andreas Färber182735e2013-05-29 22:29:20 +0200275static CPUState *find_paging_enabled_cpu(CPUState *start_cpu)
Wen Congyangc5170762012-05-07 12:06:40 +0800276{
Andreas Färber182735e2013-05-29 22:29:20 +0200277 CPUState *cpu;
Wen Congyangc5170762012-05-07 12:06:40 +0800278
Andreas Färberbdc44642013-06-24 23:50:24 +0200279 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200280 if (cpu_paging_enabled(cpu)) {
281 return cpu;
Wen Congyangc5170762012-05-07 12:06:40 +0800282 }
283 }
284
285 return NULL;
286}
287
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200288void qemu_get_guest_memory_mapping(MemoryMappingList *list,
289 const GuestPhysBlockList *guest_phys_blocks,
290 Error **errp)
Wen Congyangc5170762012-05-07 12:06:40 +0800291{
Andreas Färber182735e2013-05-29 22:29:20 +0200292 CPUState *cpu, *first_paging_enabled_cpu;
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200293 GuestPhysBlock *block;
Wen Congyangc5170762012-05-07 12:06:40 +0800294 ram_addr_t offset, length;
Wen Congyangc5170762012-05-07 12:06:40 +0800295
296 first_paging_enabled_cpu = find_paging_enabled_cpu(first_cpu);
297 if (first_paging_enabled_cpu) {
Andreas Färberbdc44642013-06-24 23:50:24 +0200298 for (cpu = first_paging_enabled_cpu; cpu != NULL;
299 cpu = CPU_NEXT(cpu)) {
Andreas Färbera23bbfd2013-05-28 13:52:01 +0200300 Error *err = NULL;
Andreas Färber182735e2013-05-29 22:29:20 +0200301 cpu_get_memory_mapping(cpu, list, &err);
Andreas Färbera23bbfd2013-05-28 13:52:01 +0200302 if (err) {
Andreas Färber11ed09c2013-05-29 21:54:03 +0200303 error_propagate(errp, err);
304 return;
Wen Congyangc5170762012-05-07 12:06:40 +0800305 }
306 }
Andreas Färber11ed09c2013-05-29 21:54:03 +0200307 return;
Wen Congyangc5170762012-05-07 12:06:40 +0800308 }
309
310 /*
311 * If the guest doesn't use paging, the virtual address is equal to physical
312 * address.
313 */
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200314 QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
315 offset = block->target_start;
316 length = block->target_end - block->target_start;
Wen Congyangc5170762012-05-07 12:06:40 +0800317 create_new_memory_mapping(list, offset, offset, length);
318 }
Wen Congyangc5170762012-05-07 12:06:40 +0800319}
Wen Congyang2b05ab52012-05-07 12:07:07 +0800320
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200321void qemu_get_guest_simple_memory_mapping(MemoryMappingList *list,
322 const GuestPhysBlockList *guest_phys_blocks)
Wen Congyang2b05ab52012-05-07 12:07:07 +0800323{
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200324 GuestPhysBlock *block;
Wen Congyang2b05ab52012-05-07 12:07:07 +0800325
Laszlo Ersek56c4bfb2013-08-06 12:37:11 +0200326 QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
327 create_new_memory_mapping(list, block->target_start, 0,
328 block->target_end - block->target_start);
Wen Congyang2b05ab52012-05-07 12:07:07 +0800329 }
330}
Wen Congyang783e9b42012-05-07 12:10:47 +0800331
332void memory_mapping_filter(MemoryMappingList *list, int64_t begin,
333 int64_t length)
334{
335 MemoryMapping *cur, *next;
336
337 QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) {
338 if (cur->phys_addr >= begin + length ||
339 cur->phys_addr + cur->length <= begin) {
340 QTAILQ_REMOVE(&list->head, cur, next);
Marc-André Lureau22c3aea2017-05-04 02:38:46 +0400341 g_free(cur);
Wen Congyang783e9b42012-05-07 12:10:47 +0800342 list->num--;
343 continue;
344 }
345
346 if (cur->phys_addr < begin) {
347 cur->length -= begin - cur->phys_addr;
348 if (cur->virt_addr) {
349 cur->virt_addr += begin - cur->phys_addr;
350 }
351 cur->phys_addr = begin;
352 }
353
354 if (cur->phys_addr + cur->length > begin + length) {
355 cur->length -= cur->phys_addr + cur->length - begin - length;
356 }
357 }
358}