Merge remote-tracking branch 'remotes/palmer/tags/riscv-for-master-4.1-sf1-v3' into...
[qemu/ar7.git] / include / exec / ram_addr.h
blobf96777bb992198f1952d7c08849eceb2d3edf6dd
1 /*
2 * Declarations for cpu physical memory functions
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
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.
15 * This header is for use by exec.c and memory.c ONLY. Do not include it.
16 * The functions declared here will be removed soon.
19 #ifndef RAM_ADDR_H
20 #define RAM_ADDR_H
22 #ifndef CONFIG_USER_ONLY
23 #include "hw/xen/xen.h"
24 #include "sysemu/tcg.h"
25 #include "exec/ramlist.h"
27 struct RAMBlock {
28 struct rcu_head rcu;
29 struct MemoryRegion *mr;
30 uint8_t *host;
31 uint8_t *colo_cache; /* For colo, VM's ram cache */
32 ram_addr_t offset;
33 ram_addr_t used_length;
34 ram_addr_t max_length;
35 void (*resized)(const char*, uint64_t length, void *host);
36 uint32_t flags;
37 /* Protected by iothread lock. */
38 char idstr[256];
39 /* RCU-enabled, writes protected by the ramlist lock */
40 QLIST_ENTRY(RAMBlock) next;
41 QLIST_HEAD(, RAMBlockNotifier) ramblock_notifiers;
42 int fd;
43 size_t page_size;
44 /* dirty bitmap used during migration */
45 unsigned long *bmap;
46 /* bitmap of pages that haven't been sent even once
47 * only maintained and used in postcopy at the moment
48 * where it's used to send the dirtymap at the start
49 * of the postcopy phase
51 unsigned long *unsentmap;
52 /* bitmap of already received pages in postcopy */
53 unsigned long *receivedmap;
56 static inline bool offset_in_ramblock(RAMBlock *b, ram_addr_t offset)
58 return (b && b->host && offset < b->used_length) ? true : false;
61 static inline void *ramblock_ptr(RAMBlock *block, ram_addr_t offset)
63 assert(offset_in_ramblock(block, offset));
64 return (char *)block->host + offset;
67 static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr,
68 RAMBlock *rb)
70 uint64_t host_addr_offset =
71 (uint64_t)(uintptr_t)(host_addr - (void *)rb->host);
72 return host_addr_offset >> TARGET_PAGE_BITS;
75 bool ramblock_is_pmem(RAMBlock *rb);
77 long qemu_minrampagesize(void);
78 long qemu_maxrampagesize(void);
80 /**
81 * qemu_ram_alloc_from_file,
82 * qemu_ram_alloc_from_fd: Allocate a ram block from the specified backing
83 * file or device
85 * Parameters:
86 * @size: the size in bytes of the ram block
87 * @mr: the memory region where the ram block is
88 * @ram_flags: specify the properties of the ram block, which can be one
89 * or bit-or of following values
90 * - RAM_SHARED: mmap the backing file or device with MAP_SHARED
91 * - RAM_PMEM: the backend @mem_path or @fd is persistent memory
92 * Other bits are ignored.
93 * @mem_path or @fd: specify the backing file or device
94 * @errp: pointer to Error*, to store an error if it happens
96 * Return:
97 * On success, return a pointer to the ram block.
98 * On failure, return NULL.
100 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
101 uint32_t ram_flags, const char *mem_path,
102 Error **errp);
103 RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
104 uint32_t ram_flags, int fd,
105 Error **errp);
107 RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
108 MemoryRegion *mr, Error **errp);
109 RAMBlock *qemu_ram_alloc(ram_addr_t size, bool share, MemoryRegion *mr,
110 Error **errp);
111 RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t max_size,
112 void (*resized)(const char*,
113 uint64_t length,
114 void *host),
115 MemoryRegion *mr, Error **errp);
116 void qemu_ram_free(RAMBlock *block);
118 int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp);
120 #define DIRTY_CLIENTS_ALL ((1 << DIRTY_MEMORY_NUM) - 1)
121 #define DIRTY_CLIENTS_NOCODE (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
123 void tb_invalidate_phys_range(ram_addr_t start, ram_addr_t end);
125 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start,
126 ram_addr_t length,
127 unsigned client)
129 DirtyMemoryBlocks *blocks;
130 unsigned long end, page;
131 unsigned long idx, offset, base;
132 bool dirty = false;
134 assert(client < DIRTY_MEMORY_NUM);
136 end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
137 page = start >> TARGET_PAGE_BITS;
139 rcu_read_lock();
141 blocks = atomic_rcu_read(&ram_list.dirty_memory[client]);
143 idx = page / DIRTY_MEMORY_BLOCK_SIZE;
144 offset = page % DIRTY_MEMORY_BLOCK_SIZE;
145 base = page - offset;
146 while (page < end) {
147 unsigned long next = MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE);
148 unsigned long num = next - base;
149 unsigned long found = find_next_bit(blocks->blocks[idx], num, offset);
150 if (found < num) {
151 dirty = true;
152 break;
155 page = next;
156 idx++;
157 offset = 0;
158 base += DIRTY_MEMORY_BLOCK_SIZE;
161 rcu_read_unlock();
163 return dirty;
166 static inline bool cpu_physical_memory_all_dirty(ram_addr_t start,
167 ram_addr_t length,
168 unsigned client)
170 DirtyMemoryBlocks *blocks;
171 unsigned long end, page;
172 unsigned long idx, offset, base;
173 bool dirty = true;
175 assert(client < DIRTY_MEMORY_NUM);
177 end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
178 page = start >> TARGET_PAGE_BITS;
180 rcu_read_lock();
182 blocks = atomic_rcu_read(&ram_list.dirty_memory[client]);
184 idx = page / DIRTY_MEMORY_BLOCK_SIZE;
185 offset = page % DIRTY_MEMORY_BLOCK_SIZE;
186 base = page - offset;
187 while (page < end) {
188 unsigned long next = MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE);
189 unsigned long num = next - base;
190 unsigned long found = find_next_zero_bit(blocks->blocks[idx], num, offset);
191 if (found < num) {
192 dirty = false;
193 break;
196 page = next;
197 idx++;
198 offset = 0;
199 base += DIRTY_MEMORY_BLOCK_SIZE;
202 rcu_read_unlock();
204 return dirty;
207 static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr,
208 unsigned client)
210 return cpu_physical_memory_get_dirty(addr, 1, client);
213 static inline bool cpu_physical_memory_is_clean(ram_addr_t addr)
215 bool vga = cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_VGA);
216 bool code = cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_CODE);
217 bool migration =
218 cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_MIGRATION);
219 return !(vga && code && migration);
222 static inline uint8_t cpu_physical_memory_range_includes_clean(ram_addr_t start,
223 ram_addr_t length,
224 uint8_t mask)
226 uint8_t ret = 0;
228 if (mask & (1 << DIRTY_MEMORY_VGA) &&
229 !cpu_physical_memory_all_dirty(start, length, DIRTY_MEMORY_VGA)) {
230 ret |= (1 << DIRTY_MEMORY_VGA);
232 if (mask & (1 << DIRTY_MEMORY_CODE) &&
233 !cpu_physical_memory_all_dirty(start, length, DIRTY_MEMORY_CODE)) {
234 ret |= (1 << DIRTY_MEMORY_CODE);
236 if (mask & (1 << DIRTY_MEMORY_MIGRATION) &&
237 !cpu_physical_memory_all_dirty(start, length, DIRTY_MEMORY_MIGRATION)) {
238 ret |= (1 << DIRTY_MEMORY_MIGRATION);
240 return ret;
243 static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr,
244 unsigned client)
246 unsigned long page, idx, offset;
247 DirtyMemoryBlocks *blocks;
249 assert(client < DIRTY_MEMORY_NUM);
251 page = addr >> TARGET_PAGE_BITS;
252 idx = page / DIRTY_MEMORY_BLOCK_SIZE;
253 offset = page % DIRTY_MEMORY_BLOCK_SIZE;
255 rcu_read_lock();
257 blocks = atomic_rcu_read(&ram_list.dirty_memory[client]);
259 set_bit_atomic(offset, blocks->blocks[idx]);
261 rcu_read_unlock();
264 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start,
265 ram_addr_t length,
266 uint8_t mask)
268 DirtyMemoryBlocks *blocks[DIRTY_MEMORY_NUM];
269 unsigned long end, page;
270 unsigned long idx, offset, base;
271 int i;
273 if (!mask && !xen_enabled()) {
274 return;
277 end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
278 page = start >> TARGET_PAGE_BITS;
280 rcu_read_lock();
282 for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
283 blocks[i] = atomic_rcu_read(&ram_list.dirty_memory[i]);
286 idx = page / DIRTY_MEMORY_BLOCK_SIZE;
287 offset = page % DIRTY_MEMORY_BLOCK_SIZE;
288 base = page - offset;
289 while (page < end) {
290 unsigned long next = MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE);
292 if (likely(mask & (1 << DIRTY_MEMORY_MIGRATION))) {
293 bitmap_set_atomic(blocks[DIRTY_MEMORY_MIGRATION]->blocks[idx],
294 offset, next - page);
296 if (unlikely(mask & (1 << DIRTY_MEMORY_VGA))) {
297 bitmap_set_atomic(blocks[DIRTY_MEMORY_VGA]->blocks[idx],
298 offset, next - page);
300 if (unlikely(mask & (1 << DIRTY_MEMORY_CODE))) {
301 bitmap_set_atomic(blocks[DIRTY_MEMORY_CODE]->blocks[idx],
302 offset, next - page);
305 page = next;
306 idx++;
307 offset = 0;
308 base += DIRTY_MEMORY_BLOCK_SIZE;
311 rcu_read_unlock();
313 xen_hvm_modified_memory(start, length);
316 #if !defined(_WIN32)
317 static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap,
318 ram_addr_t start,
319 ram_addr_t pages)
321 unsigned long i, j;
322 unsigned long page_number, c;
323 hwaddr addr;
324 ram_addr_t ram_addr;
325 unsigned long len = (pages + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
326 unsigned long hpratio = getpagesize() / TARGET_PAGE_SIZE;
327 unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
329 /* start address is aligned at the start of a word? */
330 if ((((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) &&
331 (hpratio == 1)) {
332 unsigned long **blocks[DIRTY_MEMORY_NUM];
333 unsigned long idx;
334 unsigned long offset;
335 long k;
336 long nr = BITS_TO_LONGS(pages);
338 idx = (start >> TARGET_PAGE_BITS) / DIRTY_MEMORY_BLOCK_SIZE;
339 offset = BIT_WORD((start >> TARGET_PAGE_BITS) %
340 DIRTY_MEMORY_BLOCK_SIZE);
342 rcu_read_lock();
344 for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
345 blocks[i] = atomic_rcu_read(&ram_list.dirty_memory[i])->blocks;
348 for (k = 0; k < nr; k++) {
349 if (bitmap[k]) {
350 unsigned long temp = leul_to_cpu(bitmap[k]);
352 atomic_or(&blocks[DIRTY_MEMORY_MIGRATION][idx][offset], temp);
353 atomic_or(&blocks[DIRTY_MEMORY_VGA][idx][offset], temp);
354 if (tcg_enabled()) {
355 atomic_or(&blocks[DIRTY_MEMORY_CODE][idx][offset], temp);
359 if (++offset >= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE)) {
360 offset = 0;
361 idx++;
365 rcu_read_unlock();
367 xen_hvm_modified_memory(start, pages << TARGET_PAGE_BITS);
368 } else {
369 uint8_t clients = tcg_enabled() ? DIRTY_CLIENTS_ALL : DIRTY_CLIENTS_NOCODE;
371 * bitmap-traveling is faster than memory-traveling (for addr...)
372 * especially when most of the memory is not dirty.
374 for (i = 0; i < len; i++) {
375 if (bitmap[i] != 0) {
376 c = leul_to_cpu(bitmap[i]);
377 do {
378 j = ctzl(c);
379 c &= ~(1ul << j);
380 page_number = (i * HOST_LONG_BITS + j) * hpratio;
381 addr = page_number * TARGET_PAGE_SIZE;
382 ram_addr = start + addr;
383 cpu_physical_memory_set_dirty_range(ram_addr,
384 TARGET_PAGE_SIZE * hpratio, clients);
385 } while (c != 0);
390 #endif /* not _WIN32 */
392 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
393 ram_addr_t length,
394 unsigned client);
396 DirtyBitmapSnapshot *cpu_physical_memory_snapshot_and_clear_dirty
397 (ram_addr_t start, ram_addr_t length, unsigned client);
399 bool cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot *snap,
400 ram_addr_t start,
401 ram_addr_t length);
403 static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start,
404 ram_addr_t length)
406 cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_MIGRATION);
407 cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_VGA);
408 cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_CODE);
412 static inline
413 uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBlock *rb,
414 ram_addr_t start,
415 ram_addr_t length,
416 uint64_t *real_dirty_pages)
418 ram_addr_t addr;
419 unsigned long word = BIT_WORD((start + rb->offset) >> TARGET_PAGE_BITS);
420 uint64_t num_dirty = 0;
421 unsigned long *dest = rb->bmap;
423 /* start address and length is aligned at the start of a word? */
424 if (((word * BITS_PER_LONG) << TARGET_PAGE_BITS) ==
425 (start + rb->offset) &&
426 !(length & ((BITS_PER_LONG << TARGET_PAGE_BITS) - 1))) {
427 int k;
428 int nr = BITS_TO_LONGS(length >> TARGET_PAGE_BITS);
429 unsigned long * const *src;
430 unsigned long idx = (word * BITS_PER_LONG) / DIRTY_MEMORY_BLOCK_SIZE;
431 unsigned long offset = BIT_WORD((word * BITS_PER_LONG) %
432 DIRTY_MEMORY_BLOCK_SIZE);
433 unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
435 rcu_read_lock();
437 src = atomic_rcu_read(
438 &ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION])->blocks;
440 for (k = page; k < page + nr; k++) {
441 if (src[idx][offset]) {
442 unsigned long bits = atomic_xchg(&src[idx][offset], 0);
443 unsigned long new_dirty;
444 *real_dirty_pages += ctpopl(bits);
445 new_dirty = ~dest[k];
446 dest[k] |= bits;
447 new_dirty &= bits;
448 num_dirty += ctpopl(new_dirty);
451 if (++offset >= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE)) {
452 offset = 0;
453 idx++;
457 rcu_read_unlock();
458 } else {
459 ram_addr_t offset = rb->offset;
461 for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
462 if (cpu_physical_memory_test_and_clear_dirty(
463 start + addr + offset,
464 TARGET_PAGE_SIZE,
465 DIRTY_MEMORY_MIGRATION)) {
466 *real_dirty_pages += 1;
467 long k = (start + addr) >> TARGET_PAGE_BITS;
468 if (!test_and_set_bit(k, dest)) {
469 num_dirty++;
475 return num_dirty;
477 #endif
478 #endif