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/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qapi/error.h"
24 #include "qemu/cutils.h"
26 #include "exec/exec-all.h"
27 #include "exec/target_page.h"
29 #include "hw/qdev-core.h"
30 #include "hw/qdev-properties.h"
31 #if !defined(CONFIG_USER_ONLY)
32 #include "hw/boards.h"
33 #include "hw/xen/xen.h"
35 #include "sysemu/kvm.h"
36 #include "sysemu/sysemu.h"
37 #include "sysemu/tcg.h"
38 #include "sysemu/qtest.h"
39 #include "qemu/timer.h"
40 #include "qemu/config-file.h"
41 #include "qemu/error-report.h"
42 #include "qemu/qemu-print.h"
43 #if defined(CONFIG_USER_ONLY)
45 #else /* !CONFIG_USER_ONLY */
46 #include "exec/memory.h"
47 #include "exec/ioport.h"
48 #include "sysemu/dma.h"
49 #include "sysemu/hostmem.h"
50 #include "sysemu/hw_accel.h"
51 #include "exec/address-spaces.h"
52 #include "sysemu/xen-mapcache.h"
53 #include "trace-root.h"
55 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
56 #include <linux/falloc.h>
60 #include "qemu/rcu_queue.h"
61 #include "qemu/main-loop.h"
62 #include "translate-all.h"
63 #include "sysemu/replay.h"
65 #include "exec/memory-internal.h"
66 #include "exec/ram_addr.h"
69 #include "qemu/pmem.h"
71 #include "migration/vmstate.h"
73 #include "qemu/range.h"
75 #include "qemu/mmap-alloc.h"
78 #include "monitor/monitor.h"
80 #ifdef CONFIG_LIBDAXCTL
81 #include <daxctl/libdaxctl.h>
84 //#define DEBUG_SUBPAGE
86 #if !defined(CONFIG_USER_ONLY)
87 /* ram_list is read under rcu_read_lock()/rcu_read_unlock(). Writes
88 * are protected by the ramlist lock.
90 RAMList ram_list
= { .blocks
= QLIST_HEAD_INITIALIZER(ram_list
.blocks
) };
92 static MemoryRegion
*system_memory
;
93 static MemoryRegion
*system_io
;
95 AddressSpace address_space_io
;
96 AddressSpace address_space_memory
;
98 static MemoryRegion io_mem_unassigned
;
101 CPUTailQ cpus
= QTAILQ_HEAD_INITIALIZER(cpus
);
103 /* current CPU in the current thread. It is only valid inside
105 __thread CPUState
*current_cpu
;
107 uintptr_t qemu_host_page_size
;
108 intptr_t qemu_host_page_mask
;
110 #if !defined(CONFIG_USER_ONLY)
111 /* 0 = Do not count executed instructions.
112 1 = Precise instruction counting.
113 2 = Adaptive rate instruction counting. */
116 typedef struct PhysPageEntry PhysPageEntry
;
118 struct PhysPageEntry
{
119 /* How many bits skip to next level (in units of L2_SIZE). 0 for a leaf. */
121 /* index into phys_sections (!skip) or phys_map_nodes (skip) */
125 #define PHYS_MAP_NODE_NIL (((uint32_t)~0) >> 6)
127 /* Size of the L2 (and L3, etc) page tables. */
128 #define ADDR_SPACE_BITS 64
131 #define P_L2_SIZE (1 << P_L2_BITS)
133 #define P_L2_LEVELS (((ADDR_SPACE_BITS - TARGET_PAGE_BITS - 1) / P_L2_BITS) + 1)
135 typedef PhysPageEntry Node
[P_L2_SIZE
];
137 typedef struct PhysPageMap
{
140 unsigned sections_nb
;
141 unsigned sections_nb_alloc
;
143 unsigned nodes_nb_alloc
;
145 MemoryRegionSection
*sections
;
148 struct AddressSpaceDispatch
{
149 MemoryRegionSection
*mru_section
;
150 /* This is a multi-level map on the physical address space.
151 * The bottom level has pointers to MemoryRegionSections.
153 PhysPageEntry phys_map
;
157 #define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
158 typedef struct subpage_t
{
162 uint16_t sub_section
[];
165 #define PHYS_SECTION_UNASSIGNED 0
167 static void io_mem_init(void);
168 static void memory_map_init(void);
169 static void tcg_log_global_after_sync(MemoryListener
*listener
);
170 static void tcg_commit(MemoryListener
*listener
);
173 * CPUAddressSpace: all the information a CPU needs about an AddressSpace
174 * @cpu: the CPU whose AddressSpace this is
175 * @as: the AddressSpace itself
176 * @memory_dispatch: its dispatch pointer (cached, RCU protected)
177 * @tcg_as_listener: listener for tracking changes to the AddressSpace
179 struct CPUAddressSpace
{
182 struct AddressSpaceDispatch
*memory_dispatch
;
183 MemoryListener tcg_as_listener
;
186 struct DirtyBitmapSnapshot
{
189 unsigned long dirty
[];
194 #if !defined(CONFIG_USER_ONLY)
196 static void phys_map_node_reserve(PhysPageMap
*map
, unsigned nodes
)
198 static unsigned alloc_hint
= 16;
199 if (map
->nodes_nb
+ nodes
> map
->nodes_nb_alloc
) {
200 map
->nodes_nb_alloc
= MAX(alloc_hint
, map
->nodes_nb
+ nodes
);
201 map
->nodes
= g_renew(Node
, map
->nodes
, map
->nodes_nb_alloc
);
202 alloc_hint
= map
->nodes_nb_alloc
;
206 static uint32_t phys_map_node_alloc(PhysPageMap
*map
, bool leaf
)
213 ret
= map
->nodes_nb
++;
215 assert(ret
!= PHYS_MAP_NODE_NIL
);
216 assert(ret
!= map
->nodes_nb_alloc
);
218 e
.skip
= leaf
? 0 : 1;
219 e
.ptr
= leaf
? PHYS_SECTION_UNASSIGNED
: PHYS_MAP_NODE_NIL
;
220 for (i
= 0; i
< P_L2_SIZE
; ++i
) {
221 memcpy(&p
[i
], &e
, sizeof(e
));
226 static void phys_page_set_level(PhysPageMap
*map
, PhysPageEntry
*lp
,
227 hwaddr
*index
, uint64_t *nb
, uint16_t leaf
,
231 hwaddr step
= (hwaddr
)1 << (level
* P_L2_BITS
);
233 if (lp
->skip
&& lp
->ptr
== PHYS_MAP_NODE_NIL
) {
234 lp
->ptr
= phys_map_node_alloc(map
, level
== 0);
236 p
= map
->nodes
[lp
->ptr
];
237 lp
= &p
[(*index
>> (level
* P_L2_BITS
)) & (P_L2_SIZE
- 1)];
239 while (*nb
&& lp
< &p
[P_L2_SIZE
]) {
240 if ((*index
& (step
- 1)) == 0 && *nb
>= step
) {
246 phys_page_set_level(map
, lp
, index
, nb
, leaf
, level
- 1);
252 static void phys_page_set(AddressSpaceDispatch
*d
,
253 hwaddr index
, uint64_t nb
,
256 /* Wildly overreserve - it doesn't matter much. */
257 phys_map_node_reserve(&d
->map
, 3 * P_L2_LEVELS
);
259 phys_page_set_level(&d
->map
, &d
->phys_map
, &index
, &nb
, leaf
, P_L2_LEVELS
- 1);
262 /* Compact a non leaf page entry. Simply detect that the entry has a single child,
263 * and update our entry so we can skip it and go directly to the destination.
265 static void phys_page_compact(PhysPageEntry
*lp
, Node
*nodes
)
267 unsigned valid_ptr
= P_L2_SIZE
;
272 if (lp
->ptr
== PHYS_MAP_NODE_NIL
) {
277 for (i
= 0; i
< P_L2_SIZE
; i
++) {
278 if (p
[i
].ptr
== PHYS_MAP_NODE_NIL
) {
285 phys_page_compact(&p
[i
], nodes
);
289 /* We can only compress if there's only one child. */
294 assert(valid_ptr
< P_L2_SIZE
);
296 /* Don't compress if it won't fit in the # of bits we have. */
297 if (P_L2_LEVELS
>= (1 << 6) &&
298 lp
->skip
+ p
[valid_ptr
].skip
>= (1 << 6)) {
302 lp
->ptr
= p
[valid_ptr
].ptr
;
303 if (!p
[valid_ptr
].skip
) {
304 /* If our only child is a leaf, make this a leaf. */
305 /* By design, we should have made this node a leaf to begin with so we
306 * should never reach here.
307 * But since it's so simple to handle this, let's do it just in case we
312 lp
->skip
+= p
[valid_ptr
].skip
;
316 void address_space_dispatch_compact(AddressSpaceDispatch
*d
)
318 if (d
->phys_map
.skip
) {
319 phys_page_compact(&d
->phys_map
, d
->map
.nodes
);
323 static inline bool section_covers_addr(const MemoryRegionSection
*section
,
326 /* Memory topology clips a memory region to [0, 2^64); size.hi > 0 means
327 * the section must cover the entire address space.
329 return int128_gethi(section
->size
) ||
330 range_covers_byte(section
->offset_within_address_space
,
331 int128_getlo(section
->size
), addr
);
334 static MemoryRegionSection
*phys_page_find(AddressSpaceDispatch
*d
, hwaddr addr
)
336 PhysPageEntry lp
= d
->phys_map
, *p
;
337 Node
*nodes
= d
->map
.nodes
;
338 MemoryRegionSection
*sections
= d
->map
.sections
;
339 hwaddr index
= addr
>> TARGET_PAGE_BITS
;
342 for (i
= P_L2_LEVELS
; lp
.skip
&& (i
-= lp
.skip
) >= 0;) {
343 if (lp
.ptr
== PHYS_MAP_NODE_NIL
) {
344 return §ions
[PHYS_SECTION_UNASSIGNED
];
347 lp
= p
[(index
>> (i
* P_L2_BITS
)) & (P_L2_SIZE
- 1)];
350 if (section_covers_addr(§ions
[lp
.ptr
], addr
)) {
351 return §ions
[lp
.ptr
];
353 return §ions
[PHYS_SECTION_UNASSIGNED
];
357 /* Called from RCU critical section */
358 static MemoryRegionSection
*address_space_lookup_region(AddressSpaceDispatch
*d
,
360 bool resolve_subpage
)
362 MemoryRegionSection
*section
= atomic_read(&d
->mru_section
);
365 if (!section
|| section
== &d
->map
.sections
[PHYS_SECTION_UNASSIGNED
] ||
366 !section_covers_addr(section
, addr
)) {
367 section
= phys_page_find(d
, addr
);
368 atomic_set(&d
->mru_section
, section
);
370 if (resolve_subpage
&& section
->mr
->subpage
) {
371 subpage
= container_of(section
->mr
, subpage_t
, iomem
);
372 section
= &d
->map
.sections
[subpage
->sub_section
[SUBPAGE_IDX(addr
)]];
377 /* Called from RCU critical section */
378 static MemoryRegionSection
*
379 address_space_translate_internal(AddressSpaceDispatch
*d
, hwaddr addr
, hwaddr
*xlat
,
380 hwaddr
*plen
, bool resolve_subpage
)
382 MemoryRegionSection
*section
;
386 section
= address_space_lookup_region(d
, addr
, resolve_subpage
);
387 /* Compute offset within MemoryRegionSection */
388 addr
-= section
->offset_within_address_space
;
390 /* Compute offset within MemoryRegion */
391 *xlat
= addr
+ section
->offset_within_region
;
395 /* MMIO registers can be expected to perform full-width accesses based only
396 * on their address, without considering adjacent registers that could
397 * decode to completely different MemoryRegions. When such registers
398 * exist (e.g. I/O ports 0xcf8 and 0xcf9 on most PC chipsets), MMIO
399 * regions overlap wildly. For this reason we cannot clamp the accesses
402 * If the length is small (as is the case for address_space_ldl/stl),
403 * everything works fine. If the incoming length is large, however,
404 * the caller really has to do the clamping through memory_access_size.
406 if (memory_region_is_ram(mr
)) {
407 diff
= int128_sub(section
->size
, int128_make64(addr
));
408 *plen
= int128_get64(int128_min(diff
, int128_make64(*plen
)));
414 * address_space_translate_iommu - translate an address through an IOMMU
415 * memory region and then through the target address space.
417 * @iommu_mr: the IOMMU memory region that we start the translation from
418 * @addr: the address to be translated through the MMU
419 * @xlat: the translated address offset within the destination memory region.
420 * It cannot be %NULL.
421 * @plen_out: valid read/write length of the translated address. It
423 * @page_mask_out: page mask for the translated address. This
424 * should only be meaningful for IOMMU translated
425 * addresses, since there may be huge pages that this bit
426 * would tell. It can be %NULL if we don't care about it.
427 * @is_write: whether the translation operation is for write
428 * @is_mmio: whether this can be MMIO, set true if it can
429 * @target_as: the address space targeted by the IOMMU
430 * @attrs: transaction attributes
432 * This function is called from RCU critical section. It is the common
433 * part of flatview_do_translate and address_space_translate_cached.
435 static MemoryRegionSection
address_space_translate_iommu(IOMMUMemoryRegion
*iommu_mr
,
438 hwaddr
*page_mask_out
,
441 AddressSpace
**target_as
,
444 MemoryRegionSection
*section
;
445 hwaddr page_mask
= (hwaddr
)-1;
449 IOMMUMemoryRegionClass
*imrc
= memory_region_get_iommu_class_nocheck(iommu_mr
);
453 if (imrc
->attrs_to_index
) {
454 iommu_idx
= imrc
->attrs_to_index(iommu_mr
, attrs
);
457 iotlb
= imrc
->translate(iommu_mr
, addr
, is_write
?
458 IOMMU_WO
: IOMMU_RO
, iommu_idx
);
460 if (!(iotlb
.perm
& (1 << is_write
))) {
464 addr
= ((iotlb
.translated_addr
& ~iotlb
.addr_mask
)
465 | (addr
& iotlb
.addr_mask
));
466 page_mask
&= iotlb
.addr_mask
;
467 *plen_out
= MIN(*plen_out
, (addr
| iotlb
.addr_mask
) - addr
+ 1);
468 *target_as
= iotlb
.target_as
;
470 section
= address_space_translate_internal(
471 address_space_to_dispatch(iotlb
.target_as
), addr
, xlat
,
474 iommu_mr
= memory_region_get_iommu(section
->mr
);
475 } while (unlikely(iommu_mr
));
478 *page_mask_out
= page_mask
;
483 return (MemoryRegionSection
) { .mr
= &io_mem_unassigned
};
487 * flatview_do_translate - translate an address in FlatView
489 * @fv: the flat view that we want to translate on
490 * @addr: the address to be translated in above address space
491 * @xlat: the translated address offset within memory region. It
493 * @plen_out: valid read/write length of the translated address. It
494 * can be @NULL when we don't care about it.
495 * @page_mask_out: page mask for the translated address. This
496 * should only be meaningful for IOMMU translated
497 * addresses, since there may be huge pages that this bit
498 * would tell. It can be @NULL if we don't care about it.
499 * @is_write: whether the translation operation is for write
500 * @is_mmio: whether this can be MMIO, set true if it can
501 * @target_as: the address space targeted by the IOMMU
502 * @attrs: memory transaction attributes
504 * This function is called from RCU critical section
506 static MemoryRegionSection
flatview_do_translate(FlatView
*fv
,
510 hwaddr
*page_mask_out
,
513 AddressSpace
**target_as
,
516 MemoryRegionSection
*section
;
517 IOMMUMemoryRegion
*iommu_mr
;
518 hwaddr plen
= (hwaddr
)(-1);
524 section
= address_space_translate_internal(
525 flatview_to_dispatch(fv
), addr
, xlat
,
528 iommu_mr
= memory_region_get_iommu(section
->mr
);
529 if (unlikely(iommu_mr
)) {
530 return address_space_translate_iommu(iommu_mr
, xlat
,
531 plen_out
, page_mask_out
,
536 /* Not behind an IOMMU, use default page size. */
537 *page_mask_out
= ~TARGET_PAGE_MASK
;
543 /* Called from RCU critical section */
544 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
545 bool is_write
, MemTxAttrs attrs
)
547 MemoryRegionSection section
;
548 hwaddr xlat
, page_mask
;
551 * This can never be MMIO, and we don't really care about plen,
554 section
= flatview_do_translate(address_space_to_flatview(as
), addr
, &xlat
,
555 NULL
, &page_mask
, is_write
, false, &as
,
558 /* Illegal translation */
559 if (section
.mr
== &io_mem_unassigned
) {
563 /* Convert memory region offset into address space offset */
564 xlat
+= section
.offset_within_address_space
-
565 section
.offset_within_region
;
567 return (IOMMUTLBEntry
) {
569 .iova
= addr
& ~page_mask
,
570 .translated_addr
= xlat
& ~page_mask
,
571 .addr_mask
= page_mask
,
572 /* IOTLBs are for DMAs, and DMA only allows on RAMs. */
577 return (IOMMUTLBEntry
) {0};
580 /* Called from RCU critical section */
581 MemoryRegion
*flatview_translate(FlatView
*fv
, hwaddr addr
, hwaddr
*xlat
,
582 hwaddr
*plen
, bool is_write
,
586 MemoryRegionSection section
;
587 AddressSpace
*as
= NULL
;
589 /* This can be MMIO, so setup MMIO bit. */
590 section
= flatview_do_translate(fv
, addr
, xlat
, plen
, NULL
,
591 is_write
, true, &as
, attrs
);
594 if (xen_enabled() && memory_access_is_direct(mr
, is_write
)) {
595 hwaddr page
= ((addr
& TARGET_PAGE_MASK
) + TARGET_PAGE_SIZE
) - addr
;
596 *plen
= MIN(page
, *plen
);
602 typedef struct TCGIOMMUNotifier
{
610 static void tcg_iommu_unmap_notify(IOMMUNotifier
*n
, IOMMUTLBEntry
*iotlb
)
612 TCGIOMMUNotifier
*notifier
= container_of(n
, TCGIOMMUNotifier
, n
);
614 if (!notifier
->active
) {
617 tlb_flush(notifier
->cpu
);
618 notifier
->active
= false;
619 /* We leave the notifier struct on the list to avoid reallocating it later.
620 * Generally the number of IOMMUs a CPU deals with will be small.
621 * In any case we can't unregister the iommu notifier from a notify
626 static void tcg_register_iommu_notifier(CPUState
*cpu
,
627 IOMMUMemoryRegion
*iommu_mr
,
630 /* Make sure this CPU has an IOMMU notifier registered for this
631 * IOMMU/IOMMU index combination, so that we can flush its TLB
632 * when the IOMMU tells us the mappings we've cached have changed.
634 MemoryRegion
*mr
= MEMORY_REGION(iommu_mr
);
635 TCGIOMMUNotifier
*notifier
;
639 for (i
= 0; i
< cpu
->iommu_notifiers
->len
; i
++) {
640 notifier
= g_array_index(cpu
->iommu_notifiers
, TCGIOMMUNotifier
*, i
);
641 if (notifier
->mr
== mr
&& notifier
->iommu_idx
== iommu_idx
) {
645 if (i
== cpu
->iommu_notifiers
->len
) {
646 /* Not found, add a new entry at the end of the array */
647 cpu
->iommu_notifiers
= g_array_set_size(cpu
->iommu_notifiers
, i
+ 1);
648 notifier
= g_new0(TCGIOMMUNotifier
, 1);
649 g_array_index(cpu
->iommu_notifiers
, TCGIOMMUNotifier
*, i
) = notifier
;
652 notifier
->iommu_idx
= iommu_idx
;
654 /* Rather than trying to register interest in the specific part
655 * of the iommu's address space that we've accessed and then
656 * expand it later as subsequent accesses touch more of it, we
657 * just register interest in the whole thing, on the assumption
658 * that iommu reconfiguration will be rare.
660 iommu_notifier_init(¬ifier
->n
,
661 tcg_iommu_unmap_notify
,
662 IOMMU_NOTIFIER_UNMAP
,
666 ret
= memory_region_register_iommu_notifier(notifier
->mr
, ¬ifier
->n
,
669 error_report_err(err
);
674 if (!notifier
->active
) {
675 notifier
->active
= true;
679 static void tcg_iommu_free_notifier_list(CPUState
*cpu
)
681 /* Destroy the CPU's notifier list */
683 TCGIOMMUNotifier
*notifier
;
685 for (i
= 0; i
< cpu
->iommu_notifiers
->len
; i
++) {
686 notifier
= g_array_index(cpu
->iommu_notifiers
, TCGIOMMUNotifier
*, i
);
687 memory_region_unregister_iommu_notifier(notifier
->mr
, ¬ifier
->n
);
690 g_array_free(cpu
->iommu_notifiers
, true);
693 /* Called from RCU critical section */
694 MemoryRegionSection
*
695 address_space_translate_for_iotlb(CPUState
*cpu
, int asidx
, hwaddr addr
,
696 hwaddr
*xlat
, hwaddr
*plen
,
697 MemTxAttrs attrs
, int *prot
)
699 MemoryRegionSection
*section
;
700 IOMMUMemoryRegion
*iommu_mr
;
701 IOMMUMemoryRegionClass
*imrc
;
704 AddressSpaceDispatch
*d
= atomic_rcu_read(&cpu
->cpu_ases
[asidx
].memory_dispatch
);
707 section
= address_space_translate_internal(d
, addr
, &addr
, plen
, false);
709 iommu_mr
= memory_region_get_iommu(section
->mr
);
714 imrc
= memory_region_get_iommu_class_nocheck(iommu_mr
);
716 iommu_idx
= imrc
->attrs_to_index(iommu_mr
, attrs
);
717 tcg_register_iommu_notifier(cpu
, iommu_mr
, iommu_idx
);
718 /* We need all the permissions, so pass IOMMU_NONE so the IOMMU
719 * doesn't short-cut its translation table walk.
721 iotlb
= imrc
->translate(iommu_mr
, addr
, IOMMU_NONE
, iommu_idx
);
722 addr
= ((iotlb
.translated_addr
& ~iotlb
.addr_mask
)
723 | (addr
& iotlb
.addr_mask
));
724 /* Update the caller's prot bits to remove permissions the IOMMU
725 * is giving us a failure response for. If we get down to no
726 * permissions left at all we can give up now.
728 if (!(iotlb
.perm
& IOMMU_RO
)) {
729 *prot
&= ~(PAGE_READ
| PAGE_EXEC
);
731 if (!(iotlb
.perm
& IOMMU_WO
)) {
732 *prot
&= ~PAGE_WRITE
;
739 d
= flatview_to_dispatch(address_space_to_flatview(iotlb
.target_as
));
742 assert(!memory_region_is_iommu(section
->mr
));
747 return &d
->map
.sections
[PHYS_SECTION_UNASSIGNED
];
751 #if !defined(CONFIG_USER_ONLY)
753 static int cpu_common_post_load(void *opaque
, int version_id
)
755 CPUState
*cpu
= opaque
;
757 /* 0x01 was CPU_INTERRUPT_EXIT. This line can be removed when the
758 version_id is increased. */
759 cpu
->interrupt_request
&= ~0x01;
762 /* loadvm has just updated the content of RAM, bypassing the
763 * usual mechanisms that ensure we flush TBs for writes to
764 * memory we've translated code from. So we must flush all TBs,
765 * which will now be stale.
772 static int cpu_common_pre_load(void *opaque
)
774 CPUState
*cpu
= opaque
;
776 cpu
->exception_index
= -1;
781 static bool cpu_common_exception_index_needed(void *opaque
)
783 CPUState
*cpu
= opaque
;
785 return tcg_enabled() && cpu
->exception_index
!= -1;
788 static const VMStateDescription vmstate_cpu_common_exception_index
= {
789 .name
= "cpu_common/exception_index",
791 .minimum_version_id
= 1,
792 .needed
= cpu_common_exception_index_needed
,
793 .fields
= (VMStateField
[]) {
794 VMSTATE_INT32(exception_index
, CPUState
),
795 VMSTATE_END_OF_LIST()
799 static bool cpu_common_crash_occurred_needed(void *opaque
)
801 CPUState
*cpu
= opaque
;
803 return cpu
->crash_occurred
;
806 static const VMStateDescription vmstate_cpu_common_crash_occurred
= {
807 .name
= "cpu_common/crash_occurred",
809 .minimum_version_id
= 1,
810 .needed
= cpu_common_crash_occurred_needed
,
811 .fields
= (VMStateField
[]) {
812 VMSTATE_BOOL(crash_occurred
, CPUState
),
813 VMSTATE_END_OF_LIST()
817 const VMStateDescription vmstate_cpu_common
= {
818 .name
= "cpu_common",
820 .minimum_version_id
= 1,
821 .pre_load
= cpu_common_pre_load
,
822 .post_load
= cpu_common_post_load
,
823 .fields
= (VMStateField
[]) {
824 VMSTATE_UINT32(halted
, CPUState
),
825 VMSTATE_UINT32(interrupt_request
, CPUState
),
826 VMSTATE_END_OF_LIST()
828 .subsections
= (const VMStateDescription
*[]) {
829 &vmstate_cpu_common_exception_index
,
830 &vmstate_cpu_common_crash_occurred
,
837 CPUState
*qemu_get_cpu(int index
)
842 if (cpu
->cpu_index
== index
) {
850 #if !defined(CONFIG_USER_ONLY)
851 void cpu_address_space_init(CPUState
*cpu
, int asidx
,
852 const char *prefix
, MemoryRegion
*mr
)
854 CPUAddressSpace
*newas
;
855 AddressSpace
*as
= g_new0(AddressSpace
, 1);
859 as_name
= g_strdup_printf("%s-%d", prefix
, cpu
->cpu_index
);
860 address_space_init(as
, mr
, as_name
);
863 /* Target code should have set num_ases before calling us */
864 assert(asidx
< cpu
->num_ases
);
867 /* address space 0 gets the convenience alias */
871 /* KVM cannot currently support multiple address spaces. */
872 assert(asidx
== 0 || !kvm_enabled());
874 if (!cpu
->cpu_ases
) {
875 cpu
->cpu_ases
= g_new0(CPUAddressSpace
, cpu
->num_ases
);
878 newas
= &cpu
->cpu_ases
[asidx
];
882 newas
->tcg_as_listener
.log_global_after_sync
= tcg_log_global_after_sync
;
883 newas
->tcg_as_listener
.commit
= tcg_commit
;
884 memory_listener_register(&newas
->tcg_as_listener
, as
);
888 AddressSpace
*cpu_get_address_space(CPUState
*cpu
, int asidx
)
890 /* Return the AddressSpace corresponding to the specified index */
891 return cpu
->cpu_ases
[asidx
].as
;
895 void cpu_exec_unrealizefn(CPUState
*cpu
)
897 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
900 cpu_list_remove(cpu
);
902 if (cc
->vmsd
!= NULL
) {
903 vmstate_unregister(NULL
, cc
->vmsd
, cpu
);
905 if (qdev_get_vmsd(DEVICE(cpu
)) == NULL
) {
906 vmstate_unregister(NULL
, &vmstate_cpu_common
, cpu
);
908 #ifndef CONFIG_USER_ONLY
909 tcg_iommu_free_notifier_list(cpu
);
913 Property cpu_common_props
[] = {
914 #ifndef CONFIG_USER_ONLY
915 /* Create a memory property for softmmu CPU object,
916 * so users can wire up its memory. (This can't go in hw/core/cpu.c
917 * because that file is compiled only once for both user-mode
918 * and system builds.) The default if no link is set up is to use
919 * the system address space.
921 DEFINE_PROP_LINK("memory", CPUState
, memory
, TYPE_MEMORY_REGION
,
924 DEFINE_PROP_END_OF_LIST(),
927 void cpu_exec_initfn(CPUState
*cpu
)
932 #ifndef CONFIG_USER_ONLY
933 cpu
->thread_id
= qemu_get_thread_id();
934 cpu
->memory
= system_memory
;
935 object_ref(OBJECT(cpu
->memory
));
939 void cpu_exec_realizefn(CPUState
*cpu
, Error
**errp
)
941 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
942 static bool tcg_target_initialized
;
946 if (tcg_enabled() && !tcg_target_initialized
) {
947 tcg_target_initialized
= true;
948 cc
->tcg_initialize();
952 qemu_plugin_vcpu_init_hook(cpu
);
954 #ifdef CONFIG_USER_ONLY
955 assert(cc
->vmsd
== NULL
);
956 #else /* !CONFIG_USER_ONLY */
957 if (qdev_get_vmsd(DEVICE(cpu
)) == NULL
) {
958 vmstate_register(NULL
, cpu
->cpu_index
, &vmstate_cpu_common
, cpu
);
960 if (cc
->vmsd
!= NULL
) {
961 vmstate_register(NULL
, cpu
->cpu_index
, cc
->vmsd
, cpu
);
964 cpu
->iommu_notifiers
= g_array_new(false, true, sizeof(TCGIOMMUNotifier
*));
968 const char *parse_cpu_option(const char *cpu_option
)
972 gchar
**model_pieces
;
973 const char *cpu_type
;
975 model_pieces
= g_strsplit(cpu_option
, ",", 2);
976 if (!model_pieces
[0]) {
977 error_report("-cpu option cannot be empty");
981 oc
= cpu_class_by_name(CPU_RESOLVING_TYPE
, model_pieces
[0]);
983 error_report("unable to find CPU model '%s'", model_pieces
[0]);
984 g_strfreev(model_pieces
);
988 cpu_type
= object_class_get_name(oc
);
990 cc
->parse_features(cpu_type
, model_pieces
[1], &error_fatal
);
991 g_strfreev(model_pieces
);
995 #if defined(CONFIG_USER_ONLY)
996 void tb_invalidate_phys_addr(target_ulong addr
)
999 tb_invalidate_phys_page_range(addr
, addr
+ 1);
1003 static void breakpoint_invalidate(CPUState
*cpu
, target_ulong pc
)
1005 tb_invalidate_phys_addr(pc
);
1008 void tb_invalidate_phys_addr(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
)
1010 ram_addr_t ram_addr
;
1014 if (!tcg_enabled()) {
1018 RCU_READ_LOCK_GUARD();
1019 mr
= address_space_translate(as
, addr
, &addr
, &l
, false, attrs
);
1020 if (!(memory_region_is_ram(mr
)
1021 || memory_region_is_romd(mr
))) {
1024 ram_addr
= memory_region_get_ram_addr(mr
) + addr
;
1025 tb_invalidate_phys_page_range(ram_addr
, ram_addr
+ 1);
1028 static void breakpoint_invalidate(CPUState
*cpu
, target_ulong pc
)
1031 * There may not be a virtual to physical translation for the pc
1032 * right now, but there may exist cached TB for this pc.
1033 * Flush the whole TB cache to force re-translation of such TBs.
1034 * This is heavyweight, but we're debugging anyway.
1040 #ifndef CONFIG_USER_ONLY
1041 /* Add a watchpoint. */
1042 int cpu_watchpoint_insert(CPUState
*cpu
, vaddr addr
, vaddr len
,
1043 int flags
, CPUWatchpoint
**watchpoint
)
1048 /* forbid ranges which are empty or run off the end of the address space */
1049 if (len
== 0 || (addr
+ len
- 1) < addr
) {
1050 error_report("tried to set invalid watchpoint at %"
1051 VADDR_PRIx
", len=%" VADDR_PRIu
, addr
, len
);
1054 wp
= g_malloc(sizeof(*wp
));
1060 /* keep all GDB-injected watchpoints in front */
1061 if (flags
& BP_GDB
) {
1062 QTAILQ_INSERT_HEAD(&cpu
->watchpoints
, wp
, entry
);
1064 QTAILQ_INSERT_TAIL(&cpu
->watchpoints
, wp
, entry
);
1067 in_page
= -(addr
| TARGET_PAGE_MASK
);
1068 if (len
<= in_page
) {
1069 tlb_flush_page(cpu
, addr
);
1079 /* Remove a specific watchpoint. */
1080 int cpu_watchpoint_remove(CPUState
*cpu
, vaddr addr
, vaddr len
,
1085 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
1086 if (addr
== wp
->vaddr
&& len
== wp
->len
1087 && flags
== (wp
->flags
& ~BP_WATCHPOINT_HIT
)) {
1088 cpu_watchpoint_remove_by_ref(cpu
, wp
);
1095 /* Remove a specific watchpoint by reference. */
1096 void cpu_watchpoint_remove_by_ref(CPUState
*cpu
, CPUWatchpoint
*watchpoint
)
1098 QTAILQ_REMOVE(&cpu
->watchpoints
, watchpoint
, entry
);
1100 tlb_flush_page(cpu
, watchpoint
->vaddr
);
1105 /* Remove all matching watchpoints. */
1106 void cpu_watchpoint_remove_all(CPUState
*cpu
, int mask
)
1108 CPUWatchpoint
*wp
, *next
;
1110 QTAILQ_FOREACH_SAFE(wp
, &cpu
->watchpoints
, entry
, next
) {
1111 if (wp
->flags
& mask
) {
1112 cpu_watchpoint_remove_by_ref(cpu
, wp
);
1117 /* Return true if this watchpoint address matches the specified
1118 * access (ie the address range covered by the watchpoint overlaps
1119 * partially or completely with the address range covered by the
1122 static inline bool watchpoint_address_matches(CPUWatchpoint
*wp
,
1123 vaddr addr
, vaddr len
)
1125 /* We know the lengths are non-zero, but a little caution is
1126 * required to avoid errors in the case where the range ends
1127 * exactly at the top of the address space and so addr + len
1128 * wraps round to zero.
1130 vaddr wpend
= wp
->vaddr
+ wp
->len
- 1;
1131 vaddr addrend
= addr
+ len
- 1;
1133 return !(addr
> wpend
|| wp
->vaddr
> addrend
);
1136 /* Return flags for watchpoints that match addr + prot. */
1137 int cpu_watchpoint_address_matches(CPUState
*cpu
, vaddr addr
, vaddr len
)
1142 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
1143 if (watchpoint_address_matches(wp
, addr
, len
)) {
1149 #endif /* !CONFIG_USER_ONLY */
1151 /* Add a breakpoint. */
1152 int cpu_breakpoint_insert(CPUState
*cpu
, vaddr pc
, int flags
,
1153 CPUBreakpoint
**breakpoint
)
1157 bp
= g_malloc(sizeof(*bp
));
1162 /* keep all GDB-injected breakpoints in front */
1163 if (flags
& BP_GDB
) {
1164 QTAILQ_INSERT_HEAD(&cpu
->breakpoints
, bp
, entry
);
1166 QTAILQ_INSERT_TAIL(&cpu
->breakpoints
, bp
, entry
);
1169 breakpoint_invalidate(cpu
, pc
);
1177 /* Remove a specific breakpoint. */
1178 int cpu_breakpoint_remove(CPUState
*cpu
, vaddr pc
, int flags
)
1182 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
1183 if (bp
->pc
== pc
&& bp
->flags
== flags
) {
1184 cpu_breakpoint_remove_by_ref(cpu
, bp
);
1191 /* Remove a specific breakpoint by reference. */
1192 void cpu_breakpoint_remove_by_ref(CPUState
*cpu
, CPUBreakpoint
*breakpoint
)
1194 QTAILQ_REMOVE(&cpu
->breakpoints
, breakpoint
, entry
);
1196 breakpoint_invalidate(cpu
, breakpoint
->pc
);
1201 /* Remove all matching breakpoints. */
1202 void cpu_breakpoint_remove_all(CPUState
*cpu
, int mask
)
1204 CPUBreakpoint
*bp
, *next
;
1206 QTAILQ_FOREACH_SAFE(bp
, &cpu
->breakpoints
, entry
, next
) {
1207 if (bp
->flags
& mask
) {
1208 cpu_breakpoint_remove_by_ref(cpu
, bp
);
1213 /* enable or disable single step mode. EXCP_DEBUG is returned by the
1214 CPU loop after each instruction */
1215 void cpu_single_step(CPUState
*cpu
, int enabled
)
1217 if (cpu
->singlestep_enabled
!= enabled
) {
1218 cpu
->singlestep_enabled
= enabled
;
1219 if (kvm_enabled()) {
1220 kvm_update_guest_debug(cpu
, 0);
1222 /* must flush all the translated code to avoid inconsistencies */
1223 /* XXX: only flush what is necessary */
1229 void cpu_abort(CPUState
*cpu
, const char *fmt
, ...)
1236 fprintf(stderr
, "qemu: fatal: ");
1237 vfprintf(stderr
, fmt
, ap
);
1238 fprintf(stderr
, "\n");
1239 cpu_dump_state(cpu
, stderr
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
1240 if (qemu_log_separate()) {
1241 FILE *logfile
= qemu_log_lock();
1242 qemu_log("qemu: fatal: ");
1243 qemu_log_vprintf(fmt
, ap2
);
1245 log_cpu_state(cpu
, CPU_DUMP_FPU
| CPU_DUMP_CCOP
);
1247 qemu_log_unlock(logfile
);
1253 #if defined(CONFIG_USER_ONLY)
1255 struct sigaction act
;
1256 sigfillset(&act
.sa_mask
);
1257 act
.sa_handler
= SIG_DFL
;
1259 sigaction(SIGABRT
, &act
, NULL
);
1265 #if !defined(CONFIG_USER_ONLY)
1266 /* Called from RCU critical section */
1267 static RAMBlock
*qemu_get_ram_block(ram_addr_t addr
)
1271 block
= atomic_rcu_read(&ram_list
.mru_block
);
1272 if (block
&& addr
- block
->offset
< block
->max_length
) {
1275 RAMBLOCK_FOREACH(block
) {
1276 if (addr
- block
->offset
< block
->max_length
) {
1281 fprintf(stderr
, "Bad ram offset %" PRIx64
"\n", (uint64_t)addr
);
1285 /* It is safe to write mru_block outside the iothread lock. This
1290 * xxx removed from list
1294 * call_rcu(reclaim_ramblock, xxx);
1297 * atomic_rcu_set is not needed here. The block was already published
1298 * when it was placed into the list. Here we're just making an extra
1299 * copy of the pointer.
1301 ram_list
.mru_block
= block
;
1305 static void tlb_reset_dirty_range_all(ram_addr_t start
, ram_addr_t length
)
1312 assert(tcg_enabled());
1313 end
= TARGET_PAGE_ALIGN(start
+ length
);
1314 start
&= TARGET_PAGE_MASK
;
1316 RCU_READ_LOCK_GUARD();
1317 block
= qemu_get_ram_block(start
);
1318 assert(block
== qemu_get_ram_block(end
- 1));
1319 start1
= (uintptr_t)ramblock_ptr(block
, start
- block
->offset
);
1321 tlb_reset_dirty(cpu
, start1
, length
);
1325 /* Note: start and end must be within the same ram block. */
1326 bool cpu_physical_memory_test_and_clear_dirty(ram_addr_t start
,
1330 DirtyMemoryBlocks
*blocks
;
1331 unsigned long end
, page
, start_page
;
1334 uint64_t mr_offset
, mr_size
;
1340 end
= TARGET_PAGE_ALIGN(start
+ length
) >> TARGET_PAGE_BITS
;
1341 start_page
= start
>> TARGET_PAGE_BITS
;
1344 WITH_RCU_READ_LOCK_GUARD() {
1345 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
1346 ramblock
= qemu_get_ram_block(start
);
1347 /* Range sanity check on the ramblock */
1348 assert(start
>= ramblock
->offset
&&
1349 start
+ length
<= ramblock
->offset
+ ramblock
->used_length
);
1351 while (page
< end
) {
1352 unsigned long idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
1353 unsigned long offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
1354 unsigned long num
= MIN(end
- page
,
1355 DIRTY_MEMORY_BLOCK_SIZE
- offset
);
1357 dirty
|= bitmap_test_and_clear_atomic(blocks
->blocks
[idx
],
1362 mr_offset
= (ram_addr_t
)(start_page
<< TARGET_PAGE_BITS
) - ramblock
->offset
;
1363 mr_size
= (end
- start_page
) << TARGET_PAGE_BITS
;
1364 memory_region_clear_dirty_bitmap(ramblock
->mr
, mr_offset
, mr_size
);
1367 if (dirty
&& tcg_enabled()) {
1368 tlb_reset_dirty_range_all(start
, length
);
1374 DirtyBitmapSnapshot
*cpu_physical_memory_snapshot_and_clear_dirty
1375 (MemoryRegion
*mr
, hwaddr offset
, hwaddr length
, unsigned client
)
1377 DirtyMemoryBlocks
*blocks
;
1378 ram_addr_t start
= memory_region_get_ram_addr(mr
) + offset
;
1379 unsigned long align
= 1UL << (TARGET_PAGE_BITS
+ BITS_PER_LEVEL
);
1380 ram_addr_t first
= QEMU_ALIGN_DOWN(start
, align
);
1381 ram_addr_t last
= QEMU_ALIGN_UP(start
+ length
, align
);
1382 DirtyBitmapSnapshot
*snap
;
1383 unsigned long page
, end
, dest
;
1385 snap
= g_malloc0(sizeof(*snap
) +
1386 ((last
- first
) >> (TARGET_PAGE_BITS
+ 3)));
1387 snap
->start
= first
;
1390 page
= first
>> TARGET_PAGE_BITS
;
1391 end
= last
>> TARGET_PAGE_BITS
;
1394 WITH_RCU_READ_LOCK_GUARD() {
1395 blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[client
]);
1397 while (page
< end
) {
1398 unsigned long idx
= page
/ DIRTY_MEMORY_BLOCK_SIZE
;
1399 unsigned long offset
= page
% DIRTY_MEMORY_BLOCK_SIZE
;
1400 unsigned long num
= MIN(end
- page
,
1401 DIRTY_MEMORY_BLOCK_SIZE
- offset
);
1403 assert(QEMU_IS_ALIGNED(offset
, (1 << BITS_PER_LEVEL
)));
1404 assert(QEMU_IS_ALIGNED(num
, (1 << BITS_PER_LEVEL
)));
1405 offset
>>= BITS_PER_LEVEL
;
1407 bitmap_copy_and_clear_atomic(snap
->dirty
+ dest
,
1408 blocks
->blocks
[idx
] + offset
,
1411 dest
+= num
>> BITS_PER_LEVEL
;
1415 if (tcg_enabled()) {
1416 tlb_reset_dirty_range_all(start
, length
);
1419 memory_region_clear_dirty_bitmap(mr
, offset
, length
);
1424 bool cpu_physical_memory_snapshot_get_dirty(DirtyBitmapSnapshot
*snap
,
1428 unsigned long page
, end
;
1430 assert(start
>= snap
->start
);
1431 assert(start
+ length
<= snap
->end
);
1433 end
= TARGET_PAGE_ALIGN(start
+ length
- snap
->start
) >> TARGET_PAGE_BITS
;
1434 page
= (start
- snap
->start
) >> TARGET_PAGE_BITS
;
1436 while (page
< end
) {
1437 if (test_bit(page
, snap
->dirty
)) {
1445 /* Called from RCU critical section */
1446 hwaddr
memory_region_section_get_iotlb(CPUState
*cpu
,
1447 MemoryRegionSection
*section
)
1449 AddressSpaceDispatch
*d
= flatview_to_dispatch(section
->fv
);
1450 return section
- d
->map
.sections
;
1452 #endif /* defined(CONFIG_USER_ONLY) */
1454 #if !defined(CONFIG_USER_ONLY)
1456 static int subpage_register(subpage_t
*mmio
, uint32_t start
, uint32_t end
,
1458 static subpage_t
*subpage_init(FlatView
*fv
, hwaddr base
);
1460 static void *(*phys_mem_alloc
)(size_t size
, uint64_t *align
, bool shared
) =
1461 qemu_anon_ram_alloc
;
1464 * Set a custom physical guest memory alloator.
1465 * Accelerators with unusual needs may need this. Hopefully, we can
1466 * get rid of it eventually.
1468 void phys_mem_set_alloc(void *(*alloc
)(size_t, uint64_t *align
, bool shared
))
1470 phys_mem_alloc
= alloc
;
1473 static uint16_t phys_section_add(PhysPageMap
*map
,
1474 MemoryRegionSection
*section
)
1476 /* The physical section number is ORed with a page-aligned
1477 * pointer to produce the iotlb entries. Thus it should
1478 * never overflow into the page-aligned value.
1480 assert(map
->sections_nb
< TARGET_PAGE_SIZE
);
1482 if (map
->sections_nb
== map
->sections_nb_alloc
) {
1483 map
->sections_nb_alloc
= MAX(map
->sections_nb_alloc
* 2, 16);
1484 map
->sections
= g_renew(MemoryRegionSection
, map
->sections
,
1485 map
->sections_nb_alloc
);
1487 map
->sections
[map
->sections_nb
] = *section
;
1488 memory_region_ref(section
->mr
);
1489 return map
->sections_nb
++;
1492 static void phys_section_destroy(MemoryRegion
*mr
)
1494 bool have_sub_page
= mr
->subpage
;
1496 memory_region_unref(mr
);
1498 if (have_sub_page
) {
1499 subpage_t
*subpage
= container_of(mr
, subpage_t
, iomem
);
1500 object_unref(OBJECT(&subpage
->iomem
));
1505 static void phys_sections_free(PhysPageMap
*map
)
1507 while (map
->sections_nb
> 0) {
1508 MemoryRegionSection
*section
= &map
->sections
[--map
->sections_nb
];
1509 phys_section_destroy(section
->mr
);
1511 g_free(map
->sections
);
1515 static void register_subpage(FlatView
*fv
, MemoryRegionSection
*section
)
1517 AddressSpaceDispatch
*d
= flatview_to_dispatch(fv
);
1519 hwaddr base
= section
->offset_within_address_space
1521 MemoryRegionSection
*existing
= phys_page_find(d
, base
);
1522 MemoryRegionSection subsection
= {
1523 .offset_within_address_space
= base
,
1524 .size
= int128_make64(TARGET_PAGE_SIZE
),
1528 assert(existing
->mr
->subpage
|| existing
->mr
== &io_mem_unassigned
);
1530 if (!(existing
->mr
->subpage
)) {
1531 subpage
= subpage_init(fv
, base
);
1533 subsection
.mr
= &subpage
->iomem
;
1534 phys_page_set(d
, base
>> TARGET_PAGE_BITS
, 1,
1535 phys_section_add(&d
->map
, &subsection
));
1537 subpage
= container_of(existing
->mr
, subpage_t
, iomem
);
1539 start
= section
->offset_within_address_space
& ~TARGET_PAGE_MASK
;
1540 end
= start
+ int128_get64(section
->size
) - 1;
1541 subpage_register(subpage
, start
, end
,
1542 phys_section_add(&d
->map
, section
));
1546 static void register_multipage(FlatView
*fv
,
1547 MemoryRegionSection
*section
)
1549 AddressSpaceDispatch
*d
= flatview_to_dispatch(fv
);
1550 hwaddr start_addr
= section
->offset_within_address_space
;
1551 uint16_t section_index
= phys_section_add(&d
->map
, section
);
1552 uint64_t num_pages
= int128_get64(int128_rshift(section
->size
,
1556 phys_page_set(d
, start_addr
>> TARGET_PAGE_BITS
, num_pages
, section_index
);
1560 * The range in *section* may look like this:
1564 * where s stands for subpage and P for page.
1566 void flatview_add_to_dispatch(FlatView
*fv
, MemoryRegionSection
*section
)
1568 MemoryRegionSection remain
= *section
;
1569 Int128 page_size
= int128_make64(TARGET_PAGE_SIZE
);
1571 /* register first subpage */
1572 if (remain
.offset_within_address_space
& ~TARGET_PAGE_MASK
) {
1573 uint64_t left
= TARGET_PAGE_ALIGN(remain
.offset_within_address_space
)
1574 - remain
.offset_within_address_space
;
1576 MemoryRegionSection now
= remain
;
1577 now
.size
= int128_min(int128_make64(left
), now
.size
);
1578 register_subpage(fv
, &now
);
1579 if (int128_eq(remain
.size
, now
.size
)) {
1582 remain
.size
= int128_sub(remain
.size
, now
.size
);
1583 remain
.offset_within_address_space
+= int128_get64(now
.size
);
1584 remain
.offset_within_region
+= int128_get64(now
.size
);
1587 /* register whole pages */
1588 if (int128_ge(remain
.size
, page_size
)) {
1589 MemoryRegionSection now
= remain
;
1590 now
.size
= int128_and(now
.size
, int128_neg(page_size
));
1591 register_multipage(fv
, &now
);
1592 if (int128_eq(remain
.size
, now
.size
)) {
1595 remain
.size
= int128_sub(remain
.size
, now
.size
);
1596 remain
.offset_within_address_space
+= int128_get64(now
.size
);
1597 remain
.offset_within_region
+= int128_get64(now
.size
);
1600 /* register last subpage */
1601 register_subpage(fv
, &remain
);
1604 void qemu_flush_coalesced_mmio_buffer(void)
1607 kvm_flush_coalesced_mmio_buffer();
1610 void qemu_mutex_lock_ramlist(void)
1612 qemu_mutex_lock(&ram_list
.mutex
);
1615 void qemu_mutex_unlock_ramlist(void)
1617 qemu_mutex_unlock(&ram_list
.mutex
);
1620 void ram_block_dump(Monitor
*mon
)
1625 RCU_READ_LOCK_GUARD();
1626 monitor_printf(mon
, "%24s %8s %18s %18s %18s\n",
1627 "Block Name", "PSize", "Offset", "Used", "Total");
1628 RAMBLOCK_FOREACH(block
) {
1629 psize
= size_to_str(block
->page_size
);
1630 monitor_printf(mon
, "%24s %8s 0x%016" PRIx64
" 0x%016" PRIx64
1631 " 0x%016" PRIx64
"\n", block
->idstr
, psize
,
1632 (uint64_t)block
->offset
,
1633 (uint64_t)block
->used_length
,
1634 (uint64_t)block
->max_length
);
1641 * FIXME TOCTTOU: this iterates over memory backends' mem-path, which
1642 * may or may not name the same files / on the same filesystem now as
1643 * when we actually open and map them. Iterate over the file
1644 * descriptors instead, and use qemu_fd_getpagesize().
1646 static int find_min_backend_pagesize(Object
*obj
, void *opaque
)
1648 long *hpsize_min
= opaque
;
1650 if (object_dynamic_cast(obj
, TYPE_MEMORY_BACKEND
)) {
1651 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
1652 long hpsize
= host_memory_backend_pagesize(backend
);
1654 if (host_memory_backend_is_mapped(backend
) && (hpsize
< *hpsize_min
)) {
1655 *hpsize_min
= hpsize
;
1662 static int find_max_backend_pagesize(Object
*obj
, void *opaque
)
1664 long *hpsize_max
= opaque
;
1666 if (object_dynamic_cast(obj
, TYPE_MEMORY_BACKEND
)) {
1667 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
1668 long hpsize
= host_memory_backend_pagesize(backend
);
1670 if (host_memory_backend_is_mapped(backend
) && (hpsize
> *hpsize_max
)) {
1671 *hpsize_max
= hpsize
;
1679 * TODO: We assume right now that all mapped host memory backends are
1680 * used as RAM, however some might be used for different purposes.
1682 long qemu_minrampagesize(void)
1684 long hpsize
= LONG_MAX
;
1685 Object
*memdev_root
= object_resolve_path("/objects", NULL
);
1687 object_child_foreach(memdev_root
, find_min_backend_pagesize
, &hpsize
);
1691 long qemu_maxrampagesize(void)
1694 Object
*memdev_root
= object_resolve_path("/objects", NULL
);
1696 object_child_foreach(memdev_root
, find_max_backend_pagesize
, &pagesize
);
1700 long qemu_minrampagesize(void)
1702 return qemu_real_host_page_size
;
1704 long qemu_maxrampagesize(void)
1706 return qemu_real_host_page_size
;
1711 static int64_t get_file_size(int fd
)
1714 #if defined(__linux__)
1717 if (fstat(fd
, &st
) < 0) {
1721 /* Special handling for devdax character devices */
1722 if (S_ISCHR(st
.st_mode
)) {
1723 g_autofree
char *subsystem_path
= NULL
;
1724 g_autofree
char *subsystem
= NULL
;
1726 subsystem_path
= g_strdup_printf("/sys/dev/char/%d:%d/subsystem",
1727 major(st
.st_rdev
), minor(st
.st_rdev
));
1728 subsystem
= g_file_read_link(subsystem_path
, NULL
);
1730 if (subsystem
&& g_str_has_suffix(subsystem
, "/dax")) {
1731 g_autofree
char *size_path
= NULL
;
1732 g_autofree
char *size_str
= NULL
;
1734 size_path
= g_strdup_printf("/sys/dev/char/%d:%d/size",
1735 major(st
.st_rdev
), minor(st
.st_rdev
));
1737 if (g_file_get_contents(size_path
, &size_str
, NULL
, NULL
)) {
1738 return g_ascii_strtoll(size_str
, NULL
, 0);
1742 #endif /* defined(__linux__) */
1744 /* st.st_size may be zero for special files yet lseek(2) works */
1745 size
= lseek(fd
, 0, SEEK_END
);
1752 static int64_t get_file_align(int fd
)
1755 #if defined(__linux__) && defined(CONFIG_LIBDAXCTL)
1758 if (fstat(fd
, &st
) < 0) {
1762 /* Special handling for devdax character devices */
1763 if (S_ISCHR(st
.st_mode
)) {
1764 g_autofree
char *path
= NULL
;
1765 g_autofree
char *rpath
= NULL
;
1766 struct daxctl_ctx
*ctx
;
1767 struct daxctl_region
*region
;
1770 path
= g_strdup_printf("/sys/dev/char/%d:%d",
1771 major(st
.st_rdev
), minor(st
.st_rdev
));
1772 rpath
= realpath(path
, NULL
);
1774 rc
= daxctl_new(&ctx
);
1779 daxctl_region_foreach(ctx
, region
) {
1780 if (strstr(rpath
, daxctl_region_get_path(region
))) {
1781 align
= daxctl_region_get_align(region
);
1787 #endif /* defined(__linux__) && defined(CONFIG_LIBDAXCTL) */
1792 static int file_ram_open(const char *path
,
1793 const char *region_name
,
1798 char *sanitized_name
;
1804 fd
= open(path
, O_RDWR
);
1806 /* @path names an existing file, use it */
1809 if (errno
== ENOENT
) {
1810 /* @path names a file that doesn't exist, create it */
1811 fd
= open(path
, O_RDWR
| O_CREAT
| O_EXCL
, 0644);
1816 } else if (errno
== EISDIR
) {
1817 /* @path names a directory, create a file there */
1818 /* Make name safe to use with mkstemp by replacing '/' with '_'. */
1819 sanitized_name
= g_strdup(region_name
);
1820 for (c
= sanitized_name
; *c
!= '\0'; c
++) {
1826 filename
= g_strdup_printf("%s/qemu_back_mem.%s.XXXXXX", path
,
1828 g_free(sanitized_name
);
1830 fd
= mkstemp(filename
);
1838 if (errno
!= EEXIST
&& errno
!= EINTR
) {
1839 error_setg_errno(errp
, errno
,
1840 "can't open backing store %s for guest RAM",
1845 * Try again on EINTR and EEXIST. The latter happens when
1846 * something else creates the file between our two open().
1853 static void *file_ram_alloc(RAMBlock
*block
,
1861 block
->page_size
= qemu_fd_getpagesize(fd
);
1862 if (block
->mr
->align
% block
->page_size
) {
1863 error_setg(errp
, "alignment 0x%" PRIx64
1864 " must be multiples of page size 0x%zx",
1865 block
->mr
->align
, block
->page_size
);
1867 } else if (block
->mr
->align
&& !is_power_of_2(block
->mr
->align
)) {
1868 error_setg(errp
, "alignment 0x%" PRIx64
1869 " must be a power of two", block
->mr
->align
);
1872 block
->mr
->align
= MAX(block
->page_size
, block
->mr
->align
);
1873 #if defined(__s390x__)
1874 if (kvm_enabled()) {
1875 block
->mr
->align
= MAX(block
->mr
->align
, QEMU_VMALLOC_ALIGN
);
1879 if (memory
< block
->page_size
) {
1880 error_setg(errp
, "memory size 0x" RAM_ADDR_FMT
" must be equal to "
1881 "or larger than page size 0x%zx",
1882 memory
, block
->page_size
);
1886 memory
= ROUND_UP(memory
, block
->page_size
);
1889 * ftruncate is not supported by hugetlbfs in older
1890 * hosts, so don't bother bailing out on errors.
1891 * If anything goes wrong with it under other filesystems,
1894 * Do not truncate the non-empty backend file to avoid corrupting
1895 * the existing data in the file. Disabling shrinking is not
1896 * enough. For example, the current vNVDIMM implementation stores
1897 * the guest NVDIMM labels at the end of the backend file. If the
1898 * backend file is later extended, QEMU will not be able to find
1899 * those labels. Therefore, extending the non-empty backend file
1900 * is disabled as well.
1902 if (truncate
&& ftruncate(fd
, memory
)) {
1903 perror("ftruncate");
1906 area
= qemu_ram_mmap(fd
, memory
, block
->mr
->align
,
1907 block
->flags
& RAM_SHARED
, block
->flags
& RAM_PMEM
);
1908 if (area
== MAP_FAILED
) {
1909 error_setg_errno(errp
, errno
,
1910 "unable to map backing store for guest RAM");
1919 /* Allocate space within the ram_addr_t space that governs the
1921 * Called with the ramlist lock held.
1923 static ram_addr_t
find_ram_offset(ram_addr_t size
)
1925 RAMBlock
*block
, *next_block
;
1926 ram_addr_t offset
= RAM_ADDR_MAX
, mingap
= RAM_ADDR_MAX
;
1928 assert(size
!= 0); /* it would hand out same offset multiple times */
1930 if (QLIST_EMPTY_RCU(&ram_list
.blocks
)) {
1934 RAMBLOCK_FOREACH(block
) {
1935 ram_addr_t candidate
, next
= RAM_ADDR_MAX
;
1937 /* Align blocks to start on a 'long' in the bitmap
1938 * which makes the bitmap sync'ing take the fast path.
1940 candidate
= block
->offset
+ block
->max_length
;
1941 candidate
= ROUND_UP(candidate
, BITS_PER_LONG
<< TARGET_PAGE_BITS
);
1943 /* Search for the closest following block
1946 RAMBLOCK_FOREACH(next_block
) {
1947 if (next_block
->offset
>= candidate
) {
1948 next
= MIN(next
, next_block
->offset
);
1952 /* If it fits remember our place and remember the size
1953 * of gap, but keep going so that we might find a smaller
1954 * gap to fill so avoiding fragmentation.
1956 if (next
- candidate
>= size
&& next
- candidate
< mingap
) {
1958 mingap
= next
- candidate
;
1961 trace_find_ram_offset_loop(size
, candidate
, offset
, next
, mingap
);
1964 if (offset
== RAM_ADDR_MAX
) {
1965 fprintf(stderr
, "Failed to find gap of requested size: %" PRIu64
"\n",
1970 trace_find_ram_offset(size
, offset
);
1975 static unsigned long last_ram_page(void)
1978 ram_addr_t last
= 0;
1980 RCU_READ_LOCK_GUARD();
1981 RAMBLOCK_FOREACH(block
) {
1982 last
= MAX(last
, block
->offset
+ block
->max_length
);
1984 return last
>> TARGET_PAGE_BITS
;
1987 static void qemu_ram_setup_dump(void *addr
, ram_addr_t size
)
1991 /* Use MADV_DONTDUMP, if user doesn't want the guest memory in the core */
1992 if (!machine_dump_guest_core(current_machine
)) {
1993 ret
= qemu_madvise(addr
, size
, QEMU_MADV_DONTDUMP
);
1995 perror("qemu_madvise");
1996 fprintf(stderr
, "madvise doesn't support MADV_DONTDUMP, "
1997 "but dump_guest_core=off specified\n");
2002 const char *qemu_ram_get_idstr(RAMBlock
*rb
)
2007 void *qemu_ram_get_host_addr(RAMBlock
*rb
)
2012 ram_addr_t
qemu_ram_get_offset(RAMBlock
*rb
)
2017 ram_addr_t
qemu_ram_get_used_length(RAMBlock
*rb
)
2019 return rb
->used_length
;
2022 bool qemu_ram_is_shared(RAMBlock
*rb
)
2024 return rb
->flags
& RAM_SHARED
;
2027 /* Note: Only set at the start of postcopy */
2028 bool qemu_ram_is_uf_zeroable(RAMBlock
*rb
)
2030 return rb
->flags
& RAM_UF_ZEROPAGE
;
2033 void qemu_ram_set_uf_zeroable(RAMBlock
*rb
)
2035 rb
->flags
|= RAM_UF_ZEROPAGE
;
2038 bool qemu_ram_is_migratable(RAMBlock
*rb
)
2040 return rb
->flags
& RAM_MIGRATABLE
;
2043 void qemu_ram_set_migratable(RAMBlock
*rb
)
2045 rb
->flags
|= RAM_MIGRATABLE
;
2048 void qemu_ram_unset_migratable(RAMBlock
*rb
)
2050 rb
->flags
&= ~RAM_MIGRATABLE
;
2053 /* Called with iothread lock held. */
2054 void qemu_ram_set_idstr(RAMBlock
*new_block
, const char *name
, DeviceState
*dev
)
2059 assert(!new_block
->idstr
[0]);
2062 char *id
= qdev_get_dev_path(dev
);
2064 snprintf(new_block
->idstr
, sizeof(new_block
->idstr
), "%s/", id
);
2068 pstrcat(new_block
->idstr
, sizeof(new_block
->idstr
), name
);
2070 RCU_READ_LOCK_GUARD();
2071 RAMBLOCK_FOREACH(block
) {
2072 if (block
!= new_block
&&
2073 !strcmp(block
->idstr
, new_block
->idstr
)) {
2074 fprintf(stderr
, "RAMBlock \"%s\" already registered, abort!\n",
2081 /* Called with iothread lock held. */
2082 void qemu_ram_unset_idstr(RAMBlock
*block
)
2084 /* FIXME: arch_init.c assumes that this is not called throughout
2085 * migration. Ignore the problem since hot-unplug during migration
2086 * does not work anyway.
2089 memset(block
->idstr
, 0, sizeof(block
->idstr
));
2093 size_t qemu_ram_pagesize(RAMBlock
*rb
)
2095 return rb
->page_size
;
2098 /* Returns the largest size of page in use */
2099 size_t qemu_ram_pagesize_largest(void)
2104 RAMBLOCK_FOREACH(block
) {
2105 largest
= MAX(largest
, qemu_ram_pagesize(block
));
2111 static int memory_try_enable_merging(void *addr
, size_t len
)
2113 if (!machine_mem_merge(current_machine
)) {
2114 /* disabled by the user */
2118 return qemu_madvise(addr
, len
, QEMU_MADV_MERGEABLE
);
2121 /* Only legal before guest might have detected the memory size: e.g. on
2122 * incoming migration, or right after reset.
2124 * As memory core doesn't know how is memory accessed, it is up to
2125 * resize callback to update device state and/or add assertions to detect
2126 * misuse, if necessary.
2128 int qemu_ram_resize(RAMBlock
*block
, ram_addr_t newsize
, Error
**errp
)
2130 const ram_addr_t unaligned_size
= newsize
;
2134 newsize
= HOST_PAGE_ALIGN(newsize
);
2136 if (block
->used_length
== newsize
) {
2138 * We don't have to resize the ram block (which only knows aligned
2139 * sizes), however, we have to notify if the unaligned size changed.
2141 if (unaligned_size
!= memory_region_size(block
->mr
)) {
2142 memory_region_set_size(block
->mr
, unaligned_size
);
2143 if (block
->resized
) {
2144 block
->resized(block
->idstr
, unaligned_size
, block
->host
);
2150 if (!(block
->flags
& RAM_RESIZEABLE
)) {
2151 error_setg_errno(errp
, EINVAL
,
2152 "Length mismatch: %s: 0x" RAM_ADDR_FMT
2153 " in != 0x" RAM_ADDR_FMT
, block
->idstr
,
2154 newsize
, block
->used_length
);
2158 if (block
->max_length
< newsize
) {
2159 error_setg_errno(errp
, EINVAL
,
2160 "Length too large: %s: 0x" RAM_ADDR_FMT
2161 " > 0x" RAM_ADDR_FMT
, block
->idstr
,
2162 newsize
, block
->max_length
);
2166 cpu_physical_memory_clear_dirty_range(block
->offset
, block
->used_length
);
2167 block
->used_length
= newsize
;
2168 cpu_physical_memory_set_dirty_range(block
->offset
, block
->used_length
,
2170 memory_region_set_size(block
->mr
, unaligned_size
);
2171 if (block
->resized
) {
2172 block
->resized(block
->idstr
, unaligned_size
, block
->host
);
2178 * Trigger sync on the given ram block for range [start, start + length]
2179 * with the backing store if one is available.
2181 * @Note: this is supposed to be a synchronous op.
2183 void qemu_ram_msync(RAMBlock
*block
, ram_addr_t start
, ram_addr_t length
)
2185 /* The requested range should fit in within the block range */
2186 g_assert((start
+ length
) <= block
->used_length
);
2188 #ifdef CONFIG_LIBPMEM
2189 /* The lack of support for pmem should not block the sync */
2190 if (ramblock_is_pmem(block
)) {
2191 void *addr
= ramblock_ptr(block
, start
);
2192 pmem_persist(addr
, length
);
2196 if (block
->fd
>= 0) {
2198 * Case there is no support for PMEM or the memory has not been
2199 * specified as persistent (or is not one) - use the msync.
2200 * Less optimal but still achieves the same goal
2202 void *addr
= ramblock_ptr(block
, start
);
2203 if (qemu_msync(addr
, length
, block
->fd
)) {
2204 warn_report("%s: failed to sync memory range: start: "
2205 RAM_ADDR_FMT
" length: " RAM_ADDR_FMT
,
2206 __func__
, start
, length
);
2211 /* Called with ram_list.mutex held */
2212 static void dirty_memory_extend(ram_addr_t old_ram_size
,
2213 ram_addr_t new_ram_size
)
2215 ram_addr_t old_num_blocks
= DIV_ROUND_UP(old_ram_size
,
2216 DIRTY_MEMORY_BLOCK_SIZE
);
2217 ram_addr_t new_num_blocks
= DIV_ROUND_UP(new_ram_size
,
2218 DIRTY_MEMORY_BLOCK_SIZE
);
2221 /* Only need to extend if block count increased */
2222 if (new_num_blocks
<= old_num_blocks
) {
2226 for (i
= 0; i
< DIRTY_MEMORY_NUM
; i
++) {
2227 DirtyMemoryBlocks
*old_blocks
;
2228 DirtyMemoryBlocks
*new_blocks
;
2231 old_blocks
= atomic_rcu_read(&ram_list
.dirty_memory
[i
]);
2232 new_blocks
= g_malloc(sizeof(*new_blocks
) +
2233 sizeof(new_blocks
->blocks
[0]) * new_num_blocks
);
2235 if (old_num_blocks
) {
2236 memcpy(new_blocks
->blocks
, old_blocks
->blocks
,
2237 old_num_blocks
* sizeof(old_blocks
->blocks
[0]));
2240 for (j
= old_num_blocks
; j
< new_num_blocks
; j
++) {
2241 new_blocks
->blocks
[j
] = bitmap_new(DIRTY_MEMORY_BLOCK_SIZE
);
2244 atomic_rcu_set(&ram_list
.dirty_memory
[i
], new_blocks
);
2247 g_free_rcu(old_blocks
, rcu
);
2252 static void ram_block_add(RAMBlock
*new_block
, Error
**errp
, bool shared
)
2255 RAMBlock
*last_block
= NULL
;
2256 ram_addr_t old_ram_size
, new_ram_size
;
2259 old_ram_size
= last_ram_page();
2261 qemu_mutex_lock_ramlist();
2262 new_block
->offset
= find_ram_offset(new_block
->max_length
);
2264 if (!new_block
->host
) {
2265 if (xen_enabled()) {
2266 xen_ram_alloc(new_block
->offset
, new_block
->max_length
,
2267 new_block
->mr
, &err
);
2269 error_propagate(errp
, err
);
2270 qemu_mutex_unlock_ramlist();
2274 new_block
->host
= phys_mem_alloc(new_block
->max_length
,
2275 &new_block
->mr
->align
, shared
);
2276 if (!new_block
->host
) {
2277 error_setg_errno(errp
, errno
,
2278 "cannot set up guest memory '%s'",
2279 memory_region_name(new_block
->mr
));
2280 qemu_mutex_unlock_ramlist();
2283 memory_try_enable_merging(new_block
->host
, new_block
->max_length
);
2287 new_ram_size
= MAX(old_ram_size
,
2288 (new_block
->offset
+ new_block
->max_length
) >> TARGET_PAGE_BITS
);
2289 if (new_ram_size
> old_ram_size
) {
2290 dirty_memory_extend(old_ram_size
, new_ram_size
);
2292 /* Keep the list sorted from biggest to smallest block. Unlike QTAILQ,
2293 * QLIST (which has an RCU-friendly variant) does not have insertion at
2294 * tail, so save the last element in last_block.
2296 RAMBLOCK_FOREACH(block
) {
2298 if (block
->max_length
< new_block
->max_length
) {
2303 QLIST_INSERT_BEFORE_RCU(block
, new_block
, next
);
2304 } else if (last_block
) {
2305 QLIST_INSERT_AFTER_RCU(last_block
, new_block
, next
);
2306 } else { /* list is empty */
2307 QLIST_INSERT_HEAD_RCU(&ram_list
.blocks
, new_block
, next
);
2309 ram_list
.mru_block
= NULL
;
2311 /* Write list before version */
2314 qemu_mutex_unlock_ramlist();
2316 cpu_physical_memory_set_dirty_range(new_block
->offset
,
2317 new_block
->used_length
,
2320 if (new_block
->host
) {
2321 qemu_ram_setup_dump(new_block
->host
, new_block
->max_length
);
2322 qemu_madvise(new_block
->host
, new_block
->max_length
, QEMU_MADV_HUGEPAGE
);
2324 * MADV_DONTFORK is also needed by KVM in absence of synchronous MMU
2325 * Configure it unless the machine is a qtest server, in which case
2326 * KVM is not used and it may be forked (eg for fuzzing purposes).
2328 if (!qtest_enabled()) {
2329 qemu_madvise(new_block
->host
, new_block
->max_length
,
2330 QEMU_MADV_DONTFORK
);
2332 ram_block_notify_add(new_block
->host
, new_block
->max_length
);
2337 RAMBlock
*qemu_ram_alloc_from_fd(ram_addr_t size
, MemoryRegion
*mr
,
2338 uint32_t ram_flags
, int fd
,
2341 RAMBlock
*new_block
;
2342 Error
*local_err
= NULL
;
2343 int64_t file_size
, file_align
;
2345 /* Just support these ram flags by now. */
2346 assert((ram_flags
& ~(RAM_SHARED
| RAM_PMEM
)) == 0);
2348 if (xen_enabled()) {
2349 error_setg(errp
, "-mem-path not supported with Xen");
2353 if (kvm_enabled() && !kvm_has_sync_mmu()) {
2355 "host lacks kvm mmu notifiers, -mem-path unsupported");
2359 if (phys_mem_alloc
!= qemu_anon_ram_alloc
) {
2361 * file_ram_alloc() needs to allocate just like
2362 * phys_mem_alloc, but we haven't bothered to provide
2366 "-mem-path not supported with this accelerator");
2370 size
= HOST_PAGE_ALIGN(size
);
2371 file_size
= get_file_size(fd
);
2372 if (file_size
> 0 && file_size
< size
) {
2373 error_setg(errp
, "backing store size 0x%" PRIx64
2374 " does not match 'size' option 0x" RAM_ADDR_FMT
,
2379 file_align
= get_file_align(fd
);
2380 if (file_align
> 0 && mr
&& file_align
> mr
->align
) {
2381 error_setg(errp
, "backing store align 0x%" PRIx64
2382 " is larger than 'align' option 0x%" PRIx64
,
2383 file_align
, mr
->align
);
2387 new_block
= g_malloc0(sizeof(*new_block
));
2389 new_block
->used_length
= size
;
2390 new_block
->max_length
= size
;
2391 new_block
->flags
= ram_flags
;
2392 new_block
->host
= file_ram_alloc(new_block
, size
, fd
, !file_size
, errp
);
2393 if (!new_block
->host
) {
2398 ram_block_add(new_block
, &local_err
, ram_flags
& RAM_SHARED
);
2401 error_propagate(errp
, local_err
);
2409 RAMBlock
*qemu_ram_alloc_from_file(ram_addr_t size
, MemoryRegion
*mr
,
2410 uint32_t ram_flags
, const char *mem_path
,
2417 fd
= file_ram_open(mem_path
, memory_region_name(mr
), &created
, errp
);
2422 block
= qemu_ram_alloc_from_fd(size
, mr
, ram_flags
, fd
, errp
);
2436 RAMBlock
*qemu_ram_alloc_internal(ram_addr_t size
, ram_addr_t max_size
,
2437 void (*resized
)(const char*,
2440 void *host
, bool resizeable
, bool share
,
2441 MemoryRegion
*mr
, Error
**errp
)
2443 RAMBlock
*new_block
;
2444 Error
*local_err
= NULL
;
2446 size
= HOST_PAGE_ALIGN(size
);
2447 max_size
= HOST_PAGE_ALIGN(max_size
);
2448 new_block
= g_malloc0(sizeof(*new_block
));
2450 new_block
->resized
= resized
;
2451 new_block
->used_length
= size
;
2452 new_block
->max_length
= max_size
;
2453 assert(max_size
>= size
);
2455 new_block
->page_size
= qemu_real_host_page_size
;
2456 new_block
->host
= host
;
2458 new_block
->flags
|= RAM_PREALLOC
;
2461 new_block
->flags
|= RAM_RESIZEABLE
;
2463 ram_block_add(new_block
, &local_err
, share
);
2466 error_propagate(errp
, local_err
);
2472 RAMBlock
*qemu_ram_alloc_from_ptr(ram_addr_t size
, void *host
,
2473 MemoryRegion
*mr
, Error
**errp
)
2475 return qemu_ram_alloc_internal(size
, size
, NULL
, host
, false,
2479 RAMBlock
*qemu_ram_alloc(ram_addr_t size
, bool share
,
2480 MemoryRegion
*mr
, Error
**errp
)
2482 return qemu_ram_alloc_internal(size
, size
, NULL
, NULL
, false,
2486 RAMBlock
*qemu_ram_alloc_resizeable(ram_addr_t size
, ram_addr_t maxsz
,
2487 void (*resized
)(const char*,
2490 MemoryRegion
*mr
, Error
**errp
)
2492 return qemu_ram_alloc_internal(size
, maxsz
, resized
, NULL
, true,
2496 static void reclaim_ramblock(RAMBlock
*block
)
2498 if (block
->flags
& RAM_PREALLOC
) {
2500 } else if (xen_enabled()) {
2501 xen_invalidate_map_cache_entry(block
->host
);
2503 } else if (block
->fd
>= 0) {
2504 qemu_ram_munmap(block
->fd
, block
->host
, block
->max_length
);
2508 qemu_anon_ram_free(block
->host
, block
->max_length
);
2513 void qemu_ram_free(RAMBlock
*block
)
2520 ram_block_notify_remove(block
->host
, block
->max_length
);
2523 qemu_mutex_lock_ramlist();
2524 QLIST_REMOVE_RCU(block
, next
);
2525 ram_list
.mru_block
= NULL
;
2526 /* Write list before version */
2529 call_rcu(block
, reclaim_ramblock
, rcu
);
2530 qemu_mutex_unlock_ramlist();
2534 void qemu_ram_remap(ram_addr_t addr
, ram_addr_t length
)
2541 RAMBLOCK_FOREACH(block
) {
2542 offset
= addr
- block
->offset
;
2543 if (offset
< block
->max_length
) {
2544 vaddr
= ramblock_ptr(block
, offset
);
2545 if (block
->flags
& RAM_PREALLOC
) {
2547 } else if (xen_enabled()) {
2551 if (block
->fd
>= 0) {
2552 flags
|= (block
->flags
& RAM_SHARED
?
2553 MAP_SHARED
: MAP_PRIVATE
);
2554 area
= mmap(vaddr
, length
, PROT_READ
| PROT_WRITE
,
2555 flags
, block
->fd
, offset
);
2558 * Remap needs to match alloc. Accelerators that
2559 * set phys_mem_alloc never remap. If they did,
2560 * we'd need a remap hook here.
2562 assert(phys_mem_alloc
== qemu_anon_ram_alloc
);
2564 flags
|= MAP_PRIVATE
| MAP_ANONYMOUS
;
2565 area
= mmap(vaddr
, length
, PROT_READ
| PROT_WRITE
,
2568 if (area
!= vaddr
) {
2569 error_report("Could not remap addr: "
2570 RAM_ADDR_FMT
"@" RAM_ADDR_FMT
"",
2574 memory_try_enable_merging(vaddr
, length
);
2575 qemu_ram_setup_dump(vaddr
, length
);
2580 #endif /* !_WIN32 */
2582 /* Return a host pointer to ram allocated with qemu_ram_alloc.
2583 * This should not be used for general purpose DMA. Use address_space_map
2584 * or address_space_rw instead. For local memory (e.g. video ram) that the
2585 * device owns, use memory_region_get_ram_ptr.
2587 * Called within RCU critical section.
2589 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
)
2591 RAMBlock
*block
= ram_block
;
2593 if (block
== NULL
) {
2594 block
= qemu_get_ram_block(addr
);
2595 addr
-= block
->offset
;
2598 if (xen_enabled() && block
->host
== NULL
) {
2599 /* We need to check if the requested address is in the RAM
2600 * because we don't want to map the entire memory in QEMU.
2601 * In that case just map until the end of the page.
2603 if (block
->offset
== 0) {
2604 return xen_map_cache(addr
, 0, 0, false);
2607 block
->host
= xen_map_cache(block
->offset
, block
->max_length
, 1, false);
2609 return ramblock_ptr(block
, addr
);
2612 /* Return a host pointer to guest's ram. Similar to qemu_map_ram_ptr
2613 * but takes a size argument.
2615 * Called within RCU critical section.
2617 static void *qemu_ram_ptr_length(RAMBlock
*ram_block
, ram_addr_t addr
,
2618 hwaddr
*size
, bool lock
)
2620 RAMBlock
*block
= ram_block
;
2625 if (block
== NULL
) {
2626 block
= qemu_get_ram_block(addr
);
2627 addr
-= block
->offset
;
2629 *size
= MIN(*size
, block
->max_length
- addr
);
2631 if (xen_enabled() && block
->host
== NULL
) {
2632 /* We need to check if the requested address is in the RAM
2633 * because we don't want to map the entire memory in QEMU.
2634 * In that case just map the requested area.
2636 if (block
->offset
== 0) {
2637 return xen_map_cache(addr
, *size
, lock
, lock
);
2640 block
->host
= xen_map_cache(block
->offset
, block
->max_length
, 1, lock
);
2643 return ramblock_ptr(block
, addr
);
2646 /* Return the offset of a hostpointer within a ramblock */
2647 ram_addr_t
qemu_ram_block_host_offset(RAMBlock
*rb
, void *host
)
2649 ram_addr_t res
= (uint8_t *)host
- (uint8_t *)rb
->host
;
2650 assert((uintptr_t)host
>= (uintptr_t)rb
->host
);
2651 assert(res
< rb
->max_length
);
2657 * Translates a host ptr back to a RAMBlock, a ram_addr and an offset
2660 * ptr: Host pointer to look up
2661 * round_offset: If true round the result offset down to a page boundary
2662 * *ram_addr: set to result ram_addr
2663 * *offset: set to result offset within the RAMBlock
2665 * Returns: RAMBlock (or NULL if not found)
2667 * By the time this function returns, the returned pointer is not protected
2668 * by RCU anymore. If the caller is not within an RCU critical section and
2669 * does not hold the iothread lock, it must have other means of protecting the
2670 * pointer, such as a reference to the region that includes the incoming
2673 RAMBlock
*qemu_ram_block_from_host(void *ptr
, bool round_offset
,
2677 uint8_t *host
= ptr
;
2679 if (xen_enabled()) {
2680 ram_addr_t ram_addr
;
2681 RCU_READ_LOCK_GUARD();
2682 ram_addr
= xen_ram_addr_from_mapcache(ptr
);
2683 block
= qemu_get_ram_block(ram_addr
);
2685 *offset
= ram_addr
- block
->offset
;
2690 RCU_READ_LOCK_GUARD();
2691 block
= atomic_rcu_read(&ram_list
.mru_block
);
2692 if (block
&& block
->host
&& host
- block
->host
< block
->max_length
) {
2696 RAMBLOCK_FOREACH(block
) {
2697 /* This case append when the block is not mapped. */
2698 if (block
->host
== NULL
) {
2701 if (host
- block
->host
< block
->max_length
) {
2709 *offset
= (host
- block
->host
);
2711 *offset
&= TARGET_PAGE_MASK
;
2717 * Finds the named RAMBlock
2719 * name: The name of RAMBlock to find
2721 * Returns: RAMBlock (or NULL if not found)
2723 RAMBlock
*qemu_ram_block_by_name(const char *name
)
2727 RAMBLOCK_FOREACH(block
) {
2728 if (!strcmp(name
, block
->idstr
)) {
2736 /* Some of the softmmu routines need to translate from a host pointer
2737 (typically a TLB entry) back to a ram offset. */
2738 ram_addr_t
qemu_ram_addr_from_host(void *ptr
)
2743 block
= qemu_ram_block_from_host(ptr
, false, &offset
);
2745 return RAM_ADDR_INVALID
;
2748 return block
->offset
+ offset
;
2751 /* Generate a debug exception if a watchpoint has been hit. */
2752 void cpu_check_watchpoint(CPUState
*cpu
, vaddr addr
, vaddr len
,
2753 MemTxAttrs attrs
, int flags
, uintptr_t ra
)
2755 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
2758 assert(tcg_enabled());
2759 if (cpu
->watchpoint_hit
) {
2761 * We re-entered the check after replacing the TB.
2762 * Now raise the debug interrupt so that it will
2763 * trigger after the current instruction.
2765 qemu_mutex_lock_iothread();
2766 cpu_interrupt(cpu
, CPU_INTERRUPT_DEBUG
);
2767 qemu_mutex_unlock_iothread();
2771 addr
= cc
->adjust_watchpoint_address(cpu
, addr
, len
);
2772 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
2773 if (watchpoint_address_matches(wp
, addr
, len
)
2774 && (wp
->flags
& flags
)) {
2775 if (flags
== BP_MEM_READ
) {
2776 wp
->flags
|= BP_WATCHPOINT_HIT_READ
;
2778 wp
->flags
|= BP_WATCHPOINT_HIT_WRITE
;
2780 wp
->hitaddr
= MAX(addr
, wp
->vaddr
);
2781 wp
->hitattrs
= attrs
;
2782 if (!cpu
->watchpoint_hit
) {
2783 if (wp
->flags
& BP_CPU
&&
2784 !cc
->debug_check_watchpoint(cpu
, wp
)) {
2785 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
2788 cpu
->watchpoint_hit
= wp
;
2791 tb_check_watchpoint(cpu
, ra
);
2792 if (wp
->flags
& BP_STOP_BEFORE_ACCESS
) {
2793 cpu
->exception_index
= EXCP_DEBUG
;
2795 cpu_loop_exit_restore(cpu
, ra
);
2797 /* Force execution of one insn next time. */
2798 cpu
->cflags_next_tb
= 1 | curr_cflags();
2801 cpu_restore_state(cpu
, ra
, true);
2803 cpu_loop_exit_noexc(cpu
);
2807 wp
->flags
&= ~BP_WATCHPOINT_HIT
;
2812 static MemTxResult
flatview_read(FlatView
*fv
, hwaddr addr
,
2813 MemTxAttrs attrs
, void *buf
, hwaddr len
);
2814 static MemTxResult
flatview_write(FlatView
*fv
, hwaddr addr
, MemTxAttrs attrs
,
2815 const void *buf
, hwaddr len
);
2816 static bool flatview_access_valid(FlatView
*fv
, hwaddr addr
, hwaddr len
,
2817 bool is_write
, MemTxAttrs attrs
);
2819 static MemTxResult
subpage_read(void *opaque
, hwaddr addr
, uint64_t *data
,
2820 unsigned len
, MemTxAttrs attrs
)
2822 subpage_t
*subpage
= opaque
;
2826 #if defined(DEBUG_SUBPAGE)
2827 printf("%s: subpage %p len %u addr " TARGET_FMT_plx
"\n", __func__
,
2828 subpage
, len
, addr
);
2830 res
= flatview_read(subpage
->fv
, addr
+ subpage
->base
, attrs
, buf
, len
);
2834 *data
= ldn_p(buf
, len
);
2838 static MemTxResult
subpage_write(void *opaque
, hwaddr addr
,
2839 uint64_t value
, unsigned len
, MemTxAttrs attrs
)
2841 subpage_t
*subpage
= opaque
;
2844 #if defined(DEBUG_SUBPAGE)
2845 printf("%s: subpage %p len %u addr " TARGET_FMT_plx
2846 " value %"PRIx64
"\n",
2847 __func__
, subpage
, len
, addr
, value
);
2849 stn_p(buf
, len
, value
);
2850 return flatview_write(subpage
->fv
, addr
+ subpage
->base
, attrs
, buf
, len
);
2853 static bool subpage_accepts(void *opaque
, hwaddr addr
,
2854 unsigned len
, bool is_write
,
2857 subpage_t
*subpage
= opaque
;
2858 #if defined(DEBUG_SUBPAGE)
2859 printf("%s: subpage %p %c len %u addr " TARGET_FMT_plx
"\n",
2860 __func__
, subpage
, is_write
? 'w' : 'r', len
, addr
);
2863 return flatview_access_valid(subpage
->fv
, addr
+ subpage
->base
,
2864 len
, is_write
, attrs
);
2867 static const MemoryRegionOps subpage_ops
= {
2868 .read_with_attrs
= subpage_read
,
2869 .write_with_attrs
= subpage_write
,
2870 .impl
.min_access_size
= 1,
2871 .impl
.max_access_size
= 8,
2872 .valid
.min_access_size
= 1,
2873 .valid
.max_access_size
= 8,
2874 .valid
.accepts
= subpage_accepts
,
2875 .endianness
= DEVICE_NATIVE_ENDIAN
,
2878 static int subpage_register(subpage_t
*mmio
, uint32_t start
, uint32_t end
,
2883 if (start
>= TARGET_PAGE_SIZE
|| end
>= TARGET_PAGE_SIZE
)
2885 idx
= SUBPAGE_IDX(start
);
2886 eidx
= SUBPAGE_IDX(end
);
2887 #if defined(DEBUG_SUBPAGE)
2888 printf("%s: %p start %08x end %08x idx %08x eidx %08x section %d\n",
2889 __func__
, mmio
, start
, end
, idx
, eidx
, section
);
2891 for (; idx
<= eidx
; idx
++) {
2892 mmio
->sub_section
[idx
] = section
;
2898 static subpage_t
*subpage_init(FlatView
*fv
, hwaddr base
)
2902 /* mmio->sub_section is set to PHYS_SECTION_UNASSIGNED with g_malloc0 */
2903 mmio
= g_malloc0(sizeof(subpage_t
) + TARGET_PAGE_SIZE
* sizeof(uint16_t));
2906 memory_region_init_io(&mmio
->iomem
, NULL
, &subpage_ops
, mmio
,
2907 NULL
, TARGET_PAGE_SIZE
);
2908 mmio
->iomem
.subpage
= true;
2909 #if defined(DEBUG_SUBPAGE)
2910 printf("%s: %p base " TARGET_FMT_plx
" len %08x\n", __func__
,
2911 mmio
, base
, TARGET_PAGE_SIZE
);
2917 static uint16_t dummy_section(PhysPageMap
*map
, FlatView
*fv
, MemoryRegion
*mr
)
2920 MemoryRegionSection section
= {
2923 .offset_within_address_space
= 0,
2924 .offset_within_region
= 0,
2925 .size
= int128_2_64(),
2928 return phys_section_add(map
, §ion
);
2931 MemoryRegionSection
*iotlb_to_section(CPUState
*cpu
,
2932 hwaddr index
, MemTxAttrs attrs
)
2934 int asidx
= cpu_asidx_from_attrs(cpu
, attrs
);
2935 CPUAddressSpace
*cpuas
= &cpu
->cpu_ases
[asidx
];
2936 AddressSpaceDispatch
*d
= atomic_rcu_read(&cpuas
->memory_dispatch
);
2937 MemoryRegionSection
*sections
= d
->map
.sections
;
2939 return §ions
[index
& ~TARGET_PAGE_MASK
];
2942 static void io_mem_init(void)
2944 memory_region_init_io(&io_mem_unassigned
, NULL
, &unassigned_mem_ops
, NULL
,
2948 AddressSpaceDispatch
*address_space_dispatch_new(FlatView
*fv
)
2950 AddressSpaceDispatch
*d
= g_new0(AddressSpaceDispatch
, 1);
2953 n
= dummy_section(&d
->map
, fv
, &io_mem_unassigned
);
2954 assert(n
== PHYS_SECTION_UNASSIGNED
);
2956 d
->phys_map
= (PhysPageEntry
) { .ptr
= PHYS_MAP_NODE_NIL
, .skip
= 1 };
2961 void address_space_dispatch_free(AddressSpaceDispatch
*d
)
2963 phys_sections_free(&d
->map
);
2967 static void do_nothing(CPUState
*cpu
, run_on_cpu_data d
)
2971 static void tcg_log_global_after_sync(MemoryListener
*listener
)
2973 CPUAddressSpace
*cpuas
;
2975 /* Wait for the CPU to end the current TB. This avoids the following
2979 * ---------------------- -------------------------
2980 * TLB check -> slow path
2981 * notdirty_mem_write
2985 * TLB check -> fast path
2989 * by pushing the migration thread's memory read after the vCPU thread has
2990 * written the memory.
2992 if (replay_mode
== REPLAY_MODE_NONE
) {
2994 * VGA can make calls to this function while updating the screen.
2995 * In record/replay mode this causes a deadlock, because
2996 * run_on_cpu waits for rr mutex. Therefore no races are possible
2997 * in this case and no need for making run_on_cpu when
2998 * record/replay is not enabled.
3000 cpuas
= container_of(listener
, CPUAddressSpace
, tcg_as_listener
);
3001 run_on_cpu(cpuas
->cpu
, do_nothing
, RUN_ON_CPU_NULL
);
3005 static void tcg_commit(MemoryListener
*listener
)
3007 CPUAddressSpace
*cpuas
;
3008 AddressSpaceDispatch
*d
;
3010 assert(tcg_enabled());
3011 /* since each CPU stores ram addresses in its TLB cache, we must
3012 reset the modified entries */
3013 cpuas
= container_of(listener
, CPUAddressSpace
, tcg_as_listener
);
3014 cpu_reloading_memory_map();
3015 /* The CPU and TLB are protected by the iothread lock.
3016 * We reload the dispatch pointer now because cpu_reloading_memory_map()
3017 * may have split the RCU critical section.
3019 d
= address_space_to_dispatch(cpuas
->as
);
3020 atomic_rcu_set(&cpuas
->memory_dispatch
, d
);
3021 tlb_flush(cpuas
->cpu
);
3024 static void memory_map_init(void)
3026 system_memory
= g_malloc(sizeof(*system_memory
));
3028 memory_region_init(system_memory
, NULL
, "system", UINT64_MAX
);
3029 address_space_init(&address_space_memory
, system_memory
, "memory");
3031 system_io
= g_malloc(sizeof(*system_io
));
3032 memory_region_init_io(system_io
, NULL
, &unassigned_io_ops
, NULL
, "io",
3034 address_space_init(&address_space_io
, system_io
, "I/O");
3037 MemoryRegion
*get_system_memory(void)
3039 return system_memory
;
3042 MemoryRegion
*get_system_io(void)
3047 #endif /* !defined(CONFIG_USER_ONLY) */
3049 /* physical memory access (slow version, mainly for debug) */
3050 #if defined(CONFIG_USER_ONLY)
3051 int cpu_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
3052 void *ptr
, target_ulong len
, bool is_write
)
3055 target_ulong l
, page
;
3060 page
= addr
& TARGET_PAGE_MASK
;
3061 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3064 flags
= page_get_flags(page
);
3065 if (!(flags
& PAGE_VALID
))
3068 if (!(flags
& PAGE_WRITE
))
3070 /* XXX: this code should not depend on lock_user */
3071 if (!(p
= lock_user(VERIFY_WRITE
, addr
, l
, 0)))
3074 unlock_user(p
, addr
, l
);
3076 if (!(flags
& PAGE_READ
))
3078 /* XXX: this code should not depend on lock_user */
3079 if (!(p
= lock_user(VERIFY_READ
, addr
, l
, 1)))
3082 unlock_user(p
, addr
, 0);
3093 static void invalidate_and_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
3096 uint8_t dirty_log_mask
= memory_region_get_dirty_log_mask(mr
);
3097 addr
+= memory_region_get_ram_addr(mr
);
3099 /* No early return if dirty_log_mask is or becomes 0, because
3100 * cpu_physical_memory_set_dirty_range will still call
3101 * xen_modified_memory.
3103 if (dirty_log_mask
) {
3105 cpu_physical_memory_range_includes_clean(addr
, length
, dirty_log_mask
);
3107 if (dirty_log_mask
& (1 << DIRTY_MEMORY_CODE
)) {
3108 assert(tcg_enabled());
3109 tb_invalidate_phys_range(addr
, addr
+ length
);
3110 dirty_log_mask
&= ~(1 << DIRTY_MEMORY_CODE
);
3112 cpu_physical_memory_set_dirty_range(addr
, length
, dirty_log_mask
);
3115 void memory_region_flush_rom_device(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
)
3118 * In principle this function would work on other memory region types too,
3119 * but the ROM device use case is the only one where this operation is
3120 * necessary. Other memory regions should use the
3121 * address_space_read/write() APIs.
3123 assert(memory_region_is_romd(mr
));
3125 invalidate_and_set_dirty(mr
, addr
, size
);
3128 static int memory_access_size(MemoryRegion
*mr
, unsigned l
, hwaddr addr
)
3130 unsigned access_size_max
= mr
->ops
->valid
.max_access_size
;
3132 /* Regions are assumed to support 1-4 byte accesses unless
3133 otherwise specified. */
3134 if (access_size_max
== 0) {
3135 access_size_max
= 4;
3138 /* Bound the maximum access by the alignment of the address. */
3139 if (!mr
->ops
->impl
.unaligned
) {
3140 unsigned align_size_max
= addr
& -addr
;
3141 if (align_size_max
!= 0 && align_size_max
< access_size_max
) {
3142 access_size_max
= align_size_max
;
3146 /* Don't attempt accesses larger than the maximum. */
3147 if (l
> access_size_max
) {
3148 l
= access_size_max
;
3155 static bool prepare_mmio_access(MemoryRegion
*mr
)
3157 bool unlocked
= !qemu_mutex_iothread_locked();
3158 bool release_lock
= false;
3160 if (unlocked
&& mr
->global_locking
) {
3161 qemu_mutex_lock_iothread();
3163 release_lock
= true;
3165 if (mr
->flush_coalesced_mmio
) {
3167 qemu_mutex_lock_iothread();
3169 qemu_flush_coalesced_mmio_buffer();
3171 qemu_mutex_unlock_iothread();
3175 return release_lock
;
3178 /* Called within RCU critical section. */
3179 static MemTxResult
flatview_write_continue(FlatView
*fv
, hwaddr addr
,
3182 hwaddr len
, hwaddr addr1
,
3183 hwaddr l
, MemoryRegion
*mr
)
3187 MemTxResult result
= MEMTX_OK
;
3188 bool release_lock
= false;
3189 const uint8_t *buf
= ptr
;
3192 if (!memory_access_is_direct(mr
, true)) {
3193 release_lock
|= prepare_mmio_access(mr
);
3194 l
= memory_access_size(mr
, l
, addr1
);
3195 /* XXX: could force current_cpu to NULL to avoid
3197 val
= ldn_he_p(buf
, l
);
3198 result
|= memory_region_dispatch_write(mr
, addr1
, val
,
3199 size_memop(l
), attrs
);
3202 ram_ptr
= qemu_ram_ptr_length(mr
->ram_block
, addr1
, &l
, false);
3203 memcpy(ram_ptr
, buf
, l
);
3204 invalidate_and_set_dirty(mr
, addr1
, l
);
3208 qemu_mutex_unlock_iothread();
3209 release_lock
= false;
3221 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, true, attrs
);
3227 /* Called from RCU critical section. */
3228 static MemTxResult
flatview_write(FlatView
*fv
, hwaddr addr
, MemTxAttrs attrs
,
3229 const void *buf
, hwaddr len
)
3234 MemTxResult result
= MEMTX_OK
;
3237 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, true, attrs
);
3238 result
= flatview_write_continue(fv
, addr
, attrs
, buf
, len
,
3244 /* Called within RCU critical section. */
3245 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
3246 MemTxAttrs attrs
, void *ptr
,
3247 hwaddr len
, hwaddr addr1
, hwaddr l
,
3252 MemTxResult result
= MEMTX_OK
;
3253 bool release_lock
= false;
3257 if (!memory_access_is_direct(mr
, false)) {
3259 release_lock
|= prepare_mmio_access(mr
);
3260 l
= memory_access_size(mr
, l
, addr1
);
3261 result
|= memory_region_dispatch_read(mr
, addr1
, &val
,
3262 size_memop(l
), attrs
);
3263 stn_he_p(buf
, l
, val
);
3266 ram_ptr
= qemu_ram_ptr_length(mr
->ram_block
, addr1
, &l
, false);
3267 memcpy(buf
, ram_ptr
, l
);
3271 qemu_mutex_unlock_iothread();
3272 release_lock
= false;
3284 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
3290 /* Called from RCU critical section. */
3291 static MemTxResult
flatview_read(FlatView
*fv
, hwaddr addr
,
3292 MemTxAttrs attrs
, void *buf
, hwaddr len
)
3299 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
3300 return flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
3304 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
3305 MemTxAttrs attrs
, void *buf
, hwaddr len
)
3307 MemTxResult result
= MEMTX_OK
;
3311 RCU_READ_LOCK_GUARD();
3312 fv
= address_space_to_flatview(as
);
3313 result
= flatview_read(fv
, addr
, attrs
, buf
, len
);
3319 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
3321 const void *buf
, hwaddr len
)
3323 MemTxResult result
= MEMTX_OK
;
3327 RCU_READ_LOCK_GUARD();
3328 fv
= address_space_to_flatview(as
);
3329 result
= flatview_write(fv
, addr
, attrs
, buf
, len
);
3335 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
3336 void *buf
, hwaddr len
, bool is_write
)
3339 return address_space_write(as
, addr
, attrs
, buf
, len
);
3341 return address_space_read_full(as
, addr
, attrs
, buf
, len
);
3345 void cpu_physical_memory_rw(hwaddr addr
, void *buf
,
3346 hwaddr len
, bool is_write
)
3348 address_space_rw(&address_space_memory
, addr
, MEMTXATTRS_UNSPECIFIED
,
3349 buf
, len
, is_write
);
3352 enum write_rom_type
{
3357 static inline MemTxResult
address_space_write_rom_internal(AddressSpace
*as
,
3362 enum write_rom_type type
)
3368 const uint8_t *buf
= ptr
;
3370 RCU_READ_LOCK_GUARD();
3373 mr
= address_space_translate(as
, addr
, &addr1
, &l
, true, attrs
);
3375 if (!(memory_region_is_ram(mr
) ||
3376 memory_region_is_romd(mr
))) {
3377 l
= memory_access_size(mr
, l
, addr1
);
3380 ram_ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
3383 memcpy(ram_ptr
, buf
, l
);
3384 invalidate_and_set_dirty(mr
, addr1
, l
);
3387 flush_icache_range((uintptr_t)ram_ptr
, (uintptr_t)ram_ptr
+ l
);
3398 /* used for ROM loading : can write in RAM and ROM */
3399 MemTxResult
address_space_write_rom(AddressSpace
*as
, hwaddr addr
,
3401 const void *buf
, hwaddr len
)
3403 return address_space_write_rom_internal(as
, addr
, attrs
,
3404 buf
, len
, WRITE_DATA
);
3407 void cpu_flush_icache_range(hwaddr start
, hwaddr len
)
3410 * This function should do the same thing as an icache flush that was
3411 * triggered from within the guest. For TCG we are always cache coherent,
3412 * so there is no need to flush anything. For KVM / Xen we need to flush
3413 * the host's instruction cache at least.
3415 if (tcg_enabled()) {
3419 address_space_write_rom_internal(&address_space_memory
,
3420 start
, MEMTXATTRS_UNSPECIFIED
,
3421 NULL
, len
, FLUSH_CACHE
);
3432 static BounceBuffer bounce
;
3434 typedef struct MapClient
{
3436 QLIST_ENTRY(MapClient
) link
;
3439 QemuMutex map_client_list_lock
;
3440 static QLIST_HEAD(, MapClient
) map_client_list
3441 = QLIST_HEAD_INITIALIZER(map_client_list
);
3443 static void cpu_unregister_map_client_do(MapClient
*client
)
3445 QLIST_REMOVE(client
, link
);
3449 static void cpu_notify_map_clients_locked(void)
3453 while (!QLIST_EMPTY(&map_client_list
)) {
3454 client
= QLIST_FIRST(&map_client_list
);
3455 qemu_bh_schedule(client
->bh
);
3456 cpu_unregister_map_client_do(client
);
3460 void cpu_register_map_client(QEMUBH
*bh
)
3462 MapClient
*client
= g_malloc(sizeof(*client
));
3464 qemu_mutex_lock(&map_client_list_lock
);
3466 QLIST_INSERT_HEAD(&map_client_list
, client
, link
);
3467 if (!atomic_read(&bounce
.in_use
)) {
3468 cpu_notify_map_clients_locked();
3470 qemu_mutex_unlock(&map_client_list_lock
);
3473 void cpu_exec_init_all(void)
3475 qemu_mutex_init(&ram_list
.mutex
);
3476 /* The data structures we set up here depend on knowing the page size,
3477 * so no more changes can be made after this point.
3478 * In an ideal world, nothing we did before we had finished the
3479 * machine setup would care about the target page size, and we could
3480 * do this much later, rather than requiring board models to state
3481 * up front what their requirements are.
3483 finalize_target_page_bits();
3486 qemu_mutex_init(&map_client_list_lock
);
3489 void cpu_unregister_map_client(QEMUBH
*bh
)
3493 qemu_mutex_lock(&map_client_list_lock
);
3494 QLIST_FOREACH(client
, &map_client_list
, link
) {
3495 if (client
->bh
== bh
) {
3496 cpu_unregister_map_client_do(client
);
3500 qemu_mutex_unlock(&map_client_list_lock
);
3503 static void cpu_notify_map_clients(void)
3505 qemu_mutex_lock(&map_client_list_lock
);
3506 cpu_notify_map_clients_locked();
3507 qemu_mutex_unlock(&map_client_list_lock
);
3510 static bool flatview_access_valid(FlatView
*fv
, hwaddr addr
, hwaddr len
,
3511 bool is_write
, MemTxAttrs attrs
)
3518 mr
= flatview_translate(fv
, addr
, &xlat
, &l
, is_write
, attrs
);
3519 if (!memory_access_is_direct(mr
, is_write
)) {
3520 l
= memory_access_size(mr
, l
, addr
);
3521 if (!memory_region_access_valid(mr
, xlat
, l
, is_write
, attrs
)) {
3532 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
,
3533 hwaddr len
, bool is_write
,
3539 RCU_READ_LOCK_GUARD();
3540 fv
= address_space_to_flatview(as
);
3541 result
= flatview_access_valid(fv
, addr
, len
, is_write
, attrs
);
3546 flatview_extend_translation(FlatView
*fv
, hwaddr addr
,
3548 MemoryRegion
*mr
, hwaddr base
, hwaddr len
,
3549 bool is_write
, MemTxAttrs attrs
)
3553 MemoryRegion
*this_mr
;
3559 if (target_len
== 0) {
3564 this_mr
= flatview_translate(fv
, addr
, &xlat
,
3565 &len
, is_write
, attrs
);
3566 if (this_mr
!= mr
|| xlat
!= base
+ done
) {
3572 /* Map a physical memory region into a host virtual address.
3573 * May map a subset of the requested range, given by and returned in *plen.
3574 * May return NULL if resources needed to perform the mapping are exhausted.
3575 * Use only for reads OR writes - not for read-modify-write operations.
3576 * Use cpu_register_map_client() to know when retrying the map operation is
3577 * likely to succeed.
3579 void *address_space_map(AddressSpace
*as
,
3596 RCU_READ_LOCK_GUARD();
3597 fv
= address_space_to_flatview(as
);
3598 mr
= flatview_translate(fv
, addr
, &xlat
, &l
, is_write
, attrs
);
3600 if (!memory_access_is_direct(mr
, is_write
)) {
3601 if (atomic_xchg(&bounce
.in_use
, true)) {
3605 /* Avoid unbounded allocations */
3606 l
= MIN(l
, TARGET_PAGE_SIZE
);
3607 bounce
.buffer
= qemu_memalign(TARGET_PAGE_SIZE
, l
);
3611 memory_region_ref(mr
);
3614 flatview_read(fv
, addr
, MEMTXATTRS_UNSPECIFIED
,
3619 return bounce
.buffer
;
3623 memory_region_ref(mr
);
3624 *plen
= flatview_extend_translation(fv
, addr
, len
, mr
, xlat
,
3625 l
, is_write
, attrs
);
3626 ptr
= qemu_ram_ptr_length(mr
->ram_block
, xlat
, plen
, true);
3631 /* Unmaps a memory region previously mapped by address_space_map().
3632 * Will also mark the memory as dirty if is_write is true. access_len gives
3633 * the amount of memory that was actually read or written by the caller.
3635 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
3636 bool is_write
, hwaddr access_len
)
3638 if (buffer
!= bounce
.buffer
) {
3642 mr
= memory_region_from_host(buffer
, &addr1
);
3645 invalidate_and_set_dirty(mr
, addr1
, access_len
);
3647 if (xen_enabled()) {
3648 xen_invalidate_map_cache_entry(buffer
);
3650 memory_region_unref(mr
);
3654 address_space_write(as
, bounce
.addr
, MEMTXATTRS_UNSPECIFIED
,
3655 bounce
.buffer
, access_len
);
3657 qemu_vfree(bounce
.buffer
);
3658 bounce
.buffer
= NULL
;
3659 memory_region_unref(bounce
.mr
);
3660 atomic_mb_set(&bounce
.in_use
, false);
3661 cpu_notify_map_clients();
3664 void *cpu_physical_memory_map(hwaddr addr
,
3668 return address_space_map(&address_space_memory
, addr
, plen
, is_write
,
3669 MEMTXATTRS_UNSPECIFIED
);
3672 void cpu_physical_memory_unmap(void *buffer
, hwaddr len
,
3673 bool is_write
, hwaddr access_len
)
3675 return address_space_unmap(&address_space_memory
, buffer
, len
, is_write
, access_len
);
3678 #define ARG1_DECL AddressSpace *as
3681 #define TRANSLATE(...) address_space_translate(as, __VA_ARGS__)
3682 #define RCU_READ_LOCK(...) rcu_read_lock()
3683 #define RCU_READ_UNLOCK(...) rcu_read_unlock()
3684 #include "memory_ldst.inc.c"
3686 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
3692 AddressSpaceDispatch
*d
;
3699 cache
->fv
= address_space_get_flatview(as
);
3700 d
= flatview_to_dispatch(cache
->fv
);
3701 cache
->mrs
= *address_space_translate_internal(d
, addr
, &cache
->xlat
, &l
, true);
3704 memory_region_ref(mr
);
3705 if (memory_access_is_direct(mr
, is_write
)) {
3706 /* We don't care about the memory attributes here as we're only
3707 * doing this if we found actual RAM, which behaves the same
3708 * regardless of attributes; so UNSPECIFIED is fine.
3710 l
= flatview_extend_translation(cache
->fv
, addr
, len
, mr
,
3711 cache
->xlat
, l
, is_write
,
3712 MEMTXATTRS_UNSPECIFIED
);
3713 cache
->ptr
= qemu_ram_ptr_length(mr
->ram_block
, cache
->xlat
, &l
, true);
3719 cache
->is_write
= is_write
;
3723 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
3727 assert(cache
->is_write
);
3728 if (likely(cache
->ptr
)) {
3729 invalidate_and_set_dirty(cache
->mrs
.mr
, addr
+ cache
->xlat
, access_len
);
3733 void address_space_cache_destroy(MemoryRegionCache
*cache
)
3735 if (!cache
->mrs
.mr
) {
3739 if (xen_enabled()) {
3740 xen_invalidate_map_cache_entry(cache
->ptr
);
3742 memory_region_unref(cache
->mrs
.mr
);
3743 flatview_unref(cache
->fv
);
3744 cache
->mrs
.mr
= NULL
;
3748 /* Called from RCU critical section. This function has the same
3749 * semantics as address_space_translate, but it only works on a
3750 * predefined range of a MemoryRegion that was mapped with
3751 * address_space_cache_init.
3753 static inline MemoryRegion
*address_space_translate_cached(
3754 MemoryRegionCache
*cache
, hwaddr addr
, hwaddr
*xlat
,
3755 hwaddr
*plen
, bool is_write
, MemTxAttrs attrs
)
3757 MemoryRegionSection section
;
3759 IOMMUMemoryRegion
*iommu_mr
;
3760 AddressSpace
*target_as
;
3762 assert(!cache
->ptr
);
3763 *xlat
= addr
+ cache
->xlat
;
3766 iommu_mr
= memory_region_get_iommu(mr
);
3772 section
= address_space_translate_iommu(iommu_mr
, xlat
, plen
,
3773 NULL
, is_write
, true,
3778 /* Called from RCU critical section. address_space_read_cached uses this
3779 * out of line function when the target is an MMIO or IOMMU region.
3782 address_space_read_cached_slow(MemoryRegionCache
*cache
, hwaddr addr
,
3783 void *buf
, hwaddr len
)
3789 mr
= address_space_translate_cached(cache
, addr
, &addr1
, &l
, false,
3790 MEMTXATTRS_UNSPECIFIED
);
3791 return flatview_read_continue(cache
->fv
,
3792 addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
,
3796 /* Called from RCU critical section. address_space_write_cached uses this
3797 * out of line function when the target is an MMIO or IOMMU region.
3800 address_space_write_cached_slow(MemoryRegionCache
*cache
, hwaddr addr
,
3801 const void *buf
, hwaddr len
)
3807 mr
= address_space_translate_cached(cache
, addr
, &addr1
, &l
, true,
3808 MEMTXATTRS_UNSPECIFIED
);
3809 return flatview_write_continue(cache
->fv
,
3810 addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
,
3814 #define ARG1_DECL MemoryRegionCache *cache
3816 #define SUFFIX _cached_slow
3817 #define TRANSLATE(...) address_space_translate_cached(cache, __VA_ARGS__)
3818 #define RCU_READ_LOCK() ((void)0)
3819 #define RCU_READ_UNLOCK() ((void)0)
3820 #include "memory_ldst.inc.c"
3822 /* virtual memory access for debug (includes writing to ROM) */
3823 int cpu_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
3824 void *ptr
, target_ulong len
, bool is_write
)
3827 target_ulong l
, page
;
3830 cpu_synchronize_state(cpu
);
3836 page
= addr
& TARGET_PAGE_MASK
;
3837 phys_addr
= cpu_get_phys_page_attrs_debug(cpu
, page
, &attrs
);
3838 asidx
= cpu_asidx_from_attrs(cpu
, attrs
);
3839 /* if no physical page mapped, return an error */
3840 if (phys_addr
== -1)
3842 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
3845 phys_addr
+= (addr
& ~TARGET_PAGE_MASK
);
3847 res
= address_space_write_rom(cpu
->cpu_ases
[asidx
].as
, phys_addr
,
3850 res
= address_space_read(cpu
->cpu_ases
[asidx
].as
, phys_addr
,
3853 if (res
!= MEMTX_OK
) {
3864 * Allows code that needs to deal with migration bitmaps etc to still be built
3865 * target independent.
3867 size_t qemu_target_page_size(void)
3869 return TARGET_PAGE_SIZE
;
3872 int qemu_target_page_bits(void)
3874 return TARGET_PAGE_BITS
;
3877 int qemu_target_page_bits_min(void)
3879 return TARGET_PAGE_BITS_MIN
;
3883 bool target_words_bigendian(void)
3885 #if defined(TARGET_WORDS_BIGENDIAN)
3892 #ifndef CONFIG_USER_ONLY
3893 bool cpu_physical_memory_is_io(hwaddr phys_addr
)
3899 RCU_READ_LOCK_GUARD();
3900 mr
= address_space_translate(&address_space_memory
,
3901 phys_addr
, &phys_addr
, &l
, false,
3902 MEMTXATTRS_UNSPECIFIED
);
3904 res
= !(memory_region_is_ram(mr
) || memory_region_is_romd(mr
));
3908 int qemu_ram_foreach_block(RAMBlockIterFunc func
, void *opaque
)
3913 RCU_READ_LOCK_GUARD();
3914 RAMBLOCK_FOREACH(block
) {
3915 ret
= func(block
, opaque
);
3924 * Unmap pages of memory from start to start+length such that
3925 * they a) read as 0, b) Trigger whatever fault mechanism
3926 * the OS provides for postcopy.
3927 * The pages must be unmapped by the end of the function.
3928 * Returns: 0 on success, none-0 on failure
3931 int ram_block_discard_range(RAMBlock
*rb
, uint64_t start
, size_t length
)
3935 uint8_t *host_startaddr
= rb
->host
+ start
;
3937 if (!QEMU_PTR_IS_ALIGNED(host_startaddr
, rb
->page_size
)) {
3938 error_report("ram_block_discard_range: Unaligned start address: %p",
3943 if ((start
+ length
) <= rb
->used_length
) {
3944 bool need_madvise
, need_fallocate
;
3945 if (!QEMU_IS_ALIGNED(length
, rb
->page_size
)) {
3946 error_report("ram_block_discard_range: Unaligned length: %zx",
3951 errno
= ENOTSUP
; /* If we are missing MADVISE etc */
3953 /* The logic here is messy;
3954 * madvise DONTNEED fails for hugepages
3955 * fallocate works on hugepages and shmem
3957 need_madvise
= (rb
->page_size
== qemu_host_page_size
);
3958 need_fallocate
= rb
->fd
!= -1;
3959 if (need_fallocate
) {
3960 /* For a file, this causes the area of the file to be zero'd
3961 * if read, and for hugetlbfs also causes it to be unmapped
3962 * so a userfault will trigger.
3964 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
3965 ret
= fallocate(rb
->fd
, FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
3969 error_report("ram_block_discard_range: Failed to fallocate "
3970 "%s:%" PRIx64
" +%zx (%d)",
3971 rb
->idstr
, start
, length
, ret
);
3976 error_report("ram_block_discard_range: fallocate not available/file"
3977 "%s:%" PRIx64
" +%zx (%d)",
3978 rb
->idstr
, start
, length
, ret
);
3983 /* For normal RAM this causes it to be unmapped,
3984 * for shared memory it causes the local mapping to disappear
3985 * and to fall back on the file contents (which we just
3986 * fallocate'd away).
3988 #if defined(CONFIG_MADVISE)
3989 ret
= madvise(host_startaddr
, length
, MADV_DONTNEED
);
3992 error_report("ram_block_discard_range: Failed to discard range "
3993 "%s:%" PRIx64
" +%zx (%d)",
3994 rb
->idstr
, start
, length
, ret
);
3999 error_report("ram_block_discard_range: MADVISE not available"
4000 "%s:%" PRIx64
" +%zx (%d)",
4001 rb
->idstr
, start
, length
, ret
);
4005 trace_ram_block_discard_range(rb
->idstr
, host_startaddr
, length
,
4006 need_madvise
, need_fallocate
, ret
);
4008 error_report("ram_block_discard_range: Overrun block '%s' (%" PRIu64
4009 "/%zx/" RAM_ADDR_FMT
")",
4010 rb
->idstr
, start
, length
, rb
->used_length
);
4017 bool ramblock_is_pmem(RAMBlock
*rb
)
4019 return rb
->flags
& RAM_PMEM
;
4024 void page_size_init(void)
4026 /* NOTE: we can always suppose that qemu_host_page_size >=
4028 if (qemu_host_page_size
== 0) {
4029 qemu_host_page_size
= qemu_real_host_page_size
;
4031 if (qemu_host_page_size
< TARGET_PAGE_SIZE
) {
4032 qemu_host_page_size
= TARGET_PAGE_SIZE
;
4034 qemu_host_page_mask
= -(intptr_t)qemu_host_page_size
;
4037 #if !defined(CONFIG_USER_ONLY)
4039 static void mtree_print_phys_entries(int start
, int end
, int skip
, int ptr
)
4041 if (start
== end
- 1) {
4042 qemu_printf("\t%3d ", start
);
4044 qemu_printf("\t%3d..%-3d ", start
, end
- 1);
4046 qemu_printf(" skip=%d ", skip
);
4047 if (ptr
== PHYS_MAP_NODE_NIL
) {
4048 qemu_printf(" ptr=NIL");
4050 qemu_printf(" ptr=#%d", ptr
);
4052 qemu_printf(" ptr=[%d]", ptr
);
4057 #define MR_SIZE(size) (int128_nz(size) ? (hwaddr)int128_get64( \
4058 int128_sub((size), int128_one())) : 0)
4060 void mtree_print_dispatch(AddressSpaceDispatch
*d
, MemoryRegion
*root
)
4064 qemu_printf(" Dispatch\n");
4065 qemu_printf(" Physical sections\n");
4067 for (i
= 0; i
< d
->map
.sections_nb
; ++i
) {
4068 MemoryRegionSection
*s
= d
->map
.sections
+ i
;
4069 const char *names
[] = { " [unassigned]", " [not dirty]",
4070 " [ROM]", " [watch]" };
4072 qemu_printf(" #%d @" TARGET_FMT_plx
".." TARGET_FMT_plx
4075 s
->offset_within_address_space
,
4076 s
->offset_within_address_space
+ MR_SIZE(s
->mr
->size
),
4077 s
->mr
->name
? s
->mr
->name
: "(noname)",
4078 i
< ARRAY_SIZE(names
) ? names
[i
] : "",
4079 s
->mr
== root
? " [ROOT]" : "",
4080 s
== d
->mru_section
? " [MRU]" : "",
4081 s
->mr
->is_iommu
? " [iommu]" : "");
4084 qemu_printf(" alias=%s", s
->mr
->alias
->name
?
4085 s
->mr
->alias
->name
: "noname");
4090 qemu_printf(" Nodes (%d bits per level, %d levels) ptr=[%d] skip=%d\n",
4091 P_L2_BITS
, P_L2_LEVELS
, d
->phys_map
.ptr
, d
->phys_map
.skip
);
4092 for (i
= 0; i
< d
->map
.nodes_nb
; ++i
) {
4095 Node
*n
= d
->map
.nodes
+ i
;
4097 qemu_printf(" [%d]\n", i
);
4099 for (j
= 0, jprev
= 0, prev
= *n
[0]; j
< ARRAY_SIZE(*n
); ++j
) {
4100 PhysPageEntry
*pe
= *n
+ j
;
4102 if (pe
->ptr
== prev
.ptr
&& pe
->skip
== prev
.skip
) {
4106 mtree_print_phys_entries(jprev
, j
, prev
.skip
, prev
.ptr
);
4112 if (jprev
!= ARRAY_SIZE(*n
)) {
4113 mtree_print_phys_entries(jprev
, j
, prev
.skip
, prev
.ptr
);