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 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 15 | #include "qapi/error.h" |
Laszlo Ersek | c5d7f60 | 2013-08-06 12:37:10 +0200 | [diff] [blame] | 16 | |
Peter Crosthwaite | 94beb66 | 2015-06-07 14:59:09 -0700 | [diff] [blame] | 17 | #include "qemu-common.h" |
Wen Congyang | 80167a8 | 2012-05-07 12:03:46 +0800 | [diff] [blame] | 18 | #include "cpu.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 19 | #include "sysemu/memory_mapping.h" |
Laszlo Ersek | c5d7f60 | 2013-08-06 12:37:10 +0200 | [diff] [blame] | 20 | #include "exec/memory.h" |
| 21 | #include "exec/address-spaces.h" |
| 22 | |
| 23 | //#define DEBUG_GUEST_PHYS_REGION_ADD |
Wen Congyang | 80167a8 | 2012-05-07 12:03:46 +0800 | [diff] [blame] | 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, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 40 | hwaddr phys_addr, |
| 41 | hwaddr virt_addr, |
Wen Congyang | 80167a8 | 2012-05-07 12:03:46 +0800 | [diff] [blame] | 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, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 56 | hwaddr phys_addr, |
| 57 | hwaddr virt_addr) |
Wen Congyang | 80167a8 | 2012-05-07 12:03:46 +0800 | [diff] [blame] | 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, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 68 | hwaddr phys_addr, |
Wen Congyang | 80167a8 | 2012-05-07 12:03:46 +0800 | [diff] [blame] | 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, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 81 | hwaddr phys_addr, |
| 82 | hwaddr virt_addr) |
Wen Congyang | 80167a8 | 2012-05-07 12:03:46 +0800 | [diff] [blame] | 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, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 93 | hwaddr virt_addr, |
Wen Congyang | 80167a8 | 2012-05-07 12:03:46 +0800 | [diff] [blame] | 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, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 108 | hwaddr phys_addr, |
| 109 | hwaddr virt_addr, |
Wen Congyang | 80167a8 | 2012-05-07 12:03:46 +0800 | [diff] [blame] | 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 | } |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 174 | |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 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); |
Peter Xu | 1fbeff7 | 2016-02-18 13:16:52 +0800 | [diff] [blame] | 181 | memory_region_unref(p->mr); |
Laszlo Ersek | 5ee163e | 2013-08-06 12:37:09 +0200 | [diff] [blame] | 182 | g_free(p); |
| 183 | } |
| 184 | list->num = 0; |
| 185 | } |
| 186 | |
| 187 | void guest_phys_blocks_init(GuestPhysBlockList *list) |
| 188 | { |
| 189 | list->num = 0; |
| 190 | QTAILQ_INIT(&list->head); |
| 191 | } |
| 192 | |
Laszlo Ersek | c5d7f60 | 2013-08-06 12:37:10 +0200 | [diff] [blame] | 193 | typedef struct GuestPhysListener { |
| 194 | GuestPhysBlockList *list; |
| 195 | MemoryListener listener; |
| 196 | } GuestPhysListener; |
| 197 | |
| 198 | static 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 Dadhania | e4dc3f5 | 2014-09-15 09:28:23 +0530 | [diff] [blame] | 208 | if (!memory_region_is_ram(section->mr) || |
| 209 | memory_region_is_skip_dump(section->mr)) { |
Laszlo Ersek | c5d7f60 | 2013-08-06 12:37:10 +0200 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | |
| 213 | g = container_of(listener, GuestPhysListener, listener); |
| 214 | section_size = int128_get64(section->size); |
| 215 | target_start = section->offset_within_address_space; |
| 216 | target_end = target_start + section_size; |
| 217 | host_addr = memory_region_get_ram_ptr(section->mr) + |
| 218 | section->offset_within_region; |
| 219 | predecessor = NULL; |
| 220 | |
| 221 | /* find continuity in guest physical address space */ |
| 222 | if (!QTAILQ_EMPTY(&g->list->head)) { |
| 223 | hwaddr predecessor_size; |
| 224 | |
| 225 | predecessor = QTAILQ_LAST(&g->list->head, GuestPhysBlockHead); |
| 226 | predecessor_size = predecessor->target_end - predecessor->target_start; |
| 227 | |
| 228 | /* the memory API guarantees monotonically increasing traversal */ |
| 229 | g_assert(predecessor->target_end <= target_start); |
| 230 | |
| 231 | /* we want continuity in both guest-physical and host-virtual memory */ |
| 232 | if (predecessor->target_end < target_start || |
| 233 | predecessor->host_addr + predecessor_size != host_addr) { |
| 234 | predecessor = NULL; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if (predecessor == NULL) { |
| 239 | /* isolated mapping, allocate it and add it to the list */ |
| 240 | GuestPhysBlock *block = g_malloc0(sizeof *block); |
| 241 | |
| 242 | block->target_start = target_start; |
| 243 | block->target_end = target_end; |
| 244 | block->host_addr = host_addr; |
Peter Xu | 1fbeff7 | 2016-02-18 13:16:52 +0800 | [diff] [blame] | 245 | block->mr = section->mr; |
| 246 | memory_region_ref(section->mr); |
Laszlo Ersek | c5d7f60 | 2013-08-06 12:37:10 +0200 | [diff] [blame] | 247 | |
| 248 | QTAILQ_INSERT_TAIL(&g->list->head, block, next); |
| 249 | ++g->list->num; |
| 250 | } else { |
| 251 | /* expand predecessor until @target_end; predecessor's start doesn't |
| 252 | * change |
| 253 | */ |
| 254 | predecessor->target_end = target_end; |
| 255 | } |
| 256 | |
| 257 | #ifdef DEBUG_GUEST_PHYS_REGION_ADD |
| 258 | fprintf(stderr, "%s: target_start=" TARGET_FMT_plx " target_end=" |
| 259 | TARGET_FMT_plx ": %s (count: %u)\n", __FUNCTION__, target_start, |
| 260 | target_end, predecessor ? "joined" : "added", g->list->num); |
| 261 | #endif |
| 262 | } |
| 263 | |
| 264 | void guest_phys_blocks_append(GuestPhysBlockList *list) |
| 265 | { |
| 266 | GuestPhysListener g = { 0 }; |
| 267 | |
| 268 | g.list = list; |
| 269 | g.listener.region_add = &guest_phys_blocks_region_add; |
| 270 | memory_listener_register(&g.listener, &address_space_memory); |
| 271 | memory_listener_unregister(&g.listener); |
| 272 | } |
| 273 | |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 274 | static CPUState *find_paging_enabled_cpu(CPUState *start_cpu) |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 275 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 276 | CPUState *cpu; |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 277 | |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 278 | CPU_FOREACH(cpu) { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 279 | if (cpu_paging_enabled(cpu)) { |
| 280 | return cpu; |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | return NULL; |
| 285 | } |
| 286 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 287 | void qemu_get_guest_memory_mapping(MemoryMappingList *list, |
| 288 | const GuestPhysBlockList *guest_phys_blocks, |
| 289 | Error **errp) |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 290 | { |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 291 | CPUState *cpu, *first_paging_enabled_cpu; |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 292 | GuestPhysBlock *block; |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 293 | ram_addr_t offset, length; |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 294 | |
| 295 | first_paging_enabled_cpu = find_paging_enabled_cpu(first_cpu); |
| 296 | if (first_paging_enabled_cpu) { |
Andreas Färber | bdc4464 | 2013-06-24 23:50:24 +0200 | [diff] [blame] | 297 | for (cpu = first_paging_enabled_cpu; cpu != NULL; |
| 298 | cpu = CPU_NEXT(cpu)) { |
Andreas Färber | a23bbfd | 2013-05-28 13:52:01 +0200 | [diff] [blame] | 299 | Error *err = NULL; |
Andreas Färber | 182735e | 2013-05-29 22:29:20 +0200 | [diff] [blame] | 300 | cpu_get_memory_mapping(cpu, list, &err); |
Andreas Färber | a23bbfd | 2013-05-28 13:52:01 +0200 | [diff] [blame] | 301 | if (err) { |
Andreas Färber | 11ed09c | 2013-05-29 21:54:03 +0200 | [diff] [blame] | 302 | error_propagate(errp, err); |
| 303 | return; |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 304 | } |
| 305 | } |
Andreas Färber | 11ed09c | 2013-05-29 21:54:03 +0200 | [diff] [blame] | 306 | return; |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
| 310 | * If the guest doesn't use paging, the virtual address is equal to physical |
| 311 | * address. |
| 312 | */ |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 313 | QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) { |
| 314 | offset = block->target_start; |
| 315 | length = block->target_end - block->target_start; |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 316 | create_new_memory_mapping(list, offset, offset, length); |
| 317 | } |
Wen Congyang | c517076 | 2012-05-07 12:06:40 +0800 | [diff] [blame] | 318 | } |
Wen Congyang | 2b05ab5 | 2012-05-07 12:07:07 +0800 | [diff] [blame] | 319 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 320 | void qemu_get_guest_simple_memory_mapping(MemoryMappingList *list, |
| 321 | const GuestPhysBlockList *guest_phys_blocks) |
Wen Congyang | 2b05ab5 | 2012-05-07 12:07:07 +0800 | [diff] [blame] | 322 | { |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 323 | GuestPhysBlock *block; |
Wen Congyang | 2b05ab5 | 2012-05-07 12:07:07 +0800 | [diff] [blame] | 324 | |
Laszlo Ersek | 56c4bfb | 2013-08-06 12:37:11 +0200 | [diff] [blame] | 325 | QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) { |
| 326 | create_new_memory_mapping(list, block->target_start, 0, |
| 327 | block->target_end - block->target_start); |
Wen Congyang | 2b05ab5 | 2012-05-07 12:07:07 +0800 | [diff] [blame] | 328 | } |
| 329 | } |
Wen Congyang | 783e9b4 | 2012-05-07 12:10:47 +0800 | [diff] [blame] | 330 | |
| 331 | void memory_mapping_filter(MemoryMappingList *list, int64_t begin, |
| 332 | int64_t length) |
| 333 | { |
| 334 | MemoryMapping *cur, *next; |
| 335 | |
| 336 | QTAILQ_FOREACH_SAFE(cur, &list->head, next, next) { |
| 337 | if (cur->phys_addr >= begin + length || |
| 338 | cur->phys_addr + cur->length <= begin) { |
| 339 | QTAILQ_REMOVE(&list->head, cur, next); |
| 340 | list->num--; |
| 341 | continue; |
| 342 | } |
| 343 | |
| 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; |
| 348 | } |
| 349 | cur->phys_addr = begin; |
| 350 | } |
| 351 | |
| 352 | if (cur->phys_addr + cur->length > begin + length) { |
| 353 | cur->length -= cur->phys_addr + cur->length - begin - length; |
| 354 | } |
| 355 | } |
| 356 | } |