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 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
127 IOMMUNotifierFlag flags
,
128 hwaddr start
, hwaddr end
,
132 n
->notifier_flags
= flags
;
135 n
->iommu_idx
= iommu_idx
;
139 * Memory region callbacks
141 struct MemoryRegionOps
{
142 /* Read from the memory region. @addr is relative to @mr; @size is
144 uint64_t (*read
)(void *opaque
,
147 /* Write to the memory region. @addr is relative to @mr; @size is
149 void (*write
)(void *opaque
,
154 MemTxResult (*read_with_attrs
)(void *opaque
,
159 MemTxResult (*write_with_attrs
)(void *opaque
,
164 /* Instruction execution pre-callback:
165 * @addr is the address of the access relative to the @mr.
166 * @size is the size of the area returned by the callback.
167 * @offset is the location of the pointer inside @mr.
169 * Returns a pointer to a location which contains guest code.
171 void *(*request_ptr
)(void *opaque
, hwaddr addr
, unsigned *size
,
174 enum device_endian endianness
;
175 /* Guest-visible constraints: */
177 /* If nonzero, specify bounds on access sizes beyond which a machine
180 unsigned min_access_size
;
181 unsigned max_access_size
;
182 /* If true, unaligned accesses are supported. Otherwise unaligned
183 * accesses throw machine checks.
187 * If present, and returns #false, the transaction is not accepted
188 * by the device (and results in machine dependent behaviour such
189 * as a machine check exception).
191 bool (*accepts
)(void *opaque
, hwaddr addr
,
192 unsigned size
, bool is_write
,
195 /* Internal implementation constraints: */
197 /* If nonzero, specifies the minimum size implemented. Smaller sizes
198 * will be rounded upwards and a partial result will be returned.
200 unsigned min_access_size
;
201 /* If nonzero, specifies the maximum size implemented. Larger sizes
202 * will be done as a series of accesses with smaller sizes.
204 unsigned max_access_size
;
205 /* If true, unaligned accesses are supported. Otherwise all accesses
206 * are converted to (possibly multiple) naturally aligned accesses.
211 /* If .read and .write are not present, old_mmio may be used for
212 * backwards compatibility with old mmio registration
214 const MemoryRegionMmio old_mmio
;
217 enum IOMMUMemoryRegionAttr
{
218 IOMMU_ATTR_SPAPR_TCE_FD
222 * IOMMUMemoryRegionClass:
224 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
225 * and provide an implementation of at least the @translate method here
226 * to handle requests to the memory region. Other methods are optional.
228 * The IOMMU implementation must use the IOMMU notifier infrastructure
229 * to report whenever mappings are changed, by calling
230 * memory_region_notify_iommu() (or, if necessary, by calling
231 * memory_region_notify_one() for each registered notifier).
233 * Conceptually an IOMMU provides a mapping from input address
234 * to an output TLB entry. If the IOMMU is aware of memory transaction
235 * attributes and the output TLB entry depends on the transaction
236 * attributes, we represent this using IOMMU indexes. Each index
237 * selects a particular translation table that the IOMMU has:
238 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
239 * @translate takes an input address and an IOMMU index
240 * and the mapping returned can only depend on the input address and the
243 * Most IOMMUs don't care about the transaction attributes and support
244 * only a single IOMMU index. A more complex IOMMU might have one index
245 * for secure transactions and one for non-secure transactions.
247 typedef struct IOMMUMemoryRegionClass
{
249 struct DeviceClass parent_class
;
252 * Return a TLB entry that contains a given address.
254 * The IOMMUAccessFlags indicated via @flag are optional and may
255 * be specified as IOMMU_NONE to indicate that the caller needs
256 * the full translation information for both reads and writes. If
257 * the access flags are specified then the IOMMU implementation
258 * may use this as an optimization, to stop doing a page table
259 * walk as soon as it knows that the requested permissions are not
260 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
261 * full page table walk and report the permissions in the returned
262 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
263 * return different mappings for reads and writes.)
265 * The returned information remains valid while the caller is
266 * holding the big QEMU lock or is inside an RCU critical section;
267 * if the caller wishes to cache the mapping beyond that it must
268 * register an IOMMU notifier so it can invalidate its cached
269 * information when the IOMMU mapping changes.
271 * @iommu: the IOMMUMemoryRegion
272 * @hwaddr: address to be translated within the memory region
273 * @flag: requested access permissions
274 * @iommu_idx: IOMMU index for the translation
276 IOMMUTLBEntry (*translate
)(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
277 IOMMUAccessFlags flag
, int iommu_idx
);
278 /* Returns minimum supported page size in bytes.
279 * If this method is not provided then the minimum is assumed to
280 * be TARGET_PAGE_SIZE.
282 * @iommu: the IOMMUMemoryRegion
284 uint64_t (*get_min_page_size
)(IOMMUMemoryRegion
*iommu
);
285 /* Called when IOMMU Notifier flag changes (ie when the set of
286 * events which IOMMU users are requesting notification for changes).
287 * Optional method -- need not be provided if the IOMMU does not
288 * need to know exactly which events must be notified.
290 * @iommu: the IOMMUMemoryRegion
291 * @old_flags: events which previously needed to be notified
292 * @new_flags: events which now need to be notified
294 void (*notify_flag_changed
)(IOMMUMemoryRegion
*iommu
,
295 IOMMUNotifierFlag old_flags
,
296 IOMMUNotifierFlag new_flags
);
297 /* Called to handle memory_region_iommu_replay().
299 * The default implementation of memory_region_iommu_replay() is to
300 * call the IOMMU translate method for every page in the address space
301 * with flag == IOMMU_NONE and then call the notifier if translate
302 * returns a valid mapping. If this method is implemented then it
303 * overrides the default behaviour, and must provide the full semantics
304 * of memory_region_iommu_replay(), by calling @notifier for every
305 * translation present in the IOMMU.
307 * Optional method -- an IOMMU only needs to provide this method
308 * if the default is inefficient or produces undesirable side effects.
310 * Note: this is not related to record-and-replay functionality.
312 void (*replay
)(IOMMUMemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
314 /* Get IOMMU misc attributes. This is an optional method that
315 * can be used to allow users of the IOMMU to get implementation-specific
316 * information. The IOMMU implements this method to handle calls
317 * by IOMMU users to memory_region_iommu_get_attr() by filling in
318 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
319 * the IOMMU supports. If the method is unimplemented then
320 * memory_region_iommu_get_attr() will always return -EINVAL.
322 * @iommu: the IOMMUMemoryRegion
323 * @attr: attribute being queried
324 * @data: memory to fill in with the attribute data
326 * Returns 0 on success, or a negative errno; in particular
327 * returns -EINVAL for unrecognized or unimplemented attribute types.
329 int (*get_attr
)(IOMMUMemoryRegion
*iommu
, enum IOMMUMemoryRegionAttr attr
,
332 /* Return the IOMMU index to use for a given set of transaction attributes.
334 * Optional method: if an IOMMU only supports a single IOMMU index then
335 * the default implementation of memory_region_iommu_attrs_to_index()
338 * The indexes supported by an IOMMU must be contiguous, starting at 0.
340 * @iommu: the IOMMUMemoryRegion
341 * @attrs: memory transaction attributes
343 int (*attrs_to_index
)(IOMMUMemoryRegion
*iommu
, MemTxAttrs attrs
);
345 /* Return the number of IOMMU indexes this IOMMU supports.
347 * Optional method: if this method is not provided, then
348 * memory_region_iommu_num_indexes() will return 1, indicating that
349 * only a single IOMMU index is supported.
351 * @iommu: the IOMMUMemoryRegion
353 int (*num_indexes
)(IOMMUMemoryRegion
*iommu
);
354 } IOMMUMemoryRegionClass
;
356 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
357 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
359 struct MemoryRegion
{
362 /* All fields are private - violators will be prosecuted */
364 /* The following fields should fit in a cache line */
368 bool readonly
; /* For RAM regions */
370 bool flush_coalesced_mmio
;
372 uint8_t dirty_log_mask
;
377 const MemoryRegionOps
*ops
;
379 MemoryRegion
*container
;
382 void (*destructor
)(MemoryRegion
*mr
);
387 bool warning_printed
; /* For reservations */
388 uint8_t vga_logging_count
;
392 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
393 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
394 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
396 unsigned ioeventfd_nb
;
397 MemoryRegionIoeventfd
*ioeventfds
;
400 struct IOMMUMemoryRegion
{
401 MemoryRegion parent_obj
;
403 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
404 IOMMUNotifierFlag iommu_notify_flags
;
407 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
408 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
411 * MemoryListener: callbacks structure for updates to the physical memory map
413 * Allows a component to adjust to changes in the guest-visible memory map.
414 * Use with memory_listener_register() and memory_listener_unregister().
416 struct MemoryListener
{
417 void (*begin
)(MemoryListener
*listener
);
418 void (*commit
)(MemoryListener
*listener
);
419 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
420 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
421 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
422 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
424 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
426 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
427 void (*log_global_start
)(MemoryListener
*listener
);
428 void (*log_global_stop
)(MemoryListener
*listener
);
429 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
430 bool match_data
, uint64_t data
, EventNotifier
*e
);
431 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
432 bool match_data
, uint64_t data
, EventNotifier
*e
);
433 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
434 hwaddr addr
, hwaddr len
);
435 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
436 hwaddr addr
, hwaddr len
);
437 /* Lower = earlier (during add), later (during del) */
439 AddressSpace
*address_space
;
440 QTAILQ_ENTRY(MemoryListener
) link
;
441 QTAILQ_ENTRY(MemoryListener
) link_as
;
445 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
447 struct AddressSpace
{
448 /* All fields are private. */
453 /* Accessed via RCU. */
454 struct FlatView
*current_map
;
457 struct MemoryRegionIoeventfd
*ioeventfds
;
458 QTAILQ_HEAD(memory_listeners_as
, MemoryListener
) listeners
;
459 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
462 typedef struct AddressSpaceDispatch AddressSpaceDispatch
;
463 typedef struct FlatRange FlatRange
;
465 /* Flattened global view of current active memory hierarchy. Kept in sorted
473 unsigned nr_allocated
;
474 struct AddressSpaceDispatch
*dispatch
;
478 static inline FlatView
*address_space_to_flatview(AddressSpace
*as
)
480 return atomic_rcu_read(&as
->current_map
);
485 * MemoryRegionSection: describes a fragment of a #MemoryRegion
487 * @mr: the region, or %NULL if empty
488 * @fv: the flat view of the address space the region is mapped in
489 * @offset_within_region: the beginning of the section, relative to @mr's start
490 * @size: the size of the section; will not exceed @mr's boundaries
491 * @offset_within_address_space: the address of the first byte of the section
492 * relative to the region's address space
493 * @readonly: writes to this section are ignored
495 struct MemoryRegionSection
{
498 hwaddr offset_within_region
;
500 hwaddr offset_within_address_space
;
505 * memory_region_init: Initialize a memory region
507 * The region typically acts as a container for other memory regions. Use
508 * memory_region_add_subregion() to add subregions.
510 * @mr: the #MemoryRegion to be initialized
511 * @owner: the object that tracks the region's reference count
512 * @name: used for debugging; not visible to the user or ABI
513 * @size: size of the region; any subregions beyond this size will be clipped
515 void memory_region_init(MemoryRegion
*mr
,
516 struct Object
*owner
,
521 * memory_region_ref: Add 1 to a memory region's reference count
523 * Whenever memory regions are accessed outside the BQL, they need to be
524 * preserved against hot-unplug. MemoryRegions actually do not have their
525 * own reference count; they piggyback on a QOM object, their "owner".
526 * This function adds a reference to the owner.
528 * All MemoryRegions must have an owner if they can disappear, even if the
529 * device they belong to operates exclusively under the BQL. This is because
530 * the region could be returned at any time by memory_region_find, and this
531 * is usually under guest control.
533 * @mr: the #MemoryRegion
535 void memory_region_ref(MemoryRegion
*mr
);
538 * memory_region_unref: Remove 1 to a memory region's reference count
540 * Whenever memory regions are accessed outside the BQL, they need to be
541 * preserved against hot-unplug. MemoryRegions actually do not have their
542 * own reference count; they piggyback on a QOM object, their "owner".
543 * This function removes a reference to the owner and possibly destroys it.
545 * @mr: the #MemoryRegion
547 void memory_region_unref(MemoryRegion
*mr
);
550 * memory_region_init_io: Initialize an I/O memory region.
552 * Accesses into the region will cause the callbacks in @ops to be called.
553 * if @size is nonzero, subregions will be clipped to @size.
555 * @mr: the #MemoryRegion to be initialized.
556 * @owner: the object that tracks the region's reference count
557 * @ops: a structure containing read and write callbacks to be used when
558 * I/O is performed on the region.
559 * @opaque: passed to the read and write callbacks of the @ops structure.
560 * @name: used for debugging; not visible to the user or ABI
561 * @size: size of the region.
563 void memory_region_init_io(MemoryRegion
*mr
,
564 struct Object
*owner
,
565 const MemoryRegionOps
*ops
,
571 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
572 * into the region will modify memory
575 * @mr: the #MemoryRegion to be initialized.
576 * @owner: the object that tracks the region's reference count
577 * @name: Region name, becomes part of RAMBlock name used in migration stream
578 * must be unique within any device
579 * @size: size of the region.
580 * @errp: pointer to Error*, to store an error if it happens.
582 * Note that this function does not do anything to cause the data in the
583 * RAM memory region to be migrated; that is the responsibility of the caller.
585 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
586 struct Object
*owner
,
592 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
593 * Accesses into the region will
594 * modify memory directly.
596 * @mr: the #MemoryRegion to be initialized.
597 * @owner: the object that tracks the region's reference count
598 * @name: Region name, becomes part of RAMBlock name used in migration stream
599 * must be unique within any device
600 * @size: size of the region.
601 * @share: allow remapping RAM to different addresses
602 * @errp: pointer to Error*, to store an error if it happens.
604 * Note that this function is similar to memory_region_init_ram_nomigrate.
605 * The only difference is part of the RAM region can be remapped.
607 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
608 struct Object
*owner
,
615 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
616 * RAM. Accesses into the region will
617 * modify memory directly. Only an initial
618 * portion of this RAM is actually used.
619 * The used size can change across reboots.
621 * @mr: the #MemoryRegion to be initialized.
622 * @owner: the object that tracks the region's reference count
623 * @name: Region name, becomes part of RAMBlock name used in migration stream
624 * must be unique within any device
625 * @size: used size of the region.
626 * @max_size: max size of the region.
627 * @resized: callback to notify owner about used size change.
628 * @errp: pointer to Error*, to store an error if it happens.
630 * Note that this function does not do anything to cause the data in the
631 * RAM memory region to be migrated; that is the responsibility of the caller.
633 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
634 struct Object
*owner
,
638 void (*resized
)(const char*,
645 * memory_region_init_ram_from_file: Initialize RAM memory region with a
648 * @mr: the #MemoryRegion to be initialized.
649 * @owner: the object that tracks the region's reference count
650 * @name: Region name, becomes part of RAMBlock name used in migration stream
651 * must be unique within any device
652 * @size: size of the region.
653 * @align: alignment of the region base address; if 0, the default alignment
654 * (getpagesize()) will be used.
655 * @ram_flags: Memory region features:
656 * - RAM_SHARED: memory must be mmaped with the MAP_SHARED flag
657 * Other bits are ignored now.
658 * @path: the path in which to allocate the RAM.
659 * @errp: pointer to Error*, to store an error if it happens.
661 * Note that this function does not do anything to cause the data in the
662 * RAM memory region to be migrated; that is the responsibility of the caller.
664 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
665 struct Object
*owner
,
674 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
677 * @mr: the #MemoryRegion to be initialized.
678 * @owner: the object that tracks the region's reference count
679 * @name: the name of the region.
680 * @size: size of the region.
681 * @share: %true if memory must be mmaped with the MAP_SHARED flag
682 * @fd: the fd to mmap.
683 * @errp: pointer to Error*, to store an error if it happens.
685 * Note that this function does not do anything to cause the data in the
686 * RAM memory region to be migrated; that is the responsibility of the caller.
688 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
689 struct Object
*owner
,
698 * memory_region_init_ram_ptr: Initialize RAM memory region from a
699 * user-provided pointer. Accesses into the
700 * region will modify memory directly.
702 * @mr: the #MemoryRegion to be initialized.
703 * @owner: the object that tracks the region's reference count
704 * @name: Region name, becomes part of RAMBlock name used in migration stream
705 * must be unique within any device
706 * @size: size of the region.
707 * @ptr: memory to be mapped; must contain at least @size bytes.
709 * Note that this function does not do anything to cause the data in the
710 * RAM memory region to be migrated; that is the responsibility of the caller.
712 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
713 struct Object
*owner
,
719 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
720 * a user-provided pointer.
722 * A RAM device represents a mapping to a physical device, such as to a PCI
723 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
724 * into the VM address space and access to the region will modify memory
725 * directly. However, the memory region should not be included in a memory
726 * dump (device may not be enabled/mapped at the time of the dump), and
727 * operations incompatible with manipulating MMIO should be avoided. Replaces
730 * @mr: the #MemoryRegion to be initialized.
731 * @owner: the object that tracks the region's reference count
732 * @name: the name of the region.
733 * @size: size of the region.
734 * @ptr: memory to be mapped; must contain at least @size bytes.
736 * Note that this function does not do anything to cause the data in the
737 * RAM memory region to be migrated; that is the responsibility of the caller.
738 * (For RAM device memory regions, migrating the contents rarely makes sense.)
740 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
741 struct Object
*owner
,
747 * memory_region_init_alias: Initialize a memory region that aliases all or a
748 * part of another memory region.
750 * @mr: the #MemoryRegion to be initialized.
751 * @owner: the object that tracks the region's reference count
752 * @name: used for debugging; not visible to the user or ABI
753 * @orig: the region to be referenced; @mr will be equivalent to
754 * @orig between @offset and @offset + @size - 1.
755 * @offset: start of the section in @orig to be referenced.
756 * @size: size of the region.
758 void memory_region_init_alias(MemoryRegion
*mr
,
759 struct Object
*owner
,
766 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
768 * This has the same effect as calling memory_region_init_ram_nomigrate()
769 * and then marking the resulting region read-only with
770 * memory_region_set_readonly().
772 * Note that this function does not do anything to cause the data in the
773 * RAM side of the memory region to be migrated; that is the responsibility
776 * @mr: the #MemoryRegion to be initialized.
777 * @owner: the object that tracks the region's reference count
778 * @name: Region name, becomes part of RAMBlock name used in migration stream
779 * must be unique within any device
780 * @size: size of the region.
781 * @errp: pointer to Error*, to store an error if it happens.
783 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
784 struct Object
*owner
,
790 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
791 * Writes are handled via callbacks.
793 * Note that this function does not do anything to cause the data in the
794 * RAM side of the memory region to be migrated; that is the responsibility
797 * @mr: the #MemoryRegion to be initialized.
798 * @owner: the object that tracks the region's reference count
799 * @ops: callbacks for write access handling (must not be NULL).
800 * @opaque: passed to the read and write callbacks of the @ops structure.
801 * @name: Region name, becomes part of RAMBlock name used in migration stream
802 * must be unique within any device
803 * @size: size of the region.
804 * @errp: pointer to Error*, to store an error if it happens.
806 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
807 struct Object
*owner
,
808 const MemoryRegionOps
*ops
,
815 * memory_region_init_iommu: Initialize a memory region of a custom type
816 * that translates addresses
818 * An IOMMU region translates addresses and forwards accesses to a target
821 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
822 * @_iommu_mr should be a pointer to enough memory for an instance of
823 * that subclass, @instance_size is the size of that subclass, and
824 * @mrtypename is its name. This function will initialize @_iommu_mr as an
825 * instance of the subclass, and its methods will then be called to handle
826 * accesses to the memory region. See the documentation of
827 * #IOMMUMemoryRegionClass for further details.
829 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
830 * @instance_size: the IOMMUMemoryRegion subclass instance size
831 * @mrtypename: the type name of the #IOMMUMemoryRegion
832 * @owner: the object that tracks the region's reference count
833 * @name: used for debugging; not visible to the user or ABI
834 * @size: size of the region.
836 void memory_region_init_iommu(void *_iommu_mr
,
837 size_t instance_size
,
838 const char *mrtypename
,
844 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
845 * region will modify memory directly.
847 * @mr: the #MemoryRegion to be initialized
848 * @owner: the object that tracks the region's reference count (must be
849 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
850 * @name: name of the memory region
851 * @size: size of the region in bytes
852 * @errp: pointer to Error*, to store an error if it happens.
854 * This function allocates RAM for a board model or device, and
855 * arranges for it to be migrated (by calling vmstate_register_ram()
856 * if @owner is a DeviceState, or vmstate_register_ram_global() if
859 * TODO: Currently we restrict @owner to being either NULL (for
860 * global RAM regions with no owner) or devices, so that we can
861 * give the RAM block a unique name for migration purposes.
862 * We should lift this restriction and allow arbitrary Objects.
863 * If you pass a non-NULL non-device @owner then we will assert.
865 void memory_region_init_ram(MemoryRegion
*mr
,
866 struct Object
*owner
,
872 * memory_region_init_rom: Initialize a ROM memory region.
874 * This has the same effect as calling memory_region_init_ram()
875 * and then marking the resulting region read-only with
876 * memory_region_set_readonly(). This includes arranging for the
877 * contents to be migrated.
879 * TODO: Currently we restrict @owner to being either NULL (for
880 * global RAM regions with no owner) or devices, so that we can
881 * give the RAM block a unique name for migration purposes.
882 * We should lift this restriction and allow arbitrary Objects.
883 * If you pass a non-NULL non-device @owner then we will assert.
885 * @mr: the #MemoryRegion to be initialized.
886 * @owner: the object that tracks the region's reference count
887 * @name: Region name, becomes part of RAMBlock name used in migration stream
888 * must be unique within any device
889 * @size: size of the region.
890 * @errp: pointer to Error*, to store an error if it happens.
892 void memory_region_init_rom(MemoryRegion
*mr
,
893 struct Object
*owner
,
899 * memory_region_init_rom_device: Initialize a ROM memory region.
900 * Writes are handled via callbacks.
902 * This function initializes a memory region backed by RAM for reads
903 * and callbacks for writes, and arranges for the RAM backing to
904 * be migrated (by calling vmstate_register_ram()
905 * if @owner is a DeviceState, or vmstate_register_ram_global() if
908 * TODO: Currently we restrict @owner to being either NULL (for
909 * global RAM regions with no owner) or devices, so that we can
910 * give the RAM block a unique name for migration purposes.
911 * We should lift this restriction and allow arbitrary Objects.
912 * If you pass a non-NULL non-device @owner then we will assert.
914 * @mr: the #MemoryRegion to be initialized.
915 * @owner: the object that tracks the region's reference count
916 * @ops: callbacks for write access handling (must not be NULL).
917 * @name: Region name, becomes part of RAMBlock name used in migration stream
918 * must be unique within any device
919 * @size: size of the region.
920 * @errp: pointer to Error*, to store an error if it happens.
922 void memory_region_init_rom_device(MemoryRegion
*mr
,
923 struct Object
*owner
,
924 const MemoryRegionOps
*ops
,
932 * memory_region_owner: get a memory region's owner.
934 * @mr: the memory region being queried.
936 struct Object
*memory_region_owner(MemoryRegion
*mr
);
939 * memory_region_size: get a memory region's size.
941 * @mr: the memory region being queried.
943 uint64_t memory_region_size(MemoryRegion
*mr
);
946 * memory_region_is_ram: check whether a memory region is random access
948 * Returns %true is a memory region is random access.
950 * @mr: the memory region being queried
952 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
958 * memory_region_is_ram_device: check whether a memory region is a ram device
960 * Returns %true is a memory region is a device backed ram region
962 * @mr: the memory region being queried
964 bool memory_region_is_ram_device(MemoryRegion
*mr
);
967 * memory_region_is_romd: check whether a memory region is in ROMD mode
969 * Returns %true if a memory region is a ROM device and currently set to allow
972 * @mr: the memory region being queried
974 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
976 return mr
->rom_device
&& mr
->romd_mode
;
980 * memory_region_get_iommu: check whether a memory region is an iommu
982 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
985 * @mr: the memory region being queried
987 static inline IOMMUMemoryRegion
*memory_region_get_iommu(MemoryRegion
*mr
)
990 return memory_region_get_iommu(mr
->alias
);
993 return (IOMMUMemoryRegion
*) mr
;
999 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
1000 * if an iommu or NULL if not
1002 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
1003 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1005 * @mr: the memory region being queried
1007 static inline IOMMUMemoryRegionClass
*memory_region_get_iommu_class_nocheck(
1008 IOMMUMemoryRegion
*iommu_mr
)
1010 return (IOMMUMemoryRegionClass
*) (((Object
*)iommu_mr
)->class);
1013 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1016 * memory_region_iommu_get_min_page_size: get minimum supported page size
1019 * Returns minimum supported page size for an iommu.
1021 * @iommu_mr: the memory region being queried
1023 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
);
1026 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1028 * The notification type will be decided by entry.perm bits:
1030 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
1031 * - For MAP (newly added entry) notifies: set entry.perm to the
1032 * permission of the page (which is definitely !IOMMU_NONE).
1034 * Note: for any IOMMU implementation, an in-place mapping change
1035 * should be notified with an UNMAP followed by a MAP.
1037 * @iommu_mr: the memory region that was changed
1038 * @iommu_idx: the IOMMU index for the translation table which has changed
1039 * @entry: the new entry in the IOMMU translation table. The entry
1040 * replaces all old entries for the same virtual I/O address range.
1041 * Deleted entries have .@perm == 0.
1043 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1045 IOMMUTLBEntry entry
);
1048 * memory_region_notify_one: notify a change in an IOMMU translation
1049 * entry to a single notifier
1051 * This works just like memory_region_notify_iommu(), but it only
1052 * notifies a specific notifier, not all of them.
1054 * @notifier: the notifier to be notified
1055 * @entry: the new entry in the IOMMU translation table. The entry
1056 * replaces all old entries for the same virtual I/O address range.
1057 * Deleted entries have .@perm == 0.
1059 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1060 IOMMUTLBEntry
*entry
);
1063 * memory_region_register_iommu_notifier: register a notifier for changes to
1064 * IOMMU translation entries.
1066 * @mr: the memory region to observe
1067 * @n: the IOMMUNotifier to be added; the notify callback receives a
1068 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1069 * ceases to be valid on exit from the notifier.
1071 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1075 * memory_region_iommu_replay: replay existing IOMMU translations to
1076 * a notifier with the minimum page granularity returned by
1077 * mr->iommu_ops->get_page_size().
1079 * Note: this is not related to record-and-replay functionality.
1081 * @iommu_mr: the memory region to observe
1082 * @n: the notifier to which to replay iommu mappings
1084 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
);
1087 * memory_region_iommu_replay_all: replay existing IOMMU translations
1088 * to all the notifiers registered.
1090 * Note: this is not related to record-and-replay functionality.
1092 * @iommu_mr: the memory region to observe
1094 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
);
1097 * memory_region_unregister_iommu_notifier: unregister a notifier for
1098 * changes to IOMMU translation entries.
1100 * @mr: the memory region which was observed and for which notity_stopped()
1101 * needs to be called
1102 * @n: the notifier to be removed.
1104 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1108 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1109 * defined on the IOMMU.
1111 * Returns 0 on success, or a negative errno otherwise. In particular,
1112 * -EINVAL indicates that the IOMMU does not support the requested
1115 * @iommu_mr: the memory region
1116 * @attr: the requested attribute
1117 * @data: a pointer to the requested attribute data
1119 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1120 enum IOMMUMemoryRegionAttr attr
,
1124 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1125 * use for translations with the given memory transaction attributes.
1127 * @iommu_mr: the memory region
1128 * @attrs: the memory transaction attributes
1130 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1134 * memory_region_iommu_num_indexes: return the total number of IOMMU
1135 * indexes that this IOMMU supports.
1137 * @iommu_mr: the memory region
1139 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
);
1142 * memory_region_name: get a memory region's name
1144 * Returns the string that was used to initialize the memory region.
1146 * @mr: the memory region being queried
1148 const char *memory_region_name(const MemoryRegion
*mr
);
1151 * memory_region_is_logging: return whether a memory region is logging writes
1153 * Returns %true if the memory region is logging writes for the given client
1155 * @mr: the memory region being queried
1156 * @client: the client being queried
1158 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
1161 * memory_region_get_dirty_log_mask: return the clients for which a
1162 * memory region is logging writes.
1164 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1165 * are the bit indices.
1167 * @mr: the memory region being queried
1169 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
1172 * memory_region_is_rom: check whether a memory region is ROM
1174 * Returns %true is a memory region is read-only memory.
1176 * @mr: the memory region being queried
1178 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
1180 return mr
->ram
&& mr
->readonly
;
1185 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1187 * Returns a file descriptor backing a file-based RAM memory region,
1188 * or -1 if the region is not a file-based RAM memory region.
1190 * @mr: the RAM or alias memory region being queried.
1192 int memory_region_get_fd(MemoryRegion
*mr
);
1195 * memory_region_from_host: Convert a pointer into a RAM memory region
1196 * and an offset within it.
1198 * Given a host pointer inside a RAM memory region (created with
1199 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1200 * the MemoryRegion and the offset within it.
1202 * Use with care; by the time this function returns, the returned pointer is
1203 * not protected by RCU anymore. If the caller is not within an RCU critical
1204 * section and does not hold the iothread lock, it must have other means of
1205 * protecting the pointer, such as a reference to the region that includes
1206 * the incoming ram_addr_t.
1208 * @ptr: the host pointer to be converted
1209 * @offset: the offset within memory region
1211 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
1214 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1216 * Returns a host pointer to a RAM memory region (created with
1217 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1219 * Use with care; by the time this function returns, the returned pointer is
1220 * not protected by RCU anymore. If the caller is not within an RCU critical
1221 * section and does not hold the iothread lock, it must have other means of
1222 * protecting the pointer, such as a reference to the region that includes
1223 * the incoming ram_addr_t.
1225 * @mr: the memory region being queried.
1227 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
1229 /* memory_region_ram_resize: Resize a RAM region.
1231 * Only legal before guest might have detected the memory size: e.g. on
1232 * incoming migration, or right after reset.
1234 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1235 * @newsize: the new size the region
1236 * @errp: pointer to Error*, to store an error if it happens.
1238 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
1242 * memory_region_set_log: Turn dirty logging on or off for a region.
1244 * Turns dirty logging on or off for a specified client (display, migration).
1245 * Only meaningful for RAM regions.
1247 * @mr: the memory region being updated.
1248 * @log: whether dirty logging is to be enabled or disabled.
1249 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1251 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
1254 * memory_region_get_dirty: Check whether a range of bytes is dirty
1255 * for a specified client.
1257 * Checks whether a range of bytes has been written to since the last
1258 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1261 * @mr: the memory region being queried.
1262 * @addr: the address (relative to the start of the region) being queried.
1263 * @size: the size of the range being queried.
1264 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1265 * %DIRTY_MEMORY_VGA.
1267 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1268 hwaddr size
, unsigned client
);
1271 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1273 * Marks a range of bytes as dirty, after it has been dirtied outside
1276 * @mr: the memory region being dirtied.
1277 * @addr: the address (relative to the start of the region) being dirtied.
1278 * @size: size of the range being dirtied.
1280 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1284 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1285 * bitmap and clear it.
1287 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1288 * returns the snapshot. The snapshot can then be used to query dirty
1289 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1290 * querying the same page multiple times, which is especially useful for
1291 * display updates where the scanlines often are not page aligned.
1293 * The dirty bitmap region which gets copyed into the snapshot (and
1294 * cleared afterwards) can be larger than requested. The boundaries
1295 * are rounded up/down so complete bitmap longs (covering 64 pages on
1296 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1297 * isn't a problem for display updates as the extra pages are outside
1298 * the visible area, and in case the visible area changes a full
1299 * display redraw is due anyway. Should other use cases for this
1300 * function emerge we might have to revisit this implementation
1303 * Use g_free to release DirtyBitmapSnapshot.
1305 * @mr: the memory region being queried.
1306 * @addr: the address (relative to the start of the region) being queried.
1307 * @size: the size of the range being queried.
1308 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1310 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1316 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1317 * in the specified dirty bitmap snapshot.
1319 * @mr: the memory region being queried.
1320 * @snap: the dirty bitmap snapshot
1321 * @addr: the address (relative to the start of the region) being queried.
1322 * @size: the size of the range being queried.
1324 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
1325 DirtyBitmapSnapshot
*snap
,
1326 hwaddr addr
, hwaddr size
);
1329 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1332 * Marks a range of pages as no longer dirty.
1334 * @mr: the region being updated.
1335 * @addr: the start of the subrange being cleaned.
1336 * @size: the size of the subrange being cleaned.
1337 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1338 * %DIRTY_MEMORY_VGA.
1340 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1341 hwaddr size
, unsigned client
);
1344 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1346 * Allows a memory region to be marked as read-only (turning it into a ROM).
1347 * only useful on RAM regions.
1349 * @mr: the region being updated.
1350 * @readonly: whether rhe region is to be ROM or RAM.
1352 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
1355 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1357 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1358 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1359 * device is mapped to guest memory and satisfies read access directly.
1360 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1361 * Writes are always handled by the #MemoryRegion.write function.
1363 * @mr: the memory region to be updated
1364 * @romd_mode: %true to put the region into ROMD mode
1366 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
1369 * memory_region_set_coalescing: Enable memory coalescing for the region.
1371 * Enabled writes to a region to be queued for later processing. MMIO ->write
1372 * callbacks may be delayed until a non-coalesced MMIO is issued.
1373 * Only useful for IO regions. Roughly similar to write-combining hardware.
1375 * @mr: the memory region to be write coalesced
1377 void memory_region_set_coalescing(MemoryRegion
*mr
);
1380 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1383 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1384 * Multiple calls can be issued coalesced disjoint ranges.
1386 * @mr: the memory region to be updated.
1387 * @offset: the start of the range within the region to be coalesced.
1388 * @size: the size of the subrange to be coalesced.
1390 void memory_region_add_coalescing(MemoryRegion
*mr
,
1395 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1397 * Disables any coalescing caused by memory_region_set_coalescing() or
1398 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1401 * @mr: the memory region to be updated.
1403 void memory_region_clear_coalescing(MemoryRegion
*mr
);
1406 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1409 * Ensure that pending coalesced MMIO request are flushed before the memory
1410 * region is accessed. This property is automatically enabled for all regions
1411 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1413 * @mr: the memory region to be updated.
1415 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
1418 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1421 * Clear the automatic coalesced MMIO flushing enabled via
1422 * memory_region_set_flush_coalesced. Note that this service has no effect on
1423 * memory regions that have MMIO coalescing enabled for themselves. For them,
1424 * automatic flushing will stop once coalescing is disabled.
1426 * @mr: the memory region to be updated.
1428 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1431 * memory_region_clear_global_locking: Declares that access processing does
1432 * not depend on the QEMU global lock.
1434 * By clearing this property, accesses to the memory region will be processed
1435 * outside of QEMU's global lock (unless the lock is held on when issuing the
1436 * access request). In this case, the device model implementing the access
1437 * handlers is responsible for synchronization of concurrency.
1439 * @mr: the memory region to be updated.
1441 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1444 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1445 * is written to a location.
1447 * Marks a word in an IO region (initialized with memory_region_init_io())
1448 * as a trigger for an eventfd event. The I/O callback will not be called.
1449 * The caller must be prepared to handle failure (that is, take the required
1450 * action if the callback _is_ called).
1452 * @mr: the memory region being updated.
1453 * @addr: the address within @mr that is to be monitored
1454 * @size: the size of the access to trigger the eventfd
1455 * @match_data: whether to match against @data, instead of just @addr
1456 * @data: the data to match against the guest write
1457 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1459 void memory_region_add_eventfd(MemoryRegion
*mr
,
1467 * memory_region_del_eventfd: Cancel an eventfd.
1469 * Cancels an eventfd trigger requested by a previous
1470 * memory_region_add_eventfd() call.
1472 * @mr: the memory region being updated.
1473 * @addr: the address within @mr that is to be monitored
1474 * @size: the size of the access to trigger the eventfd
1475 * @match_data: whether to match against @data, instead of just @addr
1476 * @data: the data to match against the guest write
1477 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1479 void memory_region_del_eventfd(MemoryRegion
*mr
,
1487 * memory_region_add_subregion: Add a subregion to a container.
1489 * Adds a subregion at @offset. The subregion may not overlap with other
1490 * subregions (except for those explicitly marked as overlapping). A region
1491 * may only be added once as a subregion (unless removed with
1492 * memory_region_del_subregion()); use memory_region_init_alias() if you
1493 * want a region to be a subregion in multiple locations.
1495 * @mr: the region to contain the new subregion; must be a container
1496 * initialized with memory_region_init().
1497 * @offset: the offset relative to @mr where @subregion is added.
1498 * @subregion: the subregion to be added.
1500 void memory_region_add_subregion(MemoryRegion
*mr
,
1502 MemoryRegion
*subregion
);
1504 * memory_region_add_subregion_overlap: Add a subregion to a container
1507 * Adds a subregion at @offset. The subregion may overlap with other
1508 * subregions. Conflicts are resolved by having a higher @priority hide a
1509 * lower @priority. Subregions without priority are taken as @priority 0.
1510 * A region may only be added once as a subregion (unless removed with
1511 * memory_region_del_subregion()); use memory_region_init_alias() if you
1512 * want a region to be a subregion in multiple locations.
1514 * @mr: the region to contain the new subregion; must be a container
1515 * initialized with memory_region_init().
1516 * @offset: the offset relative to @mr where @subregion is added.
1517 * @subregion: the subregion to be added.
1518 * @priority: used for resolving overlaps; highest priority wins.
1520 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1522 MemoryRegion
*subregion
,
1526 * memory_region_get_ram_addr: Get the ram address associated with a memory
1529 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1531 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1533 * memory_region_del_subregion: Remove a subregion.
1535 * Removes a subregion from its container.
1537 * @mr: the container to be updated.
1538 * @subregion: the region being removed; must be a current subregion of @mr.
1540 void memory_region_del_subregion(MemoryRegion
*mr
,
1541 MemoryRegion
*subregion
);
1544 * memory_region_set_enabled: dynamically enable or disable a region
1546 * Enables or disables a memory region. A disabled memory region
1547 * ignores all accesses to itself and its subregions. It does not
1548 * obscure sibling subregions with lower priority - it simply behaves as
1549 * if it was removed from the hierarchy.
1551 * Regions default to being enabled.
1553 * @mr: the region to be updated
1554 * @enabled: whether to enable or disable the region
1556 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1559 * memory_region_set_address: dynamically update the address of a region
1561 * Dynamically updates the address of a region, relative to its container.
1562 * May be used on regions are currently part of a memory hierarchy.
1564 * @mr: the region to be updated
1565 * @addr: new address, relative to container region
1567 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1570 * memory_region_set_size: dynamically update the size of a region.
1572 * Dynamically updates the size of a region.
1574 * @mr: the region to be updated
1575 * @size: used size of the region.
1577 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1580 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1582 * Dynamically updates the offset into the target region that an alias points
1583 * to, as if the fourth argument to memory_region_init_alias() has changed.
1585 * @mr: the #MemoryRegion to be updated; should be an alias.
1586 * @offset: the new offset into the target memory region
1588 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1592 * memory_region_present: checks if an address relative to a @container
1593 * translates into #MemoryRegion within @container
1595 * Answer whether a #MemoryRegion within @container covers the address
1598 * @container: a #MemoryRegion within which @addr is a relative address
1599 * @addr: the area within @container to be searched
1601 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1604 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1605 * into any address space.
1607 * @mr: a #MemoryRegion which should be checked if it's mapped
1609 bool memory_region_is_mapped(MemoryRegion
*mr
);
1612 * memory_region_find: translate an address/size relative to a
1613 * MemoryRegion into a #MemoryRegionSection.
1615 * Locates the first #MemoryRegion within @mr that overlaps the range
1616 * given by @addr and @size.
1618 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1619 * It will have the following characteristics:
1620 * .@size = 0 iff no overlap was found
1621 * .@mr is non-%NULL iff an overlap was found
1623 * Remember that in the return value the @offset_within_region is
1624 * relative to the returned region (in the .@mr field), not to the
1627 * Similarly, the .@offset_within_address_space is relative to the
1628 * address space that contains both regions, the passed and the
1629 * returned one. However, in the special case where the @mr argument
1630 * has no container (and thus is the root of the address space), the
1631 * following will hold:
1632 * .@offset_within_address_space >= @addr
1633 * .@offset_within_address_space + .@size <= @addr + @size
1635 * @mr: a MemoryRegion within which @addr is a relative address
1636 * @addr: start of the area within @as to be searched
1637 * @size: size of the area to be searched
1639 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1640 hwaddr addr
, uint64_t size
);
1643 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1645 * Synchronizes the dirty page log for all address spaces.
1647 void memory_global_dirty_log_sync(void);
1650 * memory_region_transaction_begin: Start a transaction.
1652 * During a transaction, changes will be accumulated and made visible
1653 * only when the transaction ends (is committed).
1655 void memory_region_transaction_begin(void);
1658 * memory_region_transaction_commit: Commit a transaction and make changes
1659 * visible to the guest.
1661 void memory_region_transaction_commit(void);
1664 * memory_listener_register: register callbacks to be called when memory
1665 * sections are mapped or unmapped into an address
1668 * @listener: an object containing the callbacks to be called
1669 * @filter: if non-%NULL, only regions in this address space will be observed
1671 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1674 * memory_listener_unregister: undo the effect of memory_listener_register()
1676 * @listener: an object containing the callbacks to be removed
1678 void memory_listener_unregister(MemoryListener
*listener
);
1681 * memory_global_dirty_log_start: begin dirty logging for all regions
1683 void memory_global_dirty_log_start(void);
1686 * memory_global_dirty_log_stop: end dirty logging for all regions
1688 void memory_global_dirty_log_stop(void);
1690 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
,
1691 bool dispatch_tree
, bool owner
);
1694 * memory_region_request_mmio_ptr: request a pointer to an mmio
1695 * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer.
1696 * When the device wants to invalidate the pointer it will call
1697 * memory_region_invalidate_mmio_ptr.
1699 * @mr: #MemoryRegion to check
1700 * @addr: address within that region
1702 * Returns true on success, false otherwise.
1704 bool memory_region_request_mmio_ptr(MemoryRegion
*mr
, hwaddr addr
);
1707 * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio
1708 * previously requested.
1709 * In the end that means that if something wants to execute from this area it
1710 * will need to request the pointer again.
1712 * @mr: #MemoryRegion associated to the pointer.
1713 * @offset: offset within the memory region
1714 * @size: size of that area.
1716 void memory_region_invalidate_mmio_ptr(MemoryRegion
*mr
, hwaddr offset
,
1720 * memory_region_dispatch_read: perform a read directly to the specified
1723 * @mr: #MemoryRegion to access
1724 * @addr: address within that region
1725 * @pval: pointer to uint64_t which the data is written to
1726 * @size: size of the access in bytes
1727 * @attrs: memory transaction attributes to use for the access
1729 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1735 * memory_region_dispatch_write: perform a write directly to the specified
1738 * @mr: #MemoryRegion to access
1739 * @addr: address within that region
1740 * @data: data to write
1741 * @size: size of the access in bytes
1742 * @attrs: memory transaction attributes to use for the access
1744 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1751 * address_space_init: initializes an address space
1753 * @as: an uninitialized #AddressSpace
1754 * @root: a #MemoryRegion that routes addresses for the address space
1755 * @name: an address space name. The name is only used for debugging
1758 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1761 * address_space_destroy: destroy an address space
1763 * Releases all resources associated with an address space. After an address space
1764 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1767 * @as: address space to be destroyed
1769 void address_space_destroy(AddressSpace
*as
);
1772 * address_space_rw: read from or write to an address space.
1774 * Return a MemTxResult indicating whether the operation succeeded
1775 * or failed (eg unassigned memory, device rejected the transaction,
1778 * @as: #AddressSpace to be accessed
1779 * @addr: address within that address space
1780 * @attrs: memory transaction attributes
1781 * @buf: buffer with the data transferred
1782 * @len: the number of bytes to read or write
1783 * @is_write: indicates the transfer direction
1785 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1786 MemTxAttrs attrs
, uint8_t *buf
,
1787 int len
, bool is_write
);
1790 * address_space_write: write to address space.
1792 * Return a MemTxResult indicating whether the operation succeeded
1793 * or failed (eg unassigned memory, device rejected the transaction,
1796 * @as: #AddressSpace to be accessed
1797 * @addr: address within that address space
1798 * @attrs: memory transaction attributes
1799 * @buf: buffer with the data transferred
1800 * @len: the number of bytes to write
1802 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1804 const uint8_t *buf
, int len
);
1806 /* address_space_ld*: load from an address space
1807 * address_space_st*: store to an address space
1809 * These functions perform a load or store of the byte, word,
1810 * longword or quad to the specified address within the AddressSpace.
1811 * The _le suffixed functions treat the data as little endian;
1812 * _be indicates big endian; no suffix indicates "same endianness
1815 * The "guest CPU endianness" accessors are deprecated for use outside
1816 * target-* code; devices should be CPU-agnostic and use either the LE
1817 * or the BE accessors.
1819 * @as #AddressSpace to be accessed
1820 * @addr: address within that address space
1821 * @val: data value, for stores
1822 * @attrs: memory transaction attributes
1823 * @result: location to write the success/failure of the transaction;
1824 * if NULL, this information is discarded
1829 #define ARG1_DECL AddressSpace *as
1830 #include "exec/memory_ldst.inc.h"
1834 #define ARG1_DECL AddressSpace *as
1835 #include "exec/memory_ldst_phys.inc.h"
1837 struct MemoryRegionCache
{
1842 MemoryRegionSection mrs
;
1846 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
1849 /* address_space_ld*_cached: load from a cached #MemoryRegion
1850 * address_space_st*_cached: store into a cached #MemoryRegion
1852 * These functions perform a load or store of the byte, word,
1853 * longword or quad to the specified address. The address is
1854 * a physical address in the AddressSpace, but it must lie within
1855 * a #MemoryRegion that was mapped with address_space_cache_init.
1857 * The _le suffixed functions treat the data as little endian;
1858 * _be indicates big endian; no suffix indicates "same endianness
1861 * The "guest CPU endianness" accessors are deprecated for use outside
1862 * target-* code; devices should be CPU-agnostic and use either the LE
1863 * or the BE accessors.
1865 * @cache: previously initialized #MemoryRegionCache to be accessed
1866 * @addr: address within the address space
1867 * @val: data value, for stores
1868 * @attrs: memory transaction attributes
1869 * @result: location to write the success/failure of the transaction;
1870 * if NULL, this information is discarded
1873 #define SUFFIX _cached_slow
1875 #define ARG1_DECL MemoryRegionCache *cache
1876 #include "exec/memory_ldst.inc.h"
1878 /* Inline fast path for direct RAM access. */
1879 static inline uint8_t address_space_ldub_cached(MemoryRegionCache
*cache
,
1880 hwaddr addr
, MemTxAttrs attrs
, MemTxResult
*result
)
1882 assert(addr
< cache
->len
);
1883 if (likely(cache
->ptr
)) {
1884 return ldub_p(cache
->ptr
+ addr
);
1886 return address_space_ldub_cached_slow(cache
, addr
, attrs
, result
);
1890 static inline void address_space_stb_cached(MemoryRegionCache
*cache
,
1891 hwaddr addr
, uint32_t val
, MemTxAttrs attrs
, MemTxResult
*result
)
1893 assert(addr
< cache
->len
);
1894 if (likely(cache
->ptr
)) {
1895 stb_p(cache
->ptr
+ addr
, val
);
1897 address_space_stb_cached_slow(cache
, addr
, val
, attrs
, result
);
1901 #define ENDIANNESS _le
1902 #include "exec/memory_ldst_cached.inc.h"
1904 #define ENDIANNESS _be
1905 #include "exec/memory_ldst_cached.inc.h"
1907 #define SUFFIX _cached
1909 #define ARG1_DECL MemoryRegionCache *cache
1910 #include "exec/memory_ldst_phys.inc.h"
1912 /* address_space_cache_init: prepare for repeated access to a physical
1915 * @cache: #MemoryRegionCache to be filled
1916 * @as: #AddressSpace to be accessed
1917 * @addr: address within that address space
1918 * @len: length of buffer
1919 * @is_write: indicates the transfer direction
1921 * Will only work with RAM, and may map a subset of the requested range by
1922 * returning a value that is less than @len. On failure, return a negative
1925 * Because it only works with RAM, this function can be used for
1926 * read-modify-write operations. In this case, is_write should be %true.
1928 * Note that addresses passed to the address_space_*_cached functions
1929 * are relative to @addr.
1931 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1938 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1940 * @cache: The #MemoryRegionCache to operate on.
1941 * @addr: The first physical address that was written, relative to the
1942 * address that was passed to @address_space_cache_init.
1943 * @access_len: The number of bytes that were written starting at @addr.
1945 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1950 * address_space_cache_destroy: free a #MemoryRegionCache
1952 * @cache: The #MemoryRegionCache whose memory should be released.
1954 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1956 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1957 * entry. Should be called from an RCU critical section.
1959 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1960 bool is_write
, MemTxAttrs attrs
);
1962 /* address_space_translate: translate an address range into an address space
1963 * into a MemoryRegion and an address range into that section. Should be
1964 * called from an RCU critical section, to avoid that the last reference
1965 * to the returned region disappears after address_space_translate returns.
1967 * @fv: #FlatView to be accessed
1968 * @addr: address within that address space
1969 * @xlat: pointer to address within the returned memory region section's
1971 * @len: pointer to length
1972 * @is_write: indicates the transfer direction
1973 * @attrs: memory attributes
1975 MemoryRegion
*flatview_translate(FlatView
*fv
,
1976 hwaddr addr
, hwaddr
*xlat
,
1977 hwaddr
*len
, bool is_write
,
1980 static inline MemoryRegion
*address_space_translate(AddressSpace
*as
,
1981 hwaddr addr
, hwaddr
*xlat
,
1982 hwaddr
*len
, bool is_write
,
1985 return flatview_translate(address_space_to_flatview(as
),
1986 addr
, xlat
, len
, is_write
, attrs
);
1989 /* address_space_access_valid: check for validity of accessing an address
1992 * Check whether memory is assigned to the given address space range, and
1993 * access is permitted by any IOMMU regions that are active for the address
1996 * For now, addr and len should be aligned to a page size. This limitation
1997 * will be lifted in the future.
1999 * @as: #AddressSpace to be accessed
2000 * @addr: address within that address space
2001 * @len: length of the area to be checked
2002 * @is_write: indicates the transfer direction
2003 * @attrs: memory attributes
2005 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
,
2006 bool is_write
, MemTxAttrs attrs
);
2008 /* address_space_map: map a physical memory region into a host virtual address
2010 * May map a subset of the requested range, given by and returned in @plen.
2011 * May return %NULL if resources needed to perform the mapping are exhausted.
2012 * Use only for reads OR writes - not for read-modify-write operations.
2013 * Use cpu_register_map_client() to know when retrying the map operation is
2014 * likely to succeed.
2016 * @as: #AddressSpace to be accessed
2017 * @addr: address within that address space
2018 * @plen: pointer to length of buffer; updated on return
2019 * @is_write: indicates the transfer direction
2020 * @attrs: memory attributes
2022 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
2023 hwaddr
*plen
, bool is_write
, MemTxAttrs attrs
);
2025 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2027 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2028 * the amount of memory that was actually read or written by the caller.
2030 * @as: #AddressSpace used
2031 * @buffer: host pointer as returned by address_space_map()
2032 * @len: buffer length as returned by address_space_map()
2033 * @access_len: amount of data actually transferred
2034 * @is_write: indicates the transfer direction
2036 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
2037 int is_write
, hwaddr access_len
);
2040 /* Internal functions, part of the implementation of address_space_read. */
2041 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
2042 MemTxAttrs attrs
, uint8_t *buf
, int len
);
2043 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
2044 MemTxAttrs attrs
, uint8_t *buf
,
2045 int len
, hwaddr addr1
, hwaddr l
,
2047 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
2049 /* Internal functions, part of the implementation of address_space_read_cached
2050 * and address_space_write_cached. */
2051 void address_space_read_cached_slow(MemoryRegionCache
*cache
,
2052 hwaddr addr
, void *buf
, int len
);
2053 void address_space_write_cached_slow(MemoryRegionCache
*cache
,
2054 hwaddr addr
, const void *buf
, int len
);
2056 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
2059 return memory_region_is_ram(mr
) &&
2060 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
2062 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
2063 memory_region_is_romd(mr
);
2068 * address_space_read: read from an address space.
2070 * Return a MemTxResult indicating whether the operation succeeded
2071 * or failed (eg unassigned memory, device rejected the transaction,
2072 * IOMMU fault). Called within RCU critical section.
2074 * @as: #AddressSpace to be accessed
2075 * @addr: address within that address space
2076 * @attrs: memory transaction attributes
2077 * @buf: buffer with the data transferred
2079 static inline __attribute__((__always_inline__
))
2080 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
2081 MemTxAttrs attrs
, uint8_t *buf
,
2084 MemTxResult result
= MEMTX_OK
;
2090 if (__builtin_constant_p(len
)) {
2093 fv
= address_space_to_flatview(as
);
2095 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
2096 if (len
== l
&& memory_access_is_direct(mr
, false)) {
2097 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
2098 memcpy(buf
, ptr
, len
);
2100 result
= flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
2106 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
2112 * address_space_read_cached: read from a cached RAM region
2114 * @cache: Cached region to be addressed
2115 * @addr: address relative to the base of the RAM region
2116 * @buf: buffer with the data transferred
2117 * @len: length of the data transferred
2120 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2123 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2124 if (likely(cache
->ptr
)) {
2125 memcpy(buf
, cache
->ptr
+ addr
, len
);
2127 address_space_read_cached_slow(cache
, addr
, buf
, len
);
2132 * address_space_write_cached: write to a cached RAM region
2134 * @cache: Cached region to be addressed
2135 * @addr: address relative to the base of the RAM region
2136 * @buf: buffer with the data transferred
2137 * @len: length of the data transferred
2140 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2143 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2144 if (likely(cache
->ptr
)) {
2145 memcpy(cache
->ptr
+ addr
, buf
, len
);
2147 address_space_write_cached_slow(cache
, addr
, buf
, len
);