hw/arm: gsj add pca9548
[qemu.git] / softmmu / memory_mapping.c
blobe7af276546607caf92df8f3cf46b63e128927bff
1 /*
2 * QEMU memory mapping
4 * Copyright Fujitsu, Corp. 2011, 2012
6 * Authors:
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.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
17 #include "sysemu/memory_mapping.h"
18 #include "exec/memory.h"
19 #include "exec/address-spaces.h"
21 //#define DEBUG_GUEST_PHYS_REGION_ADD
23 static void memory_mapping_list_add_mapping_sorted(MemoryMappingList *list,
24 MemoryMapping *mapping)
26 MemoryMapping *p;
28 QTAILQ_FOREACH(p, &list->head, next) {
29 if (p->phys_addr >= mapping->phys_addr) {
30 QTAILQ_INSERT_BEFORE(p, mapping, next);
31 return;
34 QTAILQ_INSERT_TAIL(&list->head, mapping, next);
37 static void create_new_memory_mapping(MemoryMappingList *list,
38 hwaddr phys_addr,
39 hwaddr virt_addr,
40 ram_addr_t length)
42 MemoryMapping *memory_mapping;
44 memory_mapping = g_malloc(sizeof(MemoryMapping));
45 memory_mapping->phys_addr = phys_addr;
46 memory_mapping->virt_addr = virt_addr;
47 memory_mapping->length = length;
48 list->last_mapping = memory_mapping;
49 list->num++;
50 memory_mapping_list_add_mapping_sorted(list, memory_mapping);
53 static inline bool mapping_contiguous(MemoryMapping *map,
54 hwaddr phys_addr,
55 hwaddr virt_addr)
57 return phys_addr == map->phys_addr + map->length &&
58 virt_addr == map->virt_addr + map->length;
62 * [map->phys_addr, map->phys_addr + map->length) and
63 * [phys_addr, phys_addr + length) have intersection?
65 static inline bool mapping_have_same_region(MemoryMapping *map,
66 hwaddr phys_addr,
67 ram_addr_t length)
69 return !(phys_addr + length < map->phys_addr ||
70 phys_addr >= map->phys_addr + map->length);
74 * [map->phys_addr, map->phys_addr + map->length) and
75 * [phys_addr, phys_addr + length) have intersection. The virtual address in the
76 * intersection are the same?
78 static inline bool mapping_conflict(MemoryMapping *map,
79 hwaddr phys_addr,
80 hwaddr virt_addr)
82 return virt_addr - map->virt_addr != phys_addr - map->phys_addr;
86 * [map->virt_addr, map->virt_addr + map->length) and
87 * [virt_addr, virt_addr + length) have intersection. And the physical address
88 * in the intersection are the same.
90 static inline void mapping_merge(MemoryMapping *map,
91 hwaddr virt_addr,
92 ram_addr_t length)
94 if (virt_addr < map->virt_addr) {
95 map->length += map->virt_addr - virt_addr;
96 map->virt_addr = virt_addr;
99 if ((virt_addr + length) >
100 (map->virt_addr + map->length)) {
101 map->length = virt_addr + length - map->virt_addr;
105 void memory_mapping_list_add_merge_sorted(MemoryMappingList *list,
106 hwaddr phys_addr,
107 hwaddr virt_addr,
108 ram_addr_t length)
110 MemoryMapping *memory_mapping, *last_mapping;
112 if (QTAILQ_EMPTY(&list->head)) {
113 create_new_memory_mapping(list, phys_addr, virt_addr, length);
114 return;
117 last_mapping = list->last_mapping;
118 if (last_mapping) {
119 if (mapping_contiguous(last_mapping, phys_addr, virt_addr)) {
120 last_mapping->length += length;
121 return;
125 QTAILQ_FOREACH(memory_mapping, &list->head, next) {
126 if (mapping_contiguous(memory_mapping, phys_addr, virt_addr)) {
127 memory_mapping->length += length;
128 list->last_mapping = memory_mapping;
129 return;
132 if (phys_addr + length < memory_mapping->phys_addr) {
133 /* create a new region before memory_mapping */
134 break;
137 if (mapping_have_same_region(memory_mapping, phys_addr, length)) {
138 if (mapping_conflict(memory_mapping, phys_addr, virt_addr)) {
139 continue;
142 /* merge this region into memory_mapping */
143 mapping_merge(memory_mapping, virt_addr, length);
144 list->last_mapping = memory_mapping;
145 return;
149 /* this region can not be merged into any existed memory mapping. */
150 create_new_memory_mapping(list, phys_addr, virt_addr, length);
153 void memory_mapping_list_free(MemoryMappingList *list)
155 MemoryMapping *p, *q;
157 QTAILQ_FOREACH_SAFE(p, &list->head, next, q) {
158 QTAILQ_REMOVE(&list->head, p, next);
159 g_free(p);
162 list->num = 0;
163 list->last_mapping = NULL;
166 void memory_mapping_list_init(MemoryMappingList *list)
168 list->num = 0;
169 list->last_mapping = NULL;
170 QTAILQ_INIT(&list->head);
173 void guest_phys_blocks_free(GuestPhysBlockList *list)
175 GuestPhysBlock *p, *q;
177 QTAILQ_FOREACH_SAFE(p, &list->head, next, q) {
178 QTAILQ_REMOVE(&list->head, p, next);
179 memory_region_unref(p->mr);
180 g_free(p);
182 list->num = 0;
185 void guest_phys_blocks_init(GuestPhysBlockList *list)
187 list->num = 0;
188 QTAILQ_INIT(&list->head);
191 typedef struct GuestPhysListener {
192 GuestPhysBlockList *list;
193 MemoryListener listener;
194 } GuestPhysListener;
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;
202 uint8_t *host_addr;
203 GuestPhysBlock *predecessor;
205 /* we only care about RAM */
206 if (!memory_region_is_ram(section->mr) ||
207 memory_region_is_ram_device(section->mr) ||
208 memory_region_is_nonvolatile(section->mr)) {
209 return;
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;
220 /* find continuity in guest physical address space */
221 if (!QTAILQ_EMPTY(&g->list->head)) {
222 hwaddr predecessor_size;
224 predecessor = QTAILQ_LAST(&g->list->head);
225 predecessor_size = predecessor->target_end - predecessor->target_start;
227 /* the memory API guarantees monotonically increasing traversal */
228 g_assert(predecessor->target_end <= target_start);
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;
237 if (predecessor == NULL) {
238 /* isolated mapping, allocate it and add it to the list */
239 GuestPhysBlock *block = g_malloc0(sizeof *block);
241 block->target_start = target_start;
242 block->target_end = target_end;
243 block->host_addr = host_addr;
244 block->mr = section->mr;
245 memory_region_ref(section->mr);
247 QTAILQ_INSERT_TAIL(&g->list->head, block, next);
248 ++g->list->num;
249 } else {
250 /* expand predecessor until @target_end; predecessor's start doesn't
251 * change
253 predecessor->target_end = target_end;
256 #ifdef DEBUG_GUEST_PHYS_REGION_ADD
257 fprintf(stderr, "%s: target_start=" TARGET_FMT_plx " target_end="
258 TARGET_FMT_plx ": %s (count: %u)\n", __func__, target_start,
259 target_end, predecessor ? "joined" : "added", g->list->num);
260 #endif
263 void guest_phys_blocks_append(GuestPhysBlockList *list)
265 GuestPhysListener g = { 0 };
267 g.list = list;
268 g.listener.region_add = &guest_phys_blocks_region_add;
269 memory_listener_register(&g.listener, &address_space_memory);
270 memory_listener_unregister(&g.listener);
273 static CPUState *find_paging_enabled_cpu(CPUState *start_cpu)
275 CPUState *cpu;
277 CPU_FOREACH(cpu) {
278 if (cpu_paging_enabled(cpu)) {
279 return cpu;
283 return NULL;
286 void qemu_get_guest_memory_mapping(MemoryMappingList *list,
287 const GuestPhysBlockList *guest_phys_blocks,
288 Error **errp)
290 CPUState *cpu, *first_paging_enabled_cpu;
291 GuestPhysBlock *block;
292 ram_addr_t offset, length;
294 first_paging_enabled_cpu = find_paging_enabled_cpu(first_cpu);
295 if (first_paging_enabled_cpu) {
296 for (cpu = first_paging_enabled_cpu; cpu != NULL;
297 cpu = CPU_NEXT(cpu)) {
298 Error *err = NULL;
299 cpu_get_memory_mapping(cpu, list, &err);
300 if (err) {
301 error_propagate(errp, err);
302 return;
305 return;
309 * If the guest doesn't use paging, the virtual address is equal to physical
310 * address.
312 QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
313 offset = block->target_start;
314 length = block->target_end - block->target_start;
315 create_new_memory_mapping(list, offset, offset, length);
319 void qemu_get_guest_simple_memory_mapping(MemoryMappingList *list,
320 const GuestPhysBlockList *guest_phys_blocks)
322 GuestPhysBlock *block;
324 QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
325 create_new_memory_mapping(list, block->target_start, 0,
326 block->target_end - block->target_start);
330 void memory_mapping_filter(MemoryMappingList *list, int64_t begin,
331 int64_t length)
333 MemoryMapping *cur, *next;
335 QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) {
336 if (cur->phys_addr >= begin + length ||
337 cur->phys_addr + cur->length <= begin) {
338 QTAILQ_REMOVE(&list->head, cur, next);
339 g_free(cur);
340 list->num--;
341 continue;
344 if (cur->phys_addr < begin) {
345 cur->length -= begin - cur->phys_addr;
346 if (cur->virt_addr) {
347 cur->virt_addr += begin - cur->phys_addr;
349 cur->phys_addr = begin;
352 if (cur->phys_addr + cur->length > begin + length) {
353 cur->length -= cur->phys_addr + cur->length - begin - length;