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 /* Possible bits for global_dirty_log_{start|stop} */
66 /* Dirty tracking enabled because migration is running */
67 #define GLOBAL_DIRTY_MIGRATION (1U << 0)
69 /* Dirty tracking enabled because measuring dirty rate */
70 #define GLOBAL_DIRTY_DIRTY_RATE (1U << 1)
72 /* Dirty tracking enabled because dirty limit */
73 #define GLOBAL_DIRTY_LIMIT (1U << 2)
75 #define GLOBAL_DIRTY_MASK (0x7)
77 extern unsigned int global_dirty_tracking
;
79 typedef struct MemoryRegionOps MemoryRegionOps
;
81 struct ReservedRegion
{
88 * struct MemoryRegionSection: describes a fragment of a #MemoryRegion
90 * @mr: the region, or %NULL if empty
91 * @fv: the flat view of the address space the region is mapped in
92 * @offset_within_region: the beginning of the section, relative to @mr's start
93 * @size: the size of the section; will not exceed @mr's boundaries
94 * @offset_within_address_space: the address of the first byte of the section
95 * relative to the region's address space
96 * @readonly: writes to this section are ignored
97 * @nonvolatile: this section is non-volatile
99 struct MemoryRegionSection
{
103 hwaddr offset_within_region
;
104 hwaddr offset_within_address_space
;
109 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
111 /* See address_space_translate: bit 0 is read, bit 1 is write. */
119 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
121 struct IOMMUTLBEntry
{
122 AddressSpace
*target_as
;
124 hwaddr translated_addr
;
125 hwaddr addr_mask
; /* 0xfff = 4k translation */
126 IOMMUAccessFlags perm
;
130 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
131 * register with one or multiple IOMMU Notifier capability bit(s).
133 * Normally there're two use cases for the notifiers:
135 * (1) When the device needs accurate synchronizations of the vIOMMU page
136 * tables, it needs to register with both MAP|UNMAP notifies (which
137 * is defined as IOMMU_NOTIFIER_IOTLB_EVENTS below).
139 * Regarding to accurate synchronization, it's when the notified
140 * device maintains a shadow page table and must be notified on each
141 * guest MAP (page table entry creation) and UNMAP (invalidation)
142 * events (e.g. VFIO). Both notifications must be accurate so that
143 * the shadow page table is fully in sync with the guest view.
145 * (2) When the device doesn't need accurate synchronizations of the
146 * vIOMMU page tables, it needs to register only with UNMAP or
147 * DEVIOTLB_UNMAP notifies.
149 * It's when the device maintains a cache of IOMMU translations
150 * (IOTLB) and is able to fill that cache by requesting translations
151 * from the vIOMMU through a protocol similar to ATS (Address
152 * Translation Service).
154 * Note that in this mode the vIOMMU will not maintain a shadowed
155 * page table for the address space, and the UNMAP messages can cover
156 * more than the pages that used to get mapped. The IOMMU notifiee
157 * should be able to take care of over-sized invalidations.
160 IOMMU_NOTIFIER_NONE
= 0,
161 /* Notify cache invalidations */
162 IOMMU_NOTIFIER_UNMAP
= 0x1,
163 /* Notify entry changes (newly created entries) */
164 IOMMU_NOTIFIER_MAP
= 0x2,
165 /* Notify changes on device IOTLB entries */
166 IOMMU_NOTIFIER_DEVIOTLB_UNMAP
= 0x04,
169 #define IOMMU_NOTIFIER_IOTLB_EVENTS (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
170 #define IOMMU_NOTIFIER_DEVIOTLB_EVENTS IOMMU_NOTIFIER_DEVIOTLB_UNMAP
171 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_IOTLB_EVENTS | \
172 IOMMU_NOTIFIER_DEVIOTLB_EVENTS)
174 struct IOMMUNotifier
;
175 typedef void (*IOMMUNotify
)(struct IOMMUNotifier
*notifier
,
176 IOMMUTLBEntry
*data
);
178 struct IOMMUNotifier
{
180 IOMMUNotifierFlag notifier_flags
;
181 /* Notify for address space range start <= addr <= end */
185 QLIST_ENTRY(IOMMUNotifier
) node
;
187 typedef struct IOMMUNotifier IOMMUNotifier
;
189 typedef struct IOMMUTLBEvent
{
190 IOMMUNotifierFlag type
;
194 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
195 #define RAM_PREALLOC (1 << 0)
197 /* RAM is mmap-ed with MAP_SHARED */
198 #define RAM_SHARED (1 << 1)
200 /* Only a portion of RAM (used_length) is actually used, and migrated.
201 * Resizing RAM while migrating can result in the migration being canceled.
203 #define RAM_RESIZEABLE (1 << 2)
205 /* UFFDIO_ZEROPAGE is available on this RAMBlock to atomically
206 * zero the page and wake waiting processes.
207 * (Set during postcopy)
209 #define RAM_UF_ZEROPAGE (1 << 3)
211 /* RAM can be migrated */
212 #define RAM_MIGRATABLE (1 << 4)
214 /* RAM is a persistent kind memory */
215 #define RAM_PMEM (1 << 5)
219 * UFFDIO_WRITEPROTECT is used on this RAMBlock to
220 * support 'write-tracking' migration type.
221 * Implies ram_state->ram_wt_enabled.
223 #define RAM_UF_WRITEPROTECT (1 << 6)
226 * RAM is mmap-ed with MAP_NORESERVE. When set, reserving swap space (or huge
227 * pages if applicable) is skipped: will bail out if not supported. When not
228 * set, the OS will do the reservation, if supported for the memory type.
230 #define RAM_NORESERVE (1 << 7)
232 /* RAM that isn't accessible through normal means. */
233 #define RAM_PROTECTED (1 << 8)
235 /* RAM is an mmap-ed named file */
236 #define RAM_NAMED_FILE (1 << 9)
238 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
239 IOMMUNotifierFlag flags
,
240 hwaddr start
, hwaddr end
,
244 n
->notifier_flags
= flags
;
247 n
->iommu_idx
= iommu_idx
;
251 * Memory region callbacks
253 struct MemoryRegionOps
{
254 /* Read from the memory region. @addr is relative to @mr; @size is
256 uint64_t (*read
)(void *opaque
,
259 /* Write to the memory region. @addr is relative to @mr; @size is
261 void (*write
)(void *opaque
,
266 MemTxResult (*read_with_attrs
)(void *opaque
,
271 MemTxResult (*write_with_attrs
)(void *opaque
,
277 enum device_endian endianness
;
278 /* Guest-visible constraints: */
280 /* If nonzero, specify bounds on access sizes beyond which a machine
283 unsigned min_access_size
;
284 unsigned max_access_size
;
285 /* If true, unaligned accesses are supported. Otherwise unaligned
286 * accesses throw machine checks.
290 * If present, and returns #false, the transaction is not accepted
291 * by the device (and results in machine dependent behaviour such
292 * as a machine check exception).
294 bool (*accepts
)(void *opaque
, hwaddr addr
,
295 unsigned size
, bool is_write
,
298 /* Internal implementation constraints: */
300 /* If nonzero, specifies the minimum size implemented. Smaller sizes
301 * will be rounded upwards and a partial result will be returned.
303 unsigned min_access_size
;
304 /* If nonzero, specifies the maximum size implemented. Larger sizes
305 * will be done as a series of accesses with smaller sizes.
307 unsigned max_access_size
;
308 /* If true, unaligned accesses are supported. Otherwise all accesses
309 * are converted to (possibly multiple) naturally aligned accesses.
315 typedef struct MemoryRegionClass
{
317 ObjectClass parent_class
;
321 enum IOMMUMemoryRegionAttr
{
322 IOMMU_ATTR_SPAPR_TCE_FD
326 * IOMMUMemoryRegionClass:
328 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
329 * and provide an implementation of at least the @translate method here
330 * to handle requests to the memory region. Other methods are optional.
332 * The IOMMU implementation must use the IOMMU notifier infrastructure
333 * to report whenever mappings are changed, by calling
334 * memory_region_notify_iommu() (or, if necessary, by calling
335 * memory_region_notify_iommu_one() for each registered notifier).
337 * Conceptually an IOMMU provides a mapping from input address
338 * to an output TLB entry. If the IOMMU is aware of memory transaction
339 * attributes and the output TLB entry depends on the transaction
340 * attributes, we represent this using IOMMU indexes. Each index
341 * selects a particular translation table that the IOMMU has:
343 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
345 * @translate takes an input address and an IOMMU index
347 * and the mapping returned can only depend on the input address and the
350 * Most IOMMUs don't care about the transaction attributes and support
351 * only a single IOMMU index. A more complex IOMMU might have one index
352 * for secure transactions and one for non-secure transactions.
354 struct IOMMUMemoryRegionClass
{
356 MemoryRegionClass parent_class
;
362 * Return a TLB entry that contains a given address.
364 * The IOMMUAccessFlags indicated via @flag are optional and may
365 * be specified as IOMMU_NONE to indicate that the caller needs
366 * the full translation information for both reads and writes. If
367 * the access flags are specified then the IOMMU implementation
368 * may use this as an optimization, to stop doing a page table
369 * walk as soon as it knows that the requested permissions are not
370 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
371 * full page table walk and report the permissions in the returned
372 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
373 * return different mappings for reads and writes.)
375 * The returned information remains valid while the caller is
376 * holding the big QEMU lock or is inside an RCU critical section;
377 * if the caller wishes to cache the mapping beyond that it must
378 * register an IOMMU notifier so it can invalidate its cached
379 * information when the IOMMU mapping changes.
381 * @iommu: the IOMMUMemoryRegion
383 * @hwaddr: address to be translated within the memory region
385 * @flag: requested access permission
387 * @iommu_idx: IOMMU index for the translation
389 IOMMUTLBEntry (*translate
)(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
390 IOMMUAccessFlags flag
, int iommu_idx
);
392 * @get_min_page_size:
394 * Returns minimum supported page size in bytes.
396 * If this method is not provided then the minimum is assumed to
397 * be TARGET_PAGE_SIZE.
399 * @iommu: the IOMMUMemoryRegion
401 uint64_t (*get_min_page_size
)(IOMMUMemoryRegion
*iommu
);
403 * @notify_flag_changed:
405 * Called when IOMMU Notifier flag changes (ie when the set of
406 * events which IOMMU users are requesting notification for changes).
407 * Optional method -- need not be provided if the IOMMU does not
408 * need to know exactly which events must be notified.
410 * @iommu: the IOMMUMemoryRegion
412 * @old_flags: events which previously needed to be notified
414 * @new_flags: events which now need to be notified
416 * Returns 0 on success, or a negative errno; in particular
417 * returns -EINVAL if the new flag bitmap is not supported by the
418 * IOMMU memory region. In case of failure, the error object
421 int (*notify_flag_changed
)(IOMMUMemoryRegion
*iommu
,
422 IOMMUNotifierFlag old_flags
,
423 IOMMUNotifierFlag new_flags
,
428 * Called to handle memory_region_iommu_replay().
430 * The default implementation of memory_region_iommu_replay() is to
431 * call the IOMMU translate method for every page in the address space
432 * with flag == IOMMU_NONE and then call the notifier if translate
433 * returns a valid mapping. If this method is implemented then it
434 * overrides the default behaviour, and must provide the full semantics
435 * of memory_region_iommu_replay(), by calling @notifier for every
436 * translation present in the IOMMU.
438 * Optional method -- an IOMMU only needs to provide this method
439 * if the default is inefficient or produces undesirable side effects.
441 * Note: this is not related to record-and-replay functionality.
443 void (*replay
)(IOMMUMemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
448 * Get IOMMU misc attributes. This is an optional method that
449 * can be used to allow users of the IOMMU to get implementation-specific
450 * information. The IOMMU implements this method to handle calls
451 * by IOMMU users to memory_region_iommu_get_attr() by filling in
452 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
453 * the IOMMU supports. If the method is unimplemented then
454 * memory_region_iommu_get_attr() will always return -EINVAL.
456 * @iommu: the IOMMUMemoryRegion
458 * @attr: attribute being queried
460 * @data: memory to fill in with the attribute data
462 * Returns 0 on success, or a negative errno; in particular
463 * returns -EINVAL for unrecognized or unimplemented attribute types.
465 int (*get_attr
)(IOMMUMemoryRegion
*iommu
, enum IOMMUMemoryRegionAttr attr
,
471 * Return the IOMMU index to use for a given set of transaction attributes.
473 * Optional method: if an IOMMU only supports a single IOMMU index then
474 * the default implementation of memory_region_iommu_attrs_to_index()
477 * The indexes supported by an IOMMU must be contiguous, starting at 0.
479 * @iommu: the IOMMUMemoryRegion
480 * @attrs: memory transaction attributes
482 int (*attrs_to_index
)(IOMMUMemoryRegion
*iommu
, MemTxAttrs attrs
);
487 * Return the number of IOMMU indexes this IOMMU supports.
489 * Optional method: if this method is not provided, then
490 * memory_region_iommu_num_indexes() will return 1, indicating that
491 * only a single IOMMU index is supported.
493 * @iommu: the IOMMUMemoryRegion
495 int (*num_indexes
)(IOMMUMemoryRegion
*iommu
);
498 * @iommu_set_page_size_mask:
500 * Restrict the page size mask that can be supported with a given IOMMU
501 * memory region. Used for example to propagate host physical IOMMU page
502 * size mask limitations to the virtual IOMMU.
504 * Optional method: if this method is not provided, then the default global
507 * @iommu: the IOMMUMemoryRegion
509 * @page_size_mask: a bitmask of supported page sizes. At least one bit,
510 * representing the smallest page size, must be set. Additional set bits
511 * represent supported block sizes. For example a host physical IOMMU that
512 * uses page tables with a page size of 4kB, and supports 2MB and 4GB
513 * blocks, will set mask 0x40201000. A granule of 4kB with indiscriminate
514 * block sizes is specified with mask 0xfffffffffffff000.
516 * Returns 0 on success, or a negative error. In case of failure, the error
517 * object must be created.
519 int (*iommu_set_page_size_mask
)(IOMMUMemoryRegion
*iommu
,
520 uint64_t page_size_mask
,
524 typedef struct RamDiscardListener RamDiscardListener
;
525 typedef int (*NotifyRamPopulate
)(RamDiscardListener
*rdl
,
526 MemoryRegionSection
*section
);
527 typedef void (*NotifyRamDiscard
)(RamDiscardListener
*rdl
,
528 MemoryRegionSection
*section
);
530 struct RamDiscardListener
{
534 * Notification that previously discarded memory is about to get populated.
535 * Listeners are able to object. If any listener objects, already
536 * successfully notified listeners are notified about a discard again.
538 * @rdl: the #RamDiscardListener getting notified
539 * @section: the #MemoryRegionSection to get populated. The section
540 * is aligned within the memory region to the minimum granularity
541 * unless it would exceed the registered section.
543 * Returns 0 on success. If the notification is rejected by the listener,
544 * an error is returned.
546 NotifyRamPopulate notify_populate
;
551 * Notification that previously populated memory was discarded successfully
552 * and listeners should drop all references to such memory and prevent
553 * new population (e.g., unmap).
555 * @rdl: the #RamDiscardListener getting notified
556 * @section: the #MemoryRegionSection to get populated. The section
557 * is aligned within the memory region to the minimum granularity
558 * unless it would exceed the registered section.
560 NotifyRamDiscard notify_discard
;
563 * @double_discard_supported:
565 * The listener suppors getting @notify_discard notifications that span
566 * already discarded parts.
568 bool double_discard_supported
;
570 MemoryRegionSection
*section
;
571 QLIST_ENTRY(RamDiscardListener
) next
;
574 static inline void ram_discard_listener_init(RamDiscardListener
*rdl
,
575 NotifyRamPopulate populate_fn
,
576 NotifyRamDiscard discard_fn
,
577 bool double_discard_supported
)
579 rdl
->notify_populate
= populate_fn
;
580 rdl
->notify_discard
= discard_fn
;
581 rdl
->double_discard_supported
= double_discard_supported
;
584 typedef int (*ReplayRamPopulate
)(MemoryRegionSection
*section
, void *opaque
);
585 typedef void (*ReplayRamDiscard
)(MemoryRegionSection
*section
, void *opaque
);
588 * RamDiscardManagerClass:
590 * A #RamDiscardManager coordinates which parts of specific RAM #MemoryRegion
591 * regions are currently populated to be used/accessed by the VM, notifying
592 * after parts were discarded (freeing up memory) and before parts will be
593 * populated (consuming memory), to be used/accessed by the VM.
595 * A #RamDiscardManager can only be set for a RAM #MemoryRegion while the
596 * #MemoryRegion isn't mapped yet; it cannot change while the #MemoryRegion is
599 * The #RamDiscardManager is intended to be used by technologies that are
600 * incompatible with discarding of RAM (e.g., VFIO, which may pin all
601 * memory inside a #MemoryRegion), and require proper coordination to only
602 * map the currently populated parts, to hinder parts that are expected to
603 * remain discarded from silently getting populated and consuming memory.
604 * Technologies that support discarding of RAM don't have to bother and can
605 * simply map the whole #MemoryRegion.
607 * An example #RamDiscardManager is virtio-mem, which logically (un)plugs
608 * memory within an assigned RAM #MemoryRegion, coordinated with the VM.
609 * Logically unplugging memory consists of discarding RAM. The VM agreed to not
610 * access unplugged (discarded) memory - especially via DMA. virtio-mem will
611 * properly coordinate with listeners before memory is plugged (populated),
612 * and after memory is unplugged (discarded).
614 * Listeners are called in multiples of the minimum granularity (unless it
615 * would exceed the registered range) and changes are aligned to the minimum
616 * granularity within the #MemoryRegion. Listeners have to prepare for memory
617 * becoming discarded in a different granularity than it was populated and the
620 struct RamDiscardManagerClass
{
622 InterfaceClass parent_class
;
627 * @get_min_granularity:
629 * Get the minimum granularity in which listeners will get notified
630 * about changes within the #MemoryRegion via the #RamDiscardManager.
632 * @rdm: the #RamDiscardManager
633 * @mr: the #MemoryRegion
635 * Returns the minimum granularity.
637 uint64_t (*get_min_granularity
)(const RamDiscardManager
*rdm
,
638 const MemoryRegion
*mr
);
643 * Check whether the given #MemoryRegionSection is completely populated
644 * (i.e., no parts are currently discarded) via the #RamDiscardManager.
645 * There are no alignment requirements.
647 * @rdm: the #RamDiscardManager
648 * @section: the #MemoryRegionSection
650 * Returns whether the given range is completely populated.
652 bool (*is_populated
)(const RamDiscardManager
*rdm
,
653 const MemoryRegionSection
*section
);
658 * Call the #ReplayRamPopulate callback for all populated parts within the
659 * #MemoryRegionSection via the #RamDiscardManager.
661 * In case any call fails, no further calls are made.
663 * @rdm: the #RamDiscardManager
664 * @section: the #MemoryRegionSection
665 * @replay_fn: the #ReplayRamPopulate callback
666 * @opaque: pointer to forward to the callback
668 * Returns 0 on success, or a negative error if any notification failed.
670 int (*replay_populated
)(const RamDiscardManager
*rdm
,
671 MemoryRegionSection
*section
,
672 ReplayRamPopulate replay_fn
, void *opaque
);
677 * Call the #ReplayRamDiscard callback for all discarded parts within the
678 * #MemoryRegionSection via the #RamDiscardManager.
680 * @rdm: the #RamDiscardManager
681 * @section: the #MemoryRegionSection
682 * @replay_fn: the #ReplayRamDiscard callback
683 * @opaque: pointer to forward to the callback
685 void (*replay_discarded
)(const RamDiscardManager
*rdm
,
686 MemoryRegionSection
*section
,
687 ReplayRamDiscard replay_fn
, void *opaque
);
690 * @register_listener:
692 * Register a #RamDiscardListener for the given #MemoryRegionSection and
693 * immediately notify the #RamDiscardListener about all populated parts
694 * within the #MemoryRegionSection via the #RamDiscardManager.
696 * In case any notification fails, no further notifications are triggered
697 * and an error is logged.
699 * @rdm: the #RamDiscardManager
700 * @rdl: the #RamDiscardListener
701 * @section: the #MemoryRegionSection
703 void (*register_listener
)(RamDiscardManager
*rdm
,
704 RamDiscardListener
*rdl
,
705 MemoryRegionSection
*section
);
708 * @unregister_listener:
710 * Unregister a previously registered #RamDiscardListener via the
711 * #RamDiscardManager after notifying the #RamDiscardListener about all
712 * populated parts becoming unpopulated within the registered
713 * #MemoryRegionSection.
715 * @rdm: the #RamDiscardManager
716 * @rdl: the #RamDiscardListener
718 void (*unregister_listener
)(RamDiscardManager
*rdm
,
719 RamDiscardListener
*rdl
);
722 uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager
*rdm
,
723 const MemoryRegion
*mr
);
725 bool ram_discard_manager_is_populated(const RamDiscardManager
*rdm
,
726 const MemoryRegionSection
*section
);
728 int ram_discard_manager_replay_populated(const RamDiscardManager
*rdm
,
729 MemoryRegionSection
*section
,
730 ReplayRamPopulate replay_fn
,
733 void ram_discard_manager_replay_discarded(const RamDiscardManager
*rdm
,
734 MemoryRegionSection
*section
,
735 ReplayRamDiscard replay_fn
,
738 void ram_discard_manager_register_listener(RamDiscardManager
*rdm
,
739 RamDiscardListener
*rdl
,
740 MemoryRegionSection
*section
);
742 void ram_discard_manager_unregister_listener(RamDiscardManager
*rdm
,
743 RamDiscardListener
*rdl
);
745 bool memory_get_xlat_addr(IOMMUTLBEntry
*iotlb
, void **vaddr
,
746 ram_addr_t
*ram_addr
, bool *read_only
,
747 bool *mr_has_discard_manager
);
749 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
750 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
754 * A struct representing a memory region.
756 struct MemoryRegion
{
761 /* The following fields should fit in a cache line */
765 bool readonly
; /* For RAM regions */
768 bool flush_coalesced_mmio
;
769 uint8_t dirty_log_mask
;
773 /* owner as TYPE_DEVICE. Used for re-entrancy checks in MR access hotpath */
776 const MemoryRegionOps
*ops
;
778 MemoryRegion
*container
;
779 int mapped_via_alias
; /* Mapped via an alias, container might be NULL */
782 void (*destructor
)(MemoryRegion
*mr
);
787 bool warning_printed
; /* For reservations */
788 uint8_t vga_logging_count
;
792 QTAILQ_HEAD(, MemoryRegion
) subregions
;
793 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
794 QTAILQ_HEAD(, CoalescedMemoryRange
) coalesced
;
796 unsigned ioeventfd_nb
;
797 MemoryRegionIoeventfd
*ioeventfds
;
798 RamDiscardManager
*rdm
; /* Only for RAM */
800 /* For devices designed to perform re-entrant IO into their own IO MRs */
801 bool disable_reentrancy_guard
;
804 struct IOMMUMemoryRegion
{
805 MemoryRegion parent_obj
;
807 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
808 IOMMUNotifierFlag iommu_notify_flags
;
811 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
812 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
815 * struct MemoryListener: callbacks structure for updates to the physical memory map
817 * Allows a component to adjust to changes in the guest-visible memory map.
818 * Use with memory_listener_register() and memory_listener_unregister().
820 struct MemoryListener
{
824 * Called at the beginning of an address space update transaction.
825 * Followed by calls to #MemoryListener.region_add(),
826 * #MemoryListener.region_del(), #MemoryListener.region_nop(),
827 * #MemoryListener.log_start() and #MemoryListener.log_stop() in
828 * increasing address order.
830 * @listener: The #MemoryListener.
832 void (*begin
)(MemoryListener
*listener
);
837 * Called at the end of an address space update transaction,
838 * after the last call to #MemoryListener.region_add(),
839 * #MemoryListener.region_del() or #MemoryListener.region_nop(),
840 * #MemoryListener.log_start() and #MemoryListener.log_stop().
842 * @listener: The #MemoryListener.
844 void (*commit
)(MemoryListener
*listener
);
849 * Called during an address space update transaction,
850 * for a section of the address space that is new in this address space
851 * space since the last transaction.
853 * @listener: The #MemoryListener.
854 * @section: The new #MemoryRegionSection.
856 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
861 * Called during an address space update transaction,
862 * for a section of the address space that has disappeared in the address
863 * space since the last transaction.
865 * @listener: The #MemoryListener.
866 * @section: The old #MemoryRegionSection.
868 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
873 * Called during an address space update transaction,
874 * for a section of the address space that is in the same place in the address
875 * space as in the last transaction.
877 * @listener: The #MemoryListener.
878 * @section: The #MemoryRegionSection.
880 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
885 * Called during an address space update transaction, after
886 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or
887 * #MemoryListener.region_nop(), if dirty memory logging clients have
888 * become active since the last transaction.
890 * @listener: The #MemoryListener.
891 * @section: The #MemoryRegionSection.
892 * @old: A bitmap of dirty memory logging clients that were active in
893 * the previous transaction.
894 * @new: A bitmap of dirty memory logging clients that are active in
895 * the current transaction.
897 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
903 * Called during an address space update transaction, after
904 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or
905 * #MemoryListener.region_nop() and possibly after
906 * #MemoryListener.log_start(), if dirty memory logging clients have
907 * become inactive since the last transaction.
909 * @listener: The #MemoryListener.
910 * @section: The #MemoryRegionSection.
911 * @old: A bitmap of dirty memory logging clients that were active in
912 * the previous transaction.
913 * @new: A bitmap of dirty memory logging clients that are active in
914 * the current transaction.
916 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
922 * Called by memory_region_snapshot_and_clear_dirty() and
923 * memory_global_dirty_log_sync(), before accessing QEMU's "official"
924 * copy of the dirty memory bitmap for a #MemoryRegionSection.
926 * @listener: The #MemoryListener.
927 * @section: The #MemoryRegionSection.
929 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
934 * This is the global version of @log_sync when the listener does
935 * not have a way to synchronize the log with finer granularity.
936 * When the listener registers with @log_sync_global defined, then
937 * its @log_sync must be NULL. Vice versa.
939 * @listener: The #MemoryListener.
940 * @last_stage: The last stage to synchronize the log during migration.
941 * The caller should gurantee that the synchronization with true for
942 * @last_stage is triggered for once after all VCPUs have been stopped.
944 void (*log_sync_global
)(MemoryListener
*listener
, bool last_stage
);
949 * Called before reading the dirty memory bitmap for a
950 * #MemoryRegionSection.
952 * @listener: The #MemoryListener.
953 * @section: The #MemoryRegionSection.
955 void (*log_clear
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
960 * Called by memory_global_dirty_log_start(), which
961 * enables the %DIRTY_LOG_MIGRATION client on all memory regions in
962 * the address space. #MemoryListener.log_global_start() is also
963 * called when a #MemoryListener is added, if global dirty logging is
964 * active at that time.
966 * @listener: The #MemoryListener.
968 void (*log_global_start
)(MemoryListener
*listener
);
973 * Called by memory_global_dirty_log_stop(), which
974 * disables the %DIRTY_LOG_MIGRATION client on all memory regions in
977 * @listener: The #MemoryListener.
979 void (*log_global_stop
)(MemoryListener
*listener
);
982 * @log_global_after_sync:
984 * Called after reading the dirty memory bitmap
985 * for any #MemoryRegionSection.
987 * @listener: The #MemoryListener.
989 void (*log_global_after_sync
)(MemoryListener
*listener
);
994 * Called during an address space update transaction,
995 * for a section of the address space that has had a new ioeventfd
996 * registration since the last transaction.
998 * @listener: The #MemoryListener.
999 * @section: The new #MemoryRegionSection.
1000 * @match_data: The @match_data parameter for the new ioeventfd.
1001 * @data: The @data parameter for the new ioeventfd.
1002 * @e: The #EventNotifier parameter for the new ioeventfd.
1004 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
1005 bool match_data
, uint64_t data
, EventNotifier
*e
);
1010 * Called during an address space update transaction,
1011 * for a section of the address space that has dropped an ioeventfd
1012 * registration since the last transaction.
1014 * @listener: The #MemoryListener.
1015 * @section: The new #MemoryRegionSection.
1016 * @match_data: The @match_data parameter for the dropped ioeventfd.
1017 * @data: The @data parameter for the dropped ioeventfd.
1018 * @e: The #EventNotifier parameter for the dropped ioeventfd.
1020 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
1021 bool match_data
, uint64_t data
, EventNotifier
*e
);
1024 * @coalesced_io_add:
1026 * Called during an address space update transaction,
1027 * for a section of the address space that has had a new coalesced
1028 * MMIO range registration since the last transaction.
1030 * @listener: The #MemoryListener.
1031 * @section: The new #MemoryRegionSection.
1032 * @addr: The starting address for the coalesced MMIO range.
1033 * @len: The length of the coalesced MMIO range.
1035 void (*coalesced_io_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
1036 hwaddr addr
, hwaddr len
);
1039 * @coalesced_io_del:
1041 * Called during an address space update transaction,
1042 * for a section of the address space that has dropped a coalesced
1043 * MMIO range since the last transaction.
1045 * @listener: The #MemoryListener.
1046 * @section: The new #MemoryRegionSection.
1047 * @addr: The starting address for the coalesced MMIO range.
1048 * @len: The length of the coalesced MMIO range.
1050 void (*coalesced_io_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
1051 hwaddr addr
, hwaddr len
);
1055 * Govern the order in which memory listeners are invoked. Lower priorities
1056 * are invoked earlier for "add" or "start" callbacks, and later for "delete"
1057 * or "stop" callbacks.
1064 * Name of the listener. It can be used in contexts where we'd like to
1065 * identify one memory listener with the rest.
1070 AddressSpace
*address_space
;
1071 QTAILQ_ENTRY(MemoryListener
) link
;
1072 QTAILQ_ENTRY(MemoryListener
) link_as
;
1076 * struct AddressSpace: describes a mapping of addresses to #MemoryRegion objects
1078 struct AddressSpace
{
1080 struct rcu_head rcu
;
1084 /* Accessed via RCU. */
1085 struct FlatView
*current_map
;
1088 struct MemoryRegionIoeventfd
*ioeventfds
;
1089 QTAILQ_HEAD(, MemoryListener
) listeners
;
1090 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
1093 typedef struct AddressSpaceDispatch AddressSpaceDispatch
;
1094 typedef struct FlatRange FlatRange
;
1096 /* Flattened global view of current active memory hierarchy. Kept in sorted
1100 struct rcu_head rcu
;
1104 unsigned nr_allocated
;
1105 struct AddressSpaceDispatch
*dispatch
;
1109 static inline FlatView
*address_space_to_flatview(AddressSpace
*as
)
1111 return qatomic_rcu_read(&as
->current_map
);
1115 * typedef flatview_cb: callback for flatview_for_each_range()
1117 * @start: start address of the range within the FlatView
1118 * @len: length of the range in bytes
1119 * @mr: MemoryRegion covering this range
1120 * @offset_in_region: offset of the first byte of the range within @mr
1121 * @opaque: data pointer passed to flatview_for_each_range()
1123 * Returns: true to stop the iteration, false to keep going.
1125 typedef bool (*flatview_cb
)(Int128 start
,
1127 const MemoryRegion
*mr
,
1128 hwaddr offset_in_region
,
1132 * flatview_for_each_range: Iterate through a FlatView
1133 * @fv: the FlatView to iterate through
1134 * @cb: function to call for each range
1135 * @opaque: opaque data pointer to pass to @cb
1137 * A FlatView is made up of a list of non-overlapping ranges, each of
1138 * which is a slice of a MemoryRegion. This function iterates through
1139 * each range in @fv, calling @cb. The callback function can terminate
1140 * iteration early by returning 'true'.
1142 void flatview_for_each_range(FlatView
*fv
, flatview_cb cb
, void *opaque
);
1144 static inline bool MemoryRegionSection_eq(MemoryRegionSection
*a
,
1145 MemoryRegionSection
*b
)
1147 return a
->mr
== b
->mr
&&
1149 a
->offset_within_region
== b
->offset_within_region
&&
1150 a
->offset_within_address_space
== b
->offset_within_address_space
&&
1151 int128_eq(a
->size
, b
->size
) &&
1152 a
->readonly
== b
->readonly
&&
1153 a
->nonvolatile
== b
->nonvolatile
;
1157 * memory_region_section_new_copy: Copy a memory region section
1159 * Allocate memory for a new copy, copy the memory region section, and
1160 * properly take a reference on all relevant members.
1162 * @s: the #MemoryRegionSection to copy
1164 MemoryRegionSection
*memory_region_section_new_copy(MemoryRegionSection
*s
);
1167 * memory_region_section_new_copy: Free a copied memory region section
1169 * Free a copy of a memory section created via memory_region_section_new_copy().
1170 * properly dropping references on all relevant members.
1172 * @s: the #MemoryRegionSection to copy
1174 void memory_region_section_free_copy(MemoryRegionSection
*s
);
1177 * memory_region_init: Initialize a memory region
1179 * The region typically acts as a container for other memory regions. Use
1180 * memory_region_add_subregion() to add subregions.
1182 * @mr: the #MemoryRegion to be initialized
1183 * @owner: the object that tracks the region's reference count
1184 * @name: used for debugging; not visible to the user or ABI
1185 * @size: size of the region; any subregions beyond this size will be clipped
1187 void memory_region_init(MemoryRegion
*mr
,
1193 * memory_region_ref: Add 1 to a memory region's reference count
1195 * Whenever memory regions are accessed outside the BQL, they need to be
1196 * preserved against hot-unplug. MemoryRegions actually do not have their
1197 * own reference count; they piggyback on a QOM object, their "owner".
1198 * This function adds a reference to the owner.
1200 * All MemoryRegions must have an owner if they can disappear, even if the
1201 * device they belong to operates exclusively under the BQL. This is because
1202 * the region could be returned at any time by memory_region_find, and this
1203 * is usually under guest control.
1205 * @mr: the #MemoryRegion
1207 void memory_region_ref(MemoryRegion
*mr
);
1210 * memory_region_unref: Remove 1 to a memory region's reference count
1212 * Whenever memory regions are accessed outside the BQL, they need to be
1213 * preserved against hot-unplug. MemoryRegions actually do not have their
1214 * own reference count; they piggyback on a QOM object, their "owner".
1215 * This function removes a reference to the owner and possibly destroys it.
1217 * @mr: the #MemoryRegion
1219 void memory_region_unref(MemoryRegion
*mr
);
1222 * memory_region_init_io: Initialize an I/O memory region.
1224 * Accesses into the region will cause the callbacks in @ops to be called.
1225 * if @size is nonzero, subregions will be clipped to @size.
1227 * @mr: the #MemoryRegion to be initialized.
1228 * @owner: the object that tracks the region's reference count
1229 * @ops: a structure containing read and write callbacks to be used when
1230 * I/O is performed on the region.
1231 * @opaque: passed to the read and write callbacks of the @ops structure.
1232 * @name: used for debugging; not visible to the user or ABI
1233 * @size: size of the region.
1235 void memory_region_init_io(MemoryRegion
*mr
,
1237 const MemoryRegionOps
*ops
,
1243 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
1244 * into the region will modify memory
1247 * @mr: the #MemoryRegion to be initialized.
1248 * @owner: the object that tracks the region's reference count
1249 * @name: Region name, becomes part of RAMBlock name used in migration stream
1250 * must be unique within any device
1251 * @size: size of the region.
1252 * @errp: pointer to Error*, to store an error if it happens.
1254 * Note that this function does not do anything to cause the data in the
1255 * RAM memory region to be migrated; that is the responsibility of the caller.
1257 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1264 * memory_region_init_ram_flags_nomigrate: Initialize RAM memory region.
1265 * Accesses into the region will
1266 * modify memory directly.
1268 * @mr: the #MemoryRegion to be initialized.
1269 * @owner: the object that tracks the region's reference count
1270 * @name: Region name, becomes part of RAMBlock name used in migration stream
1271 * must be unique within any device
1272 * @size: size of the region.
1273 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_NORESERVE.
1274 * @errp: pointer to Error*, to store an error if it happens.
1276 * Note that this function does not do anything to cause the data in the
1277 * RAM memory region to be migrated; that is the responsibility of the caller.
1279 void memory_region_init_ram_flags_nomigrate(MemoryRegion
*mr
,
1287 * memory_region_init_resizeable_ram: Initialize memory region with resizable
1288 * RAM. Accesses into the region will
1289 * modify memory directly. Only an initial
1290 * portion of this RAM is actually used.
1291 * Changing the size while migrating
1292 * can result in the migration being
1295 * @mr: the #MemoryRegion to be initialized.
1296 * @owner: the object that tracks the region's reference count
1297 * @name: Region name, becomes part of RAMBlock name used in migration stream
1298 * must be unique within any device
1299 * @size: used size of the region.
1300 * @max_size: max size of the region.
1301 * @resized: callback to notify owner about used size change.
1302 * @errp: pointer to Error*, to store an error if it happens.
1304 * Note that this function does not do anything to cause the data in the
1305 * RAM memory region to be migrated; that is the responsibility of the caller.
1307 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1312 void (*resized
)(const char*,
1319 * memory_region_init_ram_from_file: Initialize RAM memory region with a
1322 * @mr: the #MemoryRegion to be initialized.
1323 * @owner: the object that tracks the region's reference count
1324 * @name: Region name, becomes part of RAMBlock name used in migration stream
1325 * must be unique within any device
1326 * @size: size of the region.
1327 * @align: alignment of the region base address; if 0, the default alignment
1328 * (getpagesize()) will be used.
1329 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
1331 * @path: the path in which to allocate the RAM.
1332 * @offset: offset within the file referenced by path
1333 * @readonly: true to open @path for reading, false for read/write.
1334 * @errp: pointer to Error*, to store an error if it happens.
1336 * Note that this function does not do anything to cause the data in the
1337 * RAM memory region to be migrated; that is the responsibility of the caller.
1339 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1351 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
1354 * @mr: the #MemoryRegion to be initialized.
1355 * @owner: the object that tracks the region's reference count
1356 * @name: the name of the region.
1357 * @size: size of the region.
1358 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
1359 * RAM_NORESERVE, RAM_PROTECTED.
1360 * @fd: the fd to mmap.
1361 * @offset: offset within the file referenced by fd
1362 * @errp: pointer to Error*, to store an error if it happens.
1364 * Note that this function does not do anything to cause the data in the
1365 * RAM memory region to be migrated; that is the responsibility of the caller.
1367 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1378 * memory_region_init_ram_ptr: Initialize RAM memory region from a
1379 * user-provided pointer. Accesses into the
1380 * region will modify memory directly.
1382 * @mr: the #MemoryRegion to be initialized.
1383 * @owner: the object that tracks the region's reference count
1384 * @name: Region name, becomes part of RAMBlock name used in migration stream
1385 * must be unique within any device
1386 * @size: size of the region.
1387 * @ptr: memory to be mapped; must contain at least @size bytes.
1389 * Note that this function does not do anything to cause the data in the
1390 * RAM memory region to be migrated; that is the responsibility of the caller.
1392 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1399 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
1400 * a user-provided pointer.
1402 * A RAM device represents a mapping to a physical device, such as to a PCI
1403 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
1404 * into the VM address space and access to the region will modify memory
1405 * directly. However, the memory region should not be included in a memory
1406 * dump (device may not be enabled/mapped at the time of the dump), and
1407 * operations incompatible with manipulating MMIO should be avoided. Replaces
1410 * @mr: the #MemoryRegion to be initialized.
1411 * @owner: the object that tracks the region's reference count
1412 * @name: the name of the region.
1413 * @size: size of the region.
1414 * @ptr: memory to be mapped; must contain at least @size bytes.
1416 * Note that this function does not do anything to cause the data in the
1417 * RAM memory region to be migrated; that is the responsibility of the caller.
1418 * (For RAM device memory regions, migrating the contents rarely makes sense.)
1420 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1427 * memory_region_init_alias: Initialize a memory region that aliases all or a
1428 * part of another memory region.
1430 * @mr: the #MemoryRegion to be initialized.
1431 * @owner: the object that tracks the region's reference count
1432 * @name: used for debugging; not visible to the user or ABI
1433 * @orig: the region to be referenced; @mr will be equivalent to
1434 * @orig between @offset and @offset + @size - 1.
1435 * @offset: start of the section in @orig to be referenced.
1436 * @size: size of the region.
1438 void memory_region_init_alias(MemoryRegion
*mr
,
1446 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
1448 * This has the same effect as calling memory_region_init_ram_nomigrate()
1449 * and then marking the resulting region read-only with
1450 * memory_region_set_readonly().
1452 * Note that this function does not do anything to cause the data in the
1453 * RAM side of the memory region to be migrated; that is the responsibility
1456 * @mr: the #MemoryRegion to be initialized.
1457 * @owner: the object that tracks the region's reference count
1458 * @name: Region name, becomes part of RAMBlock name used in migration stream
1459 * must be unique within any device
1460 * @size: size of the region.
1461 * @errp: pointer to Error*, to store an error if it happens.
1463 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1470 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
1471 * Writes are handled via callbacks.
1473 * Note that this function does not do anything to cause the data in the
1474 * RAM side of the memory region to be migrated; that is the responsibility
1477 * @mr: the #MemoryRegion to be initialized.
1478 * @owner: the object that tracks the region's reference count
1479 * @ops: callbacks for write access handling (must not be NULL).
1480 * @opaque: passed to the read and write callbacks of the @ops structure.
1481 * @name: Region name, becomes part of RAMBlock name used in migration stream
1482 * must be unique within any device
1483 * @size: size of the region.
1484 * @errp: pointer to Error*, to store an error if it happens.
1486 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1488 const MemoryRegionOps
*ops
,
1495 * memory_region_init_iommu: Initialize a memory region of a custom type
1496 * that translates addresses
1498 * An IOMMU region translates addresses and forwards accesses to a target
1501 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
1502 * @_iommu_mr should be a pointer to enough memory for an instance of
1503 * that subclass, @instance_size is the size of that subclass, and
1504 * @mrtypename is its name. This function will initialize @_iommu_mr as an
1505 * instance of the subclass, and its methods will then be called to handle
1506 * accesses to the memory region. See the documentation of
1507 * #IOMMUMemoryRegionClass for further details.
1509 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
1510 * @instance_size: the IOMMUMemoryRegion subclass instance size
1511 * @mrtypename: the type name of the #IOMMUMemoryRegion
1512 * @owner: the object that tracks the region's reference count
1513 * @name: used for debugging; not visible to the user or ABI
1514 * @size: size of the region.
1516 void memory_region_init_iommu(void *_iommu_mr
,
1517 size_t instance_size
,
1518 const char *mrtypename
,
1524 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
1525 * region will modify memory directly.
1527 * @mr: the #MemoryRegion to be initialized
1528 * @owner: the object that tracks the region's reference count (must be
1529 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
1530 * @name: name of the memory region
1531 * @size: size of the region in bytes
1532 * @errp: pointer to Error*, to store an error if it happens.
1534 * This function allocates RAM for a board model or device, and
1535 * arranges for it to be migrated (by calling vmstate_register_ram()
1536 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1539 * TODO: Currently we restrict @owner to being either NULL (for
1540 * global RAM regions with no owner) or devices, so that we can
1541 * give the RAM block a unique name for migration purposes.
1542 * We should lift this restriction and allow arbitrary Objects.
1543 * If you pass a non-NULL non-device @owner then we will assert.
1545 void memory_region_init_ram(MemoryRegion
*mr
,
1552 * memory_region_init_rom: Initialize a ROM memory region.
1554 * This has the same effect as calling memory_region_init_ram()
1555 * and then marking the resulting region read-only with
1556 * memory_region_set_readonly(). This includes arranging for the
1557 * contents to be migrated.
1559 * TODO: Currently we restrict @owner to being either NULL (for
1560 * global RAM regions with no owner) or devices, so that we can
1561 * give the RAM block a unique name for migration purposes.
1562 * We should lift this restriction and allow arbitrary Objects.
1563 * If you pass a non-NULL non-device @owner then we will assert.
1565 * @mr: the #MemoryRegion to be initialized.
1566 * @owner: the object that tracks the region's reference count
1567 * @name: Region name, becomes part of RAMBlock name used in migration stream
1568 * must be unique within any device
1569 * @size: size of the region.
1570 * @errp: pointer to Error*, to store an error if it happens.
1572 void memory_region_init_rom(MemoryRegion
*mr
,
1579 * memory_region_init_rom_device: Initialize a ROM memory region.
1580 * Writes are handled via callbacks.
1582 * This function initializes a memory region backed by RAM for reads
1583 * and callbacks for writes, and arranges for the RAM backing to
1584 * be migrated (by calling vmstate_register_ram()
1585 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1588 * TODO: Currently we restrict @owner to being either NULL (for
1589 * global RAM regions with no owner) or devices, so that we can
1590 * give the RAM block a unique name for migration purposes.
1591 * We should lift this restriction and allow arbitrary Objects.
1592 * If you pass a non-NULL non-device @owner then we will assert.
1594 * @mr: the #MemoryRegion to be initialized.
1595 * @owner: the object that tracks the region's reference count
1596 * @ops: callbacks for write access handling (must not be NULL).
1597 * @opaque: passed to the read and write callbacks of the @ops structure.
1598 * @name: Region name, becomes part of RAMBlock name used in migration stream
1599 * must be unique within any device
1600 * @size: size of the region.
1601 * @errp: pointer to Error*, to store an error if it happens.
1603 void memory_region_init_rom_device(MemoryRegion
*mr
,
1605 const MemoryRegionOps
*ops
,
1613 * memory_region_owner: get a memory region's owner.
1615 * @mr: the memory region being queried.
1617 Object
*memory_region_owner(MemoryRegion
*mr
);
1620 * memory_region_size: get a memory region's size.
1622 * @mr: the memory region being queried.
1624 uint64_t memory_region_size(MemoryRegion
*mr
);
1627 * memory_region_is_ram: check whether a memory region is random access
1629 * Returns %true if a memory region is random access.
1631 * @mr: the memory region being queried
1633 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
1639 * memory_region_is_ram_device: check whether a memory region is a ram device
1641 * Returns %true if a memory region is a device backed ram region
1643 * @mr: the memory region being queried
1645 bool memory_region_is_ram_device(MemoryRegion
*mr
);
1648 * memory_region_is_romd: check whether a memory region is in ROMD mode
1650 * Returns %true if a memory region is a ROM device and currently set to allow
1653 * @mr: the memory region being queried
1655 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
1657 return mr
->rom_device
&& mr
->romd_mode
;
1661 * memory_region_is_protected: check whether a memory region is protected
1663 * Returns %true if a memory region is protected RAM and cannot be accessed
1664 * via standard mechanisms, e.g. DMA.
1666 * @mr: the memory region being queried
1668 bool memory_region_is_protected(MemoryRegion
*mr
);
1671 * memory_region_get_iommu: check whether a memory region is an iommu
1673 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
1676 * @mr: the memory region being queried
1678 static inline IOMMUMemoryRegion
*memory_region_get_iommu(MemoryRegion
*mr
)
1681 return memory_region_get_iommu(mr
->alias
);
1684 return (IOMMUMemoryRegion
*) mr
;
1690 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
1691 * if an iommu or NULL if not
1693 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
1694 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1696 * @iommu_mr: the memory region being queried
1698 static inline IOMMUMemoryRegionClass
*memory_region_get_iommu_class_nocheck(
1699 IOMMUMemoryRegion
*iommu_mr
)
1701 return (IOMMUMemoryRegionClass
*) (((Object
*)iommu_mr
)->class);
1704 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1707 * memory_region_iommu_get_min_page_size: get minimum supported page size
1710 * Returns minimum supported page size for an iommu.
1712 * @iommu_mr: the memory region being queried
1714 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
);
1717 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1719 * Note: for any IOMMU implementation, an in-place mapping change
1720 * should be notified with an UNMAP followed by a MAP.
1722 * @iommu_mr: the memory region that was changed
1723 * @iommu_idx: the IOMMU index for the translation table which has changed
1724 * @event: TLB event with the new entry in the IOMMU translation table.
1725 * The entry replaces all old entries for the same virtual I/O address
1728 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1730 IOMMUTLBEvent event
);
1733 * memory_region_notify_iommu_one: notify a change in an IOMMU translation
1734 * entry to a single notifier
1736 * This works just like memory_region_notify_iommu(), but it only
1737 * notifies a specific notifier, not all of them.
1739 * @notifier: the notifier to be notified
1740 * @event: TLB event with the new entry in the IOMMU translation table.
1741 * The entry replaces all old entries for the same virtual I/O address
1744 void memory_region_notify_iommu_one(IOMMUNotifier
*notifier
,
1745 IOMMUTLBEvent
*event
);
1748 * memory_region_unmap_iommu_notifier_range: notify a unmap for an IOMMU
1749 * translation that covers the
1750 * range of a notifier
1752 * @notifier: the notifier to be notified
1754 void memory_region_unmap_iommu_notifier_range(IOMMUNotifier
*notifier
);
1758 * memory_region_register_iommu_notifier: register a notifier for changes to
1759 * IOMMU translation entries.
1761 * Returns 0 on success, or a negative errno otherwise. In particular,
1762 * -EINVAL indicates that at least one of the attributes of the notifier
1763 * is not supported (flag/range) by the IOMMU memory region. In case of error
1764 * the error object must be created.
1766 * @mr: the memory region to observe
1767 * @n: the IOMMUNotifier to be added; the notify callback receives a
1768 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1769 * ceases to be valid on exit from the notifier.
1770 * @errp: pointer to Error*, to store an error if it happens.
1772 int memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1773 IOMMUNotifier
*n
, Error
**errp
);
1776 * memory_region_iommu_replay: replay existing IOMMU translations to
1777 * a notifier with the minimum page granularity returned by
1778 * mr->iommu_ops->get_page_size().
1780 * Note: this is not related to record-and-replay functionality.
1782 * @iommu_mr: the memory region to observe
1783 * @n: the notifier to which to replay iommu mappings
1785 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
);
1788 * memory_region_unregister_iommu_notifier: unregister a notifier for
1789 * changes to IOMMU translation entries.
1791 * @mr: the memory region which was observed and for which notity_stopped()
1792 * needs to be called
1793 * @n: the notifier to be removed.
1795 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1799 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1800 * defined on the IOMMU.
1802 * Returns 0 on success, or a negative errno otherwise. In particular,
1803 * -EINVAL indicates that the IOMMU does not support the requested
1806 * @iommu_mr: the memory region
1807 * @attr: the requested attribute
1808 * @data: a pointer to the requested attribute data
1810 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1811 enum IOMMUMemoryRegionAttr attr
,
1815 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1816 * use for translations with the given memory transaction attributes.
1818 * @iommu_mr: the memory region
1819 * @attrs: the memory transaction attributes
1821 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1825 * memory_region_iommu_num_indexes: return the total number of IOMMU
1826 * indexes that this IOMMU supports.
1828 * @iommu_mr: the memory region
1830 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
);
1833 * memory_region_iommu_set_page_size_mask: set the supported page
1834 * sizes for a given IOMMU memory region
1836 * @iommu_mr: IOMMU memory region
1837 * @page_size_mask: supported page size mask
1838 * @errp: pointer to Error*, to store an error if it happens.
1840 int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion
*iommu_mr
,
1841 uint64_t page_size_mask
,
1845 * memory_region_name: get a memory region's name
1847 * Returns the string that was used to initialize the memory region.
1849 * @mr: the memory region being queried
1851 const char *memory_region_name(const MemoryRegion
*mr
);
1854 * memory_region_is_logging: return whether a memory region is logging writes
1856 * Returns %true if the memory region is logging writes for the given client
1858 * @mr: the memory region being queried
1859 * @client: the client being queried
1861 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
1864 * memory_region_get_dirty_log_mask: return the clients for which a
1865 * memory region is logging writes.
1867 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1868 * are the bit indices.
1870 * @mr: the memory region being queried
1872 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
1875 * memory_region_is_rom: check whether a memory region is ROM
1877 * Returns %true if a memory region is read-only memory.
1879 * @mr: the memory region being queried
1881 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
1883 return mr
->ram
&& mr
->readonly
;
1887 * memory_region_is_nonvolatile: check whether a memory region is non-volatile
1889 * Returns %true is a memory region is non-volatile memory.
1891 * @mr: the memory region being queried
1893 static inline bool memory_region_is_nonvolatile(MemoryRegion
*mr
)
1895 return mr
->nonvolatile
;
1899 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1901 * Returns a file descriptor backing a file-based RAM memory region,
1902 * or -1 if the region is not a file-based RAM memory region.
1904 * @mr: the RAM or alias memory region being queried.
1906 int memory_region_get_fd(MemoryRegion
*mr
);
1909 * memory_region_from_host: Convert a pointer into a RAM memory region
1910 * and an offset within it.
1912 * Given a host pointer inside a RAM memory region (created with
1913 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1914 * the MemoryRegion and the offset within it.
1916 * Use with care; by the time this function returns, the returned pointer is
1917 * not protected by RCU anymore. If the caller is not within an RCU critical
1918 * section and does not hold the iothread lock, it must have other means of
1919 * protecting the pointer, such as a reference to the region that includes
1920 * the incoming ram_addr_t.
1922 * @ptr: the host pointer to be converted
1923 * @offset: the offset within memory region
1925 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
1928 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1930 * Returns a host pointer to a RAM memory region (created with
1931 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1933 * Use with care; by the time this function returns, the returned pointer is
1934 * not protected by RCU anymore. If the caller is not within an RCU critical
1935 * section and does not hold the iothread lock, it must have other means of
1936 * protecting the pointer, such as a reference to the region that includes
1937 * the incoming ram_addr_t.
1939 * @mr: the memory region being queried.
1941 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
1943 /* memory_region_ram_resize: Resize a RAM region.
1945 * Resizing RAM while migrating can result in the migration being canceled.
1946 * Care has to be taken if the guest might have already detected the memory.
1948 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1949 * @newsize: the new size the region
1950 * @errp: pointer to Error*, to store an error if it happens.
1952 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
1956 * memory_region_msync: Synchronize selected address range of
1957 * a memory mapped region
1959 * @mr: the memory region to be msync
1960 * @addr: the initial address of the range to be sync
1961 * @size: the size of the range to be sync
1963 void memory_region_msync(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1966 * memory_region_writeback: Trigger cache writeback for
1967 * selected address range
1969 * @mr: the memory region to be updated
1970 * @addr: the initial address of the range to be written back
1971 * @size: the size of the range to be written back
1973 void memory_region_writeback(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1976 * memory_region_set_log: Turn dirty logging on or off for a region.
1978 * Turns dirty logging on or off for a specified client (display, migration).
1979 * Only meaningful for RAM regions.
1981 * @mr: the memory region being updated.
1982 * @log: whether dirty logging is to be enabled or disabled.
1983 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1985 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
1988 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1990 * Marks a range of bytes as dirty, after it has been dirtied outside
1993 * @mr: the memory region being dirtied.
1994 * @addr: the address (relative to the start of the region) being dirtied.
1995 * @size: size of the range being dirtied.
1997 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
2001 * memory_region_clear_dirty_bitmap - clear dirty bitmap for memory range
2003 * This function is called when the caller wants to clear the remote
2004 * dirty bitmap of a memory range within the memory region. This can
2005 * be used by e.g. KVM to manually clear dirty log when
2006 * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT is declared support by the host
2009 * @mr: the memory region to clear the dirty log upon
2010 * @start: start address offset within the memory region
2011 * @len: length of the memory region to clear dirty bitmap
2013 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
2017 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
2018 * bitmap and clear it.
2020 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
2021 * returns the snapshot. The snapshot can then be used to query dirty
2022 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
2023 * querying the same page multiple times, which is especially useful for
2024 * display updates where the scanlines often are not page aligned.
2026 * The dirty bitmap region which gets copied into the snapshot (and
2027 * cleared afterwards) can be larger than requested. The boundaries
2028 * are rounded up/down so complete bitmap longs (covering 64 pages on
2029 * 64bit hosts) can be copied over into the bitmap snapshot. Which
2030 * isn't a problem for display updates as the extra pages are outside
2031 * the visible area, and in case the visible area changes a full
2032 * display redraw is due anyway. Should other use cases for this
2033 * function emerge we might have to revisit this implementation
2036 * Use g_free to release DirtyBitmapSnapshot.
2038 * @mr: the memory region being queried.
2039 * @addr: the address (relative to the start of the region) being queried.
2040 * @size: the size of the range being queried.
2041 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
2043 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
2049 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
2050 * in the specified dirty bitmap snapshot.
2052 * @mr: the memory region being queried.
2053 * @snap: the dirty bitmap snapshot
2054 * @addr: the address (relative to the start of the region) being queried.
2055 * @size: the size of the range being queried.
2057 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
2058 DirtyBitmapSnapshot
*snap
,
2059 hwaddr addr
, hwaddr size
);
2062 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
2065 * Marks a range of pages as no longer dirty.
2067 * @mr: the region being updated.
2068 * @addr: the start of the subrange being cleaned.
2069 * @size: the size of the subrange being cleaned.
2070 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
2071 * %DIRTY_MEMORY_VGA.
2073 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2074 hwaddr size
, unsigned client
);
2077 * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate
2078 * TBs (for self-modifying code).
2080 * The MemoryRegionOps->write() callback of a ROM device must use this function
2081 * to mark byte ranges that have been modified internally, such as by directly
2082 * accessing the memory returned by memory_region_get_ram_ptr().
2084 * This function marks the range dirty and invalidates TBs so that TCG can
2085 * detect self-modifying code.
2087 * @mr: the region being flushed.
2088 * @addr: the start, relative to the start of the region, of the range being
2090 * @size: the size, in bytes, of the range being flushed.
2092 void memory_region_flush_rom_device(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
2095 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
2097 * Allows a memory region to be marked as read-only (turning it into a ROM).
2098 * only useful on RAM regions.
2100 * @mr: the region being updated.
2101 * @readonly: whether rhe region is to be ROM or RAM.
2103 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
2106 * memory_region_set_nonvolatile: Turn a memory region non-volatile
2108 * Allows a memory region to be marked as non-volatile.
2109 * only useful on RAM regions.
2111 * @mr: the region being updated.
2112 * @nonvolatile: whether rhe region is to be non-volatile.
2114 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
);
2117 * memory_region_rom_device_set_romd: enable/disable ROMD mode
2119 * Allows a ROM device (initialized with memory_region_init_rom_device() to
2120 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
2121 * device is mapped to guest memory and satisfies read access directly.
2122 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
2123 * Writes are always handled by the #MemoryRegion.write function.
2125 * @mr: the memory region to be updated
2126 * @romd_mode: %true to put the region into ROMD mode
2128 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
2131 * memory_region_set_coalescing: Enable memory coalescing for the region.
2133 * Enabled writes to a region to be queued for later processing. MMIO ->write
2134 * callbacks may be delayed until a non-coalesced MMIO is issued.
2135 * Only useful for IO regions. Roughly similar to write-combining hardware.
2137 * @mr: the memory region to be write coalesced
2139 void memory_region_set_coalescing(MemoryRegion
*mr
);
2142 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
2145 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
2146 * Multiple calls can be issued coalesced disjoint ranges.
2148 * @mr: the memory region to be updated.
2149 * @offset: the start of the range within the region to be coalesced.
2150 * @size: the size of the subrange to be coalesced.
2152 void memory_region_add_coalescing(MemoryRegion
*mr
,
2157 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
2159 * Disables any coalescing caused by memory_region_set_coalescing() or
2160 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
2163 * @mr: the memory region to be updated.
2165 void memory_region_clear_coalescing(MemoryRegion
*mr
);
2168 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
2171 * Ensure that pending coalesced MMIO request are flushed before the memory
2172 * region is accessed. This property is automatically enabled for all regions
2173 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
2175 * @mr: the memory region to be updated.
2177 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
2180 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
2183 * Clear the automatic coalesced MMIO flushing enabled via
2184 * memory_region_set_flush_coalesced. Note that this service has no effect on
2185 * memory regions that have MMIO coalescing enabled for themselves. For them,
2186 * automatic flushing will stop once coalescing is disabled.
2188 * @mr: the memory region to be updated.
2190 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
2193 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
2194 * is written to a location.
2196 * Marks a word in an IO region (initialized with memory_region_init_io())
2197 * as a trigger for an eventfd event. The I/O callback will not be called.
2198 * The caller must be prepared to handle failure (that is, take the required
2199 * action if the callback _is_ called).
2201 * @mr: the memory region being updated.
2202 * @addr: the address within @mr that is to be monitored
2203 * @size: the size of the access to trigger the eventfd
2204 * @match_data: whether to match against @data, instead of just @addr
2205 * @data: the data to match against the guest write
2206 * @e: event notifier to be triggered when @addr, @size, and @data all match.
2208 void memory_region_add_eventfd(MemoryRegion
*mr
,
2216 * memory_region_del_eventfd: Cancel an eventfd.
2218 * Cancels an eventfd trigger requested by a previous
2219 * memory_region_add_eventfd() call.
2221 * @mr: the memory region being updated.
2222 * @addr: the address within @mr that is to be monitored
2223 * @size: the size of the access to trigger the eventfd
2224 * @match_data: whether to match against @data, instead of just @addr
2225 * @data: the data to match against the guest write
2226 * @e: event notifier to be triggered when @addr, @size, and @data all match.
2228 void memory_region_del_eventfd(MemoryRegion
*mr
,
2236 * memory_region_add_subregion: Add a subregion to a container.
2238 * Adds a subregion at @offset. The subregion may not overlap with other
2239 * subregions (except for those explicitly marked as overlapping). A region
2240 * may only be added once as a subregion (unless removed with
2241 * memory_region_del_subregion()); use memory_region_init_alias() if you
2242 * want a region to be a subregion in multiple locations.
2244 * @mr: the region to contain the new subregion; must be a container
2245 * initialized with memory_region_init().
2246 * @offset: the offset relative to @mr where @subregion is added.
2247 * @subregion: the subregion to be added.
2249 void memory_region_add_subregion(MemoryRegion
*mr
,
2251 MemoryRegion
*subregion
);
2253 * memory_region_add_subregion_overlap: Add a subregion to a container
2256 * Adds a subregion at @offset. The subregion may overlap with other
2257 * subregions. Conflicts are resolved by having a higher @priority hide a
2258 * lower @priority. Subregions without priority are taken as @priority 0.
2259 * A region may only be added once as a subregion (unless removed with
2260 * memory_region_del_subregion()); use memory_region_init_alias() if you
2261 * want a region to be a subregion in multiple locations.
2263 * @mr: the region to contain the new subregion; must be a container
2264 * initialized with memory_region_init().
2265 * @offset: the offset relative to @mr where @subregion is added.
2266 * @subregion: the subregion to be added.
2267 * @priority: used for resolving overlaps; highest priority wins.
2269 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2271 MemoryRegion
*subregion
,
2275 * memory_region_get_ram_addr: Get the ram address associated with a memory
2278 * @mr: the region to be queried
2280 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
2282 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
2284 * memory_region_del_subregion: Remove a subregion.
2286 * Removes a subregion from its container.
2288 * @mr: the container to be updated.
2289 * @subregion: the region being removed; must be a current subregion of @mr.
2291 void memory_region_del_subregion(MemoryRegion
*mr
,
2292 MemoryRegion
*subregion
);
2295 * memory_region_set_enabled: dynamically enable or disable a region
2297 * Enables or disables a memory region. A disabled memory region
2298 * ignores all accesses to itself and its subregions. It does not
2299 * obscure sibling subregions with lower priority - it simply behaves as
2300 * if it was removed from the hierarchy.
2302 * Regions default to being enabled.
2304 * @mr: the region to be updated
2305 * @enabled: whether to enable or disable the region
2307 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
2310 * memory_region_set_address: dynamically update the address of a region
2312 * Dynamically updates the address of a region, relative to its container.
2313 * May be used on regions are currently part of a memory hierarchy.
2315 * @mr: the region to be updated
2316 * @addr: new address, relative to container region
2318 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
2321 * memory_region_set_size: dynamically update the size of a region.
2323 * Dynamically updates the size of a region.
2325 * @mr: the region to be updated
2326 * @size: used size of the region.
2328 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
2331 * memory_region_set_alias_offset: dynamically update a memory alias's offset
2333 * Dynamically updates the offset into the target region that an alias points
2334 * to, as if the fourth argument to memory_region_init_alias() has changed.
2336 * @mr: the #MemoryRegion to be updated; should be an alias.
2337 * @offset: the new offset into the target memory region
2339 void memory_region_set_alias_offset(MemoryRegion
*mr
,
2343 * memory_region_present: checks if an address relative to a @container
2344 * translates into #MemoryRegion within @container
2346 * Answer whether a #MemoryRegion within @container covers the address
2349 * @container: a #MemoryRegion within which @addr is a relative address
2350 * @addr: the area within @container to be searched
2352 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
2355 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
2356 * into another memory region, which does not necessarily imply that it is
2357 * mapped into an address space.
2359 * @mr: a #MemoryRegion which should be checked if it's mapped
2361 bool memory_region_is_mapped(MemoryRegion
*mr
);
2364 * memory_region_get_ram_discard_manager: get the #RamDiscardManager for a
2367 * The #RamDiscardManager cannot change while a memory region is mapped.
2369 * @mr: the #MemoryRegion
2371 RamDiscardManager
*memory_region_get_ram_discard_manager(MemoryRegion
*mr
);
2374 * memory_region_has_ram_discard_manager: check whether a #MemoryRegion has a
2375 * #RamDiscardManager assigned
2377 * @mr: the #MemoryRegion
2379 static inline bool memory_region_has_ram_discard_manager(MemoryRegion
*mr
)
2381 return !!memory_region_get_ram_discard_manager(mr
);
2385 * memory_region_set_ram_discard_manager: set the #RamDiscardManager for a
2388 * This function must not be called for a mapped #MemoryRegion, a #MemoryRegion
2389 * that does not cover RAM, or a #MemoryRegion that already has a
2390 * #RamDiscardManager assigned.
2392 * @mr: the #MemoryRegion
2393 * @rdm: #RamDiscardManager to set
2395 void memory_region_set_ram_discard_manager(MemoryRegion
*mr
,
2396 RamDiscardManager
*rdm
);
2399 * memory_region_find: translate an address/size relative to a
2400 * MemoryRegion into a #MemoryRegionSection.
2402 * Locates the first #MemoryRegion within @mr that overlaps the range
2403 * given by @addr and @size.
2405 * Returns a #MemoryRegionSection that describes a contiguous overlap.
2406 * It will have the following characteristics:
2407 * - @size = 0 iff no overlap was found
2408 * - @mr is non-%NULL iff an overlap was found
2410 * Remember that in the return value the @offset_within_region is
2411 * relative to the returned region (in the .@mr field), not to the
2414 * Similarly, the .@offset_within_address_space is relative to the
2415 * address space that contains both regions, the passed and the
2416 * returned one. However, in the special case where the @mr argument
2417 * has no container (and thus is the root of the address space), the
2418 * following will hold:
2419 * - @offset_within_address_space >= @addr
2420 * - @offset_within_address_space + .@size <= @addr + @size
2422 * @mr: a MemoryRegion within which @addr is a relative address
2423 * @addr: start of the area within @as to be searched
2424 * @size: size of the area to be searched
2426 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2427 hwaddr addr
, uint64_t size
);
2430 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
2432 * Synchronizes the dirty page log for all address spaces.
2434 * @last_stage: whether this is the last stage of live migration
2436 void memory_global_dirty_log_sync(bool last_stage
);
2439 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
2441 * Synchronizes the vCPUs with a thread that is reading the dirty bitmap.
2442 * This function must be called after the dirty log bitmap is cleared, and
2443 * before dirty guest memory pages are read. If you are using
2444 * #DirtyBitmapSnapshot, memory_region_snapshot_and_clear_dirty() takes
2445 * care of doing this.
2447 void memory_global_after_dirty_log_sync(void);
2450 * memory_region_transaction_begin: Start a transaction.
2452 * During a transaction, changes will be accumulated and made visible
2453 * only when the transaction ends (is committed).
2455 void memory_region_transaction_begin(void);
2458 * memory_region_transaction_commit: Commit a transaction and make changes
2459 * visible to the guest.
2461 void memory_region_transaction_commit(void);
2464 * memory_listener_register: register callbacks to be called when memory
2465 * sections are mapped or unmapped into an address
2468 * @listener: an object containing the callbacks to be called
2469 * @filter: if non-%NULL, only regions in this address space will be observed
2471 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
2474 * memory_listener_unregister: undo the effect of memory_listener_register()
2476 * @listener: an object containing the callbacks to be removed
2478 void memory_listener_unregister(MemoryListener
*listener
);
2481 * memory_global_dirty_log_start: begin dirty logging for all regions
2483 * @flags: purpose of starting dirty log, migration or dirty rate
2485 void memory_global_dirty_log_start(unsigned int flags
);
2488 * memory_global_dirty_log_stop: end dirty logging for all regions
2490 * @flags: purpose of stopping dirty log, migration or dirty rate
2492 void memory_global_dirty_log_stop(unsigned int flags
);
2494 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
, bool disabled
);
2496 bool memory_region_access_valid(MemoryRegion
*mr
, hwaddr addr
,
2497 unsigned size
, bool is_write
,
2501 * memory_region_dispatch_read: perform a read directly to the specified
2504 * @mr: #MemoryRegion to access
2505 * @addr: address within that region
2506 * @pval: pointer to uint64_t which the data is written to
2507 * @op: size, sign, and endianness of the memory operation
2508 * @attrs: memory transaction attributes to use for the access
2510 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
2516 * memory_region_dispatch_write: perform a write directly to the specified
2519 * @mr: #MemoryRegion to access
2520 * @addr: address within that region
2521 * @data: data to write
2522 * @op: size, sign, and endianness of the memory operation
2523 * @attrs: memory transaction attributes to use for the access
2525 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
2532 * address_space_init: initializes an address space
2534 * @as: an uninitialized #AddressSpace
2535 * @root: a #MemoryRegion that routes addresses for the address space
2536 * @name: an address space name. The name is only used for debugging
2539 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
2542 * address_space_destroy: destroy an address space
2544 * Releases all resources associated with an address space. After an address space
2545 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
2548 * @as: address space to be destroyed
2550 void address_space_destroy(AddressSpace
*as
);
2553 * address_space_remove_listeners: unregister all listeners of an address space
2555 * Removes all callbacks previously registered with memory_listener_register()
2558 * @as: an initialized #AddressSpace
2560 void address_space_remove_listeners(AddressSpace
*as
);
2563 * address_space_rw: read from or write to an address space.
2565 * Return a MemTxResult indicating whether the operation succeeded
2566 * or failed (eg unassigned memory, device rejected the transaction,
2569 * @as: #AddressSpace to be accessed
2570 * @addr: address within that address space
2571 * @attrs: memory transaction attributes
2572 * @buf: buffer with the data transferred
2573 * @len: the number of bytes to read or write
2574 * @is_write: indicates the transfer direction
2576 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
2577 MemTxAttrs attrs
, void *buf
,
2578 hwaddr len
, bool is_write
);
2581 * address_space_write: write to address space.
2583 * Return a MemTxResult indicating whether the operation succeeded
2584 * or failed (eg unassigned memory, device rejected the transaction,
2587 * @as: #AddressSpace to be accessed
2588 * @addr: address within that address space
2589 * @attrs: memory transaction attributes
2590 * @buf: buffer with the data transferred
2591 * @len: the number of bytes to write
2593 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
2595 const void *buf
, hwaddr len
);
2598 * address_space_write_rom: write to address space, including ROM.
2600 * This function writes to the specified address space, but will
2601 * write data to both ROM and RAM. This is used for non-guest
2602 * writes like writes from the gdb debug stub or initial loading
2605 * Note that portions of the write which attempt to write data to
2606 * a device will be silently ignored -- only real RAM and ROM will
2609 * Return a MemTxResult indicating whether the operation succeeded
2610 * or failed (eg unassigned memory, device rejected the transaction,
2613 * @as: #AddressSpace to be accessed
2614 * @addr: address within that address space
2615 * @attrs: memory transaction attributes
2616 * @buf: buffer with the data transferred
2617 * @len: the number of bytes to write
2619 MemTxResult
address_space_write_rom(AddressSpace
*as
, hwaddr addr
,
2621 const void *buf
, hwaddr len
);
2623 /* address_space_ld*: load from an address space
2624 * address_space_st*: store to an address space
2626 * These functions perform a load or store of the byte, word,
2627 * longword or quad to the specified address within the AddressSpace.
2628 * The _le suffixed functions treat the data as little endian;
2629 * _be indicates big endian; no suffix indicates "same endianness
2632 * The "guest CPU endianness" accessors are deprecated for use outside
2633 * target-* code; devices should be CPU-agnostic and use either the LE
2634 * or the BE accessors.
2636 * @as #AddressSpace to be accessed
2637 * @addr: address within that address space
2638 * @val: data value, for stores
2639 * @attrs: memory transaction attributes
2640 * @result: location to write the success/failure of the transaction;
2641 * if NULL, this information is discarded
2646 #define ARG1_DECL AddressSpace *as
2647 #include "exec/memory_ldst.h.inc"
2651 #define ARG1_DECL AddressSpace *as
2652 #include "exec/memory_ldst_phys.h.inc"
2654 struct MemoryRegionCache
{
2659 MemoryRegionSection mrs
;
2663 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
2666 /* address_space_ld*_cached: load from a cached #MemoryRegion
2667 * address_space_st*_cached: store into a cached #MemoryRegion
2669 * These functions perform a load or store of the byte, word,
2670 * longword or quad to the specified address. The address is
2671 * a physical address in the AddressSpace, but it must lie within
2672 * a #MemoryRegion that was mapped with address_space_cache_init.
2674 * The _le suffixed functions treat the data as little endian;
2675 * _be indicates big endian; no suffix indicates "same endianness
2678 * The "guest CPU endianness" accessors are deprecated for use outside
2679 * target-* code; devices should be CPU-agnostic and use either the LE
2680 * or the BE accessors.
2682 * @cache: previously initialized #MemoryRegionCache to be accessed
2683 * @addr: address within the address space
2684 * @val: data value, for stores
2685 * @attrs: memory transaction attributes
2686 * @result: location to write the success/failure of the transaction;
2687 * if NULL, this information is discarded
2690 #define SUFFIX _cached_slow
2692 #define ARG1_DECL MemoryRegionCache *cache
2693 #include "exec/memory_ldst.h.inc"
2695 /* Inline fast path for direct RAM access. */
2696 static inline uint8_t address_space_ldub_cached(MemoryRegionCache
*cache
,
2697 hwaddr addr
, MemTxAttrs attrs
, MemTxResult
*result
)
2699 assert(addr
< cache
->len
);
2700 if (likely(cache
->ptr
)) {
2701 return ldub_p(cache
->ptr
+ addr
);
2703 return address_space_ldub_cached_slow(cache
, addr
, attrs
, result
);
2707 static inline void address_space_stb_cached(MemoryRegionCache
*cache
,
2708 hwaddr addr
, uint8_t val
, MemTxAttrs attrs
, MemTxResult
*result
)
2710 assert(addr
< cache
->len
);
2711 if (likely(cache
->ptr
)) {
2712 stb_p(cache
->ptr
+ addr
, val
);
2714 address_space_stb_cached_slow(cache
, addr
, val
, attrs
, result
);
2718 #define ENDIANNESS _le
2719 #include "exec/memory_ldst_cached.h.inc"
2721 #define ENDIANNESS _be
2722 #include "exec/memory_ldst_cached.h.inc"
2724 #define SUFFIX _cached
2726 #define ARG1_DECL MemoryRegionCache *cache
2727 #include "exec/memory_ldst_phys.h.inc"
2729 /* address_space_cache_init: prepare for repeated access to a physical
2732 * @cache: #MemoryRegionCache to be filled
2733 * @as: #AddressSpace to be accessed
2734 * @addr: address within that address space
2735 * @len: length of buffer
2736 * @is_write: indicates the transfer direction
2738 * Will only work with RAM, and may map a subset of the requested range by
2739 * returning a value that is less than @len. On failure, return a negative
2742 * Because it only works with RAM, this function can be used for
2743 * read-modify-write operations. In this case, is_write should be %true.
2745 * Note that addresses passed to the address_space_*_cached functions
2746 * are relative to @addr.
2748 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
2755 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
2757 * @cache: The #MemoryRegionCache to operate on.
2758 * @addr: The first physical address that was written, relative to the
2759 * address that was passed to @address_space_cache_init.
2760 * @access_len: The number of bytes that were written starting at @addr.
2762 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
2767 * address_space_cache_destroy: free a #MemoryRegionCache
2769 * @cache: The #MemoryRegionCache whose memory should be released.
2771 void address_space_cache_destroy(MemoryRegionCache
*cache
);
2773 /* address_space_get_iotlb_entry: translate an address into an IOTLB
2774 * entry. Should be called from an RCU critical section.
2776 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
2777 bool is_write
, MemTxAttrs attrs
);
2779 /* address_space_translate: translate an address range into an address space
2780 * into a MemoryRegion and an address range into that section. Should be
2781 * called from an RCU critical section, to avoid that the last reference
2782 * to the returned region disappears after address_space_translate returns.
2784 * @fv: #FlatView to be accessed
2785 * @addr: address within that address space
2786 * @xlat: pointer to address within the returned memory region section's
2788 * @len: pointer to length
2789 * @is_write: indicates the transfer direction
2790 * @attrs: memory attributes
2792 MemoryRegion
*flatview_translate(FlatView
*fv
,
2793 hwaddr addr
, hwaddr
*xlat
,
2794 hwaddr
*len
, bool is_write
,
2797 static inline MemoryRegion
*address_space_translate(AddressSpace
*as
,
2798 hwaddr addr
, hwaddr
*xlat
,
2799 hwaddr
*len
, bool is_write
,
2802 return flatview_translate(address_space_to_flatview(as
),
2803 addr
, xlat
, len
, is_write
, attrs
);
2806 /* address_space_access_valid: check for validity of accessing an address
2809 * Check whether memory is assigned to the given address space range, and
2810 * access is permitted by any IOMMU regions that are active for the address
2813 * For now, addr and len should be aligned to a page size. This limitation
2814 * will be lifted in the future.
2816 * @as: #AddressSpace to be accessed
2817 * @addr: address within that address space
2818 * @len: length of the area to be checked
2819 * @is_write: indicates the transfer direction
2820 * @attrs: memory attributes
2822 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, hwaddr len
,
2823 bool is_write
, MemTxAttrs attrs
);
2825 /* address_space_map: map a physical memory region into a host virtual address
2827 * May map a subset of the requested range, given by and returned in @plen.
2828 * May return %NULL and set *@plen to zero(0), if resources needed to perform
2829 * the mapping are exhausted.
2830 * Use only for reads OR writes - not for read-modify-write operations.
2831 * Use cpu_register_map_client() to know when retrying the map operation is
2832 * likely to succeed.
2834 * @as: #AddressSpace to be accessed
2835 * @addr: address within that address space
2836 * @plen: pointer to length of buffer; updated on return
2837 * @is_write: indicates the transfer direction
2838 * @attrs: memory attributes
2840 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
2841 hwaddr
*plen
, bool is_write
, MemTxAttrs attrs
);
2843 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2845 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2846 * the amount of memory that was actually read or written by the caller.
2848 * @as: #AddressSpace used
2849 * @buffer: host pointer as returned by address_space_map()
2850 * @len: buffer length as returned by address_space_map()
2851 * @access_len: amount of data actually transferred
2852 * @is_write: indicates the transfer direction
2854 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
2855 bool is_write
, hwaddr access_len
);
2858 /* Internal functions, part of the implementation of address_space_read. */
2859 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
2860 MemTxAttrs attrs
, void *buf
, hwaddr len
);
2861 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
2862 MemTxAttrs attrs
, void *buf
,
2863 hwaddr len
, hwaddr addr1
, hwaddr l
,
2865 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
2867 /* Internal functions, part of the implementation of address_space_read_cached
2868 * and address_space_write_cached. */
2869 MemTxResult
address_space_read_cached_slow(MemoryRegionCache
*cache
,
2870 hwaddr addr
, void *buf
, hwaddr len
);
2871 MemTxResult
address_space_write_cached_slow(MemoryRegionCache
*cache
,
2872 hwaddr addr
, const void *buf
,
2875 int memory_access_size(MemoryRegion
*mr
, unsigned l
, hwaddr addr
);
2876 bool prepare_mmio_access(MemoryRegion
*mr
);
2878 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
2881 return memory_region_is_ram(mr
) && !mr
->readonly
&&
2882 !mr
->rom_device
&& !memory_region_is_ram_device(mr
);
2884 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
2885 memory_region_is_romd(mr
);
2890 * address_space_read: read from an address space.
2892 * Return a MemTxResult indicating whether the operation succeeded
2893 * or failed (eg unassigned memory, device rejected the transaction,
2894 * IOMMU fault). Called within RCU critical section.
2896 * @as: #AddressSpace to be accessed
2897 * @addr: address within that address space
2898 * @attrs: memory transaction attributes
2899 * @buf: buffer with the data transferred
2900 * @len: length of the data transferred
2902 static inline __attribute__((__always_inline__
))
2903 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
2904 MemTxAttrs attrs
, void *buf
,
2907 MemTxResult result
= MEMTX_OK
;
2913 if (__builtin_constant_p(len
)) {
2915 RCU_READ_LOCK_GUARD();
2916 fv
= address_space_to_flatview(as
);
2918 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
2919 if (len
== l
&& memory_access_is_direct(mr
, false)) {
2920 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
2921 memcpy(buf
, ptr
, len
);
2923 result
= flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
2928 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
2934 * address_space_read_cached: read from a cached RAM region
2936 * @cache: Cached region to be addressed
2937 * @addr: address relative to the base of the RAM region
2938 * @buf: buffer with the data transferred
2939 * @len: length of the data transferred
2941 static inline MemTxResult
2942 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2943 void *buf
, hwaddr len
)
2945 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2946 fuzz_dma_read_cb(cache
->xlat
+ addr
, len
, cache
->mrs
.mr
);
2947 if (likely(cache
->ptr
)) {
2948 memcpy(buf
, cache
->ptr
+ addr
, len
);
2951 return address_space_read_cached_slow(cache
, addr
, buf
, len
);
2956 * address_space_write_cached: write to a cached RAM region
2958 * @cache: Cached region to be addressed
2959 * @addr: address relative to the base of the RAM region
2960 * @buf: buffer with the data transferred
2961 * @len: length of the data transferred
2963 static inline MemTxResult
2964 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2965 const void *buf
, hwaddr len
)
2967 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2968 if (likely(cache
->ptr
)) {
2969 memcpy(cache
->ptr
+ addr
, buf
, len
);
2972 return address_space_write_cached_slow(cache
, addr
, buf
, len
);
2977 * address_space_set: Fill address space with a constant byte.
2979 * Return a MemTxResult indicating whether the operation succeeded
2980 * or failed (eg unassigned memory, device rejected the transaction,
2983 * @as: #AddressSpace to be accessed
2984 * @addr: address within that address space
2985 * @c: constant byte to fill the memory
2986 * @len: the number of bytes to fill with the constant byte
2987 * @attrs: memory transaction attributes
2989 MemTxResult
address_space_set(AddressSpace
*as
, hwaddr addr
,
2990 uint8_t c
, hwaddr len
, MemTxAttrs attrs
);
2993 /* enum device_endian to MemOp. */
2994 static inline MemOp
devend_memop(enum device_endian end
)
2996 QEMU_BUILD_BUG_ON(DEVICE_HOST_ENDIAN
!= DEVICE_LITTLE_ENDIAN
&&
2997 DEVICE_HOST_ENDIAN
!= DEVICE_BIG_ENDIAN
);
2999 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
3000 /* Swap if non-host endianness or native (target) endianness */
3001 return (end
== DEVICE_HOST_ENDIAN
) ? 0 : MO_BSWAP
;
3003 const int non_host_endianness
=
3004 DEVICE_LITTLE_ENDIAN
^ DEVICE_BIG_ENDIAN
^ DEVICE_HOST_ENDIAN
;
3006 /* In this case, native (target) endianness needs no swap. */
3007 return (end
== non_host_endianness
) ? MO_BSWAP
: 0;
3013 * Inhibit technologies that require discarding of pages in RAM blocks, e.g.,
3014 * to manage the actual amount of memory consumed by the VM (then, the memory
3015 * provided by RAM blocks might be bigger than the desired memory consumption).
3016 * This *must* be set if:
3017 * - Discarding parts of a RAM blocks does not result in the change being
3018 * reflected in the VM and the pages getting freed.
3019 * - All memory in RAM blocks is pinned or duplicated, invaldiating any previous
3021 * - Discarding parts of a RAM blocks will result in integrity issues (e.g.,
3023 * Technologies that only temporarily pin the current working set of a
3024 * driver are fine, because we don't expect such pages to be discarded
3025 * (esp. based on guest action like balloon inflation).
3027 * This is *not* to be used to protect from concurrent discards (esp.,
3030 * Returns 0 if successful. Returns -EBUSY if a technology that relies on
3031 * discards to work reliably is active.
3033 int ram_block_discard_disable(bool state
);
3036 * See ram_block_discard_disable(): only disable uncoordinated discards,
3037 * keeping coordinated discards (via the RamDiscardManager) enabled.
3039 int ram_block_uncoordinated_discard_disable(bool state
);
3042 * Inhibit technologies that disable discarding of pages in RAM blocks.
3044 * Returns 0 if successful. Returns -EBUSY if discards are already set to
3047 int ram_block_discard_require(bool state
);
3050 * See ram_block_discard_require(): only inhibit technologies that disable
3051 * uncoordinated discarding of pages in RAM blocks, allowing co-existance with
3052 * technologies that only inhibit uncoordinated discards (via the
3053 * RamDiscardManager).
3055 int ram_block_coordinated_discard_require(bool state
);
3058 * Test if any discarding of memory in ram blocks is disabled.
3060 bool ram_block_discard_is_disabled(void);
3063 * Test if any discarding of memory in ram blocks is required to work reliably.
3065 bool ram_block_discard_is_required(void);