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 unsigned long last_ram_page(void);
75 RAMBlock
*qemu_ram_alloc_from_file(ram_addr_t size
, MemoryRegion
*mr
,
76 bool share
, const char *mem_path
,
78 RAMBlock
*qemu_ram_alloc_from_fd(ram_addr_t size
, MemoryRegion
*mr
,
81 RAMBlock
*qemu_ram_alloc_from_ptr(ram_addr_t size
, void *host
,
82 MemoryRegion
*mr
, Error
**errp
);
83 RAMBlock
*qemu_ram_alloc(ram_addr_t size
, MemoryRegion
*mr
, Error
**errp
);
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 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start
,
100 DirtyMemoryBlocks
*blocks
;
101 unsigned long end
, page
;
102 unsigned long idx
, offset
, base
;
105 assert(client
< DIRTY_MEMORY_NUM
);
107 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
108 page
= start
>> TARGET_PAGE_BITS
;
112 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
114 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
115 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
116 base
= page
- offset
;
118 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
119 unsigned long num
= next
- base
;
120 unsigned long found
= find_next_bit(blocks
->blocks
[idx
], num
, offset
);
129 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
137 static inline bool cpu_physical_memory_all_dirty(ram_addr_t start
,
141 DirtyMemoryBlocks
*blocks
;
142 unsigned long end
, page
;
143 unsigned long idx
, offset
, base
;
146 assert(client
< DIRTY_MEMORY_NUM
);
148 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
149 page
= start
>> TARGET_PAGE_BITS
;
153 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
155 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
156 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
157 base
= page
- offset
;
159 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
160 unsigned long num
= next
- base
;
161 unsigned long found
= find_next_zero_bit(blocks
->blocks
[idx
], num
, offset
);
170 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
178 static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr
,
181 return cpu_physical_memory_get_dirty(addr
, 1, client
);
184 static inline bool cpu_physical_memory_is_clean(ram_addr_t addr
)
186 bool vga
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_VGA
);
187 bool code
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_CODE
);
189 cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_MIGRATION
);
190 return !(vga
&& code
&& migration
);
193 static inline uint8_t cpu_physical_memory_range_includes_clean(ram_addr_t start
,
199 if (mask
& (1 << DIRTY_MEMORY_VGA
) &&
200 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_VGA
)) {
201 ret
|= (1 << DIRTY_MEMORY_VGA
);
203 if (mask
& (1 << DIRTY_MEMORY_CODE
) &&
204 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_CODE
)) {
205 ret
|= (1 << DIRTY_MEMORY_CODE
);
207 if (mask
& (1 << DIRTY_MEMORY_MIGRATION
) &&
208 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
)) {
209 ret
|= (1 << DIRTY_MEMORY_MIGRATION
);
214 static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr
,
217 unsigned long page
, idx
, offset
;
218 DirtyMemoryBlocks
*blocks
;
220 assert(client
< DIRTY_MEMORY_NUM
);
222 page
= addr
>> TARGET_PAGE_BITS
;
223 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
224 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
228 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
230 set_bit_atomic(offset
, blocks
->blocks
[idx
]);
235 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start
,
239 DirtyMemoryBlocks
*blocks
[DIRTY_MEMORY_NUM
];
240 unsigned long end
, page
;
241 unsigned long idx
, offset
, base
;
244 if (!mask
&& !xen_enabled()) {
248 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
249 page
= start
>> TARGET_PAGE_BITS
;
253 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
254 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
]);
257 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
258 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
259 base
= page
- offset
;
261 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
263 if (likely(mask
& (1 << DIRTY_MEMORY_MIGRATION
))) {
264 bitmap_set_atomic(blocks
[DIRTY_MEMORY_MIGRATION
]->blocks
[idx
],
265 offset
, next
- page
);
267 if (unlikely(mask
& (1 << DIRTY_MEMORY_VGA
))) {
268 bitmap_set_atomic(blocks
[DIRTY_MEMORY_VGA
]->blocks
[idx
],
269 offset
, next
- page
);
271 if (unlikely(mask
& (1 << DIRTY_MEMORY_CODE
))) {
272 bitmap_set_atomic(blocks
[DIRTY_MEMORY_CODE
]->blocks
[idx
],
273 offset
, next
- page
);
279 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
284 xen_hvm_modified_memory(start
, length
);
288 static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap
,
293 unsigned long page_number
, c
;
296 unsigned long len
= (pages
+ HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
297 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
298 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
300 /* start address is aligned at the start of a word? */
301 if ((((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) &&
303 unsigned long **blocks
[DIRTY_MEMORY_NUM
];
305 unsigned long offset
;
307 long nr
= BITS_TO_LONGS(pages
);
309 idx
= (start
>> TARGET_PAGE_BITS
) / DIRTY_MEMORY_BLOCK_SIZE
;
310 offset
= BIT_WORD((start
>> TARGET_PAGE_BITS
) %
311 DIRTY_MEMORY_BLOCK_SIZE
);
315 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
316 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
])->blocks
;
319 for (k
= 0; k
< nr
; k
++) {
321 unsigned long temp
= leul_to_cpu(bitmap
[k
]);
323 atomic_or(&blocks
[DIRTY_MEMORY_MIGRATION
][idx
][offset
], temp
);
324 atomic_or(&blocks
[DIRTY_MEMORY_VGA
][idx
][offset
], temp
);
326 atomic_or(&blocks
[DIRTY_MEMORY_CODE
][idx
][offset
], temp
);
330 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
338 xen_hvm_modified_memory(start
, pages
<< TARGET_PAGE_BITS
);
340 uint8_t clients
= tcg_enabled() ? DIRTY_CLIENTS_ALL
: DIRTY_CLIENTS_NOCODE
;
342 * bitmap-traveling is faster than memory-traveling (for addr...)
343 * especially when most of the memory is not dirty.
345 for (i
= 0; i
< len
; i
++) {
346 if (bitmap
[i
] != 0) {
347 c
= leul_to_cpu(bitmap
[i
]);
351 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
352 addr
= page_number
* TARGET_PAGE_SIZE
;
353 ram_addr
= start
+ addr
;
354 cpu_physical_memory_set_dirty_range(ram_addr
,
355 TARGET_PAGE_SIZE
* hpratio
, clients
);
361 #endif /* not _WIN32 */
363 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start
,
367 DirtyBitmapSnapshot
*cpu_physical_memory_snapshot_and_clear_dirty
368 (ram_addr_t start
, ram_addr_t length
, unsigned client
);
370 bool cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot
*snap
,
374 static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start
,
377 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
);
378 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_VGA
);
379 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_CODE
);
384 uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBlock
*rb
,
387 uint64_t *real_dirty_pages
)
390 unsigned long word
= BIT_WORD((start
+ rb
->offset
) >> TARGET_PAGE_BITS
);
391 uint64_t num_dirty
= 0;
392 unsigned long *dest
= rb
->bmap
;
394 /* start address is aligned at the start of a word? */
395 if (((word
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) ==
396 (start
+ rb
->offset
)) {
398 int nr
= BITS_TO_LONGS(length
>> TARGET_PAGE_BITS
);
399 unsigned long * const *src
;
400 unsigned long idx
= (word
* BITS_PER_LONG
) / DIRTY_MEMORY_BLOCK_SIZE
;
401 unsigned long offset
= BIT_WORD((word
* BITS_PER_LONG
) %
402 DIRTY_MEMORY_BLOCK_SIZE
);
403 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
407 src
= atomic_rcu_read(
408 &ram_list
.dirty_memory
[DIRTY_MEMORY_MIGRATION
])->blocks
;
410 for (k
= page
; k
< page
+ nr
; k
++) {
411 if (src
[idx
][offset
]) {
412 unsigned long bits
= atomic_xchg(&src
[idx
][offset
], 0);
413 unsigned long new_dirty
;
414 *real_dirty_pages
+= ctpopl(bits
);
415 new_dirty
= ~dest
[k
];
418 num_dirty
+= ctpopl(new_dirty
);
421 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
429 ram_addr_t offset
= rb
->offset
;
431 for (addr
= 0; addr
< length
; addr
+= TARGET_PAGE_SIZE
) {
432 if (cpu_physical_memory_test_and_clear_dirty(
433 start
+ addr
+ offset
,
435 DIRTY_MEMORY_MIGRATION
)) {
436 *real_dirty_pages
+= 1;
437 long k
= (start
+ addr
) >> TARGET_PAGE_BITS
;
438 if (!test_and_set_bit(k
, dest
)) {