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/>.
21 #include <sys/types.h>
25 #include "qemu-common.h"
30 #include "qemu/osdep.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/sysemu.h"
33 #include "hw/xen/xen.h"
34 #include "qemu/timer.h"
35 #include "qemu/config-file.h"
36 #include "qemu/error-report.h"
37 #include "exec/memory.h"
38 #include "sysemu/dma.h"
39 #include "exec/address-spaces.h"
40 #if defined(CONFIG_USER_ONLY)
42 #else /* !CONFIG_USER_ONLY */
43 #include "sysemu/xen-mapcache.h"
46 #include "exec/cpu-all.h"
47 #include "qemu/rcu_queue.h"
48 #include "exec/cputlb.h"
49 #include "translate-all.h"
51 #include "exec/memory-internal.h"
52 #include "exec/ram_addr.h"
54 #include "qemu/range.h"
56 //#define DEBUG_SUBPAGE
58 #if !defined(CONFIG_USER_ONLY)
59 static bool in_migration
;
61 /* ram_list is read under rcu_read_lock()/rcu_read_unlock(). Writes
62 * are protected by the ramlist lock.
64 RAMList ram_list
= { .blocks
= QLIST_HEAD_INITIALIZER(ram_list
.blocks
) };
66 static MemoryRegion
*system_memory
;
67 static MemoryRegion
*system_io
;
69 AddressSpace address_space_io
;
70 AddressSpace address_space_memory
;
72 MemoryRegion io_mem_rom
, io_mem_notdirty
;
73 static MemoryRegion io_mem_unassigned
;
75 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
76 #define RAM_PREALLOC (1 << 0)
78 /* RAM is mmap-ed with MAP_SHARED */
79 #define RAM_SHARED (1 << 1)
81 /* Only a portion of RAM (used_length) is actually used, and migrated.
82 * This used_length size can change across reboots.
84 #define RAM_RESIZEABLE (1 << 2)
88 struct CPUTailQ cpus
= QTAILQ_HEAD_INITIALIZER(cpus
);
89 /* current CPU in the current thread. It is only valid inside
91 DEFINE_TLS(CPUState
*, current_cpu
);
92 /* 0 = Do not count executed instructions.
93 1 = Precise instruction counting.
94 2 = Adaptive rate instruction counting. */
97 #if !defined(CONFIG_USER_ONLY)
99 typedef struct PhysPageEntry PhysPageEntry
;
101 struct PhysPageEntry
{
102 /* How many bits skip to next level (in units of L2_SIZE). 0 for a leaf. */
104 /* index into phys_sections (!skip) or phys_map_nodes (skip) */
108 #define PHYS_MAP_NODE_NIL (((uint32_t)~0) >> 6)
110 /* Size of the L2 (and L3, etc) page tables. */
111 #define ADDR_SPACE_BITS 64
114 #define P_L2_SIZE (1 << P_L2_BITS)
116 #define P_L2_LEVELS (((ADDR_SPACE_BITS - TARGET_PAGE_BITS - 1) / P_L2_BITS) + 1)
118 typedef PhysPageEntry Node
[P_L2_SIZE
];
120 typedef struct PhysPageMap
{
123 unsigned sections_nb
;
124 unsigned sections_nb_alloc
;
126 unsigned nodes_nb_alloc
;
128 MemoryRegionSection
*sections
;
131 struct AddressSpaceDispatch
{
134 /* This is a multi-level map on the physical address space.
135 * The bottom level has pointers to MemoryRegionSections.
137 PhysPageEntry phys_map
;
142 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
143 typedef struct subpage_t
{
147 uint16_t sub_section
[TARGET_PAGE_SIZE
];
150 #define PHYS_SECTION_UNASSIGNED 0
151 #define PHYS_SECTION_NOTDIRTY 1
152 #define PHYS_SECTION_ROM 2
153 #define PHYS_SECTION_WATCH 3
155 static void io_mem_init(void);
156 static void memory_map_init(void);
157 static void tcg_commit(MemoryListener
*listener
);
159 static MemoryRegion io_mem_watch
;
162 #if !defined(CONFIG_USER_ONLY)
164 static void phys_map_node_reserve(PhysPageMap
*map
, unsigned nodes
)
166 if (map
->nodes_nb
+ nodes
> map
->nodes_nb_alloc
) {
167 map
->nodes_nb_alloc
= MAX(map
->nodes_nb_alloc
* 2, 16);
168 map
->nodes_nb_alloc
= MAX(map
->nodes_nb_alloc
, map
->nodes_nb
+ nodes
);
169 map
->nodes
= g_renew(Node
, map
->nodes
, map
->nodes_nb_alloc
);
173 static uint32_t phys_map_node_alloc(PhysPageMap
*map
)
178 ret
= map
->nodes_nb
++;
179 assert(ret
!= PHYS_MAP_NODE_NIL
);
180 assert(ret
!= map
->nodes_nb_alloc
);
181 for (i
= 0; i
< P_L2_SIZE
; ++i
) {
182 map
->nodes
[ret
][i
].skip
= 1;
183 map
->nodes
[ret
][i
].ptr
= PHYS_MAP_NODE_NIL
;
188 static void phys_page_set_level(PhysPageMap
*map
, PhysPageEntry
*lp
,
189 hwaddr
*index
, hwaddr
*nb
, uint16_t leaf
,
194 hwaddr step
= (hwaddr
)1 << (level
* P_L2_BITS
);
196 if (lp
->skip
&& lp
->ptr
== PHYS_MAP_NODE_NIL
) {
197 lp
->ptr
= phys_map_node_alloc(map
);
198 p
= map
->nodes
[lp
->ptr
];
200 for (i
= 0; i
< P_L2_SIZE
; i
++) {
202 p
[i
].ptr
= PHYS_SECTION_UNASSIGNED
;
206 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
) {
217 phys_page_set_level(map
, lp
, index
, nb
, leaf
, level
- 1);
223 static void phys_page_set(AddressSpaceDispatch
*d
,
224 hwaddr index
, hwaddr nb
,
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
;
243 if (lp
->ptr
== PHYS_MAP_NODE_NIL
) {
248 for (i
= 0; i
< P_L2_SIZE
; i
++) {
249 if (p
[i
].ptr
== PHYS_MAP_NODE_NIL
) {
256 phys_page_compact(&p
[i
], nodes
, compacted
);
260 /* We can only compress if there's only one child. */
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)) {
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
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
)
299 hwaddr index
= addr
>> TARGET_PAGE_BITS
;
302 for (i
= P_L2_LEVELS
; lp
.skip
&& (i
-= lp
.skip
) >= 0;) {
303 if (lp
.ptr
== PHYS_MAP_NODE_NIL
) {
304 return §ions
[PHYS_SECTION_UNASSIGNED
];
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 §ions
[lp
.ptr
];
315 return §ions
[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
,
328 bool resolve_subpage
)
330 MemoryRegionSection
*section
;
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
)]];
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
;
349 section
= address_space_lookup_region(d
, addr
, resolve_subpage
);
350 /* Compute offset within MemoryRegionSection */
351 addr
-= section
->offset_within_address_space
;
353 /* Compute offset within MemoryRegion */
354 *xlat
= addr
+ section
->offset_within_region
;
356 diff
= int128_sub(section
->mr
->size
, int128_make64(addr
));
357 *plen
= int128_get64(int128_min(diff
, int128_make64(*plen
)));
361 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
363 if (memory_region_is_ram(mr
)) {
364 return !(is_write
&& mr
->readonly
);
366 if (memory_region_is_romd(mr
)) {
373 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
374 hwaddr
*xlat
, hwaddr
*plen
,
378 MemoryRegionSection
*section
;
384 AddressSpaceDispatch
*d
= atomic_rcu_read(&as
->dispatch
);
385 section
= address_space_translate_internal(d
, addr
, &addr
, plen
, true);
388 if (!mr
->iommu_ops
) {
392 iotlb
= mr
->iommu_ops
->translate(mr
, addr
, is_write
);
393 addr
= ((iotlb
.translated_addr
& ~iotlb
.addr_mask
)
394 | (addr
& iotlb
.addr_mask
));
395 len
= MIN(len
, (addr
| iotlb
.addr_mask
) - addr
+ 1);
396 if (!(iotlb
.perm
& (1 << is_write
))) {
397 mr
= &io_mem_unassigned
;
401 as
= iotlb
.target_as
;
404 if (xen_enabled() && memory_access_is_direct(mr
, is_write
)) {
405 hwaddr page
= ((addr
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
) - addr
;
406 len
= MIN(page
, len
);
415 /* Called from RCU critical section */
416 MemoryRegionSection
*
417 address_space_translate_for_iotlb(CPUState
*cpu
, hwaddr addr
,
418 hwaddr
*xlat
, hwaddr
*plen
)
420 MemoryRegionSection
*section
;
421 section
= address_space_translate_internal(cpu
->memory_dispatch
,
422 addr
, xlat
, plen
, false);
424 assert(!section
->mr
->iommu_ops
);
429 void cpu_exec_init_all(void)
431 #if !defined(CONFIG_USER_ONLY)
432 qemu_mutex_init(&ram_list
.mutex
);
438 #if !defined(CONFIG_USER_ONLY)
440 static int cpu_common_post_load(void *opaque
, int version_id
)
442 CPUState
*cpu
= opaque
;
444 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
445 version_id is increased. */
446 cpu
->interrupt_request
&= ~0x01;
452 static int cpu_common_pre_load(void *opaque
)
454 CPUState
*cpu
= opaque
;
456 cpu
->exception_index
= -1;
461 static bool cpu_common_exception_index_needed(void *opaque
)
463 CPUState
*cpu
= opaque
;
465 return tcg_enabled() && cpu
->exception_index
!= -1;
468 static const VMStateDescription vmstate_cpu_common_exception_index
= {
469 .name
= "cpu_common/exception_index",
471 .minimum_version_id
= 1,
472 .fields
= (VMStateField
[]) {
473 VMSTATE_INT32(exception_index
, CPUState
),
474 VMSTATE_END_OF_LIST()
478 const VMStateDescription vmstate_cpu_common
= {
479 .name
= "cpu_common",
481 .minimum_version_id
= 1,
482 .pre_load
= cpu_common_pre_load
,
483 .post_load
= cpu_common_post_load
,
484 .fields
= (VMStateField
[]) {
485 VMSTATE_UINT32(halted
, CPUState
),
486 VMSTATE_UINT32(interrupt_request
, CPUState
),
487 VMSTATE_END_OF_LIST()
489 .subsections
= (VMStateSubsection
[]) {
491 .vmsd
= &vmstate_cpu_common_exception_index
,
492 .needed
= cpu_common_exception_index_needed
,
501 CPUState
*qemu_get_cpu(int index
)
506 if (cpu
->cpu_index
== index
) {
514 #if !defined(CONFIG_USER_ONLY)
515 void tcg_cpu_address_space_init(CPUState
*cpu
, AddressSpace
*as
)
517 /* We only support one address space per cpu at the moment. */
518 assert(cpu
->as
== as
);
520 if (cpu
->tcg_as_listener
) {
521 memory_listener_unregister(cpu
->tcg_as_listener
);
523 cpu
->tcg_as_listener
= g_new0(MemoryListener
, 1);
525 cpu
->tcg_as_listener
->commit
= tcg_commit
;
526 memory_listener_register(cpu
->tcg_as_listener
, as
);
530 void cpu_exec_init(CPUArchState
*env
)
532 CPUState
*cpu
= ENV_GET_CPU(env
);
533 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
537 #if defined(CONFIG_USER_ONLY)
541 CPU_FOREACH(some_cpu
) {
544 cpu
->cpu_index
= cpu_index
;
546 QTAILQ_INIT(&cpu
->breakpoints
);
547 QTAILQ_INIT(&cpu
->watchpoints
);
548 #ifndef CONFIG_USER_ONLY
549 cpu
->as
= &address_space_memory
;
550 cpu
->thread_id
= qemu_get_thread_id();
552 QTAILQ_INSERT_TAIL(&cpus
, cpu
, node
);
553 #if defined(CONFIG_USER_ONLY)
556 if (qdev_get_vmsd(DEVICE(cpu
)) == NULL
) {
557 vmstate_register(NULL
, cpu_index
, &vmstate_cpu_common
, cpu
);
559 #if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
560 register_savevm(NULL
, "cpu", cpu_index
, CPU_SAVE_VERSION
,
561 cpu_save
, cpu_load
, env
);
562 assert(cc
->vmsd
== NULL
);
563 assert(qdev_get_vmsd(DEVICE(cpu
)) == NULL
);
565 if (cc
->vmsd
!= NULL
) {
566 vmstate_register(NULL
, cpu_index
, cc
->vmsd
, cpu
);
570 #if defined(CONFIG_USER_ONLY)
571 static void breakpoint_invalidate(CPUState
*cpu
, target_ulong pc
)
573 tb_invalidate_phys_page_range(pc
, pc
+ 1, 0);
576 static void breakpoint_invalidate(CPUState
*cpu
, target_ulong pc
)
578 hwaddr phys
= cpu_get_phys_page_debug(cpu
, pc
);
580 tb_invalidate_phys_addr(cpu
->as
,
581 phys
| (pc
& ~TARGET_PAGE_MASK
));
586 #if defined(CONFIG_USER_ONLY)
587 void cpu_watchpoint_remove_all(CPUState
*cpu
, int mask
)
592 int cpu_watchpoint_remove(CPUState
*cpu
, vaddr addr
, vaddr len
,
598 void cpu_watchpoint_remove_by_ref(CPUState
*cpu
, CPUWatchpoint
*watchpoint
)
602 int cpu_watchpoint_insert(CPUState
*cpu
, vaddr addr
, vaddr len
,
603 int flags
, CPUWatchpoint
**watchpoint
)
608 /* Add a watchpoint. */
609 int cpu_watchpoint_insert(CPUState
*cpu
, vaddr addr
, vaddr len
,
610 int flags
, CPUWatchpoint
**watchpoint
)
614 /* forbid ranges which are empty or run off the end of the address space */
615 if (len
== 0 || (addr
+ len
- 1) < addr
) {
616 error_report("tried to set invalid watchpoint at %"
617 VADDR_PRIx
", len=%" VADDR_PRIu
, addr
, len
);
620 wp
= g_malloc(sizeof(*wp
));
626 /* keep all GDB-injected watchpoints in front */
627 if (flags
& BP_GDB
) {
628 QTAILQ_INSERT_HEAD(&cpu
->watchpoints
, wp
, entry
);
630 QTAILQ_INSERT_TAIL(&cpu
->watchpoints
, wp
, entry
);
633 tlb_flush_page(cpu
, addr
);
640 /* Remove a specific watchpoint. */
641 int cpu_watchpoint_remove(CPUState
*cpu
, vaddr addr
, vaddr len
,
646 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
647 if (addr
== wp
->vaddr
&& len
== wp
->len
648 && flags
== (wp
->flags
& ~BP_WATCHPOINT_HIT
)) {
649 cpu_watchpoint_remove_by_ref(cpu
, wp
);
656 /* Remove a specific watchpoint by reference. */
657 void cpu_watchpoint_remove_by_ref(CPUState
*cpu
, CPUWatchpoint
*watchpoint
)
659 QTAILQ_REMOVE(&cpu
->watchpoints
, watchpoint
, entry
);
661 tlb_flush_page(cpu
, watchpoint
->vaddr
);
666 /* Remove all matching watchpoints. */
667 void cpu_watchpoint_remove_all(CPUState
*cpu
, int mask
)
669 CPUWatchpoint
*wp
, *next
;
671 QTAILQ_FOREACH_SAFE(wp
, &cpu
->watchpoints
, entry
, next
) {
672 if (wp
->flags
& mask
) {
673 cpu_watchpoint_remove_by_ref(cpu
, wp
);
678 /* Return true if this watchpoint address matches the specified
679 * access (ie the address range covered by the watchpoint overlaps
680 * partially or completely with the address range covered by the
683 static inline bool cpu_watchpoint_address_matches(CPUWatchpoint
*wp
,
687 /* We know the lengths are non-zero, but a little caution is
688 * required to avoid errors in the case where the range ends
689 * exactly at the top of the address space and so addr + len
690 * wraps round to zero.
692 vaddr wpend
= wp
->vaddr
+ wp
->len
- 1;
693 vaddr addrend
= addr
+ len
- 1;
695 return !(addr
> wpend
|| wp
->vaddr
> addrend
);
700 /* Add a breakpoint. */
701 int cpu_breakpoint_insert(CPUState
*cpu
, vaddr pc
, int flags
,
702 CPUBreakpoint
**breakpoint
)
706 bp
= g_malloc(sizeof(*bp
));
711 /* keep all GDB-injected breakpoints in front */
712 if (flags
& BP_GDB
) {
713 QTAILQ_INSERT_HEAD(&cpu
->breakpoints
, bp
, entry
);
715 QTAILQ_INSERT_TAIL(&cpu
->breakpoints
, bp
, entry
);
718 breakpoint_invalidate(cpu
, pc
);
726 /* Remove a specific breakpoint. */
727 int cpu_breakpoint_remove(CPUState
*cpu
, vaddr pc
, int flags
)
731 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
732 if (bp
->pc
== pc
&& bp
->flags
== flags
) {
733 cpu_breakpoint_remove_by_ref(cpu
, bp
);
740 /* Remove a specific breakpoint by reference. */
741 void cpu_breakpoint_remove_by_ref(CPUState
*cpu
, CPUBreakpoint
*breakpoint
)
743 QTAILQ_REMOVE(&cpu
->breakpoints
, breakpoint
, entry
);
745 breakpoint_invalidate(cpu
, breakpoint
->pc
);
750 /* Remove all matching breakpoints. */
751 void cpu_breakpoint_remove_all(CPUState
*cpu
, int mask
)
753 CPUBreakpoint
*bp
, *next
;
755 QTAILQ_FOREACH_SAFE(bp
, &cpu
->breakpoints
, entry
, next
) {
756 if (bp
->flags
& mask
) {
757 cpu_breakpoint_remove_by_ref(cpu
, bp
);
762 /* enable or disable single step mode. EXCP_DEBUG is returned by the
763 CPU loop after each instruction */
764 void cpu_single_step(CPUState
*cpu
, int enabled
)
766 if (cpu
->singlestep_enabled
!= enabled
) {
767 cpu
->singlestep_enabled
= enabled
;
769 kvm_update_guest_debug(cpu
, 0);
771 /* must flush all the translated code to avoid inconsistencies */
772 /* XXX: only flush what is necessary */
773 CPUArchState
*env
= cpu
->env_ptr
;
779 void cpu_abort(CPUState
*cpu
, const char *fmt
, ...)
786 fprintf(stderr
, "qemu: fatal: ");
787 vfprintf(stderr
, fmt
, ap
);
788 fprintf(stderr
, "\n");
789 cpu_dump_state(cpu
, stderr
, fprintf
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
790 if (qemu_log_enabled()) {
791 qemu_log("qemu: fatal: ");
792 qemu_log_vprintf(fmt
, ap2
);
794 log_cpu_state(cpu
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
800 #if defined(CONFIG_USER_ONLY)
802 struct sigaction act
;
803 sigfillset(&act
.sa_mask
);
804 act
.sa_handler
= SIG_DFL
;
805 sigaction(SIGABRT
, &act
, NULL
);
811 #if !defined(CONFIG_USER_ONLY)
812 /* Called from RCU critical section */
813 static RAMBlock
*qemu_get_ram_block(ram_addr_t addr
)
817 block
= atomic_rcu_read(&ram_list
.mru_block
);
818 if (block
&& addr
- block
->offset
< block
->max_length
) {
821 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
822 if (addr
- block
->offset
< block
->max_length
) {
827 fprintf(stderr
, "Bad ram offset %" PRIx64
"\n", (uint64_t)addr
);
831 /* It is safe to write mru_block outside the iothread lock. This
836 * xxx removed from list
840 * call_rcu(reclaim_ramblock, xxx);
843 * atomic_rcu_set is not needed here. The block was already published
844 * when it was placed into the list. Here we're just making an extra
845 * copy of the pointer.
847 ram_list
.mru_block
= block
;
851 static void tlb_reset_dirty_range_all(ram_addr_t start
, ram_addr_t length
)
857 end
= TARGET_PAGE_ALIGN(start
+ length
);
858 start
&= TARGET_PAGE_MASK
;
861 block
= qemu_get_ram_block(start
);
862 assert(block
== qemu_get_ram_block(end
- 1));
863 start1
= (uintptr_t)ramblock_ptr(block
, start
- block
->offset
);
864 cpu_tlb_reset_dirty_all(start1
, length
);
868 /* Note: start and end must be within the same ram block. */
869 void cpu_physical_memory_reset_dirty(ram_addr_t start
, ram_addr_t length
,
874 cpu_physical_memory_clear_dirty_range_type(start
, length
, client
);
877 tlb_reset_dirty_range_all(start
, length
);
881 static void cpu_physical_memory_set_dirty_tracking(bool enable
)
883 in_migration
= enable
;
886 /* Called from RCU critical section */
887 hwaddr
memory_region_section_get_iotlb(CPUState
*cpu
,
888 MemoryRegionSection
*section
,
890 hwaddr paddr
, hwaddr xlat
,
892 target_ulong
*address
)
897 if (memory_region_is_ram(section
->mr
)) {
899 iotlb
= (memory_region_get_ram_addr(section
->mr
) & TARGET_PAGE_MASK
)
901 if (!section
->readonly
) {
902 iotlb
|= PHYS_SECTION_NOTDIRTY
;
904 iotlb
|= PHYS_SECTION_ROM
;
907 iotlb
= section
- section
->address_space
->dispatch
->map
.sections
;
911 /* Make accesses to pages with watchpoints go via the
912 watchpoint trap routines. */
913 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
914 if (cpu_watchpoint_address_matches(wp
, vaddr
, TARGET_PAGE_SIZE
)) {
915 /* Avoid trapping reads of pages with a write breakpoint. */
916 if ((prot
& PAGE_WRITE
) || (wp
->flags
& BP_MEM_READ
)) {
917 iotlb
= PHYS_SECTION_WATCH
+ paddr
;
918 *address
|= TLB_MMIO
;
926 #endif /* defined(CONFIG_USER_ONLY) */
928 #if !defined(CONFIG_USER_ONLY)
930 static int subpage_register (subpage_t
*mmio
, uint32_t start
, uint32_t end
,
932 static subpage_t
*subpage_init(AddressSpace
*as
, hwaddr base
);
934 static void *(*phys_mem_alloc
)(size_t size
, uint64_t *align
) =
938 * Set a custom physical guest memory alloator.
939 * Accelerators with unusual needs may need this. Hopefully, we can
940 * get rid of it eventually.
942 void phys_mem_set_alloc(void *(*alloc
)(size_t, uint64_t *align
))
944 phys_mem_alloc
= alloc
;
947 static uint16_t phys_section_add(PhysPageMap
*map
,
948 MemoryRegionSection
*section
)
950 /* The physical section number is ORed with a page-aligned
951 * pointer to produce the iotlb entries. Thus it should
952 * never overflow into the page-aligned value.
954 assert(map
->sections_nb
< TARGET_PAGE_SIZE
);
956 if (map
->sections_nb
== map
->sections_nb_alloc
) {
957 map
->sections_nb_alloc
= MAX(map
->sections_nb_alloc
* 2, 16);
958 map
->sections
= g_renew(MemoryRegionSection
, map
->sections
,
959 map
->sections_nb_alloc
);
961 map
->sections
[map
->sections_nb
] = *section
;
962 memory_region_ref(section
->mr
);
963 return map
->sections_nb
++;
966 static void phys_section_destroy(MemoryRegion
*mr
)
968 memory_region_unref(mr
);
971 subpage_t
*subpage
= container_of(mr
, subpage_t
, iomem
);
972 object_unref(OBJECT(&subpage
->iomem
));
977 static void phys_sections_free(PhysPageMap
*map
)
979 while (map
->sections_nb
> 0) {
980 MemoryRegionSection
*section
= &map
->sections
[--map
->sections_nb
];
981 phys_section_destroy(section
->mr
);
983 g_free(map
->sections
);
987 static void register_subpage(AddressSpaceDispatch
*d
, MemoryRegionSection
*section
)
990 hwaddr base
= section
->offset_within_address_space
992 MemoryRegionSection
*existing
= phys_page_find(d
->phys_map
, base
,
993 d
->map
.nodes
, d
->map
.sections
);
994 MemoryRegionSection subsection
= {
995 .offset_within_address_space
= base
,
996 .size
= int128_make64(TARGET_PAGE_SIZE
),
1000 assert(existing
->mr
->subpage
|| existing
->mr
== &io_mem_unassigned
);
1002 if (!(existing
->mr
->subpage
)) {
1003 subpage
= subpage_init(d
->as
, base
);
1004 subsection
.address_space
= d
->as
;
1005 subsection
.mr
= &subpage
->iomem
;
1006 phys_page_set(d
, base
>> TARGET_PAGE_BITS
, 1,
1007 phys_section_add(&d
->map
, &subsection
));
1009 subpage
= container_of(existing
->mr
, subpage_t
, iomem
);
1011 start
= section
->offset_within_address_space
& ~TARGET_PAGE_MASK
;
1012 end
= start
+ int128_get64(section
->size
) - 1;
1013 subpage_register(subpage
, start
, end
,
1014 phys_section_add(&d
->map
, section
));
1018 static void register_multipage(AddressSpaceDispatch
*d
,
1019 MemoryRegionSection
*section
)
1021 hwaddr start_addr
= section
->offset_within_address_space
;
1022 uint16_t section_index
= phys_section_add(&d
->map
, section
);
1023 uint64_t num_pages
= int128_get64(int128_rshift(section
->size
,
1027 phys_page_set(d
, start_addr
>> TARGET_PAGE_BITS
, num_pages
, section_index
);
1030 static void mem_add(MemoryListener
*listener
, MemoryRegionSection
*section
)
1032 AddressSpace
*as
= container_of(listener
, AddressSpace
, dispatch_listener
);
1033 AddressSpaceDispatch
*d
= as
->next_dispatch
;
1034 MemoryRegionSection now
= *section
, remain
= *section
;
1035 Int128 page_size
= int128_make64(TARGET_PAGE_SIZE
);
1037 if (now
.offset_within_address_space
& ~TARGET_PAGE_MASK
) {
1038 uint64_t left
= TARGET_PAGE_ALIGN(now
.offset_within_address_space
)
1039 - now
.offset_within_address_space
;
1041 now
.size
= int128_min(int128_make64(left
), now
.size
);
1042 register_subpage(d
, &now
);
1044 now
.size
= int128_zero();
1046 while (int128_ne(remain
.size
, now
.size
)) {
1047 remain
.size
= int128_sub(remain
.size
, now
.size
);
1048 remain
.offset_within_address_space
+= int128_get64(now
.size
);
1049 remain
.offset_within_region
+= int128_get64(now
.size
);
1051 if (int128_lt(remain
.size
, page_size
)) {
1052 register_subpage(d
, &now
);
1053 } else if (remain
.offset_within_address_space
& ~TARGET_PAGE_MASK
) {
1054 now
.size
= page_size
;
1055 register_subpage(d
, &now
);
1057 now
.size
= int128_and(now
.size
, int128_neg(page_size
));
1058 register_multipage(d
, &now
);
1063 void qemu_flush_coalesced_mmio_buffer(void)
1066 kvm_flush_coalesced_mmio_buffer();
1069 void qemu_mutex_lock_ramlist(void)
1071 qemu_mutex_lock(&ram_list
.mutex
);
1074 void qemu_mutex_unlock_ramlist(void)
1076 qemu_mutex_unlock(&ram_list
.mutex
);
1081 #include <sys/vfs.h>
1083 #define HUGETLBFS_MAGIC 0x958458f6
1085 static long gethugepagesize(const char *path
, Error
**errp
)
1091 ret
= statfs(path
, &fs
);
1092 } while (ret
!= 0 && errno
== EINTR
);
1095 error_setg_errno(errp
, errno
, "failed to get page size of file %s",
1100 if (fs
.f_type
!= HUGETLBFS_MAGIC
)
1101 fprintf(stderr
, "Warning: path not on HugeTLBFS: %s\n", path
);
1106 static void *file_ram_alloc(RAMBlock
*block
,
1112 char *sanitized_name
;
1117 Error
*local_err
= NULL
;
1119 hpagesize
= gethugepagesize(path
, &local_err
);
1121 error_propagate(errp
, local_err
);
1124 block
->mr
->align
= hpagesize
;
1126 if (memory
< hpagesize
) {
1127 error_setg(errp
, "memory size 0x" RAM_ADDR_FMT
" must be equal to "
1128 "or larger than huge page size 0x%" PRIx64
,
1133 if (kvm_enabled() && !kvm_has_sync_mmu()) {
1135 "host lacks kvm mmu notifiers, -mem-path unsupported");
1139 /* Make name safe to use with mkstemp by replacing '/' with '_'. */
1140 sanitized_name
= g_strdup(memory_region_name(block
->mr
));
1141 for (c
= sanitized_name
; *c
!= '\0'; c
++) {
1146 filename
= g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path
,
1148 g_free(sanitized_name
);
1150 fd
= mkstemp(filename
);
1152 error_setg_errno(errp
, errno
,
1153 "unable to create backing store for hugepages");
1160 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
1163 * ftruncate is not supported by hugetlbfs in older
1164 * hosts, so don't bother bailing out on errors.
1165 * If anything goes wrong with it under other filesystems,
1168 if (ftruncate(fd
, memory
)) {
1169 perror("ftruncate");
1172 area
= mmap(0, memory
, PROT_READ
| PROT_WRITE
,
1173 (block
->flags
& RAM_SHARED
? MAP_SHARED
: MAP_PRIVATE
),
1175 if (area
== MAP_FAILED
) {
1176 error_setg_errno(errp
, errno
,
1177 "unable to map backing store for hugepages");
1183 os_mem_prealloc(fd
, area
, memory
);
1191 error_report("%s\n", error_get_pretty(*errp
));
1198 /* Called with the ramlist lock held. */
1199 static ram_addr_t
find_ram_offset(ram_addr_t size
)
1201 RAMBlock
*block
, *next_block
;
1202 ram_addr_t offset
= RAM_ADDR_MAX
, mingap
= RAM_ADDR_MAX
;
1204 assert(size
!= 0); /* it would hand out same offset multiple times */
1206 if (QLIST_EMPTY_RCU(&ram_list
.blocks
)) {
1210 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1211 ram_addr_t end
, next
= RAM_ADDR_MAX
;
1213 end
= block
->offset
+ block
->max_length
;
1215 QLIST_FOREACH_RCU(next_block
, &ram_list
.blocks
, next
) {
1216 if (next_block
->offset
>= end
) {
1217 next
= MIN(next
, next_block
->offset
);
1220 if (next
- end
>= size
&& next
- end
< mingap
) {
1222 mingap
= next
- end
;
1226 if (offset
== RAM_ADDR_MAX
) {
1227 fprintf(stderr
, "Failed to find gap of requested size: %" PRIu64
"\n",
1235 ram_addr_t
last_ram_offset(void)
1238 ram_addr_t last
= 0;
1241 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1242 last
= MAX(last
, block
->offset
+ block
->max_length
);
1248 static void qemu_ram_setup_dump(void *addr
, ram_addr_t size
)
1252 /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
1253 if (!qemu_opt_get_bool(qemu_get_machine_opts(),
1254 "dump-guest-core", true)) {
1255 ret
= qemu_madvise(addr
, size
, QEMU_MADV_DONTDUMP
);
1257 perror("qemu_madvise");
1258 fprintf(stderr
, "madvise doesn't support MADV_DONTDUMP, "
1259 "but dump_guest_core=off specified\n");
1264 /* Called within an RCU critical section, or while the ramlist lock
1267 static RAMBlock
*find_ram_block(ram_addr_t addr
)
1271 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1272 if (block
->offset
== addr
) {
1280 /* Called with iothread lock held. */
1281 void qemu_ram_set_idstr(ram_addr_t addr
, const char *name
, DeviceState
*dev
)
1283 RAMBlock
*new_block
, *block
;
1286 new_block
= find_ram_block(addr
);
1288 assert(!new_block
->idstr
[0]);
1291 char *id
= qdev_get_dev_path(dev
);
1293 snprintf(new_block
->idstr
, sizeof(new_block
->idstr
), "%s/", id
);
1297 pstrcat(new_block
->idstr
, sizeof(new_block
->idstr
), name
);
1299 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1300 if (block
!= new_block
&& !strcmp(block
->idstr
, new_block
->idstr
)) {
1301 fprintf(stderr
, "RAMBlock \"%s\" already registered, abort!\n",
1309 /* Called with iothread lock held. */
1310 void qemu_ram_unset_idstr(ram_addr_t addr
)
1314 /* FIXME: arch_init.c assumes that this is not called throughout
1315 * migration. Ignore the problem since hot-unplug during migration
1316 * does not work anyway.
1320 block
= find_ram_block(addr
);
1322 memset(block
->idstr
, 0, sizeof(block
->idstr
));
1327 static int memory_try_enable_merging(void *addr
, size_t len
)
1329 if (!qemu_opt_get_bool(qemu_get_machine_opts(), "mem-merge", true)) {
1330 /* disabled by the user */
1334 return qemu_madvise(addr
, len
, QEMU_MADV_MERGEABLE
);
1337 /* Only legal before guest might have detected the memory size: e.g. on
1338 * incoming migration, or right after reset.
1340 * As memory core doesn't know how is memory accessed, it is up to
1341 * resize callback to update device state and/or add assertions to detect
1342 * misuse, if necessary.
1344 int qemu_ram_resize(ram_addr_t base
, ram_addr_t newsize
, Error
**errp
)
1346 RAMBlock
*block
= find_ram_block(base
);
1350 if (block
->used_length
== newsize
) {
1354 if (!(block
->flags
& RAM_RESIZEABLE
)) {
1355 error_setg_errno(errp
, EINVAL
,
1356 "Length mismatch: %s: 0x" RAM_ADDR_FMT
1357 " in != 0x" RAM_ADDR_FMT
, block
->idstr
,
1358 newsize
, block
->used_length
);
1362 if (block
->max_length
< newsize
) {
1363 error_setg_errno(errp
, EINVAL
,
1364 "Length too large: %s: 0x" RAM_ADDR_FMT
1365 " > 0x" RAM_ADDR_FMT
, block
->idstr
,
1366 newsize
, block
->max_length
);
1370 cpu_physical_memory_clear_dirty_range(block
->offset
, block
->used_length
);
1371 block
->used_length
= newsize
;
1372 cpu_physical_memory_set_dirty_range(block
->offset
, block
->used_length
);
1373 memory_region_set_size(block
->mr
, newsize
);
1374 if (block
->resized
) {
1375 block
->resized(block
->idstr
, newsize
, block
->host
);
1380 static ram_addr_t
ram_block_add(RAMBlock
*new_block
, Error
**errp
)
1383 RAMBlock
*last_block
= NULL
;
1384 ram_addr_t old_ram_size
, new_ram_size
;
1386 old_ram_size
= last_ram_offset() >> TARGET_PAGE_BITS
;
1388 qemu_mutex_lock_ramlist();
1389 new_block
->offset
= find_ram_offset(new_block
->max_length
);
1391 if (!new_block
->host
) {
1392 if (xen_enabled()) {
1393 xen_ram_alloc(new_block
->offset
, new_block
->max_length
,
1396 new_block
->host
= phys_mem_alloc(new_block
->max_length
,
1397 &new_block
->mr
->align
);
1398 if (!new_block
->host
) {
1399 error_setg_errno(errp
, errno
,
1400 "cannot set up guest memory '%s'",
1401 memory_region_name(new_block
->mr
));
1402 qemu_mutex_unlock_ramlist();
1405 memory_try_enable_merging(new_block
->host
, new_block
->max_length
);
1409 /* Keep the list sorted from biggest to smallest block. Unlike QTAILQ,
1410 * QLIST (which has an RCU-friendly variant) does not have insertion at
1411 * tail, so save the last element in last_block.
1413 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1415 if (block
->max_length
< new_block
->max_length
) {
1420 QLIST_INSERT_BEFORE_RCU(block
, new_block
, next
);
1421 } else if (last_block
) {
1422 QLIST_INSERT_AFTER_RCU(last_block
, new_block
, next
);
1423 } else { /* list is empty */
1424 QLIST_INSERT_HEAD_RCU(&ram_list
.blocks
, new_block
, next
);
1426 ram_list
.mru_block
= NULL
;
1428 /* Write list before version */
1431 qemu_mutex_unlock_ramlist();
1433 new_ram_size
= last_ram_offset() >> TARGET_PAGE_BITS
;
1435 if (new_ram_size
> old_ram_size
) {
1438 /* ram_list.dirty_memory[] is protected by the iothread lock. */
1439 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
1440 ram_list
.dirty_memory
[i
] =
1441 bitmap_zero_extend(ram_list
.dirty_memory
[i
],
1442 old_ram_size
, new_ram_size
);
1445 cpu_physical_memory_set_dirty_range(new_block
->offset
,
1446 new_block
->used_length
);
1448 if (new_block
->host
) {
1449 qemu_ram_setup_dump(new_block
->host
, new_block
->max_length
);
1450 qemu_madvise(new_block
->host
, new_block
->max_length
, QEMU_MADV_HUGEPAGE
);
1451 qemu_madvise(new_block
->host
, new_block
->max_length
, QEMU_MADV_DONTFORK
);
1452 if (kvm_enabled()) {
1453 kvm_setup_guest_memory(new_block
->host
, new_block
->max_length
);
1457 return new_block
->offset
;
1461 ram_addr_t
qemu_ram_alloc_from_file(ram_addr_t size
, MemoryRegion
*mr
,
1462 bool share
, const char *mem_path
,
1465 RAMBlock
*new_block
;
1467 Error
*local_err
= NULL
;
1469 if (xen_enabled()) {
1470 error_setg(errp
, "-mem-path not supported with Xen");
1474 if (phys_mem_alloc
!= qemu_anon_ram_alloc
) {
1476 * file_ram_alloc() needs to allocate just like
1477 * phys_mem_alloc, but we haven't bothered to provide
1481 "-mem-path not supported with this accelerator");
1485 size
= TARGET_PAGE_ALIGN(size
);
1486 new_block
= g_malloc0(sizeof(*new_block
));
1488 new_block
->used_length
= size
;
1489 new_block
->max_length
= size
;
1490 new_block
->flags
= share
? RAM_SHARED
: 0;
1491 new_block
->host
= file_ram_alloc(new_block
, size
,
1493 if (!new_block
->host
) {
1498 addr
= ram_block_add(new_block
, &local_err
);
1501 error_propagate(errp
, local_err
);
1509 ram_addr_t
qemu_ram_alloc_internal(ram_addr_t size
, ram_addr_t max_size
,
1510 void (*resized
)(const char*,
1513 void *host
, bool resizeable
,
1514 MemoryRegion
*mr
, Error
**errp
)
1516 RAMBlock
*new_block
;
1518 Error
*local_err
= NULL
;
1520 size
= TARGET_PAGE_ALIGN(size
);
1521 max_size
= TARGET_PAGE_ALIGN(max_size
);
1522 new_block
= g_malloc0(sizeof(*new_block
));
1524 new_block
->resized
= resized
;
1525 new_block
->used_length
= size
;
1526 new_block
->max_length
= max_size
;
1527 assert(max_size
>= size
);
1529 new_block
->host
= host
;
1531 new_block
->flags
|= RAM_PREALLOC
;
1534 new_block
->flags
|= RAM_RESIZEABLE
;
1536 addr
= ram_block_add(new_block
, &local_err
);
1539 error_propagate(errp
, local_err
);
1545 ram_addr_t
qemu_ram_alloc_from_ptr(ram_addr_t size
, void *host
,
1546 MemoryRegion
*mr
, Error
**errp
)
1548 return qemu_ram_alloc_internal(size
, size
, NULL
, host
, false, mr
, errp
);
1551 ram_addr_t
qemu_ram_alloc(ram_addr_t size
, MemoryRegion
*mr
, Error
**errp
)
1553 return qemu_ram_alloc_internal(size
, size
, NULL
, NULL
, false, mr
, errp
);
1556 ram_addr_t
qemu_ram_alloc_resizeable(ram_addr_t size
, ram_addr_t maxsz
,
1557 void (*resized
)(const char*,
1560 MemoryRegion
*mr
, Error
**errp
)
1562 return qemu_ram_alloc_internal(size
, maxsz
, resized
, NULL
, true, mr
, errp
);
1565 void qemu_ram_free_from_ptr(ram_addr_t addr
)
1569 qemu_mutex_lock_ramlist();
1570 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1571 if (addr
== block
->offset
) {
1572 QLIST_REMOVE_RCU(block
, next
);
1573 ram_list
.mru_block
= NULL
;
1574 /* Write list before version */
1577 g_free_rcu(block
, rcu
);
1581 qemu_mutex_unlock_ramlist();
1584 static void reclaim_ramblock(RAMBlock
*block
)
1586 if (block
->flags
& RAM_PREALLOC
) {
1588 } else if (xen_enabled()) {
1589 xen_invalidate_map_cache_entry(block
->host
);
1591 } else if (block
->fd
>= 0) {
1592 munmap(block
->host
, block
->max_length
);
1596 qemu_anon_ram_free(block
->host
, block
->max_length
);
1601 void qemu_ram_free(ram_addr_t addr
)
1605 qemu_mutex_lock_ramlist();
1606 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1607 if (addr
== block
->offset
) {
1608 QLIST_REMOVE_RCU(block
, next
);
1609 ram_list
.mru_block
= NULL
;
1610 /* Write list before version */
1613 call_rcu(block
, reclaim_ramblock
, rcu
);
1617 qemu_mutex_unlock_ramlist();
1621 void qemu_ram_remap(ram_addr_t addr
, ram_addr_t length
)
1628 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1629 offset
= addr
- block
->offset
;
1630 if (offset
< block
->max_length
) {
1631 vaddr
= ramblock_ptr(block
, offset
);
1632 if (block
->flags
& RAM_PREALLOC
) {
1634 } else if (xen_enabled()) {
1638 munmap(vaddr
, length
);
1639 if (block
->fd
>= 0) {
1640 flags
|= (block
->flags
& RAM_SHARED
?
1641 MAP_SHARED
: MAP_PRIVATE
);
1642 area
= mmap(vaddr
, length
, PROT_READ
| PROT_WRITE
,
1643 flags
, block
->fd
, offset
);
1646 * Remap needs to match alloc. Accelerators that
1647 * set phys_mem_alloc never remap. If they did,
1648 * we'd need a remap hook here.
1650 assert(phys_mem_alloc
== qemu_anon_ram_alloc
);
1652 flags
|= MAP_PRIVATE
| MAP_ANONYMOUS
;
1653 area
= mmap(vaddr
, length
, PROT_READ
| PROT_WRITE
,
1656 if (area
!= vaddr
) {
1657 fprintf(stderr
, "Could not remap addr: "
1658 RAM_ADDR_FMT
"@" RAM_ADDR_FMT
"\n",
1662 memory_try_enable_merging(vaddr
, length
);
1663 qemu_ram_setup_dump(vaddr
, length
);
1668 #endif /* !_WIN32 */
1670 int qemu_get_ram_fd(ram_addr_t addr
)
1676 block
= qemu_get_ram_block(addr
);
1682 void *qemu_get_ram_block_host_ptr(ram_addr_t addr
)
1688 block
= qemu_get_ram_block(addr
);
1689 ptr
= ramblock_ptr(block
, 0);
1694 /* Return a host pointer to ram allocated with qemu_ram_alloc.
1695 * This should not be used for general purpose DMA. Use address_space_map
1696 * or address_space_rw instead. For local memory (e.g. video ram) that the
1697 * device owns, use memory_region_get_ram_ptr.
1699 * By the time this function returns, the returned pointer is not protected
1700 * by RCU anymore. If the caller is not within an RCU critical section and
1701 * does not hold the iothread lock, it must have other means of protecting the
1702 * pointer, such as a reference to the region that includes the incoming
1705 void *qemu_get_ram_ptr(ram_addr_t addr
)
1711 block
= qemu_get_ram_block(addr
);
1713 if (xen_enabled() && block
->host
== NULL
) {
1714 /* We need to check if the requested address is in the RAM
1715 * because we don't want to map the entire memory in QEMU.
1716 * In that case just map until the end of the page.
1718 if (block
->offset
== 0) {
1719 ptr
= xen_map_cache(addr
, 0, 0);
1723 block
->host
= xen_map_cache(block
->offset
, block
->max_length
, 1);
1725 ptr
= ramblock_ptr(block
, addr
- block
->offset
);
1732 /* Return a host pointer to guest's ram. Similar to qemu_get_ram_ptr
1733 * but takes a size argument.
1735 * By the time this function returns, the returned pointer is not protected
1736 * by RCU anymore. If the caller is not within an RCU critical section and
1737 * does not hold the iothread lock, it must have other means of protecting the
1738 * pointer, such as a reference to the region that includes the incoming
1741 static void *qemu_ram_ptr_length(ram_addr_t addr
, hwaddr
*size
)
1747 if (xen_enabled()) {
1748 return xen_map_cache(addr
, *size
, 1);
1752 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1753 if (addr
- block
->offset
< block
->max_length
) {
1754 if (addr
- block
->offset
+ *size
> block
->max_length
)
1755 *size
= block
->max_length
- addr
+ block
->offset
;
1756 ptr
= ramblock_ptr(block
, addr
- block
->offset
);
1762 fprintf(stderr
, "Bad ram offset %" PRIx64
"\n", (uint64_t)addr
);
1767 /* Some of the softmmu routines need to translate from a host pointer
1768 * (typically a TLB entry) back to a ram offset.
1770 * By the time this function returns, the returned pointer is not protected
1771 * by RCU anymore. If the caller is not within an RCU critical section and
1772 * does not hold the iothread lock, it must have other means of protecting the
1773 * pointer, such as a reference to the region that includes the incoming
1776 MemoryRegion
*qemu_ram_addr_from_host(void *ptr
, ram_addr_t
*ram_addr
)
1779 uint8_t *host
= ptr
;
1782 if (xen_enabled()) {
1784 *ram_addr
= xen_ram_addr_from_mapcache(ptr
);
1785 mr
= qemu_get_ram_block(*ram_addr
)->mr
;
1791 block
= atomic_rcu_read(&ram_list
.mru_block
);
1792 if (block
&& block
->host
&& host
- block
->host
< block
->max_length
) {
1796 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
1797 /* This case append when the block is not mapped. */
1798 if (block
->host
== NULL
) {
1801 if (host
- block
->host
< block
->max_length
) {
1810 *ram_addr
= block
->offset
+ (host
- block
->host
);
1816 static void notdirty_mem_write(void *opaque
, hwaddr ram_addr
,
1817 uint64_t val
, unsigned size
)
1819 if (!cpu_physical_memory_get_dirty_flag(ram_addr
, DIRTY_MEMORY_CODE
)) {
1820 tb_invalidate_phys_page_fast(ram_addr
, size
);
1824 stb_p(qemu_get_ram_ptr(ram_addr
), val
);
1827 stw_p(qemu_get_ram_ptr(ram_addr
), val
);
1830 stl_p(qemu_get_ram_ptr(ram_addr
), val
);
1835 cpu_physical_memory_set_dirty_range_nocode(ram_addr
, size
);
1836 /* we remove the notdirty callback only if the code has been
1838 if (!cpu_physical_memory_is_clean(ram_addr
)) {
1839 CPUArchState
*env
= current_cpu
->env_ptr
;
1840 tlb_set_dirty(env
, current_cpu
->mem_io_vaddr
);
1844 static bool notdirty_mem_accepts(void *opaque
, hwaddr addr
,
1845 unsigned size
, bool is_write
)
1850 static const MemoryRegionOps notdirty_mem_ops
= {
1851 .write
= notdirty_mem_write
,
1852 .valid
.accepts
= notdirty_mem_accepts
,
1853 .endianness
= DEVICE_NATIVE_ENDIAN
,
1856 /* Generate a debug exception if a watchpoint has been hit. */
1857 static void check_watchpoint(int offset
, int len
, int flags
)
1859 CPUState
*cpu
= current_cpu
;
1860 CPUArchState
*env
= cpu
->env_ptr
;
1861 target_ulong pc
, cs_base
;
1866 if (cpu
->watchpoint_hit
) {
1867 /* We re-entered the check after replacing the TB. Now raise
1868 * the debug interrupt so that is will trigger after the
1869 * current instruction. */
1870 cpu_interrupt(cpu
, CPU_INTERRUPT_DEBUG
);
1873 vaddr
= (cpu
->mem_io_vaddr
& TARGET_PAGE_MASK
) + offset
;
1874 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
1875 if (cpu_watchpoint_address_matches(wp
, vaddr
, len
)
1876 && (wp
->flags
& flags
)) {
1877 if (flags
== BP_MEM_READ
) {
1878 wp
->flags
|= BP_WATCHPOINT_HIT_READ
;
1880 wp
->flags
|= BP_WATCHPOINT_HIT_WRITE
;
1882 wp
->hitaddr
= vaddr
;
1883 if (!cpu
->watchpoint_hit
) {
1884 cpu
->watchpoint_hit
= wp
;
1885 tb_check_watchpoint(cpu
);
1886 if (wp
->flags
& BP_STOP_BEFORE_ACCESS
) {
1887 cpu
->exception_index
= EXCP_DEBUG
;
1890 cpu_get_tb_cpu_state(env
, &pc
, &cs_base
, &cpu_flags
);
1891 tb_gen_code(cpu
, pc
, cs_base
, cpu_flags
, 1);
1892 cpu_resume_from_signal(cpu
, NULL
);
1896 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
1901 /* Watchpoint access routines. Watchpoints are inserted using TLB tricks,
1902 so these check for a hit then pass through to the normal out-of-line
1904 static uint64_t watch_mem_read(void *opaque
, hwaddr addr
,
1907 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, size
, BP_MEM_READ
);
1909 case 1: return ldub_phys(&address_space_memory
, addr
);
1910 case 2: return lduw_phys(&address_space_memory
, addr
);
1911 case 4: return ldl_phys(&address_space_memory
, addr
);
1916 static void watch_mem_write(void *opaque
, hwaddr addr
,
1917 uint64_t val
, unsigned size
)
1919 check_watchpoint(addr
& ~TARGET_PAGE_MASK
, size
, BP_MEM_WRITE
);
1922 stb_phys(&address_space_memory
, addr
, val
);
1925 stw_phys(&address_space_memory
, addr
, val
);
1928 stl_phys(&address_space_memory
, addr
, val
);
1934 static const MemoryRegionOps watch_mem_ops
= {
1935 .read
= watch_mem_read
,
1936 .write
= watch_mem_write
,
1937 .endianness
= DEVICE_NATIVE_ENDIAN
,
1940 static uint64_t subpage_read(void *opaque
, hwaddr addr
,
1943 subpage_t
*subpage
= opaque
;
1946 #if defined(DEBUG_SUBPAGE)
1947 printf("%s: subpage %p len %u addr " TARGET_FMT_plx
"\n", __func__
,
1948 subpage
, len
, addr
);
1950 address_space_read(subpage
->as
, addr
+ subpage
->base
, buf
, len
);
1965 static void subpage_write(void *opaque
, hwaddr addr
,
1966 uint64_t value
, unsigned len
)
1968 subpage_t
*subpage
= opaque
;
1971 #if defined(DEBUG_SUBPAGE)
1972 printf("%s: subpage %p len %u addr " TARGET_FMT_plx
1973 " value %"PRIx64
"\n",
1974 __func__
, subpage
, len
, addr
, value
);
1992 address_space_write(subpage
->as
, addr
+ subpage
->base
, buf
, len
);
1995 static bool subpage_accepts(void *opaque
, hwaddr addr
,
1996 unsigned len
, bool is_write
)
1998 subpage_t
*subpage
= opaque
;
1999 #if defined(DEBUG_SUBPAGE)
2000 printf("%s: subpage %p %c len %u addr " TARGET_FMT_plx
"\n",
2001 __func__
, subpage
, is_write
? 'w' : 'r', len
, addr
);
2004 return address_space_access_valid(subpage
->as
, addr
+ subpage
->base
,
2008 static const MemoryRegionOps subpage_ops
= {
2009 .read
= subpage_read
,
2010 .write
= subpage_write
,
2011 .impl
.min_access_size
= 1,
2012 .impl
.max_access_size
= 8,
2013 .valid
.min_access_size
= 1,
2014 .valid
.max_access_size
= 8,
2015 .valid
.accepts
= subpage_accepts
,
2016 .endianness
= DEVICE_NATIVE_ENDIAN
,
2019 static int subpage_register (subpage_t
*mmio
, uint32_t start
, uint32_t end
,
2024 if (start
>= TARGET_PAGE_SIZE
|| end
>= TARGET_PAGE_SIZE
)
2026 idx
= SUBPAGE_IDX(start
);
2027 eidx
= SUBPAGE_IDX(end
);
2028 #if defined(DEBUG_SUBPAGE)
2029 printf("%s: %p start %08x end %08x idx %08x eidx %08x section %d\n",
2030 __func__
, mmio
, start
, end
, idx
, eidx
, section
);
2032 for (; idx
<= eidx
; idx
++) {
2033 mmio
->sub_section
[idx
] = section
;
2039 static subpage_t
*subpage_init(AddressSpace
*as
, hwaddr base
)
2043 mmio
= g_malloc0(sizeof(subpage_t
));
2047 memory_region_init_io(&mmio
->iomem
, NULL
, &subpage_ops
, mmio
,
2048 NULL
, TARGET_PAGE_SIZE
);
2049 mmio
->iomem
.subpage
= true;
2050 #if defined(DEBUG_SUBPAGE)
2051 printf("%s: %p base " TARGET_FMT_plx
" len %08x\n", __func__
,
2052 mmio
, base
, TARGET_PAGE_SIZE
);
2054 subpage_register(mmio
, 0, TARGET_PAGE_SIZE
-1, PHYS_SECTION_UNASSIGNED
);
2059 static uint16_t dummy_section(PhysPageMap
*map
, AddressSpace
*as
,
2063 MemoryRegionSection section
= {
2064 .address_space
= as
,
2066 .offset_within_address_space
= 0,
2067 .offset_within_region
= 0,
2068 .size
= int128_2_64(),
2071 return phys_section_add(map
, §ion
);
2074 MemoryRegion
*iotlb_to_region(CPUState
*cpu
, hwaddr index
)
2076 AddressSpaceDispatch
*d
= atomic_rcu_read(&cpu
->memory_dispatch
);
2077 MemoryRegionSection
*sections
= d
->map
.sections
;
2079 return sections
[index
& ~TARGET_PAGE_MASK
].mr
;
2082 static void io_mem_init(void)
2084 memory_region_init_io(&io_mem_rom
, NULL
, &unassigned_mem_ops
, NULL
, NULL
, UINT64_MAX
);
2085 memory_region_init_io(&io_mem_unassigned
, NULL
, &unassigned_mem_ops
, NULL
,
2087 memory_region_init_io(&io_mem_notdirty
, NULL
, ¬dirty_mem_ops
, NULL
,
2089 memory_region_init_io(&io_mem_watch
, NULL
, &watch_mem_ops
, NULL
,
2093 static void mem_begin(MemoryListener
*listener
)
2095 AddressSpace
*as
= container_of(listener
, AddressSpace
, dispatch_listener
);
2096 AddressSpaceDispatch
*d
= g_new0(AddressSpaceDispatch
, 1);
2099 n
= dummy_section(&d
->map
, as
, &io_mem_unassigned
);
2100 assert(n
== PHYS_SECTION_UNASSIGNED
);
2101 n
= dummy_section(&d
->map
, as
, &io_mem_notdirty
);
2102 assert(n
== PHYS_SECTION_NOTDIRTY
);
2103 n
= dummy_section(&d
->map
, as
, &io_mem_rom
);
2104 assert(n
== PHYS_SECTION_ROM
);
2105 n
= dummy_section(&d
->map
, as
, &io_mem_watch
);
2106 assert(n
== PHYS_SECTION_WATCH
);
2108 d
->phys_map
= (PhysPageEntry
) { .ptr
= PHYS_MAP_NODE_NIL
, .skip
= 1 };
2110 as
->next_dispatch
= d
;
2113 static void address_space_dispatch_free(AddressSpaceDispatch
*d
)
2115 phys_sections_free(&d
->map
);
2119 static void mem_commit(MemoryListener
*listener
)
2121 AddressSpace
*as
= container_of(listener
, AddressSpace
, dispatch_listener
);
2122 AddressSpaceDispatch
*cur
= as
->dispatch
;
2123 AddressSpaceDispatch
*next
= as
->next_dispatch
;
2125 phys_page_compact_all(next
, next
->map
.nodes_nb
);
2127 atomic_rcu_set(&as
->dispatch
, next
);
2129 call_rcu(cur
, address_space_dispatch_free
, rcu
);
2133 static void tcg_commit(MemoryListener
*listener
)
2137 /* since each CPU stores ram addresses in its TLB cache, we must
2138 reset the modified entries */
2141 /* FIXME: Disentangle the cpu.h circular files deps so we can
2142 directly get the right CPU from listener. */
2143 if (cpu
->tcg_as_listener
!= listener
) {
2146 cpu_reload_memory_map(cpu
);
2150 static void core_log_global_start(MemoryListener
*listener
)
2152 cpu_physical_memory_set_dirty_tracking(true);
2155 static void core_log_global_stop(MemoryListener
*listener
)
2157 cpu_physical_memory_set_dirty_tracking(false);
2160 static MemoryListener core_memory_listener
= {
2161 .log_global_start
= core_log_global_start
,
2162 .log_global_stop
= core_log_global_stop
,
2166 void address_space_init_dispatch(AddressSpace
*as
)
2168 as
->dispatch
= NULL
;
2169 as
->dispatch_listener
= (MemoryListener
) {
2171 .commit
= mem_commit
,
2172 .region_add
= mem_add
,
2173 .region_nop
= mem_add
,
2176 memory_listener_register(&as
->dispatch_listener
, as
);
2179 void address_space_unregister(AddressSpace
*as
)
2181 memory_listener_unregister(&as
->dispatch_listener
);
2184 void address_space_destroy_dispatch(AddressSpace
*as
)
2186 AddressSpaceDispatch
*d
= as
->dispatch
;
2188 atomic_rcu_set(&as
->dispatch
, NULL
);
2190 call_rcu(d
, address_space_dispatch_free
, rcu
);
2194 static void memory_map_init(void)
2196 system_memory
= g_malloc(sizeof(*system_memory
));
2198 memory_region_init(system_memory
, NULL
, "system", UINT64_MAX
);
2199 address_space_init(&address_space_memory
, system_memory
, "memory");
2201 system_io
= g_malloc(sizeof(*system_io
));
2202 memory_region_init_io(system_io
, NULL
, &unassigned_io_ops
, NULL
, "io",
2204 address_space_init(&address_space_io
, system_io
, "I/O");
2206 memory_listener_register(&core_memory_listener
, &address_space_memory
);
2209 MemoryRegion
*get_system_memory(void)
2211 return system_memory
;
2214 MemoryRegion
*get_system_io(void)
2219 #endif /* !defined(CONFIG_USER_ONLY) */
2221 /* physical memory access (slow version, mainly for debug) */
2222 #if defined(CONFIG_USER_ONLY)
2223 int cpu_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
2224 uint8_t *buf
, int len
, int is_write
)
2231 page
= addr
& TARGET_PAGE_MASK
;
2232 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
2235 flags
= page_get_flags(page
);
2236 if (!(flags
& PAGE_VALID
))
2239 if (!(flags
& PAGE_WRITE
))
2241 /* XXX: this code should not depend on lock_user */
2242 if (!(p
= lock_user(VERIFY_WRITE
, addr
, l
, 0)))
2245 unlock_user(p
, addr
, l
);
2247 if (!(flags
& PAGE_READ
))
2249 /* XXX: this code should not depend on lock_user */
2250 if (!(p
= lock_user(VERIFY_READ
, addr
, l
, 1)))
2253 unlock_user(p
, addr
, 0);
2264 static void invalidate_and_set_dirty(hwaddr addr
,
2267 if (cpu_physical_memory_range_includes_clean(addr
, length
)) {
2268 tb_invalidate_phys_range(addr
, addr
+ length
, 0);
2269 cpu_physical_memory_set_dirty_range_nocode(addr
, length
);
2271 xen_modified_memory(addr
, length
);
2274 static int memory_access_size(MemoryRegion
*mr
, unsigned l
, hwaddr addr
)
2276 unsigned access_size_max
= mr
->ops
->valid
.max_access_size
;
2278 /* Regions are assumed to support 1-4 byte accesses unless
2279 otherwise specified. */
2280 if (access_size_max
== 0) {
2281 access_size_max
= 4;
2284 /* Bound the maximum access by the alignment of the address. */
2285 if (!mr
->ops
->impl
.unaligned
) {
2286 unsigned align_size_max
= addr
& -addr
;
2287 if (align_size_max
!= 0 && align_size_max
< access_size_max
) {
2288 access_size_max
= align_size_max
;
2292 /* Don't attempt accesses larger than the maximum. */
2293 if (l
> access_size_max
) {
2294 l
= access_size_max
;
2297 l
= 1 << (qemu_fls(l
) - 1);
2303 bool address_space_rw(AddressSpace
*as
, hwaddr addr
, uint8_t *buf
,
2304 int len
, bool is_write
)
2315 mr
= address_space_translate(as
, addr
, &addr1
, &l
, is_write
);
2318 if (!memory_access_is_direct(mr
, is_write
)) {
2319 l
= memory_access_size(mr
, l
, addr1
);
2320 /* XXX: could force current_cpu to NULL to avoid
2324 /* 64 bit write access */
2326 error
|= io_mem_write(mr
, addr1
, val
, 8);
2329 /* 32 bit write access */
2331 error
|= io_mem_write(mr
, addr1
, val
, 4);
2334 /* 16 bit write access */
2336 error
|= io_mem_write(mr
, addr1
, val
, 2);
2339 /* 8 bit write access */
2341 error
|= io_mem_write(mr
, addr1
, val
, 1);
2347 addr1
+= memory_region_get_ram_addr(mr
);
2349 ptr
= qemu_get_ram_ptr(addr1
);
2350 memcpy(ptr
, buf
, l
);
2351 invalidate_and_set_dirty(addr1
, l
);
2354 if (!memory_access_is_direct(mr
, is_write
)) {
2356 l
= memory_access_size(mr
, l
, addr1
);
2359 /* 64 bit read access */
2360 error
|= io_mem_read(mr
, addr1
, &val
, 8);
2364 /* 32 bit read access */
2365 error
|= io_mem_read(mr
, addr1
, &val
, 4);
2369 /* 16 bit read access */
2370 error
|= io_mem_read(mr
, addr1
, &val
, 2);
2374 /* 8 bit read access */
2375 error
|= io_mem_read(mr
, addr1
, &val
, 1);
2383 ptr
= qemu_get_ram_ptr(mr
->ram_addr
+ addr1
);
2384 memcpy(buf
, ptr
, l
);
2395 bool address_space_write(AddressSpace
*as
, hwaddr addr
,
2396 const uint8_t *buf
, int len
)
2398 return address_space_rw(as
, addr
, (uint8_t *)buf
, len
, true);
2401 bool address_space_read(AddressSpace
*as
, hwaddr addr
, uint8_t *buf
, int len
)
2403 return address_space_rw(as
, addr
, buf
, len
, false);
2407 void cpu_physical_memory_rw(hwaddr addr
, uint8_t *buf
,
2408 int len
, int is_write
)
2410 address_space_rw(&address_space_memory
, addr
, buf
, len
, is_write
);
2413 enum write_rom_type
{
2418 static inline void cpu_physical_memory_write_rom_internal(AddressSpace
*as
,
2419 hwaddr addr
, const uint8_t *buf
, int len
, enum write_rom_type type
)
2428 mr
= address_space_translate(as
, addr
, &addr1
, &l
, true);
2430 if (!(memory_region_is_ram(mr
) ||
2431 memory_region_is_romd(mr
))) {
2434 addr1
+= memory_region_get_ram_addr(mr
);
2436 ptr
= qemu_get_ram_ptr(addr1
);
2439 memcpy(ptr
, buf
, l
);
2440 invalidate_and_set_dirty(addr1
, l
);
2443 flush_icache_range((uintptr_t)ptr
, (uintptr_t)ptr
+ l
);
2453 /* used for ROM loading : can write in RAM and ROM */
2454 void cpu_physical_memory_write_rom(AddressSpace
*as
, hwaddr addr
,
2455 const uint8_t *buf
, int len
)
2457 cpu_physical_memory_write_rom_internal(as
, addr
, buf
, len
, WRITE_DATA
);
2460 void cpu_flush_icache_range(hwaddr start
, int len
)
2463 * This function should do the same thing as an icache flush that was
2464 * triggered from within the guest. For TCG we are always cache coherent,
2465 * so there is no need to flush anything. For KVM / Xen we need to flush
2466 * the host's instruction cache at least.
2468 if (tcg_enabled()) {
2472 cpu_physical_memory_write_rom_internal(&address_space_memory
,
2473 start
, NULL
, len
, FLUSH_CACHE
);
2483 static BounceBuffer bounce
;
2485 typedef struct MapClient
{
2487 void (*callback
)(void *opaque
);
2488 QLIST_ENTRY(MapClient
) link
;
2491 static QLIST_HEAD(map_client_list
, MapClient
) map_client_list
2492 = QLIST_HEAD_INITIALIZER(map_client_list
);
2494 void *cpu_register_map_client(void *opaque
, void (*callback
)(void *opaque
))
2496 MapClient
*client
= g_malloc(sizeof(*client
));
2498 client
->opaque
= opaque
;
2499 client
->callback
= callback
;
2500 QLIST_INSERT_HEAD(&map_client_list
, client
, link
);
2504 static void cpu_unregister_map_client(void *_client
)
2506 MapClient
*client
= (MapClient
*)_client
;
2508 QLIST_REMOVE(client
, link
);
2512 static void cpu_notify_map_clients(void)
2516 while (!QLIST_EMPTY(&map_client_list
)) {
2517 client
= QLIST_FIRST(&map_client_list
);
2518 client
->callback(client
->opaque
);
2519 cpu_unregister_map_client(client
);
2523 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
)
2530 mr
= address_space_translate(as
, addr
, &xlat
, &l
, is_write
);
2531 if (!memory_access_is_direct(mr
, is_write
)) {
2532 l
= memory_access_size(mr
, l
, addr
);
2533 if (!memory_region_access_valid(mr
, xlat
, l
, is_write
)) {
2544 /* Map a physical memory region into a host virtual address.
2545 * May map a subset of the requested range, given by and returned in *plen.
2546 * May return NULL if resources needed to perform the mapping are exhausted.
2547 * Use only for reads OR writes - not for read-modify-write operations.
2548 * Use cpu_register_map_client() to know when retrying the map operation is
2549 * likely to succeed.
2551 void *address_space_map(AddressSpace
*as
,
2558 hwaddr l
, xlat
, base
;
2559 MemoryRegion
*mr
, *this_mr
;
2567 mr
= address_space_translate(as
, addr
, &xlat
, &l
, is_write
);
2568 if (!memory_access_is_direct(mr
, is_write
)) {
2569 if (bounce
.buffer
) {
2572 /* Avoid unbounded allocations */
2573 l
= MIN(l
, TARGET_PAGE_SIZE
);
2574 bounce
.buffer
= qemu_memalign(TARGET_PAGE_SIZE
, l
);
2578 memory_region_ref(mr
);
2581 address_space_read(as
, addr
, bounce
.buffer
, l
);
2585 return bounce
.buffer
;
2589 raddr
= memory_region_get_ram_addr(mr
);
2600 this_mr
= address_space_translate(as
, addr
, &xlat
, &l
, is_write
);
2601 if (this_mr
!= mr
|| xlat
!= base
+ done
) {
2606 memory_region_ref(mr
);
2608 return qemu_ram_ptr_length(raddr
+ base
, plen
);
2611 /* Unmaps a memory region previously mapped by address_space_map().
2612 * Will also mark the memory as dirty if is_write == 1. access_len gives
2613 * the amount of memory that was actually read or written by the caller.
2615 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
2616 int is_write
, hwaddr access_len
)
2618 if (buffer
!= bounce
.buffer
) {
2622 mr
= qemu_ram_addr_from_host(buffer
, &addr1
);
2625 invalidate_and_set_dirty(addr1
, access_len
);
2627 if (xen_enabled()) {
2628 xen_invalidate_map_cache_entry(buffer
);
2630 memory_region_unref(mr
);
2634 address_space_write(as
, bounce
.addr
, bounce
.buffer
, access_len
);
2636 qemu_vfree(bounce
.buffer
);
2637 bounce
.buffer
= NULL
;
2638 memory_region_unref(bounce
.mr
);
2639 cpu_notify_map_clients();
2642 void *cpu_physical_memory_map(hwaddr addr
,
2646 return address_space_map(&address_space_memory
, addr
, plen
, is_write
);
2649 void cpu_physical_memory_unmap(void *buffer
, hwaddr len
,
2650 int is_write
, hwaddr access_len
)
2652 return address_space_unmap(&address_space_memory
, buffer
, len
, is_write
, access_len
);
2655 /* warning: addr must be aligned */
2656 static inline uint32_t ldl_phys_internal(AddressSpace
*as
, hwaddr addr
,
2657 enum device_endian endian
)
2665 mr
= address_space_translate(as
, addr
, &addr1
, &l
, false);
2666 if (l
< 4 || !memory_access_is_direct(mr
, false)) {
2668 io_mem_read(mr
, addr1
, &val
, 4);
2669 #if defined(TARGET_WORDS_BIGENDIAN)
2670 if (endian
== DEVICE_LITTLE_ENDIAN
) {
2674 if (endian
== DEVICE_BIG_ENDIAN
) {
2680 ptr
= qemu_get_ram_ptr((memory_region_get_ram_addr(mr
)
2684 case DEVICE_LITTLE_ENDIAN
:
2685 val
= ldl_le_p(ptr
);
2687 case DEVICE_BIG_ENDIAN
:
2688 val
= ldl_be_p(ptr
);
2698 uint32_t ldl_phys(AddressSpace
*as
, hwaddr addr
)
2700 return ldl_phys_internal(as
, addr
, DEVICE_NATIVE_ENDIAN
);
2703 uint32_t ldl_le_phys(AddressSpace
*as
, hwaddr addr
)
2705 return ldl_phys_internal(as
, addr
, DEVICE_LITTLE_ENDIAN
);
2708 uint32_t ldl_be_phys(AddressSpace
*as
, hwaddr addr
)
2710 return ldl_phys_internal(as
, addr
, DEVICE_BIG_ENDIAN
);
2713 /* warning: addr must be aligned */
2714 static inline uint64_t ldq_phys_internal(AddressSpace
*as
, hwaddr addr
,
2715 enum device_endian endian
)
2723 mr
= address_space_translate(as
, addr
, &addr1
, &l
,
2725 if (l
< 8 || !memory_access_is_direct(mr
, false)) {
2727 io_mem_read(mr
, addr1
, &val
, 8);
2728 #if defined(TARGET_WORDS_BIGENDIAN)
2729 if (endian
== DEVICE_LITTLE_ENDIAN
) {
2733 if (endian
== DEVICE_BIG_ENDIAN
) {
2739 ptr
= qemu_get_ram_ptr((memory_region_get_ram_addr(mr
)
2743 case DEVICE_LITTLE_ENDIAN
:
2744 val
= ldq_le_p(ptr
);
2746 case DEVICE_BIG_ENDIAN
:
2747 val
= ldq_be_p(ptr
);
2757 uint64_t ldq_phys(AddressSpace
*as
, hwaddr addr
)
2759 return ldq_phys_internal(as
, addr
, DEVICE_NATIVE_ENDIAN
);
2762 uint64_t ldq_le_phys(AddressSpace
*as
, hwaddr addr
)
2764 return ldq_phys_internal(as
, addr
, DEVICE_LITTLE_ENDIAN
);
2767 uint64_t ldq_be_phys(AddressSpace
*as
, hwaddr addr
)
2769 return ldq_phys_internal(as
, addr
, DEVICE_BIG_ENDIAN
);
2773 uint32_t ldub_phys(AddressSpace
*as
, hwaddr addr
)
2776 address_space_rw(as
, addr
, &val
, 1, 0);
2780 /* warning: addr must be aligned */
2781 static inline uint32_t lduw_phys_internal(AddressSpace
*as
, hwaddr addr
,
2782 enum device_endian endian
)
2790 mr
= address_space_translate(as
, addr
, &addr1
, &l
,
2792 if (l
< 2 || !memory_access_is_direct(mr
, false)) {
2794 io_mem_read(mr
, addr1
, &val
, 2);
2795 #if defined(TARGET_WORDS_BIGENDIAN)
2796 if (endian
== DEVICE_LITTLE_ENDIAN
) {
2800 if (endian
== DEVICE_BIG_ENDIAN
) {
2806 ptr
= qemu_get_ram_ptr((memory_region_get_ram_addr(mr
)
2810 case DEVICE_LITTLE_ENDIAN
:
2811 val
= lduw_le_p(ptr
);
2813 case DEVICE_BIG_ENDIAN
:
2814 val
= lduw_be_p(ptr
);
2824 uint32_t lduw_phys(AddressSpace
*as
, hwaddr addr
)
2826 return lduw_phys_internal(as
, addr
, DEVICE_NATIVE_ENDIAN
);
2829 uint32_t lduw_le_phys(AddressSpace
*as
, hwaddr addr
)
2831 return lduw_phys_internal(as
, addr
, DEVICE_LITTLE_ENDIAN
);
2834 uint32_t lduw_be_phys(AddressSpace
*as
, hwaddr addr
)
2836 return lduw_phys_internal(as
, addr
, DEVICE_BIG_ENDIAN
);
2839 /* warning: addr must be aligned. The ram page is not masked as dirty
2840 and the code inside is not invalidated. It is useful if the dirty
2841 bits are used to track modified PTEs */
2842 void stl_phys_notdirty(AddressSpace
*as
, hwaddr addr
, uint32_t val
)
2849 mr
= address_space_translate(as
, addr
, &addr1
, &l
,
2851 if (l
< 4 || !memory_access_is_direct(mr
, true)) {
2852 io_mem_write(mr
, addr1
, val
, 4);
2854 addr1
+= memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
;
2855 ptr
= qemu_get_ram_ptr(addr1
);
2858 if (unlikely(in_migration
)) {
2859 if (cpu_physical_memory_is_clean(addr1
)) {
2860 /* invalidate code */
2861 tb_invalidate_phys_page_range(addr1
, addr1
+ 4, 0);
2863 cpu_physical_memory_set_dirty_range_nocode(addr1
, 4);
2869 /* warning: addr must be aligned */
2870 static inline void stl_phys_internal(AddressSpace
*as
,
2871 hwaddr addr
, uint32_t val
,
2872 enum device_endian endian
)
2879 mr
= address_space_translate(as
, addr
, &addr1
, &l
,
2881 if (l
< 4 || !memory_access_is_direct(mr
, true)) {
2882 #if defined(TARGET_WORDS_BIGENDIAN)
2883 if (endian
== DEVICE_LITTLE_ENDIAN
) {
2887 if (endian
== DEVICE_BIG_ENDIAN
) {
2891 io_mem_write(mr
, addr1
, val
, 4);
2894 addr1
+= memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
;
2895 ptr
= qemu_get_ram_ptr(addr1
);
2897 case DEVICE_LITTLE_ENDIAN
:
2900 case DEVICE_BIG_ENDIAN
:
2907 invalidate_and_set_dirty(addr1
, 4);
2911 void stl_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
)
2913 stl_phys_internal(as
, addr
, val
, DEVICE_NATIVE_ENDIAN
);
2916 void stl_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
)
2918 stl_phys_internal(as
, addr
, val
, DEVICE_LITTLE_ENDIAN
);
2921 void stl_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
)
2923 stl_phys_internal(as
, addr
, val
, DEVICE_BIG_ENDIAN
);
2927 void stb_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
)
2930 address_space_rw(as
, addr
, &v
, 1, 1);
2933 /* warning: addr must be aligned */
2934 static inline void stw_phys_internal(AddressSpace
*as
,
2935 hwaddr addr
, uint32_t val
,
2936 enum device_endian endian
)
2943 mr
= address_space_translate(as
, addr
, &addr1
, &l
, true);
2944 if (l
< 2 || !memory_access_is_direct(mr
, true)) {
2945 #if defined(TARGET_WORDS_BIGENDIAN)
2946 if (endian
== DEVICE_LITTLE_ENDIAN
) {
2950 if (endian
== DEVICE_BIG_ENDIAN
) {
2954 io_mem_write(mr
, addr1
, val
, 2);
2957 addr1
+= memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
;
2958 ptr
= qemu_get_ram_ptr(addr1
);
2960 case DEVICE_LITTLE_ENDIAN
:
2963 case DEVICE_BIG_ENDIAN
:
2970 invalidate_and_set_dirty(addr1
, 2);
2974 void stw_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
)
2976 stw_phys_internal(as
, addr
, val
, DEVICE_NATIVE_ENDIAN
);
2979 void stw_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
)
2981 stw_phys_internal(as
, addr
, val
, DEVICE_LITTLE_ENDIAN
);
2984 void stw_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
)
2986 stw_phys_internal(as
, addr
, val
, DEVICE_BIG_ENDIAN
);
2990 void stq_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
)
2993 address_space_rw(as
, addr
, (void *) &val
, 8, 1);
2996 void stq_le_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
)
2998 val
= cpu_to_le64(val
);
2999 address_space_rw(as
, addr
, (void *) &val
, 8, 1);
3002 void stq_be_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
)
3004 val
= cpu_to_be64(val
);
3005 address_space_rw(as
, addr
, (void *) &val
, 8, 1);
3008 /* virtual memory access for debug (includes writing to ROM) */
3009 int cpu_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
3010 uint8_t *buf
, int len
, int is_write
)
3017 page
= addr
& TARGET_PAGE_MASK
;
3018 phys_addr
= cpu_get_phys_page_debug(cpu
, page
);
3019 /* if no physical page mapped, return an error */
3020 if (phys_addr
== -1)
3022 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3025 phys_addr
+= (addr
& ~TARGET_PAGE_MASK
);
3027 cpu_physical_memory_write_rom(cpu
->as
, phys_addr
, buf
, l
);
3029 address_space_rw(cpu
->as
, phys_addr
, buf
, l
, 0);
3040 * A helper function for the _utterly broken_ virtio device model to find out if
3041 * it's running on a big endian machine. Don't do this at home kids!
3043 bool target_words_bigendian(void);
3044 bool target_words_bigendian(void)
3046 #if defined(TARGET_WORDS_BIGENDIAN)
3053 #ifndef CONFIG_USER_ONLY
3054 bool cpu_physical_memory_is_io(hwaddr phys_addr
)
3059 mr
= address_space_translate(&address_space_memory
,
3060 phys_addr
, &phys_addr
, &l
, false);
3062 return !(memory_region_is_ram(mr
) ||
3063 memory_region_is_romd(mr
));
3066 void qemu_ram_foreach_block(RAMBlockIterFunc func
, void *opaque
)
3071 QLIST_FOREACH_RCU(block
, &ram_list
.blocks
, next
) {
3072 func(block
->host
, block
->offset
, block
->used_length
, opaque
);