]> Git Repo - qemu.git/blob - exec.c
qapi: Avoid assertion failure on union 'type' collision
[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 #if !defined(CONFIG_USER_ONLY)
30 #include "hw/boards.h"
31 #endif
32 #include "hw/qdev.h"
33 #include "qemu/osdep.h"
34 #include "sysemu/kvm.h"
35 #include "sysemu/sysemu.h"
36 #include "hw/xen/xen.h"
37 #include "qemu/timer.h"
38 #include "qemu/config-file.h"
39 #include "qemu/error-report.h"
40 #include "exec/memory.h"
41 #include "sysemu/dma.h"
42 #include "exec/address-spaces.h"
43 #if defined(CONFIG_USER_ONLY)
44 #include <qemu.h>
45 #else /* !CONFIG_USER_ONLY */
46 #include "sysemu/xen-mapcache.h"
47 #include "trace.h"
48 #endif
49 #include "exec/cpu-all.h"
50 #include "qemu/rcu_queue.h"
51 #include "qemu/main-loop.h"
52 #include "translate-all.h"
53
54 #include "exec/memory-internal.h"
55 #include "exec/ram_addr.h"
56
57 #include "qemu/range.h"
58
59 //#define DEBUG_SUBPAGE
60
61 #if !defined(CONFIG_USER_ONLY)
62 /* ram_list is read under rcu_read_lock()/rcu_read_unlock().  Writes
63  * are protected by the ramlist lock.
64  */
65 RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list.blocks) };
66
67 static MemoryRegion *system_memory;
68 static MemoryRegion *system_io;
69
70 AddressSpace address_space_io;
71 AddressSpace address_space_memory;
72
73 MemoryRegion io_mem_rom, io_mem_notdirty;
74 static MemoryRegion io_mem_unassigned;
75
76 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
77 #define RAM_PREALLOC   (1 << 0)
78
79 /* RAM is mmap-ed with MAP_SHARED */
80 #define RAM_SHARED     (1 << 1)
81
82 /* Only a portion of RAM (used_length) is actually used, and migrated.
83  * This used_length size can change across reboots.
84  */
85 #define RAM_RESIZEABLE (1 << 2)
86
87 /* An extra page is mapped on top of this RAM.
88  */
89 #define RAM_EXTRA (1 << 3)
90 #endif
91
92 struct CPUTailQ cpus = QTAILQ_HEAD_INITIALIZER(cpus);
93 /* current CPU in the current thread. It is only valid inside
94    cpu_exec() */
95 __thread CPUState *current_cpu;
96 /* 0 = Do not count executed instructions.
97    1 = Precise instruction counting.
98    2 = Adaptive rate instruction counting.  */
99 int use_icount;
100
101 #if !defined(CONFIG_USER_ONLY)
102
103 typedef struct PhysPageEntry PhysPageEntry;
104
105 struct PhysPageEntry {
106     /* How many bits skip to next level (in units of L2_SIZE). 0 for a leaf. */
107     uint32_t skip : 6;
108      /* index into phys_sections (!skip) or phys_map_nodes (skip) */
109     uint32_t ptr : 26;
110 };
111
112 #define PHYS_MAP_NODE_NIL (((uint32_t)~0) >> 6)
113
114 /* Size of the L2 (and L3, etc) page tables.  */
115 #define ADDR_SPACE_BITS 64
116
117 #define P_L2_BITS 9
118 #define P_L2_SIZE (1 << P_L2_BITS)
119
120 #define P_L2_LEVELS (((ADDR_SPACE_BITS - TARGET_PAGE_BITS - 1) / P_L2_BITS) + 1)
121
122 typedef PhysPageEntry Node[P_L2_SIZE];
123
124 typedef struct PhysPageMap {
125     struct rcu_head rcu;
126
127     unsigned sections_nb;
128     unsigned sections_nb_alloc;
129     unsigned nodes_nb;
130     unsigned nodes_nb_alloc;
131     Node *nodes;
132     MemoryRegionSection *sections;
133 } PhysPageMap;
134
135 struct AddressSpaceDispatch {
136     struct rcu_head rcu;
137
138     /* This is a multi-level map on the physical address space.
139      * The bottom level has pointers to MemoryRegionSections.
140      */
141     PhysPageEntry phys_map;
142     PhysPageMap map;
143     AddressSpace *as;
144 };
145
146 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
147 typedef struct subpage_t {
148     MemoryRegion iomem;
149     AddressSpace *as;
150     hwaddr base;
151     uint16_t sub_section[TARGET_PAGE_SIZE];
152 } subpage_t;
153
154 #define PHYS_SECTION_UNASSIGNED 0
155 #define PHYS_SECTION_NOTDIRTY 1
156 #define PHYS_SECTION_ROM 2
157 #define PHYS_SECTION_WATCH 3
158
159 static void io_mem_init(void);
160 static void memory_map_init(void);
161 static void tcg_commit(MemoryListener *listener);
162
163 static MemoryRegion io_mem_watch;
164 #endif
165
166 #if !defined(CONFIG_USER_ONLY)
167
168 static void phys_map_node_reserve(PhysPageMap *map, unsigned nodes)
169 {
170     if (map->nodes_nb + nodes > map->nodes_nb_alloc) {
171         map->nodes_nb_alloc = MAX(map->nodes_nb_alloc * 2, 16);
172         map->nodes_nb_alloc = MAX(map->nodes_nb_alloc, map->nodes_nb + nodes);
173         map->nodes = g_renew(Node, map->nodes, map->nodes_nb_alloc);
174     }
175 }
176
177 static uint32_t phys_map_node_alloc(PhysPageMap *map, bool leaf)
178 {
179     unsigned i;
180     uint32_t ret;
181     PhysPageEntry e;
182     PhysPageEntry *p;
183
184     ret = map->nodes_nb++;
185     p = map->nodes[ret];
186     assert(ret != PHYS_MAP_NODE_NIL);
187     assert(ret != map->nodes_nb_alloc);
188
189     e.skip = leaf ? 0 : 1;
190     e.ptr = leaf ? PHYS_SECTION_UNASSIGNED : PHYS_MAP_NODE_NIL;
191     for (i = 0; i < P_L2_SIZE; ++i) {
192         memcpy(&p[i], &e, sizeof(e));
193     }
194     return ret;
195 }
196
197 static void phys_page_set_level(PhysPageMap *map, PhysPageEntry *lp,
198                                 hwaddr *index, hwaddr *nb, uint16_t leaf,
199                                 int level)
200 {
201     PhysPageEntry *p;
202     hwaddr step = (hwaddr)1 << (level * P_L2_BITS);
203
204     if (lp->skip && lp->ptr == PHYS_MAP_NODE_NIL) {
205         lp->ptr = phys_map_node_alloc(map, level == 0);
206     }
207     p = map->nodes[lp->ptr];
208     lp = &p[(*index >> (level * P_L2_BITS)) & (P_L2_SIZE - 1)];
209
210     while (*nb && lp < &p[P_L2_SIZE]) {
211         if ((*index & (step - 1)) == 0 && *nb >= step) {
212             lp->skip = 0;
213             lp->ptr = leaf;
214             *index += step;
215             *nb -= step;
216         } else {
217             phys_page_set_level(map, lp, index, nb, leaf, level - 1);
218         }
219         ++lp;
220     }
221 }
222
223 static void phys_page_set(AddressSpaceDispatch *d,
224                           hwaddr index, hwaddr nb,
225                           uint16_t leaf)
226 {
227     /* Wildly overreserve - it doesn't matter much. */
228     phys_map_node_reserve(&d->map, 3 * P_L2_LEVELS);
229
230     phys_page_set_level(&d->map, &d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
231 }
232
233 /* Compact a non leaf page entry. Simply detect that the entry has a single child,
234  * and update our entry so we can skip it and go directly to the destination.
235  */
236 static void phys_page_compact(PhysPageEntry *lp, Node *nodes, unsigned long *compacted)
237 {
238     unsigned valid_ptr = P_L2_SIZE;
239     int valid = 0;
240     PhysPageEntry *p;
241     int i;
242
243     if (lp->ptr == PHYS_MAP_NODE_NIL) {
244         return;
245     }
246
247     p = nodes[lp->ptr];
248     for (i = 0; i < P_L2_SIZE; i++) {
249         if (p[i].ptr == PHYS_MAP_NODE_NIL) {
250             continue;
251         }
252
253         valid_ptr = i;
254         valid++;
255         if (p[i].skip) {
256             phys_page_compact(&p[i], nodes, compacted);
257         }
258     }
259
260     /* We can only compress if there's only one child. */
261     if (valid != 1) {
262         return;
263     }
264
265     assert(valid_ptr < P_L2_SIZE);
266
267     /* Don't compress if it won't fit in the # of bits we have. */
268     if (lp->skip + p[valid_ptr].skip >= (1 << 3)) {
269         return;
270     }
271
272     lp->ptr = p[valid_ptr].ptr;
273     if (!p[valid_ptr].skip) {
274         /* If our only child is a leaf, make this a leaf. */
275         /* By design, we should have made this node a leaf to begin with so we
276          * should never reach here.
277          * But since it's so simple to handle this, let's do it just in case we
278          * change this rule.
279          */
280         lp->skip = 0;
281     } else {
282         lp->skip += p[valid_ptr].skip;
283     }
284 }
285
286 static void phys_page_compact_all(AddressSpaceDispatch *d, int nodes_nb)
287 {
288     DECLARE_BITMAP(compacted, nodes_nb);
289
290     if (d->phys_map.skip) {
291         phys_page_compact(&d->phys_map, d->map.nodes, compacted);
292     }
293 }
294
295 static MemoryRegionSection *phys_page_find(PhysPageEntry lp, hwaddr addr,
296                                            Node *nodes, MemoryRegionSection *sections)
297 {
298     PhysPageEntry *p;
299     hwaddr index = addr >> TARGET_PAGE_BITS;
300     int i;
301
302     for (i = P_L2_LEVELS; lp.skip && (i -= lp.skip) >= 0;) {
303         if (lp.ptr == PHYS_MAP_NODE_NIL) {
304             return &sections[PHYS_SECTION_UNASSIGNED];
305         }
306         p = nodes[lp.ptr];
307         lp = p[(index >> (i * P_L2_BITS)) & (P_L2_SIZE - 1)];
308     }
309
310     if (sections[lp.ptr].size.hi ||
311         range_covers_byte(sections[lp.ptr].offset_within_address_space,
312                           sections[lp.ptr].size.lo, addr)) {
313         return &sections[lp.ptr];
314     } else {
315         return &sections[PHYS_SECTION_UNASSIGNED];
316     }
317 }
318
319 bool memory_region_is_unassigned(MemoryRegion *mr)
320 {
321     return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device
322         && mr != &io_mem_watch;
323 }
324
325 /* Called from RCU critical section */
326 static MemoryRegionSection *address_space_lookup_region(AddressSpaceDispatch *d,
327                                                         hwaddr addr,
328                                                         bool resolve_subpage)
329 {
330     MemoryRegionSection *section;
331     subpage_t *subpage;
332
333     section = phys_page_find(d->phys_map, addr, d->map.nodes, d->map.sections);
334     if (resolve_subpage && section->mr->subpage) {
335         subpage = container_of(section->mr, subpage_t, iomem);
336         section = &d->map.sections[subpage->sub_section[SUBPAGE_IDX(addr)]];
337     }
338     return section;
339 }
340
341 /* Called from RCU critical section */
342 static MemoryRegionSection *
343 address_space_translate_internal(AddressSpaceDispatch *d, hwaddr addr, hwaddr *xlat,
344                                  hwaddr *plen, bool resolve_subpage)
345 {
346     MemoryRegionSection *section;
347     MemoryRegion *mr;
348     Int128 diff;
349
350     section = address_space_lookup_region(d, addr, resolve_subpage);
351     /* Compute offset within MemoryRegionSection */
352     addr -= section->offset_within_address_space;
353
354     /* Compute offset within MemoryRegion */
355     *xlat = addr + section->offset_within_region;
356
357     mr = section->mr;
358
359     /* MMIO registers can be expected to perform full-width accesses based only
360      * on their address, without considering adjacent registers that could
361      * decode to completely different MemoryRegions.  When such registers
362      * exist (e.g. I/O ports 0xcf8 and 0xcf9 on most PC chipsets), MMIO
363      * regions overlap wildly.  For this reason we cannot clamp the accesses
364      * here.
365      *
366      * If the length is small (as is the case for address_space_ldl/stl),
367      * everything works fine.  If the incoming length is large, however,
368      * the caller really has to do the clamping through memory_access_size.
369      */
370     if (memory_region_is_ram(mr)) {
371         diff = int128_sub(section->size, int128_make64(addr));
372         *plen = int128_get64(int128_min(diff, int128_make64(*plen)));
373     }
374     return section;
375 }
376
377 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
378 {
379     if (memory_region_is_ram(mr)) {
380         return !(is_write && mr->readonly);
381     }
382     if (memory_region_is_romd(mr)) {
383         return !is_write;
384     }
385
386     return false;
387 }
388
389 /* Called from RCU critical section */
390 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
391                                       hwaddr *xlat, hwaddr *plen,
392                                       bool is_write)
393 {
394     IOMMUTLBEntry iotlb;
395     MemoryRegionSection *section;
396     MemoryRegion *mr;
397
398     for (;;) {
399         AddressSpaceDispatch *d = atomic_rcu_read(&as->dispatch);
400         section = address_space_translate_internal(d, addr, &addr, plen, true);
401         mr = section->mr;
402
403         if (!mr->iommu_ops) {
404             break;
405         }
406
407         iotlb = mr->iommu_ops->translate(mr, addr, is_write);
408         addr = ((iotlb.translated_addr & ~iotlb.addr_mask)
409                 | (addr & iotlb.addr_mask));
410         *plen = MIN(*plen, (addr | iotlb.addr_mask) - addr + 1);
411         if (!(iotlb.perm & (1 << is_write))) {
412             mr = &io_mem_unassigned;
413             break;
414         }
415
416         as = iotlb.target_as;
417     }
418
419     if (xen_enabled() && memory_access_is_direct(mr, is_write)) {
420         hwaddr page = ((addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE) - addr;
421         *plen = MIN(page, *plen);
422     }
423
424     *xlat = addr;
425     return mr;
426 }
427
428 /* Called from RCU critical section */
429 MemoryRegionSection *
430 address_space_translate_for_iotlb(CPUState *cpu, hwaddr addr,
431                                   hwaddr *xlat, hwaddr *plen)
432 {
433     MemoryRegionSection *section;
434     section = address_space_translate_internal(cpu->memory_dispatch,
435                                                addr, xlat, plen, false);
436
437     assert(!section->mr->iommu_ops);
438     return section;
439 }
440 #endif
441
442 #if !defined(CONFIG_USER_ONLY)
443
444 static int cpu_common_post_load(void *opaque, int version_id)
445 {
446     CPUState *cpu = opaque;
447
448     /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
449        version_id is increased. */
450     cpu->interrupt_request &= ~0x01;
451     tlb_flush(cpu, 1);
452
453     return 0;
454 }
455
456 static int cpu_common_pre_load(void *opaque)
457 {
458     CPUState *cpu = opaque;
459
460     cpu->exception_index = -1;
461
462     return 0;
463 }
464
465 static bool cpu_common_exception_index_needed(void *opaque)
466 {
467     CPUState *cpu = opaque;
468
469     return tcg_enabled() && cpu->exception_index != -1;
470 }
471
472 static const VMStateDescription vmstate_cpu_common_exception_index = {
473     .name = "cpu_common/exception_index",
474     .version_id = 1,
475     .minimum_version_id = 1,
476     .needed = cpu_common_exception_index_needed,
477     .fields = (VMStateField[]) {
478         VMSTATE_INT32(exception_index, CPUState),
479         VMSTATE_END_OF_LIST()
480     }
481 };
482
483 static bool cpu_common_crash_occurred_needed(void *opaque)
484 {
485     CPUState *cpu = opaque;
486
487     return cpu->crash_occurred;
488 }
489
490 static const VMStateDescription vmstate_cpu_common_crash_occurred = {
491     .name = "cpu_common/crash_occurred",
492     .version_id = 1,
493     .minimum_version_id = 1,
494     .needed = cpu_common_crash_occurred_needed,
495     .fields = (VMStateField[]) {
496         VMSTATE_BOOL(crash_occurred, CPUState),
497         VMSTATE_END_OF_LIST()
498     }
499 };
500
501 const VMStateDescription vmstate_cpu_common = {
502     .name = "cpu_common",
503     .version_id = 1,
504     .minimum_version_id = 1,
505     .pre_load = cpu_common_pre_load,
506     .post_load = cpu_common_post_load,
507     .fields = (VMStateField[]) {
508         VMSTATE_UINT32(halted, CPUState),
509         VMSTATE_UINT32(interrupt_request, CPUState),
510         VMSTATE_END_OF_LIST()
511     },
512     .subsections = (const VMStateDescription*[]) {
513         &vmstate_cpu_common_exception_index,
514         &vmstate_cpu_common_crash_occurred,
515         NULL
516     }
517 };
518
519 #endif
520
521 CPUState *qemu_get_cpu(int index)
522 {
523     CPUState *cpu;
524
525     CPU_FOREACH(cpu) {
526         if (cpu->cpu_index == index) {
527             return cpu;
528         }
529     }
530
531     return NULL;
532 }
533
534 #if !defined(CONFIG_USER_ONLY)
535 void tcg_cpu_address_space_init(CPUState *cpu, AddressSpace *as)
536 {
537     /* We only support one address space per cpu at the moment.  */
538     assert(cpu->as == as);
539
540     if (cpu->tcg_as_listener) {
541         memory_listener_unregister(cpu->tcg_as_listener);
542     } else {
543         cpu->tcg_as_listener = g_new0(MemoryListener, 1);
544     }
545     cpu->tcg_as_listener->commit = tcg_commit;
546     memory_listener_register(cpu->tcg_as_listener, as);
547 }
548 #endif
549
550 #ifndef CONFIG_USER_ONLY
551 static DECLARE_BITMAP(cpu_index_map, MAX_CPUMASK_BITS);
552
553 static int cpu_get_free_index(Error **errp)
554 {
555     int cpu = find_first_zero_bit(cpu_index_map, MAX_CPUMASK_BITS);
556
557     if (cpu >= MAX_CPUMASK_BITS) {
558         error_setg(errp, "Trying to use more CPUs than max of %d",
559                    MAX_CPUMASK_BITS);
560         return -1;
561     }
562
563     bitmap_set(cpu_index_map, cpu, 1);
564     return cpu;
565 }
566
567 void cpu_exec_exit(CPUState *cpu)
568 {
569     if (cpu->cpu_index == -1) {
570         /* cpu_index was never allocated by this @cpu or was already freed. */
571         return;
572     }
573
574     bitmap_clear(cpu_index_map, cpu->cpu_index, 1);
575     cpu->cpu_index = -1;
576 }
577 #else
578
579 static int cpu_get_free_index(Error **errp)
580 {
581     CPUState *some_cpu;
582     int cpu_index = 0;
583
584     CPU_FOREACH(some_cpu) {
585         cpu_index++;
586     }
587     return cpu_index;
588 }
589
590 void cpu_exec_exit(CPUState *cpu)
591 {
592 }
593 #endif
594
595 void cpu_exec_init(CPUState *cpu, Error **errp)
596 {
597     CPUClass *cc = CPU_GET_CLASS(cpu);
598     int cpu_index;
599     Error *local_err = NULL;
600
601 #ifndef CONFIG_USER_ONLY
602     cpu->as = &address_space_memory;
603     cpu->thread_id = qemu_get_thread_id();
604     cpu_reload_memory_map(cpu);
605 #endif
606
607 #if defined(CONFIG_USER_ONLY)
608     cpu_list_lock();
609 #endif
610     cpu_index = cpu->cpu_index = cpu_get_free_index(&local_err);
611     if (local_err) {
612         error_propagate(errp, local_err);
613 #if defined(CONFIG_USER_ONLY)
614         cpu_list_unlock();
615 #endif
616         return;
617     }
618     QTAILQ_INSERT_TAIL(&cpus, cpu, node);
619 #if defined(CONFIG_USER_ONLY)
620     cpu_list_unlock();
621 #endif
622     if (qdev_get_vmsd(DEVICE(cpu)) == NULL) {
623         vmstate_register(NULL, cpu_index, &vmstate_cpu_common, cpu);
624     }
625 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
626     register_savevm(NULL, "cpu", cpu_index, CPU_SAVE_VERSION,
627                     cpu_save, cpu_load, cpu->env_ptr);
628     assert(cc->vmsd == NULL);
629     assert(qdev_get_vmsd(DEVICE(cpu)) == NULL);
630 #endif
631     if (cc->vmsd != NULL) {
632         vmstate_register(NULL, cpu_index, cc->vmsd, cpu);
633     }
634 }
635
636 #if defined(CONFIG_USER_ONLY)
637 static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
638 {
639     tb_invalidate_phys_page_range(pc, pc + 1, 0);
640 }
641 #else
642 static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
643 {
644     hwaddr phys = cpu_get_phys_page_debug(cpu, pc);
645     if (phys != -1) {
646         tb_invalidate_phys_addr(cpu->as,
647                                 phys | (pc & ~TARGET_PAGE_MASK));
648     }
649 }
650 #endif
651
652 #if defined(CONFIG_USER_ONLY)
653 void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
654
655 {
656 }
657
658 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len,
659                           int flags)
660 {
661     return -ENOSYS;
662 }
663
664 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint)
665 {
666 }
667
668 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
669                           int flags, CPUWatchpoint **watchpoint)
670 {
671     return -ENOSYS;
672 }
673 #else
674 /* Add a watchpoint.  */
675 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
676                           int flags, CPUWatchpoint **watchpoint)
677 {
678     CPUWatchpoint *wp;
679
680     /* forbid ranges which are empty or run off the end of the address space */
681     if (len == 0 || (addr + len - 1) < addr) {
682         error_report("tried to set invalid watchpoint at %"
683                      VADDR_PRIx ", len=%" VADDR_PRIu, addr, len);
684         return -EINVAL;
685     }
686     wp = g_malloc(sizeof(*wp));
687
688     wp->vaddr = addr;
689     wp->len = len;
690     wp->flags = flags;
691
692     /* keep all GDB-injected watchpoints in front */
693     if (flags & BP_GDB) {
694         QTAILQ_INSERT_HEAD(&cpu->watchpoints, wp, entry);
695     } else {
696         QTAILQ_INSERT_TAIL(&cpu->watchpoints, wp, entry);
697     }
698
699     tlb_flush_page(cpu, addr);
700
701     if (watchpoint)
702         *watchpoint = wp;
703     return 0;
704 }
705
706 /* Remove a specific watchpoint.  */
707 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len,
708                           int flags)
709 {
710     CPUWatchpoint *wp;
711
712     QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
713         if (addr == wp->vaddr && len == wp->len
714                 && flags == (wp->flags & ~BP_WATCHPOINT_HIT)) {
715             cpu_watchpoint_remove_by_ref(cpu, wp);
716             return 0;
717         }
718     }
719     return -ENOENT;
720 }
721
722 /* Remove a specific watchpoint by reference.  */
723 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint)
724 {
725     QTAILQ_REMOVE(&cpu->watchpoints, watchpoint, entry);
726
727     tlb_flush_page(cpu, watchpoint->vaddr);
728
729     g_free(watchpoint);
730 }
731
732 /* Remove all matching watchpoints.  */
733 void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
734 {
735     CPUWatchpoint *wp, *next;
736
737     QTAILQ_FOREACH_SAFE(wp, &cpu->watchpoints, entry, next) {
738         if (wp->flags & mask) {
739             cpu_watchpoint_remove_by_ref(cpu, wp);
740         }
741     }
742 }
743
744 /* Return true if this watchpoint address matches the specified
745  * access (ie the address range covered by the watchpoint overlaps
746  * partially or completely with the address range covered by the
747  * access).
748  */
749 static inline bool cpu_watchpoint_address_matches(CPUWatchpoint *wp,
750                                                   vaddr addr,
751                                                   vaddr len)
752 {
753     /* We know the lengths are non-zero, but a little caution is
754      * required to avoid errors in the case where the range ends
755      * exactly at the top of the address space and so addr + len
756      * wraps round to zero.
757      */
758     vaddr wpend = wp->vaddr + wp->len - 1;
759     vaddr addrend = addr + len - 1;
760
761     return !(addr > wpend || wp->vaddr > addrend);
762 }
763
764 #endif
765
766 /* Add a breakpoint.  */
767 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
768                           CPUBreakpoint **breakpoint)
769 {
770     CPUBreakpoint *bp;
771
772     bp = g_malloc(sizeof(*bp));
773
774     bp->pc = pc;
775     bp->flags = flags;
776
777     /* keep all GDB-injected breakpoints in front */
778     if (flags & BP_GDB) {
779         QTAILQ_INSERT_HEAD(&cpu->breakpoints, bp, entry);
780     } else {
781         QTAILQ_INSERT_TAIL(&cpu->breakpoints, bp, entry);
782     }
783
784     breakpoint_invalidate(cpu, pc);
785
786     if (breakpoint) {
787         *breakpoint = bp;
788     }
789     return 0;
790 }
791
792 /* Remove a specific breakpoint.  */
793 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags)
794 {
795     CPUBreakpoint *bp;
796
797     QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
798         if (bp->pc == pc && bp->flags == flags) {
799             cpu_breakpoint_remove_by_ref(cpu, bp);
800             return 0;
801         }
802     }
803     return -ENOENT;
804 }
805
806 /* Remove a specific breakpoint by reference.  */
807 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint)
808 {
809     QTAILQ_REMOVE(&cpu->breakpoints, breakpoint, entry);
810
811     breakpoint_invalidate(cpu, breakpoint->pc);
812
813     g_free(breakpoint);
814 }
815
816 /* Remove all matching breakpoints. */
817 void cpu_breakpoint_remove_all(CPUState *cpu, int mask)
818 {
819     CPUBreakpoint *bp, *next;
820
821     QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) {
822         if (bp->flags & mask) {
823             cpu_breakpoint_remove_by_ref(cpu, bp);
824         }
825     }
826 }
827
828 /* enable or disable single step mode. EXCP_DEBUG is returned by the
829    CPU loop after each instruction */
830 void cpu_single_step(CPUState *cpu, int enabled)
831 {
832     if (cpu->singlestep_enabled != enabled) {
833         cpu->singlestep_enabled = enabled;
834         if (kvm_enabled()) {
835             kvm_update_guest_debug(cpu, 0);
836         } else {
837             /* must flush all the translated code to avoid inconsistencies */
838             /* XXX: only flush what is necessary */
839             tb_flush(cpu);
840         }
841     }
842 }
843
844 void cpu_abort(CPUState *cpu, const char *fmt, ...)
845 {
846     va_list ap;
847     va_list ap2;
848
849     va_start(ap, fmt);
850     va_copy(ap2, ap);
851     fprintf(stderr, "qemu: fatal: ");
852     vfprintf(stderr, fmt, ap);
853     fprintf(stderr, "\n");
854     cpu_dump_state(cpu, stderr, fprintf, CPU_DUMP_FPU | CPU_DUMP_CCOP);
855     if (qemu_log_enabled()) {
856         qemu_log("qemu: fatal: ");
857         qemu_log_vprintf(fmt, ap2);
858         qemu_log("\n");
859         log_cpu_state(cpu, CPU_DUMP_FPU | CPU_DUMP_CCOP);
860         qemu_log_flush();
861         qemu_log_close();
862     }
863     va_end(ap2);
864     va_end(ap);
865 #if defined(CONFIG_USER_ONLY)
866     {
867         struct sigaction act;
868         sigfillset(&act.sa_mask);
869         act.sa_handler = SIG_DFL;
870         sigaction(SIGABRT, &act, NULL);
871     }
872 #endif
873     abort();
874 }
875
876 #if !defined(CONFIG_USER_ONLY)
877 /* Called from RCU critical section */
878 static RAMBlock *qemu_get_ram_block(ram_addr_t addr)
879 {
880     RAMBlock *block;
881
882     block = atomic_rcu_read(&ram_list.mru_block);
883     if (block && addr - block->offset < block->max_length) {
884         goto found;
885     }
886     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
887         if (addr - block->offset < block->max_length) {
888             goto found;
889         }
890     }
891
892     fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
893     abort();
894
895 found:
896     /* It is safe to write mru_block outside the iothread lock.  This
897      * is what happens:
898      *
899      *     mru_block = xxx
900      *     rcu_read_unlock()
901      *                                        xxx removed from list
902      *                  rcu_read_lock()
903      *                  read mru_block
904      *                                        mru_block = NULL;
905      *                                        call_rcu(reclaim_ramblock, xxx);
906      *                  rcu_read_unlock()
907      *
908      * atomic_rcu_set is not needed here.  The block was already published
909      * when it was placed into the list.  Here we're just making an extra
910      * copy of the pointer.
911      */
912     ram_list.mru_block = block;
913     return block;
914 }
915
916 static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length)
917 {
918     CPUState *cpu;
919     ram_addr_t start1;
920     RAMBlock *block;
921     ram_addr_t end;
922
923     end = TARGET_PAGE_ALIGN(start + length);
924     start &= TARGET_PAGE_MASK;
925
926     rcu_read_lock();
927     block = qemu_get_ram_block(start);
928     assert(block == qemu_get_ram_block(end - 1));
929     start1 = (uintptr_t)ramblock_ptr(block, start - block->offset);
930     CPU_FOREACH(cpu) {
931         tlb_reset_dirty(cpu, start1, length);
932     }
933     rcu_read_unlock();
934 }
935
936 /* Note: start and end must be within the same ram block.  */
937 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start,
938                                               ram_addr_t length,
939                                               unsigned client)
940 {
941     unsigned long end, page;
942     bool dirty;
943
944     if (length == 0) {
945         return false;
946     }
947
948     end = TARGET_PAGE_ALIGN(start + length) >> TARGET_PAGE_BITS;
949     page = start >> TARGET_PAGE_BITS;
950     dirty = bitmap_test_and_clear_atomic(ram_list.dirty_memory[client],
951                                          page, end - page);
952
953     if (dirty && tcg_enabled()) {
954         tlb_reset_dirty_range_all(start, length);
955     }
956
957     return dirty;
958 }
959
960 /* Called from RCU critical section */
961 hwaddr memory_region_section_get_iotlb(CPUState *cpu,
962                                        MemoryRegionSection *section,
963                                        target_ulong vaddr,
964                                        hwaddr paddr, hwaddr xlat,
965                                        int prot,
966                                        target_ulong *address)
967 {
968     hwaddr iotlb;
969     CPUWatchpoint *wp;
970
971     if (memory_region_is_ram(section->mr)) {
972         /* Normal RAM.  */
973         iotlb = (memory_region_get_ram_addr(section->mr) & TARGET_PAGE_MASK)
974             + xlat;
975         if (!section->readonly) {
976             iotlb |= PHYS_SECTION_NOTDIRTY;
977         } else {
978             iotlb |= PHYS_SECTION_ROM;
979         }
980     } else {
981         AddressSpaceDispatch *d;
982
983         d = atomic_rcu_read(&section->address_space->dispatch);
984         iotlb = section - d->map.sections;
985         iotlb += xlat;
986     }
987
988     /* Make accesses to pages with watchpoints go via the
989        watchpoint trap routines.  */
990     QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
991         if (cpu_watchpoint_address_matches(wp, vaddr, TARGET_PAGE_SIZE)) {
992             /* Avoid trapping reads of pages with a write breakpoint. */
993             if ((prot & PAGE_WRITE) || (wp->flags & BP_MEM_READ)) {
994                 iotlb = PHYS_SECTION_WATCH + paddr;
995                 *address |= TLB_MMIO;
996                 break;
997             }
998         }
999     }
1000
1001     return iotlb;
1002 }
1003 #endif /* defined(CONFIG_USER_ONLY) */
1004
1005 #if !defined(CONFIG_USER_ONLY)
1006
1007 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
1008                              uint16_t section);
1009 static subpage_t *subpage_init(AddressSpace *as, hwaddr base);
1010
1011 static void *(*phys_mem_alloc)(size_t size, uint64_t *align) =
1012                                qemu_anon_ram_alloc;
1013
1014 /*
1015  * Set a custom physical guest memory alloator.
1016  * Accelerators with unusual needs may need this.  Hopefully, we can
1017  * get rid of it eventually.
1018  */
1019 void phys_mem_set_alloc(void *(*alloc)(size_t, uint64_t *align))
1020 {
1021     phys_mem_alloc = alloc;
1022 }
1023
1024 static uint16_t phys_section_add(PhysPageMap *map,
1025                                  MemoryRegionSection *section)
1026 {
1027     /* The physical section number is ORed with a page-aligned
1028      * pointer to produce the iotlb entries.  Thus it should
1029      * never overflow into the page-aligned value.
1030      */
1031     assert(map->sections_nb < TARGET_PAGE_SIZE);
1032
1033     if (map->sections_nb == map->sections_nb_alloc) {
1034         map->sections_nb_alloc = MAX(map->sections_nb_alloc * 2, 16);
1035         map->sections = g_renew(MemoryRegionSection, map->sections,
1036                                 map->sections_nb_alloc);
1037     }
1038     map->sections[map->sections_nb] = *section;
1039     memory_region_ref(section->mr);
1040     return map->sections_nb++;
1041 }
1042
1043 static void phys_section_destroy(MemoryRegion *mr)
1044 {
1045     memory_region_unref(mr);
1046
1047     if (mr->subpage) {
1048         subpage_t *subpage = container_of(mr, subpage_t, iomem);
1049         object_unref(OBJECT(&subpage->iomem));
1050         g_free(subpage);
1051     }
1052 }
1053
1054 static void phys_sections_free(PhysPageMap *map)
1055 {
1056     while (map->sections_nb > 0) {
1057         MemoryRegionSection *section = &map->sections[--map->sections_nb];
1058         phys_section_destroy(section->mr);
1059     }
1060     g_free(map->sections);
1061     g_free(map->nodes);
1062 }
1063
1064 static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)
1065 {
1066     subpage_t *subpage;
1067     hwaddr base = section->offset_within_address_space
1068         & TARGET_PAGE_MASK;
1069     MemoryRegionSection *existing = phys_page_find(d->phys_map, base,
1070                                                    d->map.nodes, d->map.sections);
1071     MemoryRegionSection subsection = {
1072         .offset_within_address_space = base,
1073         .size = int128_make64(TARGET_PAGE_SIZE),
1074     };
1075     hwaddr start, end;
1076
1077     assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
1078
1079     if (!(existing->mr->subpage)) {
1080         subpage = subpage_init(d->as, base);
1081         subsection.address_space = d->as;
1082         subsection.mr = &subpage->iomem;
1083         phys_page_set(d, base >> TARGET_PAGE_BITS, 1,
1084                       phys_section_add(&d->map, &subsection));
1085     } else {
1086         subpage = container_of(existing->mr, subpage_t, iomem);
1087     }
1088     start = section->offset_within_address_space & ~TARGET_PAGE_MASK;
1089     end = start + int128_get64(section->size) - 1;
1090     subpage_register(subpage, start, end,
1091                      phys_section_add(&d->map, section));
1092 }
1093
1094
1095 static void register_multipage(AddressSpaceDispatch *d,
1096                                MemoryRegionSection *section)
1097 {
1098     hwaddr start_addr = section->offset_within_address_space;
1099     uint16_t section_index = phys_section_add(&d->map, section);
1100     uint64_t num_pages = int128_get64(int128_rshift(section->size,
1101                                                     TARGET_PAGE_BITS));
1102
1103     assert(num_pages);
1104     phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);
1105 }
1106
1107 static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
1108 {
1109     AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
1110     AddressSpaceDispatch *d = as->next_dispatch;
1111     MemoryRegionSection now = *section, remain = *section;
1112     Int128 page_size = int128_make64(TARGET_PAGE_SIZE);
1113
1114     if (now.offset_within_address_space & ~TARGET_PAGE_MASK) {
1115         uint64_t left = TARGET_PAGE_ALIGN(now.offset_within_address_space)
1116                        - now.offset_within_address_space;
1117
1118         now.size = int128_min(int128_make64(left), now.size);
1119         register_subpage(d, &now);
1120     } else {
1121         now.size = int128_zero();
1122     }
1123     while (int128_ne(remain.size, now.size)) {
1124         remain.size = int128_sub(remain.size, now.size);
1125         remain.offset_within_address_space += int128_get64(now.size);
1126         remain.offset_within_region += int128_get64(now.size);
1127         now = remain;
1128         if (int128_lt(remain.size, page_size)) {
1129             register_subpage(d, &now);
1130         } else if (remain.offset_within_address_space & ~TARGET_PAGE_MASK) {
1131             now.size = page_size;
1132             register_subpage(d, &now);
1133         } else {
1134             now.size = int128_and(now.size, int128_neg(page_size));
1135             register_multipage(d, &now);
1136         }
1137     }
1138 }
1139
1140 void qemu_flush_coalesced_mmio_buffer(void)
1141 {
1142     if (kvm_enabled())
1143         kvm_flush_coalesced_mmio_buffer();
1144 }
1145
1146 void qemu_mutex_lock_ramlist(void)
1147 {
1148     qemu_mutex_lock(&ram_list.mutex);
1149 }
1150
1151 void qemu_mutex_unlock_ramlist(void)
1152 {
1153     qemu_mutex_unlock(&ram_list.mutex);
1154 }
1155
1156 #ifdef __linux__
1157
1158 #include <sys/vfs.h>
1159
1160 #define HUGETLBFS_MAGIC       0x958458f6
1161
1162 static long gethugepagesize(const char *path, Error **errp)
1163 {
1164     struct statfs fs;
1165     int ret;
1166
1167     do {
1168         ret = statfs(path, &fs);
1169     } while (ret != 0 && errno == EINTR);
1170
1171     if (ret != 0) {
1172         error_setg_errno(errp, errno, "failed to get page size of file %s",
1173                          path);
1174         return 0;
1175     }
1176
1177     if (fs.f_type != HUGETLBFS_MAGIC)
1178         fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
1179
1180     return fs.f_bsize;
1181 }
1182
1183 static void *file_ram_alloc(RAMBlock *block,
1184                             ram_addr_t memory,
1185                             const char *path,
1186                             Error **errp)
1187 {
1188     char *filename;
1189     char *sanitized_name;
1190     char *c;
1191     void *ptr;
1192     void *area = NULL;
1193     int fd;
1194     uint64_t hpagesize;
1195     uint64_t total;
1196     Error *local_err = NULL;
1197     size_t offset;
1198
1199     hpagesize = gethugepagesize(path, &local_err);
1200     if (local_err) {
1201         error_propagate(errp, local_err);
1202         goto error;
1203     }
1204     block->mr->align = hpagesize;
1205
1206     if (memory < hpagesize) {
1207         error_setg(errp, "memory size 0x" RAM_ADDR_FMT " must be equal to "
1208                    "or larger than huge page size 0x%" PRIx64,
1209                    memory, hpagesize);
1210         goto error;
1211     }
1212
1213     if (kvm_enabled() && !kvm_has_sync_mmu()) {
1214         error_setg(errp,
1215                    "host lacks kvm mmu notifiers, -mem-path unsupported");
1216         goto error;
1217     }
1218
1219     /* Make name safe to use with mkstemp by replacing '/' with '_'. */
1220     sanitized_name = g_strdup(memory_region_name(block->mr));
1221     for (c = sanitized_name; *c != '\0'; c++) {
1222         if (*c == '/')
1223             *c = '_';
1224     }
1225
1226     filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
1227                                sanitized_name);
1228     g_free(sanitized_name);
1229
1230     fd = mkstemp(filename);
1231     if (fd < 0) {
1232         error_setg_errno(errp, errno,
1233                          "unable to create backing store for hugepages");
1234         g_free(filename);
1235         goto error;
1236     }
1237     unlink(filename);
1238     g_free(filename);
1239
1240     memory = ROUND_UP(memory, hpagesize);
1241     total = memory + hpagesize;
1242
1243     /*
1244      * ftruncate is not supported by hugetlbfs in older
1245      * hosts, so don't bother bailing out on errors.
1246      * If anything goes wrong with it under other filesystems,
1247      * mmap will fail.
1248      */
1249     if (ftruncate(fd, memory)) {
1250         perror("ftruncate");
1251     }
1252
1253     ptr = mmap(0, total, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
1254                 -1, 0);
1255     if (ptr == MAP_FAILED) {
1256         error_setg_errno(errp, errno,
1257                          "unable to allocate memory range for hugepages");
1258         close(fd);
1259         goto error;
1260     }
1261
1262     offset = QEMU_ALIGN_UP((uintptr_t)ptr, hpagesize) - (uintptr_t)ptr;
1263
1264     area = mmap(ptr + offset, memory, PROT_READ | PROT_WRITE,
1265                 (block->flags & RAM_SHARED ? MAP_SHARED : MAP_PRIVATE) |
1266                 MAP_FIXED,
1267                 fd, 0);
1268     if (area == MAP_FAILED) {
1269         error_setg_errno(errp, errno,
1270                          "unable to map backing store for hugepages");
1271         munmap(ptr, total);
1272         close(fd);
1273         goto error;
1274     }
1275
1276     if (offset > 0) {
1277         munmap(ptr, offset);
1278     }
1279     ptr += offset;
1280     total -= offset;
1281
1282     if (total > memory + getpagesize()) {
1283         munmap(ptr + memory + getpagesize(),
1284                total - memory - getpagesize());
1285     }
1286
1287     if (mem_prealloc) {
1288         os_mem_prealloc(fd, area, memory);
1289     }
1290
1291     block->fd = fd;
1292     return area;
1293
1294 error:
1295     if (mem_prealloc) {
1296         error_report("%s", error_get_pretty(*errp));
1297         exit(1);
1298     }
1299     return NULL;
1300 }
1301 #endif
1302
1303 /* Called with the ramlist lock held.  */
1304 static ram_addr_t find_ram_offset(ram_addr_t size)
1305 {
1306     RAMBlock *block, *next_block;
1307     ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
1308
1309     assert(size != 0); /* it would hand out same offset multiple times */
1310
1311     if (QLIST_EMPTY_RCU(&ram_list.blocks)) {
1312         return 0;
1313     }
1314
1315     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1316         ram_addr_t end, next = RAM_ADDR_MAX;
1317
1318         end = block->offset + block->max_length;
1319
1320         QLIST_FOREACH_RCU(next_block, &ram_list.blocks, next) {
1321             if (next_block->offset >= end) {
1322                 next = MIN(next, next_block->offset);
1323             }
1324         }
1325         if (next - end >= size && next - end < mingap) {
1326             offset = end;
1327             mingap = next - end;
1328         }
1329     }
1330
1331     if (offset == RAM_ADDR_MAX) {
1332         fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
1333                 (uint64_t)size);
1334         abort();
1335     }
1336
1337     return offset;
1338 }
1339
1340 ram_addr_t last_ram_offset(void)
1341 {
1342     RAMBlock *block;
1343     ram_addr_t last = 0;
1344
1345     rcu_read_lock();
1346     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1347         last = MAX(last, block->offset + block->max_length);
1348     }
1349     rcu_read_unlock();
1350     return last;
1351 }
1352
1353 static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
1354 {
1355     int ret;
1356
1357     /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
1358     if (!machine_dump_guest_core(current_machine)) {
1359         ret = qemu_madvise(addr, size, QEMU_MADV_DONTDUMP);
1360         if (ret) {
1361             perror("qemu_madvise");
1362             fprintf(stderr, "madvise doesn't support MADV_DONTDUMP, "
1363                             "but dump_guest_core=off specified\n");
1364         }
1365     }
1366 }
1367
1368 /* Called within an RCU critical section, or while the ramlist lock
1369  * is held.
1370  */
1371 static RAMBlock *find_ram_block(ram_addr_t addr)
1372 {
1373     RAMBlock *block;
1374
1375     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1376         if (block->offset == addr) {
1377             return block;
1378         }
1379     }
1380
1381     return NULL;
1382 }
1383
1384 /* Called with iothread lock held.  */
1385 void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
1386 {
1387     RAMBlock *new_block, *block;
1388
1389     rcu_read_lock();
1390     new_block = find_ram_block(addr);
1391     assert(new_block);
1392     assert(!new_block->idstr[0]);
1393
1394     if (dev) {
1395         char *id = qdev_get_dev_path(dev);
1396         if (id) {
1397             snprintf(new_block->idstr, sizeof(new_block->idstr), "%s/", id);
1398             g_free(id);
1399         }
1400     }
1401     pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
1402
1403     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1404         if (block != new_block && !strcmp(block->idstr, new_block->idstr)) {
1405             fprintf(stderr, "RAMBlock \"%s\" already registered, abort!\n",
1406                     new_block->idstr);
1407             abort();
1408         }
1409     }
1410     rcu_read_unlock();
1411 }
1412
1413 /* Called with iothread lock held.  */
1414 void qemu_ram_unset_idstr(ram_addr_t addr)
1415 {
1416     RAMBlock *block;
1417
1418     /* FIXME: arch_init.c assumes that this is not called throughout
1419      * migration.  Ignore the problem since hot-unplug during migration
1420      * does not work anyway.
1421      */
1422
1423     rcu_read_lock();
1424     block = find_ram_block(addr);
1425     if (block) {
1426         memset(block->idstr, 0, sizeof(block->idstr));
1427     }
1428     rcu_read_unlock();
1429 }
1430
1431 static int memory_try_enable_merging(void *addr, size_t len)
1432 {
1433     if (!machine_mem_merge(current_machine)) {
1434         /* disabled by the user */
1435         return 0;
1436     }
1437
1438     return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
1439 }
1440
1441 /* Only legal before guest might have detected the memory size: e.g. on
1442  * incoming migration, or right after reset.
1443  *
1444  * As memory core doesn't know how is memory accessed, it is up to
1445  * resize callback to update device state and/or add assertions to detect
1446  * misuse, if necessary.
1447  */
1448 int qemu_ram_resize(ram_addr_t base, ram_addr_t newsize, Error **errp)
1449 {
1450     RAMBlock *block = find_ram_block(base);
1451
1452     assert(block);
1453
1454     newsize = TARGET_PAGE_ALIGN(newsize);
1455
1456     if (block->used_length == newsize) {
1457         return 0;
1458     }
1459
1460     if (!(block->flags & RAM_RESIZEABLE)) {
1461         error_setg_errno(errp, EINVAL,
1462                          "Length mismatch: %s: 0x" RAM_ADDR_FMT
1463                          " in != 0x" RAM_ADDR_FMT, block->idstr,
1464                          newsize, block->used_length);
1465         return -EINVAL;
1466     }
1467
1468     if (block->max_length < newsize) {
1469         error_setg_errno(errp, EINVAL,
1470                          "Length too large: %s: 0x" RAM_ADDR_FMT
1471                          " > 0x" RAM_ADDR_FMT, block->idstr,
1472                          newsize, block->max_length);
1473         return -EINVAL;
1474     }
1475
1476     cpu_physical_memory_clear_dirty_range(block->offset, block->used_length);
1477     block->used_length = newsize;
1478     cpu_physical_memory_set_dirty_range(block->offset, block->used_length,
1479                                         DIRTY_CLIENTS_ALL);
1480     memory_region_set_size(block->mr, newsize);
1481     if (block->resized) {
1482         block->resized(block->idstr, newsize, block->host);
1483     }
1484     return 0;
1485 }
1486
1487 static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp)
1488 {
1489     RAMBlock *block;
1490     RAMBlock *last_block = NULL;
1491     ram_addr_t old_ram_size, new_ram_size;
1492
1493     old_ram_size = last_ram_offset() >> TARGET_PAGE_BITS;
1494
1495     qemu_mutex_lock_ramlist();
1496     new_block->offset = find_ram_offset(new_block->max_length);
1497
1498     if (!new_block->host) {
1499         if (xen_enabled()) {
1500             xen_ram_alloc(new_block->offset, new_block->max_length,
1501                           new_block->mr);
1502         } else {
1503             new_block->host = phys_mem_alloc(new_block->max_length,
1504                                              &new_block->mr->align);
1505             if (!new_block->host) {
1506                 error_setg_errno(errp, errno,
1507                                  "cannot set up guest memory '%s'",
1508                                  memory_region_name(new_block->mr));
1509                 qemu_mutex_unlock_ramlist();
1510                 return -1;
1511             }
1512             memory_try_enable_merging(new_block->host, new_block->max_length);
1513         }
1514     }
1515
1516     new_ram_size = MAX(old_ram_size,
1517               (new_block->offset + new_block->max_length) >> TARGET_PAGE_BITS);
1518     if (new_ram_size > old_ram_size) {
1519         migration_bitmap_extend(old_ram_size, new_ram_size);
1520     }
1521     /* Keep the list sorted from biggest to smallest block.  Unlike QTAILQ,
1522      * QLIST (which has an RCU-friendly variant) does not have insertion at
1523      * tail, so save the last element in last_block.
1524      */
1525     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1526         last_block = block;
1527         if (block->max_length < new_block->max_length) {
1528             break;
1529         }
1530     }
1531     if (block) {
1532         QLIST_INSERT_BEFORE_RCU(block, new_block, next);
1533     } else if (last_block) {
1534         QLIST_INSERT_AFTER_RCU(last_block, new_block, next);
1535     } else { /* list is empty */
1536         QLIST_INSERT_HEAD_RCU(&ram_list.blocks, new_block, next);
1537     }
1538     ram_list.mru_block = NULL;
1539
1540     /* Write list before version */
1541     smp_wmb();
1542     ram_list.version++;
1543     qemu_mutex_unlock_ramlist();
1544
1545     new_ram_size = last_ram_offset() >> TARGET_PAGE_BITS;
1546
1547     if (new_ram_size > old_ram_size) {
1548         int i;
1549
1550         /* ram_list.dirty_memory[] is protected by the iothread lock.  */
1551         for (i = 0; i < DIRTY_MEMORY_NUM; i++) {
1552             ram_list.dirty_memory[i] =
1553                 bitmap_zero_extend(ram_list.dirty_memory[i],
1554                                    old_ram_size, new_ram_size);
1555        }
1556     }
1557     cpu_physical_memory_set_dirty_range(new_block->offset,
1558                                         new_block->used_length,
1559                                         DIRTY_CLIENTS_ALL);
1560
1561     if (new_block->host) {
1562         qemu_ram_setup_dump(new_block->host, new_block->max_length);
1563         qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_HUGEPAGE);
1564         qemu_madvise(new_block->host, new_block->max_length, QEMU_MADV_DONTFORK);
1565         if (kvm_enabled()) {
1566             kvm_setup_guest_memory(new_block->host, new_block->max_length);
1567         }
1568     }
1569
1570     return new_block->offset;
1571 }
1572
1573 #ifdef __linux__
1574 ram_addr_t qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
1575                                     bool share, const char *mem_path,
1576                                     Error **errp)
1577 {
1578     RAMBlock *new_block;
1579     ram_addr_t addr;
1580     Error *local_err = NULL;
1581
1582     if (xen_enabled()) {
1583         error_setg(errp, "-mem-path not supported with Xen");
1584         return -1;
1585     }
1586
1587     if (phys_mem_alloc != qemu_anon_ram_alloc) {
1588         /*
1589          * file_ram_alloc() needs to allocate just like
1590          * phys_mem_alloc, but we haven't bothered to provide
1591          * a hook there.
1592          */
1593         error_setg(errp,
1594                    "-mem-path not supported with this accelerator");
1595         return -1;
1596     }
1597
1598     size = TARGET_PAGE_ALIGN(size);
1599     new_block = g_malloc0(sizeof(*new_block));
1600     new_block->mr = mr;
1601     new_block->used_length = size;
1602     new_block->max_length = size;
1603     new_block->flags = share ? RAM_SHARED : 0;
1604     new_block->flags |= RAM_EXTRA;
1605     new_block->host = file_ram_alloc(new_block, size,
1606                                      mem_path, errp);
1607     if (!new_block->host) {
1608         g_free(new_block);
1609         return -1;
1610     }
1611
1612     addr = ram_block_add(new_block, &local_err);
1613     if (local_err) {
1614         g_free(new_block);
1615         error_propagate(errp, local_err);
1616         return -1;
1617     }
1618     return addr;
1619 }
1620 #endif
1621
1622 static
1623 ram_addr_t qemu_ram_alloc_internal(ram_addr_t size, ram_addr_t max_size,
1624                                    void (*resized)(const char*,
1625                                                    uint64_t length,
1626                                                    void *host),
1627                                    void *host, bool resizeable,
1628                                    MemoryRegion *mr, Error **errp)
1629 {
1630     RAMBlock *new_block;
1631     ram_addr_t addr;
1632     Error *local_err = NULL;
1633
1634     size = TARGET_PAGE_ALIGN(size);
1635     max_size = TARGET_PAGE_ALIGN(max_size);
1636     new_block = g_malloc0(sizeof(*new_block));
1637     new_block->mr = mr;
1638     new_block->resized = resized;
1639     new_block->used_length = size;
1640     new_block->max_length = max_size;
1641     assert(max_size >= size);
1642     new_block->fd = -1;
1643     new_block->host = host;
1644     if (host) {
1645         new_block->flags |= RAM_PREALLOC;
1646     }
1647     if (resizeable) {
1648         new_block->flags |= RAM_RESIZEABLE;
1649     }
1650     addr = ram_block_add(new_block, &local_err);
1651     if (local_err) {
1652         g_free(new_block);
1653         error_propagate(errp, local_err);
1654         return -1;
1655     }
1656     return addr;
1657 }
1658
1659 ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
1660                                    MemoryRegion *mr, Error **errp)
1661 {
1662     return qemu_ram_alloc_internal(size, size, NULL, host, false, mr, errp);
1663 }
1664
1665 ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error **errp)
1666 {
1667     return qemu_ram_alloc_internal(size, size, NULL, NULL, false, mr, errp);
1668 }
1669
1670 ram_addr_t qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t maxsz,
1671                                      void (*resized)(const char*,
1672                                                      uint64_t length,
1673                                                      void *host),
1674                                      MemoryRegion *mr, Error **errp)
1675 {
1676     return qemu_ram_alloc_internal(size, maxsz, resized, NULL, true, mr, errp);
1677 }
1678
1679 void qemu_ram_free_from_ptr(ram_addr_t addr)
1680 {
1681     RAMBlock *block;
1682
1683     qemu_mutex_lock_ramlist();
1684     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1685         if (addr == block->offset) {
1686             QLIST_REMOVE_RCU(block, next);
1687             ram_list.mru_block = NULL;
1688             /* Write list before version */
1689             smp_wmb();
1690             ram_list.version++;
1691             g_free_rcu(block, rcu);
1692             break;
1693         }
1694     }
1695     qemu_mutex_unlock_ramlist();
1696 }
1697
1698 static void reclaim_ramblock(RAMBlock *block)
1699 {
1700     if (block->flags & RAM_PREALLOC) {
1701         ;
1702     } else if (xen_enabled()) {
1703         xen_invalidate_map_cache_entry(block->host);
1704 #ifndef _WIN32
1705     } else if (block->fd >= 0) {
1706         if (block->flags & RAM_EXTRA) {
1707             munmap(block->host, block->max_length + getpagesize());
1708         } else {
1709             munmap(block->host, block->max_length);
1710         }
1711         close(block->fd);
1712 #endif
1713     } else {
1714         qemu_anon_ram_free(block->host, block->max_length);
1715     }
1716     g_free(block);
1717 }
1718
1719 void qemu_ram_free(ram_addr_t addr)
1720 {
1721     RAMBlock *block;
1722
1723     qemu_mutex_lock_ramlist();
1724     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1725         if (addr == block->offset) {
1726             QLIST_REMOVE_RCU(block, next);
1727             ram_list.mru_block = NULL;
1728             /* Write list before version */
1729             smp_wmb();
1730             ram_list.version++;
1731             call_rcu(block, reclaim_ramblock, rcu);
1732             break;
1733         }
1734     }
1735     qemu_mutex_unlock_ramlist();
1736 }
1737
1738 #ifndef _WIN32
1739 void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
1740 {
1741     RAMBlock *block;
1742     ram_addr_t offset;
1743     int flags;
1744     void *area, *vaddr;
1745
1746     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1747         offset = addr - block->offset;
1748         if (offset < block->max_length) {
1749             vaddr = ramblock_ptr(block, offset);
1750             if (block->flags & RAM_PREALLOC) {
1751                 ;
1752             } else if (xen_enabled()) {
1753                 abort();
1754             } else {
1755                 flags = MAP_FIXED;
1756                 if (block->fd >= 0) {
1757                     flags |= (block->flags & RAM_SHARED ?
1758                               MAP_SHARED : MAP_PRIVATE);
1759                     area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1760                                 flags, block->fd, offset);
1761                 } else {
1762                     /*
1763                      * Remap needs to match alloc.  Accelerators that
1764                      * set phys_mem_alloc never remap.  If they did,
1765                      * we'd need a remap hook here.
1766                      */
1767                     assert(phys_mem_alloc == qemu_anon_ram_alloc);
1768
1769                     flags |= MAP_PRIVATE | MAP_ANONYMOUS;
1770                     area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1771                                 flags, -1, 0);
1772                 }
1773                 if (area != vaddr) {
1774                     fprintf(stderr, "Could not remap addr: "
1775                             RAM_ADDR_FMT "@" RAM_ADDR_FMT "\n",
1776                             length, addr);
1777                     exit(1);
1778                 }
1779                 memory_try_enable_merging(vaddr, length);
1780                 qemu_ram_setup_dump(vaddr, length);
1781             }
1782         }
1783     }
1784 }
1785 #endif /* !_WIN32 */
1786
1787 int qemu_get_ram_fd(ram_addr_t addr)
1788 {
1789     RAMBlock *block;
1790     int fd;
1791
1792     rcu_read_lock();
1793     block = qemu_get_ram_block(addr);
1794     fd = block->fd;
1795     rcu_read_unlock();
1796     return fd;
1797 }
1798
1799 void *qemu_get_ram_block_host_ptr(ram_addr_t addr)
1800 {
1801     RAMBlock *block;
1802     void *ptr;
1803
1804     rcu_read_lock();
1805     block = qemu_get_ram_block(addr);
1806     ptr = ramblock_ptr(block, 0);
1807     rcu_read_unlock();
1808     return ptr;
1809 }
1810
1811 /* Return a host pointer to ram allocated with qemu_ram_alloc.
1812  * This should not be used for general purpose DMA.  Use address_space_map
1813  * or address_space_rw instead. For local memory (e.g. video ram) that the
1814  * device owns, use memory_region_get_ram_ptr.
1815  *
1816  * By the time this function returns, the returned pointer is not protected
1817  * by RCU anymore.  If the caller is not within an RCU critical section and
1818  * does not hold the iothread lock, it must have other means of protecting the
1819  * pointer, such as a reference to the region that includes the incoming
1820  * ram_addr_t.
1821  */
1822 void *qemu_get_ram_ptr(ram_addr_t addr)
1823 {
1824     RAMBlock *block;
1825     void *ptr;
1826
1827     rcu_read_lock();
1828     block = qemu_get_ram_block(addr);
1829
1830     if (xen_enabled() && block->host == NULL) {
1831         /* We need to check if the requested address is in the RAM
1832          * because we don't want to map the entire memory in QEMU.
1833          * In that case just map until the end of the page.
1834          */
1835         if (block->offset == 0) {
1836             ptr = xen_map_cache(addr, 0, 0);
1837             goto unlock;
1838         }
1839
1840         block->host = xen_map_cache(block->offset, block->max_length, 1);
1841     }
1842     ptr = ramblock_ptr(block, addr - block->offset);
1843
1844 unlock:
1845     rcu_read_unlock();
1846     return ptr;
1847 }
1848
1849 /* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
1850  * but takes a size argument.
1851  *
1852  * By the time this function returns, the returned pointer is not protected
1853  * by RCU anymore.  If the caller is not within an RCU critical section and
1854  * does not hold the iothread lock, it must have other means of protecting the
1855  * pointer, such as a reference to the region that includes the incoming
1856  * ram_addr_t.
1857  */
1858 static void *qemu_ram_ptr_length(ram_addr_t addr, hwaddr *size)
1859 {
1860     void *ptr;
1861     if (*size == 0) {
1862         return NULL;
1863     }
1864     if (xen_enabled()) {
1865         return xen_map_cache(addr, *size, 1);
1866     } else {
1867         RAMBlock *block;
1868         rcu_read_lock();
1869         QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1870             if (addr - block->offset < block->max_length) {
1871                 if (addr - block->offset + *size > block->max_length)
1872                     *size = block->max_length - addr + block->offset;
1873                 ptr = ramblock_ptr(block, addr - block->offset);
1874                 rcu_read_unlock();
1875                 return ptr;
1876             }
1877         }
1878
1879         fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
1880         abort();
1881     }
1882 }
1883
1884 /* Some of the softmmu routines need to translate from a host pointer
1885  * (typically a TLB entry) back to a ram offset.
1886  *
1887  * By the time this function returns, the returned pointer is not protected
1888  * by RCU anymore.  If the caller is not within an RCU critical section and
1889  * does not hold the iothread lock, it must have other means of protecting the
1890  * pointer, such as a reference to the region that includes the incoming
1891  * ram_addr_t.
1892  */
1893 MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
1894 {
1895     RAMBlock *block;
1896     uint8_t *host = ptr;
1897     MemoryRegion *mr;
1898
1899     if (xen_enabled()) {
1900         rcu_read_lock();
1901         *ram_addr = xen_ram_addr_from_mapcache(ptr);
1902         mr = qemu_get_ram_block(*ram_addr)->mr;
1903         rcu_read_unlock();
1904         return mr;
1905     }
1906
1907     rcu_read_lock();
1908     block = atomic_rcu_read(&ram_list.mru_block);
1909     if (block && block->host && host - block->host < block->max_length) {
1910         goto found;
1911     }
1912
1913     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1914         /* This case append when the block is not mapped. */
1915         if (block->host == NULL) {
1916             continue;
1917         }
1918         if (host - block->host < block->max_length) {
1919             goto found;
1920         }
1921     }
1922
1923     rcu_read_unlock();
1924     return NULL;
1925
1926 found:
1927     *ram_addr = block->offset + (host - block->host);
1928     mr = block->mr;
1929     rcu_read_unlock();
1930     return mr;
1931 }
1932
1933 static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
1934                                uint64_t val, unsigned size)
1935 {
1936     if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
1937         tb_invalidate_phys_page_fast(ram_addr, size);
1938     }
1939     switch (size) {
1940     case 1:
1941         stb_p(qemu_get_ram_ptr(ram_addr), val);
1942         break;
1943     case 2:
1944         stw_p(qemu_get_ram_ptr(ram_addr), val);
1945         break;
1946     case 4:
1947         stl_p(qemu_get_ram_ptr(ram_addr), val);
1948         break;
1949     default:
1950         abort();
1951     }
1952     /* Set both VGA and migration bits for simplicity and to remove
1953      * the notdirty callback faster.
1954      */
1955     cpu_physical_memory_set_dirty_range(ram_addr, size,
1956                                         DIRTY_CLIENTS_NOCODE);
1957     /* we remove the notdirty callback only if the code has been
1958        flushed */
1959     if (!cpu_physical_memory_is_clean(ram_addr)) {
1960         tlb_set_dirty(current_cpu, current_cpu->mem_io_vaddr);
1961     }
1962 }
1963
1964 static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
1965                                  unsigned size, bool is_write)
1966 {
1967     return is_write;
1968 }
1969
1970 static const MemoryRegionOps notdirty_mem_ops = {
1971     .write = notdirty_mem_write,
1972     .valid.accepts = notdirty_mem_accepts,
1973     .endianness = DEVICE_NATIVE_ENDIAN,
1974 };
1975
1976 /* Generate a debug exception if a watchpoint has been hit.  */
1977 static void check_watchpoint(int offset, int len, MemTxAttrs attrs, int flags)
1978 {
1979     CPUState *cpu = current_cpu;
1980     CPUArchState *env = cpu->env_ptr;
1981     target_ulong pc, cs_base;
1982     target_ulong vaddr;
1983     CPUWatchpoint *wp;
1984     int cpu_flags;
1985
1986     if (cpu->watchpoint_hit) {
1987         /* We re-entered the check after replacing the TB. Now raise
1988          * the debug interrupt so that is will trigger after the
1989          * current instruction. */
1990         cpu_interrupt(cpu, CPU_INTERRUPT_DEBUG);
1991         return;
1992     }
1993     vaddr = (cpu->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
1994     QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
1995         if (cpu_watchpoint_address_matches(wp, vaddr, len)
1996             && (wp->flags & flags)) {
1997             if (flags == BP_MEM_READ) {
1998                 wp->flags |= BP_WATCHPOINT_HIT_READ;
1999             } else {
2000                 wp->flags |= BP_WATCHPOINT_HIT_WRITE;
2001             }
2002             wp->hitaddr = vaddr;
2003             wp->hitattrs = attrs;
2004             if (!cpu->watchpoint_hit) {
2005                 cpu->watchpoint_hit = wp;
2006                 tb_check_watchpoint(cpu);
2007                 if (wp->flags & BP_STOP_BEFORE_ACCESS) {
2008                     cpu->exception_index = EXCP_DEBUG;
2009                     cpu_loop_exit(cpu);
2010                 } else {
2011                     cpu_get_tb_cpu_state(env, &pc, &cs_base, &cpu_flags);
2012                     tb_gen_code(cpu, pc, cs_base, cpu_flags, 1);
2013                     cpu_resume_from_signal(cpu, NULL);
2014                 }
2015             }
2016         } else {
2017             wp->flags &= ~BP_WATCHPOINT_HIT;
2018         }
2019     }
2020 }
2021
2022 /* Watchpoint access routines.  Watchpoints are inserted using TLB tricks,
2023    so these check for a hit then pass through to the normal out-of-line
2024    phys routines.  */
2025 static MemTxResult watch_mem_read(void *opaque, hwaddr addr, uint64_t *pdata,
2026                                   unsigned size, MemTxAttrs attrs)
2027 {
2028     MemTxResult res;
2029     uint64_t data;
2030
2031     check_watchpoint(addr & ~TARGET_PAGE_MASK, size, attrs, BP_MEM_READ);
2032     switch (size) {
2033     case 1:
2034         data = address_space_ldub(&address_space_memory, addr, attrs, &res);
2035         break;
2036     case 2:
2037         data = address_space_lduw(&address_space_memory, addr, attrs, &res);
2038         break;
2039     case 4:
2040         data = address_space_ldl(&address_space_memory, addr, attrs, &res);
2041         break;
2042     default: abort();
2043     }
2044     *pdata = data;
2045     return res;
2046 }
2047
2048 static MemTxResult watch_mem_write(void *opaque, hwaddr addr,
2049                                    uint64_t val, unsigned size,
2050                                    MemTxAttrs attrs)
2051 {
2052     MemTxResult res;
2053
2054     check_watchpoint(addr & ~TARGET_PAGE_MASK, size, attrs, BP_MEM_WRITE);
2055     switch (size) {
2056     case 1:
2057         address_space_stb(&address_space_memory, addr, val, attrs, &res);
2058         break;
2059     case 2:
2060         address_space_stw(&address_space_memory, addr, val, attrs, &res);
2061         break;
2062     case 4:
2063         address_space_stl(&address_space_memory, addr, val, attrs, &res);
2064         break;
2065     default: abort();
2066     }
2067     return res;
2068 }
2069
2070 static const MemoryRegionOps watch_mem_ops = {
2071     .read_with_attrs = watch_mem_read,
2072     .write_with_attrs = watch_mem_write,
2073     .endianness = DEVICE_NATIVE_ENDIAN,
2074 };
2075
2076 static MemTxResult subpage_read(void *opaque, hwaddr addr, uint64_t *data,
2077                                 unsigned len, MemTxAttrs attrs)
2078 {
2079     subpage_t *subpage = opaque;
2080     uint8_t buf[8];
2081     MemTxResult res;
2082
2083 #if defined(DEBUG_SUBPAGE)
2084     printf("%s: subpage %p len %u addr " TARGET_FMT_plx "\n", __func__,
2085            subpage, len, addr);
2086 #endif
2087     res = address_space_read(subpage->as, addr + subpage->base,
2088                              attrs, buf, len);
2089     if (res) {
2090         return res;
2091     }
2092     switch (len) {
2093     case 1:
2094         *data = ldub_p(buf);
2095         return MEMTX_OK;
2096     case 2:
2097         *data = lduw_p(buf);
2098         return MEMTX_OK;
2099     case 4:
2100         *data = ldl_p(buf);
2101         return MEMTX_OK;
2102     case 8:
2103         *data = ldq_p(buf);
2104         return MEMTX_OK;
2105     default:
2106         abort();
2107     }
2108 }
2109
2110 static MemTxResult subpage_write(void *opaque, hwaddr addr,
2111                                  uint64_t value, unsigned len, MemTxAttrs attrs)
2112 {
2113     subpage_t *subpage = opaque;
2114     uint8_t buf[8];
2115
2116 #if defined(DEBUG_SUBPAGE)
2117     printf("%s: subpage %p len %u addr " TARGET_FMT_plx
2118            " value %"PRIx64"\n",
2119            __func__, subpage, len, addr, value);
2120 #endif
2121     switch (len) {
2122     case 1:
2123         stb_p(buf, value);
2124         break;
2125     case 2:
2126         stw_p(buf, value);
2127         break;
2128     case 4:
2129         stl_p(buf, value);
2130         break;
2131     case 8:
2132         stq_p(buf, value);
2133         break;
2134     default:
2135         abort();
2136     }
2137     return address_space_write(subpage->as, addr + subpage->base,
2138                                attrs, buf, len);
2139 }
2140
2141 static bool subpage_accepts(void *opaque, hwaddr addr,
2142                             unsigned len, bool is_write)
2143 {
2144     subpage_t *subpage = opaque;
2145 #if defined(DEBUG_SUBPAGE)
2146     printf("%s: subpage %p %c len %u addr " TARGET_FMT_plx "\n",
2147            __func__, subpage, is_write ? 'w' : 'r', len, addr);
2148 #endif
2149
2150     return address_space_access_valid(subpage->as, addr + subpage->base,
2151                                       len, is_write);
2152 }
2153
2154 static const MemoryRegionOps subpage_ops = {
2155     .read_with_attrs = subpage_read,
2156     .write_with_attrs = subpage_write,
2157     .impl.min_access_size = 1,
2158     .impl.max_access_size = 8,
2159     .valid.min_access_size = 1,
2160     .valid.max_access_size = 8,
2161     .valid.accepts = subpage_accepts,
2162     .endianness = DEVICE_NATIVE_ENDIAN,
2163 };
2164
2165 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2166                              uint16_t section)
2167 {
2168     int idx, eidx;
2169
2170     if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2171         return -1;
2172     idx = SUBPAGE_IDX(start);
2173     eidx = SUBPAGE_IDX(end);
2174 #if defined(DEBUG_SUBPAGE)
2175     printf("%s: %p start %08x end %08x idx %08x eidx %08x section %d\n",
2176            __func__, mmio, start, end, idx, eidx, section);
2177 #endif
2178     for (; idx <= eidx; idx++) {
2179         mmio->sub_section[idx] = section;
2180     }
2181
2182     return 0;
2183 }
2184
2185 static subpage_t *subpage_init(AddressSpace *as, hwaddr base)
2186 {
2187     subpage_t *mmio;
2188
2189     mmio = g_malloc0(sizeof(subpage_t));
2190
2191     mmio->as = as;
2192     mmio->base = base;
2193     memory_region_init_io(&mmio->iomem, NULL, &subpage_ops, mmio,
2194                           NULL, TARGET_PAGE_SIZE);
2195     mmio->iomem.subpage = true;
2196 #if defined(DEBUG_SUBPAGE)
2197     printf("%s: %p base " TARGET_FMT_plx " len %08x\n", __func__,
2198            mmio, base, TARGET_PAGE_SIZE);
2199 #endif
2200     subpage_register(mmio, 0, TARGET_PAGE_SIZE-1, PHYS_SECTION_UNASSIGNED);
2201
2202     return mmio;
2203 }
2204
2205 static uint16_t dummy_section(PhysPageMap *map, AddressSpace *as,
2206                               MemoryRegion *mr)
2207 {
2208     assert(as);
2209     MemoryRegionSection section = {
2210         .address_space = as,
2211         .mr = mr,
2212         .offset_within_address_space = 0,
2213         .offset_within_region = 0,
2214         .size = int128_2_64(),
2215     };
2216
2217     return phys_section_add(map, &section);
2218 }
2219
2220 MemoryRegion *iotlb_to_region(CPUState *cpu, hwaddr index)
2221 {
2222     AddressSpaceDispatch *d = atomic_rcu_read(&cpu->memory_dispatch);
2223     MemoryRegionSection *sections = d->map.sections;
2224
2225     return sections[index & ~TARGET_PAGE_MASK].mr;
2226 }
2227
2228 static void io_mem_init(void)
2229 {
2230     memory_region_init_io(&io_mem_rom, NULL, &unassigned_mem_ops, NULL, NULL, UINT64_MAX);
2231     memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, NULL,
2232                           NULL, UINT64_MAX);
2233     memory_region_init_io(&io_mem_notdirty, NULL, &notdirty_mem_ops, NULL,
2234                           NULL, UINT64_MAX);
2235     memory_region_init_io(&io_mem_watch, NULL, &watch_mem_ops, NULL,
2236                           NULL, UINT64_MAX);
2237 }
2238
2239 static void mem_begin(MemoryListener *listener)
2240 {
2241     AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
2242     AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1);
2243     uint16_t n;
2244
2245     n = dummy_section(&d->map, as, &io_mem_unassigned);
2246     assert(n == PHYS_SECTION_UNASSIGNED);
2247     n = dummy_section(&d->map, as, &io_mem_notdirty);
2248     assert(n == PHYS_SECTION_NOTDIRTY);
2249     n = dummy_section(&d->map, as, &io_mem_rom);
2250     assert(n == PHYS_SECTION_ROM);
2251     n = dummy_section(&d->map, as, &io_mem_watch);
2252     assert(n == PHYS_SECTION_WATCH);
2253
2254     d->phys_map  = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };
2255     d->as = as;
2256     as->next_dispatch = d;
2257 }
2258
2259 static void address_space_dispatch_free(AddressSpaceDispatch *d)
2260 {
2261     phys_sections_free(&d->map);
2262     g_free(d);
2263 }
2264
2265 static void mem_commit(MemoryListener *listener)
2266 {
2267     AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
2268     AddressSpaceDispatch *cur = as->dispatch;
2269     AddressSpaceDispatch *next = as->next_dispatch;
2270
2271     phys_page_compact_all(next, next->map.nodes_nb);
2272
2273     atomic_rcu_set(&as->dispatch, next);
2274     if (cur) {
2275         call_rcu(cur, address_space_dispatch_free, rcu);
2276     }
2277 }
2278
2279 static void tcg_commit(MemoryListener *listener)
2280 {
2281     CPUState *cpu;
2282
2283     /* since each CPU stores ram addresses in its TLB cache, we must
2284        reset the modified entries */
2285     /* XXX: slow ! */
2286     CPU_FOREACH(cpu) {
2287         /* FIXME: Disentangle the cpu.h circular files deps so we can
2288            directly get the right CPU from listener.  */
2289         if (cpu->tcg_as_listener != listener) {
2290             continue;
2291         }
2292         cpu_reload_memory_map(cpu);
2293     }
2294 }
2295
2296 void address_space_init_dispatch(AddressSpace *as)
2297 {
2298     as->dispatch = NULL;
2299     as->dispatch_listener = (MemoryListener) {
2300         .begin = mem_begin,
2301         .commit = mem_commit,
2302         .region_add = mem_add,
2303         .region_nop = mem_add,
2304         .priority = 0,
2305     };
2306     memory_listener_register(&as->dispatch_listener, as);
2307 }
2308
2309 void address_space_unregister(AddressSpace *as)
2310 {
2311     memory_listener_unregister(&as->dispatch_listener);
2312 }
2313
2314 void address_space_destroy_dispatch(AddressSpace *as)
2315 {
2316     AddressSpaceDispatch *d = as->dispatch;
2317
2318     atomic_rcu_set(&as->dispatch, NULL);
2319     if (d) {
2320         call_rcu(d, address_space_dispatch_free, rcu);
2321     }
2322 }
2323
2324 static void memory_map_init(void)
2325 {
2326     system_memory = g_malloc(sizeof(*system_memory));
2327
2328     memory_region_init(system_memory, NULL, "system", UINT64_MAX);
2329     address_space_init(&address_space_memory, system_memory, "memory");
2330
2331     system_io = g_malloc(sizeof(*system_io));
2332     memory_region_init_io(system_io, NULL, &unassigned_io_ops, NULL, "io",
2333                           65536);
2334     address_space_init(&address_space_io, system_io, "I/O");
2335 }
2336
2337 MemoryRegion *get_system_memory(void)
2338 {
2339     return system_memory;
2340 }
2341
2342 MemoryRegion *get_system_io(void)
2343 {
2344     return system_io;
2345 }
2346
2347 #endif /* !defined(CONFIG_USER_ONLY) */
2348
2349 /* physical memory access (slow version, mainly for debug) */
2350 #if defined(CONFIG_USER_ONLY)
2351 int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
2352                         uint8_t *buf, int len, int is_write)
2353 {
2354     int l, flags;
2355     target_ulong page;
2356     void * p;
2357
2358     while (len > 0) {
2359         page = addr & TARGET_PAGE_MASK;
2360         l = (page + TARGET_PAGE_SIZE) - addr;
2361         if (l > len)
2362             l = len;
2363         flags = page_get_flags(page);
2364         if (!(flags & PAGE_VALID))
2365             return -1;
2366         if (is_write) {
2367             if (!(flags & PAGE_WRITE))
2368                 return -1;
2369             /* XXX: this code should not depend on lock_user */
2370             if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
2371                 return -1;
2372             memcpy(p, buf, l);
2373             unlock_user(p, addr, l);
2374         } else {
2375             if (!(flags & PAGE_READ))
2376                 return -1;
2377             /* XXX: this code should not depend on lock_user */
2378             if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
2379                 return -1;
2380             memcpy(buf, p, l);
2381             unlock_user(p, addr, 0);
2382         }
2383         len -= l;
2384         buf += l;
2385         addr += l;
2386     }
2387     return 0;
2388 }
2389
2390 #else
2391
2392 static void invalidate_and_set_dirty(MemoryRegion *mr, hwaddr addr,
2393                                      hwaddr length)
2394 {
2395     uint8_t dirty_log_mask = memory_region_get_dirty_log_mask(mr);
2396     /* No early return if dirty_log_mask is or becomes 0, because
2397      * cpu_physical_memory_set_dirty_range will still call
2398      * xen_modified_memory.
2399      */
2400     if (dirty_log_mask) {
2401         dirty_log_mask =
2402             cpu_physical_memory_range_includes_clean(addr, length, dirty_log_mask);
2403     }
2404     if (dirty_log_mask & (1 << DIRTY_MEMORY_CODE)) {
2405         tb_invalidate_phys_range(addr, addr + length);
2406         dirty_log_mask &= ~(1 << DIRTY_MEMORY_CODE);
2407     }
2408     cpu_physical_memory_set_dirty_range(addr, length, dirty_log_mask);
2409 }
2410
2411 static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
2412 {
2413     unsigned access_size_max = mr->ops->valid.max_access_size;
2414
2415     /* Regions are assumed to support 1-4 byte accesses unless
2416        otherwise specified.  */
2417     if (access_size_max == 0) {
2418         access_size_max = 4;
2419     }
2420
2421     /* Bound the maximum access by the alignment of the address.  */
2422     if (!mr->ops->impl.unaligned) {
2423         unsigned align_size_max = addr & -addr;
2424         if (align_size_max != 0 && align_size_max < access_size_max) {
2425             access_size_max = align_size_max;
2426         }
2427     }
2428
2429     /* Don't attempt accesses larger than the maximum.  */
2430     if (l > access_size_max) {
2431         l = access_size_max;
2432     }
2433     l = pow2floor(l);
2434
2435     return l;
2436 }
2437
2438 static bool prepare_mmio_access(MemoryRegion *mr)
2439 {
2440     bool unlocked = !qemu_mutex_iothread_locked();
2441     bool release_lock = false;
2442
2443     if (unlocked && mr->global_locking) {
2444         qemu_mutex_lock_iothread();
2445         unlocked = false;
2446         release_lock = true;
2447     }
2448     if (mr->flush_coalesced_mmio) {
2449         if (unlocked) {
2450             qemu_mutex_lock_iothread();
2451         }
2452         qemu_flush_coalesced_mmio_buffer();
2453         if (unlocked) {
2454             qemu_mutex_unlock_iothread();
2455         }
2456     }
2457
2458     return release_lock;
2459 }
2460
2461 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
2462                              uint8_t *buf, int len, bool is_write)
2463 {
2464     hwaddr l;
2465     uint8_t *ptr;
2466     uint64_t val;
2467     hwaddr addr1;
2468     MemoryRegion *mr;
2469     MemTxResult result = MEMTX_OK;
2470     bool release_lock = false;
2471
2472     rcu_read_lock();
2473     while (len > 0) {
2474         l = len;
2475         mr = address_space_translate(as, addr, &addr1, &l, is_write);
2476
2477         if (is_write) {
2478             if (!memory_access_is_direct(mr, is_write)) {
2479                 release_lock |= prepare_mmio_access(mr);
2480                 l = memory_access_size(mr, l, addr1);
2481                 /* XXX: could force current_cpu to NULL to avoid
2482                    potential bugs */
2483                 switch (l) {
2484                 case 8:
2485                     /* 64 bit write access */
2486                     val = ldq_p(buf);
2487                     result |= memory_region_dispatch_write(mr, addr1, val, 8,
2488                                                            attrs);
2489                     break;
2490                 case 4:
2491                     /* 32 bit write access */
2492                     val = ldl_p(buf);
2493                     result |= memory_region_dispatch_write(mr, addr1, val, 4,
2494                                                            attrs);
2495                     break;
2496                 case 2:
2497                     /* 16 bit write access */
2498                     val = lduw_p(buf);
2499                     result |= memory_region_dispatch_write(mr, addr1, val, 2,
2500                                                            attrs);
2501                     break;
2502                 case 1:
2503                     /* 8 bit write access */
2504                     val = ldub_p(buf);
2505                     result |= memory_region_dispatch_write(mr, addr1, val, 1,
2506                                                            attrs);
2507                     break;
2508                 default:
2509                     abort();
2510                 }
2511             } else {
2512                 addr1 += memory_region_get_ram_addr(mr);
2513                 /* RAM case */
2514                 ptr = qemu_get_ram_ptr(addr1);
2515                 memcpy(ptr, buf, l);
2516                 invalidate_and_set_dirty(mr, addr1, l);
2517             }
2518         } else {
2519             if (!memory_access_is_direct(mr, is_write)) {
2520                 /* I/O case */
2521                 release_lock |= prepare_mmio_access(mr);
2522                 l = memory_access_size(mr, l, addr1);
2523                 switch (l) {
2524                 case 8:
2525                     /* 64 bit read access */
2526                     result |= memory_region_dispatch_read(mr, addr1, &val, 8,
2527                                                           attrs);
2528                     stq_p(buf, val);
2529                     break;
2530                 case 4:
2531                     /* 32 bit read access */
2532                     result |= memory_region_dispatch_read(mr, addr1, &val, 4,
2533                                                           attrs);
2534                     stl_p(buf, val);
2535                     break;
2536                 case 2:
2537                     /* 16 bit read access */
2538                     result |= memory_region_dispatch_read(mr, addr1, &val, 2,
2539                                                           attrs);
2540                     stw_p(buf, val);
2541                     break;
2542                 case 1:
2543                     /* 8 bit read access */
2544                     result |= memory_region_dispatch_read(mr, addr1, &val, 1,
2545                                                           attrs);
2546                     stb_p(buf, val);
2547                     break;
2548                 default:
2549                     abort();
2550                 }
2551             } else {
2552                 /* RAM case */
2553                 ptr = qemu_get_ram_ptr(mr->ram_addr + addr1);
2554                 memcpy(buf, ptr, l);
2555             }
2556         }
2557
2558         if (release_lock) {
2559             qemu_mutex_unlock_iothread();
2560             release_lock = false;
2561         }
2562
2563         len -= l;
2564         buf += l;
2565         addr += l;
2566     }
2567     rcu_read_unlock();
2568
2569     return result;
2570 }
2571
2572 MemTxResult address_space_write(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
2573                                 const uint8_t *buf, int len)
2574 {
2575     return address_space_rw(as, addr, attrs, (uint8_t *)buf, len, true);
2576 }
2577
2578 MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
2579                                uint8_t *buf, int len)
2580 {
2581     return address_space_rw(as, addr, attrs, buf, len, false);
2582 }
2583
2584
2585 void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
2586                             int len, int is_write)
2587 {
2588     address_space_rw(&address_space_memory, addr, MEMTXATTRS_UNSPECIFIED,
2589                      buf, len, is_write);
2590 }
2591
2592 enum write_rom_type {
2593     WRITE_DATA,
2594     FLUSH_CACHE,
2595 };
2596
2597 static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,
2598     hwaddr addr, const uint8_t *buf, int len, enum write_rom_type type)
2599 {
2600     hwaddr l;
2601     uint8_t *ptr;
2602     hwaddr addr1;
2603     MemoryRegion *mr;
2604
2605     rcu_read_lock();
2606     while (len > 0) {
2607         l = len;
2608         mr = address_space_translate(as, addr, &addr1, &l, true);
2609
2610         if (!(memory_region_is_ram(mr) ||
2611               memory_region_is_romd(mr))) {
2612             l = memory_access_size(mr, l, addr1);
2613         } else {
2614             addr1 += memory_region_get_ram_addr(mr);
2615             /* ROM/RAM case */
2616             ptr = qemu_get_ram_ptr(addr1);
2617             switch (type) {
2618             case WRITE_DATA:
2619                 memcpy(ptr, buf, l);
2620                 invalidate_and_set_dirty(mr, addr1, l);
2621                 break;
2622             case FLUSH_CACHE:
2623                 flush_icache_range((uintptr_t)ptr, (uintptr_t)ptr + l);
2624                 break;
2625             }
2626         }
2627         len -= l;
2628         buf += l;
2629         addr += l;
2630     }
2631     rcu_read_unlock();
2632 }
2633
2634 /* used for ROM loading : can write in RAM and ROM */
2635 void cpu_physical_memory_write_rom(AddressSpace *as, hwaddr addr,
2636                                    const uint8_t *buf, int len)
2637 {
2638     cpu_physical_memory_write_rom_internal(as, addr, buf, len, WRITE_DATA);
2639 }
2640
2641 void cpu_flush_icache_range(hwaddr start, int len)
2642 {
2643     /*
2644      * This function should do the same thing as an icache flush that was
2645      * triggered from within the guest. For TCG we are always cache coherent,
2646      * so there is no need to flush anything. For KVM / Xen we need to flush
2647      * the host's instruction cache at least.
2648      */
2649     if (tcg_enabled()) {
2650         return;
2651     }
2652
2653     cpu_physical_memory_write_rom_internal(&address_space_memory,
2654                                            start, NULL, len, FLUSH_CACHE);
2655 }
2656
2657 typedef struct {
2658     MemoryRegion *mr;
2659     void *buffer;
2660     hwaddr addr;
2661     hwaddr len;
2662     bool in_use;
2663 } BounceBuffer;
2664
2665 static BounceBuffer bounce;
2666
2667 typedef struct MapClient {
2668     QEMUBH *bh;
2669     QLIST_ENTRY(MapClient) link;
2670 } MapClient;
2671
2672 QemuMutex map_client_list_lock;
2673 static QLIST_HEAD(map_client_list, MapClient) map_client_list
2674     = QLIST_HEAD_INITIALIZER(map_client_list);
2675
2676 static void cpu_unregister_map_client_do(MapClient *client)
2677 {
2678     QLIST_REMOVE(client, link);
2679     g_free(client);
2680 }
2681
2682 static void cpu_notify_map_clients_locked(void)
2683 {
2684     MapClient *client;
2685
2686     while (!QLIST_EMPTY(&map_client_list)) {
2687         client = QLIST_FIRST(&map_client_list);
2688         qemu_bh_schedule(client->bh);
2689         cpu_unregister_map_client_do(client);
2690     }
2691 }
2692
2693 void cpu_register_map_client(QEMUBH *bh)
2694 {
2695     MapClient *client = g_malloc(sizeof(*client));
2696
2697     qemu_mutex_lock(&map_client_list_lock);
2698     client->bh = bh;
2699     QLIST_INSERT_HEAD(&map_client_list, client, link);
2700     if (!atomic_read(&bounce.in_use)) {
2701         cpu_notify_map_clients_locked();
2702     }
2703     qemu_mutex_unlock(&map_client_list_lock);
2704 }
2705
2706 void cpu_exec_init_all(void)
2707 {
2708     qemu_mutex_init(&ram_list.mutex);
2709     memory_map_init();
2710     io_mem_init();
2711     qemu_mutex_init(&map_client_list_lock);
2712 }
2713
2714 void cpu_unregister_map_client(QEMUBH *bh)
2715 {
2716     MapClient *client;
2717
2718     qemu_mutex_lock(&map_client_list_lock);
2719     QLIST_FOREACH(client, &map_client_list, link) {
2720         if (client->bh == bh) {
2721             cpu_unregister_map_client_do(client);
2722             break;
2723         }
2724     }
2725     qemu_mutex_unlock(&map_client_list_lock);
2726 }
2727
2728 static void cpu_notify_map_clients(void)
2729 {
2730     qemu_mutex_lock(&map_client_list_lock);
2731     cpu_notify_map_clients_locked();
2732     qemu_mutex_unlock(&map_client_list_lock);
2733 }
2734
2735 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
2736 {
2737     MemoryRegion *mr;
2738     hwaddr l, xlat;
2739
2740     rcu_read_lock();
2741     while (len > 0) {
2742         l = len;
2743         mr = address_space_translate(as, addr, &xlat, &l, is_write);
2744         if (!memory_access_is_direct(mr, is_write)) {
2745             l = memory_access_size(mr, l, addr);
2746             if (!memory_region_access_valid(mr, xlat, l, is_write)) {
2747                 return false;
2748             }
2749         }
2750
2751         len -= l;
2752         addr += l;
2753     }
2754     rcu_read_unlock();
2755     return true;
2756 }
2757
2758 /* Map a physical memory region into a host virtual address.
2759  * May map a subset of the requested range, given by and returned in *plen.
2760  * May return NULL if resources needed to perform the mapping are exhausted.
2761  * Use only for reads OR writes - not for read-modify-write operations.
2762  * Use cpu_register_map_client() to know when retrying the map operation is
2763  * likely to succeed.
2764  */
2765 void *address_space_map(AddressSpace *as,
2766                         hwaddr addr,
2767                         hwaddr *plen,
2768                         bool is_write)
2769 {
2770     hwaddr len = *plen;
2771     hwaddr done = 0;
2772     hwaddr l, xlat, base;
2773     MemoryRegion *mr, *this_mr;
2774     ram_addr_t raddr;
2775
2776     if (len == 0) {
2777         return NULL;
2778     }
2779
2780     l = len;
2781     rcu_read_lock();
2782     mr = address_space_translate(as, addr, &xlat, &l, is_write);
2783
2784     if (!memory_access_is_direct(mr, is_write)) {
2785         if (atomic_xchg(&bounce.in_use, true)) {
2786             rcu_read_unlock();
2787             return NULL;
2788         }
2789         /* Avoid unbounded allocations */
2790         l = MIN(l, TARGET_PAGE_SIZE);
2791         bounce.buffer = qemu_memalign(TARGET_PAGE_SIZE, l);
2792         bounce.addr = addr;
2793         bounce.len = l;
2794
2795         memory_region_ref(mr);
2796         bounce.mr = mr;
2797         if (!is_write) {
2798             address_space_read(as, addr, MEMTXATTRS_UNSPECIFIED,
2799                                bounce.buffer, l);
2800         }
2801
2802         rcu_read_unlock();
2803         *plen = l;
2804         return bounce.buffer;
2805     }
2806
2807     base = xlat;
2808     raddr = memory_region_get_ram_addr(mr);
2809
2810     for (;;) {
2811         len -= l;
2812         addr += l;
2813         done += l;
2814         if (len == 0) {
2815             break;
2816         }
2817
2818         l = len;
2819         this_mr = address_space_translate(as, addr, &xlat, &l, is_write);
2820         if (this_mr != mr || xlat != base + done) {
2821             break;
2822         }
2823     }
2824
2825     memory_region_ref(mr);
2826     rcu_read_unlock();
2827     *plen = done;
2828     return qemu_ram_ptr_length(raddr + base, plen);
2829 }
2830
2831 /* Unmaps a memory region previously mapped by address_space_map().
2832  * Will also mark the memory as dirty if is_write == 1.  access_len gives
2833  * the amount of memory that was actually read or written by the caller.
2834  */
2835 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2836                          int is_write, hwaddr access_len)
2837 {
2838     if (buffer != bounce.buffer) {
2839         MemoryRegion *mr;
2840         ram_addr_t addr1;
2841
2842         mr = qemu_ram_addr_from_host(buffer, &addr1);
2843         assert(mr != NULL);
2844         if (is_write) {
2845             invalidate_and_set_dirty(mr, addr1, access_len);
2846         }
2847         if (xen_enabled()) {
2848             xen_invalidate_map_cache_entry(buffer);
2849         }
2850         memory_region_unref(mr);
2851         return;
2852     }
2853     if (is_write) {
2854         address_space_write(as, bounce.addr, MEMTXATTRS_UNSPECIFIED,
2855                             bounce.buffer, access_len);
2856     }
2857     qemu_vfree(bounce.buffer);
2858     bounce.buffer = NULL;
2859     memory_region_unref(bounce.mr);
2860     atomic_mb_set(&bounce.in_use, false);
2861     cpu_notify_map_clients();
2862 }
2863
2864 void *cpu_physical_memory_map(hwaddr addr,
2865                               hwaddr *plen,
2866                               int is_write)
2867 {
2868     return address_space_map(&address_space_memory, addr, plen, is_write);
2869 }
2870
2871 void cpu_physical_memory_unmap(void *buffer, hwaddr len,
2872                                int is_write, hwaddr access_len)
2873 {
2874     return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
2875 }
2876
2877 /* warning: addr must be aligned */
2878 static inline uint32_t address_space_ldl_internal(AddressSpace *as, hwaddr addr,
2879                                                   MemTxAttrs attrs,
2880                                                   MemTxResult *result,
2881                                                   enum device_endian endian)
2882 {
2883     uint8_t *ptr;
2884     uint64_t val;
2885     MemoryRegion *mr;
2886     hwaddr l = 4;
2887     hwaddr addr1;
2888     MemTxResult r;
2889     bool release_lock = false;
2890
2891     rcu_read_lock();
2892     mr = address_space_translate(as, addr, &addr1, &l, false);
2893     if (l < 4 || !memory_access_is_direct(mr, false)) {
2894         release_lock |= prepare_mmio_access(mr);
2895
2896         /* I/O case */
2897         r = memory_region_dispatch_read(mr, addr1, &val, 4, attrs);
2898 #if defined(TARGET_WORDS_BIGENDIAN)
2899         if (endian == DEVICE_LITTLE_ENDIAN) {
2900             val = bswap32(val);
2901         }
2902 #else
2903         if (endian == DEVICE_BIG_ENDIAN) {
2904             val = bswap32(val);
2905         }
2906 #endif
2907     } else {
2908         /* RAM case */
2909         ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
2910                                 & TARGET_PAGE_MASK)
2911                                + addr1);
2912         switch (endian) {
2913         case DEVICE_LITTLE_ENDIAN:
2914             val = ldl_le_p(ptr);
2915             break;
2916         case DEVICE_BIG_ENDIAN:
2917             val = ldl_be_p(ptr);
2918             break;
2919         default:
2920             val = ldl_p(ptr);
2921             break;
2922         }
2923         r = MEMTX_OK;
2924     }
2925     if (result) {
2926         *result = r;
2927     }
2928     if (release_lock) {
2929         qemu_mutex_unlock_iothread();
2930     }
2931     rcu_read_unlock();
2932     return val;
2933 }
2934
2935 uint32_t address_space_ldl(AddressSpace *as, hwaddr addr,
2936                            MemTxAttrs attrs, MemTxResult *result)
2937 {
2938     return address_space_ldl_internal(as, addr, attrs, result,
2939                                       DEVICE_NATIVE_ENDIAN);
2940 }
2941
2942 uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
2943                               MemTxAttrs attrs, MemTxResult *result)
2944 {
2945     return address_space_ldl_internal(as, addr, attrs, result,
2946                                       DEVICE_LITTLE_ENDIAN);
2947 }
2948
2949 uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
2950                               MemTxAttrs attrs, MemTxResult *result)
2951 {
2952     return address_space_ldl_internal(as, addr, attrs, result,
2953                                       DEVICE_BIG_ENDIAN);
2954 }
2955
2956 uint32_t ldl_phys(AddressSpace *as, hwaddr addr)
2957 {
2958     return address_space_ldl(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
2959 }
2960
2961 uint32_t ldl_le_phys(AddressSpace *as, hwaddr addr)
2962 {
2963     return address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
2964 }
2965
2966 uint32_t ldl_be_phys(AddressSpace *as, hwaddr addr)
2967 {
2968     return address_space_ldl_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
2969 }
2970
2971 /* warning: addr must be aligned */
2972 static inline uint64_t address_space_ldq_internal(AddressSpace *as, hwaddr addr,
2973                                                   MemTxAttrs attrs,
2974                                                   MemTxResult *result,
2975                                                   enum device_endian endian)
2976 {
2977     uint8_t *ptr;
2978     uint64_t val;
2979     MemoryRegion *mr;
2980     hwaddr l = 8;
2981     hwaddr addr1;
2982     MemTxResult r;
2983     bool release_lock = false;
2984
2985     rcu_read_lock();
2986     mr = address_space_translate(as, addr, &addr1, &l,
2987                                  false);
2988     if (l < 8 || !memory_access_is_direct(mr, false)) {
2989         release_lock |= prepare_mmio_access(mr);
2990
2991         /* I/O case */
2992         r = memory_region_dispatch_read(mr, addr1, &val, 8, attrs);
2993 #if defined(TARGET_WORDS_BIGENDIAN)
2994         if (endian == DEVICE_LITTLE_ENDIAN) {
2995             val = bswap64(val);
2996         }
2997 #else
2998         if (endian == DEVICE_BIG_ENDIAN) {
2999             val = bswap64(val);
3000         }
3001 #endif
3002     } else {
3003         /* RAM case */
3004         ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
3005                                 & TARGET_PAGE_MASK)
3006                                + addr1);
3007         switch (endian) {
3008         case DEVICE_LITTLE_ENDIAN:
3009             val = ldq_le_p(ptr);
3010             break;
3011         case DEVICE_BIG_ENDIAN:
3012             val = ldq_be_p(ptr);
3013             break;
3014         default:
3015             val = ldq_p(ptr);
3016             break;
3017         }
3018         r = MEMTX_OK;
3019     }
3020     if (result) {
3021         *result = r;
3022     }
3023     if (release_lock) {
3024         qemu_mutex_unlock_iothread();
3025     }
3026     rcu_read_unlock();
3027     return val;
3028 }
3029
3030 uint64_t address_space_ldq(AddressSpace *as, hwaddr addr,
3031                            MemTxAttrs attrs, MemTxResult *result)
3032 {
3033     return address_space_ldq_internal(as, addr, attrs, result,
3034                                       DEVICE_NATIVE_ENDIAN);
3035 }
3036
3037 uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
3038                            MemTxAttrs attrs, MemTxResult *result)
3039 {
3040     return address_space_ldq_internal(as, addr, attrs, result,
3041                                       DEVICE_LITTLE_ENDIAN);
3042 }
3043
3044 uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
3045                            MemTxAttrs attrs, MemTxResult *result)
3046 {
3047     return address_space_ldq_internal(as, addr, attrs, result,
3048                                       DEVICE_BIG_ENDIAN);
3049 }
3050
3051 uint64_t ldq_phys(AddressSpace *as, hwaddr addr)
3052 {
3053     return address_space_ldq(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3054 }
3055
3056 uint64_t ldq_le_phys(AddressSpace *as, hwaddr addr)
3057 {
3058     return address_space_ldq_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3059 }
3060
3061 uint64_t ldq_be_phys(AddressSpace *as, hwaddr addr)
3062 {
3063     return address_space_ldq_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3064 }
3065
3066 /* XXX: optimize */
3067 uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
3068                             MemTxAttrs attrs, MemTxResult *result)
3069 {
3070     uint8_t val;
3071     MemTxResult r;
3072
3073     r = address_space_rw(as, addr, attrs, &val, 1, 0);
3074     if (result) {
3075         *result = r;
3076     }
3077     return val;
3078 }
3079
3080 uint32_t ldub_phys(AddressSpace *as, hwaddr addr)
3081 {
3082     return address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3083 }
3084
3085 /* warning: addr must be aligned */
3086 static inline uint32_t address_space_lduw_internal(AddressSpace *as,
3087                                                    hwaddr addr,
3088                                                    MemTxAttrs attrs,
3089                                                    MemTxResult *result,
3090                                                    enum device_endian endian)
3091 {
3092     uint8_t *ptr;
3093     uint64_t val;
3094     MemoryRegion *mr;
3095     hwaddr l = 2;
3096     hwaddr addr1;
3097     MemTxResult r;
3098     bool release_lock = false;
3099
3100     rcu_read_lock();
3101     mr = address_space_translate(as, addr, &addr1, &l,
3102                                  false);
3103     if (l < 2 || !memory_access_is_direct(mr, false)) {
3104         release_lock |= prepare_mmio_access(mr);
3105
3106         /* I/O case */
3107         r = memory_region_dispatch_read(mr, addr1, &val, 2, attrs);
3108 #if defined(TARGET_WORDS_BIGENDIAN)
3109         if (endian == DEVICE_LITTLE_ENDIAN) {
3110             val = bswap16(val);
3111         }
3112 #else
3113         if (endian == DEVICE_BIG_ENDIAN) {
3114             val = bswap16(val);
3115         }
3116 #endif
3117     } else {
3118         /* RAM case */
3119         ptr = qemu_get_ram_ptr((memory_region_get_ram_addr(mr)
3120                                 & TARGET_PAGE_MASK)
3121                                + addr1);
3122         switch (endian) {
3123         case DEVICE_LITTLE_ENDIAN:
3124             val = lduw_le_p(ptr);
3125             break;
3126         case DEVICE_BIG_ENDIAN:
3127             val = lduw_be_p(ptr);
3128             break;
3129         default:
3130             val = lduw_p(ptr);
3131             break;
3132         }
3133         r = MEMTX_OK;
3134     }
3135     if (result) {
3136         *result = r;
3137     }
3138     if (release_lock) {
3139         qemu_mutex_unlock_iothread();
3140     }
3141     rcu_read_unlock();
3142     return val;
3143 }
3144
3145 uint32_t address_space_lduw(AddressSpace *as, hwaddr addr,
3146                            MemTxAttrs attrs, MemTxResult *result)
3147 {
3148     return address_space_lduw_internal(as, addr, attrs, result,
3149                                        DEVICE_NATIVE_ENDIAN);
3150 }
3151
3152 uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
3153                            MemTxAttrs attrs, MemTxResult *result)
3154 {
3155     return address_space_lduw_internal(as, addr, attrs, result,
3156                                        DEVICE_LITTLE_ENDIAN);
3157 }
3158
3159 uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
3160                            MemTxAttrs attrs, MemTxResult *result)
3161 {
3162     return address_space_lduw_internal(as, addr, attrs, result,
3163                                        DEVICE_BIG_ENDIAN);
3164 }
3165
3166 uint32_t lduw_phys(AddressSpace *as, hwaddr addr)
3167 {
3168     return address_space_lduw(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3169 }
3170
3171 uint32_t lduw_le_phys(AddressSpace *as, hwaddr addr)
3172 {
3173     return address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3174 }
3175
3176 uint32_t lduw_be_phys(AddressSpace *as, hwaddr addr)
3177 {
3178     return address_space_lduw_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3179 }
3180
3181 /* warning: addr must be aligned. The ram page is not masked as dirty
3182    and the code inside is not invalidated. It is useful if the dirty
3183    bits are used to track modified PTEs */
3184 void address_space_stl_notdirty(AddressSpace *as, hwaddr addr, uint32_t val,
3185                                 MemTxAttrs attrs, MemTxResult *result)
3186 {
3187     uint8_t *ptr;
3188     MemoryRegion *mr;
3189     hwaddr l = 4;
3190     hwaddr addr1;
3191     MemTxResult r;
3192     uint8_t dirty_log_mask;
3193     bool release_lock = false;
3194
3195     rcu_read_lock();
3196     mr = address_space_translate(as, addr, &addr1, &l,
3197                                  true);
3198     if (l < 4 || !memory_access_is_direct(mr, true)) {
3199         release_lock |= prepare_mmio_access(mr);
3200
3201         r = memory_region_dispatch_write(mr, addr1, val, 4, attrs);
3202     } else {
3203         addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
3204         ptr = qemu_get_ram_ptr(addr1);
3205         stl_p(ptr, val);
3206
3207         dirty_log_mask = memory_region_get_dirty_log_mask(mr);
3208         dirty_log_mask &= ~(1 << DIRTY_MEMORY_CODE);
3209         cpu_physical_memory_set_dirty_range(addr1, 4, dirty_log_mask);
3210         r = MEMTX_OK;
3211     }
3212     if (result) {
3213         *result = r;
3214     }
3215     if (release_lock) {
3216         qemu_mutex_unlock_iothread();
3217     }
3218     rcu_read_unlock();
3219 }
3220
3221 void stl_phys_notdirty(AddressSpace *as, hwaddr addr, uint32_t val)
3222 {
3223     address_space_stl_notdirty(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3224 }
3225
3226 /* warning: addr must be aligned */
3227 static inline void address_space_stl_internal(AddressSpace *as,
3228                                               hwaddr addr, uint32_t val,
3229                                               MemTxAttrs attrs,
3230                                               MemTxResult *result,
3231                                               enum device_endian endian)
3232 {
3233     uint8_t *ptr;
3234     MemoryRegion *mr;
3235     hwaddr l = 4;
3236     hwaddr addr1;
3237     MemTxResult r;
3238     bool release_lock = false;
3239
3240     rcu_read_lock();
3241     mr = address_space_translate(as, addr, &addr1, &l,
3242                                  true);
3243     if (l < 4 || !memory_access_is_direct(mr, true)) {
3244         release_lock |= prepare_mmio_access(mr);
3245
3246 #if defined(TARGET_WORDS_BIGENDIAN)
3247         if (endian == DEVICE_LITTLE_ENDIAN) {
3248             val = bswap32(val);
3249         }
3250 #else
3251         if (endian == DEVICE_BIG_ENDIAN) {
3252             val = bswap32(val);
3253         }
3254 #endif
3255         r = memory_region_dispatch_write(mr, addr1, val, 4, attrs);
3256     } else {
3257         /* RAM case */
3258         addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
3259         ptr = qemu_get_ram_ptr(addr1);
3260         switch (endian) {
3261         case DEVICE_LITTLE_ENDIAN:
3262             stl_le_p(ptr, val);
3263             break;
3264         case DEVICE_BIG_ENDIAN:
3265             stl_be_p(ptr, val);
3266             break;
3267         default:
3268             stl_p(ptr, val);
3269             break;
3270         }
3271         invalidate_and_set_dirty(mr, addr1, 4);
3272         r = MEMTX_OK;
3273     }
3274     if (result) {
3275         *result = r;
3276     }
3277     if (release_lock) {
3278         qemu_mutex_unlock_iothread();
3279     }
3280     rcu_read_unlock();
3281 }
3282
3283 void address_space_stl(AddressSpace *as, hwaddr addr, uint32_t val,
3284                        MemTxAttrs attrs, MemTxResult *result)
3285 {
3286     address_space_stl_internal(as, addr, val, attrs, result,
3287                                DEVICE_NATIVE_ENDIAN);
3288 }
3289
3290 void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
3291                        MemTxAttrs attrs, MemTxResult *result)
3292 {
3293     address_space_stl_internal(as, addr, val, attrs, result,
3294                                DEVICE_LITTLE_ENDIAN);
3295 }
3296
3297 void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
3298                        MemTxAttrs attrs, MemTxResult *result)
3299 {
3300     address_space_stl_internal(as, addr, val, attrs, result,
3301                                DEVICE_BIG_ENDIAN);
3302 }
3303
3304 void stl_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3305 {
3306     address_space_stl(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3307 }
3308
3309 void stl_le_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3310 {
3311     address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3312 }
3313
3314 void stl_be_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3315 {
3316     address_space_stl_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3317 }
3318
3319 /* XXX: optimize */
3320 void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
3321                        MemTxAttrs attrs, MemTxResult *result)
3322 {
3323     uint8_t v = val;
3324     MemTxResult r;
3325
3326     r = address_space_rw(as, addr, attrs, &v, 1, 1);
3327     if (result) {
3328         *result = r;
3329     }
3330 }
3331
3332 void stb_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3333 {
3334     address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3335 }
3336
3337 /* warning: addr must be aligned */
3338 static inline void address_space_stw_internal(AddressSpace *as,
3339                                               hwaddr addr, uint32_t val,
3340                                               MemTxAttrs attrs,
3341                                               MemTxResult *result,
3342                                               enum device_endian endian)
3343 {
3344     uint8_t *ptr;
3345     MemoryRegion *mr;
3346     hwaddr l = 2;
3347     hwaddr addr1;
3348     MemTxResult r;
3349     bool release_lock = false;
3350
3351     rcu_read_lock();
3352     mr = address_space_translate(as, addr, &addr1, &l, true);
3353     if (l < 2 || !memory_access_is_direct(mr, true)) {
3354         release_lock |= prepare_mmio_access(mr);
3355
3356 #if defined(TARGET_WORDS_BIGENDIAN)
3357         if (endian == DEVICE_LITTLE_ENDIAN) {
3358             val = bswap16(val);
3359         }
3360 #else
3361         if (endian == DEVICE_BIG_ENDIAN) {
3362             val = bswap16(val);
3363         }
3364 #endif
3365         r = memory_region_dispatch_write(mr, addr1, val, 2, attrs);
3366     } else {
3367         /* RAM case */
3368         addr1 += memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK;
3369         ptr = qemu_get_ram_ptr(addr1);
3370         switch (endian) {
3371         case DEVICE_LITTLE_ENDIAN:
3372             stw_le_p(ptr, val);
3373             break;
3374         case DEVICE_BIG_ENDIAN:
3375             stw_be_p(ptr, val);
3376             break;
3377         default:
3378             stw_p(ptr, val);
3379             break;
3380         }
3381         invalidate_and_set_dirty(mr, addr1, 2);
3382         r = MEMTX_OK;
3383     }
3384     if (result) {
3385         *result = r;
3386     }
3387     if (release_lock) {
3388         qemu_mutex_unlock_iothread();
3389     }
3390     rcu_read_unlock();
3391 }
3392
3393 void address_space_stw(AddressSpace *as, hwaddr addr, uint32_t val,
3394                        MemTxAttrs attrs, MemTxResult *result)
3395 {
3396     address_space_stw_internal(as, addr, val, attrs, result,
3397                                DEVICE_NATIVE_ENDIAN);
3398 }
3399
3400 void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
3401                        MemTxAttrs attrs, MemTxResult *result)
3402 {
3403     address_space_stw_internal(as, addr, val, attrs, result,
3404                                DEVICE_LITTLE_ENDIAN);
3405 }
3406
3407 void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
3408                        MemTxAttrs attrs, MemTxResult *result)
3409 {
3410     address_space_stw_internal(as, addr, val, attrs, result,
3411                                DEVICE_BIG_ENDIAN);
3412 }
3413
3414 void stw_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3415 {
3416     address_space_stw(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3417 }
3418
3419 void stw_le_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3420 {
3421     address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3422 }
3423
3424 void stw_be_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3425 {
3426     address_space_stw_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3427 }
3428
3429 /* XXX: optimize */
3430 void address_space_stq(AddressSpace *as, hwaddr addr, uint64_t val,
3431                        MemTxAttrs attrs, MemTxResult *result)
3432 {
3433     MemTxResult r;
3434     val = tswap64(val);
3435     r = address_space_rw(as, addr, attrs, (void *) &val, 8, 1);
3436     if (result) {
3437         *result = r;
3438     }
3439 }
3440
3441 void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
3442                        MemTxAttrs attrs, MemTxResult *result)
3443 {
3444     MemTxResult r;
3445     val = cpu_to_le64(val);
3446     r = address_space_rw(as, addr, attrs, (void *) &val, 8, 1);
3447     if (result) {
3448         *result = r;
3449     }
3450 }
3451 void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
3452                        MemTxAttrs attrs, MemTxResult *result)
3453 {
3454     MemTxResult r;
3455     val = cpu_to_be64(val);
3456     r = address_space_rw(as, addr, attrs, (void *) &val, 8, 1);
3457     if (result) {
3458         *result = r;
3459     }
3460 }
3461
3462 void stq_phys(AddressSpace *as, hwaddr addr, uint64_t val)
3463 {
3464     address_space_stq(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3465 }
3466
3467 void stq_le_phys(AddressSpace *as, hwaddr addr, uint64_t val)
3468 {
3469     address_space_stq_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3470 }
3471
3472 void stq_be_phys(AddressSpace *as, hwaddr addr, uint64_t val)
3473 {
3474     address_space_stq_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3475 }
3476
3477 /* virtual memory access for debug (includes writing to ROM) */
3478 int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
3479                         uint8_t *buf, int len, int is_write)
3480 {
3481     int l;
3482     hwaddr phys_addr;
3483     target_ulong page;
3484
3485     while (len > 0) {
3486         page = addr & TARGET_PAGE_MASK;
3487         phys_addr = cpu_get_phys_page_debug(cpu, page);
3488         /* if no physical page mapped, return an error */
3489         if (phys_addr == -1)
3490             return -1;
3491         l = (page + TARGET_PAGE_SIZE) - addr;
3492         if (l > len)
3493             l = len;
3494         phys_addr += (addr & ~TARGET_PAGE_MASK);
3495         if (is_write) {
3496             cpu_physical_memory_write_rom(cpu->as, phys_addr, buf, l);
3497         } else {
3498             address_space_rw(cpu->as, phys_addr, MEMTXATTRS_UNSPECIFIED,
3499                              buf, l, 0);
3500         }
3501         len -= l;
3502         buf += l;
3503         addr += l;
3504     }
3505     return 0;
3506 }
3507 #endif
3508
3509 /*
3510  * A helper function for the _utterly broken_ virtio device model to find out if
3511  * it's running on a big endian machine. Don't do this at home kids!
3512  */
3513 bool target_words_bigendian(void);
3514 bool target_words_bigendian(void)
3515 {
3516 #if defined(TARGET_WORDS_BIGENDIAN)
3517     return true;
3518 #else
3519     return false;
3520 #endif
3521 }
3522
3523 #ifndef CONFIG_USER_ONLY
3524 bool cpu_physical_memory_is_io(hwaddr phys_addr)
3525 {
3526     MemoryRegion*mr;
3527     hwaddr l = 1;
3528     bool res;
3529
3530     rcu_read_lock();
3531     mr = address_space_translate(&address_space_memory,
3532                                  phys_addr, &phys_addr, &l, false);
3533
3534     res = !(memory_region_is_ram(mr) || memory_region_is_romd(mr));
3535     rcu_read_unlock();
3536     return res;
3537 }
3538
3539 int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
3540 {
3541     RAMBlock *block;
3542     int ret = 0;
3543
3544     rcu_read_lock();
3545     QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
3546         ret = func(block->idstr, block->host, block->offset,
3547                    block->used_length, opaque);
3548         if (ret) {
3549             break;
3550         }
3551     }
3552     rcu_read_unlock();
3553     return ret;
3554 }
3555 #endif
This page took 0.218355 seconds and 4 git commands to generate.