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 extern bool global_dirty_log
;
51 typedef struct MemoryRegionOps MemoryRegionOps
;
52 typedef struct MemoryRegionMmio MemoryRegionMmio
;
54 struct MemoryRegionMmio
{
55 CPUReadMemoryFunc
*read
[3];
56 CPUWriteMemoryFunc
*write
[3];
59 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
61 /* See address_space_translate: bit 0 is read, bit 1 is write. */
69 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
71 struct IOMMUTLBEntry
{
72 AddressSpace
*target_as
;
74 hwaddr translated_addr
;
75 hwaddr addr_mask
; /* 0xfff = 4k translation */
76 IOMMUAccessFlags perm
;
80 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
81 * register with one or multiple IOMMU Notifier capability bit(s).
84 IOMMU_NOTIFIER_NONE
= 0,
85 /* Notify cache invalidations */
86 IOMMU_NOTIFIER_UNMAP
= 0x1,
87 /* Notify entry changes (newly created entries) */
88 IOMMU_NOTIFIER_MAP
= 0x2,
91 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
94 typedef void (*IOMMUNotify
)(struct IOMMUNotifier
*notifier
,
97 struct IOMMUNotifier
{
99 IOMMUNotifierFlag notifier_flags
;
100 /* Notify for address space range start <= addr <= end */
104 QLIST_ENTRY(IOMMUNotifier
) node
;
106 typedef struct IOMMUNotifier IOMMUNotifier
;
108 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
109 #define RAM_PREALLOC (1 << 0)
111 /* RAM is mmap-ed with MAP_SHARED */
112 #define RAM_SHARED (1 << 1)
114 /* Only a portion of RAM (used_length) is actually used, and migrated.
115 * This used_length size can change across reboots.
117 #define RAM_RESIZEABLE (1 << 2)
119 /* UFFDIO_ZEROPAGE is available on this RAMBlock to atomically
120 * zero the page and wake waiting processes.
121 * (Set during postcopy)
123 #define RAM_UF_ZEROPAGE (1 << 3)
125 /* RAM can be migrated */
126 #define RAM_MIGRATABLE (1 << 4)
128 /* RAM is a persistent kind memory */
129 #define RAM_PMEM (1 << 5)
131 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
132 IOMMUNotifierFlag flags
,
133 hwaddr start
, hwaddr end
,
137 n
->notifier_flags
= flags
;
140 n
->iommu_idx
= iommu_idx
;
144 * Memory region callbacks
146 struct MemoryRegionOps
{
147 /* Read from the memory region. @addr is relative to @mr; @size is
149 uint64_t (*read
)(void *opaque
,
152 /* Write to the memory region. @addr is relative to @mr; @size is
154 void (*write
)(void *opaque
,
159 MemTxResult (*read_with_attrs
)(void *opaque
,
164 MemTxResult (*write_with_attrs
)(void *opaque
,
170 enum device_endian endianness
;
171 /* Guest-visible constraints: */
173 /* If nonzero, specify bounds on access sizes beyond which a machine
176 unsigned min_access_size
;
177 unsigned max_access_size
;
178 /* If true, unaligned accesses are supported. Otherwise unaligned
179 * accesses throw machine checks.
183 * If present, and returns #false, the transaction is not accepted
184 * by the device (and results in machine dependent behaviour such
185 * as a machine check exception).
187 bool (*accepts
)(void *opaque
, hwaddr addr
,
188 unsigned size
, bool is_write
,
191 /* Internal implementation constraints: */
193 /* If nonzero, specifies the minimum size implemented. Smaller sizes
194 * will be rounded upwards and a partial result will be returned.
196 unsigned min_access_size
;
197 /* If nonzero, specifies the maximum size implemented. Larger sizes
198 * will be done as a series of accesses with smaller sizes.
200 unsigned max_access_size
;
201 /* If true, unaligned accesses are supported. Otherwise all accesses
202 * are converted to (possibly multiple) naturally aligned accesses.
208 enum IOMMUMemoryRegionAttr
{
209 IOMMU_ATTR_SPAPR_TCE_FD
213 * IOMMUMemoryRegionClass:
215 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
216 * and provide an implementation of at least the @translate method here
217 * to handle requests to the memory region. Other methods are optional.
219 * The IOMMU implementation must use the IOMMU notifier infrastructure
220 * to report whenever mappings are changed, by calling
221 * memory_region_notify_iommu() (or, if necessary, by calling
222 * memory_region_notify_one() for each registered notifier).
224 * Conceptually an IOMMU provides a mapping from input address
225 * to an output TLB entry. If the IOMMU is aware of memory transaction
226 * attributes and the output TLB entry depends on the transaction
227 * attributes, we represent this using IOMMU indexes. Each index
228 * selects a particular translation table that the IOMMU has:
229 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
230 * @translate takes an input address and an IOMMU index
231 * and the mapping returned can only depend on the input address and the
234 * Most IOMMUs don't care about the transaction attributes and support
235 * only a single IOMMU index. A more complex IOMMU might have one index
236 * for secure transactions and one for non-secure transactions.
238 typedef struct IOMMUMemoryRegionClass
{
240 struct DeviceClass parent_class
;
243 * Return a TLB entry that contains a given address.
245 * The IOMMUAccessFlags indicated via @flag are optional and may
246 * be specified as IOMMU_NONE to indicate that the caller needs
247 * the full translation information for both reads and writes. If
248 * the access flags are specified then the IOMMU implementation
249 * may use this as an optimization, to stop doing a page table
250 * walk as soon as it knows that the requested permissions are not
251 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
252 * full page table walk and report the permissions in the returned
253 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
254 * return different mappings for reads and writes.)
256 * The returned information remains valid while the caller is
257 * holding the big QEMU lock or is inside an RCU critical section;
258 * if the caller wishes to cache the mapping beyond that it must
259 * register an IOMMU notifier so it can invalidate its cached
260 * information when the IOMMU mapping changes.
262 * @iommu: the IOMMUMemoryRegion
263 * @hwaddr: address to be translated within the memory region
264 * @flag: requested access permissions
265 * @iommu_idx: IOMMU index for the translation
267 IOMMUTLBEntry (*translate
)(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
268 IOMMUAccessFlags flag
, int iommu_idx
);
269 /* Returns minimum supported page size in bytes.
270 * If this method is not provided then the minimum is assumed to
271 * be TARGET_PAGE_SIZE.
273 * @iommu: the IOMMUMemoryRegion
275 uint64_t (*get_min_page_size
)(IOMMUMemoryRegion
*iommu
);
276 /* Called when IOMMU Notifier flag changes (ie when the set of
277 * events which IOMMU users are requesting notification for changes).
278 * Optional method -- need not be provided if the IOMMU does not
279 * need to know exactly which events must be notified.
281 * @iommu: the IOMMUMemoryRegion
282 * @old_flags: events which previously needed to be notified
283 * @new_flags: events which now need to be notified
285 void (*notify_flag_changed
)(IOMMUMemoryRegion
*iommu
,
286 IOMMUNotifierFlag old_flags
,
287 IOMMUNotifierFlag new_flags
);
288 /* Called to handle memory_region_iommu_replay().
290 * The default implementation of memory_region_iommu_replay() is to
291 * call the IOMMU translate method for every page in the address space
292 * with flag == IOMMU_NONE and then call the notifier if translate
293 * returns a valid mapping. If this method is implemented then it
294 * overrides the default behaviour, and must provide the full semantics
295 * of memory_region_iommu_replay(), by calling @notifier for every
296 * translation present in the IOMMU.
298 * Optional method -- an IOMMU only needs to provide this method
299 * if the default is inefficient or produces undesirable side effects.
301 * Note: this is not related to record-and-replay functionality.
303 void (*replay
)(IOMMUMemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
305 /* Get IOMMU misc attributes. This is an optional method that
306 * can be used to allow users of the IOMMU to get implementation-specific
307 * information. The IOMMU implements this method to handle calls
308 * by IOMMU users to memory_region_iommu_get_attr() by filling in
309 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
310 * the IOMMU supports. If the method is unimplemented then
311 * memory_region_iommu_get_attr() will always return -EINVAL.
313 * @iommu: the IOMMUMemoryRegion
314 * @attr: attribute being queried
315 * @data: memory to fill in with the attribute data
317 * Returns 0 on success, or a negative errno; in particular
318 * returns -EINVAL for unrecognized or unimplemented attribute types.
320 int (*get_attr
)(IOMMUMemoryRegion
*iommu
, enum IOMMUMemoryRegionAttr attr
,
323 /* Return the IOMMU index to use for a given set of transaction attributes.
325 * Optional method: if an IOMMU only supports a single IOMMU index then
326 * the default implementation of memory_region_iommu_attrs_to_index()
329 * The indexes supported by an IOMMU must be contiguous, starting at 0.
331 * @iommu: the IOMMUMemoryRegion
332 * @attrs: memory transaction attributes
334 int (*attrs_to_index
)(IOMMUMemoryRegion
*iommu
, MemTxAttrs attrs
);
336 /* Return the number of IOMMU indexes this IOMMU supports.
338 * Optional method: if this method is not provided, then
339 * memory_region_iommu_num_indexes() will return 1, indicating that
340 * only a single IOMMU index is supported.
342 * @iommu: the IOMMUMemoryRegion
344 int (*num_indexes
)(IOMMUMemoryRegion
*iommu
);
345 } IOMMUMemoryRegionClass
;
347 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
348 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
350 struct MemoryRegion
{
353 /* All fields are private - violators will be prosecuted */
355 /* The following fields should fit in a cache line */
359 bool readonly
; /* For RAM regions */
362 bool flush_coalesced_mmio
;
364 uint8_t dirty_log_mask
;
369 const MemoryRegionOps
*ops
;
371 MemoryRegion
*container
;
374 void (*destructor
)(MemoryRegion
*mr
);
379 bool warning_printed
; /* For reservations */
380 uint8_t vga_logging_count
;
384 QTAILQ_HEAD(, MemoryRegion
) subregions
;
385 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
386 QTAILQ_HEAD(, CoalescedMemoryRange
) coalesced
;
388 unsigned ioeventfd_nb
;
389 MemoryRegionIoeventfd
*ioeventfds
;
392 struct IOMMUMemoryRegion
{
393 MemoryRegion parent_obj
;
395 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
396 IOMMUNotifierFlag iommu_notify_flags
;
399 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
400 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
403 * MemoryListener: callbacks structure for updates to the physical memory map
405 * Allows a component to adjust to changes in the guest-visible memory map.
406 * Use with memory_listener_register() and memory_listener_unregister().
408 struct MemoryListener
{
409 void (*begin
)(MemoryListener
*listener
);
410 void (*commit
)(MemoryListener
*listener
);
411 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
412 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
413 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
414 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
416 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
418 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
419 void (*log_clear
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
420 void (*log_global_start
)(MemoryListener
*listener
);
421 void (*log_global_stop
)(MemoryListener
*listener
);
422 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
423 bool match_data
, uint64_t data
, EventNotifier
*e
);
424 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
425 bool match_data
, uint64_t data
, EventNotifier
*e
);
426 void (*coalesced_io_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
427 hwaddr addr
, hwaddr len
);
428 void (*coalesced_io_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
429 hwaddr addr
, hwaddr len
);
430 /* Lower = earlier (during add), later (during del) */
432 AddressSpace
*address_space
;
433 QTAILQ_ENTRY(MemoryListener
) link
;
434 QTAILQ_ENTRY(MemoryListener
) link_as
;
438 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
440 struct AddressSpace
{
441 /* All fields are private. */
446 /* Accessed via RCU. */
447 struct FlatView
*current_map
;
450 struct MemoryRegionIoeventfd
*ioeventfds
;
451 QTAILQ_HEAD(, MemoryListener
) listeners
;
452 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
455 typedef struct AddressSpaceDispatch AddressSpaceDispatch
;
456 typedef struct FlatRange FlatRange
;
458 /* Flattened global view of current active memory hierarchy. Kept in sorted
466 unsigned nr_allocated
;
467 struct AddressSpaceDispatch
*dispatch
;
471 static inline FlatView
*address_space_to_flatview(AddressSpace
*as
)
473 return atomic_rcu_read(&as
->current_map
);
478 * MemoryRegionSection: describes a fragment of a #MemoryRegion
480 * @mr: the region, or %NULL if empty
481 * @fv: the flat view of the address space the region is mapped in
482 * @offset_within_region: the beginning of the section, relative to @mr's start
483 * @size: the size of the section; will not exceed @mr's boundaries
484 * @offset_within_address_space: the address of the first byte of the section
485 * relative to the region's address space
486 * @readonly: writes to this section are ignored
487 * @nonvolatile: this section is non-volatile
489 struct MemoryRegionSection
{
492 hwaddr offset_within_region
;
494 hwaddr offset_within_address_space
;
500 * memory_region_init: Initialize a memory region
502 * The region typically acts as a container for other memory regions. Use
503 * memory_region_add_subregion() to add subregions.
505 * @mr: the #MemoryRegion to be initialized
506 * @owner: the object that tracks the region's reference count
507 * @name: used for debugging; not visible to the user or ABI
508 * @size: size of the region; any subregions beyond this size will be clipped
510 void memory_region_init(MemoryRegion
*mr
,
511 struct Object
*owner
,
516 * memory_region_ref: Add 1 to a memory region's reference count
518 * Whenever memory regions are accessed outside the BQL, they need to be
519 * preserved against hot-unplug. MemoryRegions actually do not have their
520 * own reference count; they piggyback on a QOM object, their "owner".
521 * This function adds a reference to the owner.
523 * All MemoryRegions must have an owner if they can disappear, even if the
524 * device they belong to operates exclusively under the BQL. This is because
525 * the region could be returned at any time by memory_region_find, and this
526 * is usually under guest control.
528 * @mr: the #MemoryRegion
530 void memory_region_ref(MemoryRegion
*mr
);
533 * memory_region_unref: Remove 1 to a memory region's reference count
535 * Whenever memory regions are accessed outside the BQL, they need to be
536 * preserved against hot-unplug. MemoryRegions actually do not have their
537 * own reference count; they piggyback on a QOM object, their "owner".
538 * This function removes a reference to the owner and possibly destroys it.
540 * @mr: the #MemoryRegion
542 void memory_region_unref(MemoryRegion
*mr
);
545 * memory_region_init_io: Initialize an I/O memory region.
547 * Accesses into the region will cause the callbacks in @ops to be called.
548 * if @size is nonzero, subregions will be clipped to @size.
550 * @mr: the #MemoryRegion to be initialized.
551 * @owner: the object that tracks the region's reference count
552 * @ops: a structure containing read and write callbacks to be used when
553 * I/O is performed on the region.
554 * @opaque: passed to the read and write callbacks of the @ops structure.
555 * @name: used for debugging; not visible to the user or ABI
556 * @size: size of the region.
558 void memory_region_init_io(MemoryRegion
*mr
,
559 struct Object
*owner
,
560 const MemoryRegionOps
*ops
,
566 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
567 * into the region will modify memory
570 * @mr: the #MemoryRegion to be initialized.
571 * @owner: the object that tracks the region's reference count
572 * @name: Region name, becomes part of RAMBlock name used in migration stream
573 * must be unique within any device
574 * @size: size of the region.
575 * @errp: pointer to Error*, to store an error if it happens.
577 * Note that this function does not do anything to cause the data in the
578 * RAM memory region to be migrated; that is the responsibility of the caller.
580 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
581 struct Object
*owner
,
587 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
588 * Accesses into the region will
589 * modify memory directly.
591 * @mr: the #MemoryRegion to be initialized.
592 * @owner: the object that tracks the region's reference count
593 * @name: Region name, becomes part of RAMBlock name used in migration stream
594 * must be unique within any device
595 * @size: size of the region.
596 * @share: allow remapping RAM to different addresses
597 * @errp: pointer to Error*, to store an error if it happens.
599 * Note that this function is similar to memory_region_init_ram_nomigrate.
600 * The only difference is part of the RAM region can be remapped.
602 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
603 struct Object
*owner
,
610 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
611 * RAM. Accesses into the region will
612 * modify memory directly. Only an initial
613 * portion of this RAM is actually used.
614 * The used size can change across reboots.
616 * @mr: the #MemoryRegion to be initialized.
617 * @owner: the object that tracks the region's reference count
618 * @name: Region name, becomes part of RAMBlock name used in migration stream
619 * must be unique within any device
620 * @size: used size of the region.
621 * @max_size: max size of the region.
622 * @resized: callback to notify owner about used size change.
623 * @errp: pointer to Error*, to store an error if it happens.
625 * Note that this function does not do anything to cause the data in the
626 * RAM memory region to be migrated; that is the responsibility of the caller.
628 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
629 struct Object
*owner
,
633 void (*resized
)(const char*,
640 * memory_region_init_ram_from_file: Initialize RAM memory region with a
643 * @mr: the #MemoryRegion to be initialized.
644 * @owner: the object that tracks the region's reference count
645 * @name: Region name, becomes part of RAMBlock name used in migration stream
646 * must be unique within any device
647 * @size: size of the region.
648 * @align: alignment of the region base address; if 0, the default alignment
649 * (getpagesize()) will be used.
650 * @ram_flags: Memory region features:
651 * - RAM_SHARED: memory must be mmaped with the MAP_SHARED flag
652 * - RAM_PMEM: the memory is persistent memory
653 * Other bits are ignored now.
654 * @path: the path in which to allocate the RAM.
655 * @errp: pointer to Error*, to store an error if it happens.
657 * Note that this function does not do anything to cause the data in the
658 * RAM memory region to be migrated; that is the responsibility of the caller.
660 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
661 struct Object
*owner
,
670 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
673 * @mr: the #MemoryRegion to be initialized.
674 * @owner: the object that tracks the region's reference count
675 * @name: the name of the region.
676 * @size: size of the region.
677 * @share: %true if memory must be mmaped with the MAP_SHARED flag
678 * @fd: the fd to mmap.
679 * @errp: pointer to Error*, to store an error if it happens.
681 * Note that this function does not do anything to cause the data in the
682 * RAM memory region to be migrated; that is the responsibility of the caller.
684 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
685 struct Object
*owner
,
694 * memory_region_init_ram_ptr: Initialize RAM memory region from a
695 * user-provided pointer. Accesses into the
696 * region will modify memory directly.
698 * @mr: the #MemoryRegion to be initialized.
699 * @owner: the object that tracks the region's reference count
700 * @name: Region name, becomes part of RAMBlock name used in migration stream
701 * must be unique within any device
702 * @size: size of the region.
703 * @ptr: memory to be mapped; must contain at least @size bytes.
705 * Note that this function does not do anything to cause the data in the
706 * RAM memory region to be migrated; that is the responsibility of the caller.
708 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
709 struct Object
*owner
,
715 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
716 * a user-provided pointer.
718 * A RAM device represents a mapping to a physical device, such as to a PCI
719 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
720 * into the VM address space and access to the region will modify memory
721 * directly. However, the memory region should not be included in a memory
722 * dump (device may not be enabled/mapped at the time of the dump), and
723 * operations incompatible with manipulating MMIO should be avoided. Replaces
726 * @mr: the #MemoryRegion to be initialized.
727 * @owner: the object that tracks the region's reference count
728 * @name: the name of the region.
729 * @size: size of the region.
730 * @ptr: memory to be mapped; must contain at least @size bytes.
732 * Note that this function does not do anything to cause the data in the
733 * RAM memory region to be migrated; that is the responsibility of the caller.
734 * (For RAM device memory regions, migrating the contents rarely makes sense.)
736 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
737 struct Object
*owner
,
743 * memory_region_init_alias: Initialize a memory region that aliases all or a
744 * part of another memory region.
746 * @mr: the #MemoryRegion to be initialized.
747 * @owner: the object that tracks the region's reference count
748 * @name: used for debugging; not visible to the user or ABI
749 * @orig: the region to be referenced; @mr will be equivalent to
750 * @orig between @offset and @offset + @size - 1.
751 * @offset: start of the section in @orig to be referenced.
752 * @size: size of the region.
754 void memory_region_init_alias(MemoryRegion
*mr
,
755 struct Object
*owner
,
762 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
764 * This has the same effect as calling memory_region_init_ram_nomigrate()
765 * and then marking the resulting region read-only with
766 * memory_region_set_readonly().
768 * Note that this function does not do anything to cause the data in the
769 * RAM side of the memory region to be migrated; that is the responsibility
772 * @mr: the #MemoryRegion to be initialized.
773 * @owner: the object that tracks the region's reference count
774 * @name: Region name, becomes part of RAMBlock name used in migration stream
775 * must be unique within any device
776 * @size: size of the region.
777 * @errp: pointer to Error*, to store an error if it happens.
779 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
780 struct Object
*owner
,
786 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
787 * Writes are handled via callbacks.
789 * Note that this function does not do anything to cause the data in the
790 * RAM side of the memory region to be migrated; that is the responsibility
793 * @mr: the #MemoryRegion to be initialized.
794 * @owner: the object that tracks the region's reference count
795 * @ops: callbacks for write access handling (must not be NULL).
796 * @opaque: passed to the read and write callbacks of the @ops structure.
797 * @name: Region name, becomes part of RAMBlock name used in migration stream
798 * must be unique within any device
799 * @size: size of the region.
800 * @errp: pointer to Error*, to store an error if it happens.
802 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
803 struct Object
*owner
,
804 const MemoryRegionOps
*ops
,
811 * memory_region_init_iommu: Initialize a memory region of a custom type
812 * that translates addresses
814 * An IOMMU region translates addresses and forwards accesses to a target
817 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
818 * @_iommu_mr should be a pointer to enough memory for an instance of
819 * that subclass, @instance_size is the size of that subclass, and
820 * @mrtypename is its name. This function will initialize @_iommu_mr as an
821 * instance of the subclass, and its methods will then be called to handle
822 * accesses to the memory region. See the documentation of
823 * #IOMMUMemoryRegionClass for further details.
825 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
826 * @instance_size: the IOMMUMemoryRegion subclass instance size
827 * @mrtypename: the type name of the #IOMMUMemoryRegion
828 * @owner: the object that tracks the region's reference count
829 * @name: used for debugging; not visible to the user or ABI
830 * @size: size of the region.
832 void memory_region_init_iommu(void *_iommu_mr
,
833 size_t instance_size
,
834 const char *mrtypename
,
840 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
841 * region will modify memory directly.
843 * @mr: the #MemoryRegion to be initialized
844 * @owner: the object that tracks the region's reference count (must be
845 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
846 * @name: name of the memory region
847 * @size: size of the region in bytes
848 * @errp: pointer to Error*, to store an error if it happens.
850 * This function allocates RAM for a board model or device, and
851 * arranges for it to be migrated (by calling vmstate_register_ram()
852 * if @owner is a DeviceState, or vmstate_register_ram_global() if
855 * TODO: Currently we restrict @owner to being either NULL (for
856 * global RAM regions with no owner) or devices, so that we can
857 * give the RAM block a unique name for migration purposes.
858 * We should lift this restriction and allow arbitrary Objects.
859 * If you pass a non-NULL non-device @owner then we will assert.
861 void memory_region_init_ram(MemoryRegion
*mr
,
862 struct Object
*owner
,
868 * memory_region_init_rom: Initialize a ROM memory region.
870 * This has the same effect as calling memory_region_init_ram()
871 * and then marking the resulting region read-only with
872 * memory_region_set_readonly(). This includes arranging for the
873 * contents to be migrated.
875 * TODO: Currently we restrict @owner to being either NULL (for
876 * global RAM regions with no owner) or devices, so that we can
877 * give the RAM block a unique name for migration purposes.
878 * We should lift this restriction and allow arbitrary Objects.
879 * If you pass a non-NULL non-device @owner then we will assert.
881 * @mr: the #MemoryRegion to be initialized.
882 * @owner: the object that tracks the region's reference count
883 * @name: Region name, becomes part of RAMBlock name used in migration stream
884 * must be unique within any device
885 * @size: size of the region.
886 * @errp: pointer to Error*, to store an error if it happens.
888 void memory_region_init_rom(MemoryRegion
*mr
,
889 struct Object
*owner
,
895 * memory_region_init_rom_device: Initialize a ROM memory region.
896 * Writes are handled via callbacks.
898 * This function initializes a memory region backed by RAM for reads
899 * and callbacks for writes, and arranges for the RAM backing to
900 * be migrated (by calling vmstate_register_ram()
901 * if @owner is a DeviceState, or vmstate_register_ram_global() if
904 * TODO: Currently we restrict @owner to being either NULL (for
905 * global RAM regions with no owner) or devices, so that we can
906 * give the RAM block a unique name for migration purposes.
907 * We should lift this restriction and allow arbitrary Objects.
908 * If you pass a non-NULL non-device @owner then we will assert.
910 * @mr: the #MemoryRegion to be initialized.
911 * @owner: the object that tracks the region's reference count
912 * @ops: callbacks for write access handling (must not be NULL).
913 * @name: Region name, becomes part of RAMBlock name used in migration stream
914 * must be unique within any device
915 * @size: size of the region.
916 * @errp: pointer to Error*, to store an error if it happens.
918 void memory_region_init_rom_device(MemoryRegion
*mr
,
919 struct Object
*owner
,
920 const MemoryRegionOps
*ops
,
928 * memory_region_owner: get a memory region's owner.
930 * @mr: the memory region being queried.
932 struct Object
*memory_region_owner(MemoryRegion
*mr
);
935 * memory_region_size: get a memory region's size.
937 * @mr: the memory region being queried.
939 uint64_t memory_region_size(MemoryRegion
*mr
);
942 * memory_region_is_ram: check whether a memory region is random access
944 * Returns %true if a memory region is random access.
946 * @mr: the memory region being queried
948 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
954 * memory_region_is_ram_device: check whether a memory region is a ram device
956 * Returns %true if a memory region is a device backed ram region
958 * @mr: the memory region being queried
960 bool memory_region_is_ram_device(MemoryRegion
*mr
);
963 * memory_region_is_romd: check whether a memory region is in ROMD mode
965 * Returns %true if a memory region is a ROM device and currently set to allow
968 * @mr: the memory region being queried
970 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
972 return mr
->rom_device
&& mr
->romd_mode
;
976 * memory_region_get_iommu: check whether a memory region is an iommu
978 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
981 * @mr: the memory region being queried
983 static inline IOMMUMemoryRegion
*memory_region_get_iommu(MemoryRegion
*mr
)
986 return memory_region_get_iommu(mr
->alias
);
989 return (IOMMUMemoryRegion
*) mr
;
995 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
996 * if an iommu or NULL if not
998 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
999 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1001 * @mr: the memory region being queried
1003 static inline IOMMUMemoryRegionClass
*memory_region_get_iommu_class_nocheck(
1004 IOMMUMemoryRegion
*iommu_mr
)
1006 return (IOMMUMemoryRegionClass
*) (((Object
*)iommu_mr
)->class);
1009 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1012 * memory_region_iommu_get_min_page_size: get minimum supported page size
1015 * Returns minimum supported page size for an iommu.
1017 * @iommu_mr: the memory region being queried
1019 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
);
1022 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1024 * The notification type will be decided by entry.perm bits:
1026 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
1027 * - For MAP (newly added entry) notifies: set entry.perm to the
1028 * permission of the page (which is definitely !IOMMU_NONE).
1030 * Note: for any IOMMU implementation, an in-place mapping change
1031 * should be notified with an UNMAP followed by a MAP.
1033 * @iommu_mr: the memory region that was changed
1034 * @iommu_idx: the IOMMU index for the translation table which has changed
1035 * @entry: the new entry in the IOMMU translation table. The entry
1036 * replaces all old entries for the same virtual I/O address range.
1037 * Deleted entries have .@perm == 0.
1039 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1041 IOMMUTLBEntry entry
);
1044 * memory_region_notify_one: notify a change in an IOMMU translation
1045 * entry to a single notifier
1047 * This works just like memory_region_notify_iommu(), but it only
1048 * notifies a specific notifier, not all of them.
1050 * @notifier: the notifier to be notified
1051 * @entry: the new entry in the IOMMU translation table. The entry
1052 * replaces all old entries for the same virtual I/O address range.
1053 * Deleted entries have .@perm == 0.
1055 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1056 IOMMUTLBEntry
*entry
);
1059 * memory_region_register_iommu_notifier: register a notifier for changes to
1060 * IOMMU translation entries.
1062 * @mr: the memory region to observe
1063 * @n: the IOMMUNotifier to be added; the notify callback receives a
1064 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1065 * ceases to be valid on exit from the notifier.
1067 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1071 * memory_region_iommu_replay: replay existing IOMMU translations to
1072 * a notifier with the minimum page granularity returned by
1073 * mr->iommu_ops->get_page_size().
1075 * Note: this is not related to record-and-replay functionality.
1077 * @iommu_mr: the memory region to observe
1078 * @n: the notifier to which to replay iommu mappings
1080 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
);
1083 * memory_region_iommu_replay_all: replay existing IOMMU translations
1084 * to all the notifiers registered.
1086 * Note: this is not related to record-and-replay functionality.
1088 * @iommu_mr: the memory region to observe
1090 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
);
1093 * memory_region_unregister_iommu_notifier: unregister a notifier for
1094 * changes to IOMMU translation entries.
1096 * @mr: the memory region which was observed and for which notity_stopped()
1097 * needs to be called
1098 * @n: the notifier to be removed.
1100 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1104 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1105 * defined on the IOMMU.
1107 * Returns 0 on success, or a negative errno otherwise. In particular,
1108 * -EINVAL indicates that the IOMMU does not support the requested
1111 * @iommu_mr: the memory region
1112 * @attr: the requested attribute
1113 * @data: a pointer to the requested attribute data
1115 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1116 enum IOMMUMemoryRegionAttr attr
,
1120 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1121 * use for translations with the given memory transaction attributes.
1123 * @iommu_mr: the memory region
1124 * @attrs: the memory transaction attributes
1126 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1130 * memory_region_iommu_num_indexes: return the total number of IOMMU
1131 * indexes that this IOMMU supports.
1133 * @iommu_mr: the memory region
1135 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
);
1138 * memory_region_name: get a memory region's name
1140 * Returns the string that was used to initialize the memory region.
1142 * @mr: the memory region being queried
1144 const char *memory_region_name(const MemoryRegion
*mr
);
1147 * memory_region_is_logging: return whether a memory region is logging writes
1149 * Returns %true if the memory region is logging writes for the given client
1151 * @mr: the memory region being queried
1152 * @client: the client being queried
1154 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
1157 * memory_region_get_dirty_log_mask: return the clients for which a
1158 * memory region is logging writes.
1160 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1161 * are the bit indices.
1163 * @mr: the memory region being queried
1165 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
1168 * memory_region_is_rom: check whether a memory region is ROM
1170 * Returns %true if a memory region is read-only memory.
1172 * @mr: the memory region being queried
1174 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
1176 return mr
->ram
&& mr
->readonly
;
1180 * memory_region_is_nonvolatile: check whether a memory region is non-volatile
1182 * Returns %true is a memory region is non-volatile memory.
1184 * @mr: the memory region being queried
1186 static inline bool memory_region_is_nonvolatile(MemoryRegion
*mr
)
1188 return mr
->nonvolatile
;
1192 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1194 * Returns a file descriptor backing a file-based RAM memory region,
1195 * or -1 if the region is not a file-based RAM memory region.
1197 * @mr: the RAM or alias memory region being queried.
1199 int memory_region_get_fd(MemoryRegion
*mr
);
1202 * memory_region_from_host: Convert a pointer into a RAM memory region
1203 * and an offset within it.
1205 * Given a host pointer inside a RAM memory region (created with
1206 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1207 * the MemoryRegion and the offset within it.
1209 * Use with care; by the time this function returns, the returned pointer is
1210 * not protected by RCU anymore. If the caller is not within an RCU critical
1211 * section and does not hold the iothread lock, it must have other means of
1212 * protecting the pointer, such as a reference to the region that includes
1213 * the incoming ram_addr_t.
1215 * @ptr: the host pointer to be converted
1216 * @offset: the offset within memory region
1218 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
1221 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1223 * Returns a host pointer to a RAM memory region (created with
1224 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1226 * Use with care; by the time this function returns, the returned pointer is
1227 * not protected by RCU anymore. If the caller is not within an RCU critical
1228 * section and does not hold the iothread lock, it must have other means of
1229 * protecting the pointer, such as a reference to the region that includes
1230 * the incoming ram_addr_t.
1232 * @mr: the memory region being queried.
1234 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
1236 /* memory_region_ram_resize: Resize a RAM region.
1238 * Only legal before guest might have detected the memory size: e.g. on
1239 * incoming migration, or right after reset.
1241 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1242 * @newsize: the new size the region
1243 * @errp: pointer to Error*, to store an error if it happens.
1245 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
1249 * memory_region_set_log: Turn dirty logging on or off for a region.
1251 * Turns dirty logging on or off for a specified client (display, migration).
1252 * Only meaningful for RAM regions.
1254 * @mr: the memory region being updated.
1255 * @log: whether dirty logging is to be enabled or disabled.
1256 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1258 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
1261 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1263 * Marks a range of bytes as dirty, after it has been dirtied outside
1266 * @mr: the memory region being dirtied.
1267 * @addr: the address (relative to the start of the region) being dirtied.
1268 * @size: size of the range being dirtied.
1270 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1274 * memory_region_clear_dirty_bitmap - clear dirty bitmap for memory range
1276 * This function is called when the caller wants to clear the remote
1277 * dirty bitmap of a memory range within the memory region. This can
1278 * be used by e.g. KVM to manually clear dirty log when
1279 * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT is declared support by the host
1282 * @mr: the memory region to clear the dirty log upon
1283 * @start: start address offset within the memory region
1284 * @len: length of the memory region to clear dirty bitmap
1286 void memory_region_clear_dirty_bitmap(MemoryRegion
*mr
, hwaddr start
,
1290 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1291 * bitmap and clear it.
1293 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1294 * returns the snapshot. The snapshot can then be used to query dirty
1295 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1296 * querying the same page multiple times, which is especially useful for
1297 * display updates where the scanlines often are not page aligned.
1299 * The dirty bitmap region which gets copyed into the snapshot (and
1300 * cleared afterwards) can be larger than requested. The boundaries
1301 * are rounded up/down so complete bitmap longs (covering 64 pages on
1302 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1303 * isn't a problem for display updates as the extra pages are outside
1304 * the visible area, and in case the visible area changes a full
1305 * display redraw is due anyway. Should other use cases for this
1306 * function emerge we might have to revisit this implementation
1309 * Use g_free to release DirtyBitmapSnapshot.
1311 * @mr: the memory region being queried.
1312 * @addr: the address (relative to the start of the region) being queried.
1313 * @size: the size of the range being queried.
1314 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1316 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1322 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1323 * in the specified dirty bitmap snapshot.
1325 * @mr: the memory region being queried.
1326 * @snap: the dirty bitmap snapshot
1327 * @addr: the address (relative to the start of the region) being queried.
1328 * @size: the size of the range being queried.
1330 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
1331 DirtyBitmapSnapshot
*snap
,
1332 hwaddr addr
, hwaddr size
);
1335 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1338 * Marks a range of pages as no longer dirty.
1340 * @mr: the region being updated.
1341 * @addr: the start of the subrange being cleaned.
1342 * @size: the size of the subrange being cleaned.
1343 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1344 * %DIRTY_MEMORY_VGA.
1346 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1347 hwaddr size
, unsigned client
);
1350 * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate
1351 * TBs (for self-modifying code).
1353 * The MemoryRegionOps->write() callback of a ROM device must use this function
1354 * to mark byte ranges that have been modified internally, such as by directly
1355 * accessing the memory returned by memory_region_get_ram_ptr().
1357 * This function marks the range dirty and invalidates TBs so that TCG can
1358 * detect self-modifying code.
1360 * @mr: the region being flushed.
1361 * @addr: the start, relative to the start of the region, of the range being
1363 * @size: the size, in bytes, of the range being flushed.
1365 void memory_region_flush_rom_device(MemoryRegion
*mr
, hwaddr addr
, hwaddr size
);
1368 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1370 * Allows a memory region to be marked as read-only (turning it into a ROM).
1371 * only useful on RAM regions.
1373 * @mr: the region being updated.
1374 * @readonly: whether rhe region is to be ROM or RAM.
1376 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
1379 * memory_region_set_nonvolatile: Turn a memory region non-volatile
1381 * Allows a memory region to be marked as non-volatile.
1382 * only useful on RAM regions.
1384 * @mr: the region being updated.
1385 * @nonvolatile: whether rhe region is to be non-volatile.
1387 void memory_region_set_nonvolatile(MemoryRegion
*mr
, bool nonvolatile
);
1390 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1392 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1393 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1394 * device is mapped to guest memory and satisfies read access directly.
1395 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1396 * Writes are always handled by the #MemoryRegion.write function.
1398 * @mr: the memory region to be updated
1399 * @romd_mode: %true to put the region into ROMD mode
1401 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
1404 * memory_region_set_coalescing: Enable memory coalescing for the region.
1406 * Enabled writes to a region to be queued for later processing. MMIO ->write
1407 * callbacks may be delayed until a non-coalesced MMIO is issued.
1408 * Only useful for IO regions. Roughly similar to write-combining hardware.
1410 * @mr: the memory region to be write coalesced
1412 void memory_region_set_coalescing(MemoryRegion
*mr
);
1415 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1418 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1419 * Multiple calls can be issued coalesced disjoint ranges.
1421 * @mr: the memory region to be updated.
1422 * @offset: the start of the range within the region to be coalesced.
1423 * @size: the size of the subrange to be coalesced.
1425 void memory_region_add_coalescing(MemoryRegion
*mr
,
1430 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1432 * Disables any coalescing caused by memory_region_set_coalescing() or
1433 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1436 * @mr: the memory region to be updated.
1438 void memory_region_clear_coalescing(MemoryRegion
*mr
);
1441 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1444 * Ensure that pending coalesced MMIO request are flushed before the memory
1445 * region is accessed. This property is automatically enabled for all regions
1446 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1448 * @mr: the memory region to be updated.
1450 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
1453 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1456 * Clear the automatic coalesced MMIO flushing enabled via
1457 * memory_region_set_flush_coalesced. Note that this service has no effect on
1458 * memory regions that have MMIO coalescing enabled for themselves. For them,
1459 * automatic flushing will stop once coalescing is disabled.
1461 * @mr: the memory region to be updated.
1463 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1466 * memory_region_clear_global_locking: Declares that access processing does
1467 * not depend on the QEMU global lock.
1469 * By clearing this property, accesses to the memory region will be processed
1470 * outside of QEMU's global lock (unless the lock is held on when issuing the
1471 * access request). In this case, the device model implementing the access
1472 * handlers is responsible for synchronization of concurrency.
1474 * @mr: the memory region to be updated.
1476 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1479 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1480 * is written to a location.
1482 * Marks a word in an IO region (initialized with memory_region_init_io())
1483 * as a trigger for an eventfd event. The I/O callback will not be called.
1484 * The caller must be prepared to handle failure (that is, take the required
1485 * action if the callback _is_ called).
1487 * @mr: the memory region being updated.
1488 * @addr: the address within @mr that is to be monitored
1489 * @size: the size of the access to trigger the eventfd
1490 * @match_data: whether to match against @data, instead of just @addr
1491 * @data: the data to match against the guest write
1492 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1494 void memory_region_add_eventfd(MemoryRegion
*mr
,
1502 * memory_region_del_eventfd: Cancel an eventfd.
1504 * Cancels an eventfd trigger requested by a previous
1505 * memory_region_add_eventfd() call.
1507 * @mr: the memory region being updated.
1508 * @addr: the address within @mr that is to be monitored
1509 * @size: the size of the access to trigger the eventfd
1510 * @match_data: whether to match against @data, instead of just @addr
1511 * @data: the data to match against the guest write
1512 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1514 void memory_region_del_eventfd(MemoryRegion
*mr
,
1522 * memory_region_add_subregion: Add a subregion to a container.
1524 * Adds a subregion at @offset. The subregion may not overlap with other
1525 * subregions (except for those explicitly marked as overlapping). A region
1526 * may only be added once as a subregion (unless removed with
1527 * memory_region_del_subregion()); use memory_region_init_alias() if you
1528 * want a region to be a subregion in multiple locations.
1530 * @mr: the region to contain the new subregion; must be a container
1531 * initialized with memory_region_init().
1532 * @offset: the offset relative to @mr where @subregion is added.
1533 * @subregion: the subregion to be added.
1535 void memory_region_add_subregion(MemoryRegion
*mr
,
1537 MemoryRegion
*subregion
);
1539 * memory_region_add_subregion_overlap: Add a subregion to a container
1542 * Adds a subregion at @offset. The subregion may overlap with other
1543 * subregions. Conflicts are resolved by having a higher @priority hide a
1544 * lower @priority. Subregions without priority are taken as @priority 0.
1545 * A region may only be added once as a subregion (unless removed with
1546 * memory_region_del_subregion()); use memory_region_init_alias() if you
1547 * want a region to be a subregion in multiple locations.
1549 * @mr: the region to contain the new subregion; must be a container
1550 * initialized with memory_region_init().
1551 * @offset: the offset relative to @mr where @subregion is added.
1552 * @subregion: the subregion to be added.
1553 * @priority: used for resolving overlaps; highest priority wins.
1555 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1557 MemoryRegion
*subregion
,
1561 * memory_region_get_ram_addr: Get the ram address associated with a memory
1564 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1566 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1568 * memory_region_del_subregion: Remove a subregion.
1570 * Removes a subregion from its container.
1572 * @mr: the container to be updated.
1573 * @subregion: the region being removed; must be a current subregion of @mr.
1575 void memory_region_del_subregion(MemoryRegion
*mr
,
1576 MemoryRegion
*subregion
);
1579 * memory_region_set_enabled: dynamically enable or disable a region
1581 * Enables or disables a memory region. A disabled memory region
1582 * ignores all accesses to itself and its subregions. It does not
1583 * obscure sibling subregions with lower priority - it simply behaves as
1584 * if it was removed from the hierarchy.
1586 * Regions default to being enabled.
1588 * @mr: the region to be updated
1589 * @enabled: whether to enable or disable the region
1591 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1594 * memory_region_set_address: dynamically update the address of a region
1596 * Dynamically updates the address of a region, relative to its container.
1597 * May be used on regions are currently part of a memory hierarchy.
1599 * @mr: the region to be updated
1600 * @addr: new address, relative to container region
1602 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1605 * memory_region_set_size: dynamically update the size of a region.
1607 * Dynamically updates the size of a region.
1609 * @mr: the region to be updated
1610 * @size: used size of the region.
1612 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1615 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1617 * Dynamically updates the offset into the target region that an alias points
1618 * to, as if the fourth argument to memory_region_init_alias() has changed.
1620 * @mr: the #MemoryRegion to be updated; should be an alias.
1621 * @offset: the new offset into the target memory region
1623 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1627 * memory_region_present: checks if an address relative to a @container
1628 * translates into #MemoryRegion within @container
1630 * Answer whether a #MemoryRegion within @container covers the address
1633 * @container: a #MemoryRegion within which @addr is a relative address
1634 * @addr: the area within @container to be searched
1636 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1639 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1640 * into any address space.
1642 * @mr: a #MemoryRegion which should be checked if it's mapped
1644 bool memory_region_is_mapped(MemoryRegion
*mr
);
1647 * memory_region_find: translate an address/size relative to a
1648 * MemoryRegion into a #MemoryRegionSection.
1650 * Locates the first #MemoryRegion within @mr that overlaps the range
1651 * given by @addr and @size.
1653 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1654 * It will have the following characteristics:
1655 * .@size = 0 iff no overlap was found
1656 * .@mr is non-%NULL iff an overlap was found
1658 * Remember that in the return value the @offset_within_region is
1659 * relative to the returned region (in the .@mr field), not to the
1662 * Similarly, the .@offset_within_address_space is relative to the
1663 * address space that contains both regions, the passed and the
1664 * returned one. However, in the special case where the @mr argument
1665 * has no container (and thus is the root of the address space), the
1666 * following will hold:
1667 * .@offset_within_address_space >= @addr
1668 * .@offset_within_address_space + .@size <= @addr + @size
1670 * @mr: a MemoryRegion within which @addr is a relative address
1671 * @addr: start of the area within @as to be searched
1672 * @size: size of the area to be searched
1674 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1675 hwaddr addr
, uint64_t size
);
1678 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1680 * Synchronizes the dirty page log for all address spaces.
1682 void memory_global_dirty_log_sync(void);
1685 * memory_region_transaction_begin: Start a transaction.
1687 * During a transaction, changes will be accumulated and made visible
1688 * only when the transaction ends (is committed).
1690 void memory_region_transaction_begin(void);
1693 * memory_region_transaction_commit: Commit a transaction and make changes
1694 * visible to the guest.
1696 void memory_region_transaction_commit(void);
1699 * memory_listener_register: register callbacks to be called when memory
1700 * sections are mapped or unmapped into an address
1703 * @listener: an object containing the callbacks to be called
1704 * @filter: if non-%NULL, only regions in this address space will be observed
1706 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1709 * memory_listener_unregister: undo the effect of memory_listener_register()
1711 * @listener: an object containing the callbacks to be removed
1713 void memory_listener_unregister(MemoryListener
*listener
);
1716 * memory_global_dirty_log_start: begin dirty logging for all regions
1718 void memory_global_dirty_log_start(void);
1721 * memory_global_dirty_log_stop: end dirty logging for all regions
1723 void memory_global_dirty_log_stop(void);
1725 void mtree_info(bool flatview
, bool dispatch_tree
, bool owner
);
1728 * memory_region_dispatch_read: perform a read directly to the specified
1731 * @mr: #MemoryRegion to access
1732 * @addr: address within that region
1733 * @pval: pointer to uint64_t which the data is written to
1734 * @size: size of the access in bytes
1735 * @attrs: memory transaction attributes to use for the access
1737 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1743 * memory_region_dispatch_write: perform a write directly to the specified
1746 * @mr: #MemoryRegion to access
1747 * @addr: address within that region
1748 * @data: data to write
1749 * @size: size of the access in bytes
1750 * @attrs: memory transaction attributes to use for the access
1752 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1759 * address_space_init: initializes an address space
1761 * @as: an uninitialized #AddressSpace
1762 * @root: a #MemoryRegion that routes addresses for the address space
1763 * @name: an address space name. The name is only used for debugging
1766 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1769 * address_space_destroy: destroy an address space
1771 * Releases all resources associated with an address space. After an address space
1772 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1775 * @as: address space to be destroyed
1777 void address_space_destroy(AddressSpace
*as
);
1780 * address_space_remove_listeners: unregister all listeners of an address space
1782 * Removes all callbacks previously registered with memory_listener_register()
1785 * @as: an initialized #AddressSpace
1787 void address_space_remove_listeners(AddressSpace
*as
);
1790 * address_space_rw: read from or write to an 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 read or write
1801 * @is_write: indicates the transfer direction
1803 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1804 MemTxAttrs attrs
, uint8_t *buf
,
1805 hwaddr len
, bool is_write
);
1808 * address_space_write: write to address space.
1810 * Return a MemTxResult indicating whether the operation succeeded
1811 * or failed (eg unassigned memory, device rejected the transaction,
1814 * @as: #AddressSpace to be accessed
1815 * @addr: address within that address space
1816 * @attrs: memory transaction attributes
1817 * @buf: buffer with the data transferred
1818 * @len: the number of bytes to write
1820 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1822 const uint8_t *buf
, hwaddr len
);
1825 * address_space_write_rom: write to address space, including ROM.
1827 * This function writes to the specified address space, but will
1828 * write data to both ROM and RAM. This is used for non-guest
1829 * writes like writes from the gdb debug stub or initial loading
1832 * Note that portions of the write which attempt to write data to
1833 * a device will be silently ignored -- only real RAM and ROM will
1836 * Return a MemTxResult indicating whether the operation succeeded
1837 * or failed (eg unassigned memory, device rejected the transaction,
1840 * @as: #AddressSpace to be accessed
1841 * @addr: address within that address space
1842 * @attrs: memory transaction attributes
1843 * @buf: buffer with the data transferred
1844 * @len: the number of bytes to write
1846 MemTxResult
address_space_write_rom(AddressSpace
*as
, hwaddr addr
,
1848 const uint8_t *buf
, hwaddr len
);
1850 /* address_space_ld*: load from an address space
1851 * address_space_st*: store to an address space
1853 * These functions perform a load or store of the byte, word,
1854 * longword or quad to the specified address within the AddressSpace.
1855 * The _le suffixed functions treat the data as little endian;
1856 * _be indicates big endian; no suffix indicates "same endianness
1859 * The "guest CPU endianness" accessors are deprecated for use outside
1860 * target-* code; devices should be CPU-agnostic and use either the LE
1861 * or the BE accessors.
1863 * @as #AddressSpace to be accessed
1864 * @addr: address within that address space
1865 * @val: data value, for stores
1866 * @attrs: memory transaction attributes
1867 * @result: location to write the success/failure of the transaction;
1868 * if NULL, this information is discarded
1873 #define ARG1_DECL AddressSpace *as
1874 #include "exec/memory_ldst.inc.h"
1878 #define ARG1_DECL AddressSpace *as
1879 #include "exec/memory_ldst_phys.inc.h"
1881 struct MemoryRegionCache
{
1886 MemoryRegionSection mrs
;
1890 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
1893 /* address_space_ld*_cached: load from a cached #MemoryRegion
1894 * address_space_st*_cached: store into a cached #MemoryRegion
1896 * These functions perform a load or store of the byte, word,
1897 * longword or quad to the specified address. The address is
1898 * a physical address in the AddressSpace, but it must lie within
1899 * a #MemoryRegion that was mapped with address_space_cache_init.
1901 * The _le suffixed functions treat the data as little endian;
1902 * _be indicates big endian; no suffix indicates "same endianness
1905 * The "guest CPU endianness" accessors are deprecated for use outside
1906 * target-* code; devices should be CPU-agnostic and use either the LE
1907 * or the BE accessors.
1909 * @cache: previously initialized #MemoryRegionCache to be accessed
1910 * @addr: address within the address space
1911 * @val: data value, for stores
1912 * @attrs: memory transaction attributes
1913 * @result: location to write the success/failure of the transaction;
1914 * if NULL, this information is discarded
1917 #define SUFFIX _cached_slow
1919 #define ARG1_DECL MemoryRegionCache *cache
1920 #include "exec/memory_ldst.inc.h"
1922 /* Inline fast path for direct RAM access. */
1923 static inline uint8_t address_space_ldub_cached(MemoryRegionCache
*cache
,
1924 hwaddr addr
, MemTxAttrs attrs
, MemTxResult
*result
)
1926 assert(addr
< cache
->len
);
1927 if (likely(cache
->ptr
)) {
1928 return ldub_p(cache
->ptr
+ addr
);
1930 return address_space_ldub_cached_slow(cache
, addr
, attrs
, result
);
1934 static inline void address_space_stb_cached(MemoryRegionCache
*cache
,
1935 hwaddr addr
, uint32_t val
, MemTxAttrs attrs
, MemTxResult
*result
)
1937 assert(addr
< cache
->len
);
1938 if (likely(cache
->ptr
)) {
1939 stb_p(cache
->ptr
+ addr
, val
);
1941 address_space_stb_cached_slow(cache
, addr
, val
, attrs
, result
);
1945 #define ENDIANNESS _le
1946 #include "exec/memory_ldst_cached.inc.h"
1948 #define ENDIANNESS _be
1949 #include "exec/memory_ldst_cached.inc.h"
1951 #define SUFFIX _cached
1953 #define ARG1_DECL MemoryRegionCache *cache
1954 #include "exec/memory_ldst_phys.inc.h"
1956 /* address_space_cache_init: prepare for repeated access to a physical
1959 * @cache: #MemoryRegionCache to be filled
1960 * @as: #AddressSpace to be accessed
1961 * @addr: address within that address space
1962 * @len: length of buffer
1963 * @is_write: indicates the transfer direction
1965 * Will only work with RAM, and may map a subset of the requested range by
1966 * returning a value that is less than @len. On failure, return a negative
1969 * Because it only works with RAM, this function can be used for
1970 * read-modify-write operations. In this case, is_write should be %true.
1972 * Note that addresses passed to the address_space_*_cached functions
1973 * are relative to @addr.
1975 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1982 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1984 * @cache: The #MemoryRegionCache to operate on.
1985 * @addr: The first physical address that was written, relative to the
1986 * address that was passed to @address_space_cache_init.
1987 * @access_len: The number of bytes that were written starting at @addr.
1989 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1994 * address_space_cache_destroy: free a #MemoryRegionCache
1996 * @cache: The #MemoryRegionCache whose memory should be released.
1998 void address_space_cache_destroy(MemoryRegionCache
*cache
);
2000 /* address_space_get_iotlb_entry: translate an address into an IOTLB
2001 * entry. Should be called from an RCU critical section.
2003 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
2004 bool is_write
, MemTxAttrs attrs
);
2006 /* address_space_translate: translate an address range into an address space
2007 * into a MemoryRegion and an address range into that section. Should be
2008 * called from an RCU critical section, to avoid that the last reference
2009 * to the returned region disappears after address_space_translate returns.
2011 * @fv: #FlatView to be accessed
2012 * @addr: address within that address space
2013 * @xlat: pointer to address within the returned memory region section's
2015 * @len: pointer to length
2016 * @is_write: indicates the transfer direction
2017 * @attrs: memory attributes
2019 MemoryRegion
*flatview_translate(FlatView
*fv
,
2020 hwaddr addr
, hwaddr
*xlat
,
2021 hwaddr
*len
, bool is_write
,
2024 static inline MemoryRegion
*address_space_translate(AddressSpace
*as
,
2025 hwaddr addr
, hwaddr
*xlat
,
2026 hwaddr
*len
, bool is_write
,
2029 return flatview_translate(address_space_to_flatview(as
),
2030 addr
, xlat
, len
, is_write
, attrs
);
2033 /* address_space_access_valid: check for validity of accessing an address
2036 * Check whether memory is assigned to the given address space range, and
2037 * access is permitted by any IOMMU regions that are active for the address
2040 * For now, addr and len should be aligned to a page size. This limitation
2041 * will be lifted in the future.
2043 * @as: #AddressSpace to be accessed
2044 * @addr: address within that address space
2045 * @len: length of the area to be checked
2046 * @is_write: indicates the transfer direction
2047 * @attrs: memory attributes
2049 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, hwaddr len
,
2050 bool is_write
, MemTxAttrs attrs
);
2052 /* address_space_map: map a physical memory region into a host virtual address
2054 * May map a subset of the requested range, given by and returned in @plen.
2055 * May return %NULL if resources needed to perform the mapping are exhausted.
2056 * Use only for reads OR writes - not for read-modify-write operations.
2057 * Use cpu_register_map_client() to know when retrying the map operation is
2058 * likely to succeed.
2060 * @as: #AddressSpace to be accessed
2061 * @addr: address within that address space
2062 * @plen: pointer to length of buffer; updated on return
2063 * @is_write: indicates the transfer direction
2064 * @attrs: memory attributes
2066 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
2067 hwaddr
*plen
, bool is_write
, MemTxAttrs attrs
);
2069 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2071 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2072 * the amount of memory that was actually read or written by the caller.
2074 * @as: #AddressSpace used
2075 * @buffer: host pointer as returned by address_space_map()
2076 * @len: buffer length as returned by address_space_map()
2077 * @access_len: amount of data actually transferred
2078 * @is_write: indicates the transfer direction
2080 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
2081 int is_write
, hwaddr access_len
);
2084 /* Internal functions, part of the implementation of address_space_read. */
2085 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
2086 MemTxAttrs attrs
, uint8_t *buf
, hwaddr len
);
2087 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
2088 MemTxAttrs attrs
, uint8_t *buf
,
2089 hwaddr len
, hwaddr addr1
, hwaddr l
,
2091 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
2093 /* Internal functions, part of the implementation of address_space_read_cached
2094 * and address_space_write_cached. */
2095 void address_space_read_cached_slow(MemoryRegionCache
*cache
,
2096 hwaddr addr
, void *buf
, hwaddr len
);
2097 void address_space_write_cached_slow(MemoryRegionCache
*cache
,
2098 hwaddr addr
, const void *buf
, hwaddr len
);
2100 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
2103 return memory_region_is_ram(mr
) &&
2104 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
2106 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
2107 memory_region_is_romd(mr
);
2112 * address_space_read: read from an address space.
2114 * Return a MemTxResult indicating whether the operation succeeded
2115 * or failed (eg unassigned memory, device rejected the transaction,
2116 * IOMMU fault). Called within RCU critical section.
2118 * @as: #AddressSpace to be accessed
2119 * @addr: address within that address space
2120 * @attrs: memory transaction attributes
2121 * @buf: buffer with the data transferred
2123 static inline __attribute__((__always_inline__
))
2124 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
2125 MemTxAttrs attrs
, uint8_t *buf
,
2128 MemTxResult result
= MEMTX_OK
;
2134 if (__builtin_constant_p(len
)) {
2137 fv
= address_space_to_flatview(as
);
2139 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
2140 if (len
== l
&& memory_access_is_direct(mr
, false)) {
2141 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
2142 memcpy(buf
, ptr
, len
);
2144 result
= flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
2150 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
2156 * address_space_read_cached: read from a cached RAM region
2158 * @cache: Cached region to be addressed
2159 * @addr: address relative to the base of the RAM region
2160 * @buf: buffer with the data transferred
2161 * @len: length of the data transferred
2164 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2165 void *buf
, hwaddr len
)
2167 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2168 if (likely(cache
->ptr
)) {
2169 memcpy(buf
, cache
->ptr
+ addr
, len
);
2171 address_space_read_cached_slow(cache
, addr
, buf
, len
);
2176 * address_space_write_cached: write to a cached RAM region
2178 * @cache: Cached region to be addressed
2179 * @addr: address relative to the base of the RAM region
2180 * @buf: buffer with the data transferred
2181 * @len: length of the data transferred
2184 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2185 void *buf
, hwaddr len
)
2187 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2188 if (likely(cache
->ptr
)) {
2189 memcpy(cache
->ptr
+ addr
, buf
, len
);
2191 address_space_write_cached_slow(cache
, addr
, buf
, len
);