]> Git Repo - qemu.git/blob - include/exec/ram_addr.h
softmmu/memory: Pass ram_flags to qemu_ram_alloc() and qemu_ram_alloc_internal()
[qemu.git] / include / exec / ram_addr.h
1 /*
2  * Declarations for cpu physical memory functions
3  *
4  * Copyright 2011 Red Hat, Inc. and/or its affiliates
5  *
6  * Authors:
7  *  Avi Kivity <[email protected]>
8  *
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.
11  *
12  */
13
14 /*
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.
17  */
18
19 #ifndef RAM_ADDR_H
20 #define RAM_ADDR_H
21
22 #ifndef CONFIG_USER_ONLY
23 #include "cpu.h"
24 #include "sysemu/xen.h"
25 #include "sysemu/tcg.h"
26 #include "exec/ramlist.h"
27 #include "exec/ramblock.h"
28
29 /**
30  * clear_bmap_size: calculate clear bitmap size
31  *
32  * @pages: number of guest pages
33  * @shift: guest page number shift
34  *
35  * Returns: number of bits for the clear bitmap
36  */
37 static inline long clear_bmap_size(uint64_t pages, uint8_t shift)
38 {
39     return DIV_ROUND_UP(pages, 1UL << shift);
40 }
41
42 /**
43  * clear_bmap_set: set clear bitmap for the page range
44  *
45  * @rb: the ramblock to operate on
46  * @start: the start page number
47  * @size: number of pages to set in the bitmap
48  *
49  * Returns: None
50  */
51 static inline void clear_bmap_set(RAMBlock *rb, uint64_t start,
52                                   uint64_t npages)
53 {
54     uint8_t shift = rb->clear_bmap_shift;
55
56     bitmap_set_atomic(rb->clear_bmap, start >> shift,
57                       clear_bmap_size(npages, shift));
58 }
59
60 /**
61  * clear_bmap_test_and_clear: test clear bitmap for the page, clear if set
62  *
63  * @rb: the ramblock to operate on
64  * @page: the page number to check
65  *
66  * Returns: true if the bit was set, false otherwise
67  */
68 static inline bool clear_bmap_test_and_clear(RAMBlock *rb, uint64_t page)
69 {
70     uint8_t shift = rb->clear_bmap_shift;
71
72     return bitmap_test_and_clear_atomic(rb->clear_bmap, page >> shift, 1);
73 }
74
75 static inline bool offset_in_ramblock(RAMBlock *b, ram_addr_t offset)
76 {
77     return (b && b->host && offset < b->used_length) ? true : false;
78 }
79
80 static inline void *ramblock_ptr(RAMBlock *block, ram_addr_t offset)
81 {
82     assert(offset_in_ramblock(block, offset));
83     return (char *)block->host + offset;
84 }
85
86 static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr,
87                                                             RAMBlock *rb)
88 {
89     uint64_t host_addr_offset =
90             (uint64_t)(uintptr_t)(host_addr - (void *)rb->host);
91     return host_addr_offset >> TARGET_PAGE_BITS;
92 }
93
94 bool ramblock_is_pmem(RAMBlock *rb);
95
96 long qemu_minrampagesize(void);
97 long qemu_maxrampagesize(void);
98
99 /**
100  * qemu_ram_alloc_from_file,
101  * qemu_ram_alloc_from_fd:  Allocate a ram block from the specified backing
102  *                          file or device
103  *
104  * Parameters:
105  *  @size: the size in bytes of the ram block
106  *  @mr: the memory region where the ram block is
107  *  @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM.
108  *  @mem_path or @fd: specify the backing file or device
109  *  @readonly: true to open @path for reading, false for read/write.
110  *  @errp: pointer to Error*, to store an error if it happens
111  *
112  * Return:
113  *  On success, return a pointer to the ram block.
114  *  On failure, return NULL.
115  */
116 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
117                                    uint32_t ram_flags, const char *mem_path,
118                                    bool readonly, Error **errp);
119 RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, MemoryRegion *mr,
120                                  uint32_t ram_flags, int fd, off_t offset,
121                                  bool readonly, Error **errp);
122
123 RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
124                                   MemoryRegion *mr, Error **errp);
125 RAMBlock *qemu_ram_alloc(ram_addr_t size, uint32_t ram_flags, MemoryRegion *mr,
126                          Error **errp);
127 RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t max_size,
128                                     void (*resized)(const char*,
129                                                     uint64_t length,
130                                                     void *host),
131                                     MemoryRegion *mr, Error **errp);
132 void qemu_ram_free(RAMBlock *block);
133
134 int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp);
135
136 void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length);
137
138 /* Clear whole block of mem */
139 static inline void qemu_ram_block_writeback(RAMBlock *block)
140 {
141     qemu_ram_msync(block, 0, block->used_length);
142 }
143
144 #define DIRTY_CLIENTS_ALL     ((1 << DIRTY_MEMORY_NUM) - 1)
145 #define DIRTY_CLIENTS_NOCODE  (DIRTY_CLIENTS_ALL & ~(1 << DIRTY_MEMORY_CODE))
146
147 void tb_invalidate_phys_range(ram_addr_t start, ram_addr_t end);
148
149 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start,
150                                                  ram_addr_t length,
151                                                  unsigned client)
152 {
153     DirtyMemoryBlocks *blocks;
154     unsigned long end, page;
155     unsigned long idx, offset, base;
156     bool dirty = false;
157
158     assert(client < DIRTY_MEMORY_NUM);
159
160     end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
161     page = start >> TARGET_PAGE_BITS;
162
163     WITH_RCU_READ_LOCK_GUARD() {
164         blocks = qatomic_rcu_read(&ram_list.dirty_memory[client]);
165
166         idx = page / DIRTY_MEMORY_BLOCK_SIZE;
167         offset = page % DIRTY_MEMORY_BLOCK_SIZE;
168         base = page - offset;
169         while (page < end) {
170             unsigned long next = MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE);
171             unsigned long num = next - base;
172             unsigned long found = find_next_bit(blocks->blocks[idx],
173                                                 num, offset);
174             if (found < num) {
175                 dirty = true;
176                 break;
177             }
178
179             page = next;
180             idx++;
181             offset = 0;
182             base += DIRTY_MEMORY_BLOCK_SIZE;
183         }
184     }
185
186     return dirty;
187 }
188
189 static inline bool cpu_physical_memory_all_dirty(ram_addr_t start,
190                                                  ram_addr_t length,
191                                                  unsigned client)
192 {
193     DirtyMemoryBlocks *blocks;
194     unsigned long end, page;
195     unsigned long idx, offset, base;
196     bool dirty = true;
197
198     assert(client < DIRTY_MEMORY_NUM);
199
200     end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
201     page = start >> TARGET_PAGE_BITS;
202
203     RCU_READ_LOCK_GUARD();
204
205     blocks = qatomic_rcu_read(&ram_list.dirty_memory[client]);
206
207     idx = page / DIRTY_MEMORY_BLOCK_SIZE;
208     offset = page % DIRTY_MEMORY_BLOCK_SIZE;
209     base = page - offset;
210     while (page < end) {
211         unsigned long next = MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE);
212         unsigned long num = next - base;
213         unsigned long found = find_next_zero_bit(blocks->blocks[idx], num, offset);
214         if (found < num) {
215             dirty = false;
216             break;
217         }
218
219         page = next;
220         idx++;
221         offset = 0;
222         base += DIRTY_MEMORY_BLOCK_SIZE;
223     }
224
225     return dirty;
226 }
227
228 static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr,
229                                                       unsigned client)
230 {
231     return cpu_physical_memory_get_dirty(addr, 1, client);
232 }
233
234 static inline bool cpu_physical_memory_is_clean(ram_addr_t addr)
235 {
236     bool vga = cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_VGA);
237     bool code = cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_CODE);
238     bool migration =
239         cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_MIGRATION);
240     return !(vga && code && migration);
241 }
242
243 static inline uint8_t cpu_physical_memory_range_includes_clean(ram_addr_t start,
244                                                                ram_addr_t length,
245                                                                uint8_t mask)
246 {
247     uint8_t ret = 0;
248
249     if (mask & (1 << DIRTY_MEMORY_VGA) &&
250         !cpu_physical_memory_all_dirty(start, length, DIRTY_MEMORY_VGA)) {
251         ret |= (1 << DIRTY_MEMORY_VGA);
252     }
253     if (mask & (1 << DIRTY_MEMORY_CODE) &&
254         !cpu_physical_memory_all_dirty(start, length, DIRTY_MEMORY_CODE)) {
255         ret |= (1 << DIRTY_MEMORY_CODE);
256     }
257     if (mask & (1 << DIRTY_MEMORY_MIGRATION) &&
258         !cpu_physical_memory_all_dirty(start, length, DIRTY_MEMORY_MIGRATION)) {
259         ret |= (1 << DIRTY_MEMORY_MIGRATION);
260     }
261     return ret;
262 }
263
264 static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr,
265                                                       unsigned client)
266 {
267     unsigned long page, idx, offset;
268     DirtyMemoryBlocks *blocks;
269
270     assert(client < DIRTY_MEMORY_NUM);
271
272     page = addr >> TARGET_PAGE_BITS;
273     idx = page / DIRTY_MEMORY_BLOCK_SIZE;
274     offset = page % DIRTY_MEMORY_BLOCK_SIZE;
275
276     RCU_READ_LOCK_GUARD();
277
278     blocks = qatomic_rcu_read(&ram_list.dirty_memory[client]);
279
280     set_bit_atomic(offset, blocks->blocks[idx]);
281 }
282
283 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start,
284                                                        ram_addr_t length,
285                                                        uint8_t mask)
286 {
287     DirtyMemoryBlocks *blocks[DIRTY_MEMORY_NUM];
288     unsigned long end, page;
289     unsigned long idx, offset, base;
290     int i;
291
292     if (!mask && !xen_enabled()) {
293         return;
294     }
295
296     end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
297     page = start >> TARGET_PAGE_BITS;
298
299     WITH_RCU_READ_LOCK_GUARD() {
300         for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
301             blocks[i] = qatomic_rcu_read(&ram_list.dirty_memory[i]);
302         }
303
304         idx = page / DIRTY_MEMORY_BLOCK_SIZE;
305         offset = page % DIRTY_MEMORY_BLOCK_SIZE;
306         base = page - offset;
307         while (page < end) {
308             unsigned long next = MIN(end, base + DIRTY_MEMORY_BLOCK_SIZE);
309
310             if (likely(mask & (1 << DIRTY_MEMORY_MIGRATION))) {
311                 bitmap_set_atomic(blocks[DIRTY_MEMORY_MIGRATION]->blocks[idx],
312                                   offset, next - page);
313             }
314             if (unlikely(mask & (1 << DIRTY_MEMORY_VGA))) {
315                 bitmap_set_atomic(blocks[DIRTY_MEMORY_VGA]->blocks[idx],
316                                   offset, next - page);
317             }
318             if (unlikely(mask & (1 << DIRTY_MEMORY_CODE))) {
319                 bitmap_set_atomic(blocks[DIRTY_MEMORY_CODE]->blocks[idx],
320                                   offset, next - page);
321             }
322
323             page = next;
324             idx++;
325             offset = 0;
326             base += DIRTY_MEMORY_BLOCK_SIZE;
327         }
328     }
329
330     xen_hvm_modified_memory(start, length);
331 }
332
333 #if !defined(_WIN32)
334 static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap,
335                                                           ram_addr_t start,
336                                                           ram_addr_t pages)
337 {
338     unsigned long i, j;
339     unsigned long page_number, c;
340     hwaddr addr;
341     ram_addr_t ram_addr;
342     unsigned long len = (pages + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
343     unsigned long hpratio = qemu_real_host_page_size / TARGET_PAGE_SIZE;
344     unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
345
346     /* start address is aligned at the start of a word? */
347     if ((((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) &&
348         (hpratio == 1)) {
349         unsigned long **blocks[DIRTY_MEMORY_NUM];
350         unsigned long idx;
351         unsigned long offset;
352         long k;
353         long nr = BITS_TO_LONGS(pages);
354
355         idx = (start >> TARGET_PAGE_BITS) / DIRTY_MEMORY_BLOCK_SIZE;
356         offset = BIT_WORD((start >> TARGET_PAGE_BITS) %
357                           DIRTY_MEMORY_BLOCK_SIZE);
358
359         WITH_RCU_READ_LOCK_GUARD() {
360             for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
361                 blocks[i] =
362                     qatomic_rcu_read(&ram_list.dirty_memory[i])->blocks;
363             }
364
365             for (k = 0; k < nr; k++) {
366                 if (bitmap[k]) {
367                     unsigned long temp = leul_to_cpu(bitmap[k]);
368
369                     qatomic_or(&blocks[DIRTY_MEMORY_VGA][idx][offset], temp);
370
371                     if (global_dirty_log) {
372                         qatomic_or(
373                                 &blocks[DIRTY_MEMORY_MIGRATION][idx][offset],
374                                 temp);
375                     }
376
377                     if (tcg_enabled()) {
378                         qatomic_or(&blocks[DIRTY_MEMORY_CODE][idx][offset],
379                                    temp);
380                     }
381                 }
382
383                 if (++offset >= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE)) {
384                     offset = 0;
385                     idx++;
386                 }
387             }
388         }
389
390         xen_hvm_modified_memory(start, pages << TARGET_PAGE_BITS);
391     } else {
392         uint8_t clients = tcg_enabled() ? DIRTY_CLIENTS_ALL : DIRTY_CLIENTS_NOCODE;
393
394         if (!global_dirty_log) {
395             clients &= ~(1 << DIRTY_MEMORY_MIGRATION);
396         }
397
398         /*
399          * bitmap-traveling is faster than memory-traveling (for addr...)
400          * especially when most of the memory is not dirty.
401          */
402         for (i = 0; i < len; i++) {
403             if (bitmap[i] != 0) {
404                 c = leul_to_cpu(bitmap[i]);
405                 do {
406                     j = ctzl(c);
407                     c &= ~(1ul << j);
408                     page_number = (i * HOST_LONG_BITS + j) * hpratio;
409                     addr = page_number * TARGET_PAGE_SIZE;
410                     ram_addr = start + addr;
411                     cpu_physical_memory_set_dirty_range(ram_addr,
412                                        TARGET_PAGE_SIZE * hpratio, clients);
413                 } while (c != 0);
414             }
415         }
416     }
417 }
418 #endif /* not _WIN32 */
419
420 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
421                                               ram_addr_t length,
422                                               unsigned client);
423
424 DirtyBitmapSnapshot *cpu_physical_memory_snapshot_and_clear_dirty
425     (MemoryRegion *mr, hwaddr offset, hwaddr length, unsigned client);
426
427 bool cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot *snap,
428                                             ram_addr_t start,
429                                             ram_addr_t length);
430
431 static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start,
432                                                          ram_addr_t length)
433 {
434     cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_MIGRATION);
435     cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_VGA);
436     cpu_physical_memory_test_and_clear_dirty(start, length, DIRTY_MEMORY_CODE);
437 }
438
439
440 /* Called with RCU critical section */
441 static inline
442 uint64_t cpu_physical_memory_sync_dirty_bitmap(RAMBlock *rb,
443                                                ram_addr_t start,
444                                                ram_addr_t length)
445 {
446     ram_addr_t addr;
447     unsigned long word = BIT_WORD((start + rb->offset) >> TARGET_PAGE_BITS);
448     uint64_t num_dirty = 0;
449     unsigned long *dest = rb->bmap;
450
451     /* start address and length is aligned at the start of a word? */
452     if (((word * BITS_PER_LONG) << TARGET_PAGE_BITS) ==
453          (start + rb->offset) &&
454         !(length & ((BITS_PER_LONG << TARGET_PAGE_BITS) - 1))) {
455         int k;
456         int nr = BITS_TO_LONGS(length >> TARGET_PAGE_BITS);
457         unsigned long * const *src;
458         unsigned long idx = (word * BITS_PER_LONG) / DIRTY_MEMORY_BLOCK_SIZE;
459         unsigned long offset = BIT_WORD((word * BITS_PER_LONG) %
460                                         DIRTY_MEMORY_BLOCK_SIZE);
461         unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
462
463         src = qatomic_rcu_read(
464                 &ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION])->blocks;
465
466         for (k = page; k < page + nr; k++) {
467             if (src[idx][offset]) {
468                 unsigned long bits = qatomic_xchg(&src[idx][offset], 0);
469                 unsigned long new_dirty;
470                 new_dirty = ~dest[k];
471                 dest[k] |= bits;
472                 new_dirty &= bits;
473                 num_dirty += ctpopl(new_dirty);
474             }
475
476             if (++offset >= BITS_TO_LONGS(DIRTY_MEMORY_BLOCK_SIZE)) {
477                 offset = 0;
478                 idx++;
479             }
480         }
481
482         if (rb->clear_bmap) {
483             /*
484              * Postpone the dirty bitmap clear to the point before we
485              * really send the pages, also we will split the clear
486              * dirty procedure into smaller chunks.
487              */
488             clear_bmap_set(rb, start >> TARGET_PAGE_BITS,
489                            length >> TARGET_PAGE_BITS);
490         } else {
491             /* Slow path - still do that in a huge chunk */
492             memory_region_clear_dirty_bitmap(rb->mr, start, length);
493         }
494     } else {
495         ram_addr_t offset = rb->offset;
496
497         for (addr = 0; addr < length; addr += TARGET_PAGE_SIZE) {
498             if (cpu_physical_memory_test_and_clear_dirty(
499                         start + addr + offset,
500                         TARGET_PAGE_SIZE,
501                         DIRTY_MEMORY_MIGRATION)) {
502                 long k = (start + addr) >> TARGET_PAGE_BITS;
503                 if (!test_and_set_bit(k, dest)) {
504                     num_dirty++;
505                 }
506             }
507         }
508     }
509
510     return num_dirty;
511 }
512 #endif
513 #endif
This page took 0.055002 seconds and 4 git commands to generate.