ram_addr.h 14.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Declarations for cpu physical memory functions
 *
 * Copyright 2011 Red Hat, Inc. and/or its affiliates
 *
 * Authors:
 *  Avi Kivity <avi@redhat.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or
 * later.  See the COPYING file in the top-level directory.
 *
 */

/*
 * This header is for use by exec.c and memory.c ONLY.  Do not include it.
 * The functions declared here will be removed soon.
 */

#ifndef RAM_ADDR_H
#define RAM_ADDR_H

#ifndef CONFIG_USER_ONLY
#include "hw/xen/xen.h"
P
Paolo Bonzini 已提交
24
#include "exec/ramlist.h"
25

26 27 28 29 30 31 32 33 34 35 36 37 38
struct RAMBlock {
    struct rcu_head rcu;
    struct MemoryRegion *mr;
    uint8_t *host;
    ram_addr_t offset;
    ram_addr_t used_length;
    ram_addr_t max_length;
    void (*resized)(const char*, uint64_t length, void *host);
    uint32_t flags;
    /* Protected by iothread lock.  */
    char idstr[256];
    /* RCU-enabled, writes protected by the ramlist lock */
    QLIST_ENTRY(RAMBlock) next;
P
Paolo Bonzini 已提交
39
    QLIST_HEAD(, RAMBlockNotifier) ramblock_notifiers;
40
    int fd;
41
    size_t page_size;
42 43 44 45 46 47 48 49
    /* dirty bitmap used during migration */
    unsigned long *bmap;
    /* bitmap of pages that haven't been sent even once
     * only maintained and used in postcopy at the moment
     * where it's used to send the dirtymap at the start
     * of the postcopy phase
     */
    unsigned long *unsentmap;
50 51
    /* bitmap of already received pages in postcopy */
    unsigned long *receivedmap;
52 53
};

54 55 56 57 58
static inline bool offset_in_ramblock(RAMBlock *b, ram_addr_t offset)
{
    return (b && b->host && offset < b->used_length) ? true : false;
}

59 60
static inline void *ramblock_ptr(RAMBlock *block, ram_addr_t offset)
{
61
    assert(offset_in_ramblock(block, offset));
62 63 64
    return (char *)block->host + offset;
}

65 66 67 68 69 70 71 72
static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr,
                                                            RAMBlock *rb)
{
    uint64_t host_addr_offset =
            (uint64_t)(uintptr_t)(host_addr - (void *)rb->host);
    return host_addr_offset >> TARGET_PAGE_BITS;
}

73
long qemu_getrampagesize(void);
74
unsigned long last_ram_page(void);
75 76 77
RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
                                   bool share, const char *mem_path,
                                   Error **errp);
78 79 80
RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
                                 bool share, int fd,
                                 Error **errp);
81 82 83 84 85 86 87 88
RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
                                  MemoryRegion *mr, Error **errp);
RAMBlock *qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error **errp);
RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t max_size,
                                    void (*resized)(const char*,
                                                    uint64_t length,
                                                    void *host),
                                    MemoryRegion *mr, Error **errp);
89
void qemu_ram_free(RAMBlock *block);
90

G
Gonglei 已提交
91
int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp);
92

93 94 95
#define DIRTY_CLIENTS_ALL     ((1 << DIRTY_MEMORY_NUM) - 1)
#define DIRTY_CLIENTS_NOCODE  (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))

