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
;
44 static inline bool offset_in_ramblock(RAMBlock
*b
, ram_addr_t offset
)
46 return (b
&& b
->host
&& offset
< b
->used_length
) ? true : false;
49 static inline void *ramblock_ptr(RAMBlock
*block
, ram_addr_t offset
)
51 assert(offset_in_ramblock(block
, offset
));
52 return (char *)block
->host
+ offset
;
55 long qemu_getrampagesize(void);
56 ram_addr_t
last_ram_offset(void);
57 RAMBlock
*qemu_ram_alloc_from_file(ram_addr_t size
, MemoryRegion
*mr
,
58 bool share
, const char *mem_path
,
60 RAMBlock
*qemu_ram_alloc_from_ptr(ram_addr_t size
, void *host
,
61 MemoryRegion
*mr
, Error
**errp
);
62 RAMBlock
*qemu_ram_alloc(ram_addr_t size
, MemoryRegion
*mr
, Error
**errp
);
63 RAMBlock
*qemu_ram_alloc_resizeable(ram_addr_t size
, ram_addr_t max_size
,
64 void (*resized
)(const char*,
67 MemoryRegion
*mr
, Error
**errp
);
68 void qemu_ram_free(RAMBlock
*block
);
70 int qemu_ram_resize(RAMBlock
*block
, ram_addr_t newsize
, Error
**errp
);
72 #define DIRTY_CLIENTS_ALL ((1 << DIRTY_MEMORY_NUM) - 1)
73 #define DIRTY_CLIENTS_NOCODE (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
75 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start
,
79 DirtyMemoryBlocks
*blocks
;
80 unsigned long end
, page
;
81 unsigned long idx
, offset
, base
;
84 assert(client
< DIRTY_MEMORY_NUM
);
86 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
87 page
= start
>> TARGET_PAGE_BITS
;
91 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
93 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
94 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
97 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
98 unsigned long num
= next
- base
;
99 unsigned long found
= find_next_bit(blocks
->blocks
[idx
], num
, offset
);
108 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
116 static inline bool cpu_physical_memory_all_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_zero_bit(blocks
->blocks
[idx
], num
, offset
);
149 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
157 static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr
,
160 return cpu_physical_memory_get_dirty(addr
, 1, client
);
163 static inline bool cpu_physical_memory_is_clean(ram_addr_t addr
)
165 bool vga
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_VGA
);
166 bool code
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_CODE
);
168 cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_MIGRATION
);
169 return !(vga
&& code
&& migration
);
172 static inline uint8_t cpu_physical_memory_range_includes_clean(ram_addr_t start
,
178 if (mask
& (1 << DIRTY_MEMORY_VGA
) &&
179 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_VGA
)) {
180 ret
|= (1 << DIRTY_MEMORY_VGA
);
182 if (mask
& (1 << DIRTY_MEMORY_CODE
) &&
183 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_CODE
)) {
184 ret
|= (1 << DIRTY_MEMORY_CODE
);
186 if (mask
& (1 << DIRTY_MEMORY_MIGRATION
) &&
187 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
)) {
188 ret
|= (1 << DIRTY_MEMORY_MIGRATION
);
193 static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr
,
196 unsigned long page
, idx
, offset
;
197 DirtyMemoryBlocks
*blocks
;
199 assert(client
< DIRTY_MEMORY_NUM
);
201 page
= addr
>> TARGET_PAGE_BITS
;
202 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
203 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
207 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
209 set_bit_atomic(offset
, blocks
->blocks
[idx
]);
214 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start
,
218 DirtyMemoryBlocks
*blocks
[DIRTY_MEMORY_NUM
];
219 unsigned long end
, page
;
220 unsigned long idx
, offset
, base
;
223 if (!mask
&& !xen_enabled()) {
227 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
228 page
= start
>> TARGET_PAGE_BITS
;
232 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
233 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
]);
236 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
237 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
238 base
= page
- offset
;
240 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
242 if (likely(mask
& (1 << DIRTY_MEMORY_MIGRATION
))) {
243 bitmap_set_atomic(blocks
[DIRTY_MEMORY_MIGRATION
]->blocks
[idx
],
244 offset
, next
- page
);
246 if (unlikely(mask
& (1 << DIRTY_MEMORY_VGA
))) {
247 bitmap_set_atomic(blocks
[DIRTY_MEMORY_VGA
]->blocks
[idx
],
248 offset
, next
- page
);
250 if (unlikely(mask
& (1 << DIRTY_MEMORY_CODE
))) {
251 bitmap_set_atomic(blocks
[DIRTY_MEMORY_CODE
]->blocks
[idx
],
252 offset
, next
- page
);
258 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
263 xen_modified_memory(start
, length
);
267 static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap
,
272 unsigned long page_number
, c
;
275 unsigned long len
= (pages
+ HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
276 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
277 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
279 /* start address is aligned at the start of a word? */
280 if ((((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) &&
282 unsigned long **blocks
[DIRTY_MEMORY_NUM
];
284 unsigned long offset
;
286 long nr
= BITS_TO_LONGS(pages
);
288 idx
= (start
>> TARGET_PAGE_BITS
) / DIRTY_MEMORY_BLOCK_SIZE
;
289 offset
= BIT_WORD((start
>> TARGET_PAGE_BITS
) %
290 DIRTY_MEMORY_BLOCK_SIZE
);
294 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
295 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
])->blocks
;
298 for (k
= 0; k
< nr
; k
++) {
300 unsigned long temp
= leul_to_cpu(bitmap
[k
]);
302 atomic_or(&blocks
[DIRTY_MEMORY_MIGRATION
][idx
][offset
], temp
);
303 atomic_or(&blocks
[DIRTY_MEMORY_VGA
][idx
][offset
], temp
);
305 atomic_or(&blocks
[DIRTY_MEMORY_CODE
][idx
][offset
], temp
);
309 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
317 xen_modified_memory(start
, pages
<< TARGET_PAGE_BITS
);
319 uint8_t clients
= tcg_enabled() ? DIRTY_CLIENTS_ALL
: DIRTY_CLIENTS_NOCODE
;
321 * bitmap-traveling is faster than memory-traveling (for addr...)
322 * especially when most of the memory is not dirty.
324 for (i
= 0; i
< len
; i
++) {
325 if (bitmap
[i
] != 0) {
326 c
= leul_to_cpu(bitmap
[i
]);
330 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
331 addr
= page_number
* TARGET_PAGE_SIZE
;
332 ram_addr
= start
+ addr
;
333 cpu_physical_memory_set_dirty_range(ram_addr
,
334 TARGET_PAGE_SIZE
* hpratio
, clients
);
340 #endif /* not _WIN32 */
342 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start
,
346 static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start
,
349 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
);
350 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_VGA
);
351 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_CODE
);
356 uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned long *dest
,
361 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
362 uint64_t num_dirty
= 0;
364 /* start address is aligned at the start of a word? */
365 if (((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) {
367 int nr
= BITS_TO_LONGS(length
>> TARGET_PAGE_BITS
);
368 unsigned long * const *src
;
369 unsigned long idx
= (page
* BITS_PER_LONG
) / DIRTY_MEMORY_BLOCK_SIZE
;
370 unsigned long offset
= BIT_WORD((page
* BITS_PER_LONG
) %
371 DIRTY_MEMORY_BLOCK_SIZE
);
375 src
= atomic_rcu_read(
376 &ram_list
.dirty_memory
[DIRTY_MEMORY_MIGRATION
])->blocks
;
378 for (k
= page
; k
< page
+ nr
; k
++) {
379 if (src
[idx
][offset
]) {
380 unsigned long bits
= atomic_xchg(&src
[idx
][offset
], 0);
381 unsigned long new_dirty
;
382 new_dirty
= ~dest
[k
];
385 num_dirty
+= ctpopl(new_dirty
);
388 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
396 for (addr
= 0; addr
< length
; addr
+= TARGET_PAGE_SIZE
) {
397 if (cpu_physical_memory_test_and_clear_dirty(
400 DIRTY_MEMORY_MIGRATION
)) {
401 long k
= (start
+ addr
) >> TARGET_PAGE_BITS
;
402 if (!test_and_set_bit(k
, dest
)) {
412 void migration_bitmap_extend(ram_addr_t old
, ram_addr_t
new);