]>
git.proxmox.com Git - mirror_qemu.git/blob - include/exec/ram_addr.h
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 ram_addr_t
last_ram_offset(void);
56 RAMBlock
*qemu_ram_alloc_from_file(ram_addr_t size
, MemoryRegion
*mr
,
57 bool share
, const char *mem_path
,
59 RAMBlock
*qemu_ram_alloc_from_ptr(ram_addr_t size
, void *host
,
60 MemoryRegion
*mr
, Error
**errp
);
61 RAMBlock
*qemu_ram_alloc(ram_addr_t size
, MemoryRegion
*mr
, Error
**errp
);
62 RAMBlock
*qemu_ram_alloc_resizeable(ram_addr_t size
, ram_addr_t max_size
,
63 void (*resized
)(const char*,
66 MemoryRegion
*mr
, Error
**errp
);
67 void qemu_ram_free(RAMBlock
*block
);
69 int qemu_ram_resize(RAMBlock
*block
, ram_addr_t newsize
, Error
**errp
);
71 #define DIRTY_CLIENTS_ALL ((1 << DIRTY_MEMORY_NUM) - 1)
72 #define DIRTY_CLIENTS_NOCODE (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
74 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start
,
78 DirtyMemoryBlocks
*blocks
;
79 unsigned long end
, page
;
80 unsigned long idx
, offset
, base
;
83 assert(client
< DIRTY_MEMORY_NUM
);
85 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
86 page
= start
>> TARGET_PAGE_BITS
;
90 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
92 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
93 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
96 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
97 unsigned long num
= next
- base
;
98 unsigned long found
= find_next_bit(blocks
->blocks
[idx
], num
, offset
);
107 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
115 static inline bool cpu_physical_memory_all_dirty(ram_addr_t start
,
119 DirtyMemoryBlocks
*blocks
;
120 unsigned long end
, page
;
121 unsigned long idx
, offset
, base
;
124 assert(client
< DIRTY_MEMORY_NUM
);
126 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
127 page
= start
>> TARGET_PAGE_BITS
;
131 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
133 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
134 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
135 base
= page
- offset
;
137 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
138 unsigned long num
= next
- base
;
139 unsigned long found
= find_next_zero_bit(blocks
->blocks
[idx
], num
, offset
);
148 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
156 static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr
,
159 return cpu_physical_memory_get_dirty(addr
, 1, client
);
162 static inline bool cpu_physical_memory_is_clean(ram_addr_t addr
)
164 bool vga
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_VGA
);
165 bool code
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_CODE
);
167 cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_MIGRATION
);
168 return !(vga
&& code
&& migration
);
171 static inline uint8_t cpu_physical_memory_range_includes_clean(ram_addr_t start
,
177 if (mask
& (1 << DIRTY_MEMORY_VGA
) &&
178 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_VGA
)) {
179 ret
|= (1 << DIRTY_MEMORY_VGA
);
181 if (mask
& (1 << DIRTY_MEMORY_CODE
) &&
182 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_CODE
)) {
183 ret
|= (1 << DIRTY_MEMORY_CODE
);
185 if (mask
& (1 << DIRTY_MEMORY_MIGRATION
) &&
186 !cpu_physical_memory_all_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
)) {
187 ret
|= (1 << DIRTY_MEMORY_MIGRATION
);
192 static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr
,
195 unsigned long page
, idx
, offset
;
196 DirtyMemoryBlocks
*blocks
;
198 assert(client
< DIRTY_MEMORY_NUM
);
200 page
= addr
>> TARGET_PAGE_BITS
;
201 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
202 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
206 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
208 set_bit_atomic(offset
, blocks
->blocks
[idx
]);
213 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start
,
217 DirtyMemoryBlocks
*blocks
[DIRTY_MEMORY_NUM
];
218 unsigned long end
, page
;
219 unsigned long idx
, offset
, base
;
222 if (!mask
&& !xen_enabled()) {
226 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
227 page
= start
>> TARGET_PAGE_BITS
;
231 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
232 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
]);
235 idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
236 offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
237 base
= page
- offset
;
239 unsigned long next
= MIN(end
, base
+ DIRTY_MEMORY_BLOCK_SIZE
);
241 if (likely(mask
& (1 << DIRTY_MEMORY_MIGRATION
))) {
242 bitmap_set_atomic(blocks
[DIRTY_MEMORY_MIGRATION
]->blocks
[idx
],
243 offset
, next
- page
);
245 if (unlikely(mask
& (1 << DIRTY_MEMORY_VGA
))) {
246 bitmap_set_atomic(blocks
[DIRTY_MEMORY_VGA
]->blocks
[idx
],
247 offset
, next
- page
);
249 if (unlikely(mask
& (1 << DIRTY_MEMORY_CODE
))) {
250 bitmap_set_atomic(blocks
[DIRTY_MEMORY_CODE
]->blocks
[idx
],
251 offset
, next
- page
);
257 base
+= DIRTY_MEMORY_BLOCK_SIZE
;
262 xen_modified_memory(start
, length
);
266 static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap
,
271 unsigned long page_number
, c
;
274 unsigned long len
= (pages
+ HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
275 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
276 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
278 /* start address is aligned at the start of a word? */
279 if ((((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) &&
281 unsigned long **blocks
[DIRTY_MEMORY_NUM
];
283 unsigned long offset
;
285 long nr
= BITS_TO_LONGS(pages
);
287 idx
= (start
>> TARGET_PAGE_BITS
) / DIRTY_MEMORY_BLOCK_SIZE
;
288 offset
= BIT_WORD((start
>> TARGET_PAGE_BITS
) %
289 DIRTY_MEMORY_BLOCK_SIZE
);
293 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
294 blocks
[i
] = atomic_rcu_read(&ram_list
.dirty_memory
[i
])->blocks
;
297 for (k
= 0; k
< nr
; k
++) {
299 unsigned long temp
= leul_to_cpu(bitmap
[k
]);
301 atomic_or(&blocks
[DIRTY_MEMORY_MIGRATION
][idx
][offset
], temp
);
302 atomic_or(&blocks
[DIRTY_MEMORY_VGA
][idx
][offset
], temp
);
304 atomic_or(&blocks
[DIRTY_MEMORY_CODE
][idx
][offset
], temp
);
308 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
316 xen_modified_memory(start
, pages
<< TARGET_PAGE_BITS
);
318 uint8_t clients
= tcg_enabled() ? DIRTY_CLIENTS_ALL
: DIRTY_CLIENTS_NOCODE
;
320 * bitmap-traveling is faster than memory-traveling (for addr...)
321 * especially when most of the memory is not dirty.
323 for (i
= 0; i
< len
; i
++) {
324 if (bitmap
[i
] != 0) {
325 c
= leul_to_cpu(bitmap
[i
]);
329 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
330 addr
= page_number
* TARGET_PAGE_SIZE
;
331 ram_addr
= start
+ addr
;
332 cpu_physical_memory_set_dirty_range(ram_addr
,
333 TARGET_PAGE_SIZE
* hpratio
, clients
);
339 #endif /* not _WIN32 */
341 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start
,
345 static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start
,
348 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_MIGRATION
);
349 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_VGA
);
350 cpu_physical_memory_test_and_clear_dirty(start
, length
, DIRTY_MEMORY_CODE
);
355 uint64_t cpu_physical_memory_sync_dirty_bitmap(unsigned long *dest
,
360 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
361 uint64_t num_dirty
= 0;
363 /* start address is aligned at the start of a word? */
364 if (((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) {
366 int nr
= BITS_TO_LONGS(length
>> TARGET_PAGE_BITS
);
367 unsigned long * const *src
;
368 unsigned long idx
= (page
* BITS_PER_LONG
) / DIRTY_MEMORY_BLOCK_SIZE
;
369 unsigned long offset
= BIT_WORD((page
* BITS_PER_LONG
) %
370 DIRTY_MEMORY_BLOCK_SIZE
);
374 src
= atomic_rcu_read(
375 &ram_list
.dirty_memory
[DIRTY_MEMORY_MIGRATION
])->blocks
;
377 for (k
= page
; k
< page
+ nr
; k
++) {
378 if (src
[idx
][offset
]) {
379 unsigned long bits
= atomic_xchg(&src
[idx
][offset
], 0);
380 unsigned long new_dirty
;
381 new_dirty
= ~dest
[k
];
384 num_dirty
+= ctpopl(new_dirty
);
387 if (++offset
>= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE
)) {
395 for (addr
= 0; addr
< length
; addr
+= TARGET_PAGE_SIZE
) {
396 if (cpu_physical_memory_test_and_clear_dirty(
399 DIRTY_MEMORY_MIGRATION
)) {
400 long k
= (start
+ addr
) >> TARGET_PAGE_BITS
;
401 if (!test_and_set_bit(k
, dest
)) {
411 void migration_bitmap_extend(ram_addr_t old
, ram_addr_t
new);