96 97 98 99
static inline bool cpu_physical_memory_get_dirty(ram_addr_t start,
                                                 ram_addr_t length,
                                                 unsigned client)
{
100 101
    DirtyMemoryBlocks *blocks;
    unsigned long end, page;
102
    unsigned long idx, offset, base;
103
    bool dirty = false;
104 105 106 107 108 109

    assert(client < DIRTY_MEMORY_NUM);

    end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
    page = start >> TARGET_PAGE_BITS;

110 111 112 113
    rcu_read_lock();

    blocks = atomic_rcu_read(&ram_list.dirty_memory[client]);

114 115 116
    idx = page / DIRTY_MEMORY_BLOCK_SIZE;
    offset = page % DIRTY_MEMORY_BLOCK_SIZE;
    base = page - offset;
117
    while (page < end) {
118 119 120 121
        unsigned long next = MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE);
        unsigned long num = next - base;
        unsigned long found = find_next_bit(blocks->blocks[idx], num, offset);
        if (found < num) {
122 123 124 125
            dirty = true;
            break;
        }

126 127 128 129
        page = next;
        idx++;
        offset = 0;
        base += DIRTY_MEMORY_BLOCK_SIZE;
130 131 132 133 134
    }

    rcu_read_unlock();

    return dirty;
135 136
}

137
static inline bool cpu_physical_memory_all_dirty(ram_addr_t start,
138 139 140
                                                 ram_addr_t length,
                                                 unsigned client)
{
141 142
    DirtyMemoryBlocks *blocks;
    unsigned long end, page;
143
    unsigned long idx, offset, base;
144
    bool dirty = true;
145 146 147 148 149 150

    assert(client < DIRTY_MEMORY_NUM);

    end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
    page = start >> TARGET_PAGE_BITS;

151 152 153 154
    rcu_read_lock();

    blocks = atomic_rcu_read(&ram_list.dirty_memory[client]);

155 156 157
    idx = page / DIRTY_MEMORY_BLOCK_SIZE;
    offset = page % DIRTY_MEMORY_BLOCK_SIZE;
    base = page - offset;
158
    while (page < end) {
159 160 161 162
        unsigned long next = MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE);
        unsigned long num = next - base;
        unsigned long found = find_next_zero_bit(blocks->blocks[idx], num, offset);
        if (found < num) {
163 164 165 166
            dirty = false;
            break;
        }

167 168 169 170
        page = next;
        idx++;
        offset = 0;
        base += DIRTY_MEMORY_BLOCK_SIZE;
171 172 173 174 175
    }

    rcu_read_unlock();

    return dirty;
176 177
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr,
                                                      unsigned client)
{
    return cpu_physical_memory_get_dirty(addr, 1, client);
}

static inline bool cpu_physical_memory_is_clean(ram_addr_t addr)
{
    bool vga = cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_VGA);
    bool code = cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_CODE);
    bool migration =
        cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_MIGRATION);
    return !(vga && code && migration);
}

193 194 195
static inline uint8_t cpu_physical_memory_range_includes_clean(ram_addr_t start,
                                                               ram_addr_t length,
                                                               uint8_t mask)
196
{
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    uint8_t ret = 0;

    if (mask & (1 << DIRTY_MEMORY_VGA) &&
        !cpu_physical_memory_all_dirty(start, length, DIRTY_MEMORY_VGA)) {
        ret |= (1 << DIRTY_MEMORY_VGA);
    }
    if (mask & (1 << DIRTY_MEMORY_CODE) &&
        !cpu_physical_memory_all_dirty(start, length, DIRTY_MEMORY_CODE)) {
        ret |= (1 << DIRTY_MEMORY_CODE);
    }
    if (mask & (1 << DIRTY_MEMORY_MIGRATION) &&
        !cpu_physical_memory_all_dirty(start, length, DIRTY_MEMORY_MIGRATION)) {
        ret |= (1 << DIRTY_MEMORY_MIGRATION);
    }
    return ret;
212 213
}

214 215 216
static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr,
                                                      unsigned client)
{
217 218 219
    unsigned long page, idx, offset;
    DirtyMemoryBlocks *blocks;

220
    assert(client < DIRTY_MEMORY_NUM);
221 222 223 224 225 226 227 228 229 230 231 232

    page = addr >> TARGET_PAGE_BITS;
    idx = page / DIRTY_MEMORY_BLOCK_SIZE;
    offset = page % DIRTY_MEMORY_BLOCK_SIZE;

    rcu_read_lock();

    blocks = atomic_rcu_read(&ram_list.dirty_memory[client]);

    set_bit_atomic(offset, blocks->blocks[idx]);

    rcu_read_unlock();
233 234 235
}

