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
;
52 static inline bool offset_in_ramblock(RAMBlock
*b
, ram_addr_t offset
)
54 return (b
&& b
->host
&& offset
< b
->used_length
) ? true : false;
57 static inline void *ramblock_ptr(RAMBlock
*block
, ram_addr_t offset
)
59 assert(offset_in_ramblock(block
, offset
));
60 return (char *)block
->host
+ offset
;
63 long qemu_getrampagesize(void);
64 unsigned long last_ram_page(void);
65 RAMBlock
*qemu_ram_alloc_from_file(ram_addr_t size
, MemoryRegion
*mr
,
66 bool share
, const char *mem_path
,
68 RAMBlock
*qemu_ram_alloc_from_ptr(ram_addr_t size
, void *host
,
69 MemoryRegion
*mr
, Error
**errp
);
70 RAMBlock
*qemu_ram_alloc(ram_addr_t size
, MemoryRegion
*mr
, Error
**errp
);
71 RAMBlock
*qemu_ram_alloc_resizeable(ram_addr_t size
, ram_addr_t max_size
,
72 void (*resized
)(const char*,
75 MemoryRegion
*mr
, Error
**errp
);
76 void qemu_ram_free(RAMBlock
*block
);
78 int qemu_ram_resize(RAMBlock
*block
, ram_addr_t newsize
, Error
**errp
);
80 #define DIRTY_CLIENTS_ALL ((1 << DIRTY_MEMORY_NUM) - 1)
81 #define DIRTY_CLIENTS_NOCODE (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
83 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start
,
87 DirtyMemoryBlocks
*blocks
;
88 unsigned long end
, page
;
89 unsigned long idx
, offset
, base
;
92 assert(client
< DIRTY_MEMORY_NUM
);
94 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
95 page
= start
>> TARGET_PAGE_BITS
;
99 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
101 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
102 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
103 base
= page
- offset
;
105 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
106 unsigned long num
= next
- base
;
107 unsigned long found
= find_next_bit(blocks
->blocks
[idx
], num
, offset
);
116 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
124 static inline bool cpu_physical_memory_all_dirty(ram_addr_t start
,
128 DirtyMemoryBlocks
*blocks
;
129 unsigned long end
, page
;
130 unsigned long idx
, offset
, base
;
133 assert(client
< DIRTY_MEMORY_NUM
);
135 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
136 page
= start
>> TARGET_PAGE_BITS
;
140 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
142 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
143 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
144 base
= page
- offset
;
146 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
147 unsigned long num
= next
- base
;
148 unsigned long found
= find_next_zero_bit(blocks
->blocks
[idx
], num
, offset
);
157 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
165 static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr
,
168 return cpu_physical_memory_get_dirty(addr
, 1, client
);
171 static inline bool cpu_physical_memory_is_clean(ram_addr_t addr
)
173 bool vga
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_VGA
);
174 bool code
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_CODE
);
176 cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_MIGRATION
);
177 return !(vga
&& code
&& migration
);
180 static inline uint8_t cpu_physical_memory_range_includes_clean(ram_addr_t start
,
186 if (mask
& (1 << DIRTY_MEMORY_VGA
) &&
187 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_VGA
)) {
188 ret
|= (1 << DIRTY_MEMORY_VGA
);
190 if (mask
& (1 << DIRTY_MEMORY_CODE
) &&
191 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_CODE
)) {
192 ret
|= (1 << DIRTY_MEMORY_CODE
);
194 if (mask
& (1 << DIRTY_MEMORY_MIGRATION
) &&
195 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
)) {
196 ret
|= (1 << DIRTY_MEMORY_MIGRATION
);
201 static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr
,
204 unsigned long page
, idx
, offset
;
205 DirtyMemoryBlocks
*blocks
;
207 assert(client
< DIRTY_MEMORY_NUM
);
209 page
= addr
>> TARGET_PAGE_BITS
;
210 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
211 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
215 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
217 set_bit_atomic(offset
, blocks
->blocks
[idx
]);
222 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start
,
226 DirtyMemoryBlocks
*blocks
[DIRTY_MEMORY_NUM
];
227 unsigned long end
, page
;
228 unsigned long idx
, offset
, base
;
231 if (!mask
&& !xen_enabled()) {
235 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
236 page
= start
>> TARGET_PAGE_BITS
;
240 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
241 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
]);
244 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
245 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
246 base
= page
- offset
;
248 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
250 if (likely(mask
& (1 << DIRTY_MEMORY_MIGRATION
))) {
251 bitmap_set_atomic(blocks
[DIRTY_MEMORY_MIGRATION
]->blocks
[idx
],
252 offset
, next
- page
);
254 if (unlikely(mask
& (1 << DIRTY_MEMORY_VGA
))) {
255 bitmap_set_atomic(blocks
[DIRTY_MEMORY_VGA
]->blocks
[idx
],
256 offset
, next
- page
);
258 if (unlikely(mask
& (1 << DIRTY_MEMORY_CODE
))) {
259 bitmap_set_atomic(blocks
[DIRTY_MEMORY_CODE
]->blocks
[idx
],
260 offset
, next
- page
);
266 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
271 xen_hvm_modified_memory(start
, length
);
275 static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap
,
280 unsigned long page_number
, c
;
283 unsigned long len
= (pages
+ HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
284 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
285 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
287 /* start address is aligned at the start of a word? */
288 if ((((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) &&
290 unsigned long **blocks
[DIRTY_MEMORY_NUM
];
292 unsigned long offset
;
294 long nr
= BITS_TO_LONGS(pages
);
296 idx
= (start
>> TARGET_PAGE_BITS
) / DIRTY_MEMORY_BLOCK_SIZE
;
297 offset
= BIT_WORD((start
>> TARGET_PAGE_BITS
) %
298 DIRTY_MEMORY_BLOCK_SIZE
);
302 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
303 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
])->blocks
;
306 for (k
= 0; k
< nr
; k
++) {
308 unsigned long temp
= leul_to_cpu(bitmap
[k
]);
310 atomic_or(&blocks
[DIRTY_MEMORY_MIGRATION
][idx
][offset
], temp
);
311 atomic_or(&blocks
[DIRTY_MEMORY_VGA
][idx
][offset
], temp
);
313 atomic_or(&blocks
[DIRTY_MEMORY_CODE
][idx
][offset
], temp
);
317 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
325 xen_hvm_modified_memory(start
, pages
<< TARGET_PAGE_BITS
);
327 uint8_t clients
= tcg_enabled() ? DIRTY_CLIENTS_ALL
: DIRTY_CLIENTS_NOCODE
;
329 * bitmap-traveling is faster than memory-traveling (for addr...)
330 * especially when most of the memory is not dirty.
332 for (i
= 0; i
< len
; i
++) {
333 if (bitmap
[i
] != 0) {
334 c
= leul_to_cpu(bitmap
[i
]);
338 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
339 addr
= page_number
* TARGET_PAGE_SIZE
;
340 ram_addr
= start
+ addr
;
341 cpu_physical_memory_set_dirty_range(ram_addr
,
342 TARGET_PAGE_SIZE
* hpratio
, clients
);
348 #endif /* not _WIN32 */
350 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start
,
354 DirtyBitmapSnapshot
*cpu_physical_memory_snapshot_and_clear_dirty
355 (ram_addr_t start
, ram_addr_t length
, unsigned client
);
357 bool cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot
*snap
,
361 static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start
,
364 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
);
365 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_VGA
);
366 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_CODE
);
371 uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBlock
*rb
,
374 uint64_t *real_dirty_pages
)
377 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
378 uint64_t num_dirty
= 0;
379 unsigned long *dest
= rb
->bmap
;
381 /* start address is aligned at the start of a word? */
382 if (((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) {
384 int nr
= BITS_TO_LONGS(length
>> TARGET_PAGE_BITS
);
385 unsigned long * const *src
;
386 unsigned long idx
= (page
* BITS_PER_LONG
) / DIRTY_MEMORY_BLOCK_SIZE
;
387 unsigned long offset
= BIT_WORD((page
* BITS_PER_LONG
) %
388 DIRTY_MEMORY_BLOCK_SIZE
);
392 src
= atomic_rcu_read(
393 &ram_list
.dirty_memory
[DIRTY_MEMORY_MIGRATION
])->blocks
;
395 for (k
= page
; k
< page
+ nr
; k
++) {
396 if (src
[idx
][offset
]) {
397 unsigned long bits
= atomic_xchg(&src
[idx
][offset
], 0);
398 unsigned long new_dirty
;
399 *real_dirty_pages
+= ctpopl(bits
);
400 new_dirty
= ~dest
[k
];
403 num_dirty
+= ctpopl(new_dirty
);
406 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
414 for (addr
= 0; addr
< length
; addr
+= TARGET_PAGE_SIZE
) {
415 if (cpu_physical_memory_test_and_clear_dirty(
418 DIRTY_MEMORY_MIGRATION
)) {
419 *real_dirty_pages
+= 1;
420 long k
= (start
+ addr
) >> TARGET_PAGE_BITS
;
421 if (!test_and_set_bit(k
, dest
)) {