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