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"
25 ram_addr_t
qemu_ram_alloc_from_file(ram_addr_t size
, MemoryRegion
*mr
,
26 bool share
, const char *mem_path
,
28 ram_addr_t
qemu_ram_alloc_from_ptr(ram_addr_t size
, void *host
,
30 ram_addr_t
qemu_ram_alloc(ram_addr_t size
, MemoryRegion
*mr
);
31 int qemu_get_ram_fd(ram_addr_t addr
);
32 void *qemu_get_ram_block_host_ptr(ram_addr_t addr
);
33 void *qemu_get_ram_ptr(ram_addr_t addr
);
34 void qemu_ram_free(ram_addr_t addr
);
35 void qemu_ram_free_from_ptr(ram_addr_t addr
);
37 static inline bool cpu_physical_memory_get_dirty(ram_addr_t start
,
41 unsigned long end
, page
, next
;
43 assert(client
< DIRTY_MEMORY_NUM
);
45 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
46 page
= start
>> TARGET_PAGE_BITS
;
47 next
= find_next_bit(ram_list
.dirty_memory
[client
], end
, page
);
52 static inline bool cpu_physical_memory_get_dirty_flag(ram_addr_t addr
,
55 return cpu_physical_memory_get_dirty(addr
, 1, client
);
58 static inline bool cpu_physical_memory_is_clean(ram_addr_t addr
)
60 bool vga
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_VGA
);
61 bool code
= cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_CODE
);
63 cpu_physical_memory_get_dirty_flag(addr
, DIRTY_MEMORY_MIGRATION
);
64 return !(vga
&& code
&& migration
);
67 static inline void cpu_physical_memory_set_dirty_flag(ram_addr_t addr
,
70 assert(client
< DIRTY_MEMORY_NUM
);
71 set_bit(addr
>> TARGET_PAGE_BITS
, ram_list
.dirty_memory
[client
]);
74 static inline void cpu_physical_memory_set_dirty_range(ram_addr_t start
,
77 unsigned long end
, page
;
79 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
80 page
= start
>> TARGET_PAGE_BITS
;
81 bitmap_set(ram_list
.dirty_memory
[DIRTY_MEMORY_MIGRATION
], page
, end
- page
);
82 bitmap_set(ram_list
.dirty_memory
[DIRTY_MEMORY_VGA
], page
, end
- page
);
83 bitmap_set(ram_list
.dirty_memory
[DIRTY_MEMORY_CODE
], page
, end
- page
);
84 xen_modified_memory(start
, length
);
88 static inline void cpu_physical_memory_set_dirty_lebitmap(unsigned long *bitmap
,
93 unsigned long page_number
, c
;
96 unsigned long len
= (pages
+ HOST_LONG_BITS
- 1) / HOST_LONG_BITS
;
97 unsigned long hpratio
= getpagesize() / TARGET_PAGE_SIZE
;
98 unsigned long page
= BIT_WORD(start
>> TARGET_PAGE_BITS
);
100 /* start address is aligned at the start of a word? */
101 if ((((page
* BITS_PER_LONG
) << TARGET_PAGE_BITS
) == start
) &&
104 long nr
= BITS_TO_LONGS(pages
);
106 for (k
= 0; k
< nr
; k
++) {
108 unsigned long temp
= leul_to_cpu(bitmap
[k
]);
110 ram_list
.dirty_memory
[DIRTY_MEMORY_MIGRATION
][page
+ k
] |= temp
;
111 ram_list
.dirty_memory
[DIRTY_MEMORY_VGA
][page
+ k
] |= temp
;
112 ram_list
.dirty_memory
[DIRTY_MEMORY_CODE
][page
+ k
] |= temp
;
115 xen_modified_memory(start
, pages
);
118 * bitmap-traveling is faster than memory-traveling (for addr...)
119 * especially when most of the memory is not dirty.
121 for (i
= 0; i
< len
; i
++) {
122 if (bitmap
[i
] != 0) {
123 c
= leul_to_cpu(bitmap
[i
]);
127 page_number
= (i
* HOST_LONG_BITS
+ j
) * hpratio
;
128 addr
= page_number
* TARGET_PAGE_SIZE
;
129 ram_addr
= start
+ addr
;
130 cpu_physical_memory_set_dirty_range(ram_addr
,
131 TARGET_PAGE_SIZE
* hpratio
);
137 #endif /* not _WIN32 */
139 static inline void cpu_physical_memory_clear_dirty_range(ram_addr_t start
,
143 unsigned long end
, page
;
145 assert(client
< DIRTY_MEMORY_NUM
);
146 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
147 page
= start
>> TARGET_PAGE_BITS
;
148 bitmap_clear(ram_list
.dirty_memory
[client
], page
, end
- page
);
151 void cpu_physical_memory_reset_dirty(ram_addr_t start
, ram_addr_t length
,