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