hostmem: allow preallocation of any memory region
[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 <avi@redhat.com>
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 "hw/xen/xen.h"
24
25 ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
26 const char *mem_path, Error **errp);
27 ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
28 MemoryRegion *mr);
29 ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr);
30 int qemu_get_ram_fd(ram_addr_t addr);
31 void *qemu_get_ram_ptr(ram_addr_t addr);
32 void qemu_ram_free(ram_addr_t addr);
33 void qemu_ram_free_from_ptr(ram_addr_t addr);
34
35 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start,
36 ram_addr_t length,
37 unsigned client)
38 {
39 unsigned long end, page, next;
40
41 assert(client < DIRTY_MEMORY_NUM);
42
43 end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
44 page = start >> TARGET_PAGE_BITS;
45 next = find_next_bit(ram_list.dirty_memory[client], end, page);
46
47 return next < end;
48 }
49
50 static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr,
51 unsigned client)
52 {
53 return cpu_physical_memory_get_dirty(addr, 1, client);
54 }
55
56 static inline bool cpu_physical_memory_is_clean(ram_addr_t addr)
57 {
58 bool vga = cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_VGA);
59 bool code = cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_CODE);
60 bool migration =
61 cpu_physical_memory_get_dirty_flag(addr, DIRTY_MEMORY_MIGRATION);
62 return !(vga && code && migration);
63 }
64
65 static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr,
66 unsigned client)
67 {
68 assert(client < DIRTY_MEMORY_NUM);
69 set_bit(addr >> TARGET_PAGE_BITS, ram_list.dirty_memory[client]);
70 }
71
72 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start,
73 ram_addr_t length)
74 {
75 unsigned long end, page;
76
77 end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
78 page = start >> TARGET_PAGE_BITS;
79 bitmap_set(ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION], page, end - page);
80 bitmap_set(ram_list.dirty_memory[DIRTY_MEMORY_VGA], page, end - page);
81 bitmap_set(ram_list.dirty_memory[DIRTY_MEMORY_CODE], page, end - page);
82 xen_modified_memory(start, length);
83 }
84
85 #if !defined(_WIN32)
86 static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap,
87 ram_addr_t start,
88 ram_addr_t pages)
89 {
90 unsigned long i, j;
91 unsigned long page_number, c;
92 hwaddr addr;
93 ram_addr_t ram_addr;
94 unsigned long len = (pages + HOST_LONG_BITS - 1) / HOST_LONG_BITS;
95 unsigned long hpratio = getpagesize() / TARGET_PAGE_SIZE;
96 unsigned long page = BIT_WORD(start >> TARGET_PAGE_BITS);
97
98 /* start address is aligned at the start of a word? */
99 if ((((page * BITS_PER_LONG) << TARGET_PAGE_BITS) == start) &&
100 (hpratio == 1)) {
101 long k;
102 long nr = BITS_TO_LONGS(pages);
103
104 for (k = 0; k < nr; k++) {
105 if (bitmap[k]) {
106 unsigned long temp = leul_to_cpu(bitmap[k]);
107
108 ram_list.dirty_memory[DIRTY_MEMORY_MIGRATION][page + k] |= temp;
109 ram_list.dirty_memory[DIRTY_MEMORY_VGA][page + k] |= temp;
110 ram_list.dirty_memory[DIRTY_MEMORY_CODE][page + k] |= temp;
111 }
112 }
113 xen_modified_memory(start, pages);
114 } else {
115 /*
116 * bitmap-traveling is faster than memory-traveling (for addr...)
117 * especially when most of the memory is not dirty.
118 */
119 for (i = 0; i < len; i++) {
120 if (bitmap[i] != 0) {
121 c = leul_to_cpu(bitmap[i]);
122 do {
123 j = ctzl(c);
124 c &= ~(1ul << j);
125 page_number = (i * HOST_LONG_BITS + j) * hpratio;
126 addr = page_number * TARGET_PAGE_SIZE;
127 ram_addr = start + addr;
128 cpu_physical_memory_set_dirty_range(ram_addr,
129 TARGET_PAGE_SIZE * hpratio);
130 } while (c != 0);
131 }
132 }
133 }
134 }
135 #endif /* not _WIN32 */
136
137 static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start,
138 ram_addr_t length,
139 unsigned client)
140 {
141 unsigned long end, page;
142
143 assert(client < DIRTY_MEMORY_NUM);
144 end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
145 page = start >> TARGET_PAGE_BITS;
146 bitmap_clear(ram_list.dirty_memory[client], page, end - page);
147 }
148
149 void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t length,
150 unsigned client);
151
152 #endif
153 #endif