| 1 | /* |
| 2 | * Declarations for cpu physical memory functions |
| 3 | * |
| 4 | * Copyright 2011 Red Hat, Inc. and/or its affiliates |
| 5 | * |
| 6 | * Authors: |
| 7 | * Avi Kivity <avi@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or |
| 10 | * later. See the COPYING file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #ifndef SYSTEM_RAMBLOCK_H |
| 15 | #define SYSTEM_RAMBLOCK_H |
| 16 | |
| 17 | #include "qemu/rcu.h" |
| 18 | #include "system/ram_addr.h" |
| 19 | #include "system/ramlist.h" |
| 20 | #include "system/hostmem.h" |
| 21 | |
| 22 | #define TYPE_RAM_BLOCK_ATTRIBUTES "ram-block-attributes" |
| 23 | OBJECT_DECLARE_SIMPLE_TYPE(RamBlockAttributes, RAM_BLOCK_ATTRIBUTES) |
| 24 | |
| 25 | struct RAMBlock { |
| 26 | struct rcu_head rcu; |
| 27 | struct MemoryRegion *mr; |
| 28 | uint8_t *host; |
| 29 | uint8_t *colo_cache; /* For colo, VM's ram cache */ |
| 30 | ram_addr_t offset; |
| 31 | ram_addr_t used_length; |
| 32 | ram_addr_t max_length; |
| 33 | void (*resized)(const char*, uint64_t length, void *host); |
| 34 | uint32_t flags; |
| 35 | /* Protected by the BQL. */ |
| 36 | char idstr[256]; |
| 37 | /* RCU-enabled, writes protected by the ramlist lock */ |
| 38 | QLIST_ENTRY(RAMBlock) next; |
| 39 | QLIST_HEAD(, RAMBlockNotifier) ramblock_notifiers; |
| 40 | Error *cpr_blocker; |
| 41 | int fd; |
| 42 | uint64_t fd_offset; |
| 43 | int guest_memfd; |
| 44 | RamBlockAttributes *attributes; |
| 45 | size_t page_size; |
| 46 | /* dirty bitmap used during migration */ |
| 47 | unsigned long *bmap; |
| 48 | |
| 49 | /* |
| 50 | * Below fields are only used by mapped-ram migration |
| 51 | */ |
| 52 | /* bitmap of pages present in the migration file */ |
| 53 | unsigned long *file_bmap; |
| 54 | /* |
| 55 | * offset in the file pages belonging to this ramblock are saved, |
| 56 | * used only during migration to a file. |
| 57 | */ |
| 58 | off_t bitmap_offset; |
| 59 | uint64_t pages_offset; |
| 60 | |
| 61 | /* Bitmap of already received pages. Only used on destination side. */ |
| 62 | unsigned long *receivedmap; |
| 63 | /* |
| 64 | * Bitmap for pages that are yet to be read from disk. It is required for |
| 65 | * fault thread and eager thread to keep note of which pages are currently |
| 66 | * being read. Used by fast snapshot load. |
| 67 | */ |
| 68 | unsigned long *pending_bmap; |
| 69 | |
| 70 | /* |
| 71 | * bitmap to track already cleared dirty bitmap. When the bit is |
| 72 | * set, it means the corresponding memory chunk needs a log-clear. |
| 73 | * Set this up to non-NULL to enable the capability to postpone |
| 74 | * and split clearing of dirty bitmap on the remote node (e.g., |
| 75 | * KVM). The bitmap will be set only when doing global sync. |
| 76 | * |
| 77 | * It is only used during src side of ram migration, and it is |
| 78 | * protected by the global ram_state.bitmap_mutex. |
| 79 | * |
| 80 | * NOTE: this bitmap is different comparing to the other bitmaps |
| 81 | * in that one bit can represent multiple guest pages (which is |
| 82 | * decided by the `clear_bmap_shift' variable below). On |
| 83 | * destination side, this should always be NULL, and the variable |
| 84 | * `clear_bmap_shift' is meaningless. |
| 85 | */ |
| 86 | unsigned long *clear_bmap; |
| 87 | uint8_t clear_bmap_shift; |
| 88 | |
| 89 | /* |
| 90 | * RAM block length that corresponds to the used_length on the migration |
| 91 | * source (after RAM block sizes were synchronized). Especially, after |
| 92 | * starting to run the guest, used_length and postcopy_length can differ. |
| 93 | * Used to register/unregister uffd handlers and as the size of the received |
| 94 | * bitmap. Receiving any page beyond this length will bail out, as it |
| 95 | * could not have been valid on the source. |
| 96 | */ |
| 97 | ram_addr_t postcopy_length; |
| 98 | }; |
| 99 | |
| 100 | struct RamBlockAttributes { |
| 101 | Object parent; |
| 102 | |
| 103 | RAMBlock *ram_block; |
| 104 | |
| 105 | /* 1-setting of the bitmap represents ram is populated (shared) */ |
| 106 | unsigned bitmap_size; |
| 107 | unsigned long *bitmap; |
| 108 | }; |
| 109 | |
| 110 | /* @offset: the offset within the RAMBlock */ |
| 111 | int ram_block_discard_range(RAMBlock *rb, uint64_t offset, size_t length); |
| 112 | int ram_block_discard_shared_range(RAMBlock *rb, uint64_t offset, |
| 113 | size_t length); |
| 114 | int ram_block_discard_guest_memfd_range(RAMBlock *rb, uint64_t offset, |
| 115 | size_t length); |
| 116 | |
| 117 | RamBlockAttributes *ram_block_attributes_create(RAMBlock *ram_block); |
| 118 | void ram_block_attributes_destroy(RamBlockAttributes *attr); |
| 119 | int ram_block_attributes_state_change(RamBlockAttributes *attr, uint64_t offset, |
| 120 | uint64_t size, bool to_discard); |
| 121 | |
| 122 | /** |
| 123 | * ram_block_is_pmem: Whether the RAM block is of persistent memory |
| 124 | */ |
| 125 | bool ram_block_is_pmem(RAMBlock *rb); |
| 126 | |
| 127 | static inline bool offset_in_ramblock(RAMBlock *b, ram_addr_t offset) |
| 128 | { |
| 129 | return b && b->host && (offset < b->used_length); |
| 130 | } |
| 131 | |
| 132 | static inline void *ramblock_ptr(RAMBlock *block, ram_addr_t offset) |
| 133 | { |
| 134 | assert(offset_in_ramblock(block, offset)); |
| 135 | return (char *)block->host + offset; |
| 136 | } |
| 137 | |
| 138 | /* memory API */ |
| 139 | |
| 140 | void qemu_ram_remap(ram_addr_t addr); |
| 141 | /* This should not be used by devices. */ |
| 142 | ram_addr_t qemu_ram_addr_from_host(void *ptr); |
| 143 | ram_addr_t qemu_ram_addr_from_host_nofail(void *ptr); |
| 144 | RAMBlock *qemu_ram_block_by_name(const char *name); |
| 145 | |
| 146 | /* |
| 147 | * Translates a host ptr back to a RAMBlock and an offset in that RAMBlock. |
| 148 | * |
| 149 | * @ptr: The host pointer to translate. |
| 150 | * @round_offset: Whether to round the result offset down to a target page |
| 151 | * @offset: Will be set to the offset within the returned RAMBlock. |
| 152 | * |
| 153 | * Returns: RAMBlock (or NULL if not found) |
| 154 | * |
| 155 | * By the time this function returns, the returned pointer is not protected |
| 156 | * by RCU anymore. If the caller is not within an RCU critical section and |
| 157 | * does not hold the BQL, it must have other means of protecting the |
| 158 | * pointer, such as a reference to the memory region that owns the RAMBlock. |
| 159 | */ |
| 160 | RAMBlock *qemu_ram_block_from_host(void *ptr, bool round_offset, |
| 161 | ram_addr_t *offset); |
| 162 | ram_addr_t qemu_ram_block_host_offset(const RAMBlock *rb, void *host); |
| 163 | void qemu_ram_set_idstr(RAMBlock *block, const char *name, DeviceState *dev); |
| 164 | void qemu_ram_unset_idstr(RAMBlock *block); |
| 165 | const char *qemu_ram_get_idstr(const RAMBlock *rb); |
| 166 | void *qemu_ram_get_host_addr(const RAMBlock *rb); |
| 167 | ram_addr_t qemu_ram_get_offset(const RAMBlock *rb); |
| 168 | ram_addr_t qemu_ram_get_fd_offset(const RAMBlock *rb); |
| 169 | ram_addr_t qemu_ram_get_used_length(const RAMBlock *rb); |
| 170 | ram_addr_t qemu_ram_get_max_length(const RAMBlock *rb); |
| 171 | bool qemu_ram_is_shared(const RAMBlock *rb); |
| 172 | bool qemu_ram_is_noreserve(const RAMBlock *rb); |
| 173 | bool qemu_ram_is_uf_zeroable(const RAMBlock *rb); |
| 174 | void qemu_ram_set_uf_zeroable(RAMBlock *rb); |
| 175 | bool qemu_ram_is_migratable(const RAMBlock *rb); |
| 176 | void qemu_ram_set_migratable(RAMBlock *rb); |
| 177 | void qemu_ram_unset_migratable(RAMBlock *rb); |
| 178 | bool qemu_ram_is_named_file(const RAMBlock *rb); |
| 179 | int qemu_ram_get_fd(const RAMBlock *rb); |
| 180 | |
| 181 | size_t qemu_ram_pagesize(const RAMBlock *block); |
| 182 | size_t qemu_ram_pagesize_largest(void); |
| 183 | #include "exec/target_page.h" |
| 184 | #include "exec/hwaddr.h" |
| 185 | |
| 186 | extern uint64_t total_dirty_pages; |
| 187 | |
| 188 | /** |
| 189 | * clear_bmap_size: calculate clear bitmap size |
| 190 | * |
| 191 | * @pages: number of guest pages |
| 192 | * @shift: guest page number shift |
| 193 | * |
| 194 | * Returns: number of bits for the clear bitmap |
| 195 | */ |
| 196 | static inline long clear_bmap_size(uint64_t pages, uint8_t shift) |
| 197 | { |
| 198 | return DIV_ROUND_UP(pages, 1UL << shift); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * clear_bmap_set: set clear bitmap for the page range. Must be with |
| 203 | * bitmap_mutex held. |
| 204 | * |
| 205 | * @rb: the ramblock to operate on |
| 206 | * @start: the start page number |
| 207 | * @size: number of pages to set in the bitmap |
| 208 | * |
| 209 | * Returns: None |
| 210 | */ |
| 211 | static inline void clear_bmap_set(RAMBlock *rb, uint64_t start, |
| 212 | uint64_t npages) |
| 213 | { |
| 214 | uint8_t shift = rb->clear_bmap_shift; |
| 215 | |
| 216 | bitmap_set(rb->clear_bmap, start >> shift, clear_bmap_size(npages, shift)); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * clear_bmap_test_and_clear: test clear bitmap for the page, clear if set. |
| 221 | * Must be with bitmap_mutex held. |
| 222 | * |
| 223 | * @rb: the ramblock to operate on |
| 224 | * @page: the page number to check |
| 225 | * |
| 226 | * Returns: true if the bit was set, false otherwise |
| 227 | */ |
| 228 | static inline bool clear_bmap_test_and_clear(RAMBlock *rb, uint64_t page) |
| 229 | { |
| 230 | uint8_t shift = rb->clear_bmap_shift; |
| 231 | |
| 232 | return bitmap_test_and_clear(rb->clear_bmap, page >> shift, 1); |
| 233 | } |
| 234 | |
| 235 | static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr, |
| 236 | RAMBlock *rb) |
| 237 | { |
| 238 | uint64_t host_addr_offset = |
| 239 | (uint64_t)(uintptr_t)(host_addr - (void *)rb->host); |
| 240 | return host_addr_offset >> TARGET_PAGE_BITS; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * qemu_ram_alloc_from_file, |
| 245 | * qemu_ram_alloc_from_fd: Allocate a ram block from the specified backing |
| 246 | * file or device |
| 247 | * |
| 248 | * Parameters: |
| 249 | * @size: the size in bytes of the ram block |
| 250 | * @max_size: the maximum size of the block after resizing |
| 251 | * @mr: the memory region where the ram block is |
| 252 | * @resized: callback after calls to qemu_ram_resize |
| 253 | * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM, |
| 254 | * RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY, |
| 255 | * RAM_READONLY_FD, RAM_GUEST_MEMFD |
| 256 | * @mem_path or @fd: specify the backing file or device |
| 257 | * @offset: Offset into target file |
| 258 | * @grow: extend file if necessary (but an empty file is always extended). |
| 259 | * @errp: pointer to Error*, to store an error if it happens |
| 260 | * |
| 261 | * Return: |
| 262 | * On success, return a pointer to the ram block. |
| 263 | * On failure, return NULL. |
| 264 | */ |
| 265 | typedef void (*qemu_ram_resize_cb)(const char *, uint64_t length, void *host); |
| 266 | |
| 267 | RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr, |
| 268 | uint32_t ram_flags, const char *mem_path, |
| 269 | off_t offset, Error **errp); |
| 270 | RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, ram_addr_t max_size, |
| 271 | qemu_ram_resize_cb resized, MemoryRegion *mr, |
| 272 | uint32_t ram_flags, int fd, off_t offset, |
| 273 | bool grow, |
| 274 | Error **errp); |
| 275 | |
| 276 | RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, |
| 277 | MemoryRegion *mr, Error **errp); |
| 278 | RAMBlock *qemu_ram_alloc(ram_addr_t size, uint32_t ram_flags, MemoryRegion *mr, |
| 279 | Error **errp); |
| 280 | RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t max_size, |
| 281 | qemu_ram_resize_cb resized, |
| 282 | MemoryRegion *mr, Error **errp); |
| 283 | void qemu_ram_free(RAMBlock *block); |
| 284 | |
| 285 | int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp); |
| 286 | |
| 287 | void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length); |
| 288 | |
| 289 | /* Clear whole block of mem */ |
| 290 | static inline void qemu_ram_block_writeback(RAMBlock *block) |
| 291 | { |
| 292 | qemu_ram_msync(block, 0, block->used_length); |
| 293 | } |
| 294 | #endif |