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 "qemu:memory-region"
37 #define MEMORY_REGION(obj) \
38 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
40 #define TYPE_IOMMU_MEMORY_REGION "qemu:iommu-memory-region"
41 #define IOMMU_MEMORY_REGION(obj) \
42 OBJECT_CHECK(IOMMUMemoryRegion, (obj), TYPE_IOMMU_MEMORY_REGION)
43 #define IOMMU_MEMORY_REGION_CLASS(klass) \
44 OBJECT_CLASS_CHECK(IOMMUMemoryRegionClass, (klass), \
45 TYPE_IOMMU_MEMORY_REGION)
46 #define IOMMU_MEMORY_REGION_GET_CLASS(obj) \
47 OBJECT_GET_CLASS(IOMMUMemoryRegionClass, (obj), \
48 TYPE_IOMMU_MEMORY_REGION)
50 extern bool global_dirty_log
;
52 typedef struct MemoryRegionOps MemoryRegionOps
;
54 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
56 /* See address_space_translate: bit 0 is read, bit 1 is write. */
64 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
66 struct IOMMUTLBEntry
{
67 AddressSpace
*target_as
;
69 hwaddr translated_addr
;
70 hwaddr addr_mask
; /* 0xfff = 4k translation */
71 IOMMUAccessFlags perm
;
75 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
76 * register with one or multiple IOMMU Notifier capability bit(s).
79 IOMMU_NOTIFIER_NONE
= 0,
80 /* Notify cache invalidations */
81 IOMMU_NOTIFIER_UNMAP
= 0x1,
82 /* Notify entry changes (newly created entries) */
83 IOMMU_NOTIFIER_MAP
= 0x2,
86 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
89 typedef void (*IOMMUNotify
)(struct IOMMUNotifier
*notifier
,
92 struct IOMMUNotifier
{
94 IOMMUNotifierFlag notifier_flags
;
95 /* Notify for address space range start <= addr <= end */
99 QLIST_ENTRY(IOMMUNotifier
) node
;
101 typedef struct IOMMUNotifier IOMMUNotifier
;
103 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
104 #define RAM_PREALLOC (1 << 0)
106 /* RAM is mmap-ed with MAP_SHARED */
107 #define RAM_SHARED (1 << 1)
109 /* Only a portion of RAM (used_length) is actually used, and migrated.
110 * This used_length size can change across reboots.
112 #define RAM_RESIZEABLE (1 << 2)
114 /* UFFDIO_ZEROPAGE is available on this RAMBlock to atomically
115 * zero the page and wake waiting processes.
116 * (Set during postcopy)
118 #define RAM_UF_ZEROPAGE (1 << 3)
120 /* RAM can be migrated */
121 #define RAM_MIGRATABLE (1 << 4)
123 /* RAM is a persistent kind memory */
124 #define RAM_PMEM (1 << 5)
126 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
127 IOMMUNotifierFlag flags
,
128 hwaddr start
, hwaddr end
,
132 n
->notifier_flags
= flags
;
135 n
->iommu_idx
= iommu_idx
;
139 * Memory region callbacks
141 struct MemoryRegionOps
{
142 /* Read from the memory region. @addr is relative to @mr; @size is
144 uint64_t (*read
)(void *opaque
,
147 /* Write to the memory region. @addr is relative to @mr; @size is
149 void (*write
)(void *opaque
,
154 MemTxResult (*read_with_attrs
)(void *opaque
,
159 MemTxResult (*write_with_attrs
)(void *opaque
,
165 enum device_endian endianness
;
166 /* Guest-visible constraints: */
168 /* If nonzero, specify bounds on access sizes beyond which a machine
171 unsigned min_access_size
;
172 unsigned max_access_size
;
173 /* If true, unaligned accesses are supported. Otherwise unaligned
174 * accesses throw machine checks.
178 * If present, and returns #false, the transaction is not accepted
179 * by the device (and results in machine dependent behaviour such
180 * as a machine check exception).
182 bool (*accepts
)(void *opaque
, hwaddr addr
,
183 unsigned size
, bool is_write
,
186 /* Internal implementation constraints: */
188 /* If nonzero, specifies the minimum size implemented. Smaller sizes
189 * will be rounded upwards and a partial result will be returned.
191 unsigned min_access_size
;
192 /* If nonzero, specifies the maximum size implemented. Larger sizes
193 * will be done as a series of accesses with smaller sizes.
195 unsigned max_access_size
;
196 /* If true, unaligned accesses are supported. Otherwise all accesses
197 * are converted to (possibly multiple) naturally aligned accesses.
203 typedef struct MemoryRegionClass
{
205 ObjectClass parent_class
;
209 enum IOMMUMemoryRegionAttr
{
210 IOMMU_ATTR_SPAPR_TCE_FD
214 * IOMMUMemoryRegionClass:
216 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
217 * and provide an implementation of at least the @translate method here
218 * to handle requests to the memory region. Other methods are optional.
220 * The IOMMU implementation must use the IOMMU notifier infrastructure
221 * to report whenever mappings are changed, by calling
222 * memory_region_notify_iommu() (or, if necessary, by calling
223 * memory_region_notify_one() for each registered notifier).
225 * Conceptually an IOMMU provides a mapping from input address
226 * to an output TLB entry. If the IOMMU is aware of memory transaction
227 * attributes and the output TLB entry depends on the transaction
228 * attributes, we represent this using IOMMU indexes. Each index
229 * selects a particular translation table that the IOMMU has:
230 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
231 * @translate takes an input address and an IOMMU index
232 * and the mapping returned can only depend on the input address and the
235 * Most IOMMUs don't care about the transaction attributes and support
236 * only a single IOMMU index. A more complex IOMMU might have one index
237 * for secure transactions and one for non-secure transactions.
239 typedef struct IOMMUMemoryRegionClass
{
241 MemoryRegionClass parent_class
;
244 * Return a TLB entry that contains a given address.
246 * The IOMMUAccessFlags indicated via @flag are optional and may
247 * be specified as IOMMU_NONE to indicate that the caller needs
248 * the full translation information for both reads and writes. If
249 * the access flags are specified then the IOMMU implementation
250 * may use this as an optimization, to stop doing a page table
251 * walk as soon as it knows that the requested permissions are not
252 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
253 * full page table walk and report the permissions in the returned
254 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
255 * return different mappings for reads and writes.)
257 * The returned information remains valid while the caller is
258 * holding the big QEMU lock or is inside an RCU critical section;
259 * if the caller wishes to cache the mapping beyond that it must
260 * register an IOMMU notifier so it can invalidate its cached
261 * information when the IOMMU mapping changes.
263 * @iommu: the IOMMUMemoryRegion
264 * @hwaddr: address to be translated within the memory region
265 * @flag: requested access permissions
266 * @iommu_idx: IOMMU index for the translation
268 IOMMUTLBEntry (*translate
)(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
269 IOMMUAccessFlags flag
, int iommu_idx
);
270 /* Returns minimum supported page size in bytes.
271 * If this method is not provided then the minimum is assumed to
272 * be TARGET_PAGE_SIZE.
274 * @iommu: the IOMMUMemoryRegion
276 uint64_t (*get_min_page_size
)(IOMMUMemoryRegion
*iommu
);
277 /* Called when IOMMU Notifier flag changes (ie when the set of
278 * events which IOMMU users are requesting notification for changes).
279 * Optional method -- need not be provided if the IOMMU does not
280 * need to know exactly which events must be notified.
282 * @iommu: the IOMMUMemoryRegion
283 * @old_flags: events which previously needed to be notified
284 * @new_flags: events which now need to be notified
286 * Returns 0 on success, or a negative errno; in particular
287 * returns -EINVAL if the new flag bitmap is not supported by the
288 * IOMMU memory region. In case of failure, the error object
291 int (*notify_flag_changed
)(IOMMUMemoryRegion
*iommu
,
292 IOMMUNotifierFlag old_flags
,
293 IOMMUNotifierFlag new_flags
,
295 /* Called to handle memory_region_iommu_replay().
297 * The default implementation of memory_region_iommu_replay() is to
298 * call the IOMMU translate method for every page in the address space
299 * with flag == IOMMU_NONE and then call the notifier if translate
300 * returns a valid mapping. If this method is implemented then it
301 * overrides the default behaviour, and must provide the full semantics
302 * of memory_region_iommu_replay(), by calling @notifier for every
303 * translation present in the IOMMU.
305 * Optional method -- an IOMMU only needs to provide this method
306 * if the default is inefficient or produces undesirable side effects.
308 * Note: this is not related to record-and-replay functionality.
310 void (*replay
)(IOMMUMemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
312 /* Get IOMMU misc attributes. This is an optional method that
313 * can be used to allow users of the IOMMU to get implementation-specific
314 * information. The IOMMU implements this method to handle calls
315 * by IOMMU users to memory_region_iommu_get_attr() by filling in
316 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
317 * the IOMMU supports. If the method is unimplemented then
318 * memory_region_iommu_get_attr() will always return -EINVAL.
320 * @iommu: the IOMMUMemoryRegion
321 * @attr: attribute being queried
322 * @data: memory to fill in with the attribute data
324 * Returns 0 on success, or a negative errno; in particular
325 * returns -EINVAL for unrecognized or unimplemented attribute types.
327 int (*get_attr
)(IOMMUMemoryRegion
*iommu
, enum IOMMUMemoryRegionAttr attr
,
330 /* Return the IOMMU index to use for a given set of transaction attributes.
332 * Optional method: if an IOMMU only supports a single IOMMU index then
333 * the default implementation of memory_region_iommu_attrs_to_index()
336 * The indexes supported by an IOMMU must be contiguous, starting at 0.
338 * @iommu: the IOMMUMemoryRegion
339 * @attrs: memory transaction attributes
341 int (*attrs_to_index
)(IOMMUMemoryRegion
*iommu
, MemTxAttrs attrs
);
343 /* Return the number of IOMMU indexes this IOMMU supports.
345 * Optional method: if this method is not provided, then
346 * memory_region_iommu_num_indexes() will return 1, indicating that
347 * only a single IOMMU index is supported.
349 * @iommu: the IOMMUMemoryRegion
351 int (*num_indexes
)(IOMMUMemoryRegion
*iommu
);
352 } IOMMUMemoryRegionClass
;
354 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
355 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
359 * A struct representing a memory region.
361 struct MemoryRegion
{
366 /* The following fields should fit in a cache line */
370 bool readonly
; /* For RAM regions */
373 bool flush_coalesced_mmio
;
375 uint8_t dirty_log_mask
;
380 const MemoryRegionOps
*ops
;
382 MemoryRegion
*container
;
385 void (*destructor
)(MemoryRegion
*mr
);
390 bool warning_printed
; /* For reservations */
391 uint8_t vga_logging_count
;
395 QTAILQ_HEAD(, MemoryRegion
) subregions
;
396 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
397 QTAILQ_HEAD(, CoalescedMemoryRange
) coalesced
;
399 unsigned ioeventfd_nb
;
400 MemoryRegionIoeventfd
*ioeventfds
;
403 struct IOMMUMemoryRegion
{
404 MemoryRegion parent_obj
;
406 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
407 IOMMUNotifierFlag iommu_notify_flags
;
410 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
411 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
414 * MemoryListener: callbacks structure for updates to the physical memory map
416 * Allows a component to adjust to changes in the guest-visible memory map.
417 * Use with memory_listener_register() and memory_listener_unregister().
419 struct MemoryListener
{
423 * Called at the beginning of an address space update transaction.
424 * Followed by calls to #MemoryListener.region_add(),
425 * #MemoryListener.region_del(), #MemoryListener.region_nop(),
426 * #MemoryListener.log_start() and #MemoryListener.log_stop() in
427 * increasing address order.
429 * @listener: The #MemoryListener.
431 void (*begin
)(MemoryListener
*listener
);
436 * Called at the end of an address space update transaction,
437 * after the last call to #MemoryListener.region_add(),
438 * #MemoryListener.region_del() or #MemoryListener.region_nop(),
439 * #MemoryListener.log_start() and #MemoryListener.log_stop().
441 * @listener: The #MemoryListener.
443 void (*commit
)(MemoryListener
*listener
);
448 * Called during an address space update transaction,
449 * for a section of the address space that is new in this address space
450 * space since the last transaction.
452 * @listener: The #MemoryListener.
453 * @section: The new #MemoryRegionSection.
455 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
460 * Called during an address space update transaction,
461 * for a section of the address space that has disappeared in the address
462 * space since the last transaction.
464 * @listener: The #MemoryListener.
465 * @section: The old #MemoryRegionSection.
467 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
472 * Called during an address space update transaction,
473 * for a section of the address space that is in the same place in the address
474 * space as in the last transaction.
476 * @listener: The #MemoryListener.
477 * @section: The #MemoryRegionSection.
479 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
484 * Called during an address space update transaction, after
485 * one of #MemoryListener.region_add(),#MemoryListener.region_del() or
486 * #MemoryListener.region_nop(), if dirty memory logging clients have
487 * become active since the last transaction.
489 * @listener: The #MemoryListener.
490 * @section: The #MemoryRegionSection.
491 * @old: A bitmap of dirty memory logging clients that were active in
492 * the previous transaction.
493 * @new: A bitmap of dirty memory logging clients that are active in
494 * the current transaction.
496 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
502 * Called during an address space update transaction, after
503 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or
504 * #MemoryListener.region_nop() and possibly after
505 * #MemoryListener.log_start(), if dirty memory logging clients have
506 * become inactive since the last transaction.
508 * @listener: The #MemoryListener.
509 * @section: The #MemoryRegionSection.
510 * @old: A bitmap of dirty memory logging clients that were active in
511 * the previous transaction.
512 * @new: A bitmap of dirty memory logging clients that are active in
513 * the current transaction.
515 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
521 * Called by memory_region_snapshot_and_clear_dirty() and
522 * memory_global_dirty_log_sync(), before accessing QEMU's "official"
523 * copy of the dirty memory bitmap for a #MemoryRegionSection.
525 * @listener: The #MemoryListener.
526 * @section: The #MemoryRegionSection.
528 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
533 * Called before reading the dirty memory bitmap for a
534 * #MemoryRegionSection.
536 * @listener: The #MemoryListener.
537 * @section: The #MemoryRegionSection.
539 void (*log_clear
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
544 * Called by memory_global_dirty_log_start(), which
545 * enables the %DIRTY_LOG_MIGRATION client on all memory regions in
546 * the address space. #MemoryListener.log_global_start() is also
547 * called when a #MemoryListener is added, if global dirty logging is
548 * active at that time.
550 * @listener: The #MemoryListener.
552 void (*log_global_start
)(MemoryListener
*listener
);
557 * Called by memory_global_dirty_log_stop(), which
558 * disables the %DIRTY_LOG_MIGRATION client on all memory regions in
561 * @listener: The #MemoryListener.
563 void (*log_global_stop
)(MemoryListener
*listener
);
566 * @log_global_after_sync:
568 * Called after reading the dirty memory bitmap
569 * for any #MemoryRegionSection.
571 * @listener: The #MemoryListener.
573 void (*log_global_after_sync
)(MemoryListener
*listener
);
578 * Called during an address space update transaction,
579 * for a section of the address space that has had a new ioeventfd
580 * registration since the last transaction.
582 * @listener: The #MemoryListener.
583 * @section: The new #MemoryRegionSection.
584 * @match_data: The @match_data parameter for the new ioeventfd.
585 * @data: The @data parameter for the new ioeventfd.
586 * @e: The #EventNotifier parameter for the new ioeventfd.
588 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
589 bool match_data
, uint64_t data
, EventNotifier
*e
);
594 * Called during an address space update transaction,
595 * for a section of the address space that has dropped an ioeventfd
596 * registration since the last transaction.
598 * @listener: The #MemoryListener.
599 * @section: The new #MemoryRegionSection.
600 * @match_data: The @match_data parameter for the dropped ioeventfd.
601 * @data: The @data parameter for the dropped ioeventfd.
602 * @e: The #EventNotifier parameter for the dropped ioeventfd.
604 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
605 bool match_data
, uint64_t data
, EventNotifier
*e
);
610 * Called during an address space update transaction,
611 * for a section of the address space that has had a new coalesced
612 * MMIO range registration since the last transaction.
614 * @listener: The #MemoryListener.
615 * @section: The new #MemoryRegionSection.
616 * @addr: The starting address for the coalesced MMIO range.
617 * @len: The length of the coalesced MMIO range.
619 void (*coalesced_io_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
620 hwaddr addr
, hwaddr len
);
625 * Called during an address space update transaction,
626 * for a section of the address space that has dropped a coalesced
627 * MMIO range since the last transaction.
629 * @listener: The #MemoryListener.
630 * @section: The new #MemoryRegionSection.
631 * @addr: The starting address for the coalesced MMIO range.
632 * @len: The length of the coalesced MMIO range.
634 void (*coalesced_io_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
635 hwaddr addr
, hwaddr len
);
639 * Govern the order in which memory listeners are invoked. Lower priorities
640 * are invoked earlier for "add" or "start" callbacks, and later for "delete"
641 * or "stop" callbacks.
646 AddressSpace
*address_space
;
647 QTAILQ_ENTRY(MemoryListener
) link
;
648 QTAILQ_ENTRY(MemoryListener
) link_as
;
652 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
654 struct AddressSpace
{
660 /* Accessed via RCU. */
661 struct FlatView
*current_map
;
664 struct MemoryRegionIoeventfd
*ioeventfds
;
665 QTAILQ_HEAD(, MemoryListener
) listeners
;
666 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
669 typedef struct AddressSpaceDispatch AddressSpaceDispatch
;
670 typedef struct FlatRange FlatRange
;
672 /* Flattened global view of current active memory hierarchy. Kept in sorted
680 unsigned nr_allocated
;
681 struct AddressSpaceDispatch
*dispatch
;
685 static inline FlatView
*address_space_to_flatview(AddressSpace
*as
)
687 return atomic_rcu_read(&as
->current_map
);
692 * MemoryRegionSection: describes a fragment of a #MemoryRegion
694 * @mr: the region, or %NULL if empty
695 * @fv: the flat view of the address space the region is mapped in
696 * @offset_within_region: the beginning of the section, relative to @mr's start
697 * @size: the size of the section; will not exceed @mr's boundaries
698 * @offset_within_address_space: the address of the first byte of the section
699 * relative to the region's address space
700 * @readonly: writes to this section are ignored
701 * @nonvolatile: this section is non-volatile
703 struct MemoryRegionSection
{
707 hwaddr offset_within_region
;
708 hwaddr offset_within_address_space
;
713 static inline bool MemoryRegionSection_eq(MemoryRegionSection
*a
,
714 MemoryRegionSection
*b
)
716 return a
->mr
== b
->mr
&&
718 a
->offset_within_region
== b
->offset_within_region
&&
719 a
->offset_within_address_space
== b
->offset_within_address_space
&&
720 int128_eq(a
->size
, b
->size
) &&
721 a
->readonly
== b
->readonly
&&
722 a
->nonvolatile
== b
->nonvolatile
;
726 * memory_region_init: Initialize a memory region
728 * The region typically acts as a container for other memory regions. Use
729 * memory_region_add_subregion() to add subregions.
731 * @mr: the #MemoryRegion to be initialized
732 * @owner: the object that tracks the region's reference count
733 * @name: used for debugging; not visible to the user or ABI
734 * @size: size of the region; any subregions beyond this size will be clipped
736 void memory_region_init(MemoryRegion
*mr
,
737 struct Object
*owner
,
742 * memory_region_ref: Add 1 to a memory region's reference count
744 * Whenever memory regions are accessed outside the BQL, they need to be
745 * preserved against hot-unplug. MemoryRegions actually do not have their
746 * own reference count; they piggyback on a QOM object, their "owner".
747 * This function adds a reference to the owner.
749 * All MemoryRegions must have an owner if they can disappear, even if the
750 * device they belong to operates exclusively under the BQL. This is because
751 * the region could be returned at any time by memory_region_find, and this
752 * is usually under guest control.
754 * @mr: the #MemoryRegion
756 void memory_region_ref(MemoryRegion
*mr
);
759 * memory_region_unref: Remove 1 to a memory region's reference count
761 * Whenever memory regions are accessed outside the BQL, they need to be
762 * preserved against hot-unplug. MemoryRegions actually do not have their
763 * own reference count; they piggyback on a QOM object, their "owner".
764 * This function removes a reference to the owner and possibly destroys it.
766 * @mr: the #MemoryRegion
768 void memory_region_unref(MemoryRegion
*mr
);
771 * memory_region_init_io: Initialize an I/O memory region.
773 * Accesses into the region will cause the callbacks in @ops to be called.
774 * if @size is nonzero, subregions will be clipped to @size.
776 * @mr: the #MemoryRegion to be initialized.
777 * @owner: the object that tracks the region's reference count
778 * @ops: a structure containing read and write callbacks to be used when
779 * I/O is performed on the region.
780 * @opaque: passed to the read and write callbacks of the @ops structure.
781 * @name: used for debugging; not visible to the user or ABI
782 * @size: size of the region.
784 void memory_region_init_io(MemoryRegion
*mr
,
785 struct Object
*owner
,
786 const MemoryRegionOps
*ops
,
792 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
793 * into the region will modify memory
796 * @mr: the #MemoryRegion to be initialized.
797 * @owner: the object that tracks the region's reference count
798 * @name: Region name, becomes part of RAMBlock name used in migration stream
799 * must be unique within any device
800 * @size: size of the region.
801 * @errp: pointer to Error*, to store an error if it happens.
803 * Note that this function does not do anything to cause the data in the
804 * RAM memory region to be migrated; that is the responsibility of the caller.
806 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
807 struct Object
*owner
,
813 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
814 * Accesses into the region will
815 * modify memory directly.
817 * @mr: the #MemoryRegion to be initialized.
818 * @owner: the object that tracks the region's reference count
819 * @name: Region name, becomes part of RAMBlock name used in migration stream
820 * must be unique within any device
821 * @size: size of the region.
822 * @share: allow remapping RAM to different addresses
823 * @errp: pointer to Error*, to store an error if it happens.
825 * Note that this function is similar to memory_region_init_ram_nomigrate.
826 * The only difference is part of the RAM region can be remapped.
828 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
829 struct Object
*owner
,
836 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
837 * RAM. Accesses into the region will
838 * modify memory directly. Only an initial
839 * portion of this RAM is actually used.
840 * The used size can change across reboots.
842 * @mr: the #MemoryRegion to be initialized.
843 * @owner: the object that tracks the region's reference count
844 * @name: Region name, becomes part of RAMBlock name used in migration stream
845 * must be unique within any device
846 * @size: used size of the region.
847 * @max_size: max size of the region.
848 * @resized: callback to notify owner about used size change.
849 * @errp: pointer to Error*, to store an error if it happens.
851 * Note that this function does not do anything to cause the data in the
852 * RAM memory region to be migrated; that is the responsibility of the caller.
854 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
855 struct Object
*owner
,
859 void (*resized
)(const char*,
866 * memory_region_init_ram_from_file: Initialize RAM memory region with a
869 * @mr: the #MemoryRegion to be initialized.
870 * @owner: the object that tracks the region's reference count
871 * @name: Region name, becomes part of RAMBlock name used in migration stream
872 * must be unique within any device
873 * @size: size of the region.
874 * @align: alignment of the region base address; if 0, the default alignment
875 * (getpagesize()) will be used.
876 * @ram_flags: Memory region features:
877 * - RAM_SHARED: memory must be mmaped with the MAP_SHARED flag
878 * - RAM_PMEM: the memory is persistent memory
879 * Other bits are ignored now.
880 * @path: the path in which to allocate the RAM.
881 * @errp: pointer to Error*, to store an error if it happens.
883 * Note that this function does not do anything to cause the data in the
884 * RAM memory region to be migrated; that is the responsibility of the caller.
886 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
887 struct Object
*owner
,
896 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
899 * @mr: the #MemoryRegion to be initialized.
900 * @owner: the object that tracks the region's reference count
901 * @name: the name of the region.
902 * @size: size of the region.
903 * @share: %true if memory must be mmaped with the MAP_SHARED flag
904 * @fd: the fd to mmap.
905 * @errp: pointer to Error*, to store an error if it happens.
907 * Note that this function does not do anything to cause the data in the
908 * RAM memory region to be migrated; that is the responsibility of the caller.
910 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
911 struct Object
*owner
,
920 * memory_region_init_ram_ptr: Initialize RAM memory region from a
921 * user-provided pointer. Accesses into the
922 * region will modify memory directly.
924 * @mr: the #MemoryRegion to be initialized.
925 * @owner: the object that tracks the region's reference count
926 * @name: Region name, becomes part of RAMBlock name used in migration stream
927 * must be unique within any device
928 * @size: size of the region.
929 * @ptr: memory to be mapped; must contain at least @size bytes.
931 * Note that this function does not do anything to cause the data in the
932 * RAM memory region to be migrated; that is the responsibility of the caller.
934 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
935 struct Object
*owner
,
941 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
942 * a user-provided pointer.
944 * A RAM device represents a mapping to a physical device, such as to a PCI
945 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
946 * into the VM address space and access to the region will modify memory
947 * directly. However, the memory region should not be included in a memory
948 * dump (device may not be enabled/mapped at the time of the dump), and
949 * operations incompatible with manipulating MMIO should be avoided. Replaces
952 * @mr: the #MemoryRegion to be initialized.
953 * @owner: the object that tracks the region's reference count
954 * @name: the name of the region.
955 * @size: size of the region.
956 * @ptr: memory to be mapped; must contain at least @size bytes.
958 * Note that this function does not do anything to cause the data in the
959 * RAM memory region to be migrated; that is the responsibility of the caller.
960 * (For RAM device memory regions, migrating the contents rarely makes sense.)
962 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
963 struct Object
*owner
,
969 * memory_region_init_alias: Initialize a memory region that aliases all or a
970 * part of another memory region.
972 * @mr: the #MemoryRegion to be initialized.
973 * @owner: the object that tracks the region's reference count
974 * @name: used for debugging; not visible to the user or ABI
975 * @orig: the region to be referenced; @mr will be equivalent to
976 * @orig between @offset and @offset + @size - 1.
977 * @offset: start of the section in @orig to be referenced.
978 * @size: size of the region.
980 void memory_region_init_alias(MemoryRegion
*mr
,
981 struct Object
*owner
,
988 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
990 * This has the same effect as calling memory_region_init_ram_nomigrate()
991 * and then marking the resulting region read-only with
992 * memory_region_set_readonly().
994 * Note that this function does not do anything to cause the data in the
995 * RAM side of the memory region to be migrated; that is the responsibility
998 * @mr: the #MemoryRegion to be initialized.
999 * @owner: the object that tracks the region's reference count
1000 * @name: Region name, becomes part of RAMBlock name used in migration stream
1001 * must be unique within any device
1002 * @size: size of the region.
1003 * @errp: pointer to Error*, to store an error if it happens.
1005 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
1006 struct Object
*owner
,
1012 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
1013 * Writes are handled via callbacks.
1015 * Note that this function does not do anything to cause the data in the
1016 * RAM side of the memory region to be migrated; that is the responsibility
1019 * @mr: the #MemoryRegion to be initialized.
1020 * @owner: the object that tracks the region's reference count
1021 * @ops: callbacks for write access handling (must not be NULL).
1022 * @opaque: passed to the read and write callbacks of the @ops structure.
1023 * @name: Region name, becomes part of RAMBlock name used in migration stream
1024 * must be unique within any device
1025 * @size: size of the region.
1026 * @errp: pointer to Error*, to store an error if it happens.
1028 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
1029 struct Object
*owner
,
1030 const MemoryRegionOps
*ops
,
1037 * memory_region_init_iommu: Initialize a memory region of a custom type
1038 * that translates addresses
1040 * An IOMMU region translates addresses and forwards accesses to a target
1043 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
1044 * @_iommu_mr should be a pointer to enough memory for an instance of
1045 * that subclass, @instance_size is the size of that subclass, and
1046 * @mrtypename is its name. This function will initialize @_iommu_mr as an
1047 * instance of the subclass, and its methods will then be called to handle
1048 * accesses to the memory region. See the documentation of
1049 * #IOMMUMemoryRegionClass for further details.
1051 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
1052 * @instance_size: the IOMMUMemoryRegion subclass instance size
1053 * @mrtypename: the type name of the #IOMMUMemoryRegion
1054 * @owner: the object that tracks the region's reference count
1055 * @name: used for debugging; not visible to the user or ABI
1056 * @size: size of the region.
1058 void memory_region_init_iommu(void *_iommu_mr
,
1059 size_t instance_size
,
1060 const char *mrtypename
,
1066 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
1067 * region will modify memory directly.
1069 * @mr: the #MemoryRegion to be initialized
1070 * @owner: the object that tracks the region's reference count (must be
1071 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
1072 * @name: name of the memory region
1073 * @size: size of the region in bytes
1074 * @errp: pointer to Error*, to store an error if it happens.
1076 * This function allocates RAM for a board model or device, and
1077 * arranges for it to be migrated (by calling vmstate_register_ram()
1078 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1081 * TODO: Currently we restrict @owner to being either NULL (for
1082 * global RAM regions with no owner) or devices, so that we can
1083 * give the RAM block a unique name for migration purposes.
1084 * We should lift this restriction and allow arbitrary Objects.
1085 * If you pass a non-NULL non-device @owner then we will assert.
1087 void memory_region_init_ram(MemoryRegion
*mr
,
1088 struct Object
*owner
,
1094 * memory_region_init_rom: Initialize a ROM memory region.
1096 * This has the same effect as calling memory_region_init_ram()
1097 * and then marking the resulting region read-only with
1098 * memory_region_set_readonly(). This includes arranging for the
1099 * contents to be migrated.
1101 * TODO: Currently we restrict @owner to being either NULL (for
1102 * global RAM regions with no owner) or devices, so that we can
1103 * give the RAM block a unique name for migration purposes.
1104 * We should lift this restriction and allow arbitrary Objects.
1105 * If you pass a non-NULL non-device @owner then we will assert.
1107 * @mr: the #MemoryRegion to be initialized.
1108 * @owner: the object that tracks the region's reference count
1109 * @name: Region name, becomes part of RAMBlock name used in migration stream
1110 * must be unique within any device
1111 * @size: size of the region.
1112 * @errp: pointer to Error*, to store an error if it happens.
1114 void memory_region_init_rom(MemoryRegion
*mr
,
1115 struct Object
*owner
,
1121 * memory_region_init_rom_device: Initialize a ROM memory region.
1122 * Writes are handled via callbacks.
1124 * This function initializes a memory region backed by RAM for reads
1125 * and callbacks for writes, and arranges for the RAM backing to
1126 * be migrated (by calling vmstate_register_ram()
1127 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1130 * TODO: Currently we restrict @owner to being either NULL (for
1131 * global RAM regions with no owner) or devices, so that we can
1132 * give the RAM block a unique name for migration purposes.
1133 * We should lift this restriction and allow arbitrary Objects.
1134 * If you pass a non-NULL non-device @owner then we will assert.
1136 * @mr: the #MemoryRegion to be initialized.
1137 * @owner: the object that tracks the region's reference count
1138 * @ops: callbacks for write access handling (must not be NULL).
1139 * @opaque: passed to the read and write callbacks of the @ops structure.
1140 * @name: Region name, becomes part of RAMBlock name used in migration stream
1141 * must be unique within any device
1142 * @size: size of the region.
1143 * @errp: pointer to Error*, to store an error if it happens.
1145 void memory_region_init_rom_device(MemoryRegion
*mr
,
1146 struct Object
*owner
,
1147 const MemoryRegionOps
*ops
,
1155 * memory_region_owner: get a memory region's owner.
1157 * @mr: the memory region being queried.
1159 struct Object
*memory_region_owner(MemoryRegion
*mr
);
1162 * memory_region_size: get a memory region's size.
1164 * @mr: the memory region being queried.
1166 uint64_t memory_region_size(MemoryRegion
*mr
);
1169 * memory_region_is_ram: check whether a memory region is random access
1171 * Returns %true if a memory region is random access.
1173 * @mr: the memory region being queried
1175 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
1181 * memory_region_is_ram_device: check whether a memory region is a ram device
1183 * Returns %true if a memory region is a device backed ram region
1185 * @mr: the memory region being queried
1187 bool memory_region_is_ram_device(MemoryRegion
*mr
);
1190 * memory_region_is_romd: check whether a memory region is in ROMD mode
1192 * Returns %true if a memory region is a ROM device and currently set to allow
1195 * @mr: the memory region being queried
1197 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
1199 return mr
->rom_device
&& mr
->romd_mode
;
1203 * memory_region_get_iommu: check whether a memory region is an iommu
1205 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
1208 * @mr: the memory region being queried
1210 static inline IOMMUMemoryRegion
*memory_region_get_iommu(MemoryRegion
*mr
)
1213 return memory_region_get_iommu(mr
->alias
);
1216 return (IOMMUMemoryRegion
*) mr
;
1222 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
1223 * if an iommu or NULL if not
1225 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
1226 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1228 * @iommu_mr: the memory region being queried
1230 static inline IOMMUMemoryRegionClass
*memory_region_get_iommu_class_nocheck(
1231 IOMMUMemoryRegion
*iommu_mr
)
1233 return (IOMMUMemoryRegionClass
*) (((Object
*)iommu_mr
)->class);
1236 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1239 * memory_region_iommu_get_min_page_size: get minimum supported page size
1242 * Returns minimum supported page size for an iommu.
1244 * @iommu_mr: the memory region being queried
1246 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
);
1249 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1251 * The notification type will be decided by entry.perm bits:
1253 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
1254 * - For MAP (newly added entry) notifies: set entry.perm to the
1255 * permission of the page (which is definitely !IOMMU_NONE).
1257 * Note: for any IOMMU implementation, an in-place mapping change
1258 * should be notified with an UNMAP followed by a MAP.
1260 * @iommu_mr: the memory region that was changed
1261 * @iommu_idx: the IOMMU index for the translation table which has changed
1262 * @entry: the new entry in the IOMMU translation table. The entry
1263 * replaces all old entries for the same virtual I/O address range.
1264 * Deleted entries have .@perm == 0.
1266 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1268 IOMMUTLBEntry entry
);
1271 * memory_region_notify_one: notify a change in an IOMMU translation
1272 * entry to a single notifier
1274 * This works just like memory_region_notify_iommu(), but it only
1275 * notifies a specific notifier, not all of them.
1277 * @notifier: the notifier to be notified
1278 * @entry: the new entry in the IOMMU translation table. The entry
1279 * replaces all old entries for the same virtual I/O address range.
1280 * Deleted entries have .@perm == 0.
1282 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1283 IOMMUTLBEntry
*entry
);
1286 * memory_region_register_iommu_notifier: register a notifier for changes to
1287 * IOMMU translation entries.
1289 * Returns 0 on success, or a negative errno otherwise. In particular,
1290 * -EINVAL indicates that at least one of the attributes of the notifier
1291 * is not supported (flag/range) by the IOMMU memory region. In case of error
1292 * the error object must be created.
1294 * @mr: the memory region to observe
1295 * @n: the IOMMUNotifier to be added; the notify callback receives a
1296 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1297 * ceases to be valid on exit from the notifier.
1298 * @errp: pointer to Error*, to store an error if it happens.
1300 int memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1301 IOMMUNotifier
*n
, Error
**errp
);
1304 * memory_region_iommu_replay: replay existing IOMMU translations to
1305 * a notifier with the minimum page granularity returned by
1306 * mr->iommu_ops->get_page_size().
1308 * Note: this is not related to record-and-replay functionality.
1310 * @iommu_mr: the memory region to observe
1311 * @n: the notifier to which to replay iommu mappings
1313 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
);
1316 * memory_region_unregister_iommu_notifier: unregister a notifier for
1317 * changes to IOMMU translation entries.
1319 * @mr: the memory region which was observed and for which notity_stopped()
1320 * needs to be called
1321 * @n: the notifier to be removed.
1323 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1327 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1328 * defined on the IOMMU.
1330 * Returns 0 on success, or a negative errno otherwise. In particular,
1331 * -EINVAL indicates that the IOMMU does not support the requested
1334 * @iommu_mr: the memory region
1335 * @attr: the requested attribute
1336 * @data: a pointer to the requested attribute data
1338 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1339 enum IOMMUMemoryRegionAttr attr
,
1343 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1344 * use for translations with the given memory transaction attributes.
1346 * @iommu_mr: the memory region
1347 * @attrs: the memory transaction attributes
1349 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1353 * memory_region_iommu_num_indexes: return the total number of IOMMU
1354 * indexes that this IOMMU supports.
1356 * @iommu_mr: the memory region
1358 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
);
1361 * memory_region_name: get a memory region's name
1363 * Returns the string that was used to initialize the memory region.
1365 * @mr: the memory region being queried
1367 const char *memory_region_name(const MemoryRegion
*mr
);
1370 * memory_region_is_logging: return whether a memory region is logging writes
1372 * Returns %true if the memory region is logging writes for the given client
1374 * @mr: the memory region being queried
1375 * @client: the client being queried
1377 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
1380 * memory_region_get_dirty_log_mask: return the clients for which a
1381 * memory region is logging writes.
1383 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1384 * are the bit indices.
1386 * @mr: the memory region being queried
1388 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
1391 * memory_region_is_rom: check whether a memory region is ROM
1393 * Returns %true if a memory region is read-only memory.
1395 * @mr: the memory region being queried
1397 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
1399 return mr
->ram
&& mr
->readonly
;
1403 * memory_region_is_nonvolatile: check whether a memory region is non-volatile
1405 * Returns %true is a memory region is non-volatile memory.
1407 * @mr: the memory region being queried
1409 static inline bool memory_region_is_nonvolatile(MemoryRegion
*mr
)
1411 return mr
->nonvolatile
;
1415 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1417 * Returns a file descriptor backing a file-based RAM memory region,
1418 * or -1 if the region is not a file-based RAM memory region.
1420 * @mr: the RAM or alias memory region being queried.
1422 int memory_region_get_fd(MemoryRegion
*mr
);
1425 * memory_region_from_host: Convert a pointer into a RAM memory region
1426 * and an offset within it.
1428 * Given a host pointer inside a RAM memory region (created with
1429 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1430 * the MemoryRegion and the offset within it.
1432 * Use with care; by the time this function returns, the returned pointer is
1433 * not protected by RCU anymore. If the caller is not within an RCU critical
1434 * section and does not hold the iothread lock, it must have other means of
1435 * protecting the pointer, such as a reference to the region that includes
1436 * the incoming ram_addr_t.
1438 * @ptr: the host pointer to be converted
1439 * @offset: the offset within memory region
1441 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
1444 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1446 * Returns a host pointer to a RAM memory region (created with
1447 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1449 * Use with care; by the time this function returns, the returned pointer is
1450 * not protected by RCU anymore. If the caller is not within an RCU critical
1451 * section and does not hold the iothread lock, it must have other means of
1452 * protecting the pointer, such as a reference to the region that includes
1453 * the incoming ram_addr_t.
1455 * @mr: the memory region being queried.
1457 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
1459 /* memory_region_ram_resize: Resize a RAM region.
1461 * Only legal before guest might have detected the memory size: e.g. on
1462 * incoming migration, or right after reset.
1464 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1465 * @newsize: the new size the region
1466 * @errp: pointer to Error*, to store an error if it happens.
1468 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
1472 * memory_region_msync: Synchronize selected address range of
1473 * a memory mapped region
1475 * @mr: the memory region to be msync
1476 * @addr: the initial address of the range to be sync
1477 * @size: the size of the range to be sync
1479 void memory_region_msync(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1482 * memory_region_writeback: Trigger cache writeback for
1483 * selected address range
1485 * @mr: the memory region to be updated
1486 * @addr: the initial address of the range to be written back
1487 * @size: the size of the range to be written back
1489 void memory_region_writeback(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1492 * memory_region_set_log: Turn dirty logging on or off for a region.
1494 * Turns dirty logging on or off for a specified client (display, migration).
1495 * Only meaningful for RAM regions.
1497 * @mr: the memory region being updated.
1498 * @log: whether dirty logging is to be enabled or disabled.
1499 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1501 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
1504 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1506 * Marks a range of bytes as dirty, after it has been dirtied outside
1509 * @mr: the memory region being dirtied.
1510 * @addr: the address (relative to the start of the region) being dirtied.
1511 * @size: size of the range being dirtied.
1513 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1517 * memory_region_clear_dirty_bitmap - clear dirty bitmap for memory range
1519 * This function is called when the caller wants to clear the remote
1520 * dirty bitmap of a memory range within the memory region. This can
1521 * be used by e.g. KVM to manually clear dirty log when
1522 * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT is declared support by the host
1525 * @mr: the memory region to clear the dirty log upon
1526 * @start: start address offset within the memory region
1527 * @len: length of the memory region to clear dirty bitmap
1529 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
1533 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1534 * bitmap and clear it.
1536 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1537 * returns the snapshot. The snapshot can then be used to query dirty
1538 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1539 * querying the same page multiple times, which is especially useful for
1540 * display updates where the scanlines often are not page aligned.
1542 * The dirty bitmap region which gets copyed into the snapshot (and
1543 * cleared afterwards) can be larger than requested. The boundaries
1544 * are rounded up/down so complete bitmap longs (covering 64 pages on
1545 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1546 * isn't a problem for display updates as the extra pages are outside
1547 * the visible area, and in case the visible area changes a full
1548 * display redraw is due anyway. Should other use cases for this
1549 * function emerge we might have to revisit this implementation
1552 * Use g_free to release DirtyBitmapSnapshot.
1554 * @mr: the memory region being queried.
1555 * @addr: the address (relative to the start of the region) being queried.
1556 * @size: the size of the range being queried.
1557 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1559 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1565 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1566 * in the specified dirty bitmap snapshot.
1568 * @mr: the memory region being queried.
1569 * @snap: the dirty bitmap snapshot
1570 * @addr: the address (relative to the start of the region) being queried.
1571 * @size: the size of the range being queried.
1573 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
1574 DirtyBitmapSnapshot
*snap
,
1575 hwaddr addr
, hwaddr size
);
1578 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1581 * Marks a range of pages as no longer dirty.
1583 * @mr: the region being updated.
1584 * @addr: the start of the subrange being cleaned.
1585 * @size: the size of the subrange being cleaned.
1586 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1587 * %DIRTY_MEMORY_VGA.
1589 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1590 hwaddr size
, unsigned client
);
1593 * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate
1594 * TBs (for self-modifying code).
1596 * The MemoryRegionOps->write() callback of a ROM device must use this function
1597 * to mark byte ranges that have been modified internally, such as by directly
1598 * accessing the memory returned by memory_region_get_ram_ptr().
1600 * This function marks the range dirty and invalidates TBs so that TCG can
1601 * detect self-modifying code.
1603 * @mr: the region being flushed.
1604 * @addr: the start, relative to the start of the region, of the range being
1606 * @size: the size, in bytes, of the range being flushed.
1608 void memory_region_flush_rom_device(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1611 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1613 * Allows a memory region to be marked as read-only (turning it into a ROM).
1614 * only useful on RAM regions.
1616 * @mr: the region being updated.
1617 * @readonly: whether rhe region is to be ROM or RAM.
1619 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
1622 * memory_region_set_nonvolatile: Turn a memory region non-volatile
1624 * Allows a memory region to be marked as non-volatile.
1625 * only useful on RAM regions.
1627 * @mr: the region being updated.
1628 * @nonvolatile: whether rhe region is to be non-volatile.
1630 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
);
1633 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1635 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1636 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1637 * device is mapped to guest memory and satisfies read access directly.
1638 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1639 * Writes are always handled by the #MemoryRegion.write function.
1641 * @mr: the memory region to be updated
1642 * @romd_mode: %true to put the region into ROMD mode
1644 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
1647 * memory_region_set_coalescing: Enable memory coalescing for the region.
1649 * Enabled writes to a region to be queued for later processing. MMIO ->write
1650 * callbacks may be delayed until a non-coalesced MMIO is issued.
1651 * Only useful for IO regions. Roughly similar to write-combining hardware.
1653 * @mr: the memory region to be write coalesced
1655 void memory_region_set_coalescing(MemoryRegion
*mr
);
1658 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1661 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1662 * Multiple calls can be issued coalesced disjoint ranges.
1664 * @mr: the memory region to be updated.
1665 * @offset: the start of the range within the region to be coalesced.
1666 * @size: the size of the subrange to be coalesced.
1668 void memory_region_add_coalescing(MemoryRegion
*mr
,
1673 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1675 * Disables any coalescing caused by memory_region_set_coalescing() or
1676 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1679 * @mr: the memory region to be updated.
1681 void memory_region_clear_coalescing(MemoryRegion
*mr
);
1684 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1687 * Ensure that pending coalesced MMIO request are flushed before the memory
1688 * region is accessed. This property is automatically enabled for all regions
1689 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1691 * @mr: the memory region to be updated.
1693 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
1696 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1699 * Clear the automatic coalesced MMIO flushing enabled via
1700 * memory_region_set_flush_coalesced. Note that this service has no effect on
1701 * memory regions that have MMIO coalescing enabled for themselves. For them,
1702 * automatic flushing will stop once coalescing is disabled.
1704 * @mr: the memory region to be updated.
1706 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1709 * memory_region_clear_global_locking: Declares that access processing does
1710 * not depend on the QEMU global lock.
1712 * By clearing this property, accesses to the memory region will be processed
1713 * outside of QEMU's global lock (unless the lock is held on when issuing the
1714 * access request). In this case, the device model implementing the access
1715 * handlers is responsible for synchronization of concurrency.
1717 * @mr: the memory region to be updated.
1719 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1722 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1723 * is written to a location.
1725 * Marks a word in an IO region (initialized with memory_region_init_io())
1726 * as a trigger for an eventfd event. The I/O callback will not be called.
1727 * The caller must be prepared to handle failure (that is, take the required
1728 * action if the callback _is_ called).
1730 * @mr: the memory region being updated.
1731 * @addr: the address within @mr that is to be monitored
1732 * @size: the size of the access to trigger the eventfd
1733 * @match_data: whether to match against @data, instead of just @addr
1734 * @data: the data to match against the guest write
1735 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1737 void memory_region_add_eventfd(MemoryRegion
*mr
,
1745 * memory_region_del_eventfd: Cancel an eventfd.
1747 * Cancels an eventfd trigger requested by a previous
1748 * memory_region_add_eventfd() call.
1750 * @mr: the memory region being updated.
1751 * @addr: the address within @mr that is to be monitored
1752 * @size: the size of the access to trigger the eventfd
1753 * @match_data: whether to match against @data, instead of just @addr
1754 * @data: the data to match against the guest write
1755 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1757 void memory_region_del_eventfd(MemoryRegion
*mr
,
1765 * memory_region_add_subregion: Add a subregion to a container.
1767 * Adds a subregion at @offset. The subregion may not overlap with other
1768 * subregions (except for those explicitly marked as overlapping). A region
1769 * may only be added once as a subregion (unless removed with
1770 * memory_region_del_subregion()); use memory_region_init_alias() if you
1771 * want a region to be a subregion in multiple locations.
1773 * @mr: the region to contain the new subregion; must be a container
1774 * initialized with memory_region_init().
1775 * @offset: the offset relative to @mr where @subregion is added.
1776 * @subregion: the subregion to be added.
1778 void memory_region_add_subregion(MemoryRegion
*mr
,
1780 MemoryRegion
*subregion
);
1782 * memory_region_add_subregion_overlap: Add a subregion to a container
1785 * Adds a subregion at @offset. The subregion may overlap with other
1786 * subregions. Conflicts are resolved by having a higher @priority hide a
1787 * lower @priority. Subregions without priority are taken as @priority 0.
1788 * A region may only be added once as a subregion (unless removed with
1789 * memory_region_del_subregion()); use memory_region_init_alias() if you
1790 * want a region to be a subregion in multiple locations.
1792 * @mr: the region to contain the new subregion; must be a container
1793 * initialized with memory_region_init().
1794 * @offset: the offset relative to @mr where @subregion is added.
1795 * @subregion: the subregion to be added.
1796 * @priority: used for resolving overlaps; highest priority wins.
1798 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1800 MemoryRegion
*subregion
,
1804 * memory_region_get_ram_addr: Get the ram address associated with a memory
1807 * @mr: the region to be queried
1809 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1811 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1813 * memory_region_del_subregion: Remove a subregion.
1815 * Removes a subregion from its container.
1817 * @mr: the container to be updated.
1818 * @subregion: the region being removed; must be a current subregion of @mr.
1820 void memory_region_del_subregion(MemoryRegion
*mr
,
1821 MemoryRegion
*subregion
);
1824 * memory_region_set_enabled: dynamically enable or disable a region
1826 * Enables or disables a memory region. A disabled memory region
1827 * ignores all accesses to itself and its subregions. It does not
1828 * obscure sibling subregions with lower priority - it simply behaves as
1829 * if it was removed from the hierarchy.
1831 * Regions default to being enabled.
1833 * @mr: the region to be updated
1834 * @enabled: whether to enable or disable the region
1836 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1839 * memory_region_set_address: dynamically update the address of a region
1841 * Dynamically updates the address of a region, relative to its container.
1842 * May be used on regions are currently part of a memory hierarchy.
1844 * @mr: the region to be updated
1845 * @addr: new address, relative to container region
1847 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1850 * memory_region_set_size: dynamically update the size of a region.
1852 * Dynamically updates the size of a region.
1854 * @mr: the region to be updated
1855 * @size: used size of the region.
1857 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1860 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1862 * Dynamically updates the offset into the target region that an alias points
1863 * to, as if the fourth argument to memory_region_init_alias() has changed.
1865 * @mr: the #MemoryRegion to be updated; should be an alias.
1866 * @offset: the new offset into the target memory region
1868 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1872 * memory_region_present: checks if an address relative to a @container
1873 * translates into #MemoryRegion within @container
1875 * Answer whether a #MemoryRegion within @container covers the address
1878 * @container: a #MemoryRegion within which @addr is a relative address
1879 * @addr: the area within @container to be searched
1881 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1884 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1885 * into any address space.
1887 * @mr: a #MemoryRegion which should be checked if it's mapped
1889 bool memory_region_is_mapped(MemoryRegion
*mr
);
1892 * memory_region_find: translate an address/size relative to a
1893 * MemoryRegion into a #MemoryRegionSection.
1895 * Locates the first #MemoryRegion within @mr that overlaps the range
1896 * given by @addr and @size.
1898 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1899 * It will have the following characteristics:
1900 * - @size = 0 iff no overlap was found
1901 * - @mr is non-%NULL iff an overlap was found
1903 * Remember that in the return value the @offset_within_region is
1904 * relative to the returned region (in the .@mr field), not to the
1907 * Similarly, the .@offset_within_address_space is relative to the
1908 * address space that contains both regions, the passed and the
1909 * returned one. However, in the special case where the @mr argument
1910 * has no container (and thus is the root of the address space), the
1911 * following will hold:
1912 * - @offset_within_address_space >= @addr
1913 * - @offset_within_address_space + .@size <= @addr + @size
1915 * @mr: a MemoryRegion within which @addr is a relative address
1916 * @addr: start of the area within @as to be searched
1917 * @size: size of the area to be searched
1919 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1920 hwaddr addr
, uint64_t size
);
1923 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1925 * Synchronizes the dirty page log for all address spaces.
1927 void memory_global_dirty_log_sync(void);
1930 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1932 * Synchronizes the vCPUs with a thread that is reading the dirty bitmap.
1933 * This function must be called after the dirty log bitmap is cleared, and
1934 * before dirty guest memory pages are read. If you are using
1935 * #DirtyBitmapSnapshot, memory_region_snapshot_and_clear_dirty() takes
1936 * care of doing this.
1938 void memory_global_after_dirty_log_sync(void);
1941 * memory_region_transaction_begin: Start a transaction.
1943 * During a transaction, changes will be accumulated and made visible
1944 * only when the transaction ends (is committed).
1946 void memory_region_transaction_begin(void);
1949 * memory_region_transaction_commit: Commit a transaction and make changes
1950 * visible to the guest.
1952 void memory_region_transaction_commit(void);
1955 * memory_listener_register: register callbacks to be called when memory
1956 * sections are mapped or unmapped into an address
1959 * @listener: an object containing the callbacks to be called
1960 * @filter: if non-%NULL, only regions in this address space will be observed
1962 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1965 * memory_listener_unregister: undo the effect of memory_listener_register()
1967 * @listener: an object containing the callbacks to be removed
1969 void memory_listener_unregister(MemoryListener
*listener
);
1972 * memory_global_dirty_log_start: begin dirty logging for all regions
1974 void memory_global_dirty_log_start(void);
1977 * memory_global_dirty_log_stop: end dirty logging for all regions
1979 void memory_global_dirty_log_stop(void);
1981 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
, bool disabled
);
1984 * memory_region_dispatch_read: perform a read directly to the specified
1987 * @mr: #MemoryRegion to access
1988 * @addr: address within that region
1989 * @pval: pointer to uint64_t which the data is written to
1990 * @op: size, sign, and endianness of the memory operation
1991 * @attrs: memory transaction attributes to use for the access
1993 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1999 * memory_region_dispatch_write: perform a write directly to the specified
2002 * @mr: #MemoryRegion to access
2003 * @addr: address within that region
2004 * @data: data to write
2005 * @op: size, sign, and endianness of the memory operation
2006 * @attrs: memory transaction attributes to use for the access
2008 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
2015 * address_space_init: initializes an address space
2017 * @as: an uninitialized #AddressSpace
2018 * @root: a #MemoryRegion that routes addresses for the address space
2019 * @name: an address space name. The name is only used for debugging
2022 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
2025 * address_space_destroy: destroy an address space
2027 * Releases all resources associated with an address space. After an address space
2028 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
2031 * @as: address space to be destroyed
2033 void address_space_destroy(AddressSpace
*as
);
2036 * address_space_remove_listeners: unregister all listeners of an address space
2038 * Removes all callbacks previously registered with memory_listener_register()
2041 * @as: an initialized #AddressSpace
2043 void address_space_remove_listeners(AddressSpace
*as
);
2046 * address_space_rw: read from or write to an address space.
2048 * Return a MemTxResult indicating whether the operation succeeded
2049 * or failed (eg unassigned memory, device rejected the transaction,
2052 * @as: #AddressSpace to be accessed
2053 * @addr: address within that address space
2054 * @attrs: memory transaction attributes
2055 * @buf: buffer with the data transferred
2056 * @len: the number of bytes to read or write
2057 * @is_write: indicates the transfer direction
2059 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
2060 MemTxAttrs attrs
, void *buf
,
2061 hwaddr len
, bool is_write
);
2064 * address_space_write: write to address space.
2066 * Return a MemTxResult indicating whether the operation succeeded
2067 * or failed (eg unassigned memory, device rejected the transaction,
2070 * @as: #AddressSpace to be accessed
2071 * @addr: address within that address space
2072 * @attrs: memory transaction attributes
2073 * @buf: buffer with the data transferred
2074 * @len: the number of bytes to write
2076 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
2078 const void *buf
, hwaddr len
);
2081 * address_space_write_rom: write to address space, including ROM.
2083 * This function writes to the specified address space, but will
2084 * write data to both ROM and RAM. This is used for non-guest
2085 * writes like writes from the gdb debug stub or initial loading
2088 * Note that portions of the write which attempt to write data to
2089 * a device will be silently ignored -- only real RAM and ROM will
2092 * Return a MemTxResult indicating whether the operation succeeded
2093 * or failed (eg unassigned memory, device rejected the transaction,
2096 * @as: #AddressSpace to be accessed
2097 * @addr: address within that address space
2098 * @attrs: memory transaction attributes
2099 * @buf: buffer with the data transferred
2100 * @len: the number of bytes to write
2102 MemTxResult
address_space_write_rom(AddressSpace
*as
, hwaddr addr
,
2104 const void *buf
, hwaddr len
);
2106 /* address_space_ld*: load from an address space
2107 * address_space_st*: store to an address space
2109 * These functions perform a load or store of the byte, word,
2110 * longword or quad to the specified address within the AddressSpace.
2111 * The _le suffixed functions treat the data as little endian;
2112 * _be indicates big endian; no suffix indicates "same endianness
2115 * The "guest CPU endianness" accessors are deprecated for use outside
2116 * target-* code; devices should be CPU-agnostic and use either the LE
2117 * or the BE accessors.
2119 * @as #AddressSpace to be accessed
2120 * @addr: address within that address space
2121 * @val: data value, for stores
2122 * @attrs: memory transaction attributes
2123 * @result: location to write the success/failure of the transaction;
2124 * if NULL, this information is discarded
2129 #define ARG1_DECL AddressSpace *as
2130 #include "exec/memory_ldst.inc.h"
2134 #define ARG1_DECL AddressSpace *as
2135 #include "exec/memory_ldst_phys.inc.h"
2137 struct MemoryRegionCache
{
2142 MemoryRegionSection mrs
;
2146 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
2149 /* address_space_ld*_cached: load from a cached #MemoryRegion
2150 * address_space_st*_cached: store into a cached #MemoryRegion
2152 * These functions perform a load or store of the byte, word,
2153 * longword or quad to the specified address. The address is
2154 * a physical address in the AddressSpace, but it must lie within
2155 * a #MemoryRegion that was mapped with address_space_cache_init.
2157 * The _le suffixed functions treat the data as little endian;
2158 * _be indicates big endian; no suffix indicates "same endianness
2161 * The "guest CPU endianness" accessors are deprecated for use outside
2162 * target-* code; devices should be CPU-agnostic and use either the LE
2163 * or the BE accessors.
2165 * @cache: previously initialized #MemoryRegionCache to be accessed
2166 * @addr: address within the address space
2167 * @val: data value, for stores
2168 * @attrs: memory transaction attributes
2169 * @result: location to write the success/failure of the transaction;
2170 * if NULL, this information is discarded
2173 #define SUFFIX _cached_slow
2175 #define ARG1_DECL MemoryRegionCache *cache
2176 #include "exec/memory_ldst.inc.h"
2178 /* Inline fast path for direct RAM access. */
2179 static inline uint8_t address_space_ldub_cached(MemoryRegionCache
*cache
,
2180 hwaddr addr
, MemTxAttrs attrs
, MemTxResult
*result
)
2182 assert(addr
< cache
->len
);
2183 if (likely(cache
->ptr
)) {
2184 return ldub_p(cache
->ptr
+ addr
);
2186 return address_space_ldub_cached_slow(cache
, addr
, attrs
, result
);
2190 static inline void address_space_stb_cached(MemoryRegionCache
*cache
,
2191 hwaddr addr
, uint32_t val
, MemTxAttrs attrs
, MemTxResult
*result
)
2193 assert(addr
< cache
->len
);
2194 if (likely(cache
->ptr
)) {
2195 stb_p(cache
->ptr
+ addr
, val
);
2197 address_space_stb_cached_slow(cache
, addr
, val
, attrs
, result
);
2201 #define ENDIANNESS _le
2202 #include "exec/memory_ldst_cached.inc.h"
2204 #define ENDIANNESS _be
2205 #include "exec/memory_ldst_cached.inc.h"
2207 #define SUFFIX _cached
2209 #define ARG1_DECL MemoryRegionCache *cache
2210 #include "exec/memory_ldst_phys.inc.h"
2212 /* address_space_cache_init: prepare for repeated access to a physical
2215 * @cache: #MemoryRegionCache to be filled
2216 * @as: #AddressSpace to be accessed
2217 * @addr: address within that address space
2218 * @len: length of buffer
2219 * @is_write: indicates the transfer direction
2221 * Will only work with RAM, and may map a subset of the requested range by
2222 * returning a value that is less than @len. On failure, return a negative
2225 * Because it only works with RAM, this function can be used for
2226 * read-modify-write operations. In this case, is_write should be %true.
2228 * Note that addresses passed to the address_space_*_cached functions
2229 * are relative to @addr.
2231 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
2238 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
2240 * @cache: The #MemoryRegionCache to operate on.
2241 * @addr: The first physical address that was written, relative to the
2242 * address that was passed to @address_space_cache_init.
2243 * @access_len: The number of bytes that were written starting at @addr.
2245 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
2250 * address_space_cache_destroy: free a #MemoryRegionCache
2252 * @cache: The #MemoryRegionCache whose memory should be released.
2254 void address_space_cache_destroy(MemoryRegionCache
*cache
);
2256 /* address_space_get_iotlb_entry: translate an address into an IOTLB
2257 * entry. Should be called from an RCU critical section.
2259 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
2260 bool is_write
, MemTxAttrs attrs
);
2262 /* address_space_translate: translate an address range into an address space
2263 * into a MemoryRegion and an address range into that section. Should be
2264 * called from an RCU critical section, to avoid that the last reference
2265 * to the returned region disappears after address_space_translate returns.
2267 * @fv: #FlatView to be accessed
2268 * @addr: address within that address space
2269 * @xlat: pointer to address within the returned memory region section's
2271 * @len: pointer to length
2272 * @is_write: indicates the transfer direction
2273 * @attrs: memory attributes
2275 MemoryRegion
*flatview_translate(FlatView
*fv
,
2276 hwaddr addr
, hwaddr
*xlat
,
2277 hwaddr
*len
, bool is_write
,
2280 static inline MemoryRegion
*address_space_translate(AddressSpace
*as
,
2281 hwaddr addr
, hwaddr
*xlat
,
2282 hwaddr
*len
, bool is_write
,
2285 return flatview_translate(address_space_to_flatview(as
),
2286 addr
, xlat
, len
, is_write
, attrs
);
2289 /* address_space_access_valid: check for validity of accessing an address
2292 * Check whether memory is assigned to the given address space range, and
2293 * access is permitted by any IOMMU regions that are active for the address
2296 * For now, addr and len should be aligned to a page size. This limitation
2297 * will be lifted in the future.
2299 * @as: #AddressSpace to be accessed
2300 * @addr: address within that address space
2301 * @len: length of the area to be checked
2302 * @is_write: indicates the transfer direction
2303 * @attrs: memory attributes
2305 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, hwaddr len
,
2306 bool is_write
, MemTxAttrs attrs
);
2308 /* address_space_map: map a physical memory region into a host virtual address
2310 * May map a subset of the requested range, given by and returned in @plen.
2311 * May return %NULL and set *@plen to zero(0), if resources needed to perform
2312 * the mapping are exhausted.
2313 * Use only for reads OR writes - not for read-modify-write operations.
2314 * Use cpu_register_map_client() to know when retrying the map operation is
2315 * likely to succeed.
2317 * @as: #AddressSpace to be accessed
2318 * @addr: address within that address space
2319 * @plen: pointer to length of buffer; updated on return
2320 * @is_write: indicates the transfer direction
2321 * @attrs: memory attributes
2323 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
2324 hwaddr
*plen
, bool is_write
, MemTxAttrs attrs
);
2326 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2328 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2329 * the amount of memory that was actually read or written by the caller.
2331 * @as: #AddressSpace used
2332 * @buffer: host pointer as returned by address_space_map()
2333 * @len: buffer length as returned by address_space_map()
2334 * @access_len: amount of data actually transferred
2335 * @is_write: indicates the transfer direction
2337 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
2338 bool is_write
, hwaddr access_len
);
2341 /* Internal functions, part of the implementation of address_space_read. */
2342 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
2343 MemTxAttrs attrs
, void *buf
, hwaddr len
);
2344 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
2345 MemTxAttrs attrs
, void *buf
,
2346 hwaddr len
, hwaddr addr1
, hwaddr l
,
2348 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
2350 /* Internal functions, part of the implementation of address_space_read_cached
2351 * and address_space_write_cached. */
2352 MemTxResult
address_space_read_cached_slow(MemoryRegionCache
*cache
,
2353 hwaddr addr
, void *buf
, hwaddr len
);
2354 MemTxResult
address_space_write_cached_slow(MemoryRegionCache
*cache
,
2355 hwaddr addr
, const void *buf
,
2358 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
2361 return memory_region_is_ram(mr
) && !mr
->readonly
&&
2362 !mr
->rom_device
&& !memory_region_is_ram_device(mr
);
2364 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
2365 memory_region_is_romd(mr
);
2370 * address_space_read: read from an address space.
2372 * Return a MemTxResult indicating whether the operation succeeded
2373 * or failed (eg unassigned memory, device rejected the transaction,
2374 * IOMMU fault). Called within RCU critical section.
2376 * @as: #AddressSpace to be accessed
2377 * @addr: address within that address space
2378 * @attrs: memory transaction attributes
2379 * @buf: buffer with the data transferred
2380 * @len: length of the data transferred
2382 static inline __attribute__((__always_inline__
))
2383 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
2384 MemTxAttrs attrs
, void *buf
,
2387 MemTxResult result
= MEMTX_OK
;
2393 if (__builtin_constant_p(len
)) {
2395 RCU_READ_LOCK_GUARD();
2396 fv
= address_space_to_flatview(as
);
2398 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
2399 if (len
== l
&& memory_access_is_direct(mr
, false)) {
2400 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
2401 memcpy(buf
, ptr
, len
);
2403 result
= flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
2408 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
2414 * address_space_read_cached: read from a cached RAM region
2416 * @cache: Cached region to be addressed
2417 * @addr: address relative to the base of the RAM region
2418 * @buf: buffer with the data transferred
2419 * @len: length of the data transferred
2421 static inline MemTxResult
2422 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2423 void *buf
, hwaddr len
)
2425 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2426 if (likely(cache
->ptr
)) {
2427 memcpy(buf
, cache
->ptr
+ addr
, len
);
2430 return address_space_read_cached_slow(cache
, addr
, buf
, len
);
2435 * address_space_write_cached: write to a cached RAM region
2437 * @cache: Cached region to be addressed
2438 * @addr: address relative to the base of the RAM region
2439 * @buf: buffer with the data transferred
2440 * @len: length of the data transferred
2442 static inline MemTxResult
2443 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2444 const void *buf
, hwaddr len
)
2446 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2447 if (likely(cache
->ptr
)) {
2448 memcpy(cache
->ptr
+ addr
, buf
, len
);
2451 return address_space_write_cached_slow(cache
, addr
, buf
, len
);
2456 /* enum device_endian to MemOp. */
2457 static inline MemOp
devend_memop(enum device_endian end
)
2459 QEMU_BUILD_BUG_ON(DEVICE_HOST_ENDIAN
!= DEVICE_LITTLE_ENDIAN
&&
2460 DEVICE_HOST_ENDIAN
!= DEVICE_BIG_ENDIAN
);
2462 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
2463 /* Swap if non-host endianness or native (target) endianness */
2464 return (end
== DEVICE_HOST_ENDIAN
) ? 0 : MO_BSWAP
;
2466 const int non_host_endianness
=
2467 DEVICE_LITTLE_ENDIAN
^ DEVICE_BIG_ENDIAN
^ DEVICE_HOST_ENDIAN
;
2469 /* In this case, native (target) endianness needs no swap. */
2470 return (end
== non_host_endianness
) ? MO_BSWAP
: 0;