]>
Commit | Line | Data |
---|---|---|
54936004 | 1 | /* |
5b6dd868 | 2 | * Virtual page mapping |
5fafdf24 | 3 | * |
54936004 FB |
4 | * Copyright (c) 2003 Fabrice Bellard |
5 | * | |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2 of the License, or (at your option) any later version. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
54936004 | 18 | */ |
67b915a5 | 19 | #include "config.h" |
d5a8f07c FB |
20 | #ifdef _WIN32 |
21 | #include <windows.h> | |
22 | #else | |
a98d49b1 | 23 | #include <sys/types.h> |
d5a8f07c FB |
24 | #include <sys/mman.h> |
25 | #endif | |
54936004 | 26 | |
055403b2 | 27 | #include "qemu-common.h" |
6180a181 | 28 | #include "cpu.h" |
b67d9a52 | 29 | #include "tcg.h" |
b3c7724c | 30 | #include "hw/hw.h" |
cc9e98cb | 31 | #include "hw/qdev.h" |
1de7afc9 | 32 | #include "qemu/osdep.h" |
9c17d615 | 33 | #include "sysemu/kvm.h" |
2ff3de68 | 34 | #include "sysemu/sysemu.h" |
0d09e41a | 35 | #include "hw/xen/xen.h" |
1de7afc9 PB |
36 | #include "qemu/timer.h" |
37 | #include "qemu/config-file.h" | |
022c62cb | 38 | #include "exec/memory.h" |
9c17d615 | 39 | #include "sysemu/dma.h" |
022c62cb | 40 | #include "exec/address-spaces.h" |
53a5960a PB |
41 | #if defined(CONFIG_USER_ONLY) |
42 | #include <qemu.h> | |
432d268c | 43 | #else /* !CONFIG_USER_ONLY */ |
9c17d615 | 44 | #include "sysemu/xen-mapcache.h" |
6506e4f9 | 45 | #include "trace.h" |
53a5960a | 46 | #endif |
0d6d3c87 | 47 | #include "exec/cpu-all.h" |
54936004 | 48 | |
022c62cb | 49 | #include "exec/cputlb.h" |
5b6dd868 | 50 | #include "translate-all.h" |
0cac1b66 | 51 | |
022c62cb | 52 | #include "exec/memory-internal.h" |
67d95c15 | 53 | |
db7b5426 | 54 | //#define DEBUG_SUBPAGE |
1196be37 | 55 | |
e2eef170 | 56 | #if !defined(CONFIG_USER_ONLY) |
74576198 | 57 | static int in_migration; |
94a6b54f | 58 | |
a3161038 | 59 | RAMList ram_list = { .blocks = QTAILQ_HEAD_INITIALIZER(ram_list.blocks) }; |
62152b8a AK |
60 | |
61 | static MemoryRegion *system_memory; | |
309cb471 | 62 | static MemoryRegion *system_io; |
62152b8a | 63 | |
f6790af6 AK |
64 | AddressSpace address_space_io; |
65 | AddressSpace address_space_memory; | |
2673a5da | 66 | |
0844e007 | 67 | MemoryRegion io_mem_rom, io_mem_notdirty; |
acc9d80b | 68 | static MemoryRegion io_mem_unassigned; |
0e0df1e2 | 69 | |
e2eef170 | 70 | #endif |
9fa3e853 | 71 | |
182735ef | 72 | CPUState *first_cpu; |
6a00d601 FB |
73 | /* current CPU in the current thread. It is only valid inside |
74 | cpu_exec() */ | |
4917cf44 | 75 | DEFINE_TLS(CPUState *, current_cpu); |
2e70f6ef | 76 | /* 0 = Do not count executed instructions. |
bf20dc07 | 77 | 1 = Precise instruction counting. |
2e70f6ef | 78 | 2 = Adaptive rate instruction counting. */ |
5708fc66 | 79 | int use_icount; |
6a00d601 | 80 | |
e2eef170 | 81 | #if !defined(CONFIG_USER_ONLY) |
4346ae3e | 82 | |
1db8abb1 PB |
83 | typedef struct PhysPageEntry PhysPageEntry; |
84 | ||
85 | struct PhysPageEntry { | |
86 | uint16_t is_leaf : 1; | |
87 | /* index into phys_sections (is_leaf) or phys_map_nodes (!is_leaf) */ | |
88 | uint16_t ptr : 15; | |
89 | }; | |
90 | ||
0475d94f PB |
91 | typedef PhysPageEntry Node[L2_SIZE]; |
92 | ||
1db8abb1 PB |
93 | struct AddressSpaceDispatch { |
94 | /* This is a multi-level map on the physical address space. | |
95 | * The bottom level has pointers to MemoryRegionSections. | |
96 | */ | |
97 | PhysPageEntry phys_map; | |
0475d94f PB |
98 | Node *nodes; |
99 | MemoryRegionSection *sections; | |
acc9d80b | 100 | AddressSpace *as; |
1db8abb1 PB |
101 | }; |
102 | ||
90260c6c JK |
103 | #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK) |
104 | typedef struct subpage_t { | |
105 | MemoryRegion iomem; | |
acc9d80b | 106 | AddressSpace *as; |
90260c6c JK |
107 | hwaddr base; |
108 | uint16_t sub_section[TARGET_PAGE_SIZE]; | |
109 | } subpage_t; | |
110 | ||
b41aac4f LPF |
111 | #define PHYS_SECTION_UNASSIGNED 0 |
112 | #define PHYS_SECTION_NOTDIRTY 1 | |
113 | #define PHYS_SECTION_ROM 2 | |
114 | #define PHYS_SECTION_WATCH 3 | |
5312bd8b | 115 | |
9affd6fc PB |
116 | typedef struct PhysPageMap { |
117 | unsigned sections_nb; | |
118 | unsigned sections_nb_alloc; | |
119 | unsigned nodes_nb; | |
120 | unsigned nodes_nb_alloc; | |
121 | Node *nodes; | |
122 | MemoryRegionSection *sections; | |
123 | } PhysPageMap; | |
124 | ||
6092666e | 125 | static PhysPageMap *prev_map; |
9affd6fc | 126 | static PhysPageMap next_map; |
d6f2ea22 | 127 | |
07f07b31 | 128 | #define PHYS_MAP_NODE_NIL (((uint16_t)~0) >> 1) |
d6f2ea22 | 129 | |
e2eef170 | 130 | static void io_mem_init(void); |
62152b8a | 131 | static void memory_map_init(void); |
8b9c99d9 | 132 | static void *qemu_safe_ram_ptr(ram_addr_t addr); |
e2eef170 | 133 | |
1ec9b909 | 134 | static MemoryRegion io_mem_watch; |
6658ffb8 | 135 | #endif |
fd6ce8f6 | 136 | |
6d9a1304 | 137 | #if !defined(CONFIG_USER_ONLY) |
d6f2ea22 | 138 | |
f7bf5461 | 139 | static void phys_map_node_reserve(unsigned nodes) |
d6f2ea22 | 140 | { |
9affd6fc PB |
141 | if (next_map.nodes_nb + nodes > next_map.nodes_nb_alloc) { |
142 | next_map.nodes_nb_alloc = MAX(next_map.nodes_nb_alloc * 2, | |
143 | 16); | |
144 | next_map.nodes_nb_alloc = MAX(next_map.nodes_nb_alloc, | |
145 | next_map.nodes_nb + nodes); | |
146 | next_map.nodes = g_renew(Node, next_map.nodes, | |
147 | next_map.nodes_nb_alloc); | |
d6f2ea22 | 148 | } |
f7bf5461 AK |
149 | } |
150 | ||
151 | static uint16_t phys_map_node_alloc(void) | |
152 | { | |
153 | unsigned i; | |
154 | uint16_t ret; | |
155 | ||
9affd6fc | 156 | ret = next_map.nodes_nb++; |
f7bf5461 | 157 | assert(ret != PHYS_MAP_NODE_NIL); |
9affd6fc | 158 | assert(ret != next_map.nodes_nb_alloc); |
d6f2ea22 | 159 | for (i = 0; i < L2_SIZE; ++i) { |
9affd6fc PB |
160 | next_map.nodes[ret][i].is_leaf = 0; |
161 | next_map.nodes[ret][i].ptr = PHYS_MAP_NODE_NIL; | |
d6f2ea22 | 162 | } |
f7bf5461 | 163 | return ret; |
d6f2ea22 AK |
164 | } |
165 | ||
a8170e5e AK |
166 | static void phys_page_set_level(PhysPageEntry *lp, hwaddr *index, |
167 | hwaddr *nb, uint16_t leaf, | |
2999097b | 168 | int level) |
f7bf5461 AK |
169 | { |
170 | PhysPageEntry *p; | |
171 | int i; | |
a8170e5e | 172 | hwaddr step = (hwaddr)1 << (level * L2_BITS); |
108c49b8 | 173 | |
07f07b31 | 174 | if (!lp->is_leaf && lp->ptr == PHYS_MAP_NODE_NIL) { |
c19e8800 | 175 | lp->ptr = phys_map_node_alloc(); |
9affd6fc | 176 | p = next_map.nodes[lp->ptr]; |
f7bf5461 AK |
177 | if (level == 0) { |
178 | for (i = 0; i < L2_SIZE; i++) { | |
07f07b31 | 179 | p[i].is_leaf = 1; |
b41aac4f | 180 | p[i].ptr = PHYS_SECTION_UNASSIGNED; |
4346ae3e | 181 | } |
67c4d23c | 182 | } |
f7bf5461 | 183 | } else { |
9affd6fc | 184 | p = next_map.nodes[lp->ptr]; |
92e873b9 | 185 | } |
2999097b | 186 | lp = &p[(*index >> (level * L2_BITS)) & (L2_SIZE - 1)]; |
f7bf5461 | 187 | |
2999097b | 188 | while (*nb && lp < &p[L2_SIZE]) { |
07f07b31 AK |
189 | if ((*index & (step - 1)) == 0 && *nb >= step) { |
190 | lp->is_leaf = true; | |
c19e8800 | 191 | lp->ptr = leaf; |
07f07b31 AK |
192 | *index += step; |
193 | *nb -= step; | |
2999097b AK |
194 | } else { |
195 | phys_page_set_level(lp, index, nb, leaf, level - 1); | |
196 | } | |
197 | ++lp; | |
f7bf5461 AK |
198 | } |
199 | } | |
200 | ||
ac1970fb | 201 | static void phys_page_set(AddressSpaceDispatch *d, |
a8170e5e | 202 | hwaddr index, hwaddr nb, |
2999097b | 203 | uint16_t leaf) |
f7bf5461 | 204 | { |
2999097b | 205 | /* Wildly overreserve - it doesn't matter much. */ |
07f07b31 | 206 | phys_map_node_reserve(3 * P_L2_LEVELS); |
5cd2c5b6 | 207 | |
ac1970fb | 208 | phys_page_set_level(&d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1); |
92e873b9 FB |
209 | } |
210 | ||
9affd6fc PB |
211 | static MemoryRegionSection *phys_page_find(PhysPageEntry lp, hwaddr index, |
212 | Node *nodes, MemoryRegionSection *sections) | |
92e873b9 | 213 | { |
31ab2b4a AK |
214 | PhysPageEntry *p; |
215 | int i; | |
f1f6e3b8 | 216 | |
07f07b31 | 217 | for (i = P_L2_LEVELS - 1; i >= 0 && !lp.is_leaf; i--) { |
c19e8800 | 218 | if (lp.ptr == PHYS_MAP_NODE_NIL) { |
9affd6fc | 219 | return §ions[PHYS_SECTION_UNASSIGNED]; |
31ab2b4a | 220 | } |
9affd6fc | 221 | p = nodes[lp.ptr]; |
31ab2b4a | 222 | lp = p[(index >> (i * L2_BITS)) & (L2_SIZE - 1)]; |
5312bd8b | 223 | } |
9affd6fc | 224 | return §ions[lp.ptr]; |
f3705d53 AK |
225 | } |
226 | ||
e5548617 BS |
227 | bool memory_region_is_unassigned(MemoryRegion *mr) |
228 | { | |
2a8e7499 | 229 | return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device |
5b6dd868 | 230 | && mr != &io_mem_watch; |
fd6ce8f6 | 231 | } |
149f54b5 | 232 | |
c7086b4a | 233 | static MemoryRegionSection *address_space_lookup_region(AddressSpaceDispatch *d, |
90260c6c JK |
234 | hwaddr addr, |
235 | bool resolve_subpage) | |
9f029603 | 236 | { |
90260c6c JK |
237 | MemoryRegionSection *section; |
238 | subpage_t *subpage; | |
239 | ||
0475d94f PB |
240 | section = phys_page_find(d->phys_map, addr >> TARGET_PAGE_BITS, |
241 | d->nodes, d->sections); | |
90260c6c JK |
242 | if (resolve_subpage && section->mr->subpage) { |
243 | subpage = container_of(section->mr, subpage_t, iomem); | |
0475d94f | 244 | section = &d->sections[subpage->sub_section[SUBPAGE_IDX(addr)]]; |
90260c6c JK |
245 | } |
246 | return section; | |
9f029603 JK |
247 | } |
248 | ||
90260c6c | 249 | static MemoryRegionSection * |
c7086b4a | 250 | address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *xlat, |
90260c6c | 251 | hwaddr *plen, bool resolve_subpage) |
149f54b5 PB |
252 | { |
253 | MemoryRegionSection *section; | |
254 | Int128 diff; | |
255 | ||
c7086b4a | 256 | section = address_space_lookup_region(d, addr, resolve_subpage); |
149f54b5 PB |
257 | /* Compute offset within MemoryRegionSection */ |
258 | addr -= section->offset_within_address_space; | |
259 | ||
260 | /* Compute offset within MemoryRegion */ | |
261 | *xlat = addr + section->offset_within_region; | |
262 | ||
263 | diff = int128_sub(section->mr->size, int128_make64(addr)); | |
3752a036 | 264 | *plen = int128_get64(int128_min(diff, int128_make64(*plen))); |
149f54b5 PB |
265 | return section; |
266 | } | |
90260c6c | 267 | |
5c8a00ce PB |
268 | MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr, |
269 | hwaddr *xlat, hwaddr *plen, | |
270 | bool is_write) | |
90260c6c | 271 | { |
30951157 AK |
272 | IOMMUTLBEntry iotlb; |
273 | MemoryRegionSection *section; | |
274 | MemoryRegion *mr; | |
275 | hwaddr len = *plen; | |
276 | ||
277 | for (;;) { | |
c7086b4a | 278 | section = address_space_translate_internal(as->dispatch, addr, &addr, plen, true); |
30951157 AK |
279 | mr = section->mr; |
280 | ||
281 | if (!mr->iommu_ops) { | |
282 | break; | |
283 | } | |
284 | ||
285 | iotlb = mr->iommu_ops->translate(mr, addr); | |
286 | addr = ((iotlb.translated_addr & ~iotlb.addr_mask) | |
287 | | (addr & iotlb.addr_mask)); | |
288 | len = MIN(len, (addr | iotlb.addr_mask) - addr + 1); | |
289 | if (!(iotlb.perm & (1 << is_write))) { | |
290 | mr = &io_mem_unassigned; | |
291 | break; | |
292 | } | |
293 | ||
294 | as = iotlb.target_as; | |
295 | } | |
296 | ||
297 | *plen = len; | |
298 | *xlat = addr; | |
299 | return mr; | |
90260c6c JK |
300 | } |
301 | ||
302 | MemoryRegionSection * | |
303 | address_space_translate_for_iotlb(AddressSpace *as, hwaddr addr, hwaddr *xlat, | |
304 | hwaddr *plen) | |
305 | { | |
30951157 | 306 | MemoryRegionSection *section; |
c7086b4a | 307 | section = address_space_translate_internal(as->dispatch, addr, xlat, plen, false); |
30951157 AK |
308 | |
309 | assert(!section->mr->iommu_ops); | |
310 | return section; | |
90260c6c | 311 | } |
5b6dd868 | 312 | #endif |
fd6ce8f6 | 313 | |
5b6dd868 | 314 | void cpu_exec_init_all(void) |
fdbb84d1 | 315 | { |
5b6dd868 | 316 | #if !defined(CONFIG_USER_ONLY) |
b2a8658e | 317 | qemu_mutex_init(&ram_list.mutex); |
5b6dd868 BS |
318 | memory_map_init(); |
319 | io_mem_init(); | |
fdbb84d1 | 320 | #endif |
5b6dd868 | 321 | } |
fdbb84d1 | 322 | |
b170fce3 | 323 | #if !defined(CONFIG_USER_ONLY) |
5b6dd868 BS |
324 | |
325 | static int cpu_common_post_load(void *opaque, int version_id) | |
fd6ce8f6 | 326 | { |
259186a7 | 327 | CPUState *cpu = opaque; |
a513fe19 | 328 | |
5b6dd868 BS |
329 | /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the |
330 | version_id is increased. */ | |
259186a7 AF |
331 | cpu->interrupt_request &= ~0x01; |
332 | tlb_flush(cpu->env_ptr, 1); | |
5b6dd868 BS |
333 | |
334 | return 0; | |
a513fe19 | 335 | } |
7501267e | 336 | |
1a1562f5 | 337 | const VMStateDescription vmstate_cpu_common = { |
5b6dd868 BS |
338 | .name = "cpu_common", |
339 | .version_id = 1, | |
340 | .minimum_version_id = 1, | |
341 | .minimum_version_id_old = 1, | |
342 | .post_load = cpu_common_post_load, | |
343 | .fields = (VMStateField []) { | |
259186a7 AF |
344 | VMSTATE_UINT32(halted, CPUState), |
345 | VMSTATE_UINT32(interrupt_request, CPUState), | |
5b6dd868 BS |
346 | VMSTATE_END_OF_LIST() |
347 | } | |
348 | }; | |
1a1562f5 | 349 | |
5b6dd868 | 350 | #endif |
ea041c0e | 351 | |
38d8f5c8 | 352 | CPUState *qemu_get_cpu(int index) |
ea041c0e | 353 | { |
182735ef | 354 | CPUState *cpu = first_cpu; |
ea041c0e | 355 | |
182735ef | 356 | while (cpu) { |
55e5c285 | 357 | if (cpu->cpu_index == index) { |
5b6dd868 | 358 | break; |
55e5c285 | 359 | } |
182735ef | 360 | cpu = cpu->next_cpu; |
ea041c0e | 361 | } |
5b6dd868 | 362 | |
182735ef | 363 | return cpu; |
ea041c0e FB |
364 | } |
365 | ||
d6b9e0d6 MT |
366 | void qemu_for_each_cpu(void (*func)(CPUState *cpu, void *data), void *data) |
367 | { | |
182735ef | 368 | CPUState *cpu; |
d6b9e0d6 | 369 | |
182735ef AF |
370 | cpu = first_cpu; |
371 | while (cpu) { | |
372 | func(cpu, data); | |
373 | cpu = cpu->next_cpu; | |
d6b9e0d6 MT |
374 | } |
375 | } | |
376 | ||
5b6dd868 | 377 | void cpu_exec_init(CPUArchState *env) |
ea041c0e | 378 | { |
5b6dd868 | 379 | CPUState *cpu = ENV_GET_CPU(env); |
b170fce3 | 380 | CPUClass *cc = CPU_GET_CLASS(cpu); |
182735ef | 381 | CPUState **pcpu; |
5b6dd868 BS |
382 | int cpu_index; |
383 | ||
384 | #if defined(CONFIG_USER_ONLY) | |
385 | cpu_list_lock(); | |
386 | #endif | |
182735ef AF |
387 | cpu->next_cpu = NULL; |
388 | pcpu = &first_cpu; | |
5b6dd868 | 389 | cpu_index = 0; |
182735ef AF |
390 | while (*pcpu != NULL) { |
391 | pcpu = &(*pcpu)->next_cpu; | |
5b6dd868 BS |
392 | cpu_index++; |
393 | } | |
55e5c285 | 394 | cpu->cpu_index = cpu_index; |
1b1ed8dc | 395 | cpu->numa_node = 0; |
5b6dd868 BS |
396 | QTAILQ_INIT(&env->breakpoints); |
397 | QTAILQ_INIT(&env->watchpoints); | |
398 | #ifndef CONFIG_USER_ONLY | |
399 | cpu->thread_id = qemu_get_thread_id(); | |
400 | #endif | |
182735ef | 401 | *pcpu = cpu; |
5b6dd868 BS |
402 | #if defined(CONFIG_USER_ONLY) |
403 | cpu_list_unlock(); | |
404 | #endif | |
e0d47944 AF |
405 | if (qdev_get_vmsd(DEVICE(cpu)) == NULL) { |
406 | vmstate_register(NULL, cpu_index, &vmstate_cpu_common, cpu); | |
407 | } | |
5b6dd868 | 408 | #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY) |
5b6dd868 BS |
409 | register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION, |
410 | cpu_save, cpu_load, env); | |
b170fce3 | 411 | assert(cc->vmsd == NULL); |
e0d47944 | 412 | assert(qdev_get_vmsd(DEVICE(cpu)) == NULL); |
5b6dd868 | 413 | #endif |
b170fce3 AF |
414 | if (cc->vmsd != NULL) { |
415 | vmstate_register(NULL, cpu_index, cc->vmsd, cpu); | |
416 | } | |
ea041c0e FB |
417 | } |
418 | ||
1fddef4b | 419 | #if defined(TARGET_HAS_ICE) |
94df27fd | 420 | #if defined(CONFIG_USER_ONLY) |
00b941e5 | 421 | static void breakpoint_invalidate(CPUState *cpu, target_ulong pc) |
94df27fd PB |
422 | { |
423 | tb_invalidate_phys_page_range(pc, pc + 1, 0); | |
424 | } | |
425 | #else | |
00b941e5 | 426 | static void breakpoint_invalidate(CPUState *cpu, target_ulong pc) |
1e7855a5 | 427 | { |
00b941e5 | 428 | tb_invalidate_phys_addr(cpu_get_phys_page_debug(cpu, pc) | |
9d70c4b7 | 429 | (pc & ~TARGET_PAGE_MASK)); |
1e7855a5 | 430 | } |
c27004ec | 431 | #endif |
94df27fd | 432 | #endif /* TARGET_HAS_ICE */ |
d720b93d | 433 | |
c527ee8f | 434 | #if defined(CONFIG_USER_ONLY) |
9349b4f9 | 435 | void cpu_watchpoint_remove_all(CPUArchState *env, int mask) |
c527ee8f PB |
436 | |
437 | { | |
438 | } | |
439 | ||
9349b4f9 | 440 | int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len, |
c527ee8f PB |
441 | int flags, CPUWatchpoint **watchpoint) |
442 | { | |
443 | return -ENOSYS; | |
444 | } | |
445 | #else | |
6658ffb8 | 446 | /* Add a watchpoint. */ |
9349b4f9 | 447 | int cpu_watchpoint_insert(CPUArchState *env, target_ulong addr, target_ulong len, |
a1d1bb31 | 448 | int flags, CPUWatchpoint **watchpoint) |
6658ffb8 | 449 | { |
b4051334 | 450 | target_ulong len_mask = ~(len - 1); |
c0ce998e | 451 | CPUWatchpoint *wp; |
6658ffb8 | 452 | |
b4051334 | 453 | /* sanity checks: allow power-of-2 lengths, deny unaligned watchpoints */ |
0dc23828 MF |
454 | if ((len & (len - 1)) || (addr & ~len_mask) || |
455 | len == 0 || len > TARGET_PAGE_SIZE) { | |
b4051334 AL |
456 | fprintf(stderr, "qemu: tried to set invalid watchpoint at " |
457 | TARGET_FMT_lx ", len=" TARGET_FMT_lu "\n", addr, len); | |
458 | return -EINVAL; | |
459 | } | |
7267c094 | 460 | wp = g_malloc(sizeof(*wp)); |
a1d1bb31 AL |
461 | |
462 | wp->vaddr = addr; | |
b4051334 | 463 | wp->len_mask = len_mask; |
a1d1bb31 AL |
464 | wp->flags = flags; |
465 | ||
2dc9f411 | 466 | /* keep all GDB-injected watchpoints in front */ |
c0ce998e | 467 | if (flags & BP_GDB) |
72cf2d4f | 468 | QTAILQ_INSERT_HEAD(&env->watchpoints, wp, entry); |
c0ce998e | 469 | else |
72cf2d4f | 470 | QTAILQ_INSERT_TAIL(&env->watchpoints, wp, entry); |
6658ffb8 | 471 | |
6658ffb8 | 472 | tlb_flush_page(env, addr); |
a1d1bb31 AL |
473 | |
474 | if (watchpoint) | |
475 | *watchpoint = wp; | |
476 | return 0; | |
6658ffb8 PB |
477 | } |
478 | ||
a1d1bb31 | 479 | /* Remove a specific watchpoint. */ |
9349b4f9 | 480 | int cpu_watchpoint_remove(CPUArchState *env, target_ulong addr, target_ulong len, |
a1d1bb31 | 481 | int flags) |
6658ffb8 | 482 | { |
b4051334 | 483 | target_ulong len_mask = ~(len - 1); |
a1d1bb31 | 484 | CPUWatchpoint *wp; |
6658ffb8 | 485 | |
72cf2d4f | 486 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
b4051334 | 487 | if (addr == wp->vaddr && len_mask == wp->len_mask |
6e140f28 | 488 | && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) { |
a1d1bb31 | 489 | cpu_watchpoint_remove_by_ref(env, wp); |
6658ffb8 PB |
490 | return 0; |
491 | } | |
492 | } | |
a1d1bb31 | 493 | return -ENOENT; |
6658ffb8 PB |
494 | } |
495 | ||
a1d1bb31 | 496 | /* Remove a specific watchpoint by reference. */ |
9349b4f9 | 497 | void cpu_watchpoint_remove_by_ref(CPUArchState *env, CPUWatchpoint *watchpoint) |
a1d1bb31 | 498 | { |
72cf2d4f | 499 | QTAILQ_REMOVE(&env->watchpoints, watchpoint, entry); |
7d03f82f | 500 | |
a1d1bb31 AL |
501 | tlb_flush_page(env, watchpoint->vaddr); |
502 | ||
7267c094 | 503 | g_free(watchpoint); |
a1d1bb31 AL |
504 | } |
505 | ||
506 | /* Remove all matching watchpoints. */ | |
9349b4f9 | 507 | void cpu_watchpoint_remove_all(CPUArchState *env, int mask) |
a1d1bb31 | 508 | { |
c0ce998e | 509 | CPUWatchpoint *wp, *next; |
a1d1bb31 | 510 | |
72cf2d4f | 511 | QTAILQ_FOREACH_SAFE(wp, &env->watchpoints, entry, next) { |
a1d1bb31 AL |
512 | if (wp->flags & mask) |
513 | cpu_watchpoint_remove_by_ref(env, wp); | |
c0ce998e | 514 | } |
7d03f82f | 515 | } |
c527ee8f | 516 | #endif |
7d03f82f | 517 | |
a1d1bb31 | 518 | /* Add a breakpoint. */ |
9349b4f9 | 519 | int cpu_breakpoint_insert(CPUArchState *env, target_ulong pc, int flags, |
a1d1bb31 | 520 | CPUBreakpoint **breakpoint) |
4c3a88a2 | 521 | { |
1fddef4b | 522 | #if defined(TARGET_HAS_ICE) |
c0ce998e | 523 | CPUBreakpoint *bp; |
3b46e624 | 524 | |
7267c094 | 525 | bp = g_malloc(sizeof(*bp)); |
4c3a88a2 | 526 | |
a1d1bb31 AL |
527 | bp->pc = pc; |
528 | bp->flags = flags; | |
529 | ||
2dc9f411 | 530 | /* keep all GDB-injected breakpoints in front */ |
00b941e5 | 531 | if (flags & BP_GDB) { |
72cf2d4f | 532 | QTAILQ_INSERT_HEAD(&env->breakpoints, bp, entry); |
00b941e5 | 533 | } else { |
72cf2d4f | 534 | QTAILQ_INSERT_TAIL(&env->breakpoints, bp, entry); |
00b941e5 | 535 | } |
3b46e624 | 536 | |
00b941e5 | 537 | breakpoint_invalidate(ENV_GET_CPU(env), pc); |
a1d1bb31 | 538 | |
00b941e5 | 539 | if (breakpoint) { |
a1d1bb31 | 540 | *breakpoint = bp; |
00b941e5 | 541 | } |
4c3a88a2 FB |
542 | return 0; |
543 | #else | |
a1d1bb31 | 544 | return -ENOSYS; |
4c3a88a2 FB |
545 | #endif |
546 | } | |
547 | ||
a1d1bb31 | 548 | /* Remove a specific breakpoint. */ |
9349b4f9 | 549 | int cpu_breakpoint_remove(CPUArchState *env, target_ulong pc, int flags) |
a1d1bb31 | 550 | { |
7d03f82f | 551 | #if defined(TARGET_HAS_ICE) |
a1d1bb31 AL |
552 | CPUBreakpoint *bp; |
553 | ||
72cf2d4f | 554 | QTAILQ_FOREACH(bp, &env->breakpoints, entry) { |
a1d1bb31 AL |
555 | if (bp->pc == pc && bp->flags == flags) { |
556 | cpu_breakpoint_remove_by_ref(env, bp); | |
557 | return 0; | |
558 | } | |
7d03f82f | 559 | } |
a1d1bb31 AL |
560 | return -ENOENT; |
561 | #else | |
562 | return -ENOSYS; | |
7d03f82f EI |
563 | #endif |
564 | } | |
565 | ||
a1d1bb31 | 566 | /* Remove a specific breakpoint by reference. */ |
9349b4f9 | 567 | void cpu_breakpoint_remove_by_ref(CPUArchState *env, CPUBreakpoint *breakpoint) |
4c3a88a2 | 568 | { |
1fddef4b | 569 | #if defined(TARGET_HAS_ICE) |
72cf2d4f | 570 | QTAILQ_REMOVE(&env->breakpoints, breakpoint, entry); |
d720b93d | 571 | |
00b941e5 | 572 | breakpoint_invalidate(ENV_GET_CPU(env), breakpoint->pc); |
a1d1bb31 | 573 | |
7267c094 | 574 | g_free(breakpoint); |
a1d1bb31 AL |
575 | #endif |
576 | } | |
577 | ||
578 | /* Remove all matching breakpoints. */ | |
9349b4f9 | 579 | void cpu_breakpoint_remove_all(CPUArchState *env, int mask) |
a1d1bb31 AL |
580 | { |
581 | #if defined(TARGET_HAS_ICE) | |
c0ce998e | 582 | CPUBreakpoint *bp, *next; |
a1d1bb31 | 583 | |
72cf2d4f | 584 | QTAILQ_FOREACH_SAFE(bp, &env->breakpoints, entry, next) { |
a1d1bb31 AL |
585 | if (bp->flags & mask) |
586 | cpu_breakpoint_remove_by_ref(env, bp); | |
c0ce998e | 587 | } |
4c3a88a2 FB |
588 | #endif |
589 | } | |
590 | ||
c33a346e FB |
591 | /* enable or disable single step mode. EXCP_DEBUG is returned by the |
592 | CPU loop after each instruction */ | |
3825b28f | 593 | void cpu_single_step(CPUState *cpu, int enabled) |
c33a346e | 594 | { |
1fddef4b | 595 | #if defined(TARGET_HAS_ICE) |
ed2803da AF |
596 | if (cpu->singlestep_enabled != enabled) { |
597 | cpu->singlestep_enabled = enabled; | |
598 | if (kvm_enabled()) { | |
38e478ec | 599 | kvm_update_guest_debug(cpu, 0); |
ed2803da | 600 | } else { |
ccbb4d44 | 601 | /* must flush all the translated code to avoid inconsistencies */ |
e22a25c9 | 602 | /* XXX: only flush what is necessary */ |
38e478ec | 603 | CPUArchState *env = cpu->env_ptr; |
e22a25c9 AL |
604 | tb_flush(env); |
605 | } | |
c33a346e FB |
606 | } |
607 | #endif | |
608 | } | |
609 | ||
9349b4f9 | 610 | void cpu_abort(CPUArchState *env, const char *fmt, ...) |
7501267e | 611 | { |
878096ee | 612 | CPUState *cpu = ENV_GET_CPU(env); |
7501267e | 613 | va_list ap; |
493ae1f0 | 614 | va_list ap2; |
7501267e FB |
615 | |
616 | va_start(ap, fmt); | |
493ae1f0 | 617 | va_copy(ap2, ap); |
7501267e FB |
618 | fprintf(stderr, "qemu: fatal: "); |
619 | vfprintf(stderr, fmt, ap); | |
620 | fprintf(stderr, "\n"); | |
878096ee | 621 | cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP); |
93fcfe39 AL |
622 | if (qemu_log_enabled()) { |
623 | qemu_log("qemu: fatal: "); | |
624 | qemu_log_vprintf(fmt, ap2); | |
625 | qemu_log("\n"); | |
a0762859 | 626 | log_cpu_state(cpu, CPU_DUMP_FPU | CPU_DUMP_CCOP); |
31b1a7b4 | 627 | qemu_log_flush(); |
93fcfe39 | 628 | qemu_log_close(); |
924edcae | 629 | } |
493ae1f0 | 630 | va_end(ap2); |
f9373291 | 631 | va_end(ap); |
fd052bf6 RV |
632 | #if defined(CONFIG_USER_ONLY) |
633 | { | |
634 | struct sigaction act; | |
635 | sigfillset(&act.sa_mask); | |
636 | act.sa_handler = SIG_DFL; | |
637 | sigaction(SIGABRT, &act, NULL); | |
638 | } | |
639 | #endif | |
7501267e FB |
640 | abort(); |
641 | } | |
642 | ||
9349b4f9 | 643 | CPUArchState *cpu_copy(CPUArchState *env) |
c5be9f08 | 644 | { |
9349b4f9 | 645 | CPUArchState *new_env = cpu_init(env->cpu_model_str); |
5a38f081 AL |
646 | #if defined(TARGET_HAS_ICE) |
647 | CPUBreakpoint *bp; | |
648 | CPUWatchpoint *wp; | |
649 | #endif | |
650 | ||
b24c882b AG |
651 | /* Reset non arch specific state */ |
652 | cpu_reset(ENV_GET_CPU(new_env)); | |
653 | ||
654 | /* Copy arch specific state into the new CPU */ | |
9349b4f9 | 655 | memcpy(new_env, env, sizeof(CPUArchState)); |
5a38f081 | 656 | |
5a38f081 AL |
657 | /* Clone all break/watchpoints. |
658 | Note: Once we support ptrace with hw-debug register access, make sure | |
659 | BP_CPU break/watchpoints are handled correctly on clone. */ | |
72cf2d4f BS |
660 | QTAILQ_INIT(&env->breakpoints); |
661 | QTAILQ_INIT(&env->watchpoints); | |
5a38f081 | 662 | #if defined(TARGET_HAS_ICE) |
72cf2d4f | 663 | QTAILQ_FOREACH(bp, &env->breakpoints, entry) { |
5a38f081 AL |
664 | cpu_breakpoint_insert(new_env, bp->pc, bp->flags, NULL); |
665 | } | |
72cf2d4f | 666 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
5a38f081 AL |
667 | cpu_watchpoint_insert(new_env, wp->vaddr, (~wp->len_mask) + 1, |
668 | wp->flags, NULL); | |
669 | } | |
670 | #endif | |
671 | ||
c5be9f08 TS |
672 | return new_env; |
673 | } | |
674 | ||
0124311e | 675 | #if !defined(CONFIG_USER_ONLY) |
d24981d3 JQ |
676 | static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t end, |
677 | uintptr_t length) | |
678 | { | |
679 | uintptr_t start1; | |
680 | ||
681 | /* we modify the TLB cache so that the dirty bit will be set again | |
682 | when accessing the range */ | |
683 | start1 = (uintptr_t)qemu_safe_ram_ptr(start); | |
684 | /* Check that we don't span multiple blocks - this breaks the | |
685 | address comparisons below. */ | |
686 | if ((uintptr_t)qemu_safe_ram_ptr(end - 1) - start1 | |
687 | != (end - 1) - start) { | |
688 | abort(); | |
689 | } | |
690 | cpu_tlb_reset_dirty_all(start1, length); | |
691 | ||
692 | } | |
693 | ||
5579c7f3 | 694 | /* Note: start and end must be within the same ram block. */ |
c227f099 | 695 | void cpu_physical_memory_reset_dirty(ram_addr_t start, ram_addr_t end, |
0a962c02 | 696 | int dirty_flags) |
1ccde1cb | 697 | { |
d24981d3 | 698 | uintptr_t length; |
1ccde1cb FB |
699 | |
700 | start &= TARGET_PAGE_MASK; | |
701 | end = TARGET_PAGE_ALIGN(end); | |
702 | ||
703 | length = end - start; | |
704 | if (length == 0) | |
705 | return; | |
f7c11b53 | 706 | cpu_physical_memory_mask_dirty_range(start, length, dirty_flags); |
f23db169 | 707 | |
d24981d3 JQ |
708 | if (tcg_enabled()) { |
709 | tlb_reset_dirty_range_all(start, end, length); | |
5579c7f3 | 710 | } |
1ccde1cb FB |
711 | } |
712 | ||
8b9c99d9 | 713 | static int cpu_physical_memory_set_dirty_tracking(int enable) |
74576198 | 714 | { |
f6f3fbca | 715 | int ret = 0; |
74576198 | 716 | in_migration = enable; |
f6f3fbca | 717 | return ret; |
74576198 AL |
718 | } |
719 | ||
a8170e5e | 720 | hwaddr memory_region_section_get_iotlb(CPUArchState *env, |
149f54b5 PB |
721 | MemoryRegionSection *section, |
722 | target_ulong vaddr, | |
723 | hwaddr paddr, hwaddr xlat, | |
724 | int prot, | |
725 | target_ulong *address) | |
e5548617 | 726 | { |
a8170e5e | 727 | hwaddr iotlb; |
e5548617 BS |
728 | CPUWatchpoint *wp; |
729 | ||
cc5bea60 | 730 | if (memory_region_is_ram(section->mr)) { |
e5548617 BS |
731 | /* Normal RAM. */ |
732 | iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK) | |
149f54b5 | 733 | + xlat; |
e5548617 | 734 | if (!section->readonly) { |
b41aac4f | 735 | iotlb |= PHYS_SECTION_NOTDIRTY; |
e5548617 | 736 | } else { |
b41aac4f | 737 | iotlb |= PHYS_SECTION_ROM; |
e5548617 BS |
738 | } |
739 | } else { | |
0475d94f | 740 | iotlb = section - address_space_memory.dispatch->sections; |
149f54b5 | 741 | iotlb += xlat; |
e5548617 BS |
742 | } |
743 | ||
744 | /* Make accesses to pages with watchpoints go via the | |
745 | watchpoint trap routines. */ | |
746 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { | |
747 | if (vaddr == (wp->vaddr & TARGET_PAGE_MASK)) { | |
748 | /* Avoid trapping reads of pages with a write breakpoint. */ | |
749 | if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) { | |
b41aac4f | 750 | iotlb = PHYS_SECTION_WATCH + paddr; |
e5548617 BS |
751 | *address |= TLB_MMIO; |
752 | break; | |
753 | } | |
754 | } | |
755 | } | |
756 | ||
757 | return iotlb; | |
758 | } | |
9fa3e853 FB |
759 | #endif /* defined(CONFIG_USER_ONLY) */ |
760 | ||
e2eef170 | 761 | #if !defined(CONFIG_USER_ONLY) |
8da3ff18 | 762 | |
c227f099 | 763 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
5312bd8b | 764 | uint16_t section); |
acc9d80b | 765 | static subpage_t *subpage_init(AddressSpace *as, hwaddr base); |
54688b1e | 766 | |
5312bd8b AK |
767 | static uint16_t phys_section_add(MemoryRegionSection *section) |
768 | { | |
68f3f65b PB |
769 | /* The physical section number is ORed with a page-aligned |
770 | * pointer to produce the iotlb entries. Thus it should | |
771 | * never overflow into the page-aligned value. | |
772 | */ | |
9affd6fc | 773 | assert(next_map.sections_nb < TARGET_PAGE_SIZE); |
68f3f65b | 774 | |
9affd6fc PB |
775 | if (next_map.sections_nb == next_map.sections_nb_alloc) { |
776 | next_map.sections_nb_alloc = MAX(next_map.sections_nb_alloc * 2, | |
777 | 16); | |
778 | next_map.sections = g_renew(MemoryRegionSection, next_map.sections, | |
779 | next_map.sections_nb_alloc); | |
5312bd8b | 780 | } |
9affd6fc | 781 | next_map.sections[next_map.sections_nb] = *section; |
dfde4e6e | 782 | memory_region_ref(section->mr); |
9affd6fc | 783 | return next_map.sections_nb++; |
5312bd8b AK |
784 | } |
785 | ||
058bc4b5 PB |
786 | static void phys_section_destroy(MemoryRegion *mr) |
787 | { | |
dfde4e6e PB |
788 | memory_region_unref(mr); |
789 | ||
058bc4b5 PB |
790 | if (mr->subpage) { |
791 | subpage_t *subpage = container_of(mr, subpage_t, iomem); | |
792 | memory_region_destroy(&subpage->iomem); | |
793 | g_free(subpage); | |
794 | } | |
795 | } | |
796 | ||
6092666e | 797 | static void phys_sections_free(PhysPageMap *map) |
5312bd8b | 798 | { |
9affd6fc PB |
799 | while (map->sections_nb > 0) { |
800 | MemoryRegionSection *section = &map->sections[--map->sections_nb]; | |
058bc4b5 PB |
801 | phys_section_destroy(section->mr); |
802 | } | |
9affd6fc PB |
803 | g_free(map->sections); |
804 | g_free(map->nodes); | |
6092666e | 805 | g_free(map); |
5312bd8b AK |
806 | } |
807 | ||
ac1970fb | 808 | static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section) |
0f0cb164 AK |
809 | { |
810 | subpage_t *subpage; | |
a8170e5e | 811 | hwaddr base = section->offset_within_address_space |
0f0cb164 | 812 | & TARGET_PAGE_MASK; |
9affd6fc PB |
813 | MemoryRegionSection *existing = phys_page_find(d->phys_map, base >> TARGET_PAGE_BITS, |
814 | next_map.nodes, next_map.sections); | |
0f0cb164 AK |
815 | MemoryRegionSection subsection = { |
816 | .offset_within_address_space = base, | |
052e87b0 | 817 | .size = int128_make64(TARGET_PAGE_SIZE), |
0f0cb164 | 818 | }; |
a8170e5e | 819 | hwaddr start, end; |
0f0cb164 | 820 | |
f3705d53 | 821 | assert(existing->mr->subpage || existing->mr == &io_mem_unassigned); |
0f0cb164 | 822 | |
f3705d53 | 823 | if (!(existing->mr->subpage)) { |
acc9d80b | 824 | subpage = subpage_init(d->as, base); |
0f0cb164 | 825 | subsection.mr = &subpage->iomem; |
ac1970fb | 826 | phys_page_set(d, base >> TARGET_PAGE_BITS, 1, |
2999097b | 827 | phys_section_add(&subsection)); |
0f0cb164 | 828 | } else { |
f3705d53 | 829 | subpage = container_of(existing->mr, subpage_t, iomem); |
0f0cb164 AK |
830 | } |
831 | start = section->offset_within_address_space & ~TARGET_PAGE_MASK; | |
052e87b0 | 832 | end = start + int128_get64(section->size) - 1; |
0f0cb164 AK |
833 | subpage_register(subpage, start, end, phys_section_add(section)); |
834 | } | |
835 | ||
836 | ||
052e87b0 PB |
837 | static void register_multipage(AddressSpaceDispatch *d, |
838 | MemoryRegionSection *section) | |
33417e70 | 839 | { |
a8170e5e | 840 | hwaddr start_addr = section->offset_within_address_space; |
5312bd8b | 841 | uint16_t section_index = phys_section_add(section); |
052e87b0 PB |
842 | uint64_t num_pages = int128_get64(int128_rshift(section->size, |
843 | TARGET_PAGE_BITS)); | |
dd81124b | 844 | |
733d5ef5 PB |
845 | assert(num_pages); |
846 | phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index); | |
33417e70 FB |
847 | } |
848 | ||
ac1970fb | 849 | static void mem_add(MemoryListener *listener, MemoryRegionSection *section) |
0f0cb164 | 850 | { |
89ae337a | 851 | AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener); |
00752703 | 852 | AddressSpaceDispatch *d = as->next_dispatch; |
99b9cc06 | 853 | MemoryRegionSection now = *section, remain = *section; |
052e87b0 | 854 | Int128 page_size = int128_make64(TARGET_PAGE_SIZE); |
0f0cb164 | 855 | |
733d5ef5 PB |
856 | if (now.offset_within_address_space & ~TARGET_PAGE_MASK) { |
857 | uint64_t left = TARGET_PAGE_ALIGN(now.offset_within_address_space) | |
858 | - now.offset_within_address_space; | |
859 | ||
052e87b0 | 860 | now.size = int128_min(int128_make64(left), now.size); |
ac1970fb | 861 | register_subpage(d, &now); |
733d5ef5 | 862 | } else { |
052e87b0 | 863 | now.size = int128_zero(); |
733d5ef5 | 864 | } |
052e87b0 PB |
865 | while (int128_ne(remain.size, now.size)) { |
866 | remain.size = int128_sub(remain.size, now.size); | |
867 | remain.offset_within_address_space += int128_get64(now.size); | |
868 | remain.offset_within_region += int128_get64(now.size); | |
69b67646 | 869 | now = remain; |
052e87b0 | 870 | if (int128_lt(remain.size, page_size)) { |
733d5ef5 PB |
871 | register_subpage(d, &now); |
872 | } else if (remain.offset_within_region & ~TARGET_PAGE_MASK) { | |
052e87b0 | 873 | now.size = page_size; |
ac1970fb | 874 | register_subpage(d, &now); |
69b67646 | 875 | } else { |
052e87b0 | 876 | now.size = int128_and(now.size, int128_neg(page_size)); |
ac1970fb | 877 | register_multipage(d, &now); |
69b67646 | 878 | } |
0f0cb164 AK |
879 | } |
880 | } | |
881 | ||
62a2744c SY |
882 | void qemu_flush_coalesced_mmio_buffer(void) |
883 | { | |
884 | if (kvm_enabled()) | |
885 | kvm_flush_coalesced_mmio_buffer(); | |
886 | } | |
887 | ||
b2a8658e UD |
888 | void qemu_mutex_lock_ramlist(void) |
889 | { | |
890 | qemu_mutex_lock(&ram_list.mutex); | |
891 | } | |
892 | ||
893 | void qemu_mutex_unlock_ramlist(void) | |
894 | { | |
895 | qemu_mutex_unlock(&ram_list.mutex); | |
896 | } | |
897 | ||
c902760f MT |
898 | #if defined(__linux__) && !defined(TARGET_S390X) |
899 | ||
900 | #include <sys/vfs.h> | |
901 | ||
902 | #define HUGETLBFS_MAGIC 0x958458f6 | |
903 | ||
904 | static long gethugepagesize(const char *path) | |
905 | { | |
906 | struct statfs fs; | |
907 | int ret; | |
908 | ||
909 | do { | |
9742bf26 | 910 | ret = statfs(path, &fs); |
c902760f MT |
911 | } while (ret != 0 && errno == EINTR); |
912 | ||
913 | if (ret != 0) { | |
9742bf26 YT |
914 | perror(path); |
915 | return 0; | |
c902760f MT |
916 | } |
917 | ||
918 | if (fs.f_type != HUGETLBFS_MAGIC) | |
9742bf26 | 919 | fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path); |
c902760f MT |
920 | |
921 | return fs.f_bsize; | |
922 | } | |
923 | ||
04b16653 AW |
924 | static void *file_ram_alloc(RAMBlock *block, |
925 | ram_addr_t memory, | |
926 | const char *path) | |
c902760f MT |
927 | { |
928 | char *filename; | |
8ca761f6 PF |
929 | char *sanitized_name; |
930 | char *c; | |
c902760f MT |
931 | void *area; |
932 | int fd; | |
933 | #ifdef MAP_POPULATE | |
934 | int flags; | |
935 | #endif | |
936 | unsigned long hpagesize; | |
937 | ||
938 | hpagesize = gethugepagesize(path); | |
939 | if (!hpagesize) { | |
9742bf26 | 940 | return NULL; |
c902760f MT |
941 | } |
942 | ||
943 | if (memory < hpagesize) { | |
944 | return NULL; | |
945 | } | |
946 | ||
947 | if (kvm_enabled() && !kvm_has_sync_mmu()) { | |
948 | fprintf(stderr, "host lacks kvm mmu notifiers, -mem-path unsupported\n"); | |
949 | return NULL; | |
950 | } | |
951 | ||
8ca761f6 PF |
952 | /* Make name safe to use with mkstemp by replacing '/' with '_'. */ |
953 | sanitized_name = g_strdup(block->mr->name); | |
954 | for (c = sanitized_name; *c != '\0'; c++) { | |
955 | if (*c == '/') | |
956 | *c = '_'; | |
957 | } | |
958 | ||
959 | filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path, | |
960 | sanitized_name); | |
961 | g_free(sanitized_name); | |
c902760f MT |
962 | |
963 | fd = mkstemp(filename); | |
964 | if (fd < 0) { | |
9742bf26 | 965 | perror("unable to create backing store for hugepages"); |
e4ada482 | 966 | g_free(filename); |
9742bf26 | 967 | return NULL; |
c902760f MT |
968 | } |
969 | unlink(filename); | |
e4ada482 | 970 | g_free(filename); |
c902760f MT |
971 | |
972 | memory = (memory+hpagesize-1) & ~(hpagesize-1); | |
973 | ||
974 | /* | |
975 | * ftruncate is not supported by hugetlbfs in older | |
976 | * hosts, so don't bother bailing out on errors. | |
977 | * If anything goes wrong with it under other filesystems, | |
978 | * mmap will fail. | |
979 | */ | |
980 | if (ftruncate(fd, memory)) | |
9742bf26 | 981 | perror("ftruncate"); |
c902760f MT |
982 | |
983 | #ifdef MAP_POPULATE | |
984 | /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case | |
985 | * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED | |
986 | * to sidestep this quirk. | |
987 | */ | |
988 | flags = mem_prealloc ? MAP_POPULATE | MAP_SHARED : MAP_PRIVATE; | |
989 | area = mmap(0, memory, PROT_READ | PROT_WRITE, flags, fd, 0); | |
990 | #else | |
991 | area = mmap(0, memory, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
992 | #endif | |
993 | if (area == MAP_FAILED) { | |
9742bf26 YT |
994 | perror("file_ram_alloc: can't mmap RAM pages"); |
995 | close(fd); | |
996 | return (NULL); | |
c902760f | 997 | } |
04b16653 | 998 | block->fd = fd; |
c902760f MT |
999 | return area; |
1000 | } | |
1001 | #endif | |
1002 | ||
d17b5288 | 1003 | static ram_addr_t find_ram_offset(ram_addr_t size) |
04b16653 AW |
1004 | { |
1005 | RAMBlock *block, *next_block; | |
3e837b2c | 1006 | ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX; |
04b16653 | 1007 | |
49cd9ac6 SH |
1008 | assert(size != 0); /* it would hand out same offset multiple times */ |
1009 | ||
a3161038 | 1010 | if (QTAILQ_EMPTY(&ram_list.blocks)) |
04b16653 AW |
1011 | return 0; |
1012 | ||
a3161038 | 1013 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
f15fbc4b | 1014 | ram_addr_t end, next = RAM_ADDR_MAX; |
04b16653 AW |
1015 | |
1016 | end = block->offset + block->length; | |
1017 | ||
a3161038 | 1018 | QTAILQ_FOREACH(next_block, &ram_list.blocks, next) { |
04b16653 AW |
1019 | if (next_block->offset >= end) { |
1020 | next = MIN(next, next_block->offset); | |
1021 | } | |
1022 | } | |
1023 | if (next - end >= size && next - end < mingap) { | |
3e837b2c | 1024 | offset = end; |
04b16653 AW |
1025 | mingap = next - end; |
1026 | } | |
1027 | } | |
3e837b2c AW |
1028 | |
1029 | if (offset == RAM_ADDR_MAX) { | |
1030 | fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n", | |
1031 | (uint64_t)size); | |
1032 | abort(); | |
1033 | } | |
1034 | ||
04b16653 AW |
1035 | return offset; |
1036 | } | |
1037 | ||
652d7ec2 | 1038 | ram_addr_t last_ram_offset(void) |
d17b5288 AW |
1039 | { |
1040 | RAMBlock *block; | |
1041 | ram_addr_t last = 0; | |
1042 | ||
a3161038 | 1043 | QTAILQ_FOREACH(block, &ram_list.blocks, next) |
d17b5288 AW |
1044 | last = MAX(last, block->offset + block->length); |
1045 | ||
1046 | return last; | |
1047 | } | |
1048 | ||
ddb97f1d JB |
1049 | static void qemu_ram_setup_dump(void *addr, ram_addr_t size) |
1050 | { | |
1051 | int ret; | |
ddb97f1d JB |
1052 | |
1053 | /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */ | |
2ff3de68 MA |
1054 | if (!qemu_opt_get_bool(qemu_get_machine_opts(), |
1055 | "dump-guest-core", true)) { | |
ddb97f1d JB |
1056 | ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP); |
1057 | if (ret) { | |
1058 | perror("qemu_madvise"); | |
1059 | fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, " | |
1060 | "but dump_guest_core=off specified\n"); | |
1061 | } | |
1062 | } | |
1063 | } | |
1064 | ||
c5705a77 | 1065 | void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev) |
84b89d78 CM |
1066 | { |
1067 | RAMBlock *new_block, *block; | |
1068 | ||
c5705a77 | 1069 | new_block = NULL; |
a3161038 | 1070 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
c5705a77 AK |
1071 | if (block->offset == addr) { |
1072 | new_block = block; | |
1073 | break; | |
1074 | } | |
1075 | } | |
1076 | assert(new_block); | |
1077 | assert(!new_block->idstr[0]); | |
84b89d78 | 1078 | |
09e5ab63 AL |
1079 | if (dev) { |
1080 | char *id = qdev_get_dev_path(dev); | |
84b89d78 CM |
1081 | if (id) { |
1082 | snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id); | |
7267c094 | 1083 | g_free(id); |
84b89d78 CM |
1084 | } |
1085 | } | |
1086 | pstrcat(new_block->idstr, sizeof(new_block->idstr), name); | |
1087 | ||
b2a8658e UD |
1088 | /* This assumes the iothread lock is taken here too. */ |
1089 | qemu_mutex_lock_ramlist(); | |
a3161038 | 1090 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
c5705a77 | 1091 | if (block != new_block && !strcmp(block->idstr, new_block->idstr)) { |
84b89d78 CM |
1092 | fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n", |
1093 | new_block->idstr); | |
1094 | abort(); | |
1095 | } | |
1096 | } | |
b2a8658e | 1097 | qemu_mutex_unlock_ramlist(); |
c5705a77 AK |
1098 | } |
1099 | ||
8490fc78 LC |
1100 | static int memory_try_enable_merging(void *addr, size_t len) |
1101 | { | |
2ff3de68 | 1102 | if (!qemu_opt_get_bool(qemu_get_machine_opts(), "mem-merge", true)) { |
8490fc78 LC |
1103 | /* disabled by the user */ |
1104 | return 0; | |
1105 | } | |
1106 | ||
1107 | return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE); | |
1108 | } | |
1109 | ||
c5705a77 AK |
1110 | ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host, |
1111 | MemoryRegion *mr) | |
1112 | { | |
abb26d63 | 1113 | RAMBlock *block, *new_block; |
c5705a77 AK |
1114 | |
1115 | size = TARGET_PAGE_ALIGN(size); | |
1116 | new_block = g_malloc0(sizeof(*new_block)); | |
84b89d78 | 1117 | |
b2a8658e UD |
1118 | /* This assumes the iothread lock is taken here too. */ |
1119 | qemu_mutex_lock_ramlist(); | |
7c637366 | 1120 | new_block->mr = mr; |
432d268c | 1121 | new_block->offset = find_ram_offset(size); |
6977dfe6 YT |
1122 | if (host) { |
1123 | new_block->host = host; | |
cd19cfa2 | 1124 | new_block->flags |= RAM_PREALLOC_MASK; |
6977dfe6 YT |
1125 | } else { |
1126 | if (mem_path) { | |
c902760f | 1127 | #if defined (__linux__) && !defined(TARGET_S390X) |
6977dfe6 YT |
1128 | new_block->host = file_ram_alloc(new_block, size, mem_path); |
1129 | if (!new_block->host) { | |
6eebf958 | 1130 | new_block->host = qemu_anon_ram_alloc(size); |
8490fc78 | 1131 | memory_try_enable_merging(new_block->host, size); |
6977dfe6 | 1132 | } |
c902760f | 1133 | #else |
6977dfe6 YT |
1134 | fprintf(stderr, "-mem-path option unsupported\n"); |
1135 | exit(1); | |
c902760f | 1136 | #endif |
6977dfe6 | 1137 | } else { |
868bb33f | 1138 | if (xen_enabled()) { |
fce537d4 | 1139 | xen_ram_alloc(new_block->offset, size, mr); |
fdec9918 CB |
1140 | } else if (kvm_enabled()) { |
1141 | /* some s390/kvm configurations have special constraints */ | |
6eebf958 | 1142 | new_block->host = kvm_ram_alloc(size); |
432d268c | 1143 | } else { |
6eebf958 | 1144 | new_block->host = qemu_anon_ram_alloc(size); |
432d268c | 1145 | } |
8490fc78 | 1146 | memory_try_enable_merging(new_block->host, size); |
6977dfe6 | 1147 | } |
c902760f | 1148 | } |
94a6b54f PB |
1149 | new_block->length = size; |
1150 | ||
abb26d63 PB |
1151 | /* Keep the list sorted from biggest to smallest block. */ |
1152 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { | |
1153 | if (block->length < new_block->length) { | |
1154 | break; | |
1155 | } | |
1156 | } | |
1157 | if (block) { | |
1158 | QTAILQ_INSERT_BEFORE(block, new_block, next); | |
1159 | } else { | |
1160 | QTAILQ_INSERT_TAIL(&ram_list.blocks, new_block, next); | |
1161 | } | |
0d6d3c87 | 1162 | ram_list.mru_block = NULL; |
94a6b54f | 1163 | |
f798b07f | 1164 | ram_list.version++; |
b2a8658e | 1165 | qemu_mutex_unlock_ramlist(); |
f798b07f | 1166 | |
7267c094 | 1167 | ram_list.phys_dirty = g_realloc(ram_list.phys_dirty, |
04b16653 | 1168 | last_ram_offset() >> TARGET_PAGE_BITS); |
5fda043f IM |
1169 | memset(ram_list.phys_dirty + (new_block->offset >> TARGET_PAGE_BITS), |
1170 | 0, size >> TARGET_PAGE_BITS); | |
1720aeee | 1171 | cpu_physical_memory_set_dirty_range(new_block->offset, size, 0xff); |
94a6b54f | 1172 | |
ddb97f1d | 1173 | qemu_ram_setup_dump(new_block->host, size); |
ad0b5321 | 1174 | qemu_madvise(new_block->host, size, QEMU_MADV_HUGEPAGE); |
ddb97f1d | 1175 | |
6f0437e8 JK |
1176 | if (kvm_enabled()) |
1177 | kvm_setup_guest_memory(new_block->host, size); | |
1178 | ||
94a6b54f PB |
1179 | return new_block->offset; |
1180 | } | |
e9a1ab19 | 1181 | |
c5705a77 | 1182 | ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr) |
6977dfe6 | 1183 | { |
c5705a77 | 1184 | return qemu_ram_alloc_from_ptr(size, NULL, mr); |
6977dfe6 YT |
1185 | } |
1186 | ||
1f2e98b6 AW |
1187 | void qemu_ram_free_from_ptr(ram_addr_t addr) |
1188 | { | |
1189 | RAMBlock *block; | |
1190 | ||
b2a8658e UD |
1191 | /* This assumes the iothread lock is taken here too. */ |
1192 | qemu_mutex_lock_ramlist(); | |
a3161038 | 1193 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
1f2e98b6 | 1194 | if (addr == block->offset) { |
a3161038 | 1195 | QTAILQ_REMOVE(&ram_list.blocks, block, next); |
0d6d3c87 | 1196 | ram_list.mru_block = NULL; |
f798b07f | 1197 | ram_list.version++; |
7267c094 | 1198 | g_free(block); |
b2a8658e | 1199 | break; |
1f2e98b6 AW |
1200 | } |
1201 | } | |
b2a8658e | 1202 | qemu_mutex_unlock_ramlist(); |
1f2e98b6 AW |
1203 | } |
1204 | ||
c227f099 | 1205 | void qemu_ram_free(ram_addr_t addr) |
e9a1ab19 | 1206 | { |
04b16653 AW |
1207 | RAMBlock *block; |
1208 | ||
b2a8658e UD |
1209 | /* This assumes the iothread lock is taken here too. */ |
1210 | qemu_mutex_lock_ramlist(); | |
a3161038 | 1211 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
04b16653 | 1212 | if (addr == block->offset) { |
a3161038 | 1213 | QTAILQ_REMOVE(&ram_list.blocks, block, next); |
0d6d3c87 | 1214 | ram_list.mru_block = NULL; |
f798b07f | 1215 | ram_list.version++; |
cd19cfa2 HY |
1216 | if (block->flags & RAM_PREALLOC_MASK) { |
1217 | ; | |
1218 | } else if (mem_path) { | |
04b16653 AW |
1219 | #if defined (__linux__) && !defined(TARGET_S390X) |
1220 | if (block->fd) { | |
1221 | munmap(block->host, block->length); | |
1222 | close(block->fd); | |
1223 | } else { | |
e7a09b92 | 1224 | qemu_anon_ram_free(block->host, block->length); |
04b16653 | 1225 | } |
fd28aa13 JK |
1226 | #else |
1227 | abort(); | |
04b16653 AW |
1228 | #endif |
1229 | } else { | |
868bb33f | 1230 | if (xen_enabled()) { |
e41d7c69 | 1231 | xen_invalidate_map_cache_entry(block->host); |
432d268c | 1232 | } else { |
e7a09b92 | 1233 | qemu_anon_ram_free(block->host, block->length); |
432d268c | 1234 | } |
04b16653 | 1235 | } |
7267c094 | 1236 | g_free(block); |
b2a8658e | 1237 | break; |
04b16653 AW |
1238 | } |
1239 | } | |
b2a8658e | 1240 | qemu_mutex_unlock_ramlist(); |
04b16653 | 1241 | |
e9a1ab19 FB |
1242 | } |
1243 | ||
cd19cfa2 HY |
1244 | #ifndef _WIN32 |
1245 | void qemu_ram_remap(ram_addr_t addr, ram_addr_t length) | |
1246 | { | |
1247 | RAMBlock *block; | |
1248 | ram_addr_t offset; | |
1249 | int flags; | |
1250 | void *area, *vaddr; | |
1251 | ||
a3161038 | 1252 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
cd19cfa2 HY |
1253 | offset = addr - block->offset; |
1254 | if (offset < block->length) { | |
1255 | vaddr = block->host + offset; | |
1256 | if (block->flags & RAM_PREALLOC_MASK) { | |
1257 | ; | |
1258 | } else { | |
1259 | flags = MAP_FIXED; | |
1260 | munmap(vaddr, length); | |
1261 | if (mem_path) { | |
1262 | #if defined(__linux__) && !defined(TARGET_S390X) | |
1263 | if (block->fd) { | |
1264 | #ifdef MAP_POPULATE | |
1265 | flags |= mem_prealloc ? MAP_POPULATE | MAP_SHARED : | |
1266 | MAP_PRIVATE; | |
1267 | #else | |
1268 | flags |= MAP_PRIVATE; | |
1269 | #endif | |
1270 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, | |
1271 | flags, block->fd, offset); | |
1272 | } else { | |
1273 | flags |= MAP_PRIVATE | MAP_ANONYMOUS; | |
1274 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, | |
1275 | flags, -1, 0); | |
1276 | } | |
fd28aa13 JK |
1277 | #else |
1278 | abort(); | |
cd19cfa2 HY |
1279 | #endif |
1280 | } else { | |
1281 | #if defined(TARGET_S390X) && defined(CONFIG_KVM) | |
1282 | flags |= MAP_SHARED | MAP_ANONYMOUS; | |
1283 | area = mmap(vaddr, length, PROT_EXEC|PROT_READ|PROT_WRITE, | |
1284 | flags, -1, 0); | |
1285 | #else | |
1286 | flags |= MAP_PRIVATE | MAP_ANONYMOUS; | |
1287 | area = mmap(vaddr, length, PROT_READ | PROT_WRITE, | |
1288 | flags, -1, 0); | |
1289 | #endif | |
1290 | } | |
1291 | if (area != vaddr) { | |
f15fbc4b AP |
1292 | fprintf(stderr, "Could not remap addr: " |
1293 | RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n", | |
cd19cfa2 HY |
1294 | length, addr); |
1295 | exit(1); | |
1296 | } | |
8490fc78 | 1297 | memory_try_enable_merging(vaddr, length); |
ddb97f1d | 1298 | qemu_ram_setup_dump(vaddr, length); |
cd19cfa2 HY |
1299 | } |
1300 | return; | |
1301 | } | |
1302 | } | |
1303 | } | |
1304 | #endif /* !_WIN32 */ | |
1305 | ||
1b5ec234 | 1306 | static RAMBlock *qemu_get_ram_block(ram_addr_t addr) |
dc828ca1 | 1307 | { |
94a6b54f PB |
1308 | RAMBlock *block; |
1309 | ||
b2a8658e | 1310 | /* The list is protected by the iothread lock here. */ |
0d6d3c87 PB |
1311 | block = ram_list.mru_block; |
1312 | if (block && addr - block->offset < block->length) { | |
1313 | goto found; | |
1314 | } | |
a3161038 | 1315 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
f471a17e | 1316 | if (addr - block->offset < block->length) { |
0d6d3c87 | 1317 | goto found; |
f471a17e | 1318 | } |
94a6b54f | 1319 | } |
f471a17e AW |
1320 | |
1321 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); | |
1322 | abort(); | |
1323 | ||
0d6d3c87 PB |
1324 | found: |
1325 | ram_list.mru_block = block; | |
1b5ec234 PB |
1326 | return block; |
1327 | } | |
1328 | ||
1329 | /* Return a host pointer to ram allocated with qemu_ram_alloc. | |
1330 | With the exception of the softmmu code in this file, this should | |
1331 | only be used for local memory (e.g. video ram) that the device owns, | |
1332 | and knows it isn't going to access beyond the end of the block. | |
1333 | ||
1334 | It should not be used for general purpose DMA. | |
1335 | Use cpu_physical_memory_map/cpu_physical_memory_rw instead. | |
1336 | */ | |
1337 | void *qemu_get_ram_ptr(ram_addr_t addr) | |
1338 | { | |
1339 | RAMBlock *block = qemu_get_ram_block(addr); | |
1340 | ||
0d6d3c87 PB |
1341 | if (xen_enabled()) { |
1342 | /* We need to check if the requested address is in the RAM | |
1343 | * because we don't want to map the entire memory in QEMU. | |
1344 | * In that case just map until the end of the page. | |
1345 | */ | |
1346 | if (block->offset == 0) { | |
1347 | return xen_map_cache(addr, 0, 0); | |
1348 | } else if (block->host == NULL) { | |
1349 | block->host = | |
1350 | xen_map_cache(block->offset, block->length, 1); | |
1351 | } | |
1352 | } | |
1353 | return block->host + (addr - block->offset); | |
dc828ca1 PB |
1354 | } |
1355 | ||
0d6d3c87 PB |
1356 | /* Return a host pointer to ram allocated with qemu_ram_alloc. Same as |
1357 | * qemu_get_ram_ptr but do not touch ram_list.mru_block. | |
1358 | * | |
1359 | * ??? Is this still necessary? | |
b2e0a138 | 1360 | */ |
8b9c99d9 | 1361 | static void *qemu_safe_ram_ptr(ram_addr_t addr) |
b2e0a138 MT |
1362 | { |
1363 | RAMBlock *block; | |
1364 | ||
b2a8658e | 1365 | /* The list is protected by the iothread lock here. */ |
a3161038 | 1366 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
b2e0a138 | 1367 | if (addr - block->offset < block->length) { |
868bb33f | 1368 | if (xen_enabled()) { |
432d268c JN |
1369 | /* We need to check if the requested address is in the RAM |
1370 | * because we don't want to map the entire memory in QEMU. | |
712c2b41 | 1371 | * In that case just map until the end of the page. |
432d268c JN |
1372 | */ |
1373 | if (block->offset == 0) { | |
e41d7c69 | 1374 | return xen_map_cache(addr, 0, 0); |
432d268c | 1375 | } else if (block->host == NULL) { |
e41d7c69 JK |
1376 | block->host = |
1377 | xen_map_cache(block->offset, block->length, 1); | |
432d268c JN |
1378 | } |
1379 | } | |
b2e0a138 MT |
1380 | return block->host + (addr - block->offset); |
1381 | } | |
1382 | } | |
1383 | ||
1384 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); | |
1385 | abort(); | |
1386 | ||
1387 | return NULL; | |
1388 | } | |
1389 | ||
38bee5dc SS |
1390 | /* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr |
1391 | * but takes a size argument */ | |
cb85f7ab | 1392 | static void *qemu_ram_ptr_length(ram_addr_t addr, hwaddr *size) |
38bee5dc | 1393 | { |
8ab934f9 SS |
1394 | if (*size == 0) { |
1395 | return NULL; | |
1396 | } | |
868bb33f | 1397 | if (xen_enabled()) { |
e41d7c69 | 1398 | return xen_map_cache(addr, *size, 1); |
868bb33f | 1399 | } else { |
38bee5dc SS |
1400 | RAMBlock *block; |
1401 | ||
a3161038 | 1402 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
38bee5dc SS |
1403 | if (addr - block->offset < block->length) { |
1404 | if (addr - block->offset + *size > block->length) | |
1405 | *size = block->length - addr + block->offset; | |
1406 | return block->host + (addr - block->offset); | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr); | |
1411 | abort(); | |
38bee5dc SS |
1412 | } |
1413 | } | |
1414 | ||
7443b437 PB |
1415 | /* Some of the softmmu routines need to translate from a host pointer |
1416 | (typically a TLB entry) back to a ram offset. */ | |
1b5ec234 | 1417 | MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr) |
5579c7f3 | 1418 | { |
94a6b54f PB |
1419 | RAMBlock *block; |
1420 | uint8_t *host = ptr; | |
1421 | ||
868bb33f | 1422 | if (xen_enabled()) { |
e41d7c69 | 1423 | *ram_addr = xen_ram_addr_from_mapcache(ptr); |
1b5ec234 | 1424 | return qemu_get_ram_block(*ram_addr)->mr; |
712c2b41 SS |
1425 | } |
1426 | ||
23887b79 PB |
1427 | block = ram_list.mru_block; |
1428 | if (block && block->host && host - block->host < block->length) { | |
1429 | goto found; | |
1430 | } | |
1431 | ||
a3161038 | 1432 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { |
432d268c JN |
1433 | /* This case append when the block is not mapped. */ |
1434 | if (block->host == NULL) { | |
1435 | continue; | |
1436 | } | |
f471a17e | 1437 | if (host - block->host < block->length) { |
23887b79 | 1438 | goto found; |
f471a17e | 1439 | } |
94a6b54f | 1440 | } |
432d268c | 1441 | |
1b5ec234 | 1442 | return NULL; |
23887b79 PB |
1443 | |
1444 | found: | |
1445 | *ram_addr = block->offset + (host - block->host); | |
1b5ec234 | 1446 | return block->mr; |
e890261f | 1447 | } |
f471a17e | 1448 | |
a8170e5e | 1449 | static void notdirty_mem_write(void *opaque, hwaddr ram_addr, |
0e0df1e2 | 1450 | uint64_t val, unsigned size) |
9fa3e853 | 1451 | { |
3a7d929e | 1452 | int dirty_flags; |
f7c11b53 | 1453 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
3a7d929e | 1454 | if (!(dirty_flags & CODE_DIRTY_FLAG)) { |
0e0df1e2 | 1455 | tb_invalidate_phys_page_fast(ram_addr, size); |
f7c11b53 | 1456 | dirty_flags = cpu_physical_memory_get_dirty_flags(ram_addr); |
3a7d929e | 1457 | } |
0e0df1e2 AK |
1458 | switch (size) { |
1459 | case 1: | |
1460 | stb_p(qemu_get_ram_ptr(ram_addr), val); | |
1461 | break; | |
1462 | case 2: | |
1463 | stw_p(qemu_get_ram_ptr(ram_addr), val); | |
1464 | break; | |
1465 | case 4: | |
1466 | stl_p(qemu_get_ram_ptr(ram_addr), val); | |
1467 | break; | |
1468 | default: | |
1469 | abort(); | |
3a7d929e | 1470 | } |
f23db169 | 1471 | dirty_flags |= (0xff & ~CODE_DIRTY_FLAG); |
f7c11b53 | 1472 | cpu_physical_memory_set_dirty_flags(ram_addr, dirty_flags); |
f23db169 FB |
1473 | /* we remove the notdirty callback only if the code has been |
1474 | flushed */ | |
4917cf44 AF |
1475 | if (dirty_flags == 0xff) { |
1476 | CPUArchState *env = current_cpu->env_ptr; | |
1477 | tlb_set_dirty(env, env->mem_io_vaddr); | |
1478 | } | |
9fa3e853 FB |
1479 | } |
1480 | ||
b018ddf6 PB |
1481 | static bool notdirty_mem_accepts(void *opaque, hwaddr addr, |
1482 | unsigned size, bool is_write) | |
1483 | { | |
1484 | return is_write; | |
1485 | } | |
1486 | ||
0e0df1e2 | 1487 | static const MemoryRegionOps notdirty_mem_ops = { |
0e0df1e2 | 1488 | .write = notdirty_mem_write, |
b018ddf6 | 1489 | .valid.accepts = notdirty_mem_accepts, |
0e0df1e2 | 1490 | .endianness = DEVICE_NATIVE_ENDIAN, |
1ccde1cb FB |
1491 | }; |
1492 | ||
0f459d16 | 1493 | /* Generate a debug exception if a watchpoint has been hit. */ |
b4051334 | 1494 | static void check_watchpoint(int offset, int len_mask, int flags) |
0f459d16 | 1495 | { |
4917cf44 | 1496 | CPUArchState *env = current_cpu->env_ptr; |
06d55cc1 | 1497 | target_ulong pc, cs_base; |
0f459d16 | 1498 | target_ulong vaddr; |
a1d1bb31 | 1499 | CPUWatchpoint *wp; |
06d55cc1 | 1500 | int cpu_flags; |
0f459d16 | 1501 | |
06d55cc1 AL |
1502 | if (env->watchpoint_hit) { |
1503 | /* We re-entered the check after replacing the TB. Now raise | |
1504 | * the debug interrupt so that is will trigger after the | |
1505 | * current instruction. */ | |
c3affe56 | 1506 | cpu_interrupt(ENV_GET_CPU(env), CPU_INTERRUPT_DEBUG); |
06d55cc1 AL |
1507 | return; |
1508 | } | |
2e70f6ef | 1509 | vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset; |
72cf2d4f | 1510 | QTAILQ_FOREACH(wp, &env->watchpoints, entry) { |
b4051334 AL |
1511 | if ((vaddr == (wp->vaddr & len_mask) || |
1512 | (vaddr & wp->len_mask) == wp->vaddr) && (wp->flags & flags)) { | |
6e140f28 AL |
1513 | wp->flags |= BP_WATCHPOINT_HIT; |
1514 | if (!env->watchpoint_hit) { | |
1515 | env->watchpoint_hit = wp; | |
5a316526 | 1516 | tb_check_watchpoint(env); |
6e140f28 AL |
1517 | if (wp->flags & BP_STOP_BEFORE_ACCESS) { |
1518 | env->exception_index = EXCP_DEBUG; | |
488d6577 | 1519 | cpu_loop_exit(env); |
6e140f28 AL |
1520 | } else { |
1521 | cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags); | |
1522 | tb_gen_code(env, pc, cs_base, cpu_flags, 1); | |
488d6577 | 1523 | cpu_resume_from_signal(env, NULL); |
6e140f28 | 1524 | } |
06d55cc1 | 1525 | } |
6e140f28 AL |
1526 | } else { |
1527 | wp->flags &= ~BP_WATCHPOINT_HIT; | |
0f459d16 PB |
1528 | } |
1529 | } | |
1530 | } | |
1531 | ||
6658ffb8 PB |
1532 | /* Watchpoint access routines. Watchpoints are inserted using TLB tricks, |
1533 | so these check for a hit then pass through to the normal out-of-line | |
1534 | phys routines. */ | |
a8170e5e | 1535 | static uint64_t watch_mem_read(void *opaque, hwaddr addr, |
1ec9b909 | 1536 | unsigned size) |
6658ffb8 | 1537 | { |
1ec9b909 AK |
1538 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_READ); |
1539 | switch (size) { | |
1540 | case 1: return ldub_phys(addr); | |
1541 | case 2: return lduw_phys(addr); | |
1542 | case 4: return ldl_phys(addr); | |
1543 | default: abort(); | |
1544 | } | |
6658ffb8 PB |
1545 | } |
1546 | ||
a8170e5e | 1547 | static void watch_mem_write(void *opaque, hwaddr addr, |
1ec9b909 | 1548 | uint64_t val, unsigned size) |
6658ffb8 | 1549 | { |
1ec9b909 AK |
1550 | check_watchpoint(addr & ~TARGET_PAGE_MASK, ~(size - 1), BP_MEM_WRITE); |
1551 | switch (size) { | |
67364150 MF |
1552 | case 1: |
1553 | stb_phys(addr, val); | |
1554 | break; | |
1555 | case 2: | |
1556 | stw_phys(addr, val); | |
1557 | break; | |
1558 | case 4: | |
1559 | stl_phys(addr, val); | |
1560 | break; | |
1ec9b909 AK |
1561 | default: abort(); |
1562 | } | |
6658ffb8 PB |
1563 | } |
1564 | ||
1ec9b909 AK |
1565 | static const MemoryRegionOps watch_mem_ops = { |
1566 | .read = watch_mem_read, | |
1567 | .write = watch_mem_write, | |
1568 | .endianness = DEVICE_NATIVE_ENDIAN, | |
6658ffb8 | 1569 | }; |
6658ffb8 | 1570 | |
a8170e5e | 1571 | static uint64_t subpage_read(void *opaque, hwaddr addr, |
70c68e44 | 1572 | unsigned len) |
db7b5426 | 1573 | { |
acc9d80b JK |
1574 | subpage_t *subpage = opaque; |
1575 | uint8_t buf[4]; | |
791af8c8 | 1576 | |
db7b5426 | 1577 | #if defined(DEBUG_SUBPAGE) |
acc9d80b JK |
1578 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx "\n", __func__, |
1579 | subpage, len, addr); | |
db7b5426 | 1580 | #endif |
acc9d80b JK |
1581 | address_space_read(subpage->as, addr + subpage->base, buf, len); |
1582 | switch (len) { | |
1583 | case 1: | |
1584 | return ldub_p(buf); | |
1585 | case 2: | |
1586 | return lduw_p(buf); | |
1587 | case 4: | |
1588 | return ldl_p(buf); | |
1589 | default: | |
1590 | abort(); | |
1591 | } | |
db7b5426 BS |
1592 | } |
1593 | ||
a8170e5e | 1594 | static void subpage_write(void *opaque, hwaddr addr, |
70c68e44 | 1595 | uint64_t value, unsigned len) |
db7b5426 | 1596 | { |
acc9d80b JK |
1597 | subpage_t *subpage = opaque; |
1598 | uint8_t buf[4]; | |
1599 | ||
db7b5426 | 1600 | #if defined(DEBUG_SUBPAGE) |
70c68e44 | 1601 | printf("%s: subpage %p len %d addr " TARGET_FMT_plx |
acc9d80b JK |
1602 | " value %"PRIx64"\n", |
1603 | __func__, subpage, len, addr, value); | |
db7b5426 | 1604 | #endif |
acc9d80b JK |
1605 | switch (len) { |
1606 | case 1: | |
1607 | stb_p(buf, value); | |
1608 | break; | |
1609 | case 2: | |
1610 | stw_p(buf, value); | |
1611 | break; | |
1612 | case 4: | |
1613 | stl_p(buf, value); | |
1614 | break; | |
1615 | default: | |
1616 | abort(); | |
1617 | } | |
1618 | address_space_write(subpage->as, addr + subpage->base, buf, len); | |
db7b5426 BS |
1619 | } |
1620 | ||
c353e4cc PB |
1621 | static bool subpage_accepts(void *opaque, hwaddr addr, |
1622 | unsigned size, bool is_write) | |
1623 | { | |
acc9d80b | 1624 | subpage_t *subpage = opaque; |
c353e4cc | 1625 | #if defined(DEBUG_SUBPAGE) |
acc9d80b JK |
1626 | printf("%s: subpage %p %c len %d addr " TARGET_FMT_plx "\n", |
1627 | __func__, subpage, is_write ? 'w' : 'r', len, addr); | |
c353e4cc PB |
1628 | #endif |
1629 | ||
acc9d80b JK |
1630 | return address_space_access_valid(subpage->as, addr + subpage->base, |
1631 | size, is_write); | |
c353e4cc PB |
1632 | } |
1633 | ||
70c68e44 AK |
1634 | static const MemoryRegionOps subpage_ops = { |
1635 | .read = subpage_read, | |
1636 | .write = subpage_write, | |
c353e4cc | 1637 | .valid.accepts = subpage_accepts, |
70c68e44 | 1638 | .endianness = DEVICE_NATIVE_ENDIAN, |
db7b5426 BS |
1639 | }; |
1640 | ||
c227f099 | 1641 | static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end, |
5312bd8b | 1642 | uint16_t section) |
db7b5426 BS |
1643 | { |
1644 | int idx, eidx; | |
1645 | ||
1646 | if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE) | |
1647 | return -1; | |
1648 | idx = SUBPAGE_IDX(start); | |
1649 | eidx = SUBPAGE_IDX(end); | |
1650 | #if defined(DEBUG_SUBPAGE) | |
0bf9e31a | 1651 | printf("%s: %p start %08x end %08x idx %08x eidx %08x mem %ld\n", __func__, |
db7b5426 BS |
1652 | mmio, start, end, idx, eidx, memory); |
1653 | #endif | |
db7b5426 | 1654 | for (; idx <= eidx; idx++) { |
5312bd8b | 1655 | mmio->sub_section[idx] = section; |
db7b5426 BS |
1656 | } |
1657 | ||
1658 | return 0; | |
1659 | } | |
1660 | ||
acc9d80b | 1661 | static subpage_t *subpage_init(AddressSpace *as, hwaddr base) |
db7b5426 | 1662 | { |
c227f099 | 1663 | subpage_t *mmio; |
db7b5426 | 1664 | |
7267c094 | 1665 | mmio = g_malloc0(sizeof(subpage_t)); |
1eec614b | 1666 | |
acc9d80b | 1667 | mmio->as = as; |
1eec614b | 1668 | mmio->base = base; |
2c9b15ca | 1669 | memory_region_init_io(&mmio->iomem, NULL, &subpage_ops, mmio, |
70c68e44 | 1670 | "subpage", TARGET_PAGE_SIZE); |
b3b00c78 | 1671 | mmio->iomem.subpage = true; |
db7b5426 | 1672 | #if defined(DEBUG_SUBPAGE) |
1eec614b AL |
1673 | printf("%s: %p base " TARGET_FMT_plx " len %08x %d\n", __func__, |
1674 | mmio, base, TARGET_PAGE_SIZE, subpage_memory); | |
db7b5426 | 1675 | #endif |
b41aac4f | 1676 | subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, PHYS_SECTION_UNASSIGNED); |
db7b5426 BS |
1677 | |
1678 | return mmio; | |
1679 | } | |
1680 | ||
5312bd8b AK |
1681 | static uint16_t dummy_section(MemoryRegion *mr) |
1682 | { | |
1683 | MemoryRegionSection section = { | |
1684 | .mr = mr, | |
1685 | .offset_within_address_space = 0, | |
1686 | .offset_within_region = 0, | |
052e87b0 | 1687 | .size = int128_2_64(), |
5312bd8b AK |
1688 | }; |
1689 | ||
1690 | return phys_section_add(§ion); | |
1691 | } | |
1692 | ||
a8170e5e | 1693 | MemoryRegion *iotlb_to_region(hwaddr index) |
aa102231 | 1694 | { |
0475d94f | 1695 | return address_space_memory.dispatch->sections[index & ~TARGET_PAGE_MASK].mr; |
aa102231 AK |
1696 | } |
1697 | ||
e9179ce1 AK |
1698 | static void io_mem_init(void) |
1699 | { | |
2c9b15ca PB |
1700 | memory_region_init_io(&io_mem_rom, NULL, &unassigned_mem_ops, NULL, "rom", UINT64_MAX); |
1701 | memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, NULL, | |
0e0df1e2 | 1702 | "unassigned", UINT64_MAX); |
2c9b15ca | 1703 | memory_region_init_io(&io_mem_notdirty, NULL, ¬dirty_mem_ops, NULL, |
0e0df1e2 | 1704 | "notdirty", UINT64_MAX); |
2c9b15ca | 1705 | memory_region_init_io(&io_mem_watch, NULL, &watch_mem_ops, NULL, |
1ec9b909 | 1706 | "watch", UINT64_MAX); |
e9179ce1 AK |
1707 | } |
1708 | ||
ac1970fb | 1709 | static void mem_begin(MemoryListener *listener) |
00752703 PB |
1710 | { |
1711 | AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener); | |
1712 | AddressSpaceDispatch *d = g_new(AddressSpaceDispatch, 1); | |
1713 | ||
1714 | d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .is_leaf = 0 }; | |
1715 | d->as = as; | |
1716 | as->next_dispatch = d; | |
1717 | } | |
1718 | ||
1719 | static void mem_commit(MemoryListener *listener) | |
ac1970fb | 1720 | { |
89ae337a | 1721 | AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener); |
0475d94f PB |
1722 | AddressSpaceDispatch *cur = as->dispatch; |
1723 | AddressSpaceDispatch *next = as->next_dispatch; | |
1724 | ||
1725 | next->nodes = next_map.nodes; | |
1726 | next->sections = next_map.sections; | |
ac1970fb | 1727 | |
0475d94f PB |
1728 | as->dispatch = next; |
1729 | g_free(cur); | |
ac1970fb AK |
1730 | } |
1731 | ||
50c1e149 AK |
1732 | static void core_begin(MemoryListener *listener) |
1733 | { | |
b41aac4f LPF |
1734 | uint16_t n; |
1735 | ||
6092666e PB |
1736 | prev_map = g_new(PhysPageMap, 1); |
1737 | *prev_map = next_map; | |
1738 | ||
9affd6fc | 1739 | memset(&next_map, 0, sizeof(next_map)); |
b41aac4f LPF |
1740 | n = dummy_section(&io_mem_unassigned); |
1741 | assert(n == PHYS_SECTION_UNASSIGNED); | |
1742 | n = dummy_section(&io_mem_notdirty); | |
1743 | assert(n == PHYS_SECTION_NOTDIRTY); | |
1744 | n = dummy_section(&io_mem_rom); | |
1745 | assert(n == PHYS_SECTION_ROM); | |
1746 | n = dummy_section(&io_mem_watch); | |
1747 | assert(n == PHYS_SECTION_WATCH); | |
50c1e149 AK |
1748 | } |
1749 | ||
9affd6fc PB |
1750 | /* This listener's commit run after the other AddressSpaceDispatch listeners'. |
1751 | * All AddressSpaceDispatch instances have switched to the next map. | |
1752 | */ | |
1753 | static void core_commit(MemoryListener *listener) | |
1754 | { | |
6092666e | 1755 | phys_sections_free(prev_map); |
9affd6fc PB |
1756 | } |
1757 | ||
1d71148e | 1758 | static void tcg_commit(MemoryListener *listener) |
50c1e149 | 1759 | { |
182735ef | 1760 | CPUState *cpu; |
117712c3 AK |
1761 | |
1762 | /* since each CPU stores ram addresses in its TLB cache, we must | |
1763 | reset the modified entries */ | |
1764 | /* XXX: slow ! */ | |
182735ef AF |
1765 | for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) { |
1766 | CPUArchState *env = cpu->env_ptr; | |
1767 | ||
117712c3 AK |
1768 | tlb_flush(env, 1); |
1769 | } | |
50c1e149 AK |
1770 | } |
1771 | ||
93632747 AK |
1772 | static void core_log_global_start(MemoryListener *listener) |
1773 | { | |
1774 | cpu_physical_memory_set_dirty_tracking(1); | |
1775 | } | |
1776 | ||
1777 | static void core_log_global_stop(MemoryListener *listener) | |
1778 | { | |
1779 | cpu_physical_memory_set_dirty_tracking(0); | |
1780 | } | |
1781 | ||
93632747 | 1782 | static MemoryListener core_memory_listener = { |
50c1e149 | 1783 | .begin = core_begin, |
9affd6fc | 1784 | .commit = core_commit, |
93632747 AK |
1785 | .log_global_start = core_log_global_start, |
1786 | .log_global_stop = core_log_global_stop, | |
ac1970fb | 1787 | .priority = 1, |
93632747 AK |
1788 | }; |
1789 | ||
1d71148e AK |
1790 | static MemoryListener tcg_memory_listener = { |
1791 | .commit = tcg_commit, | |
1792 | }; | |
1793 | ||
ac1970fb AK |
1794 | void address_space_init_dispatch(AddressSpace *as) |
1795 | { | |
00752703 | 1796 | as->dispatch = NULL; |
89ae337a | 1797 | as->dispatch_listener = (MemoryListener) { |
ac1970fb | 1798 | .begin = mem_begin, |
00752703 | 1799 | .commit = mem_commit, |
ac1970fb AK |
1800 | .region_add = mem_add, |
1801 | .region_nop = mem_add, | |
1802 | .priority = 0, | |
1803 | }; | |
89ae337a | 1804 | memory_listener_register(&as->dispatch_listener, as); |
ac1970fb AK |
1805 | } |
1806 | ||
83f3c251 AK |
1807 | void address_space_destroy_dispatch(AddressSpace *as) |
1808 | { | |
1809 | AddressSpaceDispatch *d = as->dispatch; | |
1810 | ||
89ae337a | 1811 | memory_listener_unregister(&as->dispatch_listener); |
83f3c251 AK |
1812 | g_free(d); |
1813 | as->dispatch = NULL; | |
1814 | } | |
1815 | ||
62152b8a AK |
1816 | static void memory_map_init(void) |
1817 | { | |
7267c094 | 1818 | system_memory = g_malloc(sizeof(*system_memory)); |
2c9b15ca | 1819 | memory_region_init(system_memory, NULL, "system", INT64_MAX); |
7dca8043 | 1820 | address_space_init(&address_space_memory, system_memory, "memory"); |
309cb471 | 1821 | |
7267c094 | 1822 | system_io = g_malloc(sizeof(*system_io)); |
2c9b15ca | 1823 | memory_region_init(system_io, NULL, "io", 65536); |
7dca8043 | 1824 | address_space_init(&address_space_io, system_io, "I/O"); |
93632747 | 1825 | |
f6790af6 | 1826 | memory_listener_register(&core_memory_listener, &address_space_memory); |
f6790af6 | 1827 | memory_listener_register(&tcg_memory_listener, &address_space_memory); |
62152b8a AK |
1828 | } |
1829 | ||
1830 | MemoryRegion *get_system_memory(void) | |
1831 | { | |
1832 | return system_memory; | |
1833 | } | |
1834 | ||
309cb471 AK |
1835 | MemoryRegion *get_system_io(void) |
1836 | { | |
1837 | return system_io; | |
1838 | } | |
1839 | ||
e2eef170 PB |
1840 | #endif /* !defined(CONFIG_USER_ONLY) */ |
1841 | ||
13eb76e0 FB |
1842 | /* physical memory access (slow version, mainly for debug) */ |
1843 | #if defined(CONFIG_USER_ONLY) | |
f17ec444 | 1844 | int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr, |
a68fe89c | 1845 | uint8_t *buf, int len, int is_write) |
13eb76e0 FB |
1846 | { |
1847 | int l, flags; | |
1848 | target_ulong page; | |
53a5960a | 1849 | void * p; |
13eb76e0 FB |
1850 | |
1851 | while (len > 0) { | |
1852 | page = addr & TARGET_PAGE_MASK; | |
1853 | l = (page + TARGET_PAGE_SIZE) - addr; | |
1854 | if (l > len) | |
1855 | l = len; | |
1856 | flags = page_get_flags(page); | |
1857 | if (!(flags & PAGE_VALID)) | |
a68fe89c | 1858 | return -1; |
13eb76e0 FB |
1859 | if (is_write) { |
1860 | if (!(flags & PAGE_WRITE)) | |
a68fe89c | 1861 | return -1; |
579a97f7 | 1862 | /* XXX: this code should not depend on lock_user */ |
72fb7daa | 1863 | if (!(p = lock_user(VERIFY_WRITE, addr, l, 0))) |
a68fe89c | 1864 | return -1; |
72fb7daa AJ |
1865 | memcpy(p, buf, l); |
1866 | unlock_user(p, addr, l); | |
13eb76e0 FB |
1867 | } else { |
1868 | if (!(flags & PAGE_READ)) | |
a68fe89c | 1869 | return -1; |
579a97f7 | 1870 | /* XXX: this code should not depend on lock_user */ |
72fb7daa | 1871 | if (!(p = lock_user(VERIFY_READ, addr, l, 1))) |
a68fe89c | 1872 | return -1; |
72fb7daa | 1873 | memcpy(buf, p, l); |
5b257578 | 1874 | unlock_user(p, addr, 0); |
13eb76e0 FB |
1875 | } |
1876 | len -= l; | |
1877 | buf += l; | |
1878 | addr += l; | |
1879 | } | |
a68fe89c | 1880 | return 0; |
13eb76e0 | 1881 | } |
8df1cd07 | 1882 | |
13eb76e0 | 1883 | #else |
51d7a9eb | 1884 | |
a8170e5e AK |
1885 | static void invalidate_and_set_dirty(hwaddr addr, |
1886 | hwaddr length) | |
51d7a9eb AP |
1887 | { |
1888 | if (!cpu_physical_memory_is_dirty(addr)) { | |
1889 | /* invalidate code */ | |
1890 | tb_invalidate_phys_page_range(addr, addr + length, 0); | |
1891 | /* set dirty bit */ | |
1892 | cpu_physical_memory_set_dirty_flags(addr, (0xff & ~CODE_DIRTY_FLAG)); | |
1893 | } | |
e226939d | 1894 | xen_modified_memory(addr, length); |
51d7a9eb AP |
1895 | } |
1896 | ||
2bbfa05d PB |
1897 | static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) |
1898 | { | |
1899 | if (memory_region_is_ram(mr)) { | |
1900 | return !(is_write && mr->readonly); | |
1901 | } | |
1902 | if (memory_region_is_romd(mr)) { | |
1903 | return !is_write; | |
1904 | } | |
1905 | ||
1906 | return false; | |
1907 | } | |
1908 | ||
23326164 | 1909 | static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr) |
82f2563f | 1910 | { |
e1622f4b | 1911 | unsigned access_size_max = mr->ops->valid.max_access_size; |
23326164 RH |
1912 | |
1913 | /* Regions are assumed to support 1-4 byte accesses unless | |
1914 | otherwise specified. */ | |
23326164 RH |
1915 | if (access_size_max == 0) { |
1916 | access_size_max = 4; | |
1917 | } | |
1918 | ||
1919 | /* Bound the maximum access by the alignment of the address. */ | |
1920 | if (!mr->ops->impl.unaligned) { | |
1921 | unsigned align_size_max = addr & -addr; | |
1922 | if (align_size_max != 0 && align_size_max < access_size_max) { | |
1923 | access_size_max = align_size_max; | |
1924 | } | |
82f2563f | 1925 | } |
23326164 RH |
1926 | |
1927 | /* Don't attempt accesses larger than the maximum. */ | |
1928 | if (l > access_size_max) { | |
1929 | l = access_size_max; | |
82f2563f | 1930 | } |
23326164 RH |
1931 | |
1932 | return l; | |
82f2563f PB |
1933 | } |
1934 | ||
fd8aaa76 | 1935 | bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf, |
ac1970fb | 1936 | int len, bool is_write) |
13eb76e0 | 1937 | { |
149f54b5 | 1938 | hwaddr l; |
13eb76e0 | 1939 | uint8_t *ptr; |
791af8c8 | 1940 | uint64_t val; |
149f54b5 | 1941 | hwaddr addr1; |
5c8a00ce | 1942 | MemoryRegion *mr; |
fd8aaa76 | 1943 | bool error = false; |
3b46e624 | 1944 | |
13eb76e0 | 1945 | while (len > 0) { |
149f54b5 | 1946 | l = len; |
5c8a00ce | 1947 | mr = address_space_translate(as, addr, &addr1, &l, is_write); |
3b46e624 | 1948 | |
13eb76e0 | 1949 | if (is_write) { |
5c8a00ce PB |
1950 | if (!memory_access_is_direct(mr, is_write)) { |
1951 | l = memory_access_size(mr, l, addr1); | |
4917cf44 | 1952 | /* XXX: could force current_cpu to NULL to avoid |
6a00d601 | 1953 | potential bugs */ |
23326164 RH |
1954 | switch (l) { |
1955 | case 8: | |
1956 | /* 64 bit write access */ | |
1957 | val = ldq_p(buf); | |
1958 | error |= io_mem_write(mr, addr1, val, 8); | |
1959 | break; | |
1960 | case 4: | |
1c213d19 | 1961 | /* 32 bit write access */ |
c27004ec | 1962 | val = ldl_p(buf); |
5c8a00ce | 1963 | error |= io_mem_write(mr, addr1, val, 4); |
23326164 RH |
1964 | break; |
1965 | case 2: | |
1c213d19 | 1966 | /* 16 bit write access */ |
c27004ec | 1967 | val = lduw_p(buf); |
5c8a00ce | 1968 | error |= io_mem_write(mr, addr1, val, 2); |
23326164 RH |
1969 | break; |
1970 | case 1: | |
1c213d19 | 1971 | /* 8 bit write access */ |
c27004ec | 1972 | val = ldub_p(buf); |
5c8a00ce | 1973 | error |= io_mem_write(mr, addr1, val, 1); |
23326164 RH |
1974 | break; |
1975 | default: | |
1976 | abort(); | |
13eb76e0 | 1977 | } |
2bbfa05d | 1978 | } else { |
5c8a00ce | 1979 | addr1 += memory_region_get_ram_addr(mr); |
13eb76e0 | 1980 | /* RAM case */ |
5579c7f3 | 1981 | ptr = qemu_get_ram_ptr(addr1); |
13eb76e0 | 1982 | memcpy(ptr, buf, l); |
51d7a9eb | 1983 | invalidate_and_set_dirty(addr1, l); |
13eb76e0 FB |
1984 | } |
1985 | } else { | |
5c8a00ce | 1986 | if (!memory_access_is_direct(mr, is_write)) { |
13eb76e0 | 1987 | /* I/O case */ |
5c8a00ce | 1988 | l = memory_access_size(mr, l, addr1); |
23326164 RH |
1989 | switch (l) { |
1990 | case 8: | |
1991 | /* 64 bit read access */ | |
1992 | error |= io_mem_read(mr, addr1, &val, 8); | |
1993 | stq_p(buf, val); | |
1994 | break; | |
1995 | case 4: | |
13eb76e0 | 1996 | /* 32 bit read access */ |
5c8a00ce | 1997 | error |= io_mem_read(mr, addr1, &val, 4); |
c27004ec | 1998 | stl_p(buf, val); |
23326164 RH |
1999 | break; |
2000 | case 2: | |
13eb76e0 | 2001 | /* 16 bit read access */ |
5c8a00ce | 2002 | error |= io_mem_read(mr, addr1, &val, 2); |
c27004ec | 2003 | stw_p(buf, val); |
23326164 RH |
2004 | break; |
2005 | case 1: | |
1c213d19 | 2006 | /* 8 bit read access */ |
5c8a00ce | 2007 | error |= io_mem_read(mr, addr1, &val, 1); |
c27004ec | 2008 | stb_p(buf, val); |
23326164 RH |
2009 | break; |
2010 | default: | |
2011 | abort(); | |
13eb76e0 FB |
2012 | } |
2013 | } else { | |
2014 | /* RAM case */ | |
5c8a00ce | 2015 | ptr = qemu_get_ram_ptr(mr->ram_addr + addr1); |
f3705d53 | 2016 | memcpy(buf, ptr, l); |
13eb76e0 FB |
2017 | } |
2018 | } | |
2019 | len -= l; | |
2020 | buf += l; | |
2021 | addr += l; | |
2022 | } | |
fd8aaa76 PB |
2023 | |
2024 | return error; | |
13eb76e0 | 2025 | } |
8df1cd07 | 2026 | |
fd8aaa76 | 2027 | bool address_space_write(AddressSpace *as, hwaddr addr, |
ac1970fb AK |
2028 | const uint8_t *buf, int len) |
2029 | { | |
fd8aaa76 | 2030 | return address_space_rw(as, addr, (uint8_t *)buf, len, true); |
ac1970fb AK |
2031 | } |
2032 | ||
fd8aaa76 | 2033 | bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len) |
ac1970fb | 2034 | { |
fd8aaa76 | 2035 | return address_space_rw(as, addr, buf, len, false); |
ac1970fb AK |
2036 | } |
2037 | ||
2038 | ||
a8170e5e | 2039 | void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf, |
ac1970fb AK |
2040 | int len, int is_write) |
2041 | { | |
fd8aaa76 | 2042 | address_space_rw(&address_space_memory, addr, buf, len, is_write); |
ac1970fb AK |
2043 | } |
2044 | ||
d0ecd2aa | 2045 | /* used for ROM loading : can write in RAM and ROM */ |
a8170e5e | 2046 | void cpu_physical_memory_write_rom(hwaddr addr, |
d0ecd2aa FB |
2047 | const uint8_t *buf, int len) |
2048 | { | |
149f54b5 | 2049 | hwaddr l; |
d0ecd2aa | 2050 | uint8_t *ptr; |
149f54b5 | 2051 | hwaddr addr1; |
5c8a00ce | 2052 | MemoryRegion *mr; |
3b46e624 | 2053 | |
d0ecd2aa | 2054 | while (len > 0) { |
149f54b5 | 2055 | l = len; |
5c8a00ce PB |
2056 | mr = address_space_translate(&address_space_memory, |
2057 | addr, &addr1, &l, true); | |
3b46e624 | 2058 | |
5c8a00ce PB |
2059 | if (!(memory_region_is_ram(mr) || |
2060 | memory_region_is_romd(mr))) { | |
d0ecd2aa FB |
2061 | /* do nothing */ |
2062 | } else { | |
5c8a00ce | 2063 | addr1 += memory_region_get_ram_addr(mr); |
d0ecd2aa | 2064 | /* ROM/RAM case */ |
5579c7f3 | 2065 | ptr = qemu_get_ram_ptr(addr1); |
d0ecd2aa | 2066 | memcpy(ptr, buf, l); |
51d7a9eb | 2067 | invalidate_and_set_dirty(addr1, l); |
d0ecd2aa FB |
2068 | } |
2069 | len -= l; | |
2070 | buf += l; | |
2071 | addr += l; | |
2072 | } | |
2073 | } | |
2074 | ||
6d16c2f8 | 2075 | typedef struct { |
d3e71559 | 2076 | MemoryRegion *mr; |
6d16c2f8 | 2077 | void *buffer; |
a8170e5e AK |
2078 | hwaddr addr; |
2079 | hwaddr len; | |
6d16c2f8 AL |
2080 | } BounceBuffer; |
2081 | ||
2082 | static BounceBuffer bounce; | |
2083 | ||
ba223c29 AL |
2084 | typedef struct MapClient { |
2085 | void *opaque; | |
2086 | void (*callback)(void *opaque); | |
72cf2d4f | 2087 | QLIST_ENTRY(MapClient) link; |
ba223c29 AL |
2088 | } MapClient; |
2089 | ||
72cf2d4f BS |
2090 | static QLIST_HEAD(map_client_list, MapClient) map_client_list |
2091 | = QLIST_HEAD_INITIALIZER(map_client_list); | |
ba223c29 AL |
2092 | |
2093 | void *cpu_register_map_client(void *opaque, void (*callback)(void *opaque)) | |
2094 | { | |
7267c094 | 2095 | MapClient *client = g_malloc(sizeof(*client)); |
ba223c29 AL |
2096 | |
2097 | client->opaque = opaque; | |
2098 | client->callback = callback; | |
72cf2d4f | 2099 | QLIST_INSERT_HEAD(&map_client_list, client, link); |
ba223c29 AL |
2100 | return client; |
2101 | } | |
2102 | ||
8b9c99d9 | 2103 | static void cpu_unregister_map_client(void *_client) |
ba223c29 AL |
2104 | { |
2105 | MapClient *client = (MapClient *)_client; | |
2106 | ||
72cf2d4f | 2107 | QLIST_REMOVE(client, link); |
7267c094 | 2108 | g_free(client); |
ba223c29 AL |
2109 | } |
2110 | ||
2111 | static void cpu_notify_map_clients(void) | |
2112 | { | |
2113 | MapClient *client; | |
2114 | ||
72cf2d4f BS |
2115 | while (!QLIST_EMPTY(&map_client_list)) { |
2116 | client = QLIST_FIRST(&map_client_list); | |
ba223c29 | 2117 | client->callback(client->opaque); |
34d5e948 | 2118 | cpu_unregister_map_client(client); |
ba223c29 AL |
2119 | } |
2120 | } | |
2121 | ||
51644ab7 PB |
2122 | bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write) |
2123 | { | |
5c8a00ce | 2124 | MemoryRegion *mr; |
51644ab7 PB |
2125 | hwaddr l, xlat; |
2126 | ||
2127 | while (len > 0) { | |
2128 | l = len; | |
5c8a00ce PB |
2129 | mr = address_space_translate(as, addr, &xlat, &l, is_write); |
2130 | if (!memory_access_is_direct(mr, is_write)) { | |
2131 | l = memory_access_size(mr, l, addr); | |
2132 | if (!memory_region_access_valid(mr, xlat, l, is_write)) { | |
51644ab7 PB |
2133 | return false; |
2134 | } | |
2135 | } | |
2136 | ||
2137 | len -= l; | |
2138 | addr += l; | |
2139 | } | |
2140 | return true; | |
2141 | } | |
2142 | ||
6d16c2f8 AL |
2143 | /* Map a physical memory region into a host virtual address. |
2144 | * May map a subset of the requested range, given by and returned in *plen. | |
2145 | * May return NULL if resources needed to perform the mapping are exhausted. | |
2146 | * Use only for reads OR writes - not for read-modify-write operations. | |
ba223c29 AL |
2147 | * Use cpu_register_map_client() to know when retrying the map operation is |
2148 | * likely to succeed. | |
6d16c2f8 | 2149 | */ |
ac1970fb | 2150 | void *address_space_map(AddressSpace *as, |
a8170e5e AK |
2151 | hwaddr addr, |
2152 | hwaddr *plen, | |
ac1970fb | 2153 | bool is_write) |
6d16c2f8 | 2154 | { |
a8170e5e | 2155 | hwaddr len = *plen; |
e3127ae0 PB |
2156 | hwaddr done = 0; |
2157 | hwaddr l, xlat, base; | |
2158 | MemoryRegion *mr, *this_mr; | |
2159 | ram_addr_t raddr; | |
6d16c2f8 | 2160 | |
e3127ae0 PB |
2161 | if (len == 0) { |
2162 | return NULL; | |
2163 | } | |
38bee5dc | 2164 | |
e3127ae0 PB |
2165 | l = len; |
2166 | mr = address_space_translate(as, addr, &xlat, &l, is_write); | |
2167 | if (!memory_access_is_direct(mr, is_write)) { | |
2168 | if (bounce.buffer) { | |
2169 | return NULL; | |
6d16c2f8 | 2170 | } |
e3127ae0 PB |
2171 | bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE); |
2172 | bounce.addr = addr; | |
2173 | bounce.len = l; | |
d3e71559 PB |
2174 | |
2175 | memory_region_ref(mr); | |
2176 | bounce.mr = mr; | |
e3127ae0 PB |
2177 | if (!is_write) { |
2178 | address_space_read(as, addr, bounce.buffer, l); | |
8ab934f9 | 2179 | } |
6d16c2f8 | 2180 | |
e3127ae0 PB |
2181 | *plen = l; |
2182 | return bounce.buffer; | |
2183 | } | |
2184 | ||
2185 | base = xlat; | |
2186 | raddr = memory_region_get_ram_addr(mr); | |
2187 | ||
2188 | for (;;) { | |
6d16c2f8 AL |
2189 | len -= l; |
2190 | addr += l; | |
e3127ae0 PB |
2191 | done += l; |
2192 | if (len == 0) { | |
2193 | break; | |
2194 | } | |
2195 | ||
2196 | l = len; | |
2197 | this_mr = address_space_translate(as, addr, &xlat, &l, is_write); | |
2198 | if (this_mr != mr || xlat != base + done) { | |
2199 | break; | |
2200 | } | |
6d16c2f8 | 2201 | } |
e3127ae0 | 2202 | |
d3e71559 | 2203 | memory_region_ref(mr); |
e3127ae0 PB |
2204 | *plen = done; |
2205 | return qemu_ram_ptr_length(raddr + base, plen); | |
6d16c2f8 AL |
2206 | } |
2207 | ||
ac1970fb | 2208 | /* Unmaps a memory region previously mapped by address_space_map(). |
6d16c2f8 AL |
2209 | * Will also mark the memory as dirty if is_write == 1. access_len gives |
2210 | * the amount of memory that was actually read or written by the caller. | |
2211 | */ | |
a8170e5e AK |
2212 | void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, |
2213 | int is_write, hwaddr access_len) | |
6d16c2f8 AL |
2214 | { |
2215 | if (buffer != bounce.buffer) { | |
d3e71559 PB |
2216 | MemoryRegion *mr; |
2217 | ram_addr_t addr1; | |
2218 | ||
2219 | mr = qemu_ram_addr_from_host(buffer, &addr1); | |
2220 | assert(mr != NULL); | |
6d16c2f8 | 2221 | if (is_write) { |
6d16c2f8 AL |
2222 | while (access_len) { |
2223 | unsigned l; | |
2224 | l = TARGET_PAGE_SIZE; | |
2225 | if (l > access_len) | |
2226 | l = access_len; | |
51d7a9eb | 2227 | invalidate_and_set_dirty(addr1, l); |
6d16c2f8 AL |
2228 | addr1 += l; |
2229 | access_len -= l; | |
2230 | } | |
2231 | } | |
868bb33f | 2232 | if (xen_enabled()) { |
e41d7c69 | 2233 | xen_invalidate_map_cache_entry(buffer); |
050a0ddf | 2234 | } |
d3e71559 | 2235 | memory_region_unref(mr); |
6d16c2f8 AL |
2236 | return; |
2237 | } | |
2238 | if (is_write) { | |
ac1970fb | 2239 | address_space_write(as, bounce.addr, bounce.buffer, access_len); |
6d16c2f8 | 2240 | } |
f8a83245 | 2241 | qemu_vfree(bounce.buffer); |
6d16c2f8 | 2242 | bounce.buffer = NULL; |
d3e71559 | 2243 | memory_region_unref(bounce.mr); |
ba223c29 | 2244 | cpu_notify_map_clients(); |
6d16c2f8 | 2245 | } |
d0ecd2aa | 2246 | |
a8170e5e AK |
2247 | void *cpu_physical_memory_map(hwaddr addr, |
2248 | hwaddr *plen, | |
ac1970fb AK |
2249 | int is_write) |
2250 | { | |
2251 | return address_space_map(&address_space_memory, addr, plen, is_write); | |
2252 | } | |
2253 | ||
a8170e5e AK |
2254 | void cpu_physical_memory_unmap(void *buffer, hwaddr len, |
2255 | int is_write, hwaddr access_len) | |
ac1970fb AK |
2256 | { |
2257 | return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len); | |
2258 | } | |
2259 | ||
8df1cd07 | 2260 | /* warning: addr must be aligned */ |
a8170e5e | 2261 | static inline uint32_t ldl_phys_internal(hwaddr addr, |
1e78bcc1 | 2262 | enum device_endian endian) |
8df1cd07 | 2263 | { |
8df1cd07 | 2264 | uint8_t *ptr; |
791af8c8 | 2265 | uint64_t val; |
5c8a00ce | 2266 | MemoryRegion *mr; |
149f54b5 PB |
2267 | hwaddr l = 4; |
2268 | hwaddr addr1; | |
8df1cd07 | 2269 | |
5c8a00ce PB |
2270 | mr = address_space_translate(&address_space_memory, addr, &addr1, &l, |
2271 | false); | |
2272 | if (l < 4 || !memory_access_is_direct(mr, false)) { | |
8df1cd07 | 2273 | /* I/O case */ |
5c8a00ce | 2274 | io_mem_read(mr, addr1, &val, 4); |
1e78bcc1 AG |
2275 | #if defined(TARGET_WORDS_BIGENDIAN) |
2276 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
2277 | val = bswap32(val); | |
2278 | } | |
2279 | #else | |
2280 | if (endian == DEVICE_BIG_ENDIAN) { | |
2281 | val = bswap32(val); | |
2282 | } | |
2283 | #endif | |
8df1cd07 FB |
2284 | } else { |
2285 | /* RAM case */ | |
5c8a00ce | 2286 | ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr) |
06ef3525 | 2287 | & TARGET_PAGE_MASK) |
149f54b5 | 2288 | + addr1); |
1e78bcc1 AG |
2289 | switch (endian) { |
2290 | case DEVICE_LITTLE_ENDIAN: | |
2291 | val = ldl_le_p(ptr); | |
2292 | break; | |
2293 | case DEVICE_BIG_ENDIAN: | |
2294 | val = ldl_be_p(ptr); | |
2295 | break; | |
2296 | default: | |
2297 | val = ldl_p(ptr); | |
2298 | break; | |
2299 | } | |
8df1cd07 FB |
2300 | } |
2301 | return val; | |
2302 | } | |
2303 | ||
a8170e5e | 2304 | uint32_t ldl_phys(hwaddr addr) |
1e78bcc1 AG |
2305 | { |
2306 | return ldl_phys_internal(addr, DEVICE_NATIVE_ENDIAN); | |
2307 | } | |
2308 | ||
a8170e5e | 2309 | uint32_t ldl_le_phys(hwaddr addr) |
1e78bcc1 AG |
2310 | { |
2311 | return ldl_phys_internal(addr, DEVICE_LITTLE_ENDIAN); | |
2312 | } | |
2313 | ||
a8170e5e | 2314 | uint32_t ldl_be_phys(hwaddr addr) |
1e78bcc1 AG |
2315 | { |
2316 | return ldl_phys_internal(addr, DEVICE_BIG_ENDIAN); | |
2317 | } | |
2318 | ||
84b7b8e7 | 2319 | /* warning: addr must be aligned */ |
a8170e5e | 2320 | static inline uint64_t ldq_phys_internal(hwaddr addr, |
1e78bcc1 | 2321 | enum device_endian endian) |
84b7b8e7 | 2322 | { |
84b7b8e7 FB |
2323 | uint8_t *ptr; |
2324 | uint64_t val; | |
5c8a00ce | 2325 | MemoryRegion *mr; |
149f54b5 PB |
2326 | hwaddr l = 8; |
2327 | hwaddr addr1; | |
84b7b8e7 | 2328 | |
5c8a00ce PB |
2329 | mr = address_space_translate(&address_space_memory, addr, &addr1, &l, |
2330 | false); | |
2331 | if (l < 8 || !memory_access_is_direct(mr, false)) { | |
84b7b8e7 | 2332 | /* I/O case */ |
5c8a00ce | 2333 | io_mem_read(mr, addr1, &val, 8); |
968a5627 PB |
2334 | #if defined(TARGET_WORDS_BIGENDIAN) |
2335 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
2336 | val = bswap64(val); | |
2337 | } | |
2338 | #else | |
2339 | if (endian == DEVICE_BIG_ENDIAN) { | |
2340 | val = bswap64(val); | |
2341 | } | |
84b7b8e7 FB |
2342 | #endif |
2343 | } else { | |
2344 | /* RAM case */ | |
5c8a00ce | 2345 | ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr) |
06ef3525 | 2346 | & TARGET_PAGE_MASK) |
149f54b5 | 2347 | + addr1); |
1e78bcc1 AG |
2348 | switch (endian) { |
2349 | case DEVICE_LITTLE_ENDIAN: | |
2350 | val = ldq_le_p(ptr); | |
2351 | break; | |
2352 | case DEVICE_BIG_ENDIAN: | |
2353 | val = ldq_be_p(ptr); | |
2354 | break; | |
2355 | default: | |
2356 | val = ldq_p(ptr); | |
2357 | break; | |
2358 | } | |
84b7b8e7 FB |
2359 | } |
2360 | return val; | |
2361 | } | |
2362 | ||
a8170e5e | 2363 | uint64_t ldq_phys(hwaddr addr) |
1e78bcc1 AG |
2364 | { |
2365 | return ldq_phys_internal(addr, DEVICE_NATIVE_ENDIAN); | |
2366 | } | |
2367 | ||
a8170e5e | 2368 | uint64_t ldq_le_phys(hwaddr addr) |
1e78bcc1 AG |
2369 | { |
2370 | return ldq_phys_internal(addr, DEVICE_LITTLE_ENDIAN); | |
2371 | } | |
2372 | ||
a8170e5e | 2373 | uint64_t ldq_be_phys(hwaddr addr) |
1e78bcc1 AG |
2374 | { |
2375 | return ldq_phys_internal(addr, DEVICE_BIG_ENDIAN); | |
2376 | } | |
2377 | ||
aab33094 | 2378 | /* XXX: optimize */ |
a8170e5e | 2379 | uint32_t ldub_phys(hwaddr addr) |
aab33094 FB |
2380 | { |
2381 | uint8_t val; | |
2382 | cpu_physical_memory_read(addr, &val, 1); | |
2383 | return val; | |
2384 | } | |
2385 | ||
733f0b02 | 2386 | /* warning: addr must be aligned */ |
a8170e5e | 2387 | static inline uint32_t lduw_phys_internal(hwaddr addr, |
1e78bcc1 | 2388 | enum device_endian endian) |
aab33094 | 2389 | { |
733f0b02 MT |
2390 | uint8_t *ptr; |
2391 | uint64_t val; | |
5c8a00ce | 2392 | MemoryRegion *mr; |
149f54b5 PB |
2393 | hwaddr l = 2; |
2394 | hwaddr addr1; | |
733f0b02 | 2395 | |
5c8a00ce PB |
2396 | mr = address_space_translate(&address_space_memory, addr, &addr1, &l, |
2397 | false); | |
2398 | if (l < 2 || !memory_access_is_direct(mr, false)) { | |
733f0b02 | 2399 | /* I/O case */ |
5c8a00ce | 2400 | io_mem_read(mr, addr1, &val, 2); |
1e78bcc1 AG |
2401 | #if defined(TARGET_WORDS_BIGENDIAN) |
2402 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
2403 | val = bswap16(val); | |
2404 | } | |
2405 | #else | |
2406 | if (endian == DEVICE_BIG_ENDIAN) { | |
2407 | val = bswap16(val); | |
2408 | } | |
2409 | #endif | |
733f0b02 MT |
2410 | } else { |
2411 | /* RAM case */ | |
5c8a00ce | 2412 | ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr) |
06ef3525 | 2413 | & TARGET_PAGE_MASK) |
149f54b5 | 2414 | + addr1); |
1e78bcc1 AG |
2415 | switch (endian) { |
2416 | case DEVICE_LITTLE_ENDIAN: | |
2417 | val = lduw_le_p(ptr); | |
2418 | break; | |
2419 | case DEVICE_BIG_ENDIAN: | |
2420 | val = lduw_be_p(ptr); | |
2421 | break; | |
2422 | default: | |
2423 | val = lduw_p(ptr); | |
2424 | break; | |
2425 | } | |
733f0b02 MT |
2426 | } |
2427 | return val; | |
aab33094 FB |
2428 | } |
2429 | ||
a8170e5e | 2430 | uint32_t lduw_phys(hwaddr addr) |
1e78bcc1 AG |
2431 | { |
2432 | return lduw_phys_internal(addr, DEVICE_NATIVE_ENDIAN); | |
2433 | } | |
2434 | ||
a8170e5e | 2435 | uint32_t lduw_le_phys(hwaddr addr) |
1e78bcc1 AG |
2436 | { |
2437 | return lduw_phys_internal(addr, DEVICE_LITTLE_ENDIAN); | |
2438 | } | |
2439 | ||
a8170e5e | 2440 | uint32_t lduw_be_phys(hwaddr addr) |
1e78bcc1 AG |
2441 | { |
2442 | return lduw_phys_internal(addr, DEVICE_BIG_ENDIAN); | |
2443 | } | |
2444 | ||
8df1cd07 FB |
2445 | /* warning: addr must be aligned. The ram page is not masked as dirty |
2446 | and the code inside is not invalidated. It is useful if the dirty | |
2447 | bits are used to track modified PTEs */ | |
a8170e5e | 2448 | void stl_phys_notdirty(hwaddr addr, uint32_t val) |
8df1cd07 | 2449 | { |
8df1cd07 | 2450 | uint8_t *ptr; |
5c8a00ce | 2451 | MemoryRegion *mr; |
149f54b5 PB |
2452 | hwaddr l = 4; |
2453 | hwaddr addr1; | |
8df1cd07 | 2454 | |
5c8a00ce PB |
2455 | mr = address_space_translate(&address_space_memory, addr, &addr1, &l, |
2456 | true); | |
2457 | if (l < 4 || !memory_access_is_direct(mr, true)) { | |
2458 | io_mem_write(mr, addr1, val, 4); | |
8df1cd07 | 2459 | } else { |
5c8a00ce | 2460 | addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK; |
5579c7f3 | 2461 | ptr = qemu_get_ram_ptr(addr1); |
8df1cd07 | 2462 | stl_p(ptr, val); |
74576198 AL |
2463 | |
2464 | if (unlikely(in_migration)) { | |
2465 | if (!cpu_physical_memory_is_dirty(addr1)) { | |
2466 | /* invalidate code */ | |
2467 | tb_invalidate_phys_page_range(addr1, addr1 + 4, 0); | |
2468 | /* set dirty bit */ | |
f7c11b53 YT |
2469 | cpu_physical_memory_set_dirty_flags( |
2470 | addr1, (0xff & ~CODE_DIRTY_FLAG)); | |
74576198 AL |
2471 | } |
2472 | } | |
8df1cd07 FB |
2473 | } |
2474 | } | |
2475 | ||
2476 | /* warning: addr must be aligned */ | |
a8170e5e | 2477 | static inline void stl_phys_internal(hwaddr addr, uint32_t val, |
1e78bcc1 | 2478 | enum device_endian endian) |
8df1cd07 | 2479 | { |
8df1cd07 | 2480 | uint8_t *ptr; |
5c8a00ce | 2481 | MemoryRegion *mr; |
149f54b5 PB |
2482 | hwaddr l = 4; |
2483 | hwaddr addr1; | |
8df1cd07 | 2484 | |
5c8a00ce PB |
2485 | mr = address_space_translate(&address_space_memory, addr, &addr1, &l, |
2486 | true); | |
2487 | if (l < 4 || !memory_access_is_direct(mr, true)) { | |
1e78bcc1 AG |
2488 | #if defined(TARGET_WORDS_BIGENDIAN) |
2489 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
2490 | val = bswap32(val); | |
2491 | } | |
2492 | #else | |
2493 | if (endian == DEVICE_BIG_ENDIAN) { | |
2494 | val = bswap32(val); | |
2495 | } | |
2496 | #endif | |
5c8a00ce | 2497 | io_mem_write(mr, addr1, val, 4); |
8df1cd07 | 2498 | } else { |
8df1cd07 | 2499 | /* RAM case */ |
5c8a00ce | 2500 | addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK; |
5579c7f3 | 2501 | ptr = qemu_get_ram_ptr(addr1); |
1e78bcc1 AG |
2502 | switch (endian) { |
2503 | case DEVICE_LITTLE_ENDIAN: | |
2504 | stl_le_p(ptr, val); | |
2505 | break; | |
2506 | case DEVICE_BIG_ENDIAN: | |
2507 | stl_be_p(ptr, val); | |
2508 | break; | |
2509 | default: | |
2510 | stl_p(ptr, val); | |
2511 | break; | |
2512 | } | |
51d7a9eb | 2513 | invalidate_and_set_dirty(addr1, 4); |
8df1cd07 FB |
2514 | } |
2515 | } | |
2516 | ||
a8170e5e | 2517 | void stl_phys(hwaddr addr, uint32_t val) |
1e78bcc1 AG |
2518 | { |
2519 | stl_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN); | |
2520 | } | |
2521 | ||
a8170e5e | 2522 | void stl_le_phys(hwaddr addr, uint32_t val) |
1e78bcc1 AG |
2523 | { |
2524 | stl_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN); | |
2525 | } | |
2526 | ||
a8170e5e | 2527 | void stl_be_phys(hwaddr addr, uint32_t val) |
1e78bcc1 AG |
2528 | { |
2529 | stl_phys_internal(addr, val, DEVICE_BIG_ENDIAN); | |
2530 | } | |
2531 | ||
aab33094 | 2532 | /* XXX: optimize */ |
a8170e5e | 2533 | void stb_phys(hwaddr addr, uint32_t val) |
aab33094 FB |
2534 | { |
2535 | uint8_t v = val; | |
2536 | cpu_physical_memory_write(addr, &v, 1); | |
2537 | } | |
2538 | ||
733f0b02 | 2539 | /* warning: addr must be aligned */ |
a8170e5e | 2540 | static inline void stw_phys_internal(hwaddr addr, uint32_t val, |
1e78bcc1 | 2541 | enum device_endian endian) |
aab33094 | 2542 | { |
733f0b02 | 2543 | uint8_t *ptr; |
5c8a00ce | 2544 | MemoryRegion *mr; |
149f54b5 PB |
2545 | hwaddr l = 2; |
2546 | hwaddr addr1; | |
733f0b02 | 2547 | |
5c8a00ce PB |
2548 | mr = address_space_translate(&address_space_memory, addr, &addr1, &l, |
2549 | true); | |
2550 | if (l < 2 || !memory_access_is_direct(mr, true)) { | |
1e78bcc1 AG |
2551 | #if defined(TARGET_WORDS_BIGENDIAN) |
2552 | if (endian == DEVICE_LITTLE_ENDIAN) { | |
2553 | val = bswap16(val); | |
2554 | } | |
2555 | #else | |
2556 | if (endian == DEVICE_BIG_ENDIAN) { | |
2557 | val = bswap16(val); | |
2558 | } | |
2559 | #endif | |
5c8a00ce | 2560 | io_mem_write(mr, addr1, val, 2); |
733f0b02 | 2561 | } else { |
733f0b02 | 2562 | /* RAM case */ |
5c8a00ce | 2563 | addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK; |
733f0b02 | 2564 | ptr = qemu_get_ram_ptr(addr1); |
1e78bcc1 AG |
2565 | switch (endian) { |
2566 | case DEVICE_LITTLE_ENDIAN: | |
2567 | stw_le_p(ptr, val); | |
2568 | break; | |
2569 | case DEVICE_BIG_ENDIAN: | |
2570 | stw_be_p(ptr, val); | |
2571 | break; | |
2572 | default: | |
2573 | stw_p(ptr, val); | |
2574 | break; | |
2575 | } | |
51d7a9eb | 2576 | invalidate_and_set_dirty(addr1, 2); |
733f0b02 | 2577 | } |
aab33094 FB |
2578 | } |
2579 | ||
a8170e5e | 2580 | void stw_phys(hwaddr addr, uint32_t val) |
1e78bcc1 AG |
2581 | { |
2582 | stw_phys_internal(addr, val, DEVICE_NATIVE_ENDIAN); | |
2583 | } | |
2584 | ||
a8170e5e | 2585 | void stw_le_phys(hwaddr addr, uint32_t val) |
1e78bcc1 AG |
2586 | { |
2587 | stw_phys_internal(addr, val, DEVICE_LITTLE_ENDIAN); | |
2588 | } | |
2589 | ||
a8170e5e | 2590 | void stw_be_phys(hwaddr addr, uint32_t val) |
1e78bcc1 AG |
2591 | { |
2592 | stw_phys_internal(addr, val, DEVICE_BIG_ENDIAN); | |
2593 | } | |
2594 | ||
aab33094 | 2595 | /* XXX: optimize */ |
a8170e5e | 2596 | void stq_phys(hwaddr addr, uint64_t val) |
aab33094 FB |
2597 | { |
2598 | val = tswap64(val); | |
71d2b725 | 2599 | cpu_physical_memory_write(addr, &val, 8); |
aab33094 FB |
2600 | } |
2601 | ||
a8170e5e | 2602 | void stq_le_phys(hwaddr addr, uint64_t val) |
1e78bcc1 AG |
2603 | { |
2604 | val = cpu_to_le64(val); | |
2605 | cpu_physical_memory_write(addr, &val, 8); | |
2606 | } | |
2607 | ||
a8170e5e | 2608 | void stq_be_phys(hwaddr addr, uint64_t val) |
1e78bcc1 AG |
2609 | { |
2610 | val = cpu_to_be64(val); | |
2611 | cpu_physical_memory_write(addr, &val, 8); | |
2612 | } | |
2613 | ||
5e2972fd | 2614 | /* virtual memory access for debug (includes writing to ROM) */ |
f17ec444 | 2615 | int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr, |
b448f2f3 | 2616 | uint8_t *buf, int len, int is_write) |
13eb76e0 FB |
2617 | { |
2618 | int l; | |
a8170e5e | 2619 | hwaddr phys_addr; |
9b3c35e0 | 2620 | target_ulong page; |
13eb76e0 FB |
2621 | |
2622 | while (len > 0) { | |
2623 | page = addr & TARGET_PAGE_MASK; | |
f17ec444 | 2624 | phys_addr = cpu_get_phys_page_debug(cpu, page); |
13eb76e0 FB |
2625 | /* if no physical page mapped, return an error */ |
2626 | if (phys_addr == -1) | |
2627 | return -1; | |
2628 | l = (page + TARGET_PAGE_SIZE) - addr; | |
2629 | if (l > len) | |
2630 | l = len; | |
5e2972fd | 2631 | phys_addr += (addr & ~TARGET_PAGE_MASK); |
5e2972fd AL |
2632 | if (is_write) |
2633 | cpu_physical_memory_write_rom(phys_addr, buf, l); | |
2634 | else | |
5e2972fd | 2635 | cpu_physical_memory_rw(phys_addr, buf, l, is_write); |
13eb76e0 FB |
2636 | len -= l; |
2637 | buf += l; | |
2638 | addr += l; | |
2639 | } | |
2640 | return 0; | |
2641 | } | |
a68fe89c | 2642 | #endif |
13eb76e0 | 2643 | |
8e4a424b BS |
2644 | #if !defined(CONFIG_USER_ONLY) |
2645 | ||
2646 | /* | |
2647 | * A helper function for the _utterly broken_ virtio device model to find out if | |
2648 | * it's running on a big endian machine. Don't do this at home kids! | |
2649 | */ | |
2650 | bool virtio_is_big_endian(void); | |
2651 | bool virtio_is_big_endian(void) | |
2652 | { | |
2653 | #if defined(TARGET_WORDS_BIGENDIAN) | |
2654 | return true; | |
2655 | #else | |
2656 | return false; | |
2657 | #endif | |
2658 | } | |
2659 | ||
2660 | #endif | |
2661 | ||
76f35538 | 2662 | #ifndef CONFIG_USER_ONLY |
a8170e5e | 2663 | bool cpu_physical_memory_is_io(hwaddr phys_addr) |
76f35538 | 2664 | { |
5c8a00ce | 2665 | MemoryRegion*mr; |
149f54b5 | 2666 | hwaddr l = 1; |
76f35538 | 2667 | |
5c8a00ce PB |
2668 | mr = address_space_translate(&address_space_memory, |
2669 | phys_addr, &phys_addr, &l, false); | |
76f35538 | 2670 | |
5c8a00ce PB |
2671 | return !(memory_region_is_ram(mr) || |
2672 | memory_region_is_romd(mr)); | |
76f35538 | 2673 | } |
bd2fa51f MH |
2674 | |
2675 | void qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque) | |
2676 | { | |
2677 | RAMBlock *block; | |
2678 | ||
2679 | QTAILQ_FOREACH(block, &ram_list.blocks, next) { | |
2680 | func(block->host, block->offset, block->length, opaque); | |
2681 | } | |
2682 | } | |
ec3f8c99 | 2683 | #endif |