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 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
236 IOMMUNotifierFlag flags
,
237 hwaddr start
, hwaddr end
,
241 n
->notifier_flags
= flags
;
244 n
->iommu_idx
= iommu_idx
;
248 * Memory region callbacks
250 struct MemoryRegionOps
{
251 /* Read from the memory region. @addr is relative to @mr; @size is
253 uint64_t (*read
)(void *opaque
,
256 /* Write to the memory region. @addr is relative to @mr; @size is
258 void (*write
)(void *opaque
,
263 MemTxResult (*read_with_attrs
)(void *opaque
,
268 MemTxResult (*write_with_attrs
)(void *opaque
,
274 enum device_endian endianness
;
275 /* Guest-visible constraints: */
277 /* If nonzero, specify bounds on access sizes beyond which a machine
280 unsigned min_access_size
;
281 unsigned max_access_size
;
282 /* If true, unaligned accesses are supported. Otherwise unaligned
283 * accesses throw machine checks.
287 * If present, and returns #false, the transaction is not accepted
288 * by the device (and results in machine dependent behaviour such
289 * as a machine check exception).
291 bool (*accepts
)(void *opaque
, hwaddr addr
,
292 unsigned size
, bool is_write
,
295 /* Internal implementation constraints: */
297 /* If nonzero, specifies the minimum size implemented. Smaller sizes
298 * will be rounded upwards and a partial result will be returned.
300 unsigned min_access_size
;
301 /* If nonzero, specifies the maximum size implemented. Larger sizes
302 * will be done as a series of accesses with smaller sizes.
304 unsigned max_access_size
;
305 /* If true, unaligned accesses are supported. Otherwise all accesses
306 * are converted to (possibly multiple) naturally aligned accesses.
312 typedef struct MemoryRegionClass
{
314 ObjectClass parent_class
;
318 enum IOMMUMemoryRegionAttr
{
319 IOMMU_ATTR_SPAPR_TCE_FD
323 * IOMMUMemoryRegionClass:
325 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
326 * and provide an implementation of at least the @translate method here
327 * to handle requests to the memory region. Other methods are optional.
329 * The IOMMU implementation must use the IOMMU notifier infrastructure
330 * to report whenever mappings are changed, by calling
331 * memory_region_notify_iommu() (or, if necessary, by calling
332 * memory_region_notify_iommu_one() for each registered notifier).
334 * Conceptually an IOMMU provides a mapping from input address
335 * to an output TLB entry. If the IOMMU is aware of memory transaction
336 * attributes and the output TLB entry depends on the transaction
337 * attributes, we represent this using IOMMU indexes. Each index
338 * selects a particular translation table that the IOMMU has:
340 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
342 * @translate takes an input address and an IOMMU index
344 * and the mapping returned can only depend on the input address and the
347 * Most IOMMUs don't care about the transaction attributes and support
348 * only a single IOMMU index. A more complex IOMMU might have one index
349 * for secure transactions and one for non-secure transactions.
351 struct IOMMUMemoryRegionClass
{
353 MemoryRegionClass parent_class
;
359 * Return a TLB entry that contains a given address.
361 * The IOMMUAccessFlags indicated via @flag are optional and may
362 * be specified as IOMMU_NONE to indicate that the caller needs
363 * the full translation information for both reads and writes. If
364 * the access flags are specified then the IOMMU implementation
365 * may use this as an optimization, to stop doing a page table
366 * walk as soon as it knows that the requested permissions are not
367 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
368 * full page table walk and report the permissions in the returned
369 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
370 * return different mappings for reads and writes.)
372 * The returned information remains valid while the caller is
373 * holding the big QEMU lock or is inside an RCU critical section;
374 * if the caller wishes to cache the mapping beyond that it must
375 * register an IOMMU notifier so it can invalidate its cached
376 * information when the IOMMU mapping changes.
378 * @iommu: the IOMMUMemoryRegion
380 * @hwaddr: address to be translated within the memory region
382 * @flag: requested access permission
384 * @iommu_idx: IOMMU index for the translation
386 IOMMUTLBEntry (*translate
)(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
387 IOMMUAccessFlags flag
, int iommu_idx
);
389 * @get_min_page_size:
391 * Returns minimum supported page size in bytes.
393 * If this method is not provided then the minimum is assumed to
394 * be TARGET_PAGE_SIZE.
396 * @iommu: the IOMMUMemoryRegion
398 uint64_t (*get_min_page_size
)(IOMMUMemoryRegion
*iommu
);
400 * @notify_flag_changed:
402 * Called when IOMMU Notifier flag changes (ie when the set of
403 * events which IOMMU users are requesting notification for changes).
404 * Optional method -- need not be provided if the IOMMU does not
405 * need to know exactly which events must be notified.
407 * @iommu: the IOMMUMemoryRegion
409 * @old_flags: events which previously needed to be notified
411 * @new_flags: events which now need to be notified
413 * Returns 0 on success, or a negative errno; in particular
414 * returns -EINVAL if the new flag bitmap is not supported by the
415 * IOMMU memory region. In case of failure, the error object
418 int (*notify_flag_changed
)(IOMMUMemoryRegion
*iommu
,
419 IOMMUNotifierFlag old_flags
,
420 IOMMUNotifierFlag new_flags
,
425 * Called to handle memory_region_iommu_replay().
427 * The default implementation of memory_region_iommu_replay() is to
428 * call the IOMMU translate method for every page in the address space
429 * with flag == IOMMU_NONE and then call the notifier if translate
430 * returns a valid mapping. If this method is implemented then it
431 * overrides the default behaviour, and must provide the full semantics
432 * of memory_region_iommu_replay(), by calling @notifier for every
433 * translation present in the IOMMU.
435 * Optional method -- an IOMMU only needs to provide this method
436 * if the default is inefficient or produces undesirable side effects.
438 * Note: this is not related to record-and-replay functionality.
440 void (*replay
)(IOMMUMemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
445 * Get IOMMU misc attributes. This is an optional method that
446 * can be used to allow users of the IOMMU to get implementation-specific
447 * information. The IOMMU implements this method to handle calls
448 * by IOMMU users to memory_region_iommu_get_attr() by filling in
449 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
450 * the IOMMU supports. If the method is unimplemented then
451 * memory_region_iommu_get_attr() will always return -EINVAL.
453 * @iommu: the IOMMUMemoryRegion
455 * @attr: attribute being queried
457 * @data: memory to fill in with the attribute data
459 * Returns 0 on success, or a negative errno; in particular
460 * returns -EINVAL for unrecognized or unimplemented attribute types.
462 int (*get_attr
)(IOMMUMemoryRegion
*iommu
, enum IOMMUMemoryRegionAttr attr
,
468 * Return the IOMMU index to use for a given set of transaction attributes.
470 * Optional method: if an IOMMU only supports a single IOMMU index then
471 * the default implementation of memory_region_iommu_attrs_to_index()
474 * The indexes supported by an IOMMU must be contiguous, starting at 0.
476 * @iommu: the IOMMUMemoryRegion
477 * @attrs: memory transaction attributes
479 int (*attrs_to_index
)(IOMMUMemoryRegion
*iommu
, MemTxAttrs attrs
);
484 * Return the number of IOMMU indexes this IOMMU supports.
486 * Optional method: if this method is not provided, then
487 * memory_region_iommu_num_indexes() will return 1, indicating that
488 * only a single IOMMU index is supported.
490 * @iommu: the IOMMUMemoryRegion
492 int (*num_indexes
)(IOMMUMemoryRegion
*iommu
);
495 * @iommu_set_page_size_mask:
497 * Restrict the page size mask that can be supported with a given IOMMU
498 * memory region. Used for example to propagate host physical IOMMU page
499 * size mask limitations to the virtual IOMMU.
501 * Optional method: if this method is not provided, then the default global
504 * @iommu: the IOMMUMemoryRegion
506 * @page_size_mask: a bitmask of supported page sizes. At least one bit,
507 * representing the smallest page size, must be set. Additional set bits
508 * represent supported block sizes. For example a host physical IOMMU that
509 * uses page tables with a page size of 4kB, and supports 2MB and 4GB
510 * blocks, will set mask 0x40201000. A granule of 4kB with indiscriminate
511 * block sizes is specified with mask 0xfffffffffffff000.
513 * Returns 0 on success, or a negative error. In case of failure, the error
514 * object must be created.
516 int (*iommu_set_page_size_mask
)(IOMMUMemoryRegion
*iommu
,
517 uint64_t page_size_mask
,
521 typedef struct RamDiscardListener RamDiscardListener
;
522 typedef int (*NotifyRamPopulate
)(RamDiscardListener
*rdl
,
523 MemoryRegionSection
*section
);
524 typedef void (*NotifyRamDiscard
)(RamDiscardListener
*rdl
,
525 MemoryRegionSection
*section
);
527 struct RamDiscardListener
{
531 * Notification that previously discarded memory is about to get populated.
532 * Listeners are able to object. If any listener objects, already
533 * successfully notified listeners are notified about a discard again.
535 * @rdl: the #RamDiscardListener getting notified
536 * @section: the #MemoryRegionSection to get populated. The section
537 * is aligned within the memory region to the minimum granularity
538 * unless it would exceed the registered section.
540 * Returns 0 on success. If the notification is rejected by the listener,
541 * an error is returned.
543 NotifyRamPopulate notify_populate
;
548 * Notification that previously populated memory was discarded successfully
549 * and listeners should drop all references to such memory and prevent
550 * new population (e.g., unmap).
552 * @rdl: the #RamDiscardListener getting notified
553 * @section: the #MemoryRegionSection to get populated. The section
554 * is aligned within the memory region to the minimum granularity
555 * unless it would exceed the registered section.
557 NotifyRamDiscard notify_discard
;
560 * @double_discard_supported:
562 * The listener suppors getting @notify_discard notifications that span
563 * already discarded parts.
565 bool double_discard_supported
;
567 MemoryRegionSection
*section
;
568 QLIST_ENTRY(RamDiscardListener
) next
;
571 static inline void ram_discard_listener_init(RamDiscardListener
*rdl
,
572 NotifyRamPopulate populate_fn
,
573 NotifyRamDiscard discard_fn
,
574 bool double_discard_supported
)
576 rdl
->notify_populate
= populate_fn
;
577 rdl
->notify_discard
= discard_fn
;
578 rdl
->double_discard_supported
= double_discard_supported
;
581 typedef int (*ReplayRamPopulate
)(MemoryRegionSection
*section
, void *opaque
);
582 typedef void (*ReplayRamDiscard
)(MemoryRegionSection
*section
, void *opaque
);
585 * RamDiscardManagerClass:
587 * A #RamDiscardManager coordinates which parts of specific RAM #MemoryRegion
588 * regions are currently populated to be used/accessed by the VM, notifying
589 * after parts were discarded (freeing up memory) and before parts will be
590 * populated (consuming memory), to be used/accessed by the VM.
592 * A #RamDiscardManager can only be set for a RAM #MemoryRegion while the
593 * #MemoryRegion isn't mapped yet; it cannot change while the #MemoryRegion is
596 * The #RamDiscardManager is intended to be used by technologies that are
597 * incompatible with discarding of RAM (e.g., VFIO, which may pin all
598 * memory inside a #MemoryRegion), and require proper coordination to only
599 * map the currently populated parts, to hinder parts that are expected to
600 * remain discarded from silently getting populated and consuming memory.
601 * Technologies that support discarding of RAM don't have to bother and can
602 * simply map the whole #MemoryRegion.
604 * An example #RamDiscardManager is virtio-mem, which logically (un)plugs
605 * memory within an assigned RAM #MemoryRegion, coordinated with the VM.
606 * Logically unplugging memory consists of discarding RAM. The VM agreed to not
607 * access unplugged (discarded) memory - especially via DMA. virtio-mem will
608 * properly coordinate with listeners before memory is plugged (populated),
609 * and after memory is unplugged (discarded).
611 * Listeners are called in multiples of the minimum granularity (unless it
612 * would exceed the registered range) and changes are aligned to the minimum
613 * granularity within the #MemoryRegion. Listeners have to prepare for memory
614 * becoming discarded in a different granularity than it was populated and the
617 struct RamDiscardManagerClass
{
619 InterfaceClass parent_class
;
624 * @get_min_granularity:
626 * Get the minimum granularity in which listeners will get notified
627 * about changes within the #MemoryRegion via the #RamDiscardManager.
629 * @rdm: the #RamDiscardManager
630 * @mr: the #MemoryRegion
632 * Returns the minimum granularity.
634 uint64_t (*get_min_granularity
)(const RamDiscardManager
*rdm
,
635 const MemoryRegion
*mr
);
640 * Check whether the given #MemoryRegionSection is completely populated
641 * (i.e., no parts are currently discarded) via the #RamDiscardManager.
642 * There are no alignment requirements.
644 * @rdm: the #RamDiscardManager
645 * @section: the #MemoryRegionSection
647 * Returns whether the given range is completely populated.
649 bool (*is_populated
)(const RamDiscardManager
*rdm
,
650 const MemoryRegionSection
*section
);
655 * Call the #ReplayRamPopulate callback for all populated parts within the
656 * #MemoryRegionSection via the #RamDiscardManager.
658 * In case any call fails, no further calls are made.
660 * @rdm: the #RamDiscardManager
661 * @section: the #MemoryRegionSection
662 * @replay_fn: the #ReplayRamPopulate callback
663 * @opaque: pointer to forward to the callback
665 * Returns 0 on success, or a negative error if any notification failed.
667 int (*replay_populated
)(const RamDiscardManager
*rdm
,
668 MemoryRegionSection
*section
,
669 ReplayRamPopulate replay_fn
, void *opaque
);
674 * Call the #ReplayRamDiscard callback for all discarded parts within the
675 * #MemoryRegionSection via the #RamDiscardManager.
677 * @rdm: the #RamDiscardManager
678 * @section: the #MemoryRegionSection
679 * @replay_fn: the #ReplayRamDiscard callback
680 * @opaque: pointer to forward to the callback
682 void (*replay_discarded
)(const RamDiscardManager
*rdm
,
683 MemoryRegionSection
*section
,
684 ReplayRamDiscard replay_fn
, void *opaque
);
687 * @register_listener:
689 * Register a #RamDiscardListener for the given #MemoryRegionSection and
690 * immediately notify the #RamDiscardListener about all populated parts
691 * within the #MemoryRegionSection via the #RamDiscardManager.
693 * In case any notification fails, no further notifications are triggered
694 * and an error is logged.
696 * @rdm: the #RamDiscardManager
697 * @rdl: the #RamDiscardListener
698 * @section: the #MemoryRegionSection
700 void (*register_listener
)(RamDiscardManager
*rdm
,
701 RamDiscardListener
*rdl
,
702 MemoryRegionSection
*section
);
705 * @unregister_listener:
707 * Unregister a previously registered #RamDiscardListener via the
708 * #RamDiscardManager after notifying the #RamDiscardListener about all
709 * populated parts becoming unpopulated within the registered
710 * #MemoryRegionSection.
712 * @rdm: the #RamDiscardManager
713 * @rdl: the #RamDiscardListener
715 void (*unregister_listener
)(RamDiscardManager
*rdm
,
716 RamDiscardListener
*rdl
);
719 uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager
*rdm
,
720 const MemoryRegion
*mr
);
722 bool ram_discard_manager_is_populated(const RamDiscardManager
*rdm
,
723 const MemoryRegionSection
*section
);
725 int ram_discard_manager_replay_populated(const RamDiscardManager
*rdm
,
726 MemoryRegionSection
*section
,
727 ReplayRamPopulate replay_fn
,
730 void ram_discard_manager_replay_discarded(const RamDiscardManager
*rdm
,
731 MemoryRegionSection
*section
,
732 ReplayRamDiscard replay_fn
,
735 void ram_discard_manager_register_listener(RamDiscardManager
*rdm
,
736 RamDiscardListener
*rdl
,
737 MemoryRegionSection
*section
);
739 void ram_discard_manager_unregister_listener(RamDiscardManager
*rdm
,
740 RamDiscardListener
*rdl
);
742 bool memory_get_xlat_addr(IOMMUTLBEntry
*iotlb
, void **vaddr
,
743 ram_addr_t
*ram_addr
, bool *read_only
,
744 bool *mr_has_discard_manager
);
746 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
747 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
751 * A struct representing a memory region.
753 struct MemoryRegion
{
758 /* The following fields should fit in a cache line */
762 bool readonly
; /* For RAM regions */
765 bool flush_coalesced_mmio
;
766 uint8_t dirty_log_mask
;
770 /* owner as TYPE_DEVICE. Used for re-entrancy checks in MR access hotpath */
773 const MemoryRegionOps
*ops
;
775 MemoryRegion
*container
;
776 int mapped_via_alias
; /* Mapped via an alias, container might be NULL */
779 void (*destructor
)(MemoryRegion
*mr
);
784 bool warning_printed
; /* For reservations */
785 uint8_t vga_logging_count
;
789 QTAILQ_HEAD(, MemoryRegion
) subregions
;
790 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
791 QTAILQ_HEAD(, CoalescedMemoryRange
) coalesced
;
793 unsigned ioeventfd_nb
;
794 MemoryRegionIoeventfd
*ioeventfds
;
795 RamDiscardManager
*rdm
; /* Only for RAM */
797 /* For devices designed to perform re-entrant IO into their own IO MRs */
798 bool disable_reentrancy_guard
;
801 struct IOMMUMemoryRegion
{
802 MemoryRegion parent_obj
;
804 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
805 IOMMUNotifierFlag iommu_notify_flags
;
808 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
809 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
812 * struct MemoryListener: callbacks structure for updates to the physical memory map
814 * Allows a component to adjust to changes in the guest-visible memory map.
815 * Use with memory_listener_register() and memory_listener_unregister().
817 struct MemoryListener
{
821 * Called at the beginning of an address space update transaction.
822 * Followed by calls to #MemoryListener.region_add(),
823 * #MemoryListener.region_del(), #MemoryListener.region_nop(),
824 * #MemoryListener.log_start() and #MemoryListener.log_stop() in
825 * increasing address order.
827 * @listener: The #MemoryListener.
829 void (*begin
)(MemoryListener
*listener
);
834 * Called at the end of an address space update transaction,
835 * after the last call to #MemoryListener.region_add(),
836 * #MemoryListener.region_del() or #MemoryListener.region_nop(),
837 * #MemoryListener.log_start() and #MemoryListener.log_stop().
839 * @listener: The #MemoryListener.
841 void (*commit
)(MemoryListener
*listener
);
846 * Called during an address space update transaction,
847 * for a section of the address space that is new in this address space
848 * space since the last transaction.
850 * @listener: The #MemoryListener.
851 * @section: The new #MemoryRegionSection.
853 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
858 * Called during an address space update transaction,
859 * for a section of the address space that has disappeared in the address
860 * space since the last transaction.
862 * @listener: The #MemoryListener.
863 * @section: The old #MemoryRegionSection.
865 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
870 * Called during an address space update transaction,
871 * for a section of the address space that is in the same place in the address
872 * space as in the last transaction.
874 * @listener: The #MemoryListener.
875 * @section: The #MemoryRegionSection.
877 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
882 * Called during an address space update transaction, after
883 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or
884 * #MemoryListener.region_nop(), if dirty memory logging clients have
885 * become active since the last transaction.
887 * @listener: The #MemoryListener.
888 * @section: The #MemoryRegionSection.
889 * @old: A bitmap of dirty memory logging clients that were active in
890 * the previous transaction.
891 * @new: A bitmap of dirty memory logging clients that are active in
892 * the current transaction.
894 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
900 * Called during an address space update transaction, after
901 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or
902 * #MemoryListener.region_nop() and possibly after
903 * #MemoryListener.log_start(), if dirty memory logging clients have
904 * become inactive since the last transaction.
906 * @listener: The #MemoryListener.
907 * @section: The #MemoryRegionSection.
908 * @old: A bitmap of dirty memory logging clients that were active in
909 * the previous transaction.
910 * @new: A bitmap of dirty memory logging clients that are active in
911 * the current transaction.
913 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
919 * Called by memory_region_snapshot_and_clear_dirty() and
920 * memory_global_dirty_log_sync(), before accessing QEMU's "official"
921 * copy of the dirty memory bitmap for a #MemoryRegionSection.
923 * @listener: The #MemoryListener.
924 * @section: The #MemoryRegionSection.
926 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
931 * This is the global version of @log_sync when the listener does
932 * not have a way to synchronize the log with finer granularity.
933 * When the listener registers with @log_sync_global defined, then
934 * its @log_sync must be NULL. Vice versa.
936 * @listener: The #MemoryListener.
937 * @last_stage: The last stage to synchronize the log during migration.
938 * The caller should gurantee that the synchronization with true for
939 * @last_stage is triggered for once after all VCPUs have been stopped.
941 void (*log_sync_global
)(MemoryListener
*listener
, bool last_stage
);
946 * Called before reading the dirty memory bitmap for a
947 * #MemoryRegionSection.
949 * @listener: The #MemoryListener.
950 * @section: The #MemoryRegionSection.
952 void (*log_clear
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
957 * Called by memory_global_dirty_log_start(), which
958 * enables the %DIRTY_LOG_MIGRATION client on all memory regions in
959 * the address space. #MemoryListener.log_global_start() is also
960 * called when a #MemoryListener is added, if global dirty logging is
961 * active at that time.
963 * @listener: The #MemoryListener.
965 void (*log_global_start
)(MemoryListener
*listener
);
970 * Called by memory_global_dirty_log_stop(), which
971 * disables the %DIRTY_LOG_MIGRATION client on all memory regions in
974 * @listener: The #MemoryListener.
976 void (*log_global_stop
)(MemoryListener
*listener
);
979 * @log_global_after_sync:
981 * Called after reading the dirty memory bitmap
982 * for any #MemoryRegionSection.
984 * @listener: The #MemoryListener.
986 void (*log_global_after_sync
)(MemoryListener
*listener
);
991 * Called during an address space update transaction,
992 * for a section of the address space that has had a new ioeventfd
993 * registration since the last transaction.
995 * @listener: The #MemoryListener.
996 * @section: The new #MemoryRegionSection.
997 * @match_data: The @match_data parameter for the new ioeventfd.
998 * @data: The @data parameter for the new ioeventfd.
999 * @e: The #EventNotifier parameter for the new ioeventfd.
1001 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
1002 bool match_data
, uint64_t data
, EventNotifier
*e
);
1007 * Called during an address space update transaction,
1008 * for a section of the address space that has dropped an ioeventfd
1009 * registration since the last transaction.
1011 * @listener: The #MemoryListener.
1012 * @section: The new #MemoryRegionSection.
1013 * @match_data: The @match_data parameter for the dropped ioeventfd.
1014 * @data: The @data parameter for the dropped ioeventfd.
1015 * @e: The #EventNotifier parameter for the dropped ioeventfd.
1017 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
1018 bool match_data
, uint64_t data
, EventNotifier
*e
);
1021 * @coalesced_io_add:
1023 * Called during an address space update transaction,
1024 * for a section of the address space that has had a new coalesced
1025 * MMIO range registration since the last transaction.
1027 * @listener: The #MemoryListener.
1028 * @section: The new #MemoryRegionSection.
1029 * @addr: The starting address for the coalesced MMIO range.
1030 * @len: The length of the coalesced MMIO range.
1032 void (*coalesced_io_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
1033 hwaddr addr
, hwaddr len
);
1036 * @coalesced_io_del:
1038 * Called during an address space update transaction,
1039 * for a section of the address space that has dropped a coalesced
1040 * MMIO range since the last transaction.
1042 * @listener: The #MemoryListener.
1043 * @section: The new #MemoryRegionSection.
1044 * @addr: The starting address for the coalesced MMIO range.
1045 * @len: The length of the coalesced MMIO range.
1047 void (*coalesced_io_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
1048 hwaddr addr
, hwaddr len
);
1052 * Govern the order in which memory listeners are invoked. Lower priorities
1053 * are invoked earlier for "add" or "start" callbacks, and later for "delete"
1054 * or "stop" callbacks.
1061 * Name of the listener. It can be used in contexts where we'd like to
1062 * identify one memory listener with the rest.
1067 AddressSpace
*address_space
;
1068 QTAILQ_ENTRY(MemoryListener
) link
;
1069 QTAILQ_ENTRY(MemoryListener
) link_as
;
1073 * struct AddressSpace: describes a mapping of addresses to #MemoryRegion objects
1075 struct AddressSpace
{
1077 struct rcu_head rcu
;
1081 /* Accessed via RCU. */
1082 struct FlatView
*current_map
;
1085 struct MemoryRegionIoeventfd
*ioeventfds
;
1086 QTAILQ_HEAD(, MemoryListener
) listeners
;
1087 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
1090 typedef struct AddressSpaceDispatch AddressSpaceDispatch
;
1091 typedef struct FlatRange FlatRange
;
1093 /* Flattened global view of current active memory hierarchy. Kept in sorted
1097 struct rcu_head rcu
;
1101 unsigned nr_allocated
;
1102 struct AddressSpaceDispatch
*dispatch
;
1106 static inline FlatView
*address_space_to_flatview(AddressSpace
*as
)
1108 return qatomic_rcu_read(&as
->current_map
);
1112 * typedef flatview_cb: callback for flatview_for_each_range()
1114 * @start: start address of the range within the FlatView
1115 * @len: length of the range in bytes
1116 * @mr: MemoryRegion covering this range
1117 * @offset_in_region: offset of the first byte of the range within @mr
1118 * @opaque: data pointer passed to flatview_for_each_range()
1120 * Returns: true to stop the iteration, false to keep going.
1122 typedef bool (*flatview_cb
)(Int128 start
,
1124 const MemoryRegion
*mr
,
1125 hwaddr offset_in_region
,
1129 * flatview_for_each_range: Iterate through a FlatView
1130 * @fv: the FlatView to iterate through
1131 * @cb: function to call for each range
1132 * @opaque: opaque data pointer to pass to @cb
1134 * A FlatView is made up of a list of non-overlapping ranges, each of
1135 * which is a slice of a MemoryRegion. This function iterates through
1136 * each range in @fv, calling @cb. The callback function can terminate
1137 * iteration early by returning 'true'.
1139 void flatview_for_each_range(FlatView
*fv
, flatview_cb cb
, void *opaque
);
1141 static inline bool MemoryRegionSection_eq(MemoryRegionSection
*a
,
1142 MemoryRegionSection
*b
)
1144 return a
->mr
== b
->mr
&&
1146 a
->offset_within_region
== b
->offset_within_region
&&
1147 a
->offset_within_address_space
== b
->offset_within_address_space
&&
1148 int128_eq(a
->size
, b
->size
) &&
1149 a
->readonly
== b
->readonly
&&
1150 a
->nonvolatile
== b
->nonvolatile
;
1154 * memory_region_section_new_copy: Copy a memory region section
1156 * Allocate memory for a new copy, copy the memory region section, and
1157 * properly take a reference on all relevant members.
1159 * @s: the #MemoryRegionSection to copy
1161 MemoryRegionSection
*memory_region_section_new_copy(MemoryRegionSection
*s
);
1164 * memory_region_section_new_copy: Free a copied memory region section
1166 * Free a copy of a memory section created via memory_region_section_new_copy().
1167 * properly dropping references on all relevant members.
1169 * @s: the #MemoryRegionSection to copy
1171 void memory_region_section_free_copy(MemoryRegionSection
*s
);
1174 * memory_region_init: Initialize a memory region
1176 * The region typically acts as a container for other memory regions. Use
1177 * memory_region_add_subregion() to add subregions.
1179 * @mr: the #MemoryRegion to be initialized
1180 * @owner: the object that tracks the region's reference count
1181 * @name: used for debugging; not visible to the user or ABI
1182 * @size: size of the region; any subregions beyond this size will be clipped
1184 void memory_region_init(MemoryRegion
*mr
,
1190 * memory_region_ref: Add 1 to a memory region's reference count
1192 * Whenever memory regions are accessed outside the BQL, they need to be
1193 * preserved against hot-unplug. MemoryRegions actually do not have their
1194 * own reference count; they piggyback on a QOM object, their "owner".
1195 * This function adds a reference to the owner.
1197 * All MemoryRegions must have an owner if they can disappear, even if the
1198 * device they belong to operates exclusively under the BQL. This is because
1199 * the region could be returned at any time by memory_region_find, and this
1200 * is usually under guest control.
1202 * @mr: the #MemoryRegion
1204 void memory_region_ref(MemoryRegion
*mr
);
1207 * memory_region_unref: Remove 1 to a memory region's reference count
1209 * Whenever memory regions are accessed outside the BQL, they need to be
1210 * preserved against hot-unplug. MemoryRegions actually do not have their
1211 * own reference count; they piggyback on a QOM object, their "owner".
1212 * This function removes a reference to the owner and possibly destroys it.
1214 * @mr: the #MemoryRegion
1216 void memory_region_unref(MemoryRegion
*mr
);
1219 * memory_region_init_io: Initialize an I/O memory region.
1221 * Accesses into the region will cause the callbacks in @ops to be called.
1222 * if @size is nonzero, subregions will be clipped to @size.
1224 * @mr: the #MemoryRegion to be initialized.
1225 * @owner: the object that tracks the region's reference count
1226 * @ops: a structure containing read and write callbacks to be used when
1227 * I/O is performed on the region.
1228 * @opaque: passed to the read and write callbacks of the @ops structure.
1229 * @name: used for debugging; not visible to the user or ABI
1230 * @size: size of the region.
1232 void memory_region_init_io(MemoryRegion
*mr
,
1234 const MemoryRegionOps
*ops
,
1240 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
1241 * into the region will modify memory
1244 * @mr: the #MemoryRegion to be initialized.
1245 * @owner: the object that tracks the region's reference count
1246 * @name: Region name, becomes part of RAMBlock name used in migration stream
1247 * must be unique within any device
1248 * @size: size of the region.
1249 * @errp: pointer to Error*, to store an error if it happens.
1251 * Note that this function does not do anything to cause the data in the
1252 * RAM memory region to be migrated; that is the responsibility of the caller.
1254 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
1261 * memory_region_init_ram_flags_nomigrate: Initialize RAM memory region.
1262 * Accesses into the region will
1263 * modify memory directly.
1265 * @mr: the #MemoryRegion to be initialized.
1266 * @owner: the object that tracks the region's reference count
1267 * @name: Region name, becomes part of RAMBlock name used in migration stream
1268 * must be unique within any device
1269 * @size: size of the region.
1270 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_NORESERVE.
1271 * @errp: pointer to Error*, to store an error if it happens.
1273 * Note that this function does not do anything to cause the data in the
1274 * RAM memory region to be migrated; that is the responsibility of the caller.
1276 void memory_region_init_ram_flags_nomigrate(MemoryRegion
*mr
,
1284 * memory_region_init_resizeable_ram: Initialize memory region with resizable
1285 * RAM. Accesses into the region will
1286 * modify memory directly. Only an initial
1287 * portion of this RAM is actually used.
1288 * Changing the size while migrating
1289 * can result in the migration being
1292 * @mr: the #MemoryRegion to be initialized.
1293 * @owner: the object that tracks the region's reference count
1294 * @name: Region name, becomes part of RAMBlock name used in migration stream
1295 * must be unique within any device
1296 * @size: used size of the region.
1297 * @max_size: max size of the region.
1298 * @resized: callback to notify owner about used size change.
1299 * @errp: pointer to Error*, to store an error if it happens.
1301 * Note that this function does not do anything to cause the data in the
1302 * RAM memory region to be migrated; that is the responsibility of the caller.
1304 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
1309 void (*resized
)(const char*,
1316 * memory_region_init_ram_from_file: Initialize RAM memory region with a
1319 * @mr: the #MemoryRegion to be initialized.
1320 * @owner: the object that tracks the region's reference count
1321 * @name: Region name, becomes part of RAMBlock name used in migration stream
1322 * must be unique within any device
1323 * @size: size of the region.
1324 * @align: alignment of the region base address; if 0, the default alignment
1325 * (getpagesize()) will be used.
1326 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
1328 * @path: the path in which to allocate the RAM.
1329 * @offset: offset within the file referenced by path
1330 * @readonly: true to open @path for reading, false for read/write.
1331 * @errp: pointer to Error*, to store an error if it happens.
1333 * Note that this function does not do anything to cause the data in the
1334 * RAM memory region to be migrated; that is the responsibility of the caller.
1336 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
1348 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
1351 * @mr: the #MemoryRegion to be initialized.
1352 * @owner: the object that tracks the region's reference count
1353 * @name: the name of the region.
1354 * @size: size of the region.
1355 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
1356 * RAM_NORESERVE, RAM_PROTECTED.
1357 * @fd: the fd to mmap.
1358 * @offset: offset within the file referenced by fd
1359 * @errp: pointer to Error*, to store an error if it happens.
1361 * Note that this function does not do anything to cause the data in the
1362 * RAM memory region to be migrated; that is the responsibility of the caller.
1364 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
1375 * memory_region_init_ram_ptr: Initialize RAM memory region from a
1376 * user-provided pointer. Accesses into the
1377 * region will modify memory directly.
1379 * @mr: the #MemoryRegion to be initialized.
1380 * @owner: the object that tracks the region's reference count
1381 * @name: Region name, becomes part of RAMBlock name used in migration stream
1382 * must be unique within any device
1383 * @size: size of the region.
1384 * @ptr: memory to be mapped; must contain at least @size bytes.
1386 * Note that this function does not do anything to cause the data in the
1387 * RAM memory region to be migrated; that is the responsibility of the caller.
1389 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
1396 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
1397 * a user-provided pointer.
1399 * A RAM device represents a mapping to a physical device, such as to a PCI
1400 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
1401 * into the VM address space and access to the region will modify memory
1402 * directly. However, the memory region should not be included in a memory
1403 * dump (device may not be enabled/mapped at the time of the dump), and
1404 * operations incompatible with manipulating MMIO should be avoided. Replaces
1407 * @mr: the #MemoryRegion to be initialized.
1408 * @owner: the object that tracks the region's reference count
1409 * @name: the name of the region.
1410 * @size: size of the region.
1411 * @ptr: memory to be mapped; must contain at least @size bytes.
1413 * Note that this function does not do anything to cause the data in the
1414 * RAM memory region to be migrated; that is the responsibility of the caller.
1415 * (For RAM device memory regions, migrating the contents rarely makes sense.)
1417 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
1424 * memory_region_init_alias: Initialize a memory region that aliases all or a
1425 * part of another memory region.
1427 * @mr: the #MemoryRegion to be initialized.
1428 * @owner: the object that tracks the region's reference count
1429 * @name: used for debugging; not visible to the user or ABI
1430 * @orig: the region to be referenced; @mr will be equivalent to
1431 * @orig between @offset and @offset + @size - 1.
1432 * @offset: start of the section in @orig to be referenced.
1433 * @size: size of the region.
1435 void memory_region_init_alias(MemoryRegion
*mr
,
1443 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
1445 * This has the same effect as calling memory_region_init_ram_nomigrate()
1446 * and then marking the resulting region read-only with
1447 * memory_region_set_readonly().
1449 * Note that this function does not do anything to cause the data in the
1450 * RAM side of the memory region to be migrated; that is the responsibility
1453 * @mr: the #MemoryRegion to be initialized.
1454 * @owner: the object that tracks the region's reference count
1455 * @name: Region name, becomes part of RAMBlock name used in migration stream
1456 * must be unique within any device
1457 * @size: size of the region.
1458 * @errp: pointer to Error*, to store an error if it happens.
1460 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1467 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
1468 * Writes are handled via callbacks.
1470 * Note that this function does not do anything to cause the data in the
1471 * RAM side of the memory region to be migrated; that is the responsibility
1474 * @mr: the #MemoryRegion to be initialized.
1475 * @owner: the object that tracks the region's reference count
1476 * @ops: callbacks for write access handling (must not be NULL).
1477 * @opaque: passed to the read and write callbacks of the @ops structure.
1478 * @name: Region name, becomes part of RAMBlock name used in migration stream
1479 * must be unique within any device
1480 * @size: size of the region.
1481 * @errp: pointer to Error*, to store an error if it happens.
1483 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1485 const MemoryRegionOps
*ops
,
1492 * memory_region_init_iommu: Initialize a memory region of a custom type
1493 * that translates addresses
1495 * An IOMMU region translates addresses and forwards accesses to a target
1498 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
1499 * @_iommu_mr should be a pointer to enough memory for an instance of
1500 * that subclass, @instance_size is the size of that subclass, and
1501 * @mrtypename is its name. This function will initialize @_iommu_mr as an
1502 * instance of the subclass, and its methods will then be called to handle
1503 * accesses to the memory region. See the documentation of
1504 * #IOMMUMemoryRegionClass for further details.
1506 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
1507 * @instance_size: the IOMMUMemoryRegion subclass instance size
1508 * @mrtypename: the type name of the #IOMMUMemoryRegion
1509 * @owner: the object that tracks the region's reference count
1510 * @name: used for debugging; not visible to the user or ABI
1511 * @size: size of the region.
1513 void memory_region_init_iommu(void *_iommu_mr
,
1514 size_t instance_size
,
1515 const char *mrtypename
,
1521 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
1522 * region will modify memory directly.
1524 * @mr: the #MemoryRegion to be initialized
1525 * @owner: the object that tracks the region's reference count (must be
1526 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
1527 * @name: name of the memory region
1528 * @size: size of the region in bytes
1529 * @errp: pointer to Error*, to store an error if it happens.
1531 * This function allocates RAM for a board model or device, and
1532 * arranges for it to be migrated (by calling vmstate_register_ram()
1533 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1536 * TODO: Currently we restrict @owner to being either NULL (for
1537 * global RAM regions with no owner) or devices, so that we can
1538 * give the RAM block a unique name for migration purposes.
1539 * We should lift this restriction and allow arbitrary Objects.
1540 * If you pass a non-NULL non-device @owner then we will assert.
1542 void memory_region_init_ram(MemoryRegion
*mr
,
1549 * memory_region_init_rom: Initialize a ROM memory region.
1551 * This has the same effect as calling memory_region_init_ram()
1552 * and then marking the resulting region read-only with
1553 * memory_region_set_readonly(). This includes arranging for the
1554 * contents to be migrated.
1556 * TODO: Currently we restrict @owner to being either NULL (for
1557 * global RAM regions with no owner) or devices, so that we can
1558 * give the RAM block a unique name for migration purposes.
1559 * We should lift this restriction and allow arbitrary Objects.
1560 * If you pass a non-NULL non-device @owner then we will assert.
1562 * @mr: the #MemoryRegion to be initialized.
1563 * @owner: the object that tracks the region's reference count
1564 * @name: Region name, becomes part of RAMBlock name used in migration stream
1565 * must be unique within any device
1566 * @size: size of the region.
1567 * @errp: pointer to Error*, to store an error if it happens.
1569 void memory_region_init_rom(MemoryRegion
*mr
,
1576 * memory_region_init_rom_device: Initialize a ROM memory region.
1577 * Writes are handled via callbacks.
1579 * This function initializes a memory region backed by RAM for reads
1580 * and callbacks for writes, and arranges for the RAM backing to
1581 * be migrated (by calling vmstate_register_ram()
1582 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1585 * TODO: Currently we restrict @owner to being either NULL (for
1586 * global RAM regions with no owner) or devices, so that we can
1587 * give the RAM block a unique name for migration purposes.
1588 * We should lift this restriction and allow arbitrary Objects.
1589 * If you pass a non-NULL non-device @owner then we will assert.
1591 * @mr: the #MemoryRegion to be initialized.
1592 * @owner: the object that tracks the region's reference count
1593 * @ops: callbacks for write access handling (must not be NULL).
1594 * @opaque: passed to the read and write callbacks of the @ops structure.
1595 * @name: Region name, becomes part of RAMBlock name used in migration stream
1596 * must be unique within any device
1597 * @size: size of the region.
1598 * @errp: pointer to Error*, to store an error if it happens.
1600 void memory_region_init_rom_device(MemoryRegion
*mr
,
1602 const MemoryRegionOps
*ops
,
1610 * memory_region_owner: get a memory region's owner.
1612 * @mr: the memory region being queried.
1614 Object
*memory_region_owner(MemoryRegion
*mr
);
1617 * memory_region_size: get a memory region's size.
1619 * @mr: the memory region being queried.
1621 uint64_t memory_region_size(MemoryRegion
*mr
);
1624 * memory_region_is_ram: check whether a memory region is random access
1626 * Returns %true if a memory region is random access.
1628 * @mr: the memory region being queried
1630 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
1636 * memory_region_is_ram_device: check whether a memory region is a ram device
1638 * Returns %true if a memory region is a device backed ram region
1640 * @mr: the memory region being queried
1642 bool memory_region_is_ram_device(MemoryRegion
*mr
);
1645 * memory_region_is_romd: check whether a memory region is in ROMD mode
1647 * Returns %true if a memory region is a ROM device and currently set to allow
1650 * @mr: the memory region being queried
1652 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
1654 return mr
->rom_device
&& mr
->romd_mode
;
1658 * memory_region_is_protected: check whether a memory region is protected
1660 * Returns %true if a memory region is protected RAM and cannot be accessed
1661 * via standard mechanisms, e.g. DMA.
1663 * @mr: the memory region being queried
1665 bool memory_region_is_protected(MemoryRegion
*mr
);
1668 * memory_region_get_iommu: check whether a memory region is an iommu
1670 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
1673 * @mr: the memory region being queried
1675 static inline IOMMUMemoryRegion
*memory_region_get_iommu(MemoryRegion
*mr
)
1678 return memory_region_get_iommu(mr
->alias
);
1681 return (IOMMUMemoryRegion
*) mr
;
1687 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
1688 * if an iommu or NULL if not
1690 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
1691 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1693 * @iommu_mr: the memory region being queried
1695 static inline IOMMUMemoryRegionClass
*memory_region_get_iommu_class_nocheck(
1696 IOMMUMemoryRegion
*iommu_mr
)
1698 return (IOMMUMemoryRegionClass
*) (((Object
*)iommu_mr
)->class);
1701 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1704 * memory_region_iommu_get_min_page_size: get minimum supported page size
1707 * Returns minimum supported page size for an iommu.
1709 * @iommu_mr: the memory region being queried
1711 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
);
1714 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1716 * Note: for any IOMMU implementation, an in-place mapping change
1717 * should be notified with an UNMAP followed by a MAP.
1719 * @iommu_mr: the memory region that was changed
1720 * @iommu_idx: the IOMMU index for the translation table which has changed
1721 * @event: TLB event with the new entry in the IOMMU translation table.
1722 * The entry replaces all old entries for the same virtual I/O address
1725 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1727 IOMMUTLBEvent event
);
1730 * memory_region_notify_iommu_one: notify a change in an IOMMU translation
1731 * entry to a single notifier
1733 * This works just like memory_region_notify_iommu(), but it only
1734 * notifies a specific notifier, not all of them.
1736 * @notifier: the notifier to be notified
1737 * @event: TLB event with the new entry in the IOMMU translation table.
1738 * The entry replaces all old entries for the same virtual I/O address
1741 void memory_region_notify_iommu_one(IOMMUNotifier
*notifier
,
1742 IOMMUTLBEvent
*event
);
1745 * memory_region_unmap_iommu_notifier_range: notify a unmap for an IOMMU
1746 * translation that covers the
1747 * range of a notifier
1749 * @notifier: the notifier to be notified
1751 void memory_region_unmap_iommu_notifier_range(IOMMUNotifier
*notifier
);
1755 * memory_region_register_iommu_notifier: register a notifier for changes to
1756 * IOMMU translation entries.
1758 * Returns 0 on success, or a negative errno otherwise. In particular,
1759 * -EINVAL indicates that at least one of the attributes of the notifier
1760 * is not supported (flag/range) by the IOMMU memory region. In case of error
1761 * the error object must be created.
1763 * @mr: the memory region to observe
1764 * @n: the IOMMUNotifier to be added; the notify callback receives a
1765 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1766 * ceases to be valid on exit from the notifier.
1767 * @errp: pointer to Error*, to store an error if it happens.
1769 int memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1770 IOMMUNotifier
*n
, Error
**errp
);
1773 * memory_region_iommu_replay: replay existing IOMMU translations to
1774 * a notifier with the minimum page granularity returned by
1775 * mr->iommu_ops->get_page_size().
1777 * Note: this is not related to record-and-replay functionality.
1779 * @iommu_mr: the memory region to observe
1780 * @n: the notifier to which to replay iommu mappings
1782 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
);
1785 * memory_region_unregister_iommu_notifier: unregister a notifier for
1786 * changes to IOMMU translation entries.
1788 * @mr: the memory region which was observed and for which notity_stopped()
1789 * needs to be called
1790 * @n: the notifier to be removed.
1792 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1796 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1797 * defined on the IOMMU.
1799 * Returns 0 on success, or a negative errno otherwise. In particular,
1800 * -EINVAL indicates that the IOMMU does not support the requested
1803 * @iommu_mr: the memory region
1804 * @attr: the requested attribute
1805 * @data: a pointer to the requested attribute data
1807 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1808 enum IOMMUMemoryRegionAttr attr
,
1812 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1813 * use for translations with the given memory transaction attributes.
1815 * @iommu_mr: the memory region
1816 * @attrs: the memory transaction attributes
1818 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1822 * memory_region_iommu_num_indexes: return the total number of IOMMU
1823 * indexes that this IOMMU supports.
1825 * @iommu_mr: the memory region
1827 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
);
1830 * memory_region_iommu_set_page_size_mask: set the supported page
1831 * sizes for a given IOMMU memory region
1833 * @iommu_mr: IOMMU memory region
1834 * @page_size_mask: supported page size mask
1835 * @errp: pointer to Error*, to store an error if it happens.
1837 int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion
*iommu_mr
,
1838 uint64_t page_size_mask
,
1842 * memory_region_name: get a memory region's name
1844 * Returns the string that was used to initialize the memory region.
1846 * @mr: the memory region being queried
1848 const char *memory_region_name(const MemoryRegion
*mr
);
1851 * memory_region_is_logging: return whether a memory region is logging writes
1853 * Returns %true if the memory region is logging writes for the given client
1855 * @mr: the memory region being queried
1856 * @client: the client being queried
1858 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
1861 * memory_region_get_dirty_log_mask: return the clients for which a
1862 * memory region is logging writes.
1864 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1865 * are the bit indices.
1867 * @mr: the memory region being queried
1869 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
1872 * memory_region_is_rom: check whether a memory region is ROM
1874 * Returns %true if a memory region is read-only memory.
1876 * @mr: the memory region being queried
1878 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
1880 return mr
->ram
&& mr
->readonly
;
1884 * memory_region_is_nonvolatile: check whether a memory region is non-volatile
1886 * Returns %true is a memory region is non-volatile memory.
1888 * @mr: the memory region being queried
1890 static inline bool memory_region_is_nonvolatile(MemoryRegion
*mr
)
1892 return mr
->nonvolatile
;
1896 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1898 * Returns a file descriptor backing a file-based RAM memory region,
1899 * or -1 if the region is not a file-based RAM memory region.
1901 * @mr: the RAM or alias memory region being queried.
1903 int memory_region_get_fd(MemoryRegion
*mr
);
1906 * memory_region_from_host: Convert a pointer into a RAM memory region
1907 * and an offset within it.
1909 * Given a host pointer inside a RAM memory region (created with
1910 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1911 * the MemoryRegion and the offset within it.
1913 * Use with care; by the time this function returns, the returned pointer is
1914 * not protected by RCU anymore. If the caller is not within an RCU critical
1915 * section and does not hold the iothread lock, it must have other means of
1916 * protecting the pointer, such as a reference to the region that includes
1917 * the incoming ram_addr_t.
1919 * @ptr: the host pointer to be converted
1920 * @offset: the offset within memory region
1922 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
1925 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1927 * Returns a host pointer to a RAM memory region (created with
1928 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1930 * Use with care; by the time this function returns, the returned pointer is
1931 * not protected by RCU anymore. If the caller is not within an RCU critical
1932 * section and does not hold the iothread lock, it must have other means of
1933 * protecting the pointer, such as a reference to the region that includes
1934 * the incoming ram_addr_t.
1936 * @mr: the memory region being queried.
1938 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
1940 /* memory_region_ram_resize: Resize a RAM region.
1942 * Resizing RAM while migrating can result in the migration being canceled.
1943 * Care has to be taken if the guest might have already detected the memory.
1945 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1946 * @newsize: the new size the region
1947 * @errp: pointer to Error*, to store an error if it happens.
1949 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
1953 * memory_region_msync: Synchronize selected address range of
1954 * a memory mapped region
1956 * @mr: the memory region to be msync
1957 * @addr: the initial address of the range to be sync
1958 * @size: the size of the range to be sync
1960 void memory_region_msync(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1963 * memory_region_writeback: Trigger cache writeback for
1964 * selected address range
1966 * @mr: the memory region to be updated
1967 * @addr: the initial address of the range to be written back
1968 * @size: the size of the range to be written back
1970 void memory_region_writeback(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1973 * memory_region_set_log: Turn dirty logging on or off for a region.
1975 * Turns dirty logging on or off for a specified client (display, migration).
1976 * Only meaningful for RAM regions.
1978 * @mr: the memory region being updated.
1979 * @log: whether dirty logging is to be enabled or disabled.
1980 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1982 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
1985 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1987 * Marks a range of bytes as dirty, after it has been dirtied outside
1990 * @mr: the memory region being dirtied.
1991 * @addr: the address (relative to the start of the region) being dirtied.
1992 * @size: size of the range being dirtied.
1994 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1998 * memory_region_clear_dirty_bitmap - clear dirty bitmap for memory range
2000 * This function is called when the caller wants to clear the remote
2001 * dirty bitmap of a memory range within the memory region. This can
2002 * be used by e.g. KVM to manually clear dirty log when
2003 * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT is declared support by the host
2006 * @mr: the memory region to clear the dirty log upon
2007 * @start: start address offset within the memory region
2008 * @len: length of the memory region to clear dirty bitmap
2010 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
2014 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
2015 * bitmap and clear it.
2017 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
2018 * returns the snapshot. The snapshot can then be used to query dirty
2019 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
2020 * querying the same page multiple times, which is especially useful for
2021 * display updates where the scanlines often are not page aligned.
2023 * The dirty bitmap region which gets copied into the snapshot (and
2024 * cleared afterwards) can be larger than requested. The boundaries
2025 * are rounded up/down so complete bitmap longs (covering 64 pages on
2026 * 64bit hosts) can be copied over into the bitmap snapshot. Which
2027 * isn't a problem for display updates as the extra pages are outside
2028 * the visible area, and in case the visible area changes a full
2029 * display redraw is due anyway. Should other use cases for this
2030 * function emerge we might have to revisit this implementation
2033 * Use g_free to release DirtyBitmapSnapshot.
2035 * @mr: the memory region being queried.
2036 * @addr: the address (relative to the start of the region) being queried.
2037 * @size: the size of the range being queried.
2038 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
2040 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
2046 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
2047 * in the specified dirty bitmap snapshot.
2049 * @mr: the memory region being queried.
2050 * @snap: the dirty bitmap snapshot
2051 * @addr: the address (relative to the start of the region) being queried.
2052 * @size: the size of the range being queried.
2054 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
2055 DirtyBitmapSnapshot
*snap
,
2056 hwaddr addr
, hwaddr size
);
2059 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
2062 * Marks a range of pages as no longer dirty.
2064 * @mr: the region being updated.
2065 * @addr: the start of the subrange being cleaned.
2066 * @size: the size of the subrange being cleaned.
2067 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
2068 * %DIRTY_MEMORY_VGA.
2070 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
2071 hwaddr size
, unsigned client
);
2074 * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate
2075 * TBs (for self-modifying code).
2077 * The MemoryRegionOps->write() callback of a ROM device must use this function
2078 * to mark byte ranges that have been modified internally, such as by directly
2079 * accessing the memory returned by memory_region_get_ram_ptr().
2081 * This function marks the range dirty and invalidates TBs so that TCG can
2082 * detect self-modifying code.
2084 * @mr: the region being flushed.
2085 * @addr: the start, relative to the start of the region, of the range being
2087 * @size: the size, in bytes, of the range being flushed.
2089 void memory_region_flush_rom_device(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
2092 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
2094 * Allows a memory region to be marked as read-only (turning it into a ROM).
2095 * only useful on RAM regions.
2097 * @mr: the region being updated.
2098 * @readonly: whether rhe region is to be ROM or RAM.
2100 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
2103 * memory_region_set_nonvolatile: Turn a memory region non-volatile
2105 * Allows a memory region to be marked as non-volatile.
2106 * only useful on RAM regions.
2108 * @mr: the region being updated.
2109 * @nonvolatile: whether rhe region is to be non-volatile.
2111 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
);
2114 * memory_region_rom_device_set_romd: enable/disable ROMD mode
2116 * Allows a ROM device (initialized with memory_region_init_rom_device() to
2117 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
2118 * device is mapped to guest memory and satisfies read access directly.
2119 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
2120 * Writes are always handled by the #MemoryRegion.write function.
2122 * @mr: the memory region to be updated
2123 * @romd_mode: %true to put the region into ROMD mode
2125 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
2128 * memory_region_set_coalescing: Enable memory coalescing for the region.
2130 * Enabled writes to a region to be queued for later processing. MMIO ->write
2131 * callbacks may be delayed until a non-coalesced MMIO is issued.
2132 * Only useful for IO regions. Roughly similar to write-combining hardware.
2134 * @mr: the memory region to be write coalesced
2136 void memory_region_set_coalescing(MemoryRegion
*mr
);
2139 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
2142 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
2143 * Multiple calls can be issued coalesced disjoint ranges.
2145 * @mr: the memory region to be updated.
2146 * @offset: the start of the range within the region to be coalesced.
2147 * @size: the size of the subrange to be coalesced.
2149 void memory_region_add_coalescing(MemoryRegion
*mr
,
2154 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
2156 * Disables any coalescing caused by memory_region_set_coalescing() or
2157 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
2160 * @mr: the memory region to be updated.
2162 void memory_region_clear_coalescing(MemoryRegion
*mr
);
2165 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
2168 * Ensure that pending coalesced MMIO request are flushed before the memory
2169 * region is accessed. This property is automatically enabled for all regions
2170 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
2172 * @mr: the memory region to be updated.
2174 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
2177 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
2180 * Clear the automatic coalesced MMIO flushing enabled via
2181 * memory_region_set_flush_coalesced. Note that this service has no effect on
2182 * memory regions that have MMIO coalescing enabled for themselves. For them,
2183 * automatic flushing will stop once coalescing is disabled.
2185 * @mr: the memory region to be updated.
2187 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
2190 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
2191 * is written to a location.
2193 * Marks a word in an IO region (initialized with memory_region_init_io())
2194 * as a trigger for an eventfd event. The I/O callback will not be called.
2195 * The caller must be prepared to handle failure (that is, take the required
2196 * action if the callback _is_ called).
2198 * @mr: the memory region being updated.
2199 * @addr: the address within @mr that is to be monitored
2200 * @size: the size of the access to trigger the eventfd
2201 * @match_data: whether to match against @data, instead of just @addr
2202 * @data: the data to match against the guest write
2203 * @e: event notifier to be triggered when @addr, @size, and @data all match.
2205 void memory_region_add_eventfd(MemoryRegion
*mr
,
2213 * memory_region_del_eventfd: Cancel an eventfd.
2215 * Cancels an eventfd trigger requested by a previous
2216 * memory_region_add_eventfd() call.
2218 * @mr: the memory region being updated.
2219 * @addr: the address within @mr that is to be monitored
2220 * @size: the size of the access to trigger the eventfd
2221 * @match_data: whether to match against @data, instead of just @addr
2222 * @data: the data to match against the guest write
2223 * @e: event notifier to be triggered when @addr, @size, and @data all match.
2225 void memory_region_del_eventfd(MemoryRegion
*mr
,
2233 * memory_region_add_subregion: Add a subregion to a container.
2235 * Adds a subregion at @offset. The subregion may not overlap with other
2236 * subregions (except for those explicitly marked as overlapping). A region
2237 * may only be added once as a subregion (unless removed with
2238 * memory_region_del_subregion()); use memory_region_init_alias() if you
2239 * want a region to be a subregion in multiple locations.
2241 * @mr: the region to contain the new subregion; must be a container
2242 * initialized with memory_region_init().
2243 * @offset: the offset relative to @mr where @subregion is added.
2244 * @subregion: the subregion to be added.
2246 void memory_region_add_subregion(MemoryRegion
*mr
,
2248 MemoryRegion
*subregion
);
2250 * memory_region_add_subregion_overlap: Add a subregion to a container
2253 * Adds a subregion at @offset. The subregion may overlap with other
2254 * subregions. Conflicts are resolved by having a higher @priority hide a
2255 * lower @priority. Subregions without priority are taken as @priority 0.
2256 * A region may only be added once as a subregion (unless removed with
2257 * memory_region_del_subregion()); use memory_region_init_alias() if you
2258 * want a region to be a subregion in multiple locations.
2260 * @mr: the region to contain the new subregion; must be a container
2261 * initialized with memory_region_init().
2262 * @offset: the offset relative to @mr where @subregion is added.
2263 * @subregion: the subregion to be added.
2264 * @priority: used for resolving overlaps; highest priority wins.
2266 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
2268 MemoryRegion
*subregion
,
2272 * memory_region_get_ram_addr: Get the ram address associated with a memory
2275 * @mr: the region to be queried
2277 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
2279 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
2281 * memory_region_del_subregion: Remove a subregion.
2283 * Removes a subregion from its container.
2285 * @mr: the container to be updated.
2286 * @subregion: the region being removed; must be a current subregion of @mr.
2288 void memory_region_del_subregion(MemoryRegion
*mr
,
2289 MemoryRegion
*subregion
);
2292 * memory_region_set_enabled: dynamically enable or disable a region
2294 * Enables or disables a memory region. A disabled memory region
2295 * ignores all accesses to itself and its subregions. It does not
2296 * obscure sibling subregions with lower priority - it simply behaves as
2297 * if it was removed from the hierarchy.
2299 * Regions default to being enabled.
2301 * @mr: the region to be updated
2302 * @enabled: whether to enable or disable the region
2304 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
2307 * memory_region_set_address: dynamically update the address of a region
2309 * Dynamically updates the address of a region, relative to its container.
2310 * May be used on regions are currently part of a memory hierarchy.
2312 * @mr: the region to be updated
2313 * @addr: new address, relative to container region
2315 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
2318 * memory_region_set_size: dynamically update the size of a region.
2320 * Dynamically updates the size of a region.
2322 * @mr: the region to be updated
2323 * @size: used size of the region.
2325 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
2328 * memory_region_set_alias_offset: dynamically update a memory alias's offset
2330 * Dynamically updates the offset into the target region that an alias points
2331 * to, as if the fourth argument to memory_region_init_alias() has changed.
2333 * @mr: the #MemoryRegion to be updated; should be an alias.
2334 * @offset: the new offset into the target memory region
2336 void memory_region_set_alias_offset(MemoryRegion
*mr
,
2340 * memory_region_present: checks if an address relative to a @container
2341 * translates into #MemoryRegion within @container
2343 * Answer whether a #MemoryRegion within @container covers the address
2346 * @container: a #MemoryRegion within which @addr is a relative address
2347 * @addr: the area within @container to be searched
2349 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
2352 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
2353 * into another memory region, which does not necessarily imply that it is
2354 * mapped into an address space.
2356 * @mr: a #MemoryRegion which should be checked if it's mapped
2358 bool memory_region_is_mapped(MemoryRegion
*mr
);
2361 * memory_region_get_ram_discard_manager: get the #RamDiscardManager for a
2364 * The #RamDiscardManager cannot change while a memory region is mapped.
2366 * @mr: the #MemoryRegion
2368 RamDiscardManager
*memory_region_get_ram_discard_manager(MemoryRegion
*mr
);
2371 * memory_region_has_ram_discard_manager: check whether a #MemoryRegion has a
2372 * #RamDiscardManager assigned
2374 * @mr: the #MemoryRegion
2376 static inline bool memory_region_has_ram_discard_manager(MemoryRegion
*mr
)
2378 return !!memory_region_get_ram_discard_manager(mr
);
2382 * memory_region_set_ram_discard_manager: set the #RamDiscardManager for a
2385 * This function must not be called for a mapped #MemoryRegion, a #MemoryRegion
2386 * that does not cover RAM, or a #MemoryRegion that already has a
2387 * #RamDiscardManager assigned.
2389 * @mr: the #MemoryRegion
2390 * @rdm: #RamDiscardManager to set
2392 void memory_region_set_ram_discard_manager(MemoryRegion
*mr
,
2393 RamDiscardManager
*rdm
);
2396 * memory_region_find: translate an address/size relative to a
2397 * MemoryRegion into a #MemoryRegionSection.
2399 * Locates the first #MemoryRegion within @mr that overlaps the range
2400 * given by @addr and @size.
2402 * Returns a #MemoryRegionSection that describes a contiguous overlap.
2403 * It will have the following characteristics:
2404 * - @size = 0 iff no overlap was found
2405 * - @mr is non-%NULL iff an overlap was found
2407 * Remember that in the return value the @offset_within_region is
2408 * relative to the returned region (in the .@mr field), not to the
2411 * Similarly, the .@offset_within_address_space is relative to the
2412 * address space that contains both regions, the passed and the
2413 * returned one. However, in the special case where the @mr argument
2414 * has no container (and thus is the root of the address space), the
2415 * following will hold:
2416 * - @offset_within_address_space >= @addr
2417 * - @offset_within_address_space + .@size <= @addr + @size
2419 * @mr: a MemoryRegion within which @addr is a relative address
2420 * @addr: start of the area within @as to be searched
2421 * @size: size of the area to be searched
2423 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
2424 hwaddr addr
, uint64_t size
);
2427 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
2429 * Synchronizes the dirty page log for all address spaces.
2431 * @last_stage: whether this is the last stage of live migration
2433 void memory_global_dirty_log_sync(bool last_stage
);
2436 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
2438 * Synchronizes the vCPUs with a thread that is reading the dirty bitmap.
2439 * This function must be called after the dirty log bitmap is cleared, and
2440 * before dirty guest memory pages are read. If you are using
2441 * #DirtyBitmapSnapshot, memory_region_snapshot_and_clear_dirty() takes
2442 * care of doing this.
2444 void memory_global_after_dirty_log_sync(void);
2447 * memory_region_transaction_begin: Start a transaction.
2449 * During a transaction, changes will be accumulated and made visible
2450 * only when the transaction ends (is committed).
2452 void memory_region_transaction_begin(void);
2455 * memory_region_transaction_commit: Commit a transaction and make changes
2456 * visible to the guest.
2458 void memory_region_transaction_commit(void);
2461 * memory_listener_register: register callbacks to be called when memory
2462 * sections are mapped or unmapped into an address
2465 * @listener: an object containing the callbacks to be called
2466 * @filter: if non-%NULL, only regions in this address space will be observed
2468 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
2471 * memory_listener_unregister: undo the effect of memory_listener_register()
2473 * @listener: an object containing the callbacks to be removed
2475 void memory_listener_unregister(MemoryListener
*listener
);
2478 * memory_global_dirty_log_start: begin dirty logging for all regions
2480 * @flags: purpose of starting dirty log, migration or dirty rate
2482 void memory_global_dirty_log_start(unsigned int flags
);
2485 * memory_global_dirty_log_stop: end dirty logging for all regions
2487 * @flags: purpose of stopping dirty log, migration or dirty rate
2489 void memory_global_dirty_log_stop(unsigned int flags
);
2491 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
, bool disabled
);
2493 bool memory_region_access_valid(MemoryRegion
*mr
, hwaddr addr
,
2494 unsigned size
, bool is_write
,
2498 * memory_region_dispatch_read: perform a read directly to the specified
2501 * @mr: #MemoryRegion to access
2502 * @addr: address within that region
2503 * @pval: pointer to uint64_t which the data is written to
2504 * @op: size, sign, and endianness of the memory operation
2505 * @attrs: memory transaction attributes to use for the access
2507 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
2513 * memory_region_dispatch_write: perform a write directly to the specified
2516 * @mr: #MemoryRegion to access
2517 * @addr: address within that region
2518 * @data: data to write
2519 * @op: size, sign, and endianness of the memory operation
2520 * @attrs: memory transaction attributes to use for the access
2522 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
2529 * address_space_init: initializes an address space
2531 * @as: an uninitialized #AddressSpace
2532 * @root: a #MemoryRegion that routes addresses for the address space
2533 * @name: an address space name. The name is only used for debugging
2536 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
2539 * address_space_destroy: destroy an address space
2541 * Releases all resources associated with an address space. After an address space
2542 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
2545 * @as: address space to be destroyed
2547 void address_space_destroy(AddressSpace
*as
);
2550 * address_space_remove_listeners: unregister all listeners of an address space
2552 * Removes all callbacks previously registered with memory_listener_register()
2555 * @as: an initialized #AddressSpace
2557 void address_space_remove_listeners(AddressSpace
*as
);
2560 * address_space_rw: read from or write to an address space.
2562 * Return a MemTxResult indicating whether the operation succeeded
2563 * or failed (eg unassigned memory, device rejected the transaction,
2566 * @as: #AddressSpace to be accessed
2567 * @addr: address within that address space
2568 * @attrs: memory transaction attributes
2569 * @buf: buffer with the data transferred
2570 * @len: the number of bytes to read or write
2571 * @is_write: indicates the transfer direction
2573 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
2574 MemTxAttrs attrs
, void *buf
,
2575 hwaddr len
, bool is_write
);
2578 * address_space_write: write to address space.
2580 * Return a MemTxResult indicating whether the operation succeeded
2581 * or failed (eg unassigned memory, device rejected the transaction,
2584 * @as: #AddressSpace to be accessed
2585 * @addr: address within that address space
2586 * @attrs: memory transaction attributes
2587 * @buf: buffer with the data transferred
2588 * @len: the number of bytes to write
2590 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
2592 const void *buf
, hwaddr len
);
2595 * address_space_write_rom: write to address space, including ROM.
2597 * This function writes to the specified address space, but will
2598 * write data to both ROM and RAM. This is used for non-guest
2599 * writes like writes from the gdb debug stub or initial loading
2602 * Note that portions of the write which attempt to write data to
2603 * a device will be silently ignored -- only real RAM and ROM will
2606 * Return a MemTxResult indicating whether the operation succeeded
2607 * or failed (eg unassigned memory, device rejected the transaction,
2610 * @as: #AddressSpace to be accessed
2611 * @addr: address within that address space
2612 * @attrs: memory transaction attributes
2613 * @buf: buffer with the data transferred
2614 * @len: the number of bytes to write
2616 MemTxResult
address_space_write_rom(AddressSpace
*as
, hwaddr addr
,
2618 const void *buf
, hwaddr len
);
2620 /* address_space_ld*: load from an address space
2621 * address_space_st*: store to an address space
2623 * These functions perform a load or store of the byte, word,
2624 * longword or quad to the specified address within the AddressSpace.
2625 * The _le suffixed functions treat the data as little endian;
2626 * _be indicates big endian; no suffix indicates "same endianness
2629 * The "guest CPU endianness" accessors are deprecated for use outside
2630 * target-* code; devices should be CPU-agnostic and use either the LE
2631 * or the BE accessors.
2633 * @as #AddressSpace to be accessed
2634 * @addr: address within that address space
2635 * @val: data value, for stores
2636 * @attrs: memory transaction attributes
2637 * @result: location to write the success/failure of the transaction;
2638 * if NULL, this information is discarded
2643 #define ARG1_DECL AddressSpace *as
2644 #include "exec/memory_ldst.h.inc"
2648 #define ARG1_DECL AddressSpace *as
2649 #include "exec/memory_ldst_phys.h.inc"
2651 struct MemoryRegionCache
{
2656 MemoryRegionSection mrs
;
2660 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
2663 /* address_space_ld*_cached: load from a cached #MemoryRegion
2664 * address_space_st*_cached: store into a cached #MemoryRegion
2666 * These functions perform a load or store of the byte, word,
2667 * longword or quad to the specified address. The address is
2668 * a physical address in the AddressSpace, but it must lie within
2669 * a #MemoryRegion that was mapped with address_space_cache_init.
2671 * The _le suffixed functions treat the data as little endian;
2672 * _be indicates big endian; no suffix indicates "same endianness
2675 * The "guest CPU endianness" accessors are deprecated for use outside
2676 * target-* code; devices should be CPU-agnostic and use either the LE
2677 * or the BE accessors.
2679 * @cache: previously initialized #MemoryRegionCache to be accessed
2680 * @addr: address within the address space
2681 * @val: data value, for stores
2682 * @attrs: memory transaction attributes
2683 * @result: location to write the success/failure of the transaction;
2684 * if NULL, this information is discarded
2687 #define SUFFIX _cached_slow
2689 #define ARG1_DECL MemoryRegionCache *cache
2690 #include "exec/memory_ldst.h.inc"
2692 /* Inline fast path for direct RAM access. */
2693 static inline uint8_t address_space_ldub_cached(MemoryRegionCache
*cache
,
2694 hwaddr addr
, MemTxAttrs attrs
, MemTxResult
*result
)
2696 assert(addr
< cache
->len
);
2697 if (likely(cache
->ptr
)) {
2698 return ldub_p(cache
->ptr
+ addr
);
2700 return address_space_ldub_cached_slow(cache
, addr
, attrs
, result
);
2704 static inline void address_space_stb_cached(MemoryRegionCache
*cache
,
2705 hwaddr addr
, uint8_t val
, MemTxAttrs attrs
, MemTxResult
*result
)
2707 assert(addr
< cache
->len
);
2708 if (likely(cache
->ptr
)) {
2709 stb_p(cache
->ptr
+ addr
, val
);
2711 address_space_stb_cached_slow(cache
, addr
, val
, attrs
, result
);
2715 #define ENDIANNESS _le
2716 #include "exec/memory_ldst_cached.h.inc"
2718 #define ENDIANNESS _be
2719 #include "exec/memory_ldst_cached.h.inc"
2721 #define SUFFIX _cached
2723 #define ARG1_DECL MemoryRegionCache *cache
2724 #include "exec/memory_ldst_phys.h.inc"
2726 /* address_space_cache_init: prepare for repeated access to a physical
2729 * @cache: #MemoryRegionCache to be filled
2730 * @as: #AddressSpace to be accessed
2731 * @addr: address within that address space
2732 * @len: length of buffer
2733 * @is_write: indicates the transfer direction
2735 * Will only work with RAM, and may map a subset of the requested range by
2736 * returning a value that is less than @len. On failure, return a negative
2739 * Because it only works with RAM, this function can be used for
2740 * read-modify-write operations. In this case, is_write should be %true.
2742 * Note that addresses passed to the address_space_*_cached functions
2743 * are relative to @addr.
2745 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
2752 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
2754 * @cache: The #MemoryRegionCache to operate on.
2755 * @addr: The first physical address that was written, relative to the
2756 * address that was passed to @address_space_cache_init.
2757 * @access_len: The number of bytes that were written starting at @addr.
2759 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
2764 * address_space_cache_destroy: free a #MemoryRegionCache
2766 * @cache: The #MemoryRegionCache whose memory should be released.
2768 void address_space_cache_destroy(MemoryRegionCache
*cache
);
2770 /* address_space_get_iotlb_entry: translate an address into an IOTLB
2771 * entry. Should be called from an RCU critical section.
2773 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
2774 bool is_write
, MemTxAttrs attrs
);
2776 /* address_space_translate: translate an address range into an address space
2777 * into a MemoryRegion and an address range into that section. Should be
2778 * called from an RCU critical section, to avoid that the last reference
2779 * to the returned region disappears after address_space_translate returns.
2781 * @fv: #FlatView to be accessed
2782 * @addr: address within that address space
2783 * @xlat: pointer to address within the returned memory region section's
2785 * @len: pointer to length
2786 * @is_write: indicates the transfer direction
2787 * @attrs: memory attributes
2789 MemoryRegion
*flatview_translate(FlatView
*fv
,
2790 hwaddr addr
, hwaddr
*xlat
,
2791 hwaddr
*len
, bool is_write
,
2794 static inline MemoryRegion
*address_space_translate(AddressSpace
*as
,
2795 hwaddr addr
, hwaddr
*xlat
,
2796 hwaddr
*len
, bool is_write
,
2799 return flatview_translate(address_space_to_flatview(as
),
2800 addr
, xlat
, len
, is_write
, attrs
);
2803 /* address_space_access_valid: check for validity of accessing an address
2806 * Check whether memory is assigned to the given address space range, and
2807 * access is permitted by any IOMMU regions that are active for the address
2810 * For now, addr and len should be aligned to a page size. This limitation
2811 * will be lifted in the future.
2813 * @as: #AddressSpace to be accessed
2814 * @addr: address within that address space
2815 * @len: length of the area to be checked
2816 * @is_write: indicates the transfer direction
2817 * @attrs: memory attributes
2819 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, hwaddr len
,
2820 bool is_write
, MemTxAttrs attrs
);
2822 /* address_space_map: map a physical memory region into a host virtual address
2824 * May map a subset of the requested range, given by and returned in @plen.
2825 * May return %NULL and set *@plen to zero(0), if resources needed to perform
2826 * the mapping are exhausted.
2827 * Use only for reads OR writes - not for read-modify-write operations.
2828 * Use cpu_register_map_client() to know when retrying the map operation is
2829 * likely to succeed.
2831 * @as: #AddressSpace to be accessed
2832 * @addr: address within that address space
2833 * @plen: pointer to length of buffer; updated on return
2834 * @is_write: indicates the transfer direction
2835 * @attrs: memory attributes
2837 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
2838 hwaddr
*plen
, bool is_write
, MemTxAttrs attrs
);
2840 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2842 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2843 * the amount of memory that was actually read or written by the caller.
2845 * @as: #AddressSpace used
2846 * @buffer: host pointer as returned by address_space_map()
2847 * @len: buffer length as returned by address_space_map()
2848 * @access_len: amount of data actually transferred
2849 * @is_write: indicates the transfer direction
2851 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
2852 bool is_write
, hwaddr access_len
);
2855 /* Internal functions, part of the implementation of address_space_read. */
2856 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
2857 MemTxAttrs attrs
, void *buf
, hwaddr len
);
2858 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
2859 MemTxAttrs attrs
, void *buf
,
2860 hwaddr len
, hwaddr addr1
, hwaddr l
,
2862 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
2864 /* Internal functions, part of the implementation of address_space_read_cached
2865 * and address_space_write_cached. */
2866 MemTxResult
address_space_read_cached_slow(MemoryRegionCache
*cache
,
2867 hwaddr addr
, void *buf
, hwaddr len
);
2868 MemTxResult
address_space_write_cached_slow(MemoryRegionCache
*cache
,
2869 hwaddr addr
, const void *buf
,
2872 int memory_access_size(MemoryRegion
*mr
, unsigned l
, hwaddr addr
);
2873 bool prepare_mmio_access(MemoryRegion
*mr
);
2875 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
2878 return memory_region_is_ram(mr
) && !mr
->readonly
&&
2879 !mr
->rom_device
&& !memory_region_is_ram_device(mr
);
2881 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
2882 memory_region_is_romd(mr
);
2887 * address_space_read: read from an address space.
2889 * Return a MemTxResult indicating whether the operation succeeded
2890 * or failed (eg unassigned memory, device rejected the transaction,
2891 * IOMMU fault). Called within RCU critical section.
2893 * @as: #AddressSpace to be accessed
2894 * @addr: address within that address space
2895 * @attrs: memory transaction attributes
2896 * @buf: buffer with the data transferred
2897 * @len: length of the data transferred
2899 static inline __attribute__((__always_inline__
))
2900 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
2901 MemTxAttrs attrs
, void *buf
,
2904 MemTxResult result
= MEMTX_OK
;
2910 if (__builtin_constant_p(len
)) {
2912 RCU_READ_LOCK_GUARD();
2913 fv
= address_space_to_flatview(as
);
2915 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
2916 if (len
== l
&& memory_access_is_direct(mr
, false)) {
2917 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
2918 memcpy(buf
, ptr
, len
);
2920 result
= flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
2925 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
2931 * address_space_read_cached: read from a cached RAM region
2933 * @cache: Cached region to be addressed
2934 * @addr: address relative to the base of the RAM region
2935 * @buf: buffer with the data transferred
2936 * @len: length of the data transferred
2938 static inline MemTxResult
2939 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2940 void *buf
, hwaddr len
)
2942 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2943 fuzz_dma_read_cb(cache
->xlat
+ addr
, len
, cache
->mrs
.mr
);
2944 if (likely(cache
->ptr
)) {
2945 memcpy(buf
, cache
->ptr
+ addr
, len
);
2948 return address_space_read_cached_slow(cache
, addr
, buf
, len
);
2953 * address_space_write_cached: write to a cached RAM region
2955 * @cache: Cached region to be addressed
2956 * @addr: address relative to the base of the RAM region
2957 * @buf: buffer with the data transferred
2958 * @len: length of the data transferred
2960 static inline MemTxResult
2961 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2962 const void *buf
, hwaddr len
)
2964 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2965 if (likely(cache
->ptr
)) {
2966 memcpy(cache
->ptr
+ addr
, buf
, len
);
2969 return address_space_write_cached_slow(cache
, addr
, buf
, len
);
2974 * address_space_set: Fill address space with a constant byte.
2976 * Return a MemTxResult indicating whether the operation succeeded
2977 * or failed (eg unassigned memory, device rejected the transaction,
2980 * @as: #AddressSpace to be accessed
2981 * @addr: address within that address space
2982 * @c: constant byte to fill the memory
2983 * @len: the number of bytes to fill with the constant byte
2984 * @attrs: memory transaction attributes
2986 MemTxResult
address_space_set(AddressSpace
*as
, hwaddr addr
,
2987 uint8_t c
, hwaddr len
, MemTxAttrs attrs
);
2990 /* enum device_endian to MemOp. */
2991 static inline MemOp
devend_memop(enum device_endian end
)
2993 QEMU_BUILD_BUG_ON(DEVICE_HOST_ENDIAN
!= DEVICE_LITTLE_ENDIAN
&&
2994 DEVICE_HOST_ENDIAN
!= DEVICE_BIG_ENDIAN
);
2996 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
2997 /* Swap if non-host endianness or native (target) endianness */
2998 return (end
== DEVICE_HOST_ENDIAN
) ? 0 : MO_BSWAP
;
3000 const int non_host_endianness
=
3001 DEVICE_LITTLE_ENDIAN
^ DEVICE_BIG_ENDIAN
^ DEVICE_HOST_ENDIAN
;
3003 /* In this case, native (target) endianness needs no swap. */
3004 return (end
== non_host_endianness
) ? MO_BSWAP
: 0;
3010 * Inhibit technologies that require discarding of pages in RAM blocks, e.g.,
3011 * to manage the actual amount of memory consumed by the VM (then, the memory
3012 * provided by RAM blocks might be bigger than the desired memory consumption).
3013 * This *must* be set if:
3014 * - Discarding parts of a RAM blocks does not result in the change being
3015 * reflected in the VM and the pages getting freed.
3016 * - All memory in RAM blocks is pinned or duplicated, invaldiating any previous
3018 * - Discarding parts of a RAM blocks will result in integrity issues (e.g.,
3020 * Technologies that only temporarily pin the current working set of a
3021 * driver are fine, because we don't expect such pages to be discarded
3022 * (esp. based on guest action like balloon inflation).
3024 * This is *not* to be used to protect from concurrent discards (esp.,
3027 * Returns 0 if successful. Returns -EBUSY if a technology that relies on
3028 * discards to work reliably is active.
3030 int ram_block_discard_disable(bool state
);
3033 * See ram_block_discard_disable(): only disable uncoordinated discards,
3034 * keeping coordinated discards (via the RamDiscardManager) enabled.
3036 int ram_block_uncoordinated_discard_disable(bool state
);
3039 * Inhibit technologies that disable discarding of pages in RAM blocks.
3041 * Returns 0 if successful. Returns -EBUSY if discards are already set to
3044 int ram_block_discard_require(bool state
);
3047 * See ram_block_discard_require(): only inhibit technologies that disable
3048 * uncoordinated discarding of pages in RAM blocks, allowing co-existance with
3049 * technologies that only inhibit uncoordinated discards (via the
3050 * RamDiscardManager).
3052 int ram_block_coordinated_discard_require(bool state
);
3055 * Test if any discarding of memory in ram blocks is disabled.
3057 bool ram_block_discard_is_disabled(void);
3060 * Test if any discarding of memory in ram blocks is required to work reliably.
3062 bool ram_block_discard_is_required(void);