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