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 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
107 IOMMUNotifierFlag flags
,
108 hwaddr start
, hwaddr end
,
112 n
->notifier_flags
= flags
;
115 n
->iommu_idx
= iommu_idx
;
119 * Memory region callbacks
121 struct MemoryRegionOps
{
122 /* Read from the memory region. @addr is relative to @mr; @size is
124 uint64_t (*read
)(void *opaque
,
127 /* Write to the memory region. @addr is relative to @mr; @size is
129 void (*write
)(void *opaque
,
134 MemTxResult (*read_with_attrs
)(void *opaque
,
139 MemTxResult (*write_with_attrs
)(void *opaque
,
145 enum device_endian endianness
;
146 /* Guest-visible constraints: */
148 /* If nonzero, specify bounds on access sizes beyond which a machine
151 unsigned min_access_size
;
152 unsigned max_access_size
;
153 /* If true, unaligned accesses are supported. Otherwise unaligned
154 * accesses throw machine checks.
158 * If present, and returns #false, the transaction is not accepted
159 * by the device (and results in machine dependent behaviour such
160 * as a machine check exception).
162 bool (*accepts
)(void *opaque
, hwaddr addr
,
163 unsigned size
, bool is_write
,
166 /* Internal implementation constraints: */
168 /* If nonzero, specifies the minimum size implemented. Smaller sizes
169 * will be rounded upwards and a partial result will be returned.
171 unsigned min_access_size
;
172 /* If nonzero, specifies the maximum size implemented. Larger sizes
173 * will be done as a series of accesses with smaller sizes.
175 unsigned max_access_size
;
176 /* If true, unaligned accesses are supported. Otherwise all accesses
177 * are converted to (possibly multiple) naturally aligned accesses.
182 /* If .read and .write are not present, old_mmio may be used for
183 * backwards compatibility with old mmio registration
185 const MemoryRegionMmio old_mmio
;
188 enum IOMMUMemoryRegionAttr
{
189 IOMMU_ATTR_SPAPR_TCE_FD
193 * IOMMUMemoryRegionClass:
195 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
196 * and provide an implementation of at least the @translate method here
197 * to handle requests to the memory region. Other methods are optional.
199 * The IOMMU implementation must use the IOMMU notifier infrastructure
200 * to report whenever mappings are changed, by calling
201 * memory_region_notify_iommu() (or, if necessary, by calling
202 * memory_region_notify_one() for each registered notifier).
204 * Conceptually an IOMMU provides a mapping from input address
205 * to an output TLB entry. If the IOMMU is aware of memory transaction
206 * attributes and the output TLB entry depends on the transaction
207 * attributes, we represent this using IOMMU indexes. Each index
208 * selects a particular translation table that the IOMMU has:
209 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
210 * @translate takes an input address and an IOMMU index
211 * and the mapping returned can only depend on the input address and the
214 * Most IOMMUs don't care about the transaction attributes and support
215 * only a single IOMMU index. A more complex IOMMU might have one index
216 * for secure transactions and one for non-secure transactions.
218 typedef struct IOMMUMemoryRegionClass
{
220 struct DeviceClass parent_class
;
223 * Return a TLB entry that contains a given address.
225 * The IOMMUAccessFlags indicated via @flag are optional and may
226 * be specified as IOMMU_NONE to indicate that the caller needs
227 * the full translation information for both reads and writes. If
228 * the access flags are specified then the IOMMU implementation
229 * may use this as an optimization, to stop doing a page table
230 * walk as soon as it knows that the requested permissions are not
231 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
232 * full page table walk and report the permissions in the returned
233 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
234 * return different mappings for reads and writes.)
236 * The returned information remains valid while the caller is
237 * holding the big QEMU lock or is inside an RCU critical section;
238 * if the caller wishes to cache the mapping beyond that it must
239 * register an IOMMU notifier so it can invalidate its cached
240 * information when the IOMMU mapping changes.
242 * @iommu: the IOMMUMemoryRegion
243 * @hwaddr: address to be translated within the memory region
244 * @flag: requested access permissions
245 * @iommu_idx: IOMMU index for the translation
247 IOMMUTLBEntry (*translate
)(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
248 IOMMUAccessFlags flag
, int iommu_idx
);
249 /* Returns minimum supported page size in bytes.
250 * If this method is not provided then the minimum is assumed to
251 * be TARGET_PAGE_SIZE.
253 * @iommu: the IOMMUMemoryRegion
255 uint64_t (*get_min_page_size
)(IOMMUMemoryRegion
*iommu
);
256 /* Called when IOMMU Notifier flag changes (ie when the set of
257 * events which IOMMU users are requesting notification for changes).
258 * Optional method -- need not be provided if the IOMMU does not
259 * need to know exactly which events must be notified.
261 * @iommu: the IOMMUMemoryRegion
262 * @old_flags: events which previously needed to be notified
263 * @new_flags: events which now need to be notified
265 void (*notify_flag_changed
)(IOMMUMemoryRegion
*iommu
,
266 IOMMUNotifierFlag old_flags
,
267 IOMMUNotifierFlag new_flags
);
268 /* Called to handle memory_region_iommu_replay().
270 * The default implementation of memory_region_iommu_replay() is to
271 * call the IOMMU translate method for every page in the address space
272 * with flag == IOMMU_NONE and then call the notifier if translate
273 * returns a valid mapping. If this method is implemented then it
274 * overrides the default behaviour, and must provide the full semantics
275 * of memory_region_iommu_replay(), by calling @notifier for every
276 * translation present in the IOMMU.
278 * Optional method -- an IOMMU only needs to provide this method
279 * if the default is inefficient or produces undesirable side effects.
281 * Note: this is not related to record-and-replay functionality.
283 void (*replay
)(IOMMUMemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
285 /* Get IOMMU misc attributes. This is an optional method that
286 * can be used to allow users of the IOMMU to get implementation-specific
287 * information. The IOMMU implements this method to handle calls
288 * by IOMMU users to memory_region_iommu_get_attr() by filling in
289 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
290 * the IOMMU supports. If the method is unimplemented then
291 * memory_region_iommu_get_attr() will always return -EINVAL.
293 * @iommu: the IOMMUMemoryRegion
294 * @attr: attribute being queried
295 * @data: memory to fill in with the attribute data
297 * Returns 0 on success, or a negative errno; in particular
298 * returns -EINVAL for unrecognized or unimplemented attribute types.
300 int (*get_attr
)(IOMMUMemoryRegion
*iommu
, enum IOMMUMemoryRegionAttr attr
,
303 /* Return the IOMMU index to use for a given set of transaction attributes.
305 * Optional method: if an IOMMU only supports a single IOMMU index then
306 * the default implementation of memory_region_iommu_attrs_to_index()
309 * The indexes supported by an IOMMU must be contiguous, starting at 0.
311 * @iommu: the IOMMUMemoryRegion
312 * @attrs: memory transaction attributes
314 int (*attrs_to_index
)(IOMMUMemoryRegion
*iommu
, MemTxAttrs attrs
);
316 /* Return the number of IOMMU indexes this IOMMU supports.
318 * Optional method: if this method is not provided, then
319 * memory_region_iommu_num_indexes() will return 1, indicating that
320 * only a single IOMMU index is supported.
322 * @iommu: the IOMMUMemoryRegion
324 int (*num_indexes
)(IOMMUMemoryRegion
*iommu
);
325 } IOMMUMemoryRegionClass
;
327 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
328 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
330 struct MemoryRegion
{
333 /* All fields are private - violators will be prosecuted */
335 /* The following fields should fit in a cache line */
339 bool readonly
; /* For RAM regions */
341 bool flush_coalesced_mmio
;
343 uint8_t dirty_log_mask
;
348 const MemoryRegionOps
*ops
;
350 MemoryRegion
*container
;
353 void (*destructor
)(MemoryRegion
*mr
);
358 bool warning_printed
; /* For reservations */
359 uint8_t vga_logging_count
;
363 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
364 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
365 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
367 unsigned ioeventfd_nb
;
368 MemoryRegionIoeventfd
*ioeventfds
;
371 struct IOMMUMemoryRegion
{
372 MemoryRegion parent_obj
;
374 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
375 IOMMUNotifierFlag iommu_notify_flags
;
378 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
379 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
382 * MemoryListener: callbacks structure for updates to the physical memory map
384 * Allows a component to adjust to changes in the guest-visible memory map.
385 * Use with memory_listener_register() and memory_listener_unregister().
387 struct MemoryListener
{
388 void (*begin
)(MemoryListener
*listener
);
389 void (*commit
)(MemoryListener
*listener
);
390 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
391 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
392 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
393 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
395 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
397 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
398 void (*log_global_start
)(MemoryListener
*listener
);
399 void (*log_global_stop
)(MemoryListener
*listener
);
400 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
401 bool match_data
, uint64_t data
, EventNotifier
*e
);
402 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
403 bool match_data
, uint64_t data
, EventNotifier
*e
);
404 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
405 hwaddr addr
, hwaddr len
);
406 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
407 hwaddr addr
, hwaddr len
);
408 /* Lower = earlier (during add), later (during del) */
410 AddressSpace
*address_space
;
411 QTAILQ_ENTRY(MemoryListener
) link
;
412 QTAILQ_ENTRY(MemoryListener
) link_as
;
416 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
418 struct AddressSpace
{
419 /* All fields are private. */
424 /* Accessed via RCU. */
425 struct FlatView
*current_map
;
428 struct MemoryRegionIoeventfd
*ioeventfds
;
429 QTAILQ_HEAD(memory_listeners_as
, MemoryListener
) listeners
;
430 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
433 typedef struct AddressSpaceDispatch AddressSpaceDispatch
;
434 typedef struct FlatRange FlatRange
;
436 /* Flattened global view of current active memory hierarchy. Kept in sorted
444 unsigned nr_allocated
;
445 struct AddressSpaceDispatch
*dispatch
;
449 static inline FlatView
*address_space_to_flatview(AddressSpace
*as
)
451 return atomic_rcu_read(&as
->current_map
);
456 * MemoryRegionSection: describes a fragment of a #MemoryRegion
458 * @mr: the region, or %NULL if empty
459 * @fv: the flat view of the address space the region is mapped in
460 * @offset_within_region: the beginning of the section, relative to @mr's start
461 * @size: the size of the section; will not exceed @mr's boundaries
462 * @offset_within_address_space: the address of the first byte of the section
463 * relative to the region's address space
464 * @readonly: writes to this section are ignored
466 struct MemoryRegionSection
{
469 hwaddr offset_within_region
;
471 hwaddr offset_within_address_space
;
476 * memory_region_init: Initialize a memory region
478 * The region typically acts as a container for other memory regions. Use
479 * memory_region_add_subregion() to add subregions.
481 * @mr: the #MemoryRegion to be initialized
482 * @owner: the object that tracks the region's reference count
483 * @name: used for debugging; not visible to the user or ABI
484 * @size: size of the region; any subregions beyond this size will be clipped
486 void memory_region_init(MemoryRegion
*mr
,
487 struct Object
*owner
,
492 * memory_region_ref: Add 1 to a memory region's reference count
494 * Whenever memory regions are accessed outside the BQL, they need to be
495 * preserved against hot-unplug. MemoryRegions actually do not have their
496 * own reference count; they piggyback on a QOM object, their "owner".
497 * This function adds a reference to the owner.
499 * All MemoryRegions must have an owner if they can disappear, even if the
500 * device they belong to operates exclusively under the BQL. This is because
501 * the region could be returned at any time by memory_region_find, and this
502 * is usually under guest control.
504 * @mr: the #MemoryRegion
506 void memory_region_ref(MemoryRegion
*mr
);
509 * memory_region_unref: Remove 1 to a memory region's reference count
511 * Whenever memory regions are accessed outside the BQL, they need to be
512 * preserved against hot-unplug. MemoryRegions actually do not have their
513 * own reference count; they piggyback on a QOM object, their "owner".
514 * This function removes a reference to the owner and possibly destroys it.
516 * @mr: the #MemoryRegion
518 void memory_region_unref(MemoryRegion
*mr
);
521 * memory_region_init_io: Initialize an I/O memory region.
523 * Accesses into the region will cause the callbacks in @ops to be called.
524 * if @size is nonzero, subregions will be clipped to @size.
526 * @mr: the #MemoryRegion to be initialized.
527 * @owner: the object that tracks the region's reference count
528 * @ops: a structure containing read and write callbacks to be used when
529 * I/O is performed on the region.
530 * @opaque: passed to the read and write callbacks of the @ops structure.
531 * @name: used for debugging; not visible to the user or ABI
532 * @size: size of the region.
534 void memory_region_init_io(MemoryRegion
*mr
,
535 struct Object
*owner
,
536 const MemoryRegionOps
*ops
,
542 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
543 * into the region will modify memory
546 * @mr: the #MemoryRegion to be initialized.
547 * @owner: the object that tracks the region's reference count
548 * @name: Region name, becomes part of RAMBlock name used in migration stream
549 * must be unique within any device
550 * @size: size of the region.
551 * @errp: pointer to Error*, to store an error if it happens.
553 * Note that this function does not do anything to cause the data in the
554 * RAM memory region to be migrated; that is the responsibility of the caller.
556 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
557 struct Object
*owner
,
563 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
564 * Accesses into the region will
565 * modify memory directly.
567 * @mr: the #MemoryRegion to be initialized.
568 * @owner: the object that tracks the region's reference count
569 * @name: Region name, becomes part of RAMBlock name used in migration stream
570 * must be unique within any device
571 * @size: size of the region.
572 * @share: allow remapping RAM to different addresses
573 * @errp: pointer to Error*, to store an error if it happens.
575 * Note that this function is similar to memory_region_init_ram_nomigrate.
576 * The only difference is part of the RAM region can be remapped.
578 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
579 struct Object
*owner
,
586 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
587 * RAM. Accesses into the region will
588 * modify memory directly. Only an initial
589 * portion of this RAM is actually used.
590 * The used size can change across reboots.
592 * @mr: the #MemoryRegion to be initialized.
593 * @owner: the object that tracks the region's reference count
594 * @name: Region name, becomes part of RAMBlock name used in migration stream
595 * must be unique within any device
596 * @size: used size of the region.
597 * @max_size: max size of the region.
598 * @resized: callback to notify owner about used size change.
599 * @errp: pointer to Error*, to store an error if it happens.
601 * Note that this function does not do anything to cause the data in the
602 * RAM memory region to be migrated; that is the responsibility of the caller.
604 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
605 struct Object
*owner
,
609 void (*resized
)(const char*,
615 * memory_region_init_ram_from_file: Initialize RAM memory region with a
618 * @mr: the #MemoryRegion to be initialized.
619 * @owner: the object that tracks the region's reference count
620 * @name: Region name, becomes part of RAMBlock name used in migration stream
621 * must be unique within any device
622 * @size: size of the region.
623 * @align: alignment of the region base address; if 0, the default alignment
624 * (getpagesize()) will be used.
625 * @share: %true if memory must be mmaped with the MAP_SHARED flag
626 * @path: the path in which to allocate the RAM.
627 * @errp: pointer to Error*, to store an error if it happens.
629 * Note that this function does not do anything to cause the data in the
630 * RAM memory region to be migrated; that is the responsibility of the caller.
632 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
633 struct Object
*owner
,
642 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
645 * @mr: the #MemoryRegion to be initialized.
646 * @owner: the object that tracks the region's reference count
647 * @name: the name of the region.
648 * @size: size of the region.
649 * @share: %true if memory must be mmaped with the MAP_SHARED flag
650 * @fd: the fd to mmap.
651 * @errp: pointer to Error*, to store an error if it happens.
653 * Note that this function does not do anything to cause the data in the
654 * RAM memory region to be migrated; that is the responsibility of the caller.
656 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
657 struct Object
*owner
,
666 * memory_region_init_ram_ptr: Initialize RAM memory region from a
667 * user-provided pointer. Accesses into the
668 * region will modify memory directly.
670 * @mr: the #MemoryRegion to be initialized.
671 * @owner: the object that tracks the region's reference count
672 * @name: Region name, becomes part of RAMBlock name used in migration stream
673 * must be unique within any device
674 * @size: size of the region.
675 * @ptr: memory to be mapped; must contain at least @size bytes.
677 * Note that this function does not do anything to cause the data in the
678 * RAM memory region to be migrated; that is the responsibility of the caller.
680 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
681 struct Object
*owner
,
687 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
688 * a user-provided pointer.
690 * A RAM device represents a mapping to a physical device, such as to a PCI
691 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
692 * into the VM address space and access to the region will modify memory
693 * directly. However, the memory region should not be included in a memory
694 * dump (device may not be enabled/mapped at the time of the dump), and
695 * operations incompatible with manipulating MMIO should be avoided. Replaces
698 * @mr: the #MemoryRegion to be initialized.
699 * @owner: the object that tracks the region's reference count
700 * @name: the name of the region.
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.
706 * (For RAM device memory regions, migrating the contents rarely makes sense.)
708 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
709 struct Object
*owner
,
715 * memory_region_init_alias: Initialize a memory region that aliases all or a
716 * part of another memory region.
718 * @mr: the #MemoryRegion to be initialized.
719 * @owner: the object that tracks the region's reference count
720 * @name: used for debugging; not visible to the user or ABI
721 * @orig: the region to be referenced; @mr will be equivalent to
722 * @orig between @offset and @offset + @size - 1.
723 * @offset: start of the section in @orig to be referenced.
724 * @size: size of the region.
726 void memory_region_init_alias(MemoryRegion
*mr
,
727 struct Object
*owner
,
734 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
736 * This has the same effect as calling memory_region_init_ram_nomigrate()
737 * and then marking the resulting region read-only with
738 * memory_region_set_readonly().
740 * Note that this function does not do anything to cause the data in the
741 * RAM side of the memory region to be migrated; that is the responsibility
744 * @mr: the #MemoryRegion to be initialized.
745 * @owner: the object that tracks the region's reference count
746 * @name: Region name, becomes part of RAMBlock name used in migration stream
747 * must be unique within any device
748 * @size: size of the region.
749 * @errp: pointer to Error*, to store an error if it happens.
751 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
752 struct Object
*owner
,
758 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
759 * Writes are handled via callbacks.
761 * Note that this function does not do anything to cause the data in the
762 * RAM side of the memory region to be migrated; that is the responsibility
765 * @mr: the #MemoryRegion to be initialized.
766 * @owner: the object that tracks the region's reference count
767 * @ops: callbacks for write access handling (must not be NULL).
768 * @opaque: passed to the read and write callbacks of the @ops structure.
769 * @name: Region name, becomes part of RAMBlock name used in migration stream
770 * must be unique within any device
771 * @size: size of the region.
772 * @errp: pointer to Error*, to store an error if it happens.
774 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
775 struct Object
*owner
,
776 const MemoryRegionOps
*ops
,
783 * memory_region_init_iommu: Initialize a memory region of a custom type
784 * that translates addresses
786 * An IOMMU region translates addresses and forwards accesses to a target
789 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
790 * @_iommu_mr should be a pointer to enough memory for an instance of
791 * that subclass, @instance_size is the size of that subclass, and
792 * @mrtypename is its name. This function will initialize @_iommu_mr as an
793 * instance of the subclass, and its methods will then be called to handle
794 * accesses to the memory region. See the documentation of
795 * #IOMMUMemoryRegionClass for further details.
797 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
798 * @instance_size: the IOMMUMemoryRegion subclass instance size
799 * @mrtypename: the type name of the #IOMMUMemoryRegion
800 * @owner: the object that tracks the region's reference count
801 * @name: used for debugging; not visible to the user or ABI
802 * @size: size of the region.
804 void memory_region_init_iommu(void *_iommu_mr
,
805 size_t instance_size
,
806 const char *mrtypename
,
812 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
813 * region will modify memory directly.
815 * @mr: the #MemoryRegion to be initialized
816 * @owner: the object that tracks the region's reference count (must be
817 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
818 * @name: name of the memory region
819 * @size: size of the region in bytes
820 * @errp: pointer to Error*, to store an error if it happens.
822 * This function allocates RAM for a board model or device, and
823 * arranges for it to be migrated (by calling vmstate_register_ram()
824 * if @owner is a DeviceState, or vmstate_register_ram_global() if
827 * TODO: Currently we restrict @owner to being either NULL (for
828 * global RAM regions with no owner) or devices, so that we can
829 * give the RAM block a unique name for migration purposes.
830 * We should lift this restriction and allow arbitrary Objects.
831 * If you pass a non-NULL non-device @owner then we will assert.
833 void memory_region_init_ram(MemoryRegion
*mr
,
834 struct Object
*owner
,
840 * memory_region_init_rom: Initialize a ROM memory region.
842 * This has the same effect as calling memory_region_init_ram()
843 * and then marking the resulting region read-only with
844 * memory_region_set_readonly(). This includes arranging for the
845 * contents to be migrated.
847 * TODO: Currently we restrict @owner to being either NULL (for
848 * global RAM regions with no owner) or devices, so that we can
849 * give the RAM block a unique name for migration purposes.
850 * We should lift this restriction and allow arbitrary Objects.
851 * If you pass a non-NULL non-device @owner then we will assert.
853 * @mr: the #MemoryRegion to be initialized.
854 * @owner: the object that tracks the region's reference count
855 * @name: Region name, becomes part of RAMBlock name used in migration stream
856 * must be unique within any device
857 * @size: size of the region.
858 * @errp: pointer to Error*, to store an error if it happens.
860 void memory_region_init_rom(MemoryRegion
*mr
,
861 struct Object
*owner
,
867 * memory_region_init_rom_device: Initialize a ROM memory region.
868 * Writes are handled via callbacks.
870 * This function initializes a memory region backed by RAM for reads
871 * and callbacks for writes, and arranges for the RAM backing to
872 * be migrated (by calling vmstate_register_ram()
873 * if @owner is a DeviceState, or vmstate_register_ram_global() if
876 * TODO: Currently we restrict @owner to being either NULL (for
877 * global RAM regions with no owner) or devices, so that we can
878 * give the RAM block a unique name for migration purposes.
879 * We should lift this restriction and allow arbitrary Objects.
880 * If you pass a non-NULL non-device @owner then we will assert.
882 * @mr: the #MemoryRegion to be initialized.
883 * @owner: the object that tracks the region's reference count
884 * @ops: callbacks for write access handling (must not be NULL).
885 * @name: Region name, becomes part of RAMBlock name used in migration stream
886 * must be unique within any device
887 * @size: size of the region.
888 * @errp: pointer to Error*, to store an error if it happens.
890 void memory_region_init_rom_device(MemoryRegion
*mr
,
891 struct Object
*owner
,
892 const MemoryRegionOps
*ops
,
900 * memory_region_owner: get a memory region's owner.
902 * @mr: the memory region being queried.
904 struct Object
*memory_region_owner(MemoryRegion
*mr
);
907 * memory_region_size: get a memory region's size.
909 * @mr: the memory region being queried.
911 uint64_t memory_region_size(MemoryRegion
*mr
);
914 * memory_region_is_ram: check whether a memory region is random access
916 * Returns %true is a memory region is random access.
918 * @mr: the memory region being queried
920 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
926 * memory_region_is_ram_device: check whether a memory region is a ram device
928 * Returns %true is a memory region is a device backed ram region
930 * @mr: the memory region being queried
932 bool memory_region_is_ram_device(MemoryRegion
*mr
);
935 * memory_region_is_romd: check whether a memory region is in ROMD mode
937 * Returns %true if a memory region is a ROM device and currently set to allow
940 * @mr: the memory region being queried
942 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
944 return mr
->rom_device
&& mr
->romd_mode
;
948 * memory_region_get_iommu: check whether a memory region is an iommu
950 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
953 * @mr: the memory region being queried
955 static inline IOMMUMemoryRegion
*memory_region_get_iommu(MemoryRegion
*mr
)
958 return memory_region_get_iommu(mr
->alias
);
961 return (IOMMUMemoryRegion
*) mr
;
967 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
968 * if an iommu or NULL if not
970 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
971 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
973 * @mr: the memory region being queried
975 static inline IOMMUMemoryRegionClass
*memory_region_get_iommu_class_nocheck(
976 IOMMUMemoryRegion
*iommu_mr
)
978 return (IOMMUMemoryRegionClass
*) (((Object
*)iommu_mr
)->class);
981 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
984 * memory_region_iommu_get_min_page_size: get minimum supported page size
987 * Returns minimum supported page size for an iommu.
989 * @iommu_mr: the memory region being queried
991 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
);
994 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
996 * The notification type will be decided by entry.perm bits:
998 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
999 * - For MAP (newly added entry) notifies: set entry.perm to the
1000 * permission of the page (which is definitely !IOMMU_NONE).
1002 * Note: for any IOMMU implementation, an in-place mapping change
1003 * should be notified with an UNMAP followed by a MAP.
1005 * @iommu_mr: the memory region that was changed
1006 * @iommu_idx: the IOMMU index for the translation table which has changed
1007 * @entry: the new entry in the IOMMU translation table. The entry
1008 * replaces all old entries for the same virtual I/O address range.
1009 * Deleted entries have .@perm == 0.
1011 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1013 IOMMUTLBEntry entry
);
1016 * memory_region_notify_one: notify a change in an IOMMU translation
1017 * entry to a single notifier
1019 * This works just like memory_region_notify_iommu(), but it only
1020 * notifies a specific notifier, not all of them.
1022 * @notifier: the notifier to be notified
1023 * @entry: the new entry in the IOMMU translation table. The entry
1024 * replaces all old entries for the same virtual I/O address range.
1025 * Deleted entries have .@perm == 0.
1027 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1028 IOMMUTLBEntry
*entry
);
1031 * memory_region_register_iommu_notifier: register a notifier for changes to
1032 * IOMMU translation entries.
1034 * @mr: the memory region to observe
1035 * @n: the IOMMUNotifier to be added; the notify callback receives a
1036 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1037 * ceases to be valid on exit from the notifier.
1039 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1043 * memory_region_iommu_replay: replay existing IOMMU translations to
1044 * a notifier with the minimum page granularity returned by
1045 * mr->iommu_ops->get_page_size().
1047 * Note: this is not related to record-and-replay functionality.
1049 * @iommu_mr: the memory region to observe
1050 * @n: the notifier to which to replay iommu mappings
1052 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
);
1055 * memory_region_iommu_replay_all: replay existing IOMMU translations
1056 * to all the notifiers registered.
1058 * Note: this is not related to record-and-replay functionality.
1060 * @iommu_mr: the memory region to observe
1062 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
);
1065 * memory_region_unregister_iommu_notifier: unregister a notifier for
1066 * changes to IOMMU translation entries.
1068 * @mr: the memory region which was observed and for which notity_stopped()
1069 * needs to be called
1070 * @n: the notifier to be removed.
1072 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1076 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1077 * defined on the IOMMU.
1079 * Returns 0 on success, or a negative errno otherwise. In particular,
1080 * -EINVAL indicates that the IOMMU does not support the requested
1083 * @iommu_mr: the memory region
1084 * @attr: the requested attribute
1085 * @data: a pointer to the requested attribute data
1087 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1088 enum IOMMUMemoryRegionAttr attr
,
1092 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1093 * use for translations with the given memory transaction attributes.
1095 * @iommu_mr: the memory region
1096 * @attrs: the memory transaction attributes
1098 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1102 * memory_region_iommu_num_indexes: return the total number of IOMMU
1103 * indexes that this IOMMU supports.
1105 * @iommu_mr: the memory region
1107 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
);
1110 * memory_region_name: get a memory region's name
1112 * Returns the string that was used to initialize the memory region.
1114 * @mr: the memory region being queried
1116 const char *memory_region_name(const MemoryRegion
*mr
);
1119 * memory_region_is_logging: return whether a memory region is logging writes
1121 * Returns %true if the memory region is logging writes for the given client
1123 * @mr: the memory region being queried
1124 * @client: the client being queried
1126 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
1129 * memory_region_get_dirty_log_mask: return the clients for which a
1130 * memory region is logging writes.
1132 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1133 * are the bit indices.
1135 * @mr: the memory region being queried
1137 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
1140 * memory_region_is_rom: check whether a memory region is ROM
1142 * Returns %true is a memory region is read-only memory.
1144 * @mr: the memory region being queried
1146 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
1148 return mr
->ram
&& mr
->readonly
;
1153 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1155 * Returns a file descriptor backing a file-based RAM memory region,
1156 * or -1 if the region is not a file-based RAM memory region.
1158 * @mr: the RAM or alias memory region being queried.
1160 int memory_region_get_fd(MemoryRegion
*mr
);
1163 * memory_region_from_host: Convert a pointer into a RAM memory region
1164 * and an offset within it.
1166 * Given a host pointer inside a RAM memory region (created with
1167 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1168 * the MemoryRegion and the offset within it.
1170 * Use with care; by the time this function returns, the returned pointer is
1171 * not protected by RCU anymore. If the caller is not within an RCU critical
1172 * section and does not hold the iothread lock, it must have other means of
1173 * protecting the pointer, such as a reference to the region that includes
1174 * the incoming ram_addr_t.
1176 * @ptr: the host pointer to be converted
1177 * @offset: the offset within memory region
1179 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
1182 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1184 * Returns a host pointer to a RAM memory region (created with
1185 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1187 * Use with care; by the time this function returns, the returned pointer is
1188 * not protected by RCU anymore. If the caller is not within an RCU critical
1189 * section and does not hold the iothread lock, it must have other means of
1190 * protecting the pointer, such as a reference to the region that includes
1191 * the incoming ram_addr_t.
1193 * @mr: the memory region being queried.
1195 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
1197 /* memory_region_ram_resize: Resize a RAM region.
1199 * Only legal before guest might have detected the memory size: e.g. on
1200 * incoming migration, or right after reset.
1202 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1203 * @newsize: the new size the region
1204 * @errp: pointer to Error*, to store an error if it happens.
1206 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
1210 * memory_region_set_log: Turn dirty logging on or off for a region.
1212 * Turns dirty logging on or off for a specified client (display, migration).
1213 * Only meaningful for RAM regions.
1215 * @mr: the memory region being updated.
1216 * @log: whether dirty logging is to be enabled or disabled.
1217 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1219 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
1222 * memory_region_get_dirty: Check whether a range of bytes is dirty
1223 * for a specified client.
1225 * Checks whether a range of bytes has been written to since the last
1226 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1229 * @mr: the memory region being queried.
1230 * @addr: the address (relative to the start of the region) being queried.
1231 * @size: the size of the range being queried.
1232 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1233 * %DIRTY_MEMORY_VGA.
1235 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1236 hwaddr size
, unsigned client
);
1239 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1241 * Marks a range of bytes as dirty, after it has been dirtied outside
1244 * @mr: the memory region being dirtied.
1245 * @addr: the address (relative to the start of the region) being dirtied.
1246 * @size: size of the range being dirtied.
1248 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1252 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1253 * bitmap and clear it.
1255 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1256 * returns the snapshot. The snapshot can then be used to query dirty
1257 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1258 * querying the same page multiple times, which is especially useful for
1259 * display updates where the scanlines often are not page aligned.
1261 * The dirty bitmap region which gets copyed into the snapshot (and
1262 * cleared afterwards) can be larger than requested. The boundaries
1263 * are rounded up/down so complete bitmap longs (covering 64 pages on
1264 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1265 * isn't a problem for display updates as the extra pages are outside
1266 * the visible area, and in case the visible area changes a full
1267 * display redraw is due anyway. Should other use cases for this
1268 * function emerge we might have to revisit this implementation
1271 * Use g_free to release DirtyBitmapSnapshot.
1273 * @mr: the memory region being queried.
1274 * @addr: the address (relative to the start of the region) being queried.
1275 * @size: the size of the range being queried.
1276 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1278 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1284 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1285 * in the specified dirty bitmap snapshot.
1287 * @mr: the memory region being queried.
1288 * @snap: the dirty bitmap snapshot
1289 * @addr: the address (relative to the start of the region) being queried.
1290 * @size: the size of the range being queried.
1292 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
1293 DirtyBitmapSnapshot
*snap
,
1294 hwaddr addr
, hwaddr size
);
1297 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1300 * Marks a range of pages as no longer dirty.
1302 * @mr: the region being updated.
1303 * @addr: the start of the subrange being cleaned.
1304 * @size: the size of the subrange being cleaned.
1305 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1306 * %DIRTY_MEMORY_VGA.
1308 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1309 hwaddr size
, unsigned client
);
1312 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1314 * Allows a memory region to be marked as read-only (turning it into a ROM).
1315 * only useful on RAM regions.
1317 * @mr: the region being updated.
1318 * @readonly: whether rhe region is to be ROM or RAM.
1320 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
1323 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1325 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1326 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1327 * device is mapped to guest memory and satisfies read access directly.
1328 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1329 * Writes are always handled by the #MemoryRegion.write function.
1331 * @mr: the memory region to be updated
1332 * @romd_mode: %true to put the region into ROMD mode
1334 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
1337 * memory_region_set_coalescing: Enable memory coalescing for the region.
1339 * Enabled writes to a region to be queued for later processing. MMIO ->write
1340 * callbacks may be delayed until a non-coalesced MMIO is issued.
1341 * Only useful for IO regions. Roughly similar to write-combining hardware.
1343 * @mr: the memory region to be write coalesced
1345 void memory_region_set_coalescing(MemoryRegion
*mr
);
1348 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1351 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1352 * Multiple calls can be issued coalesced disjoint ranges.
1354 * @mr: the memory region to be updated.
1355 * @offset: the start of the range within the region to be coalesced.
1356 * @size: the size of the subrange to be coalesced.
1358 void memory_region_add_coalescing(MemoryRegion
*mr
,
1363 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1365 * Disables any coalescing caused by memory_region_set_coalescing() or
1366 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1369 * @mr: the memory region to be updated.
1371 void memory_region_clear_coalescing(MemoryRegion
*mr
);
1374 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1377 * Ensure that pending coalesced MMIO request are flushed before the memory
1378 * region is accessed. This property is automatically enabled for all regions
1379 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1381 * @mr: the memory region to be updated.
1383 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
1386 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1389 * Clear the automatic coalesced MMIO flushing enabled via
1390 * memory_region_set_flush_coalesced. Note that this service has no effect on
1391 * memory regions that have MMIO coalescing enabled for themselves. For them,
1392 * automatic flushing will stop once coalescing is disabled.
1394 * @mr: the memory region to be updated.
1396 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1399 * memory_region_clear_global_locking: Declares that access processing does
1400 * not depend on the QEMU global lock.
1402 * By clearing this property, accesses to the memory region will be processed
1403 * outside of QEMU's global lock (unless the lock is held on when issuing the
1404 * access request). In this case, the device model implementing the access
1405 * handlers is responsible for synchronization of concurrency.
1407 * @mr: the memory region to be updated.
1409 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1412 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1413 * is written to a location.
1415 * Marks a word in an IO region (initialized with memory_region_init_io())
1416 * as a trigger for an eventfd event. The I/O callback will not be called.
1417 * The caller must be prepared to handle failure (that is, take the required
1418 * action if the callback _is_ called).
1420 * @mr: the memory region being updated.
1421 * @addr: the address within @mr that is to be monitored
1422 * @size: the size of the access to trigger the eventfd
1423 * @match_data: whether to match against @data, instead of just @addr
1424 * @data: the data to match against the guest write
1425 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1427 void memory_region_add_eventfd(MemoryRegion
*mr
,
1435 * memory_region_del_eventfd: Cancel an eventfd.
1437 * Cancels an eventfd trigger requested by a previous
1438 * memory_region_add_eventfd() call.
1440 * @mr: the memory region being updated.
1441 * @addr: the address within @mr that is to be monitored
1442 * @size: the size of the access to trigger the eventfd
1443 * @match_data: whether to match against @data, instead of just @addr
1444 * @data: the data to match against the guest write
1445 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1447 void memory_region_del_eventfd(MemoryRegion
*mr
,
1455 * memory_region_add_subregion: Add a subregion to a container.
1457 * Adds a subregion at @offset. The subregion may not overlap with other
1458 * subregions (except for those explicitly marked as overlapping). A region
1459 * may only be added once as a subregion (unless removed with
1460 * memory_region_del_subregion()); use memory_region_init_alias() if you
1461 * want a region to be a subregion in multiple locations.
1463 * @mr: the region to contain the new subregion; must be a container
1464 * initialized with memory_region_init().
1465 * @offset: the offset relative to @mr where @subregion is added.
1466 * @subregion: the subregion to be added.
1468 void memory_region_add_subregion(MemoryRegion
*mr
,
1470 MemoryRegion
*subregion
);
1472 * memory_region_add_subregion_overlap: Add a subregion to a container
1475 * Adds a subregion at @offset. The subregion may overlap with other
1476 * subregions. Conflicts are resolved by having a higher @priority hide a
1477 * lower @priority. Subregions without priority are taken as @priority 0.
1478 * A region may only be added once as a subregion (unless removed with
1479 * memory_region_del_subregion()); use memory_region_init_alias() if you
1480 * want a region to be a subregion in multiple locations.
1482 * @mr: the region to contain the new subregion; must be a container
1483 * initialized with memory_region_init().
1484 * @offset: the offset relative to @mr where @subregion is added.
1485 * @subregion: the subregion to be added.
1486 * @priority: used for resolving overlaps; highest priority wins.
1488 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1490 MemoryRegion
*subregion
,
1494 * memory_region_get_ram_addr: Get the ram address associated with a memory
1497 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1499 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1501 * memory_region_del_subregion: Remove a subregion.
1503 * Removes a subregion from its container.
1505 * @mr: the container to be updated.
1506 * @subregion: the region being removed; must be a current subregion of @mr.
1508 void memory_region_del_subregion(MemoryRegion
*mr
,
1509 MemoryRegion
*subregion
);
1512 * memory_region_set_enabled: dynamically enable or disable a region
1514 * Enables or disables a memory region. A disabled memory region
1515 * ignores all accesses to itself and its subregions. It does not
1516 * obscure sibling subregions with lower priority - it simply behaves as
1517 * if it was removed from the hierarchy.
1519 * Regions default to being enabled.
1521 * @mr: the region to be updated
1522 * @enabled: whether to enable or disable the region
1524 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1527 * memory_region_set_address: dynamically update the address of a region
1529 * Dynamically updates the address of a region, relative to its container.
1530 * May be used on regions are currently part of a memory hierarchy.
1532 * @mr: the region to be updated
1533 * @addr: new address, relative to container region
1535 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1538 * memory_region_set_size: dynamically update the size of a region.
1540 * Dynamically updates the size of a region.
1542 * @mr: the region to be updated
1543 * @size: used size of the region.
1545 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1548 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1550 * Dynamically updates the offset into the target region that an alias points
1551 * to, as if the fourth argument to memory_region_init_alias() has changed.
1553 * @mr: the #MemoryRegion to be updated; should be an alias.
1554 * @offset: the new offset into the target memory region
1556 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1560 * memory_region_present: checks if an address relative to a @container
1561 * translates into #MemoryRegion within @container
1563 * Answer whether a #MemoryRegion within @container covers the address
1566 * @container: a #MemoryRegion within which @addr is a relative address
1567 * @addr: the area within @container to be searched
1569 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1572 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1573 * into any address space.
1575 * @mr: a #MemoryRegion which should be checked if it's mapped
1577 bool memory_region_is_mapped(MemoryRegion
*mr
);
1580 * memory_region_find: translate an address/size relative to a
1581 * MemoryRegion into a #MemoryRegionSection.
1583 * Locates the first #MemoryRegion within @mr that overlaps the range
1584 * given by @addr and @size.
1586 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1587 * It will have the following characteristics:
1588 * .@size = 0 iff no overlap was found
1589 * .@mr is non-%NULL iff an overlap was found
1591 * Remember that in the return value the @offset_within_region is
1592 * relative to the returned region (in the .@mr field), not to the
1595 * Similarly, the .@offset_within_address_space is relative to the
1596 * address space that contains both regions, the passed and the
1597 * returned one. However, in the special case where the @mr argument
1598 * has no container (and thus is the root of the address space), the
1599 * following will hold:
1600 * .@offset_within_address_space >= @addr
1601 * .@offset_within_address_space + .@size <= @addr + @size
1603 * @mr: a MemoryRegion within which @addr is a relative address
1604 * @addr: start of the area within @as to be searched
1605 * @size: size of the area to be searched
1607 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1608 hwaddr addr
, uint64_t size
);
1611 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1613 * Synchronizes the dirty page log for all address spaces.
1615 void memory_global_dirty_log_sync(void);
1618 * memory_region_transaction_begin: Start a transaction.
1620 * During a transaction, changes will be accumulated and made visible
1621 * only when the transaction ends (is committed).
1623 void memory_region_transaction_begin(void);
1626 * memory_region_transaction_commit: Commit a transaction and make changes
1627 * visible to the guest.
1629 void memory_region_transaction_commit(void);
1632 * memory_listener_register: register callbacks to be called when memory
1633 * sections are mapped or unmapped into an address
1636 * @listener: an object containing the callbacks to be called
1637 * @filter: if non-%NULL, only regions in this address space will be observed
1639 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1642 * memory_listener_unregister: undo the effect of memory_listener_register()
1644 * @listener: an object containing the callbacks to be removed
1646 void memory_listener_unregister(MemoryListener
*listener
);
1649 * memory_global_dirty_log_start: begin dirty logging for all regions
1651 void memory_global_dirty_log_start(void);
1654 * memory_global_dirty_log_stop: end dirty logging for all regions
1656 void memory_global_dirty_log_stop(void);
1658 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
,
1659 bool dispatch_tree
, bool owner
);
1662 * memory_region_dispatch_read: perform a read directly to the specified
1665 * @mr: #MemoryRegion to access
1666 * @addr: address within that region
1667 * @pval: pointer to uint64_t which the data is written to
1668 * @size: size of the access in bytes
1669 * @attrs: memory transaction attributes to use for the access
1671 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1677 * memory_region_dispatch_write: perform a write directly to the specified
1680 * @mr: #MemoryRegion to access
1681 * @addr: address within that region
1682 * @data: data to write
1683 * @size: size of the access in bytes
1684 * @attrs: memory transaction attributes to use for the access
1686 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1693 * address_space_init: initializes an address space
1695 * @as: an uninitialized #AddressSpace
1696 * @root: a #MemoryRegion that routes addresses for the address space
1697 * @name: an address space name. The name is only used for debugging
1700 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1703 * address_space_destroy: destroy an address space
1705 * Releases all resources associated with an address space. After an address space
1706 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1709 * @as: address space to be destroyed
1711 void address_space_destroy(AddressSpace
*as
);
1714 * address_space_rw: read from or write to an address space.
1716 * Return a MemTxResult indicating whether the operation succeeded
1717 * or failed (eg unassigned memory, device rejected the transaction,
1720 * @as: #AddressSpace to be accessed
1721 * @addr: address within that address space
1722 * @attrs: memory transaction attributes
1723 * @buf: buffer with the data transferred
1724 * @len: the number of bytes to read or write
1725 * @is_write: indicates the transfer direction
1727 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1728 MemTxAttrs attrs
, uint8_t *buf
,
1729 int len
, bool is_write
);
1732 * address_space_write: write to address space.
1734 * Return a MemTxResult indicating whether the operation succeeded
1735 * or failed (eg unassigned memory, device rejected the transaction,
1738 * @as: #AddressSpace to be accessed
1739 * @addr: address within that address space
1740 * @attrs: memory transaction attributes
1741 * @buf: buffer with the data transferred
1742 * @len: the number of bytes to write
1744 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1746 const uint8_t *buf
, int len
);
1748 /* address_space_ld*: load from an address space
1749 * address_space_st*: store to an address space
1751 * These functions perform a load or store of the byte, word,
1752 * longword or quad to the specified address within the AddressSpace.
1753 * The _le suffixed functions treat the data as little endian;
1754 * _be indicates big endian; no suffix indicates "same endianness
1757 * The "guest CPU endianness" accessors are deprecated for use outside
1758 * target-* code; devices should be CPU-agnostic and use either the LE
1759 * or the BE accessors.
1761 * @as #AddressSpace to be accessed
1762 * @addr: address within that address space
1763 * @val: data value, for stores
1764 * @attrs: memory transaction attributes
1765 * @result: location to write the success/failure of the transaction;
1766 * if NULL, this information is discarded
1771 #define ARG1_DECL AddressSpace *as
1772 #include "exec/memory_ldst.inc.h"
1776 #define ARG1_DECL AddressSpace *as
1777 #include "exec/memory_ldst_phys.inc.h"
1779 struct MemoryRegionCache
{
1784 MemoryRegionSection mrs
;
1788 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
1791 /* address_space_ld*_cached: load from a cached #MemoryRegion
1792 * address_space_st*_cached: store into a cached #MemoryRegion
1794 * These functions perform a load or store of the byte, word,
1795 * longword or quad to the specified address. The address is
1796 * a physical address in the AddressSpace, but it must lie within
1797 * a #MemoryRegion that was mapped with address_space_cache_init.
1799 * The _le suffixed functions treat the data as little endian;
1800 * _be indicates big endian; no suffix indicates "same endianness
1803 * The "guest CPU endianness" accessors are deprecated for use outside
1804 * target-* code; devices should be CPU-agnostic and use either the LE
1805 * or the BE accessors.
1807 * @cache: previously initialized #MemoryRegionCache to be accessed
1808 * @addr: address within the address space
1809 * @val: data value, for stores
1810 * @attrs: memory transaction attributes
1811 * @result: location to write the success/failure of the transaction;
1812 * if NULL, this information is discarded
1815 #define SUFFIX _cached_slow
1817 #define ARG1_DECL MemoryRegionCache *cache
1818 #include "exec/memory_ldst.inc.h"
1820 /* Inline fast path for direct RAM access. */
1821 static inline uint8_t address_space_ldub_cached(MemoryRegionCache
*cache
,
1822 hwaddr addr
, MemTxAttrs attrs
, MemTxResult
*result
)
1824 assert(addr
< cache
->len
);
1825 if (likely(cache
->ptr
)) {
1826 return ldub_p(cache
->ptr
+ addr
);
1828 return address_space_ldub_cached_slow(cache
, addr
, attrs
, result
);
1832 static inline void address_space_stb_cached(MemoryRegionCache
*cache
,
1833 hwaddr addr
, uint32_t val
, MemTxAttrs attrs
, MemTxResult
*result
)
1835 assert(addr
< cache
->len
);
1836 if (likely(cache
->ptr
)) {
1837 stb_p(cache
->ptr
+ addr
, val
);
1839 address_space_stb_cached_slow(cache
, addr
, val
, attrs
, result
);
1843 #define ENDIANNESS _le
1844 #include "exec/memory_ldst_cached.inc.h"
1846 #define ENDIANNESS _be
1847 #include "exec/memory_ldst_cached.inc.h"
1849 #define SUFFIX _cached
1851 #define ARG1_DECL MemoryRegionCache *cache
1852 #include "exec/memory_ldst_phys.inc.h"
1854 /* address_space_cache_init: prepare for repeated access to a physical
1857 * @cache: #MemoryRegionCache to be filled
1858 * @as: #AddressSpace to be accessed
1859 * @addr: address within that address space
1860 * @len: length of buffer
1861 * @is_write: indicates the transfer direction
1863 * Will only work with RAM, and may map a subset of the requested range by
1864 * returning a value that is less than @len. On failure, return a negative
1867 * Because it only works with RAM, this function can be used for
1868 * read-modify-write operations. In this case, is_write should be %true.
1870 * Note that addresses passed to the address_space_*_cached functions
1871 * are relative to @addr.
1873 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1880 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1882 * @cache: The #MemoryRegionCache to operate on.
1883 * @addr: The first physical address that was written, relative to the
1884 * address that was passed to @address_space_cache_init.
1885 * @access_len: The number of bytes that were written starting at @addr.
1887 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1892 * address_space_cache_destroy: free a #MemoryRegionCache
1894 * @cache: The #MemoryRegionCache whose memory should be released.
1896 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1898 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1899 * entry. Should be called from an RCU critical section.
1901 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1902 bool is_write
, MemTxAttrs attrs
);
1904 /* address_space_translate: translate an address range into an address space
1905 * into a MemoryRegion and an address range into that section. Should be
1906 * called from an RCU critical section, to avoid that the last reference
1907 * to the returned region disappears after address_space_translate returns.
1909 * @fv: #FlatView to be accessed
1910 * @addr: address within that address space
1911 * @xlat: pointer to address within the returned memory region section's
1913 * @len: pointer to length
1914 * @is_write: indicates the transfer direction
1915 * @attrs: memory attributes
1917 MemoryRegion
*flatview_translate(FlatView
*fv
,
1918 hwaddr addr
, hwaddr
*xlat
,
1919 hwaddr
*len
, bool is_write
,
1922 static inline MemoryRegion
*address_space_translate(AddressSpace
*as
,
1923 hwaddr addr
, hwaddr
*xlat
,
1924 hwaddr
*len
, bool is_write
,
1927 return flatview_translate(address_space_to_flatview(as
),
1928 addr
, xlat
, len
, is_write
, attrs
);
1931 /* address_space_access_valid: check for validity of accessing an address
1934 * Check whether memory is assigned to the given address space range, and
1935 * access is permitted by any IOMMU regions that are active for the address
1938 * For now, addr and len should be aligned to a page size. This limitation
1939 * will be lifted in the future.
1941 * @as: #AddressSpace to be accessed
1942 * @addr: address within that address space
1943 * @len: length of the area to be checked
1944 * @is_write: indicates the transfer direction
1945 * @attrs: memory attributes
1947 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
,
1948 bool is_write
, MemTxAttrs attrs
);
1950 /* address_space_map: map a physical memory region into a host virtual address
1952 * May map a subset of the requested range, given by and returned in @plen.
1953 * May return %NULL if resources needed to perform the mapping are exhausted.
1954 * Use only for reads OR writes - not for read-modify-write operations.
1955 * Use cpu_register_map_client() to know when retrying the map operation is
1956 * likely to succeed.
1958 * @as: #AddressSpace to be accessed
1959 * @addr: address within that address space
1960 * @plen: pointer to length of buffer; updated on return
1961 * @is_write: indicates the transfer direction
1962 * @attrs: memory attributes
1964 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1965 hwaddr
*plen
, bool is_write
, MemTxAttrs attrs
);
1967 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1969 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1970 * the amount of memory that was actually read or written by the caller.
1972 * @as: #AddressSpace used
1973 * @buffer: host pointer as returned by address_space_map()
1974 * @len: buffer length as returned by address_space_map()
1975 * @access_len: amount of data actually transferred
1976 * @is_write: indicates the transfer direction
1978 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1979 int is_write
, hwaddr access_len
);
1982 /* Internal functions, part of the implementation of address_space_read. */
1983 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
1984 MemTxAttrs attrs
, uint8_t *buf
, int len
);
1985 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
1986 MemTxAttrs attrs
, uint8_t *buf
,
1987 int len
, hwaddr addr1
, hwaddr l
,
1989 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
1991 /* Internal functions, part of the implementation of address_space_read_cached
1992 * and address_space_write_cached. */
1993 void address_space_read_cached_slow(MemoryRegionCache
*cache
,
1994 hwaddr addr
, void *buf
, int len
);
1995 void address_space_write_cached_slow(MemoryRegionCache
*cache
,
1996 hwaddr addr
, const void *buf
, int len
);
1998 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
2001 return memory_region_is_ram(mr
) &&
2002 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
2004 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
2005 memory_region_is_romd(mr
);
2010 * address_space_read: read from an address space.
2012 * Return a MemTxResult indicating whether the operation succeeded
2013 * or failed (eg unassigned memory, device rejected the transaction,
2014 * IOMMU fault). Called within RCU critical section.
2016 * @as: #AddressSpace to be accessed
2017 * @addr: address within that address space
2018 * @attrs: memory transaction attributes
2019 * @buf: buffer with the data transferred
2021 static inline __attribute__((__always_inline__
))
2022 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
2023 MemTxAttrs attrs
, uint8_t *buf
,
2026 MemTxResult result
= MEMTX_OK
;
2032 if (__builtin_constant_p(len
)) {
2035 fv
= address_space_to_flatview(as
);
2037 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
2038 if (len
== l
&& memory_access_is_direct(mr
, false)) {
2039 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
2040 memcpy(buf
, ptr
, len
);
2042 result
= flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
2048 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
2054 * address_space_read_cached: read from a cached RAM region
2056 * @cache: Cached region to be addressed
2057 * @addr: address relative to the base of the RAM region
2058 * @buf: buffer with the data transferred
2059 * @len: length of the data transferred
2062 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2065 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2066 if (likely(cache
->ptr
)) {
2067 memcpy(buf
, cache
->ptr
+ addr
, len
);
2069 address_space_read_cached_slow(cache
, addr
, buf
, len
);
2074 * address_space_write_cached: write to a cached RAM region
2076 * @cache: Cached region to be addressed
2077 * @addr: address relative to the base of the RAM region
2078 * @buf: buffer with the data transferred
2079 * @len: length of the data transferred
2082 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2085 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2086 if (likely(cache
->ptr
)) {
2087 memcpy(cache
->ptr
+ addr
, buf
, len
);
2089 address_space_write_cached_slow(cache
, addr
, buf
, len
);