2 * Declarations for cpu physical memory functions
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
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.
22 #ifndef CONFIG_USER_ONLY
23 #include "hw/xen/xen.h"
24 #include "exec/ramlist.h"
28 struct MemoryRegion
*mr
;
31 ram_addr_t used_length
;
32 ram_addr_t max_length
;
33 void (*resized
)(const char*, uint64_t length
, void *host
);
35 /* Protected by iothread lock. */
37 /* RCU-enabled, writes protected by the ramlist lock */
38 QLIST_ENTRY(RAMBlock
) next
;
39 QLIST_HEAD(, RAMBlockNotifier
) ramblock_notifiers
;
42 /* dirty bitmap used during migration */
44 /* bitmap of pages that haven't been sent even once
45 * only maintained and used in postcopy at the moment
46 * where it's used to send the dirtymap at the start
47 * of the postcopy phase
49 unsigned long *unsentmap
;
50 /* bitmap of already received pages in postcopy */
51 unsigned long *receivedmap
;
54 static inline bool offset_in_ramblock(RAMBlock
*b
, ram_addr_t offset
)
56 return (b
&& b
->host
&& offset
< b
->used_length
) ? true : false;
59 static inline void *ramblock_ptr(RAMBlock
*block
, ram_addr_t offset
)
61 assert(offset_in_ramblock(block
, offset
));
62 return (char *)block
->host
+ offset
;
65 static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr
,
68 uint64_t host_addr_offset
=
69 (uint64_t)(uintptr_t)(host_addr
- (void *)rb
->host
);
70 return host_addr_offset
>> TARGET_PAGE_BITS
;
73 long qemu_getrampagesize(void);
74 RAMBlock
*qemu_ram_alloc_from_file(ram_addr_t size
, MemoryRegion
*mr
,
75 bool share
, const char *mem_path
,
77 RAMBlock
*qemu_ram_alloc_from_fd(ram_addr_t size
, MemoryRegion
*mr
,
80 RAMBlock
*qemu_ram_alloc_from_ptr(ram_addr_t size
, void *host
,
81 MemoryRegion
*mr
, Error
**errp
);
82 RAMBlock
*qemu_ram_alloc(ram_addr_t size
, bool share
, MemoryRegion
*mr
,
84 RAMBlock
*qemu_ram_alloc_resizeable(ram_addr_t size
, ram_addr_t max_size
,
85 void (*resized
)(const char*,
88 MemoryRegion
*mr
, Error
**errp
);
89 void qemu_ram_free(RAMBlock
*block
);
91 int qemu_ram_resize(RAMBlock
*block
, ram_addr_t newsize
, Error
**errp
);
93 #define DIRTY_CLIENTS_ALL ((1 << DIRTY_MEMORY_NUM) - 1)
94 #define DIRTY_CLIENTS_NOCODE (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
96 void tb_invalidate_phys_range(ram_addr_t start
, ram_addr_t end
);
98 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start
,
102 DirtyMemoryBlocks
*blocks
;
103 unsigned long end
, page
;
104 unsigned long idx
, offset
, base
;
107 assert(client
< DIRTY_MEMORY_NUM
);
109 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
110 page
= start
>> TARGET_PAGE_BITS
;
114 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
116 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
117 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
118 base
= page
- offset
;
120 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
121 unsigned long num
= next
- base
;
122 unsigned long found
= find_next_bit(blocks
->blocks
[idx
], num
, offset
);
131 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
139 static inline bool cpu_physical_memory_all_dirty(ram_addr_t start
,
143 DirtyMemoryBlocks
*blocks
;
144 unsigned long end
, page
;
145 unsigned long idx
, offset
, base
;
148 assert(client
< DIRTY_MEMORY_NUM
);
150 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
151 page
= start
>> TARGET_PAGE_BITS
;
155 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
157 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
158 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
159 base
= page
- offset
;
161 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
162 unsigned long num
= next
- base
;
163 unsigned long found
= find_next_zero_bit(blocks
->blocks
[idx
], num
, offset
);
172 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
180 static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr
,
183 return cpu_physical_memory_get_dirty(addr
, 1, client
);
186 static inline bool cpu_physical_memory_is_clean(ram_addr_t addr
)
188 bool vga
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_VGA
);
189 bool code
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_CODE
);
191 cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_MIGRATION
);
192 return !(vga
&& code
&& migration
);
195 static inline uint8_t cpu_physical_memory_range_includes_clean(ram_addr_t start
,
201 if (mask
& (1 << DIRTY_MEMORY_VGA
) &&
202 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_VGA
)) {
203 ret
|= (1 << DIRTY_MEMORY_VGA
);
205 if (mask
& (1 << DIRTY_MEMORY_CODE
) &&
206 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_CODE
)) {
207 ret
|= (1 << DIRTY_MEMORY_CODE
);
209 if (mask
& (1 << DIRTY_MEMORY_MIGRATION
) &&
210 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
)) {
211 ret
|= (1 << DIRTY_MEMORY_MIGRATION
);
216 static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr
,
219 unsigned long page
, idx
, offset
;
220 DirtyMemoryBlocks
*blocks
;
222 assert(client
< DIRTY_MEMORY_NUM
);
224 page
= addr
>> TARGET_PAGE_BITS
;
225 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
226 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
230 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
232 set_bit_atomic(offset
, blocks
->blocks
[idx
]);
237 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start
,
241 DirtyMemoryBlocks
*blocks
[DIRTY_MEMORY_NUM
];
242 unsigned long end
, page
;
243 unsigned long idx
, offset
, base
;
246 if (!mask
&& !xen_enabled()) {
250 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
251 page
= start
>> TARGET_PAGE_BITS
;
255 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
256 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
]);
259 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
260 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
261 base
= page
- offset
;
263 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
265 if (likely(mask
& (1 << DIRTY_MEMORY_MIGRATION
))) {
266 bitmap_set_atomic(blocks
[DIRTY_MEMORY_MIGRATION
]->blocks
[idx
],
267 offset
, next
- page
);
269 if (unlikely(mask
& (1 << DIRTY_MEMORY_VGA
))) {
270 bitmap_set_atomic(blocks
[DIRTY_MEMORY_VGA
]->blocks
[idx
],
271 offset
, next
- page
);
273 if (unlikely(mask
& (1 << DIRTY_MEMORY_CODE
))) {
274 bitmap_set_atomic(blocks
[DIRTY_MEMORY_CODE
]->blocks
[idx
],
275 offset
, next
- page
);
281 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
286 xen_hvm_modified_memory(start
, length
);
290 static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap
,
295 unsigned long page_number
, c
;
298 unsigned long len
= (pages
+ HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
299 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
300 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
302 /* start address is aligned at the start of a word? */
303 if ((((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) &&
305 unsigned long **blocks
[DIRTY_MEMORY_NUM
];
307 unsigned long offset
;
309 long nr
= BITS_TO_LONGS(pages
);
311 idx
= (start
>> TARGET_PAGE_BITS
) / DIRTY_MEMORY_BLOCK_SIZE
;
312 offset
= BIT_WORD((start
>> TARGET_PAGE_BITS
) %
313 DIRTY_MEMORY_BLOCK_SIZE
);
317 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
318 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
])->blocks
;
321 for (k
= 0; k
< nr
; k
++) {
323 unsigned long temp
= leul_to_cpu(bitmap
[k
]);
325 atomic_or(&blocks
[DIRTY_MEMORY_MIGRATION
][idx
][offset
], temp
);
326 atomic_or(&blocks
[DIRTY_MEMORY_VGA
][idx
][offset
], temp
);
328 atomic_or(&blocks
[DIRTY_MEMORY_CODE
][idx
][offset
], temp
);
332 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
340 xen_hvm_modified_memory(start
, pages
<< TARGET_PAGE_BITS
);
342 uint8_t clients
= tcg_enabled() ? DIRTY_CLIENTS_ALL
: DIRTY_CLIENTS_NOCODE
;
344 * bitmap-traveling is faster than memory-traveling (for addr...)
345 * especially when most of the memory is not dirty.
347 for (i
= 0; i
< len
; i
++) {
348 if (bitmap
[i
] != 0) {
349 c
= leul_to_cpu(bitmap
[i
]);
353 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
354 addr
= page_number
* TARGET_PAGE_SIZE
;
355 ram_addr
= start
+ addr
;
356 cpu_physical_memory_set_dirty_range(ram_addr
,
357 TARGET_PAGE_SIZE
* hpratio
, clients
);
363 #endif /* not _WIN32 */
365 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start
,
369 DirtyBitmapSnapshot
*cpu_physical_memory_snapshot_and_clear_dirty
370 (ram_addr_t start
, ram_addr_t length
, unsigned client
);
372 bool cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot
*snap
,
376 static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start
,
379 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
);
380 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_VGA
);
381 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_CODE
);
386 uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBlock
*rb
,
389 uint64_t *real_dirty_pages
)
392 unsigned long word
= BIT_WORD((start
+ rb
->offset
) >> TARGET_PAGE_BITS
);
393 uint64_t num_dirty
= 0;
394 unsigned long *dest
= rb
->bmap
;
396 /* start address and length is aligned at the start of a word? */
397 if (((word
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) ==
398 (start
+ rb
->offset
) &&
399 !(length
& ((BITS_PER_LONG
<< TARGET_PAGE_BITS
) - 1))) {
401 int nr
= BITS_TO_LONGS(length
>> TARGET_PAGE_BITS
);
402 unsigned long * const *src
;
403 unsigned long idx
= (word
* BITS_PER_LONG
) / DIRTY_MEMORY_BLOCK_SIZE
;
404 unsigned long offset
= BIT_WORD((word
* BITS_PER_LONG
) %
405 DIRTY_MEMORY_BLOCK_SIZE
);
406 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
410 src
= atomic_rcu_read(
411 &ram_list
.dirty_memory
[DIRTY_MEMORY_MIGRATION
])->blocks
;
413 for (k
= page
; k
< page
+ nr
; k
++) {
414 if (src
[idx
][offset
]) {
415 unsigned long bits
= atomic_xchg(&src
[idx
][offset
], 0);
416 unsigned long new_dirty
;
417 *real_dirty_pages
+= ctpopl(bits
);
418 new_dirty
= ~dest
[k
];
421 num_dirty
+= ctpopl(new_dirty
);
424 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
432 ram_addr_t offset
= rb
->offset
;
434 for (addr
= 0; addr
< length
; addr
+= TARGET_PAGE_SIZE
) {
435 if (cpu_physical_memory_test_and_clear_dirty(
436 start
+ addr
+ offset
,
438 DIRTY_MEMORY_MIGRATION
)) {
439 *real_dirty_pages
+= 1;
440 long k
= (start
+ addr
) >> TARGET_PAGE_BITS
;
441 if (!test_and_set_bit(k
, dest
)) {