master
h 86 lines 3.11 KB
Raw
1 #ifndef RAMLIST_H
2 #define RAMLIST_H
3
4 #include "qemu/queue.h"
5 #include "qemu/thread.h"
6 #include "qemu/rcu.h"
7 #include "qemu/rcu_queue.h"
8 #include "system/ram_addr.h"
9
10 typedef struct RAMBlockNotifier RAMBlockNotifier;
11
12 /* The dirty memory bitmap is split into fixed-size blocks to allow growth
13 * under RCU. The bitmap for a block can be accessed as follows:
14 *
15 * rcu_read_lock();
16 *
17 * DirtyMemoryBlocks *blocks =
18 * qatomic_rcu_read(&ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION]);
19 *
20 * ram_addr_t idx = (addr >> TARGET_PAGE_BITS) / DIRTY_MEMORY_BLOCK_SIZE;
21 * unsigned long *block = blocks.blocks[idx];
22 * ...access block bitmap...
23 *
24 * rcu_read_unlock();
25 *
26 * Remember to check for the end of the block when accessing a range of
27 * addresses. Move on to the next block if you reach the end.
28 *
29 * Organization into blocks allows dirty memory to grow (but not shrink) under
30 * RCU. When adding new RAMBlocks requires the dirty memory to grow, a new
31 * DirtyMemoryBlocks array is allocated with pointers to existing blocks kept
32 * the same. Other threads can safely access existing blocks while dirty
33 * memory is being grown. When no threads are using the old DirtyMemoryBlocks
34 * anymore it is freed by RCU (but the underlying blocks stay because they are
35 * pointed to from the new DirtyMemoryBlocks).
36 */
37 #define DIRTY_MEMORY_BLOCK_SIZE ((ram_addr_t)256 * 1024 * 8)
38 typedef struct {
39 struct rcu_head rcu;
40 unsigned long *blocks[];
41 } DirtyMemoryBlocks;
42
43 typedef struct RAMList {
44 QemuMutex mutex;
45 RAMBlock *mru_block;
46 /* RCU-enabled, writes protected by the ramlist lock. */
47 QLIST_HEAD(, RAMBlock) blocks;
48 DirtyMemoryBlocks *dirty_memory[DIRTY_MEMORY_NUM];
49 unsigned int num_dirty_blocks;
50 uint32_t version;
51 QLIST_HEAD(, RAMBlockNotifier) ramblock_notifiers;
52 } RAMList;
53 extern RAMList ram_list;
54
55 /* Should be holding either ram_list.mutex, or the RCU lock. */
56 #define INTERNAL_RAMBLOCK_FOREACH(block) \
57 QLIST_FOREACH_RCU(block, &ram_list.blocks, next)
58 /* Never use the INTERNAL_ version except for defining other macros */
59 #define RAMBLOCK_FOREACH(block) INTERNAL_RAMBLOCK_FOREACH(block)
60
61 void qemu_mutex_lock_ramlist(void);
62 void qemu_mutex_unlock_ramlist(void);
63
64 struct RAMBlockNotifier {
65 void (*ram_block_added)(RAMBlockNotifier *n, void *host, size_t size,
66 size_t max_size);
67 void (*ram_block_removed)(RAMBlockNotifier *n, void *host, size_t size,
68 size_t max_size);
69 void (*ram_block_resized)(RAMBlockNotifier *n, void *host, size_t old_size,
70 size_t new_size);
71 QLIST_ENTRY(RAMBlockNotifier) next;
72 };
73
74 typedef int (RAMBlockIterFunc)(RAMBlock *rb, void *opaque);
75
76 int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque);
77
78 void ram_block_notifier_add(RAMBlockNotifier *n);
79 void ram_block_notifier_remove(RAMBlockNotifier *n);
80 void ram_block_notify_add(void *host, size_t size, size_t max_size);
81 void ram_block_notify_remove(void *host, size_t size, size_t max_size);
82 void ram_block_notify_resize(void *host, size_t old_size, size_t new_size);
83
84 GString *ram_block_format(void);
85
86 #endif /* RAMLIST_H */