ramlist.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #ifndef RAMLIST_H
  2. #define RAMLIST_H
  3. #include "qemu/queue.h"
  4. #include "qemu/thread.h"
  5. #include "qemu/rcu.h"
  6. #include "qemu/rcu_queue.h"
  7. typedef struct RAMBlockNotifier RAMBlockNotifier;
  8. #define DIRTY_MEMORY_VGA 0
  9. #define DIRTY_MEMORY_CODE 1
  10. #define DIRTY_MEMORY_MIGRATION 2
  11. #define DIRTY_MEMORY_NUM 3 /* num of dirty bits */
  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. typedef struct RAMList {
  43. QemuMutex mutex;
  44. RAMBlock *mru_block;
  45. /* RCU-enabled, writes protected by the ramlist lock. */
  46. QLIST_HEAD(, RAMBlock) blocks;
  47. DirtyMemoryBlocks *dirty_memory[DIRTY_MEMORY_NUM];
  48. unsigned int num_dirty_blocks;
  49. uint32_t version;
  50. QLIST_HEAD(, RAMBlockNotifier) ramblock_notifiers;
  51. } RAMList;
  52. extern RAMList ram_list;
  53. /* Should be holding either ram_list.mutex, or the RCU lock. */
  54. #define INTERNAL_RAMBLOCK_FOREACH(block) \
  55. QLIST_FOREACH_RCU(block, &ram_list.blocks, next)
  56. /* Never use the INTERNAL_ version except for defining other macros */
  57. #define RAMBLOCK_FOREACH(block) INTERNAL_RAMBLOCK_FOREACH(block)
  58. void qemu_mutex_lock_ramlist(void);
  59. void qemu_mutex_unlock_ramlist(void);
  60. struct RAMBlockNotifier {
  61. void (*ram_block_added)(RAMBlockNotifier *n, void *host, size_t size,
  62. size_t max_size);
  63. void (*ram_block_removed)(RAMBlockNotifier *n, void *host, size_t size,
  64. size_t max_size);
  65. void (*ram_block_resized)(RAMBlockNotifier *n, void *host, size_t old_size,
  66. size_t new_size);
  67. QLIST_ENTRY(RAMBlockNotifier) next;
  68. };
  69. void ram_block_notifier_add(RAMBlockNotifier *n);
  70. void ram_block_notifier_remove(RAMBlockNotifier *n);
  71. void ram_block_notify_add(void *host, size_t size, size_t max_size);
  72. void ram_block_notify_remove(void *host, size_t size, size_t max_size);
  73. void ram_block_notify_resize(void *host, size_t old_size, size_t new_size);
  74. GString *ram_block_format(void);
  75. #endif /* RAMLIST_H */