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/ramlist.h"
23 #include "qemu/queue.h"
24 #include "qemu/int128.h"
25 #include "qemu/notify.h"
26 #include "qom/object.h"
28 #include "hw/qdev-core.h"
30 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
32 #define MAX_PHYS_ADDR_SPACE_BITS 62
33 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
35 #define TYPE_MEMORY_REGION "qemu:memory-region"
36 #define MEMORY_REGION(obj) \
37 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
39 #define TYPE_IOMMU_MEMORY_REGION "qemu:iommu-memory-region"
40 #define IOMMU_MEMORY_REGION(obj) \
41 OBJECT_CHECK(IOMMUMemoryRegion, (obj), TYPE_IOMMU_MEMORY_REGION)
42 #define IOMMU_MEMORY_REGION_CLASS(klass) \
43 OBJECT_CLASS_CHECK(IOMMUMemoryRegionClass, (klass), \
44 TYPE_IOMMU_MEMORY_REGION)
45 #define IOMMU_MEMORY_REGION_GET_CLASS(obj) \
46 OBJECT_GET_CLASS(IOMMUMemoryRegionClass, (obj), \
47 TYPE_IOMMU_MEMORY_REGION)
49 typedef struct MemoryRegionOps MemoryRegionOps
;
50 typedef struct MemoryRegionMmio MemoryRegionMmio
;
52 struct MemoryRegionMmio
{
53 CPUReadMemoryFunc
*read
[3];
54 CPUWriteMemoryFunc
*write
[3];
57 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
59 /* See address_space_translate: bit 0 is read, bit 1 is write. */
67 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
69 struct IOMMUTLBEntry
{
70 AddressSpace
*target_as
;
72 hwaddr translated_addr
;
73 hwaddr addr_mask
; /* 0xfff = 4k translation */
74 IOMMUAccessFlags perm
;
78 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
79 * register with one or multiple IOMMU Notifier capability bit(s).
82 IOMMU_NOTIFIER_NONE
= 0,
83 /* Notify cache invalidations */
84 IOMMU_NOTIFIER_UNMAP
= 0x1,
85 /* Notify entry changes (newly created entries) */
86 IOMMU_NOTIFIER_MAP
= 0x2,
89 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
92 typedef void (*IOMMUNotify
)(struct IOMMUNotifier
*notifier
,
95 struct IOMMUNotifier
{
97 IOMMUNotifierFlag notifier_flags
;
98 /* Notify for address space range start <= addr <= end */
102 QLIST_ENTRY(IOMMUNotifier
) node
;
104 typedef struct IOMMUNotifier IOMMUNotifier
;
106 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
107 #define RAM_PREALLOC (1 << 0)
109 /* RAM is mmap-ed with MAP_SHARED */
110 #define RAM_SHARED (1 << 1)
112 /* Only a portion of RAM (used_length) is actually used, and migrated.
113 * This used_length size can change across reboots.
115 #define RAM_RESIZEABLE (1 << 2)
117 /* UFFDIO_ZEROPAGE is available on this RAMBlock to atomically
118 * zero the page and wake waiting processes.
119 * (Set during postcopy)
121 #define RAM_UF_ZEROPAGE (1 << 3)
123 /* RAM can be migrated */
124 #define RAM_MIGRATABLE (1 << 4)
126 /* RAM is a persistent kind memory */
127 #define RAM_PMEM (1 << 5)
129 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
130 IOMMUNotifierFlag flags
,
131 hwaddr start
, hwaddr end
,
135 n
->notifier_flags
= flags
;
138 n
->iommu_idx
= iommu_idx
;
142 * Memory region callbacks
144 struct MemoryRegionOps
{
145 /* Read from the memory region. @addr is relative to @mr; @size is
147 uint64_t (*read
)(void *opaque
,
150 /* Write to the memory region. @addr is relative to @mr; @size is
152 void (*write
)(void *opaque
,
157 MemTxResult (*read_with_attrs
)(void *opaque
,
162 MemTxResult (*write_with_attrs
)(void *opaque
,
168 enum device_endian endianness
;
169 /* Guest-visible constraints: */
171 /* If nonzero, specify bounds on access sizes beyond which a machine
174 unsigned min_access_size
;
175 unsigned max_access_size
;
176 /* If true, unaligned accesses are supported. Otherwise unaligned
177 * accesses throw machine checks.
181 * If present, and returns #false, the transaction is not accepted
182 * by the device (and results in machine dependent behaviour such
183 * as a machine check exception).
185 bool (*accepts
)(void *opaque
, hwaddr addr
,
186 unsigned size
, bool is_write
,
189 /* Internal implementation constraints: */
191 /* If nonzero, specifies the minimum size implemented. Smaller sizes
192 * will be rounded upwards and a partial result will be returned.
194 unsigned min_access_size
;
195 /* If nonzero, specifies the maximum size implemented. Larger sizes
196 * will be done as a series of accesses with smaller sizes.
198 unsigned max_access_size
;
199 /* If true, unaligned accesses are supported. Otherwise all accesses
200 * are converted to (possibly multiple) naturally aligned accesses.
205 /* If .read and .write are not present, old_mmio may be used for
206 * backwards compatibility with old mmio registration
208 const MemoryRegionMmio old_mmio
;
211 enum IOMMUMemoryRegionAttr
{
212 IOMMU_ATTR_SPAPR_TCE_FD
216 * IOMMUMemoryRegionClass:
218 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
219 * and provide an implementation of at least the @translate method here
220 * to handle requests to the memory region. Other methods are optional.
222 * The IOMMU implementation must use the IOMMU notifier infrastructure
223 * to report whenever mappings are changed, by calling
224 * memory_region_notify_iommu() (or, if necessary, by calling
225 * memory_region_notify_one() for each registered notifier).
227 * Conceptually an IOMMU provides a mapping from input address
228 * to an output TLB entry. If the IOMMU is aware of memory transaction
229 * attributes and the output TLB entry depends on the transaction
230 * attributes, we represent this using IOMMU indexes. Each index
231 * selects a particular translation table that the IOMMU has:
232 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
233 * @translate takes an input address and an IOMMU index
234 * and the mapping returned can only depend on the input address and the
237 * Most IOMMUs don't care about the transaction attributes and support
238 * only a single IOMMU index. A more complex IOMMU might have one index
239 * for secure transactions and one for non-secure transactions.
241 typedef struct IOMMUMemoryRegionClass
{
243 struct DeviceClass parent_class
;
246 * Return a TLB entry that contains a given address.
248 * The IOMMUAccessFlags indicated via @flag are optional and may
249 * be specified as IOMMU_NONE to indicate that the caller needs
250 * the full translation information for both reads and writes. If
251 * the access flags are specified then the IOMMU implementation
252 * may use this as an optimization, to stop doing a page table
253 * walk as soon as it knows that the requested permissions are not
254 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
255 * full page table walk and report the permissions in the returned
256 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
257 * return different mappings for reads and writes.)
259 * The returned information remains valid while the caller is
260 * holding the big QEMU lock or is inside an RCU critical section;
261 * if the caller wishes to cache the mapping beyond that it must
262 * register an IOMMU notifier so it can invalidate its cached
263 * information when the IOMMU mapping changes.
265 * @iommu: the IOMMUMemoryRegion
266 * @hwaddr: address to be translated within the memory region
267 * @flag: requested access permissions
268 * @iommu_idx: IOMMU index for the translation
270 IOMMUTLBEntry (*translate
)(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
271 IOMMUAccessFlags flag
, int iommu_idx
);
272 /* Returns minimum supported page size in bytes.
273 * If this method is not provided then the minimum is assumed to
274 * be TARGET_PAGE_SIZE.
276 * @iommu: the IOMMUMemoryRegion
278 uint64_t (*get_min_page_size
)(IOMMUMemoryRegion
*iommu
);
279 /* Called when IOMMU Notifier flag changes (ie when the set of
280 * events which IOMMU users are requesting notification for changes).
281 * Optional method -- need not be provided if the IOMMU does not
282 * need to know exactly which events must be notified.
284 * @iommu: the IOMMUMemoryRegion
285 * @old_flags: events which previously needed to be notified
286 * @new_flags: events which now need to be notified
288 void (*notify_flag_changed
)(IOMMUMemoryRegion
*iommu
,
289 IOMMUNotifierFlag old_flags
,
290 IOMMUNotifierFlag new_flags
);
291 /* Called to handle memory_region_iommu_replay().
293 * The default implementation of memory_region_iommu_replay() is to
294 * call the IOMMU translate method for every page in the address space
295 * with flag == IOMMU_NONE and then call the notifier if translate
296 * returns a valid mapping. If this method is implemented then it
297 * overrides the default behaviour, and must provide the full semantics
298 * of memory_region_iommu_replay(), by calling @notifier for every
299 * translation present in the IOMMU.
301 * Optional method -- an IOMMU only needs to provide this method
302 * if the default is inefficient or produces undesirable side effects.
304 * Note: this is not related to record-and-replay functionality.
306 void (*replay
)(IOMMUMemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
308 /* Get IOMMU misc attributes. This is an optional method that
309 * can be used to allow users of the IOMMU to get implementation-specific
310 * information. The IOMMU implements this method to handle calls
311 * by IOMMU users to memory_region_iommu_get_attr() by filling in
312 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
313 * the IOMMU supports. If the method is unimplemented then
314 * memory_region_iommu_get_attr() will always return -EINVAL.
316 * @iommu: the IOMMUMemoryRegion
317 * @attr: attribute being queried
318 * @data: memory to fill in with the attribute data
320 * Returns 0 on success, or a negative errno; in particular
321 * returns -EINVAL for unrecognized or unimplemented attribute types.
323 int (*get_attr
)(IOMMUMemoryRegion
*iommu
, enum IOMMUMemoryRegionAttr attr
,
326 /* Return the IOMMU index to use for a given set of transaction attributes.
328 * Optional method: if an IOMMU only supports a single IOMMU index then
329 * the default implementation of memory_region_iommu_attrs_to_index()
332 * The indexes supported by an IOMMU must be contiguous, starting at 0.
334 * @iommu: the IOMMUMemoryRegion
335 * @attrs: memory transaction attributes
337 int (*attrs_to_index
)(IOMMUMemoryRegion
*iommu
, MemTxAttrs attrs
);
339 /* Return the number of IOMMU indexes this IOMMU supports.
341 * Optional method: if this method is not provided, then
342 * memory_region_iommu_num_indexes() will return 1, indicating that
343 * only a single IOMMU index is supported.
345 * @iommu: the IOMMUMemoryRegion
347 int (*num_indexes
)(IOMMUMemoryRegion
*iommu
);
348 } IOMMUMemoryRegionClass
;
350 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
351 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
353 struct MemoryRegion
{
356 /* All fields are private - violators will be prosecuted */
358 /* The following fields should fit in a cache line */
362 bool readonly
; /* For RAM regions */
364 bool flush_coalesced_mmio
;
366 uint8_t dirty_log_mask
;
371 const MemoryRegionOps
*ops
;
373 MemoryRegion
*container
;
376 void (*destructor
)(MemoryRegion
*mr
);
381 bool warning_printed
; /* For reservations */
382 uint8_t vga_logging_count
;
386 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
387 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
388 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
390 unsigned ioeventfd_nb
;
391 MemoryRegionIoeventfd
*ioeventfds
;
394 struct IOMMUMemoryRegion
{
395 MemoryRegion parent_obj
;
397 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
398 IOMMUNotifierFlag iommu_notify_flags
;
401 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
402 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
405 * MemoryListener: callbacks structure for updates to the physical memory map
407 * Allows a component to adjust to changes in the guest-visible memory map.
408 * Use with memory_listener_register() and memory_listener_unregister().
410 struct MemoryListener
{
411 void (*begin
)(MemoryListener
*listener
);
412 void (*commit
)(MemoryListener
*listener
);
413 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
414 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
415 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
416 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
418 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
420 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
421 void (*log_global_start
)(MemoryListener
*listener
);
422 void (*log_global_stop
)(MemoryListener
*listener
);
423 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
424 bool match_data
, uint64_t data
, EventNotifier
*e
);
425 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
426 bool match_data
, uint64_t data
, EventNotifier
*e
);
427 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
428 hwaddr addr
, hwaddr len
);
429 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
430 hwaddr addr
, hwaddr len
);
431 /* Lower = earlier (during add), later (during del) */
433 AddressSpace
*address_space
;
434 QTAILQ_ENTRY(MemoryListener
) link
;
435 QTAILQ_ENTRY(MemoryListener
) link_as
;
439 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
441 struct AddressSpace
{
442 /* All fields are private. */
447 /* Accessed via RCU. */
448 struct FlatView
*current_map
;
451 struct MemoryRegionIoeventfd
*ioeventfds
;
452 QTAILQ_HEAD(memory_listeners_as
, MemoryListener
) listeners
;
453 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
456 typedef struct AddressSpaceDispatch AddressSpaceDispatch
;
457 typedef struct FlatRange FlatRange
;
459 /* Flattened global view of current active memory hierarchy. Kept in sorted
467 unsigned nr_allocated
;
468 struct AddressSpaceDispatch
*dispatch
;
472 static inline FlatView
*address_space_to_flatview(AddressSpace
*as
)
474 return atomic_rcu_read(&as
->current_map
);
479 * MemoryRegionSection: describes a fragment of a #MemoryRegion
481 * @mr: the region, or %NULL if empty
482 * @fv: the flat view of the address space the region is mapped in
483 * @offset_within_region: the beginning of the section, relative to @mr's start
484 * @size: the size of the section; will not exceed @mr's boundaries
485 * @offset_within_address_space: the address of the first byte of the section
486 * relative to the region's address space
487 * @readonly: writes to this section are ignored
489 struct MemoryRegionSection
{
492 hwaddr offset_within_region
;
494 hwaddr offset_within_address_space
;
499 * memory_region_init: Initialize a memory region
501 * The region typically acts as a container for other memory regions. Use
502 * memory_region_add_subregion() to add subregions.
504 * @mr: the #MemoryRegion to be initialized
505 * @owner: the object that tracks the region's reference count
506 * @name: used for debugging; not visible to the user or ABI
507 * @size: size of the region; any subregions beyond this size will be clipped
509 void memory_region_init(MemoryRegion
*mr
,
510 struct Object
*owner
,
515 * memory_region_ref: Add 1 to a memory region's reference count
517 * Whenever memory regions are accessed outside the BQL, they need to be
518 * preserved against hot-unplug. MemoryRegions actually do not have their
519 * own reference count; they piggyback on a QOM object, their "owner".
520 * This function adds a reference to the owner.
522 * All MemoryRegions must have an owner if they can disappear, even if the
523 * device they belong to operates exclusively under the BQL. This is because
524 * the region could be returned at any time by memory_region_find, and this
525 * is usually under guest control.
527 * @mr: the #MemoryRegion
529 void memory_region_ref(MemoryRegion
*mr
);
532 * memory_region_unref: Remove 1 to a memory region's reference count
534 * Whenever memory regions are accessed outside the BQL, they need to be
535 * preserved against hot-unplug. MemoryRegions actually do not have their
536 * own reference count; they piggyback on a QOM object, their "owner".
537 * This function removes a reference to the owner and possibly destroys it.
539 * @mr: the #MemoryRegion
541 void memory_region_unref(MemoryRegion
*mr
);
544 * memory_region_init_io: Initialize an I/O memory region.
546 * Accesses into the region will cause the callbacks in @ops to be called.
547 * if @size is nonzero, subregions will be clipped to @size.
549 * @mr: the #MemoryRegion to be initialized.
550 * @owner: the object that tracks the region's reference count
551 * @ops: a structure containing read and write callbacks to be used when
552 * I/O is performed on the region.
553 * @opaque: passed to the read and write callbacks of the @ops structure.
554 * @name: used for debugging; not visible to the user or ABI
555 * @size: size of the region.
557 void memory_region_init_io(MemoryRegion
*mr
,
558 struct Object
*owner
,
559 const MemoryRegionOps
*ops
,
565 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
566 * into the region will modify memory
569 * @mr: the #MemoryRegion to be initialized.
570 * @owner: the object that tracks the region's reference count
571 * @name: Region name, becomes part of RAMBlock name used in migration stream
572 * must be unique within any device
573 * @size: size of the region.
574 * @errp: pointer to Error*, to store an error if it happens.
576 * Note that this function does not do anything to cause the data in the
577 * RAM memory region to be migrated; that is the responsibility of the caller.
579 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
580 struct Object
*owner
,
586 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
587 * Accesses into the region will
588 * modify memory directly.
590 * @mr: the #MemoryRegion to be initialized.
591 * @owner: the object that tracks the region's reference count
592 * @name: Region name, becomes part of RAMBlock name used in migration stream
593 * must be unique within any device
594 * @size: size of the region.
595 * @share: allow remapping RAM to different addresses
596 * @errp: pointer to Error*, to store an error if it happens.
598 * Note that this function is similar to memory_region_init_ram_nomigrate.
599 * The only difference is part of the RAM region can be remapped.
601 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
602 struct Object
*owner
,
609 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
610 * RAM. Accesses into the region will
611 * modify memory directly. Only an initial
612 * portion of this RAM is actually used.
613 * The used size can change across reboots.
615 * @mr: the #MemoryRegion to be initialized.
616 * @owner: the object that tracks the region's reference count
617 * @name: Region name, becomes part of RAMBlock name used in migration stream
618 * must be unique within any device
619 * @size: used size of the region.
620 * @max_size: max size of the region.
621 * @resized: callback to notify owner about used size change.
622 * @errp: pointer to Error*, to store an error if it happens.
624 * Note that this function does not do anything to cause the data in the
625 * RAM memory region to be migrated; that is the responsibility of the caller.
627 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
628 struct Object
*owner
,
632 void (*resized
)(const char*,
639 * memory_region_init_ram_from_file: Initialize RAM memory region with a
642 * @mr: the #MemoryRegion to be initialized.
643 * @owner: the object that tracks the region's reference count
644 * @name: Region name, becomes part of RAMBlock name used in migration stream
645 * must be unique within any device
646 * @size: size of the region.
647 * @align: alignment of the region base address; if 0, the default alignment
648 * (getpagesize()) will be used.
649 * @ram_flags: Memory region features:
650 * - RAM_SHARED: memory must be mmaped with the MAP_SHARED flag
651 * - RAM_PMEM: the memory is persistent memory
652 * Other bits are ignored now.
653 * @path: the path in which to allocate the RAM.
654 * @errp: pointer to Error*, to store an error if it happens.
656 * Note that this function does not do anything to cause the data in the
657 * RAM memory region to be migrated; that is the responsibility of the caller.
659 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
660 struct Object
*owner
,
669 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
672 * @mr: the #MemoryRegion to be initialized.
673 * @owner: the object that tracks the region's reference count
674 * @name: the name of the region.
675 * @size: size of the region.
676 * @share: %true if memory must be mmaped with the MAP_SHARED flag
677 * @fd: the fd to mmap.
678 * @errp: pointer to Error*, to store an error if it happens.
680 * Note that this function does not do anything to cause the data in the
681 * RAM memory region to be migrated; that is the responsibility of the caller.
683 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
684 struct Object
*owner
,
693 * memory_region_init_ram_ptr: Initialize RAM memory region from a
694 * user-provided pointer. Accesses into the
695 * region will modify memory directly.
697 * @mr: the #MemoryRegion to be initialized.
698 * @owner: the object that tracks the region's reference count
699 * @name: Region name, becomes part of RAMBlock name used in migration stream
700 * must be unique within any device
701 * @size: size of the region.
702 * @ptr: memory to be mapped; must contain at least @size bytes.
704 * Note that this function does not do anything to cause the data in the
705 * RAM memory region to be migrated; that is the responsibility of the caller.
707 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
708 struct Object
*owner
,
714 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
715 * a user-provided pointer.
717 * A RAM device represents a mapping to a physical device, such as to a PCI
718 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
719 * into the VM address space and access to the region will modify memory
720 * directly. However, the memory region should not be included in a memory
721 * dump (device may not be enabled/mapped at the time of the dump), and
722 * operations incompatible with manipulating MMIO should be avoided. Replaces
725 * @mr: the #MemoryRegion to be initialized.
726 * @owner: the object that tracks the region's reference count
727 * @name: the name of the region.
728 * @size: size of the region.
729 * @ptr: memory to be mapped; must contain at least @size bytes.
731 * Note that this function does not do anything to cause the data in the
732 * RAM memory region to be migrated; that is the responsibility of the caller.
733 * (For RAM device memory regions, migrating the contents rarely makes sense.)
735 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
736 struct Object
*owner
,
742 * memory_region_init_alias: Initialize a memory region that aliases all or a
743 * part of another memory region.
745 * @mr: the #MemoryRegion to be initialized.
746 * @owner: the object that tracks the region's reference count
747 * @name: used for debugging; not visible to the user or ABI
748 * @orig: the region to be referenced; @mr will be equivalent to
749 * @orig between @offset and @offset + @size - 1.
750 * @offset: start of the section in @orig to be referenced.
751 * @size: size of the region.
753 void memory_region_init_alias(MemoryRegion
*mr
,
754 struct Object
*owner
,
761 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
763 * This has the same effect as calling memory_region_init_ram_nomigrate()
764 * and then marking the resulting region read-only with
765 * memory_region_set_readonly().
767 * Note that this function does not do anything to cause the data in the
768 * RAM side of the memory region to be migrated; that is the responsibility
771 * @mr: the #MemoryRegion to be initialized.
772 * @owner: the object that tracks the region's reference count
773 * @name: Region name, becomes part of RAMBlock name used in migration stream
774 * must be unique within any device
775 * @size: size of the region.
776 * @errp: pointer to Error*, to store an error if it happens.
778 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
779 struct Object
*owner
,
785 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
786 * Writes are handled via callbacks.
788 * Note that this function does not do anything to cause the data in the
789 * RAM side of the memory region to be migrated; that is the responsibility
792 * @mr: the #MemoryRegion to be initialized.
793 * @owner: the object that tracks the region's reference count
794 * @ops: callbacks for write access handling (must not be NULL).
795 * @opaque: passed to the read and write callbacks of the @ops structure.
796 * @name: Region name, becomes part of RAMBlock name used in migration stream
797 * must be unique within any device
798 * @size: size of the region.
799 * @errp: pointer to Error*, to store an error if it happens.
801 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
802 struct Object
*owner
,
803 const MemoryRegionOps
*ops
,
810 * memory_region_init_iommu: Initialize a memory region of a custom type
811 * that translates addresses
813 * An IOMMU region translates addresses and forwards accesses to a target
816 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
817 * @_iommu_mr should be a pointer to enough memory for an instance of
818 * that subclass, @instance_size is the size of that subclass, and
819 * @mrtypename is its name. This function will initialize @_iommu_mr as an
820 * instance of the subclass, and its methods will then be called to handle
821 * accesses to the memory region. See the documentation of
822 * #IOMMUMemoryRegionClass for further details.
824 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
825 * @instance_size: the IOMMUMemoryRegion subclass instance size
826 * @mrtypename: the type name of the #IOMMUMemoryRegion
827 * @owner: the object that tracks the region's reference count
828 * @name: used for debugging; not visible to the user or ABI
829 * @size: size of the region.
831 void memory_region_init_iommu(void *_iommu_mr
,
832 size_t instance_size
,
833 const char *mrtypename
,
839 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
840 * region will modify memory directly.
842 * @mr: the #MemoryRegion to be initialized
843 * @owner: the object that tracks the region's reference count (must be
844 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
845 * @name: name of the memory region
846 * @size: size of the region in bytes
847 * @errp: pointer to Error*, to store an error if it happens.
849 * This function allocates RAM for a board model or device, and
850 * arranges for it to be migrated (by calling vmstate_register_ram()
851 * if @owner is a DeviceState, or vmstate_register_ram_global() if
854 * TODO: Currently we restrict @owner to being either NULL (for
855 * global RAM regions with no owner) or devices, so that we can
856 * give the RAM block a unique name for migration purposes.
857 * We should lift this restriction and allow arbitrary Objects.
858 * If you pass a non-NULL non-device @owner then we will assert.
860 void memory_region_init_ram(MemoryRegion
*mr
,
861 struct Object
*owner
,
867 * memory_region_init_rom: Initialize a ROM memory region.
869 * This has the same effect as calling memory_region_init_ram()
870 * and then marking the resulting region read-only with
871 * memory_region_set_readonly(). This includes arranging for the
872 * contents to be migrated.
874 * TODO: Currently we restrict @owner to being either NULL (for
875 * global RAM regions with no owner) or devices, so that we can
876 * give the RAM block a unique name for migration purposes.
877 * We should lift this restriction and allow arbitrary Objects.
878 * If you pass a non-NULL non-device @owner then we will assert.
880 * @mr: the #MemoryRegion to be initialized.
881 * @owner: the object that tracks the region's reference count
882 * @name: Region name, becomes part of RAMBlock name used in migration stream
883 * must be unique within any device
884 * @size: size of the region.
885 * @errp: pointer to Error*, to store an error if it happens.
887 void memory_region_init_rom(MemoryRegion
*mr
,
888 struct Object
*owner
,
894 * memory_region_init_rom_device: Initialize a ROM memory region.
895 * Writes are handled via callbacks.
897 * This function initializes a memory region backed by RAM for reads
898 * and callbacks for writes, and arranges for the RAM backing to
899 * be migrated (by calling vmstate_register_ram()
900 * if @owner is a DeviceState, or vmstate_register_ram_global() if
903 * TODO: Currently we restrict @owner to being either NULL (for
904 * global RAM regions with no owner) or devices, so that we can
905 * give the RAM block a unique name for migration purposes.
906 * We should lift this restriction and allow arbitrary Objects.
907 * If you pass a non-NULL non-device @owner then we will assert.
909 * @mr: the #MemoryRegion to be initialized.
910 * @owner: the object that tracks the region's reference count
911 * @ops: callbacks for write access handling (must not be NULL).
912 * @name: Region name, becomes part of RAMBlock name used in migration stream
913 * must be unique within any device
914 * @size: size of the region.
915 * @errp: pointer to Error*, to store an error if it happens.
917 void memory_region_init_rom_device(MemoryRegion
*mr
,
918 struct Object
*owner
,
919 const MemoryRegionOps
*ops
,
927 * memory_region_owner: get a memory region's owner.
929 * @mr: the memory region being queried.
931 struct Object
*memory_region_owner(MemoryRegion
*mr
);
934 * memory_region_size: get a memory region's size.
936 * @mr: the memory region being queried.
938 uint64_t memory_region_size(MemoryRegion
*mr
);
941 * memory_region_is_ram: check whether a memory region is random access
943 * Returns %true is a memory region is random access.
945 * @mr: the memory region being queried
947 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
953 * memory_region_is_ram_device: check whether a memory region is a ram device
955 * Returns %true is a memory region is a device backed ram region
957 * @mr: the memory region being queried
959 bool memory_region_is_ram_device(MemoryRegion
*mr
);
962 * memory_region_is_romd: check whether a memory region is in ROMD mode
964 * Returns %true if a memory region is a ROM device and currently set to allow
967 * @mr: the memory region being queried
969 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
971 return mr
->rom_device
&& mr
->romd_mode
;
975 * memory_region_get_iommu: check whether a memory region is an iommu
977 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
980 * @mr: the memory region being queried
982 static inline IOMMUMemoryRegion
*memory_region_get_iommu(MemoryRegion
*mr
)
985 return memory_region_get_iommu(mr
->alias
);
988 return (IOMMUMemoryRegion
*) mr
;
994 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
995 * if an iommu or NULL if not
997 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
998 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1000 * @mr: the memory region being queried
1002 static inline IOMMUMemoryRegionClass
*memory_region_get_iommu_class_nocheck(
1003 IOMMUMemoryRegion
*iommu_mr
)
1005 return (IOMMUMemoryRegionClass
*) (((Object
*)iommu_mr
)->class);
1008 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1011 * memory_region_iommu_get_min_page_size: get minimum supported page size
1014 * Returns minimum supported page size for an iommu.
1016 * @iommu_mr: the memory region being queried
1018 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
);
1021 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1023 * The notification type will be decided by entry.perm bits:
1025 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
1026 * - For MAP (newly added entry) notifies: set entry.perm to the
1027 * permission of the page (which is definitely !IOMMU_NONE).
1029 * Note: for any IOMMU implementation, an in-place mapping change
1030 * should be notified with an UNMAP followed by a MAP.
1032 * @iommu_mr: the memory region that was changed
1033 * @iommu_idx: the IOMMU index for the translation table which has changed
1034 * @entry: the new entry in the IOMMU translation table. The entry
1035 * replaces all old entries for the same virtual I/O address range.
1036 * Deleted entries have .@perm == 0.
1038 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1040 IOMMUTLBEntry entry
);
1043 * memory_region_notify_one: notify a change in an IOMMU translation
1044 * entry to a single notifier
1046 * This works just like memory_region_notify_iommu(), but it only
1047 * notifies a specific notifier, not all of them.
1049 * @notifier: the notifier to be notified
1050 * @entry: the new entry in the IOMMU translation table. The entry
1051 * replaces all old entries for the same virtual I/O address range.
1052 * Deleted entries have .@perm == 0.
1054 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1055 IOMMUTLBEntry
*entry
);
1058 * memory_region_register_iommu_notifier: register a notifier for changes to
1059 * IOMMU translation entries.
1061 * @mr: the memory region to observe
1062 * @n: the IOMMUNotifier to be added; the notify callback receives a
1063 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1064 * ceases to be valid on exit from the notifier.
1066 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1070 * memory_region_iommu_replay: replay existing IOMMU translations to
1071 * a notifier with the minimum page granularity returned by
1072 * mr->iommu_ops->get_page_size().
1074 * Note: this is not related to record-and-replay functionality.
1076 * @iommu_mr: the memory region to observe
1077 * @n: the notifier to which to replay iommu mappings
1079 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
);
1082 * memory_region_iommu_replay_all: replay existing IOMMU translations
1083 * to all the notifiers registered.
1085 * Note: this is not related to record-and-replay functionality.
1087 * @iommu_mr: the memory region to observe
1089 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
);
1092 * memory_region_unregister_iommu_notifier: unregister a notifier for
1093 * changes to IOMMU translation entries.
1095 * @mr: the memory region which was observed and for which notity_stopped()
1096 * needs to be called
1097 * @n: the notifier to be removed.
1099 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1103 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1104 * defined on the IOMMU.
1106 * Returns 0 on success, or a negative errno otherwise. In particular,
1107 * -EINVAL indicates that the IOMMU does not support the requested
1110 * @iommu_mr: the memory region
1111 * @attr: the requested attribute
1112 * @data: a pointer to the requested attribute data
1114 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1115 enum IOMMUMemoryRegionAttr attr
,
1119 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1120 * use for translations with the given memory transaction attributes.
1122 * @iommu_mr: the memory region
1123 * @attrs: the memory transaction attributes
1125 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1129 * memory_region_iommu_num_indexes: return the total number of IOMMU
1130 * indexes that this IOMMU supports.
1132 * @iommu_mr: the memory region
1134 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
);
1137 * memory_region_name: get a memory region's name
1139 * Returns the string that was used to initialize the memory region.
1141 * @mr: the memory region being queried
1143 const char *memory_region_name(const MemoryRegion
*mr
);
1146 * memory_region_is_logging: return whether a memory region is logging writes
1148 * Returns %true if the memory region is logging writes for the given client
1150 * @mr: the memory region being queried
1151 * @client: the client being queried
1153 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
1156 * memory_region_get_dirty_log_mask: return the clients for which a
1157 * memory region is logging writes.
1159 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1160 * are the bit indices.
1162 * @mr: the memory region being queried
1164 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
1167 * memory_region_is_rom: check whether a memory region is ROM
1169 * Returns %true is a memory region is read-only memory.
1171 * @mr: the memory region being queried
1173 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
1175 return mr
->ram
&& mr
->readonly
;
1180 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1182 * Returns a file descriptor backing a file-based RAM memory region,
1183 * or -1 if the region is not a file-based RAM memory region.
1185 * @mr: the RAM or alias memory region being queried.
1187 int memory_region_get_fd(MemoryRegion
*mr
);
1190 * memory_region_from_host: Convert a pointer into a RAM memory region
1191 * and an offset within it.
1193 * Given a host pointer inside a RAM memory region (created with
1194 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1195 * the MemoryRegion and the offset within it.
1197 * Use with care; by the time this function returns, the returned pointer is
1198 * not protected by RCU anymore. If the caller is not within an RCU critical
1199 * section and does not hold the iothread lock, it must have other means of
1200 * protecting the pointer, such as a reference to the region that includes
1201 * the incoming ram_addr_t.
1203 * @ptr: the host pointer to be converted
1204 * @offset: the offset within memory region
1206 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
1209 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1211 * Returns a host pointer to a RAM memory region (created with
1212 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1214 * Use with care; by the time this function returns, the returned pointer is
1215 * not protected by RCU anymore. If the caller is not within an RCU critical
1216 * section and does not hold the iothread lock, it must have other means of
1217 * protecting the pointer, such as a reference to the region that includes
1218 * the incoming ram_addr_t.
1220 * @mr: the memory region being queried.
1222 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
1224 /* memory_region_ram_resize: Resize a RAM region.
1226 * Only legal before guest might have detected the memory size: e.g. on
1227 * incoming migration, or right after reset.
1229 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1230 * @newsize: the new size the region
1231 * @errp: pointer to Error*, to store an error if it happens.
1233 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
1237 * memory_region_set_log: Turn dirty logging on or off for a region.
1239 * Turns dirty logging on or off for a specified client (display, migration).
1240 * Only meaningful for RAM regions.
1242 * @mr: the memory region being updated.
1243 * @log: whether dirty logging is to be enabled or disabled.
1244 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1246 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
1249 * memory_region_get_dirty: Check whether a range of bytes is dirty
1250 * for a specified client.
1252 * Checks whether a range of bytes has been written to since the last
1253 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1256 * @mr: the memory region being queried.
1257 * @addr: the address (relative to the start of the region) being queried.
1258 * @size: the size of the range being queried.
1259 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1260 * %DIRTY_MEMORY_VGA.
1262 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1263 hwaddr size
, unsigned client
);
1266 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1268 * Marks a range of bytes as dirty, after it has been dirtied outside
1271 * @mr: the memory region being dirtied.
1272 * @addr: the address (relative to the start of the region) being dirtied.
1273 * @size: size of the range being dirtied.
1275 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1279 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1280 * bitmap and clear it.
1282 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1283 * returns the snapshot. The snapshot can then be used to query dirty
1284 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1285 * querying the same page multiple times, which is especially useful for
1286 * display updates where the scanlines often are not page aligned.
1288 * The dirty bitmap region which gets copyed into the snapshot (and
1289 * cleared afterwards) can be larger than requested. The boundaries
1290 * are rounded up/down so complete bitmap longs (covering 64 pages on
1291 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1292 * isn't a problem for display updates as the extra pages are outside
1293 * the visible area, and in case the visible area changes a full
1294 * display redraw is due anyway. Should other use cases for this
1295 * function emerge we might have to revisit this implementation
1298 * Use g_free to release DirtyBitmapSnapshot.
1300 * @mr: the memory region being queried.
1301 * @addr: the address (relative to the start of the region) being queried.
1302 * @size: the size of the range being queried.
1303 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1305 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1311 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1312 * in the specified dirty bitmap snapshot.
1314 * @mr: the memory region being queried.
1315 * @snap: the dirty bitmap snapshot
1316 * @addr: the address (relative to the start of the region) being queried.
1317 * @size: the size of the range being queried.
1319 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
1320 DirtyBitmapSnapshot
*snap
,
1321 hwaddr addr
, hwaddr size
);
1324 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1327 * Marks a range of pages as no longer dirty.
1329 * @mr: the region being updated.
1330 * @addr: the start of the subrange being cleaned.
1331 * @size: the size of the subrange being cleaned.
1332 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1333 * %DIRTY_MEMORY_VGA.
1335 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1336 hwaddr size
, unsigned client
);
1339 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1341 * Allows a memory region to be marked as read-only (turning it into a ROM).
1342 * only useful on RAM regions.
1344 * @mr: the region being updated.
1345 * @readonly: whether rhe region is to be ROM or RAM.
1347 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
1350 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1352 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1353 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1354 * device is mapped to guest memory and satisfies read access directly.
1355 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1356 * Writes are always handled by the #MemoryRegion.write function.
1358 * @mr: the memory region to be updated
1359 * @romd_mode: %true to put the region into ROMD mode
1361 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
1364 * memory_region_set_coalescing: Enable memory coalescing for the region.
1366 * Enabled writes to a region to be queued for later processing. MMIO ->write
1367 * callbacks may be delayed until a non-coalesced MMIO is issued.
1368 * Only useful for IO regions. Roughly similar to write-combining hardware.
1370 * @mr: the memory region to be write coalesced
1372 void memory_region_set_coalescing(MemoryRegion
*mr
);
1375 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1378 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1379 * Multiple calls can be issued coalesced disjoint ranges.
1381 * @mr: the memory region to be updated.
1382 * @offset: the start of the range within the region to be coalesced.
1383 * @size: the size of the subrange to be coalesced.
1385 void memory_region_add_coalescing(MemoryRegion
*mr
,
1390 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1392 * Disables any coalescing caused by memory_region_set_coalescing() or
1393 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1396 * @mr: the memory region to be updated.
1398 void memory_region_clear_coalescing(MemoryRegion
*mr
);
1401 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1404 * Ensure that pending coalesced MMIO request are flushed before the memory
1405 * region is accessed. This property is automatically enabled for all regions
1406 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1408 * @mr: the memory region to be updated.
1410 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
1413 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1416 * Clear the automatic coalesced MMIO flushing enabled via
1417 * memory_region_set_flush_coalesced. Note that this service has no effect on
1418 * memory regions that have MMIO coalescing enabled for themselves. For them,
1419 * automatic flushing will stop once coalescing is disabled.
1421 * @mr: the memory region to be updated.
1423 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1426 * memory_region_clear_global_locking: Declares that access processing does
1427 * not depend on the QEMU global lock.
1429 * By clearing this property, accesses to the memory region will be processed
1430 * outside of QEMU's global lock (unless the lock is held on when issuing the
1431 * access request). In this case, the device model implementing the access
1432 * handlers is responsible for synchronization of concurrency.
1434 * @mr: the memory region to be updated.
1436 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1439 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1440 * is written to a location.
1442 * Marks a word in an IO region (initialized with memory_region_init_io())
1443 * as a trigger for an eventfd event. The I/O callback will not be called.
1444 * The caller must be prepared to handle failure (that is, take the required
1445 * action if the callback _is_ called).
1447 * @mr: the memory region being updated.
1448 * @addr: the address within @mr that is to be monitored
1449 * @size: the size of the access to trigger the eventfd
1450 * @match_data: whether to match against @data, instead of just @addr
1451 * @data: the data to match against the guest write
1452 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1454 void memory_region_add_eventfd(MemoryRegion
*mr
,
1462 * memory_region_del_eventfd: Cancel an eventfd.
1464 * Cancels an eventfd trigger requested by a previous
1465 * memory_region_add_eventfd() call.
1467 * @mr: the memory region being updated.
1468 * @addr: the address within @mr that is to be monitored
1469 * @size: the size of the access to trigger the eventfd
1470 * @match_data: whether to match against @data, instead of just @addr
1471 * @data: the data to match against the guest write
1472 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1474 void memory_region_del_eventfd(MemoryRegion
*mr
,
1482 * memory_region_add_subregion: Add a subregion to a container.
1484 * Adds a subregion at @offset. The subregion may not overlap with other
1485 * subregions (except for those explicitly marked as overlapping). A region
1486 * may only be added once as a subregion (unless removed with
1487 * memory_region_del_subregion()); use memory_region_init_alias() if you
1488 * want a region to be a subregion in multiple locations.
1490 * @mr: the region to contain the new subregion; must be a container
1491 * initialized with memory_region_init().
1492 * @offset: the offset relative to @mr where @subregion is added.
1493 * @subregion: the subregion to be added.
1495 void memory_region_add_subregion(MemoryRegion
*mr
,
1497 MemoryRegion
*subregion
);
1499 * memory_region_add_subregion_overlap: Add a subregion to a container
1502 * Adds a subregion at @offset. The subregion may overlap with other
1503 * subregions. Conflicts are resolved by having a higher @priority hide a
1504 * lower @priority. Subregions without priority are taken as @priority 0.
1505 * A region may only be added once as a subregion (unless removed with
1506 * memory_region_del_subregion()); use memory_region_init_alias() if you
1507 * want a region to be a subregion in multiple locations.
1509 * @mr: the region to contain the new subregion; must be a container
1510 * initialized with memory_region_init().
1511 * @offset: the offset relative to @mr where @subregion is added.
1512 * @subregion: the subregion to be added.
1513 * @priority: used for resolving overlaps; highest priority wins.
1515 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1517 MemoryRegion
*subregion
,
1521 * memory_region_get_ram_addr: Get the ram address associated with a memory
1524 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1526 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1528 * memory_region_del_subregion: Remove a subregion.
1530 * Removes a subregion from its container.
1532 * @mr: the container to be updated.
1533 * @subregion: the region being removed; must be a current subregion of @mr.
1535 void memory_region_del_subregion(MemoryRegion
*mr
,
1536 MemoryRegion
*subregion
);
1539 * memory_region_set_enabled: dynamically enable or disable a region
1541 * Enables or disables a memory region. A disabled memory region
1542 * ignores all accesses to itself and its subregions. It does not
1543 * obscure sibling subregions with lower priority - it simply behaves as
1544 * if it was removed from the hierarchy.
1546 * Regions default to being enabled.
1548 * @mr: the region to be updated
1549 * @enabled: whether to enable or disable the region
1551 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1554 * memory_region_set_address: dynamically update the address of a region
1556 * Dynamically updates the address of a region, relative to its container.
1557 * May be used on regions are currently part of a memory hierarchy.
1559 * @mr: the region to be updated
1560 * @addr: new address, relative to container region
1562 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1565 * memory_region_set_size: dynamically update the size of a region.
1567 * Dynamically updates the size of a region.
1569 * @mr: the region to be updated
1570 * @size: used size of the region.
1572 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1575 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1577 * Dynamically updates the offset into the target region that an alias points
1578 * to, as if the fourth argument to memory_region_init_alias() has changed.
1580 * @mr: the #MemoryRegion to be updated; should be an alias.
1581 * @offset: the new offset into the target memory region
1583 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1587 * memory_region_present: checks if an address relative to a @container
1588 * translates into #MemoryRegion within @container
1590 * Answer whether a #MemoryRegion within @container covers the address
1593 * @container: a #MemoryRegion within which @addr is a relative address
1594 * @addr: the area within @container to be searched
1596 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1599 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1600 * into any address space.
1602 * @mr: a #MemoryRegion which should be checked if it's mapped
1604 bool memory_region_is_mapped(MemoryRegion
*mr
);
1607 * memory_region_find: translate an address/size relative to a
1608 * MemoryRegion into a #MemoryRegionSection.
1610 * Locates the first #MemoryRegion within @mr that overlaps the range
1611 * given by @addr and @size.
1613 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1614 * It will have the following characteristics:
1615 * .@size = 0 iff no overlap was found
1616 * .@mr is non-%NULL iff an overlap was found
1618 * Remember that in the return value the @offset_within_region is
1619 * relative to the returned region (in the .@mr field), not to the
1622 * Similarly, the .@offset_within_address_space is relative to the
1623 * address space that contains both regions, the passed and the
1624 * returned one. However, in the special case where the @mr argument
1625 * has no container (and thus is the root of the address space), the
1626 * following will hold:
1627 * .@offset_within_address_space >= @addr
1628 * .@offset_within_address_space + .@size <= @addr + @size
1630 * @mr: a MemoryRegion within which @addr is a relative address
1631 * @addr: start of the area within @as to be searched
1632 * @size: size of the area to be searched
1634 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1635 hwaddr addr
, uint64_t size
);
1638 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1640 * Synchronizes the dirty page log for all address spaces.
1642 void memory_global_dirty_log_sync(void);
1645 * memory_region_transaction_begin: Start a transaction.
1647 * During a transaction, changes will be accumulated and made visible
1648 * only when the transaction ends (is committed).
1650 void memory_region_transaction_begin(void);
1653 * memory_region_transaction_commit: Commit a transaction and make changes
1654 * visible to the guest.
1656 void memory_region_transaction_commit(void);
1659 * memory_listener_register: register callbacks to be called when memory
1660 * sections are mapped or unmapped into an address
1663 * @listener: an object containing the callbacks to be called
1664 * @filter: if non-%NULL, only regions in this address space will be observed
1666 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1669 * memory_listener_unregister: undo the effect of memory_listener_register()
1671 * @listener: an object containing the callbacks to be removed
1673 void memory_listener_unregister(MemoryListener
*listener
);
1676 * memory_global_dirty_log_start: begin dirty logging for all regions
1678 void memory_global_dirty_log_start(void);
1681 * memory_global_dirty_log_stop: end dirty logging for all regions
1683 void memory_global_dirty_log_stop(void);
1685 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
,
1686 bool dispatch_tree
, bool owner
);
1689 * memory_region_dispatch_read: perform a read directly to the specified
1692 * @mr: #MemoryRegion to access
1693 * @addr: address within that region
1694 * @pval: pointer to uint64_t which the data is written to
1695 * @size: size of the access in bytes
1696 * @attrs: memory transaction attributes to use for the access
1698 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1704 * memory_region_dispatch_write: perform a write directly to the specified
1707 * @mr: #MemoryRegion to access
1708 * @addr: address within that region
1709 * @data: data to write
1710 * @size: size of the access in bytes
1711 * @attrs: memory transaction attributes to use for the access
1713 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1720 * address_space_init: initializes an address space
1722 * @as: an uninitialized #AddressSpace
1723 * @root: a #MemoryRegion that routes addresses for the address space
1724 * @name: an address space name. The name is only used for debugging
1727 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1730 * address_space_destroy: destroy an address space
1732 * Releases all resources associated with an address space. After an address space
1733 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1736 * @as: address space to be destroyed
1738 void address_space_destroy(AddressSpace
*as
);
1741 * address_space_rw: read from or write to an address space.
1743 * Return a MemTxResult indicating whether the operation succeeded
1744 * or failed (eg unassigned memory, device rejected the transaction,
1747 * @as: #AddressSpace to be accessed
1748 * @addr: address within that address space
1749 * @attrs: memory transaction attributes
1750 * @buf: buffer with the data transferred
1751 * @len: the number of bytes to read or write
1752 * @is_write: indicates the transfer direction
1754 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1755 MemTxAttrs attrs
, uint8_t *buf
,
1756 int len
, bool is_write
);
1759 * address_space_write: write to address space.
1761 * Return a MemTxResult indicating whether the operation succeeded
1762 * or failed (eg unassigned memory, device rejected the transaction,
1765 * @as: #AddressSpace to be accessed
1766 * @addr: address within that address space
1767 * @attrs: memory transaction attributes
1768 * @buf: buffer with the data transferred
1769 * @len: the number of bytes to write
1771 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1773 const uint8_t *buf
, int len
);
1775 /* address_space_ld*: load from an address space
1776 * address_space_st*: store to an address space
1778 * These functions perform a load or store of the byte, word,
1779 * longword or quad to the specified address within the AddressSpace.
1780 * The _le suffixed functions treat the data as little endian;
1781 * _be indicates big endian; no suffix indicates "same endianness
1784 * The "guest CPU endianness" accessors are deprecated for use outside
1785 * target-* code; devices should be CPU-agnostic and use either the LE
1786 * or the BE accessors.
1788 * @as #AddressSpace to be accessed
1789 * @addr: address within that address space
1790 * @val: data value, for stores
1791 * @attrs: memory transaction attributes
1792 * @result: location to write the success/failure of the transaction;
1793 * if NULL, this information is discarded
1798 #define ARG1_DECL AddressSpace *as
1799 #include "exec/memory_ldst.inc.h"
1803 #define ARG1_DECL AddressSpace *as
1804 #include "exec/memory_ldst_phys.inc.h"
1806 struct MemoryRegionCache
{
1811 MemoryRegionSection mrs
;
1815 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
1818 /* address_space_ld*_cached: load from a cached #MemoryRegion
1819 * address_space_st*_cached: store into a cached #MemoryRegion
1821 * These functions perform a load or store of the byte, word,
1822 * longword or quad to the specified address. The address is
1823 * a physical address in the AddressSpace, but it must lie within
1824 * a #MemoryRegion that was mapped with address_space_cache_init.
1826 * The _le suffixed functions treat the data as little endian;
1827 * _be indicates big endian; no suffix indicates "same endianness
1830 * The "guest CPU endianness" accessors are deprecated for use outside
1831 * target-* code; devices should be CPU-agnostic and use either the LE
1832 * or the BE accessors.
1834 * @cache: previously initialized #MemoryRegionCache to be accessed
1835 * @addr: address within the address space
1836 * @val: data value, for stores
1837 * @attrs: memory transaction attributes
1838 * @result: location to write the success/failure of the transaction;
1839 * if NULL, this information is discarded
1842 #define SUFFIX _cached_slow
1844 #define ARG1_DECL MemoryRegionCache *cache
1845 #include "exec/memory_ldst.inc.h"
1847 /* Inline fast path for direct RAM access. */
1848 static inline uint8_t address_space_ldub_cached(MemoryRegionCache
*cache
,
1849 hwaddr addr
, MemTxAttrs attrs
, MemTxResult
*result
)
1851 assert(addr
< cache
->len
);
1852 if (likely(cache
->ptr
)) {
1853 return ldub_p(cache
->ptr
+ addr
);
1855 return address_space_ldub_cached_slow(cache
, addr
, attrs
, result
);
1859 static inline void address_space_stb_cached(MemoryRegionCache
*cache
,
1860 hwaddr addr
, uint32_t val
, MemTxAttrs attrs
, MemTxResult
*result
)
1862 assert(addr
< cache
->len
);
1863 if (likely(cache
->ptr
)) {
1864 stb_p(cache
->ptr
+ addr
, val
);
1866 address_space_stb_cached_slow(cache
, addr
, val
, attrs
, result
);
1870 #define ENDIANNESS _le
1871 #include "exec/memory_ldst_cached.inc.h"
1873 #define ENDIANNESS _be
1874 #include "exec/memory_ldst_cached.inc.h"
1876 #define SUFFIX _cached
1878 #define ARG1_DECL MemoryRegionCache *cache
1879 #include "exec/memory_ldst_phys.inc.h"
1881 /* address_space_cache_init: prepare for repeated access to a physical
1884 * @cache: #MemoryRegionCache to be filled
1885 * @as: #AddressSpace to be accessed
1886 * @addr: address within that address space
1887 * @len: length of buffer
1888 * @is_write: indicates the transfer direction
1890 * Will only work with RAM, and may map a subset of the requested range by
1891 * returning a value that is less than @len. On failure, return a negative
1894 * Because it only works with RAM, this function can be used for
1895 * read-modify-write operations. In this case, is_write should be %true.
1897 * Note that addresses passed to the address_space_*_cached functions
1898 * are relative to @addr.
1900 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1907 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1909 * @cache: The #MemoryRegionCache to operate on.
1910 * @addr: The first physical address that was written, relative to the
1911 * address that was passed to @address_space_cache_init.
1912 * @access_len: The number of bytes that were written starting at @addr.
1914 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1919 * address_space_cache_destroy: free a #MemoryRegionCache
1921 * @cache: The #MemoryRegionCache whose memory should be released.
1923 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1925 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1926 * entry. Should be called from an RCU critical section.
1928 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1929 bool is_write
, MemTxAttrs attrs
);
1931 /* address_space_translate: translate an address range into an address space
1932 * into a MemoryRegion and an address range into that section. Should be
1933 * called from an RCU critical section, to avoid that the last reference
1934 * to the returned region disappears after address_space_translate returns.
1936 * @fv: #FlatView to be accessed
1937 * @addr: address within that address space
1938 * @xlat: pointer to address within the returned memory region section's
1940 * @len: pointer to length
1941 * @is_write: indicates the transfer direction
1942 * @attrs: memory attributes
1944 MemoryRegion
*flatview_translate(FlatView
*fv
,
1945 hwaddr addr
, hwaddr
*xlat
,
1946 hwaddr
*len
, bool is_write
,
1949 static inline MemoryRegion
*address_space_translate(AddressSpace
*as
,
1950 hwaddr addr
, hwaddr
*xlat
,
1951 hwaddr
*len
, bool is_write
,
1954 return flatview_translate(address_space_to_flatview(as
),
1955 addr
, xlat
, len
, is_write
, attrs
);
1958 /* address_space_access_valid: check for validity of accessing an address
1961 * Check whether memory is assigned to the given address space range, and
1962 * access is permitted by any IOMMU regions that are active for the address
1965 * For now, addr and len should be aligned to a page size. This limitation
1966 * will be lifted in the future.
1968 * @as: #AddressSpace to be accessed
1969 * @addr: address within that address space
1970 * @len: length of the area to be checked
1971 * @is_write: indicates the transfer direction
1972 * @attrs: memory attributes
1974 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
,
1975 bool is_write
, MemTxAttrs attrs
);
1977 /* address_space_map: map a physical memory region into a host virtual address
1979 * May map a subset of the requested range, given by and returned in @plen.
1980 * May return %NULL if resources needed to perform the mapping are exhausted.
1981 * Use only for reads OR writes - not for read-modify-write operations.
1982 * Use cpu_register_map_client() to know when retrying the map operation is
1983 * likely to succeed.
1985 * @as: #AddressSpace to be accessed
1986 * @addr: address within that address space
1987 * @plen: pointer to length of buffer; updated on return
1988 * @is_write: indicates the transfer direction
1989 * @attrs: memory attributes
1991 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1992 hwaddr
*plen
, bool is_write
, MemTxAttrs attrs
);
1994 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1996 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1997 * the amount of memory that was actually read or written by the caller.
1999 * @as: #AddressSpace used
2000 * @buffer: host pointer as returned by address_space_map()
2001 * @len: buffer length as returned by address_space_map()
2002 * @access_len: amount of data actually transferred
2003 * @is_write: indicates the transfer direction
2005 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
2006 int is_write
, hwaddr access_len
);
2009 /* Internal functions, part of the implementation of address_space_read. */
2010 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
2011 MemTxAttrs attrs
, uint8_t *buf
, int len
);
2012 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
2013 MemTxAttrs attrs
, uint8_t *buf
,
2014 int len
, hwaddr addr1
, hwaddr l
,
2016 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
2018 /* Internal functions, part of the implementation of address_space_read_cached
2019 * and address_space_write_cached. */
2020 void address_space_read_cached_slow(MemoryRegionCache
*cache
,
2021 hwaddr addr
, void *buf
, int len
);
2022 void address_space_write_cached_slow(MemoryRegionCache
*cache
,
2023 hwaddr addr
, const void *buf
, int len
);
2025 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
2028 return memory_region_is_ram(mr
) &&
2029 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
2031 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
2032 memory_region_is_romd(mr
);
2037 * address_space_read: read from an address space.
2039 * Return a MemTxResult indicating whether the operation succeeded
2040 * or failed (eg unassigned memory, device rejected the transaction,
2041 * IOMMU fault). Called within RCU critical section.
2043 * @as: #AddressSpace to be accessed
2044 * @addr: address within that address space
2045 * @attrs: memory transaction attributes
2046 * @buf: buffer with the data transferred
2048 static inline __attribute__((__always_inline__
))
2049 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
2050 MemTxAttrs attrs
, uint8_t *buf
,
2053 MemTxResult result
= MEMTX_OK
;
2059 if (__builtin_constant_p(len
)) {
2062 fv
= address_space_to_flatview(as
);
2064 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
2065 if (len
== l
&& memory_access_is_direct(mr
, false)) {
2066 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
2067 memcpy(buf
, ptr
, len
);
2069 result
= flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
2075 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
2081 * address_space_read_cached: read from a cached RAM region
2083 * @cache: Cached region to be addressed
2084 * @addr: address relative to the base of the RAM region
2085 * @buf: buffer with the data transferred
2086 * @len: length of the data transferred
2089 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2092 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2093 if (likely(cache
->ptr
)) {
2094 memcpy(buf
, cache
->ptr
+ addr
, len
);
2096 address_space_read_cached_slow(cache
, addr
, buf
, len
);
2101 * address_space_write_cached: write to a cached RAM region
2103 * @cache: Cached region to be addressed
2104 * @addr: address relative to the base of the RAM region
2105 * @buf: buffer with the data transferred
2106 * @len: length of the data transferred
2109 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2112 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2113 if (likely(cache
->ptr
)) {
2114 memcpy(cache
->ptr
+ addr
, buf
, len
);
2116 address_space_write_cached_slow(cache
, addr
, buf
, len
);