static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start,
236 237
                                                       ram_addr_t length,
                                                       uint8_t mask)
238
{
239
    DirtyMemoryBlocks *blocks[DIRTY_MEMORY_NUM];
240
    unsigned long end, page;
241
    unsigned long idx, offset, base;
242
    int i;
243

244 245 246 247
    if (!mask && !xen_enabled()) {
        return;
    }

248 249
    end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
    page = start >> TARGET_PAGE_BITS;
250 251 252 253 254

    rcu_read_lock();

    for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
        blocks[i] = atomic_rcu_read(&ram_list.dirty_memory[i]);
255
    }
256

257 258 259
    idx = page / DIRTY_MEMORY_BLOCK_SIZE;
    offset = page % DIRTY_MEMORY_BLOCK_SIZE;
    base = page - offset;
260
    while (page < end) {
261
        unsigned long next = MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE);
262 263 264

        if (likely(mask & (1 << DIRTY_MEMORY_MIGRATION))) {
            bitmap_set_atomic(blocks[DIRTY_MEMORY_MIGRATION]->blocks[idx],
265
                              offset, next - page);
266 267 268
        }
        if (unlikely(mask & (1 << DIRTY_MEMORY_VGA))) {
            bitmap_set_atomic(blocks[DIRTY_MEMORY_VGA]->blocks[idx],
269
                              offset, next - page);
270 271 272
        }
        if (unlikely(mask & (1 << DIRTY_MEMORY_CODE))) {
            bitmap_set_atomic(blocks[DIRTY_MEMORY_CODE]->blocks[idx],
273
                              offset, next - page);
274 275
        }

276 277 278 279
        page = next;
        idx++;
        offset = 0;
        base += DIRTY_MEMORY_BLOCK_SIZE;
280
    }
281 282 283

    rcu_read_unlock();

284
    xen_hvm_modified_memory(start, length);
285 286
}

287
#if !defined(_WIN32)
288 289 290 291
static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap,
                                                          ram_addr_t start,
                                                          ram_addr_t pages)
{
292
    unsigned long i, j;
293 294 295
    unsigned long page_number, c;
    hwaddr addr;
    ram_addr_t ram_addr;
296
    unsigned long len = (pages + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
297
    unsigned long hpratio = getpagesize() / TARGET_PAGE_SIZE;
298
    unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
299

300
    /* start address is aligned at the start of a word? */
301 302
    if ((((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) &&
        (hpratio == 1)) {
303 304 305
        unsigned long **blocks[DIRTY_MEMORY_NUM];
        unsigned long idx;
        unsigned long offset;
306 307 308
        long k;
        long nr = BITS_TO_LONGS(pages);

309 310 311 312 313 314 315 316 317 318
        idx = (start >> TARGET_PAGE_BITS) / DIRTY_MEMORY_BLOCK_SIZE;
        offset = BIT_WORD((start >> TARGET_PAGE_BITS) %
                          DIRTY_MEMORY_BLOCK_SIZE);

        rcu_read_lock();

        for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
            blocks[i] = atomic_rcu_read(&ram_list.dirty_memory[i])->blocks;
        }

319 320 321 322
        for (k = 0; k < nr; k++) {
            if (bitmap[k]) {
                unsigned long temp = leul_to_cpu(bitmap[k]);

323 324
                atomic_or(&blocks[DIRTY_MEMORY_MIGRATION][idx][offset], temp);
                atomic_or(&blocks[DIRTY_MEMORY_VGA][idx][offset], temp);
325
                if (tcg_enabled()) {
326
                    atomic_or(&blocks[DIRTY_MEMORY_CODE][idx][offset], temp);
327
                }
328
            }
329 330 331 332 333

            if (++offset >= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE)) {
                offset = 0;
                idx++;
            }
334
        }
335 336 337

        rcu_read_unlock();

338
        xen_hvm_modified_memory(start, pages << TARGET_PAGE_BITS);
339
    } else {
340
        uint8_t clients = tcg_enabled() ? DIRTY_CLIENTS_ALL : DIRTY_CLIENTS_NOCODE;
341 342 343 344 345 346 347 348
        /*
         * bitmap-traveling is faster than memory-traveling (for addr...)
         * especially when most of the memory is not dirty.
         */
        for (i = 0; i < len; i++) {
            if (bitmap[i] != 0) {
                c = leul_to_cpu(bitmap[i]);
                do {
N
Natanael Copa 已提交
349
                    j = ctzl(c);
350 351 352 353 354
                    c &= ~(1ul << j);
                    page_number = (i * HOST_LONG_BITS + j) * hpratio;
                    addr = page_number * TARGET_PAGE_SIZE;
                    ram_addr = start + addr;
                    cpu_physical_memory_set_dirty_range(ram_addr,
355
                                       TARGET_PAGE_SIZE * hpratio, clients);
356 357
                } while (c != 0);
            }
358 359 360
        }
    }
}
361
#endif /* not _WIN32 */
362

363 364 365
bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
                                              ram_addr_t length,
                                              unsigned client);
