2 * Physical memory management API
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
17 #ifndef CONFIG_USER_ONLY
19 #include "exec/cpu-common.h"
20 #include "exec/hwaddr.h"
21 #include "exec/memattrs.h"
22 #include "exec/memop.h"
23 #include "exec/ramlist.h"
24 #include "qemu/bswap.h"
25 #include "qemu/queue.h"
26 #include "qemu/int128.h"
27 #include "qemu/notify.h"
28 #include "qom/object.h"
31 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
33 #define MAX_PHYS_ADDR_SPACE_BITS 62
34 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
36 #define TYPE_MEMORY_REGION "memory-region"
37 DECLARE_INSTANCE_CHECKER(MemoryRegion
, MEMORY_REGION
,
40 #define TYPE_IOMMU_MEMORY_REGION "iommu-memory-region"
41 typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionClass
;
42 DECLARE_OBJ_CHECKERS(IOMMUMemoryRegion
, IOMMUMemoryRegionClass
,
43 IOMMU_MEMORY_REGION
, TYPE_IOMMU_MEMORY_REGION
)
45 #define TYPE_RAM_DISCARD_MANAGER "qemu:ram-discard-manager"
46 typedef struct RamDiscardManagerClass RamDiscardManagerClass
;
47 typedef struct RamDiscardManager RamDiscardManager
;
48 DECLARE_OBJ_CHECKERS(RamDiscardManager
, RamDiscardManagerClass
,
49 RAM_DISCARD_MANAGER
, TYPE_RAM_DISCARD_MANAGER
);
52 void fuzz_dma_read_cb(size_t addr
,
56 static inline void fuzz_dma_read_cb(size_t addr
,
64 extern bool global_dirty_log
;
66 typedef struct MemoryRegionOps MemoryRegionOps
;
68 struct ReservedRegion
{
75 * struct MemoryRegionSection: describes a fragment of a #MemoryRegion
77 * @mr: the region, or %NULL if empty
78 * @fv: the flat view of the address space the region is mapped in
79 * @offset_within_region: the beginning of the section, relative to @mr's start
80 * @size: the size of the section; will not exceed @mr's boundaries
81 * @offset_within_address_space: the address of the first byte of the section
82 * relative to the region's address space
83 * @readonly: writes to this section are ignored
84 * @nonvolatile: this section is non-volatile
86 struct MemoryRegionSection
{
90 hwaddr offset_within_region
;
91 hwaddr offset_within_address_space
;
96 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
98 /* See address_space_translate: bit 0 is read, bit 1 is write. */
106 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
108 struct IOMMUTLBEntry
{
109 AddressSpace
*target_as
;
111 hwaddr translated_addr
;
112 hwaddr addr_mask
; /* 0xfff = 4k translation */
113 IOMMUAccessFlags perm
;
117 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
118 * register with one or multiple IOMMU Notifier capability bit(s).
121 IOMMU_NOTIFIER_NONE
= 0,
122 /* Notify cache invalidations */
123 IOMMU_NOTIFIER_UNMAP
= 0x1,
124 /* Notify entry changes (newly created entries) */
125 IOMMU_NOTIFIER_MAP
= 0x2,
126 /* Notify changes on device IOTLB entries */
127 IOMMU_NOTIFIER_DEVIOTLB_UNMAP
= 0x04,
130 #define IOMMU_NOTIFIER_IOTLB_EVENTS (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
131 #define IOMMU_NOTIFIER_DEVIOTLB_EVENTS IOMMU_NOTIFIER_DEVIOTLB_UNMAP
132 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_IOTLB_EVENTS | \
133 IOMMU_NOTIFIER_DEVIOTLB_EVENTS)
135 struct IOMMUNotifier
;
136 typedef void (*IOMMUNotify
)(struct IOMMUNotifier
*notifier
,
137 IOMMUTLBEntry
*data
);
139 struct IOMMUNotifier
{
141 IOMMUNotifierFlag notifier_flags
;
142 /* Notify for address space range start <= addr <= end */
146 QLIST_ENTRY(IOMMUNotifier
) node
;
148 typedef struct IOMMUNotifier IOMMUNotifier
;
150 typedef struct IOMMUTLBEvent
{
151 IOMMUNotifierFlag type
;
155 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
156 #define RAM_PREALLOC (1 << 0)
158 /* RAM is mmap-ed with MAP_SHARED */
159 #define RAM_SHARED (1 << 1)
161 /* Only a portion of RAM (used_length) is actually used, and migrated.
162 * Resizing RAM while migrating can result in the migration being canceled.
164 #define RAM_RESIZEABLE (1 << 2)
166 /* UFFDIO_ZEROPAGE is available on this RAMBlock to atomically
167 * zero the page and wake waiting processes.
168 * (Set during postcopy)
170 #define RAM_UF_ZEROPAGE (1 << 3)
172 /* RAM can be migrated */
173 #define RAM_MIGRATABLE (1 << 4)
175 /* RAM is a persistent kind memory */
176 #define RAM_PMEM (1 << 5)
180 * UFFDIO_WRITEPROTECT is used on this RAMBlock to
181 * support 'write-tracking' migration type.
182 * Implies ram_state->ram_wt_enabled.
184 #define RAM_UF_WRITEPROTECT (1 << 6)
187 * RAM is mmap-ed with MAP_NORESERVE. When set, reserving swap space (or huge
188 * pages if applicable) is skipped: will bail out if not supported. When not
189 * set, the OS will do the reservation, if supported for the memory type.
191 #define RAM_NORESERVE (1 << 7)
193 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
194 IOMMUNotifierFlag flags
,
195 hwaddr start
, hwaddr end
,
199 n
->notifier_flags
= flags
;
202 n
->iommu_idx
= iommu_idx
;
206 * Memory region callbacks
208 struct MemoryRegionOps
{
209 /* Read from the memory region. @addr is relative to @mr; @size is
211 uint64_t (*read
)(void *opaque
,
214 /* Write to the memory region. @addr is relative to @mr; @size is
216 void (*write
)(void *opaque
,
221 MemTxResult (*read_with_attrs
)(void *opaque
,
226 MemTxResult (*write_with_attrs
)(void *opaque
,
232 enum device_endian endianness
;
233 /* Guest-visible constraints: */
235 /* If nonzero, specify bounds on access sizes beyond which a machine
238 unsigned min_access_size
;
239 unsigned max_access_size
;
240 /* If true, unaligned accesses are supported. Otherwise unaligned
241 * accesses throw machine checks.
245 * If present, and returns #false, the transaction is not accepted
246 * by the device (and results in machine dependent behaviour such
247 * as a machine check exception).
249 bool (*accepts
)(void *opaque
, hwaddr addr
,
250 unsigned size
, bool is_write
,
253 /* Internal implementation constraints: */
255 /* If nonzero, specifies the minimum size implemented. Smaller sizes
256 * will be rounded upwards and a partial result will be returned.
258 unsigned min_access_size
;
259 /* If nonzero, specifies the maximum size implemented. Larger sizes
260 * will be done as a series of accesses with smaller sizes.
262 unsigned max_access_size
;
263 /* If true, unaligned accesses are supported. Otherwise all accesses
264 * are converted to (possibly multiple) naturally aligned accesses.
270 typedef struct MemoryRegionClass
{
272 ObjectClass parent_class
;
276 enum IOMMUMemoryRegionAttr
{
277 IOMMU_ATTR_SPAPR_TCE_FD
281 * IOMMUMemoryRegionClass:
283 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
284 * and provide an implementation of at least the @translate method here
285 * to handle requests to the memory region. Other methods are optional.
287 * The IOMMU implementation must use the IOMMU notifier infrastructure
288 * to report whenever mappings are changed, by calling
289 * memory_region_notify_iommu() (or, if necessary, by calling
290 * memory_region_notify_iommu_one() for each registered notifier).
292 * Conceptually an IOMMU provides a mapping from input address
293 * to an output TLB entry. If the IOMMU is aware of memory transaction
294 * attributes and the output TLB entry depends on the transaction
295 * attributes, we represent this using IOMMU indexes. Each index
296 * selects a particular translation table that the IOMMU has:
298 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
300 * @translate takes an input address and an IOMMU index
302 * and the mapping returned can only depend on the input address and the
305 * Most IOMMUs don't care about the transaction attributes and support
306 * only a single IOMMU index. A more complex IOMMU might have one index
307 * for secure transactions and one for non-secure transactions.
309 struct IOMMUMemoryRegionClass
{
311 MemoryRegionClass parent_class
;
317 * Return a TLB entry that contains a given address.
319 * The IOMMUAccessFlags indicated via @flag are optional and may
320 * be specified as IOMMU_NONE to indicate that the caller needs
321 * the full translation information for both reads and writes. If
322 * the access flags are specified then the IOMMU implementation
323 * may use this as an optimization, to stop doing a page table
324 * walk as soon as it knows that the requested permissions are not
325 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
326 * full page table walk and report the permissions in the returned
327 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
328 * return different mappings for reads and writes.)
330 * The returned information remains valid while the caller is
331 * holding the big QEMU lock or is inside an RCU critical section;
332 * if the caller wishes to cache the mapping beyond that it must
333 * register an IOMMU notifier so it can invalidate its cached
334 * information when the IOMMU mapping changes.
336 * @iommu: the IOMMUMemoryRegion
338 * @hwaddr: address to be translated within the memory region
340 * @flag: requested access permission
342 * @iommu_idx: IOMMU index for the translation
344 IOMMUTLBEntry (*translate
)(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
345 IOMMUAccessFlags flag
, int iommu_idx
);
347 * @get_min_page_size:
349 * Returns minimum supported page size in bytes.
351 * If this method is not provided then the minimum is assumed to
352 * be TARGET_PAGE_SIZE.
354 * @iommu: the IOMMUMemoryRegion
356 uint64_t (*get_min_page_size
)(IOMMUMemoryRegion
*iommu
);
358 * @notify_flag_changed:
360 * Called when IOMMU Notifier flag changes (ie when the set of
361 * events which IOMMU users are requesting notification for changes).
362 * Optional method -- need not be provided if the IOMMU does not
363 * need to know exactly which events must be notified.
365 * @iommu: the IOMMUMemoryRegion
367 * @old_flags: events which previously needed to be notified
369 * @new_flags: events which now need to be notified
371 * Returns 0 on success, or a negative errno; in particular
372 * returns -EINVAL if the new flag bitmap is not supported by the
373 * IOMMU memory region. In case of failure, the error object
376 int (*notify_flag_changed
)(IOMMUMemoryRegion
*iommu
,
377 IOMMUNotifierFlag old_flags
,
378 IOMMUNotifierFlag new_flags
,
383 * Called to handle memory_region_iommu_replay().
385 * The default implementation of memory_region_iommu_replay() is to
386 * call the IOMMU translate method for every page in the address space
387 * with flag == IOMMU_NONE and then call the notifier if translate
388 * returns a valid mapping. If this method is implemented then it
389 * overrides the default behaviour, and must provide the full semantics
390 * of memory_region_iommu_replay(), by calling @notifier for every
391 * translation present in the IOMMU.
393 * Optional method -- an IOMMU only needs to provide this method
394 * if the default is inefficient or produces undesirable side effects.
396 * Note: this is not related to record-and-replay functionality.
398 void (*replay
)(IOMMUMemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
403 * Get IOMMU misc attributes. This is an optional method that
404 * can be used to allow users of the IOMMU to get implementation-specific
405 * information. The IOMMU implements this method to handle calls
406 * by IOMMU users to memory_region_iommu_get_attr() by filling in
407 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
408 * the IOMMU supports. If the method is unimplemented then
409 * memory_region_iommu_get_attr() will always return -EINVAL.
411 * @iommu: the IOMMUMemoryRegion
413 * @attr: attribute being queried
415 * @data: memory to fill in with the attribute data
417 * Returns 0 on success, or a negative errno; in particular
418 * returns -EINVAL for unrecognized or unimplemented attribute types.
420 int (*get_attr
)(IOMMUMemoryRegion
*iommu
, enum IOMMUMemoryRegionAttr attr
,
426 * Return the IOMMU index to use for a given set of transaction attributes.
428 * Optional method: if an IOMMU only supports a single IOMMU index then
429 * the default implementation of memory_region_iommu_attrs_to_index()
432 * The indexes supported by an IOMMU must be contiguous, starting at 0.
434 * @iommu: the IOMMUMemoryRegion
435 * @attrs: memory transaction attributes
437 int (*attrs_to_index
)(IOMMUMemoryRegion
*iommu
, MemTxAttrs attrs
);
442 * Return the number of IOMMU indexes this IOMMU supports.
444 * Optional method: if this method is not provided, then
445 * memory_region_iommu_num_indexes() will return 1, indicating that
446 * only a single IOMMU index is supported.
448 * @iommu: the IOMMUMemoryRegion
450 int (*num_indexes
)(IOMMUMemoryRegion
*iommu
);
453 * @iommu_set_page_size_mask:
455 * Restrict the page size mask that can be supported with a given IOMMU
456 * memory region. Used for example to propagate host physical IOMMU page
457 * size mask limitations to the virtual IOMMU.
459 * Optional method: if this method is not provided, then the default global
462 * @iommu: the IOMMUMemoryRegion
464 * @page_size_mask: a bitmask of supported page sizes. At least one bit,
465 * representing the smallest page size, must be set. Additional set bits
466 * represent supported block sizes. For example a host physical IOMMU that
467 * uses page tables with a page size of 4kB, and supports 2MB and 4GB
468 * blocks, will set mask 0x40201000. A granule of 4kB with indiscriminate
469 * block sizes is specified with mask 0xfffffffffffff000.
471 * Returns 0 on success, or a negative error. In case of failure, the error
472 * object must be created.
474 int (*iommu_set_page_size_mask
)(IOMMUMemoryRegion
*iommu
,
475 uint64_t page_size_mask
,
479 typedef struct RamDiscardListener RamDiscardListener
;
480 typedef int (*NotifyRamPopulate
)(RamDiscardListener
*rdl
,
481 MemoryRegionSection
*section
);
482 typedef void (*NotifyRamDiscard
)(RamDiscardListener
*rdl
,
483 MemoryRegionSection
*section
);
485 struct RamDiscardListener
{
489 * Notification that previously discarded memory is about to get populated.
490 * Listeners are able to object. If any listener objects, already
491 * successfully notified listeners are notified about a discard again.
493 * @rdl: the #RamDiscardListener getting notified
494 * @section: the #MemoryRegionSection to get populated. The section
495 * is aligned within the memory region to the minimum granularity
496 * unless it would exceed the registered section.
498 * Returns 0 on success. If the notification is rejected by the listener,
499 * an error is returned.
501 NotifyRamPopulate notify_populate
;
506 * Notification that previously populated memory was discarded successfully
507 * and listeners should drop all references to such memory and prevent
508 * new population (e.g., unmap).
510 * @rdl: the #RamDiscardListener getting notified
511 * @section: the #MemoryRegionSection to get populated. The section
512 * is aligned within the memory region to the minimum granularity
513 * unless it would exceed the registered section.
515 NotifyRamDiscard notify_discard
;
518 * @double_discard_supported:
520 * The listener suppors getting @notify_discard notifications that span
521 * already discarded parts.
523 bool double_discard_supported
;
525 MemoryRegionSection
*section
;
526 QLIST_ENTRY(RamDiscardListener
) next
;
529 static inline void ram_discard_listener_init(RamDiscardListener
*rdl
,
530 NotifyRamPopulate populate_fn
,
531 NotifyRamDiscard discard_fn
,
532 bool double_discard_supported
)
534 rdl
->notify_populate
= populate_fn
;
535 rdl
->notify_discard
= discard_fn
;
536 rdl
->double_discard_supported
= double_discard_supported
;
539 typedef int (*ReplayRamPopulate
)(MemoryRegionSection
*section
, void *opaque
);
542 * RamDiscardManagerClass:
544 * A #RamDiscardManager coordinates which parts of specific RAM #MemoryRegion
545 * regions are currently populated to be used/accessed by the VM, notifying
546 * after parts were discarded (freeing up memory) and before parts will be
547 * populated (consuming memory), to be used/acessed by the VM.
549 * A #RamDiscardManager can only be set for a RAM #MemoryRegion while the
550 * #MemoryRegion isn't mapped yet; it cannot change while the #MemoryRegion is
553 * The #RamDiscardManager is intended to be used by technologies that are
554 * incompatible with discarding of RAM (e.g., VFIO, which may pin all
555 * memory inside a #MemoryRegion), and require proper coordination to only
556 * map the currently populated parts, to hinder parts that are expected to
557 * remain discarded from silently getting populated and consuming memory.
558 * Technologies that support discarding of RAM don't have to bother and can
559 * simply map the whole #MemoryRegion.
561 * An example #RamDiscardManager is virtio-mem, which logically (un)plugs
562 * memory within an assigned RAM #MemoryRegion, coordinated with the VM.
563 * Logically unplugging memory consists of discarding RAM. The VM agreed to not
564 * access unplugged (discarded) memory - especially via DMA. virtio-mem will
565 * properly coordinate with listeners before memory is plugged (populated),
566 * and after memory is unplugged (discarded).
568 * Listeners are called in multiples of the minimum granularity (unless it
569 * would exceed the registered range) and changes are aligned to the minimum
570 * granularity within the #MemoryRegion. Listeners have to prepare for memory
571 * becomming discarded in a different granularity than it was populated and the
574 struct RamDiscardManagerClass
{
576 InterfaceClass parent_class
;
581 * @get_min_granularity:
583 * Get the minimum granularity in which listeners will get notified
584 * about changes within the #MemoryRegion via the #RamDiscardManager.
586 * @rdm: the #RamDiscardManager
587 * @mr: the #MemoryRegion
589 * Returns the minimum granularity.
591 uint64_t (*get_min_granularity
)(const RamDiscardManager
*rdm
,
592 const MemoryRegion
*mr
);
597 * Check whether the given #MemoryRegionSection is completely populated
598 * (i.e., no parts are currently discarded) via the #RamDiscardManager.
599 * There are no alignment requirements.
601 * @rdm: the #RamDiscardManager
602 * @section: the #MemoryRegionSection
604 * Returns whether the given range is completely populated.
606 bool (*is_populated
)(const RamDiscardManager
*rdm
,
607 const MemoryRegionSection
*section
);
612 * Call the #ReplayRamPopulate callback for all populated parts within the
613 * #MemoryRegionSection via the #RamDiscardManager.
615 * In case any call fails, no further calls are made.
617 * @rdm: the #RamDiscardManager
618 * @section: the #MemoryRegionSection
619 * @replay_fn: the #ReplayRamPopulate callback
620 * @opaque: pointer to forward to the callback
622 * Returns 0 on success, or a negative error if any notification failed.
624 int (*replay_populated
)(const RamDiscardManager
*rdm
,
625 MemoryRegionSection
*section
,
626 ReplayRamPopulate replay_fn
, void *opaque
);
629 * @register_listener:
631 * Register a #RamDiscardListener for the given #MemoryRegionSection and
632 * immediately notify the #RamDiscardListener about all populated parts
633 * within the #MemoryRegionSection via the #RamDiscardManager.
635 * In case any notification fails, no further notifications are triggered
636 * and an error is logged.
638 * @rdm: the #RamDiscardManager
639 * @rdl: the #RamDiscardListener
640 * @section: the #MemoryRegionSection
642 void (*register_listener
)(RamDiscardManager
*rdm
,
643 RamDiscardListener
*rdl
,
644 MemoryRegionSection
*section
);
647 * @unregister_listener:
649 * Unregister a previously registered #RamDiscardListener via the
650 * #RamDiscardManager after notifying the #RamDiscardListener about all
651 * populated parts becoming unpopulated within the registered
652 * #MemoryRegionSection.
654 * @rdm: the #RamDiscardManager
655 * @rdl: the #RamDiscardListener
657 void (*unregister_listener
)(RamDiscardManager
*rdm
,
658 RamDiscardListener
*rdl
);
661 uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager
*rdm
,
662 const MemoryRegion
*mr
);
664 bool ram_discard_manager_is_populated(const RamDiscardManager
*rdm
,
665 const MemoryRegionSection
*section
);
667 int ram_discard_manager_replay_populated(const RamDiscardManager
*rdm
,
668 MemoryRegionSection
*section
,
669 ReplayRamPopulate replay_fn
,
672 void ram_discard_manager_register_listener(RamDiscardManager
*rdm
,
673 RamDiscardListener
*rdl
,
674 MemoryRegionSection
*section
);
676 void ram_discard_manager_unregister_listener(RamDiscardManager
*rdm
,
677 RamDiscardListener
*rdl
);
679 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
680 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
684 * A struct representing a memory region.
686 struct MemoryRegion
{
691 /* The following fields should fit in a cache line */
695 bool readonly
; /* For RAM regions */
698 bool flush_coalesced_mmio
;
699 uint8_t dirty_log_mask
;
704 const MemoryRegionOps
*ops
;
706 MemoryRegion
*container
;
709 void (*destructor
)(MemoryRegion
*mr
);
714 bool warning_printed
; /* For reservations */
715 uint8_t vga_logging_count
;
719 QTAILQ_HEAD(, MemoryRegion
) subregions
;
720 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
721 QTAILQ_HEAD(, CoalescedMemoryRange
) coalesced
;
723 unsigned ioeventfd_nb
;
724 MemoryRegionIoeventfd
*ioeventfds
;
725 RamDiscardManager
*rdm
; /* Only for RAM */
728 struct IOMMUMemoryRegion
{
729 MemoryRegion parent_obj
;
731 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
732 IOMMUNotifierFlag iommu_notify_flags
;
735 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
736 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
739 * struct MemoryListener: callbacks structure for updates to the physical memory map
741 * Allows a component to adjust to changes in the guest-visible memory map.
742 * Use with memory_listener_register() and memory_listener_unregister().
744 struct MemoryListener
{
748 * Called at the beginning of an address space update transaction.
749 * Followed by calls to #MemoryListener.region_add(),
750 * #MemoryListener.region_del(), #MemoryListener.region_nop(),
751 * #MemoryListener.log_start() and #MemoryListener.log_stop() in
752 * increasing address order.
754 * @listener: The #MemoryListener.
756 void (*begin
)(MemoryListener
*listener
);
761 * Called at the end of an address space update transaction,
762 * after the last call to #MemoryListener.region_add(),
763 * #MemoryListener.region_del() or #MemoryListener.region_nop(),
764 * #MemoryListener.log_start() and #MemoryListener.log_stop().
766 * @listener: The #MemoryListener.
768 void (*commit
)(MemoryListener
*listener
);
773 * Called during an address space update transaction,
774 * for a section of the address space that is new in this address space
775 * space since the last transaction.
777 * @listener: The #MemoryListener.
778 * @section: The new #MemoryRegionSection.
780 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
785 * Called during an address space update transaction,
786 * for a section of the address space that has disappeared in the address
787 * space since the last transaction.
789 * @listener: The #MemoryListener.
790 * @section: The old #MemoryRegionSection.
792 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
797 * Called during an address space update transaction,
798 * for a section of the address space that is in the same place in the address
799 * space as in the last transaction.
801 * @listener: The #MemoryListener.
802 * @section: The #MemoryRegionSection.
804 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
809 * Called during an address space update transaction, after
810 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or
811 * #MemoryListener.region_nop(), if dirty memory logging clients have
812 * become active since the last transaction.
814 * @listener: The #MemoryListener.
815 * @section: The #MemoryRegionSection.
816 * @old: A bitmap of dirty memory logging clients that were active in
817 * the previous transaction.
818 * @new: A bitmap of dirty memory logging clients that are active in
819 * the current transaction.
821 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
827 * Called during an address space update transaction, after
828 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or
829 * #MemoryListener.region_nop() and possibly after
830 * #MemoryListener.log_start(), if dirty memory logging clients have
831 * become inactive since the last transaction.
833 * @listener: The #MemoryListener.
834 * @section: The #MemoryRegionSection.
835 * @old: A bitmap of dirty memory logging clients that were active in
836 * the previous transaction.
837 * @new: A bitmap of dirty memory logging clients that are active in
838 * the current transaction.
840 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
846 * Called by memory_region_snapshot_and_clear_dirty() and
847 * memory_global_dirty_log_sync(), before accessing QEMU's "official"
848 * copy of the dirty memory bitmap for a #MemoryRegionSection.
850 * @listener: The #MemoryListener.
851 * @section: The #MemoryRegionSection.
853 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
858 * This is the global version of @log_sync when the listener does
859 * not have a way to synchronize the log with finer granularity.
860 * When the listener registers with @log_sync_global defined, then
861 * its @log_sync must be NULL. Vice versa.
863 * @listener: The #MemoryListener.
865 void (*log_sync_global
)(MemoryListener
*listener
);
870 * Called before reading the dirty memory bitmap for a
871 * #MemoryRegionSection.
873 * @listener: The #MemoryListener.
874 * @section: The #MemoryRegionSection.
876 void (*log_clear
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
881 * Called by memory_global_dirty_log_start(), which
882 * enables the %DIRTY_LOG_MIGRATION client on all memory regions in
883 * the address space. #MemoryListener.log_global_start() is also
884 * called when a #MemoryListener is added, if global dirty logging is
885 * active at that time.
887 * @listener: The #MemoryListener.
889 void (*log_global_start
)(MemoryListener
*listener
);
894 * Called by memory_global_dirty_log_stop(), which
895 * disables the %DIRTY_LOG_MIGRATION client on all memory regions in
898 * @listener: The #MemoryListener.
900 void (*log_global_stop
)(MemoryListener
*listener
);
903 * @log_global_after_sync:
905 * Called after reading the dirty memory bitmap
906 * for any #MemoryRegionSection.
908 * @listener: The #MemoryListener.
910 void (*log_global_after_sync
)(MemoryListener
*listener
);
915 * Called during an address space update transaction,
916 * for a section of the address space that has had a new ioeventfd
917 * registration since the last transaction.
919 * @listener: The #MemoryListener.
920 * @section: The new #MemoryRegionSection.
921 * @match_data: The @match_data parameter for the new ioeventfd.
922 * @data: The @data parameter for the new ioeventfd.
923 * @e: The #EventNotifier parameter for the new ioeventfd.
925 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
926 bool match_data
, uint64_t data
, EventNotifier
*e
);
931 * Called during an address space update transaction,
932 * for a section of the address space that has dropped an ioeventfd
933 * registration since the last transaction.
935 * @listener: The #MemoryListener.
936 * @section: The new #MemoryRegionSection.
937 * @match_data: The @match_data parameter for the dropped ioeventfd.
938 * @data: The @data parameter for the dropped ioeventfd.
939 * @e: The #EventNotifier parameter for the dropped ioeventfd.
941 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
942 bool match_data
, uint64_t data
, EventNotifier
*e
);
947 * Called during an address space update transaction,
948 * for a section of the address space that has had a new coalesced
949 * MMIO range registration since the last transaction.
951 * @listener: The #MemoryListener.
952 * @section: The new #MemoryRegionSection.
953 * @addr: The starting address for the coalesced MMIO range.
954 * @len: The length of the coalesced MMIO range.
956 void (*coalesced_io_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
957 hwaddr addr
, hwaddr len
);
962 * Called during an address space update transaction,
963 * for a section of the address space that has dropped a coalesced
964 * MMIO range since the last transaction.
966 * @listener: The #MemoryListener.
967 * @section: The new #MemoryRegionSection.
968 * @addr: The starting address for the coalesced MMIO range.
969 * @len: The length of the coalesced MMIO range.
971 void (*coalesced_io_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
972 hwaddr addr
, hwaddr len
);
976 * Govern the order in which memory listeners are invoked. Lower priorities
977 * are invoked earlier for "add" or "start" callbacks, and later for "delete"
978 * or "stop" callbacks.
983 AddressSpace
*address_space
;
984 QTAILQ_ENTRY(MemoryListener
) link
;
985 QTAILQ_ENTRY(MemoryListener
) link_as
;
989 * struct AddressSpace: describes a mapping of addresses to #MemoryRegion objects
991 struct AddressSpace
{
997 /* Accessed via RCU. */
998 struct FlatView
*current_map
;
1001 struct MemoryRegionIoeventfd
*ioeventfds
;
1002 QTAILQ_HEAD(, MemoryListener
) listeners
;
1003 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
1006 typedef struct AddressSpaceDispatch AddressSpaceDispatch
;
1007 typedef struct FlatRange FlatRange
;
1009 /* Flattened global view of current active memory hierarchy. Kept in sorted
1013 struct rcu_head rcu
;
1017 unsigned nr_allocated
;
1018 struct AddressSpaceDispatch
*dispatch
;
1022 static inline FlatView
*address_space_to_flatview(AddressSpace
*as
)
1024 return qatomic_rcu_read(&as
->current_map
);
1028 * typedef flatview_cb: callback for flatview_for_each_range()
1030 * @start: start address of the range within the FlatView
1031 * @len: length of the range in bytes
1032 * @mr: MemoryRegion covering this range
1033 * @offset_in_region: offset of the first byte of the range within @mr
1034 * @opaque: data pointer passed to flatview_for_each_range()
1036 * Returns: true to stop the iteration, false to keep going.
1038 typedef bool (*flatview_cb
)(Int128 start
,
1040 const MemoryRegion
*mr
,
1041 hwaddr offset_in_region
,
1045 * flatview_for_each_range: Iterate through a FlatView
1046 * @fv: the FlatView to iterate through
1047 * @cb: function to call for each range
1048 * @opaque: opaque data pointer to pass to @cb
1050 * A FlatView is made up of a list of non-overlapping ranges, each of
1051 * which is a slice of a MemoryRegion. This function iterates through
1052 * each range in @fv, calling @cb. The callback function can terminate
1053 * iteration early by returning 'true'.
1055 void flatview_for_each_range(FlatView
*fv
, flatview_cb cb
, void *opaque
);
1057 static inline bool MemoryRegionSection_eq(MemoryRegionSection
*a
,
1058 MemoryRegionSection
*b
)
1060 return a
->mr
== b
->mr
&&
1062 a
->offset_within_region
== b
->offset_within_region
&&
1063 a
->offset_within_address_space
== b
->offset_within_address_space
&&
1064 int128_eq(a
->size
, b
->size
) &&
1065 a
->readonly
== b
->readonly
&&
1066 a
->nonvolatile
== b
->nonvolatile
;
1070 * memory_region_section_new_copy: Copy a memory region section
1072 * Allocate memory for a new copy, copy the memory region section, and
1073 * properly take a reference on all relevant members.
1075 * @s: the #MemoryRegionSection to copy
1077 MemoryRegionSection
*memory_region_section_new_copy(MemoryRegionSection
*s
);
1080 * memory_region_section_new_copy: Free a copied memory region section
1082 * Free a copy of a memory section created via memory_region_section_new_copy().
1083 * properly dropping references on all relevant members.
1085 * @s: the #MemoryRegionSection to copy
1087 void memory_region_section_free_copy(MemoryRegionSection
*s
);
1090 * memory_region_init: Initialize a memory region
1092 * The region typically acts as a container for other memory regions. Use
1093 * memory_region_add_subregion() to add subregions.
1095 * @mr: the #MemoryRegion to be initialized
1096 * @owner: the object that tracks the region's reference count
1097 * @name: used for debugging; not visible to the user or ABI
1098 * @size: size of the region; any subregions beyond this size will be clipped
1100 void memory_region_init(MemoryRegion
*mr
,
1106 * memory_region_ref: Add 1 to a memory region's reference count
1108 * Whenever memory regions are accessed outside the BQL, they need to be
1109 * preserved against hot-unplug. MemoryRegions actually do not have their
1110 * own reference count; they piggyback on a QOM object, their "owner".
1111 * This function adds a reference to the owner.
1113 * All MemoryRegions must have an owner if they can disappear, even if the
1114 * device they belong to operates exclusively under the BQL. This is because
1115 * the region could be returned at any time by memory_region_find, and this
1116 * is usually under guest control.
1118 * @mr: the #MemoryRegion
1120 void memory_region_ref(MemoryRegion
*mr
);
1123 * memory_region_unref: Remove 1 to a memory region's reference count
1125 * Whenever memory regions are accessed outside the BQL, they need to be
1126 * preserved against hot-unplug. MemoryRegions actually do not have their
1127 * own reference count; they piggyback on a QOM object, their "owner".
1128 * This function removes a reference to the owner and possibly destroys it.
1130 * @mr: the #MemoryRegion
1132 void memory_region_unref(MemoryRegion
*mr
);
1135 * memory_region_init_io: Initialize an I/O memory region.
1137 * Accesses into the region will cause the callbacks in @ops to be called.
1138 * if @size is nonzero, subregions will be clipped to @size.
1140 * @mr: the #MemoryRegion to be initialized.
1141 * @owner: the object that tracks the region's reference count
1142 * @ops: a structure containing read and write callbacks to be used when
1143 * I/O is performed on the region.
1144 * @opaque: passed to the read and write callbacks of the @ops structure.
1145 * @name: used for debugging; not visible to the user or ABI
1146 * @size: size of the region.
1148 void memory_region_init_io(MemoryRegion
*mr
,
1150 const MemoryRegionOps
*ops
,
1156 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
1157 * into the region will modify memory
1160 * @mr: the #MemoryRegion to be initialized.
1161 * @owner: the object that tracks the region's reference count
1162 * @name: Region name, becomes part of RAMBlock name used in migration stream
1163 * must be unique within any device
1164 * @size: size of the region.
1165 * @errp: pointer to Error*, to store an error if it happens.
1167 * Note that this function does not do anything to cause the data in the
1168 * RAM memory region to be migrated; that is the responsibility of the caller.
1170 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1177 * memory_region_init_ram_flags_nomigrate: Initialize RAM memory region.
1178 * Accesses into the region will
1179 * modify memory directly.
1181 * @mr: the #MemoryRegion to be initialized.
1182 * @owner: the object that tracks the region's reference count
1183 * @name: Region name, becomes part of RAMBlock name used in migration stream
1184 * must be unique within any device
1185 * @size: size of the region.
1186 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_NORESERVE.
1187 * @errp: pointer to Error*, to store an error if it happens.
1189 * Note that this function does not do anything to cause the data in the
1190 * RAM memory region to be migrated; that is the responsibility of the caller.
1192 void memory_region_init_ram_flags_nomigrate(MemoryRegion
*mr
,
1200 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
1201 * RAM. Accesses into the region will
1202 * modify memory directly. Only an initial
1203 * portion of this RAM is actually used.
1204 * Changing the size while migrating
1205 * can result in the migration being
1208 * @mr: the #MemoryRegion to be initialized.
1209 * @owner: the object that tracks the region's reference count
1210 * @name: Region name, becomes part of RAMBlock name used in migration stream
1211 * must be unique within any device
1212 * @size: used size of the region.
1213 * @max_size: max size of the region.
1214 * @resized: callback to notify owner about used size change.
1215 * @errp: pointer to Error*, to store an error if it happens.
1217 * Note that this function does not do anything to cause the data in the
1218 * RAM memory region to be migrated; that is the responsibility of the caller.
1220 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1225 void (*resized
)(const char*,
1232 * memory_region_init_ram_from_file: Initialize RAM memory region with a
1235 * @mr: the #MemoryRegion to be initialized.
1236 * @owner: the object that tracks the region's reference count
1237 * @name: Region name, becomes part of RAMBlock name used in migration stream
1238 * must be unique within any device
1239 * @size: size of the region.
1240 * @align: alignment of the region base address; if 0, the default alignment
1241 * (getpagesize()) will be used.
1242 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
1244 * @path: the path in which to allocate the RAM.
1245 * @readonly: true to open @path for reading, false for read/write.
1246 * @errp: pointer to Error*, to store an error if it happens.
1248 * Note that this function does not do anything to cause the data in the
1249 * RAM memory region to be migrated; that is the responsibility of the caller.
1251 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1262 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
1265 * @mr: the #MemoryRegion to be initialized.
1266 * @owner: the object that tracks the region's reference count
1267 * @name: the name of the region.
1268 * @size: size of the region.
1269 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
1271 * @fd: the fd to mmap.
1272 * @offset: offset within the file referenced by fd
1273 * @errp: pointer to Error*, to store an error if it happens.
1275 * Note that this function does not do anything to cause the data in the
1276 * RAM memory region to be migrated; that is the responsibility of the caller.
1278 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1289 * memory_region_init_ram_ptr: Initialize RAM memory region from a
1290 * user-provided pointer. Accesses into the
1291 * region will modify memory directly.
1293 * @mr: the #MemoryRegion to be initialized.
1294 * @owner: the object that tracks the region's reference count
1295 * @name: Region name, becomes part of RAMBlock name used in migration stream
1296 * must be unique within any device
1297 * @size: size of the region.
1298 * @ptr: memory to be mapped; must contain at least @size bytes.
1300 * Note that this function does not do anything to cause the data in the
1301 * RAM memory region to be migrated; that is the responsibility of the caller.
1303 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1310 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
1311 * a user-provided pointer.
1313 * A RAM device represents a mapping to a physical device, such as to a PCI
1314 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
1315 * into the VM address space and access to the region will modify memory
1316 * directly. However, the memory region should not be included in a memory
1317 * dump (device may not be enabled/mapped at the time of the dump), and
1318 * operations incompatible with manipulating MMIO should be avoided. Replaces
1321 * @mr: the #MemoryRegion to be initialized.
1322 * @owner: the object that tracks the region's reference count
1323 * @name: the name of the region.
1324 * @size: size of the region.
1325 * @ptr: memory to be mapped; must contain at least @size bytes.
1327 * Note that this function does not do anything to cause the data in the
1328 * RAM memory region to be migrated; that is the responsibility of the caller.
1329 * (For RAM device memory regions, migrating the contents rarely makes sense.)
1331 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1338 * memory_region_init_alias: Initialize a memory region that aliases all or a
1339 * part of another memory region.
1341 * @mr: the #MemoryRegion to be initialized.
1342 * @owner: the object that tracks the region's reference count
1343 * @name: used for debugging; not visible to the user or ABI
1344 * @orig: the region to be referenced; @mr will be equivalent to
1345 * @orig between @offset and @offset + @size - 1.
1346 * @offset: start of the section in @orig to be referenced.
1347 * @size: size of the region.
1349 void memory_region_init_alias(MemoryRegion
*mr
,
1357 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
1359 * This has the same effect as calling memory_region_init_ram_nomigrate()
1360 * and then marking the resulting region read-only with
1361 * memory_region_set_readonly().
1363 * Note that this function does not do anything to cause the data in the
1364 * RAM side of the memory region to be migrated; that is the responsibility
1367 * @mr: the #MemoryRegion to be initialized.
1368 * @owner: the object that tracks the region's reference count
1369 * @name: Region name, becomes part of RAMBlock name used in migration stream
1370 * must be unique within any device
1371 * @size: size of the region.
1372 * @errp: pointer to Error*, to store an error if it happens.
1374 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1381 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
1382 * Writes are handled via callbacks.
1384 * Note that this function does not do anything to cause the data in the
1385 * RAM side of the memory region to be migrated; that is the responsibility
1388 * @mr: the #MemoryRegion to be initialized.
1389 * @owner: the object that tracks the region's reference count
1390 * @ops: callbacks for write access handling (must not be NULL).
1391 * @opaque: passed to the read and write callbacks of the @ops structure.
1392 * @name: Region name, becomes part of RAMBlock name used in migration stream
1393 * must be unique within any device
1394 * @size: size of the region.
1395 * @errp: pointer to Error*, to store an error if it happens.
1397 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1399 const MemoryRegionOps
*ops
,
1406 * memory_region_init_iommu: Initialize a memory region of a custom type
1407 * that translates addresses
1409 * An IOMMU region translates addresses and forwards accesses to a target
1412 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
1413 * @_iommu_mr should be a pointer to enough memory for an instance of
1414 * that subclass, @instance_size is the size of that subclass, and
1415 * @mrtypename is its name. This function will initialize @_iommu_mr as an
1416 * instance of the subclass, and its methods will then be called to handle
1417 * accesses to the memory region. See the documentation of
1418 * #IOMMUMemoryRegionClass for further details.
1420 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
1421 * @instance_size: the IOMMUMemoryRegion subclass instance size
1422 * @mrtypename: the type name of the #IOMMUMemoryRegion
1423 * @owner: the object that tracks the region's reference count
1424 * @name: used for debugging; not visible to the user or ABI
1425 * @size: size of the region.
1427 void memory_region_init_iommu(void *_iommu_mr
,
1428 size_t instance_size
,
1429 const char *mrtypename
,
1435 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
1436 * region will modify memory directly.
1438 * @mr: the #MemoryRegion to be initialized
1439 * @owner: the object that tracks the region's reference count (must be
1440 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
1441 * @name: name of the memory region
1442 * @size: size of the region in bytes
1443 * @errp: pointer to Error*, to store an error if it happens.
1445 * This function allocates RAM for a board model or device, and
1446 * arranges for it to be migrated (by calling vmstate_register_ram()
1447 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1450 * TODO: Currently we restrict @owner to being either NULL (for
1451 * global RAM regions with no owner) or devices, so that we can
1452 * give the RAM block a unique name for migration purposes.
1453 * We should lift this restriction and allow arbitrary Objects.
1454 * If you pass a non-NULL non-device @owner then we will assert.
1456 void memory_region_init_ram(MemoryRegion
*mr
,
1463 * memory_region_init_rom: Initialize a ROM memory region.
1465 * This has the same effect as calling memory_region_init_ram()
1466 * and then marking the resulting region read-only with
1467 * memory_region_set_readonly(). This includes arranging for the
1468 * contents to be migrated.
1470 * TODO: Currently we restrict @owner to being either NULL (for
1471 * global RAM regions with no owner) or devices, so that we can
1472 * give the RAM block a unique name for migration purposes.
1473 * We should lift this restriction and allow arbitrary Objects.
1474 * If you pass a non-NULL non-device @owner then we will assert.
1476 * @mr: the #MemoryRegion to be initialized.
1477 * @owner: the object that tracks the region's reference count
1478 * @name: Region name, becomes part of RAMBlock name used in migration stream
1479 * must be unique within any device
1480 * @size: size of the region.
1481 * @errp: pointer to Error*, to store an error if it happens.
1483 void memory_region_init_rom(MemoryRegion
*mr
,
1490 * memory_region_init_rom_device: Initialize a ROM memory region.
1491 * Writes are handled via callbacks.
1493 * This function initializes a memory region backed by RAM for reads
1494 * and callbacks for writes, and arranges for the RAM backing to
1495 * be migrated (by calling vmstate_register_ram()
1496 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1499 * TODO: Currently we restrict @owner to being either NULL (for
1500 * global RAM regions with no owner) or devices, so that we can
1501 * give the RAM block a unique name for migration purposes.
1502 * We should lift this restriction and allow arbitrary Objects.
1503 * If you pass a non-NULL non-device @owner then we will assert.
1505 * @mr: the #MemoryRegion to be initialized.
1506 * @owner: the object that tracks the region's reference count
1507 * @ops: callbacks for write access handling (must not be NULL).
1508 * @opaque: passed to the read and write callbacks of the @ops structure.
1509 * @name: Region name, becomes part of RAMBlock name used in migration stream
1510 * must be unique within any device
1511 * @size: size of the region.
1512 * @errp: pointer to Error*, to store an error if it happens.
1514 void memory_region_init_rom_device(MemoryRegion
*mr
,
1516 const MemoryRegionOps
*ops
,
1524 * memory_region_owner: get a memory region's owner.
1526 * @mr: the memory region being queried.
1528 Object
*memory_region_owner(MemoryRegion
*mr
);
1531 * memory_region_size: get a memory region's size.
1533 * @mr: the memory region being queried.
1535 uint64_t memory_region_size(MemoryRegion
*mr
);
1538 * memory_region_is_ram: check whether a memory region is random access
1540 * Returns %true if a memory region is random access.
1542 * @mr: the memory region being queried
1544 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
1550 * memory_region_is_ram_device: check whether a memory region is a ram device
1552 * Returns %true if a memory region is a device backed ram region
1554 * @mr: the memory region being queried
1556 bool memory_region_is_ram_device(MemoryRegion
*mr
);
1559 * memory_region_is_romd: check whether a memory region is in ROMD mode
1561 * Returns %true if a memory region is a ROM device and currently set to allow
1564 * @mr: the memory region being queried
1566 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
1568 return mr
->rom_device
&& mr
->romd_mode
;
1572 * memory_region_get_iommu: check whether a memory region is an iommu
1574 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
1577 * @mr: the memory region being queried
1579 static inline IOMMUMemoryRegion
*memory_region_get_iommu(MemoryRegion
*mr
)
1582 return memory_region_get_iommu(mr
->alias
);
1585 return (IOMMUMemoryRegion
*) mr
;
1591 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
1592 * if an iommu or NULL if not
1594 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
1595 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1597 * @iommu_mr: the memory region being queried
1599 static inline IOMMUMemoryRegionClass
*memory_region_get_iommu_class_nocheck(
1600 IOMMUMemoryRegion
*iommu_mr
)
1602 return (IOMMUMemoryRegionClass
*) (((Object
*)iommu_mr
)->class);
1605 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1608 * memory_region_iommu_get_min_page_size: get minimum supported page size
1611 * Returns minimum supported page size for an iommu.
1613 * @iommu_mr: the memory region being queried
1615 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
);
1618 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1620 * Note: for any IOMMU implementation, an in-place mapping change
1621 * should be notified with an UNMAP followed by a MAP.
1623 * @iommu_mr: the memory region that was changed
1624 * @iommu_idx: the IOMMU index for the translation table which has changed
1625 * @event: TLB event with the new entry in the IOMMU translation table.
1626 * The entry replaces all old entries for the same virtual I/O address
1629 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1631 IOMMUTLBEvent event
);
1634 * memory_region_notify_iommu_one: notify a change in an IOMMU translation
1635 * entry to a single notifier
1637 * This works just like memory_region_notify_iommu(), but it only
1638 * notifies a specific notifier, not all of them.
1640 * @notifier: the notifier to be notified
1641 * @event: TLB event with the new entry in the IOMMU translation table.
1642 * The entry replaces all old entries for the same virtual I/O address
1645 void memory_region_notify_iommu_one(IOMMUNotifier
*notifier
,
1646 IOMMUTLBEvent
*event
);
1649 * memory_region_register_iommu_notifier: register a notifier for changes to
1650 * IOMMU translation entries.
1652 * Returns 0 on success, or a negative errno otherwise. In particular,
1653 * -EINVAL indicates that at least one of the attributes of the notifier
1654 * is not supported (flag/range) by the IOMMU memory region. In case of error
1655 * the error object must be created.
1657 * @mr: the memory region to observe
1658 * @n: the IOMMUNotifier to be added; the notify callback receives a
1659 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1660 * ceases to be valid on exit from the notifier.
1661 * @errp: pointer to Error*, to store an error if it happens.
1663 int memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1664 IOMMUNotifier
*n
, Error
**errp
);
1667 * memory_region_iommu_replay: replay existing IOMMU translations to
1668 * a notifier with the minimum page granularity returned by
1669 * mr->iommu_ops->get_page_size().
1671 * Note: this is not related to record-and-replay functionality.
1673 * @iommu_mr: the memory region to observe
1674 * @n: the notifier to which to replay iommu mappings
1676 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
);
1679 * memory_region_unregister_iommu_notifier: unregister a notifier for
1680 * changes to IOMMU translation entries.
1682 * @mr: the memory region which was observed and for which notity_stopped()
1683 * needs to be called
1684 * @n: the notifier to be removed.
1686 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1690 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1691 * defined on the IOMMU.
1693 * Returns 0 on success, or a negative errno otherwise. In particular,
1694 * -EINVAL indicates that the IOMMU does not support the requested
1697 * @iommu_mr: the memory region
1698 * @attr: the requested attribute
1699 * @data: a pointer to the requested attribute data
1701 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1702 enum IOMMUMemoryRegionAttr attr
,
1706 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1707 * use for translations with the given memory transaction attributes.
1709 * @iommu_mr: the memory region
1710 * @attrs: the memory transaction attributes
1712 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1716 * memory_region_iommu_num_indexes: return the total number of IOMMU
1717 * indexes that this IOMMU supports.
1719 * @iommu_mr: the memory region
1721 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
);
1724 * memory_region_iommu_set_page_size_mask: set the supported page
1725 * sizes for a given IOMMU memory region
1727 * @iommu_mr: IOMMU memory region
1728 * @page_size_mask: supported page size mask
1729 * @errp: pointer to Error*, to store an error if it happens.
1731 int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion
*iommu_mr
,
1732 uint64_t page_size_mask
,
1736 * memory_region_name: get a memory region's name
1738 * Returns the string that was used to initialize the memory region.
1740 * @mr: the memory region being queried
1742 const char *memory_region_name(const MemoryRegion
*mr
);
1745 * memory_region_is_logging: return whether a memory region is logging writes
1747 * Returns %true if the memory region is logging writes for the given client
1749 * @mr: the memory region being queried
1750 * @client: the client being queried
1752 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
1755 * memory_region_get_dirty_log_mask: return the clients for which a
1756 * memory region is logging writes.
1758 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1759 * are the bit indices.
1761 * @mr: the memory region being queried
1763 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
1766 * memory_region_is_rom: check whether a memory region is ROM
1768 * Returns %true if a memory region is read-only memory.
1770 * @mr: the memory region being queried
1772 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
1774 return mr
->ram
&& mr
->readonly
;
1778 * memory_region_is_nonvolatile: check whether a memory region is non-volatile
1780 * Returns %true is a memory region is non-volatile memory.
1782 * @mr: the memory region being queried
1784 static inline bool memory_region_is_nonvolatile(MemoryRegion
*mr
)
1786 return mr
->nonvolatile
;
1790 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1792 * Returns a file descriptor backing a file-based RAM memory region,
1793 * or -1 if the region is not a file-based RAM memory region.
1795 * @mr: the RAM or alias memory region being queried.
1797 int memory_region_get_fd(MemoryRegion
*mr
);
1800 * memory_region_from_host: Convert a pointer into a RAM memory region
1801 * and an offset within it.
1803 * Given a host pointer inside a RAM memory region (created with
1804 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1805 * the MemoryRegion and the offset within it.
1807 * Use with care; by the time this function returns, the returned pointer is
1808 * not protected by RCU anymore. If the caller is not within an RCU critical
1809 * section and does not hold the iothread lock, it must have other means of
1810 * protecting the pointer, such as a reference to the region that includes
1811 * the incoming ram_addr_t.
1813 * @ptr: the host pointer to be converted
1814 * @offset: the offset within memory region
1816 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
1819 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1821 * Returns a host pointer to a RAM memory region (created with
1822 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1824 * Use with care; by the time this function returns, the returned pointer is
1825 * not protected by RCU anymore. If the caller is not within an RCU critical
1826 * section and does not hold the iothread lock, it must have other means of
1827 * protecting the pointer, such as a reference to the region that includes
1828 * the incoming ram_addr_t.
1830 * @mr: the memory region being queried.
1832 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
1834 /* memory_region_ram_resize: Resize a RAM region.
1836 * Resizing RAM while migrating can result in the migration being canceled.
1837 * Care has to be taken if the guest might have already detected the memory.
1839 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1840 * @newsize: the new size the region
1841 * @errp: pointer to Error*, to store an error if it happens.
1843 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
1847 * memory_region_msync: Synchronize selected address range of
1848 * a memory mapped region
1850 * @mr: the memory region to be msync
1851 * @addr: the initial address of the range to be sync
1852 * @size: the size of the range to be sync
1854 void memory_region_msync(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1857 * memory_region_writeback: Trigger cache writeback for
1858 * selected address range
1860 * @mr: the memory region to be updated
1861 * @addr: the initial address of the range to be written back
1862 * @size: the size of the range to be written back
1864 void memory_region_writeback(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1867 * memory_region_set_log: Turn dirty logging on or off for a region.
1869 * Turns dirty logging on or off for a specified client (display, migration).
1870 * Only meaningful for RAM regions.
1872 * @mr: the memory region being updated.
1873 * @log: whether dirty logging is to be enabled or disabled.
1874 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1876 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
1879 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1881 * Marks a range of bytes as dirty, after it has been dirtied outside
1884 * @mr: the memory region being dirtied.
1885 * @addr: the address (relative to the start of the region) being dirtied.
1886 * @size: size of the range being dirtied.
1888 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1892 * memory_region_clear_dirty_bitmap - clear dirty bitmap for memory range
1894 * This function is called when the caller wants to clear the remote
1895 * dirty bitmap of a memory range within the memory region. This can
1896 * be used by e.g. KVM to manually clear dirty log when
1897 * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT is declared support by the host
1900 * @mr: the memory region to clear the dirty log upon
1901 * @start: start address offset within the memory region
1902 * @len: length of the memory region to clear dirty bitmap
1904 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
1908 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1909 * bitmap and clear it.
1911 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1912 * returns the snapshot. The snapshot can then be used to query dirty
1913 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1914 * querying the same page multiple times, which is especially useful for
1915 * display updates where the scanlines often are not page aligned.
1917 * The dirty bitmap region which gets copyed into the snapshot (and
1918 * cleared afterwards) can be larger than requested. The boundaries
1919 * are rounded up/down so complete bitmap longs (covering 64 pages on
1920 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1921 * isn't a problem for display updates as the extra pages are outside
1922 * the visible area, and in case the visible area changes a full
1923 * display redraw is due anyway. Should other use cases for this
1924 * function emerge we might have to revisit this implementation
1927 * Use g_free to release DirtyBitmapSnapshot.
1929 * @mr: the memory region being queried.
1930 * @addr: the address (relative to the start of the region) being queried.
1931 * @size: the size of the range being queried.
1932 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1934 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1940 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1941 * in the specified dirty bitmap snapshot.
1943 * @mr: the memory region being queried.
1944 * @snap: the dirty bitmap snapshot
1945 * @addr: the address (relative to the start of the region) being queried.
1946 * @size: the size of the range being queried.
1948 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
1949 DirtyBitmapSnapshot
*snap
,
1950 hwaddr addr
, hwaddr size
);
1953 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1956 * Marks a range of pages as no longer dirty.
1958 * @mr: the region being updated.
1959 * @addr: the start of the subrange being cleaned.
1960 * @size: the size of the subrange being cleaned.
1961 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1962 * %DIRTY_MEMORY_VGA.
1964 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1965 hwaddr size
, unsigned client
);
1968 * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate
1969 * TBs (for self-modifying code).
1971 * The MemoryRegionOps->write() callback of a ROM device must use this function
1972 * to mark byte ranges that have been modified internally, such as by directly
1973 * accessing the memory returned by memory_region_get_ram_ptr().
1975 * This function marks the range dirty and invalidates TBs so that TCG can
1976 * detect self-modifying code.
1978 * @mr: the region being flushed.
1979 * @addr: the start, relative to the start of the region, of the range being
1981 * @size: the size, in bytes, of the range being flushed.
1983 void memory_region_flush_rom_device(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1986 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1988 * Allows a memory region to be marked as read-only (turning it into a ROM).
1989 * only useful on RAM regions.
1991 * @mr: the region being updated.
1992 * @readonly: whether rhe region is to be ROM or RAM.
1994 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
1997 * memory_region_set_nonvolatile: Turn a memory region non-volatile
1999 * Allows a memory region to be marked as non-volatile.
2000 * only useful on RAM regions.
2002 * @mr: the region being updated.
2003 * @nonvolatile: whether rhe region is to be non-volatile.
2005 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
);
2008 * memory_region_rom_device_set_romd: enable/disable ROMD mode
2010 * Allows a ROM device (initialized with memory_region_init_rom_device() to
2011 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
2012 * device is mapped to guest memory and satisfies read access directly.
2013 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
2014 * Writes are always handled by the #MemoryRegion.write function.
2016 * @mr: the memory region to be updated
2017 * @romd_mode: %true to put the region into ROMD mode
2019 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
2022 * memory_region_set_coalescing: Enable memory coalescing for the region.
2024 * Enabled writes to a region to be queued for later processing. MMIO ->write
2025 * callbacks may be delayed until a non-coalesced MMIO is issued.
2026 * Only useful for IO regions. Roughly similar to write-combining hardware.
2028 * @mr: the memory region to be write coalesced
2030 void memory_region_set_coalescing(MemoryRegion
*mr
);
2033 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
2036 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
2037 * Multiple calls can be issued coalesced disjoint ranges.
2039 * @mr: the memory region to be updated.
2040 * @offset: the start of the range within the region to be coalesced.
2041 * @size: the size of the subrange to be coalesced.
2043 void memory_region_add_coalescing(MemoryRegion
*mr
,
2048 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
2050 * Disables any coalescing caused by memory_region_set_coalescing() or
2051 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
2054 * @mr: the memory region to be updated.
2056 void memory_region_clear_coalescing(MemoryRegion
*mr
);
2059 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
2062 * Ensure that pending coalesced MMIO request are flushed before the memory
2063 * region is accessed. This property is automatically enabled for all regions
2064 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
2066 * @mr: the memory region to be updated.
2068 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
2071 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
2074 * Clear the automatic coalesced MMIO flushing enabled via
2075 * memory_region_set_flush_coalesced. Note that this service has no effect on
2076 * memory regions that have MMIO coalescing enabled for themselves. For them,
2077 * automatic flushing will stop once coalescing is disabled.
2079 * @mr: the memory region to be updated.
2081 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
2084 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
2085 * is written to a location.
2087 * Marks a word in an IO region (initialized with memory_region_init_io())
2088 * as a trigger for an eventfd event. The I/O callback will not be called.
2089 * The caller must be prepared to handle failure (that is, take the required
2090 * action if the callback _is_ called).
2092 * @mr: the memory region being updated.
2093 * @addr: the address within @mr that is to be monitored
2094 * @size: the size of the access to trigger the eventfd
2095 * @match_data: whether to match against @data, instead of just @addr
2096 * @data: the data to match against the guest write
2097 * @e: event notifier to be triggered when @addr, @size, and @data all match.
2099 void memory_region_add_eventfd(MemoryRegion
*mr
,
2107 * memory_region_del_eventfd: Cancel an eventfd.
2109 * Cancels an eventfd trigger requested by a previous
2110 * memory_region_add_eventfd() call.
2112 * @mr: the memory region being updated.
2113 * @addr: the address within @mr that is to be monitored
2114 * @size: the size of the access to trigger the eventfd
2115 * @match_data: whether to match against @data, instead of just @addr
2116 * @data: the data to match against the guest write
2117 * @e: event notifier to be triggered when @addr, @size, and @data all match.
2119 void memory_region_del_eventfd(MemoryRegion
*mr
,
2127 * memory_region_add_subregion: Add a subregion to a container.
2129 * Adds a subregion at @offset. The subregion may not overlap with other
2130 * subregions (except for those explicitly marked as overlapping). A region
2131 * may only be added once as a subregion (unless removed with
2132 * memory_region_del_subregion()); use memory_region_init_alias() if you
2133 * want a region to be a subregion in multiple locations.
2135 * @mr: the region to contain the new subregion; must be a container
2136 * initialized with memory_region_init().
2137 * @offset: the offset relative to @mr where @subregion is added.
2138 * @subregion: the subregion to be added.
2140 void memory_region_add_subregion(MemoryRegion
*mr
,
2142 MemoryRegion
*subregion
);
2144 * memory_region_add_subregion_overlap: Add a subregion to a container
2147 * Adds a subregion at @offset. The subregion may overlap with other
2148 * subregions. Conflicts are resolved by having a higher @priority hide a
2149 * lower @priority. Subregions without priority are taken as @priority 0.
2150 * A region may only be added once as a subregion (unless removed with
2151 * memory_region_del_subregion()); use memory_region_init_alias() if you
2152 * want a region to be a subregion in multiple locations.
2154 * @mr: the region to contain the new subregion; must be a container
2155 * initialized with memory_region_init().
2156 * @offset: the offset relative to @mr where @subregion is added.
2157 * @subregion: the subregion to be added.
2158 * @priority: used for resolving overlaps; highest priority wins.
2160 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2162 MemoryRegion
*subregion
,
2166 * memory_region_get_ram_addr: Get the ram address associated with a memory
2169 * @mr: the region to be queried
2171 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
2173 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
2175 * memory_region_del_subregion: Remove a subregion.
2177 * Removes a subregion from its container.
2179 * @mr: the container to be updated.
2180 * @subregion: the region being removed; must be a current subregion of @mr.
2182 void memory_region_del_subregion(MemoryRegion
*mr
,
2183 MemoryRegion
*subregion
);
2186 * memory_region_set_enabled: dynamically enable or disable a region
2188 * Enables or disables a memory region. A disabled memory region
2189 * ignores all accesses to itself and its subregions. It does not
2190 * obscure sibling subregions with lower priority - it simply behaves as
2191 * if it was removed from the hierarchy.
2193 * Regions default to being enabled.
2195 * @mr: the region to be updated
2196 * @enabled: whether to enable or disable the region
2198 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
2201 * memory_region_set_address: dynamically update the address of a region
2203 * Dynamically updates the address of a region, relative to its container.
2204 * May be used on regions are currently part of a memory hierarchy.
2206 * @mr: the region to be updated
2207 * @addr: new address, relative to container region
2209 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
2212 * memory_region_set_size: dynamically update the size of a region.
2214 * Dynamically updates the size of a region.
2216 * @mr: the region to be updated
2217 * @size: used size of the region.
2219 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
2222 * memory_region_set_alias_offset: dynamically update a memory alias's offset
2224 * Dynamically updates the offset into the target region that an alias points
2225 * to, as if the fourth argument to memory_region_init_alias() has changed.
2227 * @mr: the #MemoryRegion to be updated; should be an alias.
2228 * @offset: the new offset into the target memory region
2230 void memory_region_set_alias_offset(MemoryRegion
*mr
,
2234 * memory_region_present: checks if an address relative to a @container
2235 * translates into #MemoryRegion within @container
2237 * Answer whether a #MemoryRegion within @container covers the address
2240 * @container: a #MemoryRegion within which @addr is a relative address
2241 * @addr: the area within @container to be searched
2243 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
2246 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
2247 * into any address space.
2249 * @mr: a #MemoryRegion which should be checked if it's mapped
2251 bool memory_region_is_mapped(MemoryRegion
*mr
);
2254 * memory_region_get_ram_discard_manager: get the #RamDiscardManager for a
2257 * The #RamDiscardManager cannot change while a memory region is mapped.
2259 * @mr: the #MemoryRegion
2261 RamDiscardManager
*memory_region_get_ram_discard_manager(MemoryRegion
*mr
);
2264 * memory_region_has_ram_discard_manager: check whether a #MemoryRegion has a
2265 * #RamDiscardManager assigned
2267 * @mr: the #MemoryRegion
2269 static inline bool memory_region_has_ram_discard_manager(MemoryRegion
*mr
)
2271 return !!memory_region_get_ram_discard_manager(mr
);
2275 * memory_region_set_ram_discard_manager: set the #RamDiscardManager for a
2278 * This function must not be called for a mapped #MemoryRegion, a #MemoryRegion
2279 * that does not cover RAM, or a #MemoryRegion that already has a
2280 * #RamDiscardManager assigned.
2282 * @mr: the #MemoryRegion
2283 * @rdm: #RamDiscardManager to set
2285 void memory_region_set_ram_discard_manager(MemoryRegion
*mr
,
2286 RamDiscardManager
*rdm
);
2289 * memory_region_find: translate an address/size relative to a
2290 * MemoryRegion into a #MemoryRegionSection.
2292 * Locates the first #MemoryRegion within @mr that overlaps the range
2293 * given by @addr and @size.
2295 * Returns a #MemoryRegionSection that describes a contiguous overlap.
2296 * It will have the following characteristics:
2297 * - @size = 0 iff no overlap was found
2298 * - @mr is non-%NULL iff an overlap was found
2300 * Remember that in the return value the @offset_within_region is
2301 * relative to the returned region (in the .@mr field), not to the
2304 * Similarly, the .@offset_within_address_space is relative to the
2305 * address space that contains both regions, the passed and the
2306 * returned one. However, in the special case where the @mr argument
2307 * has no container (and thus is the root of the address space), the
2308 * following will hold:
2309 * - @offset_within_address_space >= @addr
2310 * - @offset_within_address_space + .@size <= @addr + @size
2312 * @mr: a MemoryRegion within which @addr is a relative address
2313 * @addr: start of the area within @as to be searched
2314 * @size: size of the area to be searched
2316 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2317 hwaddr addr
, uint64_t size
);
2320 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
2322 * Synchronizes the dirty page log for all address spaces.
2324 void memory_global_dirty_log_sync(void);
2327 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
2329 * Synchronizes the vCPUs with a thread that is reading the dirty bitmap.
2330 * This function must be called after the dirty log bitmap is cleared, and
2331 * before dirty guest memory pages are read. If you are using
2332 * #DirtyBitmapSnapshot, memory_region_snapshot_and_clear_dirty() takes
2333 * care of doing this.
2335 void memory_global_after_dirty_log_sync(void);
2338 * memory_region_transaction_begin: Start a transaction.
2340 * During a transaction, changes will be accumulated and made visible
2341 * only when the transaction ends (is committed).
2343 void memory_region_transaction_begin(void);
2346 * memory_region_transaction_commit: Commit a transaction and make changes
2347 * visible to the guest.
2349 void memory_region_transaction_commit(void);
2352 * memory_listener_register: register callbacks to be called when memory
2353 * sections are mapped or unmapped into an address
2356 * @listener: an object containing the callbacks to be called
2357 * @filter: if non-%NULL, only regions in this address space will be observed
2359 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
2362 * memory_listener_unregister: undo the effect of memory_listener_register()
2364 * @listener: an object containing the callbacks to be removed
2366 void memory_listener_unregister(MemoryListener
*listener
);
2369 * memory_global_dirty_log_start: begin dirty logging for all regions
2371 void memory_global_dirty_log_start(void);
2374 * memory_global_dirty_log_stop: end dirty logging for all regions
2376 void memory_global_dirty_log_stop(void);
2378 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
, bool disabled
);
2381 * memory_region_dispatch_read: perform a read directly to the specified
2384 * @mr: #MemoryRegion to access
2385 * @addr: address within that region
2386 * @pval: pointer to uint64_t which the data is written to
2387 * @op: size, sign, and endianness of the memory operation
2388 * @attrs: memory transaction attributes to use for the access
2390 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
2396 * memory_region_dispatch_write: perform a write directly to the specified
2399 * @mr: #MemoryRegion to access
2400 * @addr: address within that region
2401 * @data: data to write
2402 * @op: size, sign, and endianness of the memory operation
2403 * @attrs: memory transaction attributes to use for the access
2405 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
2412 * address_space_init: initializes an address space
2414 * @as: an uninitialized #AddressSpace
2415 * @root: a #MemoryRegion that routes addresses for the address space
2416 * @name: an address space name. The name is only used for debugging
2419 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
2422 * address_space_destroy: destroy an address space
2424 * Releases all resources associated with an address space. After an address space
2425 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
2428 * @as: address space to be destroyed
2430 void address_space_destroy(AddressSpace
*as
);
2433 * address_space_remove_listeners: unregister all listeners of an address space
2435 * Removes all callbacks previously registered with memory_listener_register()
2438 * @as: an initialized #AddressSpace
2440 void address_space_remove_listeners(AddressSpace
*as
);
2443 * address_space_rw: read from or write to an address space.
2445 * Return a MemTxResult indicating whether the operation succeeded
2446 * or failed (eg unassigned memory, device rejected the transaction,
2449 * @as: #AddressSpace to be accessed
2450 * @addr: address within that address space
2451 * @attrs: memory transaction attributes
2452 * @buf: buffer with the data transferred
2453 * @len: the number of bytes to read or write
2454 * @is_write: indicates the transfer direction
2456 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
2457 MemTxAttrs attrs
, void *buf
,
2458 hwaddr len
, bool is_write
);
2461 * address_space_write: write to address space.
2463 * Return a MemTxResult indicating whether the operation succeeded
2464 * or failed (eg unassigned memory, device rejected the transaction,
2467 * @as: #AddressSpace to be accessed
2468 * @addr: address within that address space
2469 * @attrs: memory transaction attributes
2470 * @buf: buffer with the data transferred
2471 * @len: the number of bytes to write
2473 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
2475 const void *buf
, hwaddr len
);
2478 * address_space_write_rom: write to address space, including ROM.
2480 * This function writes to the specified address space, but will
2481 * write data to both ROM and RAM. This is used for non-guest
2482 * writes like writes from the gdb debug stub or initial loading
2485 * Note that portions of the write which attempt to write data to
2486 * a device will be silently ignored -- only real RAM and ROM will
2489 * Return a MemTxResult indicating whether the operation succeeded
2490 * or failed (eg unassigned memory, device rejected the transaction,
2493 * @as: #AddressSpace to be accessed
2494 * @addr: address within that address space
2495 * @attrs: memory transaction attributes
2496 * @buf: buffer with the data transferred
2497 * @len: the number of bytes to write
2499 MemTxResult
address_space_write_rom(AddressSpace
*as
, hwaddr addr
,
2501 const void *buf
, hwaddr len
);
2503 /* address_space_ld*: load from an address space
2504 * address_space_st*: store to an address space
2506 * These functions perform a load or store of the byte, word,
2507 * longword or quad to the specified address within the AddressSpace.
2508 * The _le suffixed functions treat the data as little endian;
2509 * _be indicates big endian; no suffix indicates "same endianness
2512 * The "guest CPU endianness" accessors are deprecated for use outside
2513 * target-* code; devices should be CPU-agnostic and use either the LE
2514 * or the BE accessors.
2516 * @as #AddressSpace to be accessed
2517 * @addr: address within that address space
2518 * @val: data value, for stores
2519 * @attrs: memory transaction attributes
2520 * @result: location to write the success/failure of the transaction;
2521 * if NULL, this information is discarded
2526 #define ARG1_DECL AddressSpace *as
2527 #include "exec/memory_ldst.h.inc"
2531 #define ARG1_DECL AddressSpace *as
2532 #include "exec/memory_ldst_phys.h.inc"
2534 struct MemoryRegionCache
{
2539 MemoryRegionSection mrs
;
2543 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
2546 /* address_space_ld*_cached: load from a cached #MemoryRegion
2547 * address_space_st*_cached: store into a cached #MemoryRegion
2549 * These functions perform a load or store of the byte, word,
2550 * longword or quad to the specified address. The address is
2551 * a physical address in the AddressSpace, but it must lie within
2552 * a #MemoryRegion that was mapped with address_space_cache_init.
2554 * The _le suffixed functions treat the data as little endian;
2555 * _be indicates big endian; no suffix indicates "same endianness
2558 * The "guest CPU endianness" accessors are deprecated for use outside
2559 * target-* code; devices should be CPU-agnostic and use either the LE
2560 * or the BE accessors.
2562 * @cache: previously initialized #MemoryRegionCache to be accessed
2563 * @addr: address within the address space
2564 * @val: data value, for stores
2565 * @attrs: memory transaction attributes
2566 * @result: location to write the success/failure of the transaction;
2567 * if NULL, this information is discarded
2570 #define SUFFIX _cached_slow
2572 #define ARG1_DECL MemoryRegionCache *cache
2573 #include "exec/memory_ldst.h.inc"
2575 /* Inline fast path for direct RAM access. */
2576 static inline uint8_t address_space_ldub_cached(MemoryRegionCache
*cache
,
2577 hwaddr addr
, MemTxAttrs attrs
, MemTxResult
*result
)
2579 assert(addr
< cache
->len
);
2580 if (likely(cache
->ptr
)) {
2581 return ldub_p(cache
->ptr
+ addr
);
2583 return address_space_ldub_cached_slow(cache
, addr
, attrs
, result
);
2587 static inline void address_space_stb_cached(MemoryRegionCache
*cache
,
2588 hwaddr addr
, uint8_t val
, MemTxAttrs attrs
, MemTxResult
*result
)
2590 assert(addr
< cache
->len
);
2591 if (likely(cache
->ptr
)) {
2592 stb_p(cache
->ptr
+ addr
, val
);
2594 address_space_stb_cached_slow(cache
, addr
, val
, attrs
, result
);
2598 #define ENDIANNESS _le
2599 #include "exec/memory_ldst_cached.h.inc"
2601 #define ENDIANNESS _be
2602 #include "exec/memory_ldst_cached.h.inc"
2604 #define SUFFIX _cached
2606 #define ARG1_DECL MemoryRegionCache *cache
2607 #include "exec/memory_ldst_phys.h.inc"
2609 /* address_space_cache_init: prepare for repeated access to a physical
2612 * @cache: #MemoryRegionCache to be filled
2613 * @as: #AddressSpace to be accessed
2614 * @addr: address within that address space
2615 * @len: length of buffer
2616 * @is_write: indicates the transfer direction
2618 * Will only work with RAM, and may map a subset of the requested range by
2619 * returning a value that is less than @len. On failure, return a negative
2622 * Because it only works with RAM, this function can be used for
2623 * read-modify-write operations. In this case, is_write should be %true.
2625 * Note that addresses passed to the address_space_*_cached functions
2626 * are relative to @addr.
2628 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
2635 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
2637 * @cache: The #MemoryRegionCache to operate on.
2638 * @addr: The first physical address that was written, relative to the
2639 * address that was passed to @address_space_cache_init.
2640 * @access_len: The number of bytes that were written starting at @addr.
2642 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
2647 * address_space_cache_destroy: free a #MemoryRegionCache
2649 * @cache: The #MemoryRegionCache whose memory should be released.
2651 void address_space_cache_destroy(MemoryRegionCache
*cache
);
2653 /* address_space_get_iotlb_entry: translate an address into an IOTLB
2654 * entry. Should be called from an RCU critical section.
2656 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
2657 bool is_write
, MemTxAttrs attrs
);
2659 /* address_space_translate: translate an address range into an address space
2660 * into a MemoryRegion and an address range into that section. Should be
2661 * called from an RCU critical section, to avoid that the last reference
2662 * to the returned region disappears after address_space_translate returns.
2664 * @fv: #FlatView to be accessed
2665 * @addr: address within that address space
2666 * @xlat: pointer to address within the returned memory region section's
2668 * @len: pointer to length
2669 * @is_write: indicates the transfer direction
2670 * @attrs: memory attributes
2672 MemoryRegion
*flatview_translate(FlatView
*fv
,
2673 hwaddr addr
, hwaddr
*xlat
,
2674 hwaddr
*len
, bool is_write
,
2677 static inline MemoryRegion
*address_space_translate(AddressSpace
*as
,
2678 hwaddr addr
, hwaddr
*xlat
,
2679 hwaddr
*len
, bool is_write
,
2682 return flatview_translate(address_space_to_flatview(as
),
2683 addr
, xlat
, len
, is_write
, attrs
);
2686 /* address_space_access_valid: check for validity of accessing an address
2689 * Check whether memory is assigned to the given address space range, and
2690 * access is permitted by any IOMMU regions that are active for the address
2693 * For now, addr and len should be aligned to a page size. This limitation
2694 * will be lifted in the future.
2696 * @as: #AddressSpace to be accessed
2697 * @addr: address within that address space
2698 * @len: length of the area to be checked
2699 * @is_write: indicates the transfer direction
2700 * @attrs: memory attributes
2702 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, hwaddr len
,
2703 bool is_write
, MemTxAttrs attrs
);
2705 /* address_space_map: map a physical memory region into a host virtual address
2707 * May map a subset of the requested range, given by and returned in @plen.
2708 * May return %NULL and set *@plen to zero(0), if resources needed to perform
2709 * the mapping are exhausted.
2710 * Use only for reads OR writes - not for read-modify-write operations.
2711 * Use cpu_register_map_client() to know when retrying the map operation is
2712 * likely to succeed.
2714 * @as: #AddressSpace to be accessed
2715 * @addr: address within that address space
2716 * @plen: pointer to length of buffer; updated on return
2717 * @is_write: indicates the transfer direction
2718 * @attrs: memory attributes
2720 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
2721 hwaddr
*plen
, bool is_write
, MemTxAttrs attrs
);
2723 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2725 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2726 * the amount of memory that was actually read or written by the caller.
2728 * @as: #AddressSpace used
2729 * @buffer: host pointer as returned by address_space_map()
2730 * @len: buffer length as returned by address_space_map()
2731 * @access_len: amount of data actually transferred
2732 * @is_write: indicates the transfer direction
2734 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
2735 bool is_write
, hwaddr access_len
);
2738 /* Internal functions, part of the implementation of address_space_read. */
2739 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
2740 MemTxAttrs attrs
, void *buf
, hwaddr len
);
2741 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
2742 MemTxAttrs attrs
, void *buf
,
2743 hwaddr len
, hwaddr addr1
, hwaddr l
,
2745 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
2747 /* Internal functions, part of the implementation of address_space_read_cached
2748 * and address_space_write_cached. */
2749 MemTxResult
address_space_read_cached_slow(MemoryRegionCache
*cache
,
2750 hwaddr addr
, void *buf
, hwaddr len
);
2751 MemTxResult
address_space_write_cached_slow(MemoryRegionCache
*cache
,
2752 hwaddr addr
, const void *buf
,
2755 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
2758 return memory_region_is_ram(mr
) && !mr
->readonly
&&
2759 !mr
->rom_device
&& !memory_region_is_ram_device(mr
);
2761 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
2762 memory_region_is_romd(mr
);
2767 * address_space_read: read from an address space.
2769 * Return a MemTxResult indicating whether the operation succeeded
2770 * or failed (eg unassigned memory, device rejected the transaction,
2771 * IOMMU fault). Called within RCU critical section.
2773 * @as: #AddressSpace to be accessed
2774 * @addr: address within that address space
2775 * @attrs: memory transaction attributes
2776 * @buf: buffer with the data transferred
2777 * @len: length of the data transferred
2779 static inline __attribute__((__always_inline__
))
2780 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
2781 MemTxAttrs attrs
, void *buf
,
2784 MemTxResult result
= MEMTX_OK
;
2790 if (__builtin_constant_p(len
)) {
2792 RCU_READ_LOCK_GUARD();
2793 fv
= address_space_to_flatview(as
);
2795 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
2796 if (len
== l
&& memory_access_is_direct(mr
, false)) {
2797 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
2798 memcpy(buf
, ptr
, len
);
2800 result
= flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
2805 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
2811 * address_space_read_cached: read from a cached RAM region
2813 * @cache: Cached region to be addressed
2814 * @addr: address relative to the base of the RAM region
2815 * @buf: buffer with the data transferred
2816 * @len: length of the data transferred
2818 static inline MemTxResult
2819 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2820 void *buf
, hwaddr len
)
2822 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2823 fuzz_dma_read_cb(cache
->xlat
+ addr
, len
, cache
->mrs
.mr
);
2824 if (likely(cache
->ptr
)) {
2825 memcpy(buf
, cache
->ptr
+ addr
, len
);
2828 return address_space_read_cached_slow(cache
, addr
, buf
, len
);
2833 * address_space_write_cached: write to a cached RAM region
2835 * @cache: Cached region to be addressed
2836 * @addr: address relative to the base of the RAM region
2837 * @buf: buffer with the data transferred
2838 * @len: length of the data transferred
2840 static inline MemTxResult
2841 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2842 const void *buf
, hwaddr len
)
2844 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2845 if (likely(cache
->ptr
)) {
2846 memcpy(cache
->ptr
+ addr
, buf
, len
);
2849 return address_space_write_cached_slow(cache
, addr
, buf
, len
);
2854 /* enum device_endian to MemOp. */
2855 static inline MemOp
devend_memop(enum device_endian end
)
2857 QEMU_BUILD_BUG_ON(DEVICE_HOST_ENDIAN
!= DEVICE_LITTLE_ENDIAN
&&
2858 DEVICE_HOST_ENDIAN
!= DEVICE_BIG_ENDIAN
);
2860 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
2861 /* Swap if non-host endianness or native (target) endianness */
2862 return (end
== DEVICE_HOST_ENDIAN
) ? 0 : MO_BSWAP
;
2864 const int non_host_endianness
=
2865 DEVICE_LITTLE_ENDIAN
^ DEVICE_BIG_ENDIAN
^ DEVICE_HOST_ENDIAN
;
2867 /* In this case, native (target) endianness needs no swap. */
2868 return (end
== non_host_endianness
) ? MO_BSWAP
: 0;
2874 * Inhibit technologies that require discarding of pages in RAM blocks, e.g.,
2875 * to manage the actual amount of memory consumed by the VM (then, the memory
2876 * provided by RAM blocks might be bigger than the desired memory consumption).
2877 * This *must* be set if:
2878 * - Discarding parts of a RAM blocks does not result in the change being
2879 * reflected in the VM and the pages getting freed.
2880 * - All memory in RAM blocks is pinned or duplicated, invaldiating any previous
2882 * - Discarding parts of a RAM blocks will result in integrity issues (e.g.,
2884 * Technologies that only temporarily pin the current working set of a
2885 * driver are fine, because we don't expect such pages to be discarded
2886 * (esp. based on guest action like balloon inflation).
2888 * This is *not* to be used to protect from concurrent discards (esp.,
2891 * Returns 0 if successful. Returns -EBUSY if a technology that relies on
2892 * discards to work reliably is active.
2894 int ram_block_discard_disable(bool state
);
2897 * See ram_block_discard_disable(): only disable uncoordinated discards,
2898 * keeping coordinated discards (via the RamDiscardManager) enabled.
2900 int ram_block_uncoordinated_discard_disable(bool state
);
2903 * Inhibit technologies that disable discarding of pages in RAM blocks.
2905 * Returns 0 if successful. Returns -EBUSY if discards are already set to
2908 int ram_block_discard_require(bool state
);
2911 * See ram_block_discard_require(): only inhibit technologies that disable
2912 * uncoordinated discarding of pages in RAM blocks, allowing co-existance with
2913 * technologies that only inhibit uncoordinated discards (via the
2914 * RamDiscardManager).
2916 int ram_block_coordinated_discard_require(bool state
);
2919 * Test if any discarding of memory in ram blocks is disabled.
2921 bool ram_block_discard_is_disabled(void);
2924 * Test if any discarding of memory in ram blocks is required to work reliably.
2926 bool ram_block_discard_is_required(void);