qemu/memory_mapping.c
<<
>>
Prefs
   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 "qapi/error.h"
  16#include <glib.h>
  17
  18#include "qemu-common.h"
  19#include "cpu.h"
  20#include "sysemu/memory_mapping.h"
  21#include "exec/memory.h"
  22#include "exec/address-spaces.h"
  23
  24//#define DEBUG_GUEST_PHYS_REGION_ADD
  25
  26static void memory_mapping_list_add_mapping_sorted(MemoryMappingList *list,
  27                                                   MemoryMapping *mapping)
  28{
  29    MemoryMapping *p;
  30
  31    QTAILQ_FOREACH(p, &list->head, next) {
  32        if (p->phys_addr >= mapping->phys_addr) {
  33            QTAILQ_INSERT_BEFORE(p, mapping, next);
  34            return;
  35        }
  36    }
  37    QTAILQ_INSERT_TAIL(&list->head, mapping, next);
  38}
  39
  40static void create_new_memory_mapping(MemoryMappingList *list,
  41                                      hwaddr phys_addr,
  42                                      hwaddr virt_addr,
  43                                      ram_addr_t length)
  44{
  45    MemoryMapping *memory_mapping;
  46
  47    memory_mapping = g_malloc(sizeof(MemoryMapping));
  48    memory_mapping->phys_addr = phys_addr;
  49    memory_mapping->virt_addr = virt_addr;
  50    memory_mapping->length = length;
  51    list->last_mapping = memory_mapping;
  52    list->num++;
  53    memory_mapping_list_add_mapping_sorted(list, memory_mapping);
  54}
  55
  56static inline bool mapping_contiguous(MemoryMapping *map,
  57                                      hwaddr phys_addr,
  58                                      hwaddr virt_addr)
  59{
  60    return phys_addr == map->phys_addr + map->length &&
  61           virt_addr == map->virt_addr + map->length;
  62}
  63
  64/*
  65 * [map->phys_addr, map->phys_addr + map->length) and
  66 * [phys_addr, phys_addr + length) have intersection?
  67 */
  68static inline bool mapping_have_same_region(MemoryMapping *map,
  69                                            hwaddr phys_addr,
  70                                            ram_addr_t length)
  71{
  72    return !(phys_addr + length < map->phys_addr ||
  73             phys_addr >= map->phys_addr + map->length);
  74}
  75
  76/*
  77 * [map->phys_addr, map->phys_addr + map->length) and
  78 * [phys_addr, phys_addr + length) have intersection. The virtual address in the
  79 * intersection are the same?
  80 */
  81static inline bool mapping_conflict(MemoryMapping *map,
  82                                    hwaddr phys_addr,
  83                                    hwaddr virt_addr)
  84{
  85    return virt_addr - map->virt_addr != phys_addr - map->phys_addr;
  86}
  87
  88/*
  89 * [map->virt_addr, map->virt_addr + map->length) and
  90 * [virt_addr, virt_addr + length) have intersection. And the physical address
  91 * in the intersection are the same.
  92 */
  93static inline void mapping_merge(MemoryMapping *map,
  94                                 hwaddr virt_addr,
  95                                 ram_addr_t length)
  96{
  97    if (virt_addr < map->virt_addr) {
  98        map->length += map->virt_addr - virt_addr;
  99        map->virt_addr = virt_addr;
 100    }
 101
 102    if ((virt_addr + length) >
 103        (map->virt_addr + map->length)) {
 104        map->length = virt_addr + length - map->virt_addr;
 105    }
 106}
 107
 108void memory_mapping_list_add_merge_sorted(MemoryMappingList *list,
 109                                          hwaddr phys_addr,
 110                                          hwaddr virt_addr,
 111                                          ram_addr_t length)
 112{
 113    MemoryMapping *memory_mapping, *last_mapping;
 114
 115    if (QTAILQ_EMPTY(&list->head)) {
 116        create_new_memory_mapping(list, phys_addr, virt_addr, length);
 117        return;
 118    }
 119
 120    last_mapping = list->last_mapping;
 121    if (last_mapping) {
 122        if (mapping_contiguous(last_mapping, phys_addr, virt_addr)) {
 123            last_mapping->length += length;
 124            return;
 125        }
 126    }
 127
 128    QTAILQ_FOREACH(memory_mapping, &list->head, next) {
 129        if (mapping_contiguous(memory_mapping, phys_addr, virt_addr)) {
 130            memory_mapping->length += length;
 131            list->last_mapping = memory_mapping;
 132            return;
 133        }
 134
 135        if (phys_addr + length < memory_mapping->phys_addr) {
 136            /* create a new region before memory_mapping */
 137            break;
 138        }
 139
 140        if (mapping_have_same_region(memory_mapping, phys_addr, length)) {
 141            if (mapping_conflict(memory_mapping, phys_addr, virt_addr)) {
 142                continue;
 143            }
 144
 145            /* merge this region into memory_mapping */
 146            mapping_merge(memory_mapping, virt_addr, length);
 147            list->last_mapping = memory_mapping;
 148            return;
 149        }
 150    }
 151
 152    /* this region can not be merged into any existed memory mapping. */
 153    create_new_memory_mapping(list, phys_addr, virt_addr, length);
 154}
 155
 156void memory_mapping_list_free(MemoryMappingList *list)
 157{
 158    MemoryMapping *p, *q;
 159
 160    QTAILQ_FOREACH_SAFE(p, &list->head, next, q) {
 161        QTAILQ_REMOVE(&list->head, p, next);
 162        g_free(p);
 163    }
 164
 165    list->num = 0;
 166    list->last_mapping = NULL;
 167}
 168
 169void memory_mapping_list_init(MemoryMappingList *list)
 170{
 171    list->num = 0;
 172    list->last_mapping = NULL;
 173    QTAILQ_INIT(&list->head);
 174}
 175
 176void guest_phys_blocks_free(GuestPhysBlockList *list)
 177{
 178    GuestPhysBlock *p, *q;
 179
 180    QTAILQ_FOREACH_SAFE(p, &list->head, next, q) {
 181        QTAILQ_REMOVE(&list->head, p, next);
 182        memory_region_unref(p->mr);
 183        g_free(p);
 184    }
 185    list->num = 0;
 186}
 187
 188void guest_phys_blocks_init(GuestPhysBlockList *list)
 189{
 190    list->num = 0;
 191    QTAILQ_INIT(&list->head);
 192}
 193
 194typedef struct GuestPhysListener {
 195    GuestPhysBlockList *list;
 196    MemoryListener listener;
 197} GuestPhysListener;
 198
 199static void guest_phys_blocks_region_add(MemoryListener *listener,
 200                                         MemoryRegionSection *section)
 201{
 202    GuestPhysListener *g;
 203    uint64_t section_size;
 204    hwaddr target_start, target_end;
 205    uint8_t *host_addr;
 206    GuestPhysBlock *predecessor;
 207
 208    /* we only care about RAM */
 209    if (!memory_region_is_ram(section->mr) ||
 210        memory_region_is_skip_dump(section->mr)) {
 211        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
 226        predecessor = QTAILQ_LAST(&g->list->head, GuestPhysBlockHead);
 227        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;
 246        block->mr           = section->mr;
 247        memory_region_ref(section->mr);
 248
 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="
 260            TARGET_FMT_plx ": %s (count: %u)\n", __FUNCTION__, target_start,
 261            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
 275static CPUState *find_paging_enabled_cpu(CPUState *start_cpu)
 276{
 277    CPUState *cpu;
 278
 279    CPU_FOREACH(cpu) {
 280        if (cpu_paging_enabled(cpu)) {
 281            return cpu;
 282        }
 283    }
 284
 285    return NULL;
 286}
 287
 288void qemu_get_guest_memory_mapping(MemoryMappingList *list,
 289                                   const GuestPhysBlockList *guest_phys_blocks,
 290                                   Error **errp)
 291{
 292    CPUState *cpu, *first_paging_enabled_cpu;
 293    GuestPhysBlock *block;
 294    ram_addr_t offset, length;
 295
 296    first_paging_enabled_cpu = find_paging_enabled_cpu(first_cpu);
 297    if (first_paging_enabled_cpu) {
 298        for (cpu = first_paging_enabled_cpu; cpu != NULL;
 299             cpu = CPU_NEXT(cpu)) {
 300            Error *err = NULL;
 301            cpu_get_memory_mapping(cpu, list, &err);
 302            if (err) {
 303                error_propagate(errp, err);
 304                return;
 305            }
 306        }
 307        return;
 308    }
 309
 310    /*
 311     * If the guest doesn't use paging, the virtual address is equal to physical
 312     * address.
 313     */
 314    QTAILQ_FOREACH(block, &guest_phys_blocks->head, next) {
 315        offset = block->target_start;
 316        length = block->target_end - block->target_start;
 317        create_new_memory_mapping(list, offset, offset, length);
 318    }
 319}
 320
 321void qemu_get_guest_simple_memory_mapping(MemoryMappingList *list,
 322                                   const GuestPhysBlockList *guest_phys_blocks)
 323{
 324    GuestPhysBlock *block;
 325
 326    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);
 329    }
 330}
 331
 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);
 341            list->num--;
 342            continue;
 343        }
 344
 345        if (cur->phys_addr < begin) {
 346            cur->length -= begin - cur->phys_addr;
 347            if (cur->virt_addr) {
 348                cur->virt_addr += begin - cur->phys_addr;
 349            }
 350            cur->phys_addr = begin;
 351        }
 352
 353        if (cur->phys_addr + cur->length > begin + length) {
 354            cur->length -= cur->phys_addr + cur->length - begin - length;
 355        }
 356    }
 357}
 358