qapi: Sort qapi-schema tests
[qemu/kevin.git] / exec.c
blob7d90a522524b64a86a09c71dd54da804380ad803
1 /*
2 * Virtual page mapping
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
19 #include "config.h"
20 #ifndef _WIN32
21 #include <sys/types.h>
22 #include <sys/mman.h>
23 #endif
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"
54 #include "exec/memory-internal.h"
55 #include "exec/ram_addr.h"
57 #include "qemu/range.h"
59 //#define DEBUG_SUBPAGE
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.
65 RAMList ram_list = { .blocks = QLIST_HEAD_INITIALIZER(ram_list.blocks) };
67 static MemoryRegion *system_memory;
68 static MemoryRegion *system_io;
70 AddressSpace address_space_io;
71 AddressSpace address_space_memory;
73 MemoryRegion io_mem_rom, io_mem_notdirty;
74 static MemoryRegion io_mem_unassigned;
76 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
77 #define RAM_PREALLOC (1 << 0)
79 /* RAM is mmap-ed with MAP_SHARED */
80 #define RAM_SHARED (1 << 1)
82 /* Only a portion of RAM (used_length) is actually used, and migrated.
83 * This used_length size can change across reboots.
85 #define RAM_RESIZEABLE (1 << 2)
87 /* An extra page is mapped on top of this RAM.
89 #define RAM_EXTRA (1 << 3)
90 #endif
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;
101 #if !defined(CONFIG_USER_ONLY)
103 typedef struct PhysPageEntry PhysPageEntry;
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;
112 #define PHYS_MAP_NODE_NIL (((uint32_t)~0) >> 6)
114 /* Size of the L2 (and L3, etc) page tables. */
115 #define ADDR_SPACE_BITS 64
117 #define P_L2_BITS 9
118 #define P_L2_SIZE (1 << P_L2_BITS)
120 #define P_L2_LEVELS (((ADDR_SPACE_BITS - TARGET_PAGE_BITS - 1) / P_L2_BITS) + 1)
122 typedef PhysPageEntry Node[P_L2_SIZE];
124 typedef struct PhysPageMap {
125 struct rcu_head rcu;
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;
135 struct AddressSpaceDispatch {
136 struct rcu_head rcu;
138 /* This is a multi-level map on the physical address space.
139 * The bottom level has pointers to MemoryRegionSections.
141 PhysPageEntry phys_map;
142 PhysPageMap map;
143 AddressSpace *as;
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;
154 #define PHYS_SECTION_UNASSIGNED 0
155 #define PHYS_SECTION_NOTDIRTY 1
156 #define PHYS_SECTION_ROM 2
157 #define PHYS_SECTION_WATCH 3
159 static void io_mem_init(void);
160 static void memory_map_init(void);
161 static void tcg_commit(MemoryListener *listener);
163 static MemoryRegion io_mem_watch;
164 #endif
166 #if !defined(CONFIG_USER_ONLY)
168 static void phys_map_node_reserve(PhysPageMap *map, unsigned nodes)
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);
177 static uint32_t phys_map_node_alloc(PhysPageMap *map, bool leaf)
179 unsigned i;
180 uint32_t ret;
181 PhysPageEntry e;
182 PhysPageEntry *p;
184 ret = map->nodes_nb++;
185 p = map->nodes[ret];
186 assert(ret != PHYS_MAP_NODE_NIL);
187 assert(ret != map->nodes_nb_alloc);
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));
194 return ret;
197 static void phys_page_set_level(PhysPageMap *map, PhysPageEntry *lp,
198 hwaddr *index, hwaddr *nb, uint16_t leaf,
199 int level)
201 PhysPageEntry *p;
202 hwaddr step = (hwaddr)1 << (level * P_L2_BITS);
204 if (lp->skip && lp->ptr == PHYS_MAP_NODE_NIL) {
205 lp->ptr = phys_map_node_alloc(map, level == 0);
207 p = map->nodes[lp->ptr];
208 lp = &p[(*index >> (level * P_L2_BITS)) & (P_L2_SIZE - 1)];
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);
219 ++lp;
223 static void phys_page_set(AddressSpaceDispatch *d,
224 hwaddr index, hwaddr nb,
225 uint16_t leaf)
227 /* Wildly overreserve - it doesn't matter much. */
228 phys_map_node_reserve(&d->map, 3 * P_L2_LEVELS);
230 phys_page_set_level(&d->map, &d->phys_map, &index, &nb, leaf, P_L2_LEVELS - 1);
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.
236 static void phys_page_compact(PhysPageEntry *lp, Node *nodes, unsigned long *compacted)
238 unsigned valid_ptr = P_L2_SIZE;
239 int valid = 0;
240 PhysPageEntry *p;
241 int i;
243 if (lp->ptr == PHYS_MAP_NODE_NIL) {
244 return;
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;
253 valid_ptr = i;
254 valid++;
255 if (p[i].skip) {
256 phys_page_compact(&p[i], nodes, compacted);
260 /* We can only compress if there's only one child. */
261 if (valid != 1) {
262 return;
265 assert(valid_ptr < P_L2_SIZE);
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;
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.
280 lp->skip = 0;
281 } else {
282 lp->skip += p[valid_ptr].skip;
286 static void phys_page_compact_all(AddressSpaceDispatch *d, int nodes_nb)
288 DECLARE_BITMAP(compacted, nodes_nb);
290 if (d->phys_map.skip) {
291 phys_page_compact(&d->phys_map, d->map.nodes, compacted);
295 static MemoryRegionSection *phys_page_find(PhysPageEntry lp, hwaddr addr,
296 Node *nodes, MemoryRegionSection *sections)
298 PhysPageEntry *p;
299 hwaddr index = addr >> TARGET_PAGE_BITS;
300 int i;
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];
306 p = nodes[lp.ptr];
307 lp = p[(index >> (i * P_L2_BITS)) & (P_L2_SIZE - 1)];
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];
319 bool memory_region_is_unassigned(MemoryRegion *mr)
321 return mr != &io_mem_rom && mr != &io_mem_notdirty && !mr->rom_device
322 && mr != &io_mem_watch;
325 /* Called from RCU critical section */
326 static MemoryRegionSection *address_space_lookup_region(AddressSpaceDispatch *d,
327 hwaddr addr,
328 bool resolve_subpage)
330 MemoryRegionSection *section;
331 subpage_t *subpage;
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)]];
338 return section;
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)
346 MemoryRegionSection *section;
347 MemoryRegion *mr;
348 Int128 diff;
350 section = address_space_lookup_region(d, addr, resolve_subpage);
351 /* Compute offset within MemoryRegionSection */
352 addr -= section->offset_within_address_space;
354 /* Compute offset within MemoryRegion */
355 *xlat = addr + section->offset_within_region;
357 mr = section->mr;
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.
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.
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)));
374 return section;
377 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
379 if (memory_region_is_ram(mr)) {
380 return !(is_write && mr->readonly);
382 if (memory_region_is_romd(mr)) {
383 return !is_write;
386 return false;
389 /* Called from RCU critical section */
390 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
391 hwaddr *xlat, hwaddr *plen,
392 bool is_write)
394 IOMMUTLBEntry iotlb;
395 MemoryRegionSection *section;
396 MemoryRegion *mr;
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;
403 if (!mr->iommu_ops) {
404 break;
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;
416 as = iotlb.target_as;
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);
424 *xlat = addr;
425 return mr;
428 /* Called from RCU critical section */
429 MemoryRegionSection *
430 address_space_translate_for_iotlb(CPUState *cpu, hwaddr addr,
431 hwaddr *xlat, hwaddr *plen)
433 MemoryRegionSection *section;
434 section = address_space_translate_internal(cpu->memory_dispatch,
435 addr, xlat, plen, false);
437 assert(!section->mr->iommu_ops);
438 return section;
440 #endif
442 #if !defined(CONFIG_USER_ONLY)
444 static int cpu_common_post_load(void *opaque, int version_id)
446 CPUState *cpu = opaque;
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);
453 return 0;
456 static int cpu_common_pre_load(void *opaque)
458 CPUState *cpu = opaque;
460 cpu->exception_index = -1;
462 return 0;
465 static bool cpu_common_exception_index_needed(void *opaque)
467 CPUState *cpu = opaque;
469 return tcg_enabled() && cpu->exception_index != -1;
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()
483 static bool cpu_common_crash_occurred_needed(void *opaque)
485 CPUState *cpu = opaque;
487 return cpu->crash_occurred;
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()
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()
512 .subsections = (const VMStateDescription*[]) {
513 &vmstate_cpu_common_exception_index,
514 &vmstate_cpu_common_crash_occurred,
515 NULL
519 #endif
521 CPUState *qemu_get_cpu(int index)
523 CPUState *cpu;
525 CPU_FOREACH(cpu) {
526 if (cpu->cpu_index == index) {
527 return cpu;
531 return NULL;
534 #if !defined(CONFIG_USER_ONLY)
535 void tcg_cpu_address_space_init(CPUState *cpu, AddressSpace *as)
537 /* We only support one address space per cpu at the moment. */
538 assert(cpu->as == as);
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);
545 cpu->tcg_as_listener->commit = tcg_commit;
546 memory_listener_register(cpu->tcg_as_listener, as);
548 #endif
550 #ifndef CONFIG_USER_ONLY
551 static DECLARE_BITMAP(cpu_index_map, MAX_CPUMASK_BITS);
553 static int cpu_get_free_index(Error **errp)
555 int cpu = find_first_zero_bit(cpu_index_map, MAX_CPUMASK_BITS);
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;
563 bitmap_set(cpu_index_map, cpu, 1);
564 return cpu;
567 void cpu_exec_exit(CPUState *cpu)
569 if (cpu->cpu_index == -1) {
570 /* cpu_index was never allocated by this @cpu or was already freed. */
571 return;
574 bitmap_clear(cpu_index_map, cpu->cpu_index, 1);
575 cpu->cpu_index = -1;
577 #else
579 static int cpu_get_free_index(Error **errp)
581 CPUState *some_cpu;
582 int cpu_index = 0;
584 CPU_FOREACH(some_cpu) {
585 cpu_index++;
587 return cpu_index;
590 void cpu_exec_exit(CPUState *cpu)
593 #endif
595 void cpu_exec_init(CPUState *cpu, Error **errp)
597 CPUClass *cc = CPU_GET_CLASS(cpu);
598 int cpu_index;
599 Error *local_err = NULL;
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
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;
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);
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);
636 #if defined(CONFIG_USER_ONLY)
637 static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
639 tb_invalidate_phys_page_range(pc, pc + 1, 0);
641 #else
642 static void breakpoint_invalidate(CPUState *cpu, target_ulong pc)
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));
650 #endif
652 #if defined(CONFIG_USER_ONLY)
653 void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
658 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len,
659 int flags)
661 return -ENOSYS;
664 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint)
668 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
669 int flags, CPUWatchpoint **watchpoint)
671 return -ENOSYS;
673 #else
674 /* Add a watchpoint. */
675 int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len,
676 int flags, CPUWatchpoint **watchpoint)
678 CPUWatchpoint *wp;
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;
686 wp = g_malloc(sizeof(*wp));
688 wp->vaddr = addr;
689 wp->len = len;
690 wp->flags = flags;
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);
699 tlb_flush_page(cpu, addr);
701 if (watchpoint)
702 *watchpoint = wp;
703 return 0;
706 /* Remove a specific watchpoint. */
707 int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len,
708 int flags)
710 CPUWatchpoint *wp;
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;
719 return -ENOENT;
722 /* Remove a specific watchpoint by reference. */
723 void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint)
725 QTAILQ_REMOVE(&cpu->watchpoints, watchpoint, entry);
727 tlb_flush_page(cpu, watchpoint->vaddr);
729 g_free(watchpoint);
732 /* Remove all matching watchpoints. */
733 void cpu_watchpoint_remove_all(CPUState *cpu, int mask)
735 CPUWatchpoint *wp, *next;
737 QTAILQ_FOREACH_SAFE(wp, &cpu->watchpoints, entry, next) {
738 if (wp->flags & mask) {
739 cpu_watchpoint_remove_by_ref(cpu, wp);
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).
749 static inline bool cpu_watchpoint_address_matches(CPUWatchpoint *wp,
750 vaddr addr,
751 vaddr len)
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.
758 vaddr wpend = wp->vaddr + wp->len - 1;
759 vaddr addrend = addr + len - 1;
761 return !(addr > wpend || wp->vaddr > addrend);
764 #endif
766 /* Add a breakpoint. */
767 int cpu_breakpoint_insert(CPUState *cpu, vaddr pc, int flags,
768 CPUBreakpoint **breakpoint)
770 CPUBreakpoint *bp;
772 bp = g_malloc(sizeof(*bp));
774 bp->pc = pc;
775 bp->flags = flags;
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);
784 breakpoint_invalidate(cpu, pc);
786 if (breakpoint) {
787 *breakpoint = bp;
789 return 0;
792 /* Remove a specific breakpoint. */
793 int cpu_breakpoint_remove(CPUState *cpu, vaddr pc, int flags)
795 CPUBreakpoint *bp;
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;
803 return -ENOENT;
806 /* Remove a specific breakpoint by reference. */
807 void cpu_breakpoint_remove_by_ref(CPUState *cpu, CPUBreakpoint *breakpoint)
809 QTAILQ_REMOVE(&cpu->breakpoints, breakpoint, entry);
811 breakpoint_invalidate(cpu, breakpoint->pc);
813 g_free(breakpoint);
816 /* Remove all matching breakpoints. */
817 void cpu_breakpoint_remove_all(CPUState *cpu, int mask)
819 CPUBreakpoint *bp, *next;
821 QTAILQ_FOREACH_SAFE(bp, &cpu->breakpoints, entry, next) {
822 if (bp->flags & mask) {
823 cpu_breakpoint_remove_by_ref(cpu, bp);
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)
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);
844 void cpu_abort(CPUState *cpu, const char *fmt, ...)
846 va_list ap;
847 va_list ap2;
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();
863 va_end(ap2);
864 va_end(ap);
865 #if defined(CONFIG_USER_ONLY)
867 struct sigaction act;
868 sigfillset(&act.sa_mask);
869 act.sa_handler = SIG_DFL;
870 sigaction(SIGABRT, &act, NULL);
872 #endif
873 abort();
876 #if !defined(CONFIG_USER_ONLY)
877 /* Called from RCU critical section */
878 static RAMBlock *qemu_get_ram_block(ram_addr_t addr)
880 RAMBlock *block;
882 block = atomic_rcu_read(&ram_list.mru_block);
883 if (block && addr - block->offset < block->max_length) {
884 goto found;
886 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
887 if (addr - block->offset < block->max_length) {
888 goto found;
892 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
893 abort();
895 found:
896 /* It is safe to write mru_block outside the iothread lock. This
897 * is what happens:
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()
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.
912 ram_list.mru_block = block;
913 return block;
916 static void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length)
918 CPUState *cpu;
919 ram_addr_t start1;
920 RAMBlock *block;
921 ram_addr_t end;
923 end = TARGET_PAGE_ALIGN(start + length);
924 start &= TARGET_PAGE_MASK;
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);
933 rcu_read_unlock();
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)
941 unsigned long end, page;
942 bool dirty;
944 if (length == 0) {
945 return false;
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);
953 if (dirty && tcg_enabled()) {
954 tlb_reset_dirty_range_all(start, length);
957 return dirty;
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)
968 hwaddr iotlb;
969 CPUWatchpoint *wp;
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;
980 } else {
981 AddressSpaceDispatch *d;
983 d = atomic_rcu_read(&section->address_space->dispatch);
984 iotlb = section - d->map.sections;
985 iotlb += xlat;
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;
1001 return iotlb;
1003 #endif /* defined(CONFIG_USER_ONLY) */
1005 #if !defined(CONFIG_USER_ONLY)
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);
1011 static void *(*phys_mem_alloc)(size_t size, uint64_t *align) =
1012 qemu_anon_ram_alloc;
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.
1019 void phys_mem_set_alloc(void *(*alloc)(size_t, uint64_t *align))
1021 phys_mem_alloc = alloc;
1024 static uint16_t phys_section_add(PhysPageMap *map,
1025 MemoryRegionSection *section)
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.
1031 assert(map->sections_nb < TARGET_PAGE_SIZE);
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);
1038 map->sections[map->sections_nb] = *section;
1039 memory_region_ref(section->mr);
1040 return map->sections_nb++;
1043 static void phys_section_destroy(MemoryRegion *mr)
1045 memory_region_unref(mr);
1047 if (mr->subpage) {
1048 subpage_t *subpage = container_of(mr, subpage_t, iomem);
1049 object_unref(OBJECT(&subpage->iomem));
1050 g_free(subpage);
1054 static void phys_sections_free(PhysPageMap *map)
1056 while (map->sections_nb > 0) {
1057 MemoryRegionSection *section = &map->sections[--map->sections_nb];
1058 phys_section_destroy(section->mr);
1060 g_free(map->sections);
1061 g_free(map->nodes);
1064 static void register_subpage(AddressSpaceDispatch *d, MemoryRegionSection *section)
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),
1075 hwaddr start, end;
1077 assert(existing->mr->subpage || existing->mr == &io_mem_unassigned);
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);
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));
1095 static void register_multipage(AddressSpaceDispatch *d,
1096 MemoryRegionSection *section)
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));
1103 assert(num_pages);
1104 phys_page_set(d, start_addr >> TARGET_PAGE_BITS, num_pages, section_index);
1107 static void mem_add(MemoryListener *listener, MemoryRegionSection *section)
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);
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;
1118 now.size = int128_min(int128_make64(left), now.size);
1119 register_subpage(d, &now);
1120 } else {
1121 now.size = int128_zero();
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);
1140 void qemu_flush_coalesced_mmio_buffer(void)
1142 if (kvm_enabled())
1143 kvm_flush_coalesced_mmio_buffer();
1146 void qemu_mutex_lock_ramlist(void)
1148 qemu_mutex_lock(&ram_list.mutex);
1151 void qemu_mutex_unlock_ramlist(void)
1153 qemu_mutex_unlock(&ram_list.mutex);
1156 #ifdef __linux__
1158 #include <sys/vfs.h>
1160 #define HUGETLBFS_MAGIC 0x958458f6
1162 static long gethugepagesize(const char *path, Error **errp)
1164 struct statfs fs;
1165 int ret;
1167 do {
1168 ret = statfs(path, &fs);
1169 } while (ret != 0 && errno == EINTR);
1171 if (ret != 0) {
1172 error_setg_errno(errp, errno, "failed to get page size of file %s",
1173 path);
1174 return 0;
1177 if (fs.f_type != HUGETLBFS_MAGIC)
1178 fprintf(stderr, "Warning: path not on HugeTLBFS: %s\n", path);
1180 return fs.f_bsize;
1183 static void *file_ram_alloc(RAMBlock *block,
1184 ram_addr_t memory,
1185 const char *path,
1186 Error **errp)
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;
1199 hpagesize = gethugepagesize(path, &local_err);
1200 if (local_err) {
1201 error_propagate(errp, local_err);
1202 goto error;
1204 block->mr->align = hpagesize;
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;
1213 if (kvm_enabled() && !kvm_has_sync_mmu()) {
1214 error_setg(errp,
1215 "host lacks kvm mmu notifiers, -mem-path unsupported");
1216 goto error;
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 = '_';
1226 filename = g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path,
1227 sanitized_name);
1228 g_free(sanitized_name);
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;
1237 unlink(filename);
1238 g_free(filename);
1240 memory = ROUND_UP(memory, hpagesize);
1241 total = memory + hpagesize;
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.
1249 if (ftruncate(fd, memory)) {
1250 perror("ftruncate");
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;
1262 offset = QEMU_ALIGN_UP((uintptr_t)ptr, hpagesize) - (uintptr_t)ptr;
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;
1276 if (offset > 0) {
1277 munmap(ptr, offset);
1279 ptr += offset;
1280 total -= offset;
1282 if (total > memory + getpagesize()) {
1283 munmap(ptr + memory + getpagesize(),
1284 total - memory - getpagesize());
1287 if (mem_prealloc) {
1288 os_mem_prealloc(fd, area, memory);
1291 block->fd = fd;
1292 return area;
1294 error:
1295 if (mem_prealloc) {
1296 error_report("%s", error_get_pretty(*errp));
1297 exit(1);
1299 return NULL;
1301 #endif
1303 /* Called with the ramlist lock held. */
1304 static ram_addr_t find_ram_offset(ram_addr_t size)
1306 RAMBlock *block, *next_block;
1307 ram_addr_t offset = RAM_ADDR_MAX, mingap = RAM_ADDR_MAX;
1309 assert(size != 0); /* it would hand out same offset multiple times */
1311 if (QLIST_EMPTY_RCU(&ram_list.blocks)) {
1312 return 0;
1315 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1316 ram_addr_t end, next = RAM_ADDR_MAX;
1318 end = block->offset + block->max_length;
1320 QLIST_FOREACH_RCU(next_block, &ram_list.blocks, next) {
1321 if (next_block->offset >= end) {
1322 next = MIN(next, next_block->offset);
1325 if (next - end >= size && next - end < mingap) {
1326 offset = end;
1327 mingap = next - end;
1331 if (offset == RAM_ADDR_MAX) {
1332 fprintf(stderr, "Failed to find gap of requested size: %" PRIu64 "\n",
1333 (uint64_t)size);
1334 abort();
1337 return offset;
1340 ram_addr_t last_ram_offset(void)
1342 RAMBlock *block;
1343 ram_addr_t last = 0;
1345 rcu_read_lock();
1346 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1347 last = MAX(last, block->offset + block->max_length);
1349 rcu_read_unlock();
1350 return last;
1353 static void qemu_ram_setup_dump(void *addr, ram_addr_t size)
1355 int ret;
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");
1368 /* Called within an RCU critical section, or while the ramlist lock
1369 * is held.
1371 static RAMBlock *find_ram_block(ram_addr_t addr)
1373 RAMBlock *block;
1375 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1376 if (block->offset == addr) {
1377 return block;
1381 return NULL;
1384 /* Called with iothread lock held. */
1385 void qemu_ram_set_idstr(ram_addr_t addr, const char *name, DeviceState *dev)
1387 RAMBlock *new_block, *block;
1389 rcu_read_lock();
1390 new_block = find_ram_block(addr);
1391 assert(new_block);
1392 assert(!new_block->idstr[0]);
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);
1401 pstrcat(new_block->idstr, sizeof(new_block->idstr), name);
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();
1410 rcu_read_unlock();
1413 /* Called with iothread lock held. */
1414 void qemu_ram_unset_idstr(ram_addr_t addr)
1416 RAMBlock *block;
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.
1423 rcu_read_lock();
1424 block = find_ram_block(addr);
1425 if (block) {
1426 memset(block->idstr, 0, sizeof(block->idstr));
1428 rcu_read_unlock();
1431 static int memory_try_enable_merging(void *addr, size_t len)
1433 if (!machine_mem_merge(current_machine)) {
1434 /* disabled by the user */
1435 return 0;
1438 return qemu_madvise(addr, len, QEMU_MADV_MERGEABLE);
1441 /* Only legal before guest might have detected the memory size: e.g. on
1442 * incoming migration, or right after reset.
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.
1448 int qemu_ram_resize(ram_addr_t base, ram_addr_t newsize, Error **errp)
1450 RAMBlock *block = find_ram_block(base);
1452 assert(block);
1454 newsize = TARGET_PAGE_ALIGN(newsize);
1456 if (block->used_length == newsize) {
1457 return 0;
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;
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;
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);
1484 return 0;
1487 static ram_addr_t ram_block_add(RAMBlock *new_block, Error **errp)
1489 RAMBlock *block;
1490 RAMBlock *last_block = NULL;
1491 ram_addr_t old_ram_size, new_ram_size;
1493 old_ram_size = last_ram_offset() >> TARGET_PAGE_BITS;
1495 qemu_mutex_lock_ramlist();
1496 new_block->offset = find_ram_offset(new_block->max_length);
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;
1512 memory_try_enable_merging(new_block->host, new_block->max_length);
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);
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.
1525 QLIST_FOREACH_RCU(block, &ram_list.blocks, next) {
1526 last_block = block;
1527 if (block->max_length < new_block->max_length) {
1528 break;
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);
1538 ram_list.mru_block = NULL;
1540 /* Write list before version */
1541 smp_wmb();
1542 ram_list.version++;
1543 qemu_mutex_unlock_ramlist();
1545 new_ram_size = last_ram_offset() >> TARGET_PAGE_BITS;
1547 if (new_ram_size > old_ram_size) {
1548 int i;
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);
1557 cpu_physical_memory_set_dirty_range(new_block->offset,
1558 new_block->used_length,
1559 DIRTY_CLIENTS_ALL);
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);
1570 return new_block->offset;
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)
1578 RAMBlock *new_block;
1579 ram_addr_t addr;
1580 Error *local_err = NULL;
1582 if (xen_enabled()) {
1583 error_setg(errp, "-mem-path not supported with Xen");
1584 return -1;
1587 if (phys_mem_alloc != qemu_anon_ram_alloc) {
1589 * file_ram_alloc() needs to allocate just like
1590 * phys_mem_alloc, but we haven't bothered to provide
1591 * a hook there.
1593 error_setg(errp,
1594 "-mem-path not supported with this accelerator");
1595 return -1;
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;
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;
1618 return addr;
1620 #endif
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)
1630 RAMBlock *new_block;
1631 ram_addr_t addr;
1632 Error *local_err = NULL;
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;
1647 if (resizeable) {
1648 new_block->flags |= RAM_RESIZEABLE;
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;
1656 return addr;
1659 ram_addr_t qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
1660 MemoryRegion *mr, Error **errp)
1662 return qemu_ram_alloc_internal(size, size, NULL, host, false, mr, errp);
1665 ram_addr_t qemu_ram_alloc(ram_addr_t size, MemoryRegion *mr, Error **errp)
1667 return qemu_ram_alloc_internal(size, size, NULL, NULL, false, mr, errp);
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)
1676 return qemu_ram_alloc_internal(size, maxsz, resized, NULL, true, mr, errp);
1679 void qemu_ram_free_from_ptr(ram_addr_t addr)
1681 RAMBlock *block;
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;
1695 qemu_mutex_unlock_ramlist();
1698 static void reclaim_ramblock(RAMBlock *block)
1700 if (block->flags & RAM_PREALLOC) {
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);
1711 close(block->fd);
1712 #endif
1713 } else {
1714 qemu_anon_ram_free(block->host, block->max_length);
1716 g_free(block);
1719 void qemu_ram_free(ram_addr_t addr)
1721 RAMBlock *block;
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;
1735 qemu_mutex_unlock_ramlist();
1738 #ifndef _WIN32
1739 void qemu_ram_remap(ram_addr_t addr, ram_addr_t length)
1741 RAMBlock *block;
1742 ram_addr_t offset;
1743 int flags;
1744 void *area, *vaddr;
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) {
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 {
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.
1767 assert(phys_mem_alloc == qemu_anon_ram_alloc);
1769 flags |= MAP_PRIVATE | MAP_ANONYMOUS;
1770 area = mmap(vaddr, length, PROT_READ | PROT_WRITE,
1771 flags, -1, 0);
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);
1779 memory_try_enable_merging(vaddr, length);
1780 qemu_ram_setup_dump(vaddr, length);
1785 #endif /* !_WIN32 */
1787 int qemu_get_ram_fd(ram_addr_t addr)
1789 RAMBlock *block;
1790 int fd;
1792 rcu_read_lock();
1793 block = qemu_get_ram_block(addr);
1794 fd = block->fd;
1795 rcu_read_unlock();
1796 return fd;
1799 void *qemu_get_ram_block_host_ptr(ram_addr_t addr)
1801 RAMBlock *block;
1802 void *ptr;
1804 rcu_read_lock();
1805 block = qemu_get_ram_block(addr);
1806 ptr = ramblock_ptr(block, 0);
1807 rcu_read_unlock();
1808 return ptr;
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.
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.
1822 void *qemu_get_ram_ptr(ram_addr_t addr)
1824 RAMBlock *block;
1825 void *ptr;
1827 rcu_read_lock();
1828 block = qemu_get_ram_block(addr);
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.
1835 if (block->offset == 0) {
1836 ptr = xen_map_cache(addr, 0, 0);
1837 goto unlock;
1840 block->host = xen_map_cache(block->offset, block->max_length, 1);
1842 ptr = ramblock_ptr(block, addr - block->offset);
1844 unlock:
1845 rcu_read_unlock();
1846 return ptr;
1849 /* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
1850 * but takes a size argument.
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.
1858 static void *qemu_ram_ptr_length(ram_addr_t addr, hwaddr *size)
1860 void *ptr;
1861 if (*size == 0) {
1862 return NULL;
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;
1879 fprintf(stderr, "Bad ram offset %" PRIx64 "\n", (uint64_t)addr);
1880 abort();
1884 /* Some of the softmmu routines need to translate from a host pointer
1885 * (typically a TLB entry) back to a ram offset.
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.
1893 MemoryRegion *qemu_ram_addr_from_host(void *ptr, ram_addr_t *ram_addr)
1895 RAMBlock *block;
1896 uint8_t *host = ptr;
1897 MemoryRegion *mr;
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;
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;
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;
1918 if (host - block->host < block->max_length) {
1919 goto found;
1923 rcu_read_unlock();
1924 return NULL;
1926 found:
1927 *ram_addr = block->offset + (host - block->host);
1928 mr = block->mr;
1929 rcu_read_unlock();
1930 return mr;
1933 static void notdirty_mem_write(void *opaque, hwaddr ram_addr,
1934 uint64_t val, unsigned size)
1936 if (!cpu_physical_memory_get_dirty_flag(ram_addr, DIRTY_MEMORY_CODE)) {
1937 tb_invalidate_phys_page_fast(ram_addr, size);
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();
1952 /* Set both VGA and migration bits for simplicity and to remove
1953 * the notdirty callback faster.
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);
1964 static bool notdirty_mem_accepts(void *opaque, hwaddr addr,
1965 unsigned size, bool is_write)
1967 return is_write;
1970 static const MemoryRegionOps notdirty_mem_ops = {
1971 .write = notdirty_mem_write,
1972 .valid.accepts = notdirty_mem_accepts,
1973 .endianness = DEVICE_NATIVE_ENDIAN,
1976 /* Generate a debug exception if a watchpoint has been hit. */
1977 static void check_watchpoint(int offset, int len, MemTxAttrs attrs, int flags)
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;
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;
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;
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);
2016 } else {
2017 wp->flags &= ~BP_WATCHPOINT_HIT;
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)
2028 MemTxResult res;
2029 uint64_t data;
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();
2044 *pdata = data;
2045 return res;
2048 static MemTxResult watch_mem_write(void *opaque, hwaddr addr,
2049 uint64_t val, unsigned size,
2050 MemTxAttrs attrs)
2052 MemTxResult res;
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();
2067 return res;
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,
2076 static MemTxResult subpage_read(void *opaque, hwaddr addr, uint64_t *data,
2077 unsigned len, MemTxAttrs attrs)
2079 subpage_t *subpage = opaque;
2080 uint8_t buf[8];
2081 MemTxResult res;
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;
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();
2110 static MemTxResult subpage_write(void *opaque, hwaddr addr,
2111 uint64_t value, unsigned len, MemTxAttrs attrs)
2113 subpage_t *subpage = opaque;
2114 uint8_t buf[8];
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();
2137 return address_space_write(subpage->as, addr + subpage->base,
2138 attrs, buf, len);
2141 static bool subpage_accepts(void *opaque, hwaddr addr,
2142 unsigned len, bool is_write)
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
2150 return address_space_access_valid(subpage->as, addr + subpage->base,
2151 len, is_write);
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,
2165 static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2166 uint16_t section)
2168 int idx, eidx;
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;
2182 return 0;
2185 static subpage_t *subpage_init(AddressSpace *as, hwaddr base)
2187 subpage_t *mmio;
2189 mmio = g_malloc0(sizeof(subpage_t));
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);
2202 return mmio;
2205 static uint16_t dummy_section(PhysPageMap *map, AddressSpace *as,
2206 MemoryRegion *mr)
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(),
2217 return phys_section_add(map, &section);
2220 MemoryRegion *iotlb_to_region(CPUState *cpu, hwaddr index)
2222 AddressSpaceDispatch *d = atomic_rcu_read(&cpu->memory_dispatch);
2223 MemoryRegionSection *sections = d->map.sections;
2225 return sections[index & ~TARGET_PAGE_MASK].mr;
2228 static void io_mem_init(void)
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);
2239 static void mem_begin(MemoryListener *listener)
2241 AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
2242 AddressSpaceDispatch *d = g_new0(AddressSpaceDispatch, 1);
2243 uint16_t n;
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);
2254 d->phys_map = (PhysPageEntry) { .ptr = PHYS_MAP_NODE_NIL, .skip = 1 };
2255 d->as = as;
2256 as->next_dispatch = d;
2259 static void address_space_dispatch_free(AddressSpaceDispatch *d)
2261 phys_sections_free(&d->map);
2262 g_free(d);
2265 static void mem_commit(MemoryListener *listener)
2267 AddressSpace *as = container_of(listener, AddressSpace, dispatch_listener);
2268 AddressSpaceDispatch *cur = as->dispatch;
2269 AddressSpaceDispatch *next = as->next_dispatch;
2271 phys_page_compact_all(next, next->map.nodes_nb);
2273 atomic_rcu_set(&as->dispatch, next);
2274 if (cur) {
2275 call_rcu(cur, address_space_dispatch_free, rcu);
2279 static void tcg_commit(MemoryListener *listener)
2281 CPUState *cpu;
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;
2292 cpu_reload_memory_map(cpu);
2296 void address_space_init_dispatch(AddressSpace *as)
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,
2306 memory_listener_register(&as->dispatch_listener, as);
2309 void address_space_unregister(AddressSpace *as)
2311 memory_listener_unregister(&as->dispatch_listener);
2314 void address_space_destroy_dispatch(AddressSpace *as)
2316 AddressSpaceDispatch *d = as->dispatch;
2318 atomic_rcu_set(&as->dispatch, NULL);
2319 if (d) {
2320 call_rcu(d, address_space_dispatch_free, rcu);
2324 static void memory_map_init(void)
2326 system_memory = g_malloc(sizeof(*system_memory));
2328 memory_region_init(system_memory, NULL, "system", UINT64_MAX);
2329 address_space_init(&address_space_memory, system_memory, "memory");
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");
2337 MemoryRegion *get_system_memory(void)
2339 return system_memory;
2342 MemoryRegion *get_system_io(void)
2344 return system_io;
2347 #endif /* !defined(CONFIG_USER_ONLY) */
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)
2354 int l, flags;
2355 target_ulong page;
2356 void * p;
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);
2383 len -= l;
2384 buf += l;
2385 addr += l;
2387 return 0;
2390 #else
2392 static void invalidate_and_set_dirty(MemoryRegion *mr, hwaddr addr,
2393 hwaddr length)
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.
2400 if (dirty_log_mask) {
2401 dirty_log_mask =
2402 cpu_physical_memory_range_includes_clean(addr, length, dirty_log_mask);
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);
2408 cpu_physical_memory_set_dirty_range(addr, length, dirty_log_mask);
2411 static int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr)
2413 unsigned access_size_max = mr->ops->valid.max_access_size;
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;
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;
2429 /* Don't attempt accesses larger than the maximum. */
2430 if (l > access_size_max) {
2431 l = access_size_max;
2433 l = pow2floor(l);
2435 return l;
2438 static bool prepare_mmio_access(MemoryRegion *mr)
2440 bool unlocked = !qemu_mutex_iothread_locked();
2441 bool release_lock = false;
2443 if (unlocked && mr->global_locking) {
2444 qemu_mutex_lock_iothread();
2445 unlocked = false;
2446 release_lock = true;
2448 if (mr->flush_coalesced_mmio) {
2449 if (unlocked) {
2450 qemu_mutex_lock_iothread();
2452 qemu_flush_coalesced_mmio_buffer();
2453 if (unlocked) {
2454 qemu_mutex_unlock_iothread();
2458 return release_lock;
2461 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
2462 uint8_t *buf, int len, bool is_write)
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;
2472 rcu_read_lock();
2473 while (len > 0) {
2474 l = len;
2475 mr = address_space_translate(as, addr, &addr1, &l, is_write);
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();
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);
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();
2551 } else {
2552 /* RAM case */
2553 ptr = qemu_get_ram_ptr(mr->ram_addr + addr1);
2554 memcpy(buf, ptr, l);
2558 if (release_lock) {
2559 qemu_mutex_unlock_iothread();
2560 release_lock = false;
2563 len -= l;
2564 buf += l;
2565 addr += l;
2567 rcu_read_unlock();
2569 return result;
2572 MemTxResult address_space_write(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
2573 const uint8_t *buf, int len)
2575 return address_space_rw(as, addr, attrs, (uint8_t *)buf, len, true);
2578 MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
2579 uint8_t *buf, int len)
2581 return address_space_rw(as, addr, attrs, buf, len, false);
2585 void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,
2586 int len, int is_write)
2588 address_space_rw(&address_space_memory, addr, MEMTXATTRS_UNSPECIFIED,
2589 buf, len, is_write);
2592 enum write_rom_type {
2593 WRITE_DATA,
2594 FLUSH_CACHE,
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)
2600 hwaddr l;
2601 uint8_t *ptr;
2602 hwaddr addr1;
2603 MemoryRegion *mr;
2605 rcu_read_lock();
2606 while (len > 0) {
2607 l = len;
2608 mr = address_space_translate(as, addr, &addr1, &l, true);
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;
2627 len -= l;
2628 buf += l;
2629 addr += l;
2631 rcu_read_unlock();
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)
2638 cpu_physical_memory_write_rom_internal(as, addr, buf, len, WRITE_DATA);
2641 void cpu_flush_icache_range(hwaddr start, int len)
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.
2649 if (tcg_enabled()) {
2650 return;
2653 cpu_physical_memory_write_rom_internal(&address_space_memory,
2654 start, NULL, len, FLUSH_CACHE);
2657 typedef struct {
2658 MemoryRegion *mr;
2659 void *buffer;
2660 hwaddr addr;
2661 hwaddr len;
2662 bool in_use;
2663 } BounceBuffer;
2665 static BounceBuffer bounce;
2667 typedef struct MapClient {
2668 QEMUBH *bh;
2669 QLIST_ENTRY(MapClient) link;
2670 } MapClient;
2672 QemuMutex map_client_list_lock;
2673 static QLIST_HEAD(map_client_list, MapClient) map_client_list
2674 = QLIST_HEAD_INITIALIZER(map_client_list);
2676 static void cpu_unregister_map_client_do(MapClient *client)
2678 QLIST_REMOVE(client, link);
2679 g_free(client);
2682 static void cpu_notify_map_clients_locked(void)
2684 MapClient *client;
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);
2693 void cpu_register_map_client(QEMUBH *bh)
2695 MapClient *client = g_malloc(sizeof(*client));
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();
2703 qemu_mutex_unlock(&map_client_list_lock);
2706 void cpu_exec_init_all(void)
2708 qemu_mutex_init(&ram_list.mutex);
2709 memory_map_init();
2710 io_mem_init();
2711 qemu_mutex_init(&map_client_list_lock);
2714 void cpu_unregister_map_client(QEMUBH *bh)
2716 MapClient *client;
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;
2725 qemu_mutex_unlock(&map_client_list_lock);
2728 static void cpu_notify_map_clients(void)
2730 qemu_mutex_lock(&map_client_list_lock);
2731 cpu_notify_map_clients_locked();
2732 qemu_mutex_unlock(&map_client_list_lock);
2735 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write)
2737 MemoryRegion *mr;
2738 hwaddr l, xlat;
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;
2751 len -= l;
2752 addr += l;
2754 rcu_read_unlock();
2755 return true;
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.
2765 void *address_space_map(AddressSpace *as,
2766 hwaddr addr,
2767 hwaddr *plen,
2768 bool is_write)
2770 hwaddr len = *plen;
2771 hwaddr done = 0;
2772 hwaddr l, xlat, base;
2773 MemoryRegion *mr, *this_mr;
2774 ram_addr_t raddr;
2776 if (len == 0) {
2777 return NULL;
2780 l = len;
2781 rcu_read_lock();
2782 mr = address_space_translate(as, addr, &xlat, &l, is_write);
2784 if (!memory_access_is_direct(mr, is_write)) {
2785 if (atomic_xchg(&bounce.in_use, true)) {
2786 rcu_read_unlock();
2787 return NULL;
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;
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);
2802 rcu_read_unlock();
2803 *plen = l;
2804 return bounce.buffer;
2807 base = xlat;
2808 raddr = memory_region_get_ram_addr(mr);
2810 for (;;) {
2811 len -= l;
2812 addr += l;
2813 done += l;
2814 if (len == 0) {
2815 break;
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;
2825 memory_region_ref(mr);
2826 rcu_read_unlock();
2827 *plen = done;
2828 return qemu_ram_ptr_length(raddr + base, plen);
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.
2835 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2836 int is_write, hwaddr access_len)
2838 if (buffer != bounce.buffer) {
2839 MemoryRegion *mr;
2840 ram_addr_t addr1;
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);
2847 if (xen_enabled()) {
2848 xen_invalidate_map_cache_entry(buffer);
2850 memory_region_unref(mr);
2851 return;
2853 if (is_write) {
2854 address_space_write(as, bounce.addr, MEMTXATTRS_UNSPECIFIED,
2855 bounce.buffer, access_len);
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();
2864 void *cpu_physical_memory_map(hwaddr addr,
2865 hwaddr *plen,
2866 int is_write)
2868 return address_space_map(&address_space_memory, addr, plen, is_write);
2871 void cpu_physical_memory_unmap(void *buffer, hwaddr len,
2872 int is_write, hwaddr access_len)
2874 return address_space_unmap(&address_space_memory, buffer, len, is_write, access_len);
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)
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;
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);
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);
2902 #else
2903 if (endian == DEVICE_BIG_ENDIAN) {
2904 val = bswap32(val);
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;
2923 r = MEMTX_OK;
2925 if (result) {
2926 *result = r;
2928 if (release_lock) {
2929 qemu_mutex_unlock_iothread();
2931 rcu_read_unlock();
2932 return val;
2935 uint32_t address_space_ldl(AddressSpace *as, hwaddr addr,
2936 MemTxAttrs attrs, MemTxResult *result)
2938 return address_space_ldl_internal(as, addr, attrs, result,
2939 DEVICE_NATIVE_ENDIAN);
2942 uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
2943 MemTxAttrs attrs, MemTxResult *result)
2945 return address_space_ldl_internal(as, addr, attrs, result,
2946 DEVICE_LITTLE_ENDIAN);
2949 uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
2950 MemTxAttrs attrs, MemTxResult *result)
2952 return address_space_ldl_internal(as, addr, attrs, result,
2953 DEVICE_BIG_ENDIAN);
2956 uint32_t ldl_phys(AddressSpace *as, hwaddr addr)
2958 return address_space_ldl(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
2961 uint32_t ldl_le_phys(AddressSpace *as, hwaddr addr)
2963 return address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
2966 uint32_t ldl_be_phys(AddressSpace *as, hwaddr addr)
2968 return address_space_ldl_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
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)
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;
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);
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);
2997 #else
2998 if (endian == DEVICE_BIG_ENDIAN) {
2999 val = bswap64(val);
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;
3018 r = MEMTX_OK;
3020 if (result) {
3021 *result = r;
3023 if (release_lock) {
3024 qemu_mutex_unlock_iothread();
3026 rcu_read_unlock();
3027 return val;
3030 uint64_t address_space_ldq(AddressSpace *as, hwaddr addr,
3031 MemTxAttrs attrs, MemTxResult *result)
3033 return address_space_ldq_internal(as, addr, attrs, result,
3034 DEVICE_NATIVE_ENDIAN);
3037 uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
3038 MemTxAttrs attrs, MemTxResult *result)
3040 return address_space_ldq_internal(as, addr, attrs, result,
3041 DEVICE_LITTLE_ENDIAN);
3044 uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
3045 MemTxAttrs attrs, MemTxResult *result)
3047 return address_space_ldq_internal(as, addr, attrs, result,
3048 DEVICE_BIG_ENDIAN);
3051 uint64_t ldq_phys(AddressSpace *as, hwaddr addr)
3053 return address_space_ldq(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3056 uint64_t ldq_le_phys(AddressSpace *as, hwaddr addr)
3058 return address_space_ldq_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3061 uint64_t ldq_be_phys(AddressSpace *as, hwaddr addr)
3063 return address_space_ldq_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3066 /* XXX: optimize */
3067 uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
3068 MemTxAttrs attrs, MemTxResult *result)
3070 uint8_t val;
3071 MemTxResult r;
3073 r = address_space_rw(as, addr, attrs, &val, 1, 0);
3074 if (result) {
3075 *result = r;
3077 return val;
3080 uint32_t ldub_phys(AddressSpace *as, hwaddr addr)
3082 return address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
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)
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;
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);
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);
3112 #else
3113 if (endian == DEVICE_BIG_ENDIAN) {
3114 val = bswap16(val);
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;
3133 r = MEMTX_OK;
3135 if (result) {
3136 *result = r;
3138 if (release_lock) {
3139 qemu_mutex_unlock_iothread();
3141 rcu_read_unlock();
3142 return val;
3145 uint32_t address_space_lduw(AddressSpace *as, hwaddr addr,
3146 MemTxAttrs attrs, MemTxResult *result)
3148 return address_space_lduw_internal(as, addr, attrs, result,
3149 DEVICE_NATIVE_ENDIAN);
3152 uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
3153 MemTxAttrs attrs, MemTxResult *result)
3155 return address_space_lduw_internal(as, addr, attrs, result,
3156 DEVICE_LITTLE_ENDIAN);
3159 uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
3160 MemTxAttrs attrs, MemTxResult *result)
3162 return address_space_lduw_internal(as, addr, attrs, result,
3163 DEVICE_BIG_ENDIAN);
3166 uint32_t lduw_phys(AddressSpace *as, hwaddr addr)
3168 return address_space_lduw(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3171 uint32_t lduw_le_phys(AddressSpace *as, hwaddr addr)
3173 return address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
3176 uint32_t lduw_be_phys(AddressSpace *as, hwaddr addr)
3178 return address_space_lduw_be(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
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)
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;
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);
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);
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;
3212 if (result) {
3213 *result = r;
3215 if (release_lock) {
3216 qemu_mutex_unlock_iothread();
3218 rcu_read_unlock();
3221 void stl_phys_notdirty(AddressSpace *as, hwaddr addr, uint32_t val)
3223 address_space_stl_notdirty(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
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)
3233 uint8_t *ptr;
3234 MemoryRegion *mr;
3235 hwaddr l = 4;
3236 hwaddr addr1;
3237 MemTxResult r;
3238 bool release_lock = false;
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);
3246 #if defined(TARGET_WORDS_BIGENDIAN)
3247 if (endian == DEVICE_LITTLE_ENDIAN) {
3248 val = bswap32(val);
3250 #else
3251 if (endian == DEVICE_BIG_ENDIAN) {
3252 val = bswap32(val);
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;
3271 invalidate_and_set_dirty(mr, addr1, 4);
3272 r = MEMTX_OK;
3274 if (result) {
3275 *result = r;
3277 if (release_lock) {
3278 qemu_mutex_unlock_iothread();
3280 rcu_read_unlock();
3283 void address_space_stl(AddressSpace *as, hwaddr addr, uint32_t val,
3284 MemTxAttrs attrs, MemTxResult *result)
3286 address_space_stl_internal(as, addr, val, attrs, result,
3287 DEVICE_NATIVE_ENDIAN);
3290 void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
3291 MemTxAttrs attrs, MemTxResult *result)
3293 address_space_stl_internal(as, addr, val, attrs, result,
3294 DEVICE_LITTLE_ENDIAN);
3297 void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
3298 MemTxAttrs attrs, MemTxResult *result)
3300 address_space_stl_internal(as, addr, val, attrs, result,
3301 DEVICE_BIG_ENDIAN);
3304 void stl_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3306 address_space_stl(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3309 void stl_le_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3311 address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3314 void stl_be_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3316 address_space_stl_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3319 /* XXX: optimize */
3320 void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
3321 MemTxAttrs attrs, MemTxResult *result)
3323 uint8_t v = val;
3324 MemTxResult r;
3326 r = address_space_rw(as, addr, attrs, &v, 1, 1);
3327 if (result) {
3328 *result = r;
3332 void stb_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3334 address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
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)
3344 uint8_t *ptr;
3345 MemoryRegion *mr;
3346 hwaddr l = 2;
3347 hwaddr addr1;
3348 MemTxResult r;
3349 bool release_lock = false;
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);
3356 #if defined(TARGET_WORDS_BIGENDIAN)
3357 if (endian == DEVICE_LITTLE_ENDIAN) {
3358 val = bswap16(val);
3360 #else
3361 if (endian == DEVICE_BIG_ENDIAN) {
3362 val = bswap16(val);
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;
3381 invalidate_and_set_dirty(mr, addr1, 2);
3382 r = MEMTX_OK;
3384 if (result) {
3385 *result = r;
3387 if (release_lock) {
3388 qemu_mutex_unlock_iothread();
3390 rcu_read_unlock();
3393 void address_space_stw(AddressSpace *as, hwaddr addr, uint32_t val,
3394 MemTxAttrs attrs, MemTxResult *result)
3396 address_space_stw_internal(as, addr, val, attrs, result,
3397 DEVICE_NATIVE_ENDIAN);
3400 void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
3401 MemTxAttrs attrs, MemTxResult *result)
3403 address_space_stw_internal(as, addr, val, attrs, result,
3404 DEVICE_LITTLE_ENDIAN);
3407 void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
3408 MemTxAttrs attrs, MemTxResult *result)
3410 address_space_stw_internal(as, addr, val, attrs, result,
3411 DEVICE_BIG_ENDIAN);
3414 void stw_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3416 address_space_stw(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3419 void stw_le_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3421 address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3424 void stw_be_phys(AddressSpace *as, hwaddr addr, uint32_t val)
3426 address_space_stw_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3429 /* XXX: optimize */
3430 void address_space_stq(AddressSpace *as, hwaddr addr, uint64_t val,
3431 MemTxAttrs attrs, MemTxResult *result)
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;
3441 void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
3442 MemTxAttrs attrs, MemTxResult *result)
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;
3451 void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
3452 MemTxAttrs attrs, MemTxResult *result)
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;
3462 void stq_phys(AddressSpace *as, hwaddr addr, uint64_t val)
3464 address_space_stq(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3467 void stq_le_phys(AddressSpace *as, hwaddr addr, uint64_t val)
3469 address_space_stq_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
3472 void stq_be_phys(AddressSpace *as, hwaddr addr, uint64_t val)
3474 address_space_stq_be(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
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)
3481 int l;
3482 hwaddr phys_addr;
3483 target_ulong page;
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);
3501 len -= l;
3502 buf += l;
3503 addr += l;
3505 return 0;
3507 #endif
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!
3513 bool target_words_bigendian(void);
3514 bool target_words_bigendian(void)
3516 #if defined(TARGET_WORDS_BIGENDIAN)
3517 return true;
3518 #else
3519 return false;
3520 #endif
3523 #ifndef CONFIG_USER_ONLY
3524 bool cpu_physical_memory_is_io(hwaddr phys_addr)
3526 MemoryRegion*mr;
3527 hwaddr l = 1;
3528 bool res;
3530 rcu_read_lock();
3531 mr = address_space_translate(&address_space_memory,
3532 phys_addr, &phys_addr, &l, false);
3534 res = !(memory_region_is_ram(mr) || memory_region_is_romd(mr));
3535 rcu_read_unlock();
3536 return res;
3539 int qemu_ram_foreach_block(RAMBlockIterFunc func, void *opaque)
3541 RAMBlock *block;
3542 int ret = 0;
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;
3552 rcu_read_unlock();
3553 return ret;
3555 #endif