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"
27 struct MemoryRegion
*mr
;
30 ram_addr_t used_length
;
31 ram_addr_t max_length
;
32 void (*resized
)(const char*, uint64_t length
, void *host
);
34 /* Protected by iothread lock. */
36 /* RCU-enabled, writes protected by the ramlist lock */
37 QLIST_ENTRY(RAMBlock
) next
;
42 static inline bool offset_in_ramblock(RAMBlock
*b
, ram_addr_t offset
)
44 return (b
&& b
->host
&& offset
< b
->used_length
) ? true : false;
47 static inline void *ramblock_ptr(RAMBlock
*block
, ram_addr_t offset
)
49 assert(offset_in_ramblock(block
, offset
));
50 return (char *)block
->host
+ offset
;
53 /* The dirty memory bitmap is split into fixed-size blocks to allow growth
54 * under RCU. The bitmap for a block can be accessed as follows:
58 * DirtyMemoryBlocks *blocks =
59 * atomic_rcu_read(&ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION]);
61 * ram_addr_t idx = (addr >> TARGET_PAGE_BITS) / DIRTY_MEMORY_BLOCK_SIZE;
62 * unsigned long *block = blocks.blocks[idx];
63 * ...access block bitmap...
67 * Remember to check for the end of the block when accessing a range of
68 * addresses. Move on to the next block if you reach the end.
70 * Organization into blocks allows dirty memory to grow (but not shrink) under
71 * RCU. When adding new RAMBlocks requires the dirty memory to grow, a new
72 * DirtyMemoryBlocks array is allocated with pointers to existing blocks kept
73 * the same. Other threads can safely access existing blocks while dirty
74 * memory is being grown. When no threads are using the old DirtyMemoryBlocks
75 * anymore it is freed by RCU (but the underlying blocks stay because they are
76 * pointed to from the new DirtyMemoryBlocks).
78 #define DIRTY_MEMORY_BLOCK_SIZE ((ram_addr_t)256 * 1024 * 8)
81 unsigned long *blocks
[];
84 typedef struct RAMList
{
87 /* RCU-enabled, writes protected by the ramlist lock. */
88 QLIST_HEAD(, RAMBlock
) blocks
;
89 DirtyMemoryBlocks
*dirty_memory
[DIRTY_MEMORY_NUM
];
92 extern RAMList ram_list
;
94 ram_addr_t
last_ram_offset(void);
95 void qemu_mutex_lock_ramlist(void);
96 void qemu_mutex_unlock_ramlist(void);
98 RAMBlock
*qemu_ram_alloc_from_file(ram_addr_t size
, MemoryRegion
*mr
,
99 bool share
, const char *mem_path
,
101 RAMBlock
*qemu_ram_alloc_from_ptr(ram_addr_t size
, void *host
,
102 MemoryRegion
*mr
, Error
**errp
);
103 RAMBlock
*qemu_ram_alloc(ram_addr_t size
, MemoryRegion
*mr
, Error
**errp
);
104 RAMBlock
*qemu_ram_alloc_resizeable(ram_addr_t size
, ram_addr_t max_size
,
105 void (*resized
)(const char*,
108 MemoryRegion
*mr
, Error
**errp
);
109 void qemu_ram_free(RAMBlock
*block
);
111 int qemu_ram_resize(RAMBlock
*block
, ram_addr_t newsize
, Error
**errp
);
113 #define DIRTY_CLIENTS_ALL ((1 << DIRTY_MEMORY_NUM) - 1)
114 #define DIRTY_CLIENTS_NOCODE (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
116 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start
,
120 DirtyMemoryBlocks
*blocks
;
121 unsigned long end
, page
;
122 unsigned long idx
, offset
, base
;
125 assert(client
< DIRTY_MEMORY_NUM
);
127 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
128 page
= start
>> TARGET_PAGE_BITS
;
132 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
134 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
135 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
136 base
= page
- offset
;
138 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
139 unsigned long num
= next
- base
;
140 unsigned long found
= find_next_bit(blocks
->blocks
[idx
], num
, offset
);
149 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
157 static inline bool cpu_physical_memory_all_dirty(ram_addr_t start
,
161 DirtyMemoryBlocks
*blocks
;
162 unsigned long end
, page
;
163 unsigned long idx
, offset
, base
;
166 assert(client
< DIRTY_MEMORY_NUM
);
168 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
169 page
= start
>> TARGET_PAGE_BITS
;
173 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
175 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
176 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
177 base
= page
- offset
;
179 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
180 unsigned long num
= next
- base
;
181 unsigned long found
= find_next_zero_bit(blocks
->blocks
[idx
], num
, offset
);
190 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
198 static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr
,
201 return cpu_physical_memory_get_dirty(addr
, 1, client
);
204 static inline bool cpu_physical_memory_is_clean(ram_addr_t addr
)
206 bool vga
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_VGA
);
207 bool code
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_CODE
);
209 cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_MIGRATION
);
210 return !(vga
&& code
&& migration
);
213 static inline uint8_t cpu_physical_memory_range_includes_clean(ram_addr_t start
,
219 if (mask
& (1 << DIRTY_MEMORY_VGA
) &&
220 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_VGA
)) {
221 ret
|= (1 << DIRTY_MEMORY_VGA
);
223 if (mask
& (1 << DIRTY_MEMORY_CODE
) &&
224 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_CODE
)) {
225 ret
|= (1 << DIRTY_MEMORY_CODE
);
227 if (mask
& (1 << DIRTY_MEMORY_MIGRATION
) &&
228 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
)) {
229 ret
|= (1 << DIRTY_MEMORY_MIGRATION
);
234 static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr
,
237 unsigned long page
, idx
, offset
;
238 DirtyMemoryBlocks
*blocks
;
240 assert(client
< DIRTY_MEMORY_NUM
);
242 page
= addr
>> TARGET_PAGE_BITS
;
243 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
244 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
248 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
250 set_bit_atomic(offset
, blocks
->blocks
[idx
]);
255 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start
,
259 DirtyMemoryBlocks
*blocks
[DIRTY_MEMORY_NUM
];
260 unsigned long end
, page
;
261 unsigned long idx
, offset
, base
;
264 if (!mask
&& !xen_enabled()) {
268 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
269 page
= start
>> TARGET_PAGE_BITS
;
273 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
274 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
]);
277 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
278 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
279 base
= page
- offset
;
281 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
283 if (likely(mask
& (1 << DIRTY_MEMORY_MIGRATION
))) {
284 bitmap_set_atomic(blocks
[DIRTY_MEMORY_MIGRATION
]->blocks
[idx
],
285 offset
, next
- page
);
287 if (unlikely(mask
& (1 << DIRTY_MEMORY_VGA
))) {
288 bitmap_set_atomic(blocks
[DIRTY_MEMORY_VGA
]->blocks
[idx
],
289 offset
, next
- page
);
291 if (unlikely(mask
& (1 << DIRTY_MEMORY_CODE
))) {
292 bitmap_set_atomic(blocks
[DIRTY_MEMORY_CODE
]->blocks
[idx
],
293 offset
, next
- page
);
299 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
304 xen_modified_memory(start
, length
);
308 static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap
,
313 unsigned long page_number
, c
;
316 unsigned long len
= (pages
+ HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
317 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
318 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
320 /* start address is aligned at the start of a word? */
321 if ((((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) &&
323 unsigned long **blocks
[DIRTY_MEMORY_NUM
];
325 unsigned long offset
;
327 long nr
= BITS_TO_LONGS(pages
);
329 idx
= (start
>> TARGET_PAGE_BITS
) / DIRTY_MEMORY_BLOCK_SIZE
;
330 offset
= BIT_WORD((start
>> TARGET_PAGE_BITS
) %
331 DIRTY_MEMORY_BLOCK_SIZE
);
335 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
336 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
])->blocks
;
339 for (k
= 0; k
< nr
; k
++) {
341 unsigned long temp
= leul_to_cpu(bitmap
[k
]);
343 atomic_or(&blocks
[DIRTY_MEMORY_MIGRATION
][idx
][offset
], temp
);
344 atomic_or(&blocks
[DIRTY_MEMORY_VGA
][idx
][offset
], temp
);
346 atomic_or(&blocks
[DIRTY_MEMORY_CODE
][idx
][offset
], temp
);
350 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
358 xen_modified_memory(start
, pages
<< TARGET_PAGE_BITS
);
360 uint8_t clients
= tcg_enabled() ? DIRTY_CLIENTS_ALL
: DIRTY_CLIENTS_NOCODE
;
362 * bitmap-traveling is faster than memory-traveling (for addr...)
363 * especially when most of the memory is not dirty.
365 for (i
= 0; i
< len
; i
++) {
366 if (bitmap
[i
] != 0) {
367 c
= leul_to_cpu(bitmap
[i
]);
371 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
372 addr
= page_number
* TARGET_PAGE_SIZE
;
373 ram_addr
= start
+ addr
;
374 cpu_physical_memory_set_dirty_range(ram_addr
,
375 TARGET_PAGE_SIZE
* hpratio
, clients
);
381 #endif /* not _WIN32 */
383 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start
,
387 static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start
,
390 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
);
391 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_VGA
);
392 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_CODE
);
397 uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned long *dest
,
402 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
403 uint64_t num_dirty
= 0;
405 /* start address is aligned at the start of a word? */
406 if (((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) {
408 int nr
= BITS_TO_LONGS(length
>> TARGET_PAGE_BITS
);
409 unsigned long * const *src
;
410 unsigned long idx
= (page
* BITS_PER_LONG
) / DIRTY_MEMORY_BLOCK_SIZE
;
411 unsigned long offset
= BIT_WORD((page
* BITS_PER_LONG
) %
412 DIRTY_MEMORY_BLOCK_SIZE
);
416 src
= atomic_rcu_read(
417 &ram_list
.dirty_memory
[DIRTY_MEMORY_MIGRATION
])->blocks
;
419 for (k
= page
; k
< page
+ nr
; k
++) {
420 if (src
[idx
][offset
]) {
421 unsigned long bits
= atomic_xchg(&src
[idx
][offset
], 0);
422 unsigned long new_dirty
;
423 new_dirty
= ~dest
[k
];
426 num_dirty
+= ctpopl(new_dirty
);
429 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
437 for (addr
= 0; addr
< length
; addr
+= TARGET_PAGE_SIZE
) {
438 if (cpu_physical_memory_test_and_clear_dirty(
441 DIRTY_MEMORY_MIGRATION
)) {
442 long k
= (start
+ addr
) >> TARGET_PAGE_BITS
;
443 if (!test_and_set_bit(k
, dest
)) {
453 void migration_bitmap_extend(ram_addr_t old
, ram_addr_t
new);