366

367 368 369 370 371 372 373
DirtyBitmapSnapshot *cpu_physical_memory_snapshot_and_clear_dirty
    (ram_addr_t start, ram_addr_t length, unsigned client);

bool cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot *snap,
                                            ram_addr_t start,
                                            ram_addr_t length);

374 375 376
static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start,
                                                         ram_addr_t length)
{
377 378 379
    cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_MIGRATION);
    cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_VGA);
    cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_CODE);
380 381 382
}


383
static inline
384
uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBlock *rb,
385
                                               ram_addr_t start,
386
                                               ram_addr_t length,
387
                                               uint64_t *real_dirty_pages)
388 389
{
    ram_addr_t addr;
390
    unsigned long word = BIT_WORD((start + rb->offset) >> TARGET_PAGE_BITS);
391
    uint64_t num_dirty = 0;
392
    unsigned long *dest = rb->bmap;
393 394

    /* start address is aligned at the start of a word? */
395 396
    if (((word * BITS_PER_LONG) << TARGET_PAGE_BITS) ==
         (start + rb->offset)) {
397 398
        int k;
        int nr = BITS_TO_LONGS(length >> TARGET_PAGE_BITS);
399
        unsigned long * const *src;
400 401
        unsigned long idx = (word * BITS_PER_LONG) / DIRTY_MEMORY_BLOCK_SIZE;
        unsigned long offset = BIT_WORD((word * BITS_PER_LONG) %
402
                                        DIRTY_MEMORY_BLOCK_SIZE);
403
        unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
404 405 406 407 408

        rcu_read_lock();

        src = atomic_rcu_read(
                &ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION])->blocks;
409 410

        for (k = page; k < page + nr; k++) {
411 412
            if (src[idx][offset]) {
                unsigned long bits = atomic_xchg(&src[idx][offset], 0);
413
                unsigned long new_dirty;
414
                *real_dirty_pages += ctpopl(bits);
415
                new_dirty = ~dest[k];
416 417
                dest[k] |= bits;
                new_dirty &= bits;
418 419
                num_dirty += ctpopl(new_dirty);
            }
420 421 422 423 424

            if (++offset >= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE)) {
                offset = 0;
                idx++;
            }
425
        }
426 427

        rcu_read_unlock();
428
    } else {
429 430
        ram_addr_t offset = rb->offset;

431
        for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
432
            if (cpu_physical_memory_test_and_clear_dirty(
433
                        start + addr + offset,
434 435
                        TARGET_PAGE_SIZE,
                        DIRTY_MEMORY_MIGRATION)) {
436
                *real_dirty_pages += 1;
437 438 439 440 441 442 443 444 445 446
                long k = (start + addr) >> TARGET_PAGE_BITS;
                if (!test_and_set_bit(k, dest)) {
                    num_dirty++;
                }
            }
        }
    }

    return num_dirty;
}
447 448
#endif
#endif