2 * Physical memory management API
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
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/bswap.h"
24 #include "qemu/queue.h"
25 #include "qemu/int128.h"
26 #include "qemu/notify.h"
27 #include "qom/object.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 typedef struct MemoryRegionClass {
210 ObjectClass parent_class;
214 enum IOMMUMemoryRegionAttr {
215 IOMMU_ATTR_SPAPR_TCE_FD
219 * IOMMUMemoryRegionClass:
221 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
222 * and provide an implementation of at least the @translate method here
223 * to handle requests to the memory region. Other methods are optional.
225 * The IOMMU implementation must use the IOMMU notifier infrastructure
226 * to report whenever mappings are changed, by calling
227 * memory_region_notify_iommu() (or, if necessary, by calling
228 * memory_region_notify_one() for each registered notifier).
230 * Conceptually an IOMMU provides a mapping from input address
231 * to an output TLB entry. If the IOMMU is aware of memory transaction
232 * attributes and the output TLB entry depends on the transaction
233 * attributes, we represent this using IOMMU indexes. Each index
234 * selects a particular translation table that the IOMMU has:
235 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
236 * @translate takes an input address and an IOMMU index
237 * and the mapping returned can only depend on the input address and the
240 * Most IOMMUs don't care about the transaction attributes and support
241 * only a single IOMMU index. A more complex IOMMU might have one index
242 * for secure transactions and one for non-secure transactions.
244 typedef struct IOMMUMemoryRegionClass {
246 MemoryRegionClass parent_class;
249 * Return a TLB entry that contains a given address.
251 * The IOMMUAccessFlags indicated via @flag are optional and may
252 * be specified as IOMMU_NONE to indicate that the caller needs
253 * the full translation information for both reads and writes. If
254 * the access flags are specified then the IOMMU implementation
255 * may use this as an optimization, to stop doing a page table
256 * walk as soon as it knows that the requested permissions are not
257 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
258 * full page table walk and report the permissions in the returned
259 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
260 * return different mappings for reads and writes.)
262 * The returned information remains valid while the caller is
263 * holding the big QEMU lock or is inside an RCU critical section;
264 * if the caller wishes to cache the mapping beyond that it must
265 * register an IOMMU notifier so it can invalidate its cached
266 * information when the IOMMU mapping changes.
268 * @iommu: the IOMMUMemoryRegion
269 * @hwaddr: address to be translated within the memory region
270 * @flag: requested access permissions
271 * @iommu_idx: IOMMU index for the translation
273 IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr,
274 IOMMUAccessFlags flag, int iommu_idx);
275 /* Returns minimum supported page size in bytes.
276 * If this method is not provided then the minimum is assumed to
277 * be TARGET_PAGE_SIZE.
279 * @iommu: the IOMMUMemoryRegion
281 uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu);
282 /* Called when IOMMU Notifier flag changes (ie when the set of
283 * events which IOMMU users are requesting notification for changes).
284 * Optional method -- need not be provided if the IOMMU does not
285 * need to know exactly which events must be notified.
287 * @iommu: the IOMMUMemoryRegion
288 * @old_flags: events which previously needed to be notified
289 * @new_flags: events which now need to be notified
291 void (*notify_flag_changed)(IOMMUMemoryRegion *iommu,
292 IOMMUNotifierFlag old_flags,
293 IOMMUNotifierFlag new_flags);
294 /* Called to handle memory_region_iommu_replay().
296 * The default implementation of memory_region_iommu_replay() is to
297 * call the IOMMU translate method for every page in the address space
298 * with flag == IOMMU_NONE and then call the notifier if translate
299 * returns a valid mapping. If this method is implemented then it
300 * overrides the default behaviour, and must provide the full semantics
301 * of memory_region_iommu_replay(), by calling @notifier for every
302 * translation present in the IOMMU.
304 * Optional method -- an IOMMU only needs to provide this method
305 * if the default is inefficient or produces undesirable side effects.
307 * Note: this is not related to record-and-replay functionality.
309 void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier);
311 /* Get IOMMU misc attributes. This is an optional method that
312 * can be used to allow users of the IOMMU to get implementation-specific
313 * information. The IOMMU implements this method to handle calls
314 * by IOMMU users to memory_region_iommu_get_attr() by filling in
315 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
316 * the IOMMU supports. If the method is unimplemented then
317 * memory_region_iommu_get_attr() will always return -EINVAL.
319 * @iommu: the IOMMUMemoryRegion
320 * @attr: attribute being queried
321 * @data: memory to fill in with the attribute data
323 * Returns 0 on success, or a negative errno; in particular
324 * returns -EINVAL for unrecognized or unimplemented attribute types.
326 int (*get_attr)(IOMMUMemoryRegion *iommu, enum IOMMUMemoryRegionAttr attr,
329 /* Return the IOMMU index to use for a given set of transaction attributes.
331 * Optional method: if an IOMMU only supports a single IOMMU index then
332 * the default implementation of memory_region_iommu_attrs_to_index()
335 * The indexes supported by an IOMMU must be contiguous, starting at 0.
337 * @iommu: the IOMMUMemoryRegion
338 * @attrs: memory transaction attributes
340 int (*attrs_to_index)(IOMMUMemoryRegion *iommu, MemTxAttrs attrs);
342 /* Return the number of IOMMU indexes this IOMMU supports.
344 * Optional method: if this method is not provided, then
345 * memory_region_iommu_num_indexes() will return 1, indicating that
346 * only a single IOMMU index is supported.
348 * @iommu: the IOMMUMemoryRegion
350 int (*num_indexes)(IOMMUMemoryRegion *iommu);
351 } IOMMUMemoryRegionClass;
353 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
354 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
356 struct MemoryRegion {
359 /* All fields are private - violators will be prosecuted */
361 /* The following fields should fit in a cache line */
365 bool readonly; /* For RAM regions */
368 bool flush_coalesced_mmio;
370 uint8_t dirty_log_mask;
375 const MemoryRegionOps *ops;
377 MemoryRegion *container;
380 void (*destructor)(MemoryRegion *mr);
385 bool warning_printed; /* For reservations */
386 uint8_t vga_logging_count;
390 QTAILQ_HEAD(, MemoryRegion) subregions;
391 QTAILQ_ENTRY(MemoryRegion) subregions_link;
392 QTAILQ_HEAD(, CoalescedMemoryRange) coalesced;
394 unsigned ioeventfd_nb;
395 MemoryRegionIoeventfd *ioeventfds;
398 struct IOMMUMemoryRegion {
399 MemoryRegion parent_obj;
401 QLIST_HEAD(, IOMMUNotifier) iommu_notify;
402 IOMMUNotifierFlag iommu_notify_flags;
405 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
406 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
409 * MemoryListener: callbacks structure for updates to the physical memory map
411 * Allows a component to adjust to changes in the guest-visible memory map.
412 * Use with memory_listener_register() and memory_listener_unregister().
414 struct MemoryListener {
415 void (*begin)(MemoryListener *listener);
416 void (*commit)(MemoryListener *listener);
417 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
418 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
419 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
420 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
422 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
424 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
425 void (*log_clear)(MemoryListener *listener, MemoryRegionSection *section);
426 void (*log_global_start)(MemoryListener *listener);
427 void (*log_global_stop)(MemoryListener *listener);
428 void (*log_global_after_sync)(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_io_add)(MemoryListener *listener, MemoryRegionSection *section,
434 hwaddr addr, hwaddr len);
435 void (*coalesced_io_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(, 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
494 * @nonvolatile: this section is non-volatile
496 struct MemoryRegionSection {
499 hwaddr offset_within_region;
501 hwaddr offset_within_address_space;
507 * memory_region_init: Initialize a memory region
509 * The region typically acts as a container for other memory regions. Use
510 * memory_region_add_subregion() to add subregions.
512 * @mr: the #MemoryRegion to be initialized
513 * @owner: the object that tracks the region's reference count
514 * @name: used for debugging; not visible to the user or ABI
515 * @size: size of the region; any subregions beyond this size will be clipped
517 void memory_region_init(MemoryRegion *mr,
518 struct Object *owner,
523 * memory_region_ref: Add 1 to a memory region's reference count
525 * Whenever memory regions are accessed outside the BQL, they need to be
526 * preserved against hot-unplug. MemoryRegions actually do not have their
527 * own reference count; they piggyback on a QOM object, their "owner".
528 * This function adds a reference to the owner.
530 * All MemoryRegions must have an owner if they can disappear, even if the
531 * device they belong to operates exclusively under the BQL. This is because
532 * the region could be returned at any time by memory_region_find, and this
533 * is usually under guest control.
535 * @mr: the #MemoryRegion
537 void memory_region_ref(MemoryRegion *mr);
540 * memory_region_unref: Remove 1 to a memory region's reference count
542 * Whenever memory regions are accessed outside the BQL, they need to be
543 * preserved against hot-unplug. MemoryRegions actually do not have their
544 * own reference count; they piggyback on a QOM object, their "owner".
545 * This function removes a reference to the owner and possibly destroys it.
547 * @mr: the #MemoryRegion
549 void memory_region_unref(MemoryRegion *mr);
552 * memory_region_init_io: Initialize an I/O memory region.
554 * Accesses into the region will cause the callbacks in @ops to be called.
555 * if @size is nonzero, subregions will be clipped to @size.
557 * @mr: the #MemoryRegion to be initialized.
558 * @owner: the object that tracks the region's reference count
559 * @ops: a structure containing read and write callbacks to be used when
560 * I/O is performed on the region.
561 * @opaque: passed to the read and write callbacks of the @ops structure.
562 * @name: used for debugging; not visible to the user or ABI
563 * @size: size of the region.
565 void memory_region_init_io(MemoryRegion *mr,
566 struct Object *owner,
567 const MemoryRegionOps *ops,
573 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
574 * into the region will modify memory
577 * @mr: the #MemoryRegion to be initialized.
578 * @owner: the object that tracks the region's reference count
579 * @name: Region name, becomes part of RAMBlock name used in migration stream
580 * must be unique within any device
581 * @size: size of the region.
582 * @errp: pointer to Error*, to store an error if it happens.
584 * Note that this function does not do anything to cause the data in the
585 * RAM memory region to be migrated; that is the responsibility of the caller.
587 void memory_region_init_ram_nomigrate(MemoryRegion *mr,
588 struct Object *owner,
594 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
595 * Accesses into the region will
596 * modify memory directly.
598 * @mr: the #MemoryRegion to be initialized.
599 * @owner: the object that tracks the region's reference count
600 * @name: Region name, becomes part of RAMBlock name used in migration stream
601 * must be unique within any device
602 * @size: size of the region.
603 * @share: allow remapping RAM to different addresses
604 * @errp: pointer to Error*, to store an error if it happens.
606 * Note that this function is similar to memory_region_init_ram_nomigrate.
607 * The only difference is part of the RAM region can be remapped.
609 void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
610 struct Object *owner,
617 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
618 * RAM. Accesses into the region will
619 * modify memory directly. Only an initial
620 * portion of this RAM is actually used.
621 * The used size can change across reboots.
623 * @mr: the #MemoryRegion to be initialized.
624 * @owner: the object that tracks the region's reference count
625 * @name: Region name, becomes part of RAMBlock name used in migration stream
626 * must be unique within any device
627 * @size: used size of the region.
628 * @max_size: max size of the region.
629 * @resized: callback to notify owner about used size change.
630 * @errp: pointer to Error*, to store an error if it happens.
632 * Note that this function does not do anything to cause the data in the
633 * RAM memory region to be migrated; that is the responsibility of the caller.
635 void memory_region_init_resizeable_ram(MemoryRegion *mr,
636 struct Object *owner,
640 void (*resized)(const char*,
647 * memory_region_init_ram_from_file: Initialize RAM memory region with a
650 * @mr: the #MemoryRegion to be initialized.
651 * @owner: the object that tracks the region's reference count
652 * @name: Region name, becomes part of RAMBlock name used in migration stream
653 * must be unique within any device
654 * @size: size of the region.
655 * @align: alignment of the region base address; if 0, the default alignment
656 * (getpagesize()) will be used.
657 * @ram_flags: Memory region features:
658 * - RAM_SHARED: memory must be mmaped with the MAP_SHARED flag
659 * - RAM_PMEM: the memory is persistent memory
660 * Other bits are ignored now.
661 * @path: the path in which to allocate the RAM.
662 * @errp: pointer to Error*, to store an error if it happens.
664 * Note that this function does not do anything to cause the data in the
665 * RAM memory region to be migrated; that is the responsibility of the caller.
667 void memory_region_init_ram_from_file(MemoryRegion *mr,
668 struct Object *owner,
677 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
680 * @mr: the #MemoryRegion to be initialized.
681 * @owner: the object that tracks the region's reference count
682 * @name: the name of the region.
683 * @size: size of the region.
684 * @share: %true if memory must be mmaped with the MAP_SHARED flag
685 * @fd: the fd to mmap.
686 * @errp: pointer to Error*, to store an error if it happens.
688 * Note that this function does not do anything to cause the data in the
689 * RAM memory region to be migrated; that is the responsibility of the caller.
691 void memory_region_init_ram_from_fd(MemoryRegion *mr,
692 struct Object *owner,
701 * memory_region_init_ram_ptr: Initialize RAM memory region from a
702 * user-provided pointer. Accesses into the
703 * region will modify memory directly.
705 * @mr: the #MemoryRegion to be initialized.
706 * @owner: the object that tracks the region's reference count
707 * @name: Region name, becomes part of RAMBlock name used in migration stream
708 * must be unique within any device
709 * @size: size of the region.
710 * @ptr: memory to be mapped; must contain at least @size bytes.
712 * Note that this function does not do anything to cause the data in the
713 * RAM memory region to be migrated; that is the responsibility of the caller.
715 void memory_region_init_ram_ptr(MemoryRegion *mr,
716 struct Object *owner,
722 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
723 * a user-provided pointer.
725 * A RAM device represents a mapping to a physical device, such as to a PCI
726 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
727 * into the VM address space and access to the region will modify memory
728 * directly. However, the memory region should not be included in a memory
729 * dump (device may not be enabled/mapped at the time of the dump), and
730 * operations incompatible with manipulating MMIO should be avoided. Replaces
733 * @mr: the #MemoryRegion to be initialized.
734 * @owner: the object that tracks the region's reference count
735 * @name: the name of the region.
736 * @size: size of the region.
737 * @ptr: memory to be mapped; must contain at least @size bytes.
739 * Note that this function does not do anything to cause the data in the
740 * RAM memory region to be migrated; that is the responsibility of the caller.
741 * (For RAM device memory regions, migrating the contents rarely makes sense.)
743 void memory_region_init_ram_device_ptr(MemoryRegion *mr,
744 struct Object *owner,
750 * memory_region_init_alias: Initialize a memory region that aliases all or a
751 * part of another memory region.
753 * @mr: the #MemoryRegion to be initialized.
754 * @owner: the object that tracks the region's reference count
755 * @name: used for debugging; not visible to the user or ABI
756 * @orig: the region to be referenced; @mr will be equivalent to
757 * @orig between @offset and @offset + @size - 1.
758 * @offset: start of the section in @orig to be referenced.
759 * @size: size of the region.
761 void memory_region_init_alias(MemoryRegion *mr,
762 struct Object *owner,
769 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
771 * This has the same effect as calling memory_region_init_ram_nomigrate()
772 * and then marking the resulting region read-only with
773 * memory_region_set_readonly().
775 * Note that this function does not do anything to cause the data in the
776 * RAM side of the memory region to be migrated; that is the responsibility
779 * @mr: the #MemoryRegion to be initialized.
780 * @owner: the object that tracks the region's reference count
781 * @name: Region name, becomes part of RAMBlock name used in migration stream
782 * must be unique within any device
783 * @size: size of the region.
784 * @errp: pointer to Error*, to store an error if it happens.
786 void memory_region_init_rom_nomigrate(MemoryRegion *mr,
787 struct Object *owner,
793 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
794 * Writes are handled via callbacks.
796 * Note that this function does not do anything to cause the data in the
797 * RAM side of the memory region to be migrated; that is the responsibility
800 * @mr: the #MemoryRegion to be initialized.
801 * @owner: the object that tracks the region's reference count
802 * @ops: callbacks for write access handling (must not be NULL).
803 * @opaque: passed to the read and write callbacks of the @ops structure.
804 * @name: Region name, becomes part of RAMBlock name used in migration stream
805 * must be unique within any device
806 * @size: size of the region.
807 * @errp: pointer to Error*, to store an error if it happens.
809 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
810 struct Object *owner,
811 const MemoryRegionOps *ops,
818 * memory_region_init_iommu: Initialize a memory region of a custom type
819 * that translates addresses
821 * An IOMMU region translates addresses and forwards accesses to a target
824 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
825 * @_iommu_mr should be a pointer to enough memory for an instance of
826 * that subclass, @instance_size is the size of that subclass, and
827 * @mrtypename is its name. This function will initialize @_iommu_mr as an
828 * instance of the subclass, and its methods will then be called to handle
829 * accesses to the memory region. See the documentation of
830 * #IOMMUMemoryRegionClass for further details.
832 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
833 * @instance_size: the IOMMUMemoryRegion subclass instance size
834 * @mrtypename: the type name of the #IOMMUMemoryRegion
835 * @owner: the object that tracks the region's reference count
836 * @name: used for debugging; not visible to the user or ABI
837 * @size: size of the region.
839 void memory_region_init_iommu(void *_iommu_mr,
840 size_t instance_size,
841 const char *mrtypename,
847 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
848 * region will modify memory directly.
850 * @mr: the #MemoryRegion to be initialized
851 * @owner: the object that tracks the region's reference count (must be
852 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
853 * @name: name of the memory region
854 * @size: size of the region in bytes
855 * @errp: pointer to Error*, to store an error if it happens.
857 * This function allocates RAM for a board model or device, and
858 * arranges for it to be migrated (by calling vmstate_register_ram()
859 * if @owner is a DeviceState, or vmstate_register_ram_global() if
862 * TODO: Currently we restrict @owner to being either NULL (for
863 * global RAM regions with no owner) or devices, so that we can
864 * give the RAM block a unique name for migration purposes.
865 * We should lift this restriction and allow arbitrary Objects.
866 * If you pass a non-NULL non-device @owner then we will assert.
868 void memory_region_init_ram(MemoryRegion *mr,
869 struct Object *owner,
875 * memory_region_init_rom: Initialize a ROM memory region.
877 * This has the same effect as calling memory_region_init_ram()
878 * and then marking the resulting region read-only with
879 * memory_region_set_readonly(). This includes arranging for the
880 * contents to be migrated.
882 * TODO: Currently we restrict @owner to being either NULL (for
883 * global RAM regions with no owner) or devices, so that we can
884 * give the RAM block a unique name for migration purposes.
885 * We should lift this restriction and allow arbitrary Objects.
886 * If you pass a non-NULL non-device @owner then we will assert.
888 * @mr: the #MemoryRegion to be initialized.
889 * @owner: the object that tracks the region's reference count
890 * @name: Region name, becomes part of RAMBlock name used in migration stream
891 * must be unique within any device
892 * @size: size of the region.
893 * @errp: pointer to Error*, to store an error if it happens.
895 void memory_region_init_rom(MemoryRegion *mr,
896 struct Object *owner,
902 * memory_region_init_rom_device: Initialize a ROM memory region.
903 * Writes are handled via callbacks.
905 * This function initializes a memory region backed by RAM for reads
906 * and callbacks for writes, and arranges for the RAM backing to
907 * be migrated (by calling vmstate_register_ram()
908 * if @owner is a DeviceState, or vmstate_register_ram_global() if
911 * TODO: Currently we restrict @owner to being either NULL (for
912 * global RAM regions with no owner) or devices, so that we can
913 * give the RAM block a unique name for migration purposes.
914 * We should lift this restriction and allow arbitrary Objects.
915 * If you pass a non-NULL non-device @owner then we will assert.
917 * @mr: the #MemoryRegion to be initialized.
918 * @owner: the object that tracks the region's reference count
919 * @ops: callbacks for write access handling (must not be NULL).
920 * @name: Region name, becomes part of RAMBlock name used in migration stream
921 * must be unique within any device
922 * @size: size of the region.
923 * @errp: pointer to Error*, to store an error if it happens.
925 void memory_region_init_rom_device(MemoryRegion *mr,
926 struct Object *owner,
927 const MemoryRegionOps *ops,
935 * memory_region_owner: get a memory region's owner.
937 * @mr: the memory region being queried.
939 struct Object *memory_region_owner(MemoryRegion *mr);
942 * memory_region_size: get a memory region's size.
944 * @mr: the memory region being queried.
946 uint64_t memory_region_size(MemoryRegion *mr);
949 * memory_region_is_ram: check whether a memory region is random access
951 * Returns %true if a memory region is random access.
953 * @mr: the memory region being queried
955 static inline bool memory_region_is_ram(MemoryRegion *mr)
961 * memory_region_is_ram_device: check whether a memory region is a ram device
963 * Returns %true if a memory region is a device backed ram region
965 * @mr: the memory region being queried
967 bool memory_region_is_ram_device(MemoryRegion *mr);
970 * memory_region_is_romd: check whether a memory region is in ROMD mode
972 * Returns %true if a memory region is a ROM device and currently set to allow
975 * @mr: the memory region being queried
977 static inline bool memory_region_is_romd(MemoryRegion *mr)
979 return mr->rom_device && mr->romd_mode;
983 * memory_region_get_iommu: check whether a memory region is an iommu
985 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
988 * @mr: the memory region being queried
990 static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
993 return memory_region_get_iommu(mr->alias);
996 return (IOMMUMemoryRegion *) mr;
1002 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
1003 * if an iommu or NULL if not
1005 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
1006 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1008 * @mr: the memory region being queried
1010 static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck(
1011 IOMMUMemoryRegion *iommu_mr)
1013 return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class);
1016 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1019 * memory_region_iommu_get_min_page_size: get minimum supported page size
1022 * Returns minimum supported page size for an iommu.
1024 * @iommu_mr: the memory region being queried
1026 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
1029 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1031 * The notification type will be decided by entry.perm bits:
1033 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
1034 * - For MAP (newly added entry) notifies: set entry.perm to the
1035 * permission of the page (which is definitely !IOMMU_NONE).
1037 * Note: for any IOMMU implementation, an in-place mapping change
1038 * should be notified with an UNMAP followed by a MAP.
1040 * @iommu_mr: the memory region that was changed
1041 * @iommu_idx: the IOMMU index for the translation table which has changed
1042 * @entry: the new entry in the IOMMU translation table. The entry
1043 * replaces all old entries for the same virtual I/O address range.
1044 * Deleted entries have .@perm == 0.
1046 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1048 IOMMUTLBEntry entry);
1051 * memory_region_notify_one: notify a change in an IOMMU translation
1052 * entry to a single notifier
1054 * This works just like memory_region_notify_iommu(), but it only
1055 * notifies a specific notifier, not all of them.
1057 * @notifier: the notifier to be notified
1058 * @entry: the new entry in the IOMMU translation table. The entry
1059 * replaces all old entries for the same virtual I/O address range.
1060 * Deleted entries have .@perm == 0.
1062 void memory_region_notify_one(IOMMUNotifier *notifier,
1063 IOMMUTLBEntry *entry);
1066 * memory_region_register_iommu_notifier: register a notifier for changes to
1067 * IOMMU translation entries.
1069 * @mr: the memory region to observe
1070 * @n: the IOMMUNotifier to be added; the notify callback receives a
1071 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1072 * ceases to be valid on exit from the notifier.
1074 void memory_region_register_iommu_notifier(MemoryRegion *mr,
1078 * memory_region_iommu_replay: replay existing IOMMU translations to
1079 * a notifier with the minimum page granularity returned by
1080 * mr->iommu_ops->get_page_size().
1082 * Note: this is not related to record-and-replay functionality.
1084 * @iommu_mr: the memory region to observe
1085 * @n: the notifier to which to replay iommu mappings
1087 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
1090 * memory_region_iommu_replay_all: replay existing IOMMU translations
1091 * to all the notifiers registered.
1093 * Note: this is not related to record-and-replay functionality.
1095 * @iommu_mr: the memory region to observe
1097 void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr);
1100 * memory_region_unregister_iommu_notifier: unregister a notifier for
1101 * changes to IOMMU translation entries.
1103 * @mr: the memory region which was observed and for which notity_stopped()
1104 * needs to be called
1105 * @n: the notifier to be removed.
1107 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1111 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1112 * defined on the IOMMU.
1114 * Returns 0 on success, or a negative errno otherwise. In particular,
1115 * -EINVAL indicates that the IOMMU does not support the requested
1118 * @iommu_mr: the memory region
1119 * @attr: the requested attribute
1120 * @data: a pointer to the requested attribute data
1122 int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1123 enum IOMMUMemoryRegionAttr attr,
1127 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1128 * use for translations with the given memory transaction attributes.
1130 * @iommu_mr: the memory region
1131 * @attrs: the memory transaction attributes
1133 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr,
1137 * memory_region_iommu_num_indexes: return the total number of IOMMU
1138 * indexes that this IOMMU supports.
1140 * @iommu_mr: the memory region
1142 int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr);
1145 * memory_region_name: get a memory region's name
1147 * Returns the string that was used to initialize the memory region.
1149 * @mr: the memory region being queried
1151 const char *memory_region_name(const MemoryRegion *mr);
1154 * memory_region_is_logging: return whether a memory region is logging writes
1156 * Returns %true if the memory region is logging writes for the given client
1158 * @mr: the memory region being queried
1159 * @client: the client being queried
1161 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
1164 * memory_region_get_dirty_log_mask: return the clients for which a
1165 * memory region is logging writes.
1167 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1168 * are the bit indices.
1170 * @mr: the memory region being queried
1172 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
1175 * memory_region_is_rom: check whether a memory region is ROM
1177 * Returns %true if a memory region is read-only memory.
1179 * @mr: the memory region being queried
1181 static inline bool memory_region_is_rom(MemoryRegion *mr)
1183 return mr->ram && mr->readonly;
1187 * memory_region_is_nonvolatile: check whether a memory region is non-volatile
1189 * Returns %true is a memory region is non-volatile memory.
1191 * @mr: the memory region being queried
1193 static inline bool memory_region_is_nonvolatile(MemoryRegion *mr)
1195 return mr->nonvolatile;
1199 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1201 * Returns a file descriptor backing a file-based RAM memory region,
1202 * or -1 if the region is not a file-based RAM memory region.
1204 * @mr: the RAM or alias memory region being queried.
1206 int memory_region_get_fd(MemoryRegion *mr);
1209 * memory_region_from_host: Convert a pointer into a RAM memory region
1210 * and an offset within it.
1212 * Given a host pointer inside a RAM memory region (created with
1213 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1214 * the MemoryRegion and the offset within it.
1216 * Use with care; by the time this function returns, the returned pointer is
1217 * not protected by RCU anymore. If the caller is not within an RCU critical
1218 * section and does not hold the iothread lock, it must have other means of
1219 * protecting the pointer, such as a reference to the region that includes
1220 * the incoming ram_addr_t.
1222 * @ptr: the host pointer to be converted
1223 * @offset: the offset within memory region
1225 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
1228 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1230 * Returns a host pointer to a RAM memory region (created with
1231 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1233 * Use with care; by the time this function returns, the returned pointer is
1234 * not protected by RCU anymore. If the caller is not within an RCU critical
1235 * section and does not hold the iothread lock, it must have other means of
1236 * protecting the pointer, such as a reference to the region that includes
1237 * the incoming ram_addr_t.
1239 * @mr: the memory region being queried.
1241 void *memory_region_get_ram_ptr(MemoryRegion *mr);
1243 /* memory_region_ram_resize: Resize a RAM region.
1245 * Only legal before guest might have detected the memory size: e.g. on
1246 * incoming migration, or right after reset.
1248 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1249 * @newsize: the new size the region
1250 * @errp: pointer to Error*, to store an error if it happens.
1252 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
1256 * memory_region_set_log: Turn dirty logging on or off for a region.
1258 * Turns dirty logging on or off for a specified client (display, migration).
1259 * Only meaningful for RAM regions.
1261 * @mr: the memory region being updated.
1262 * @log: whether dirty logging is to be enabled or disabled.
1263 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1265 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
1268 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1270 * Marks a range of bytes as dirty, after it has been dirtied outside
1273 * @mr: the memory region being dirtied.
1274 * @addr: the address (relative to the start of the region) being dirtied.
1275 * @size: size of the range being dirtied.
1277 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1281 * memory_region_clear_dirty_bitmap - clear dirty bitmap for memory range
1283 * This function is called when the caller wants to clear the remote
1284 * dirty bitmap of a memory range within the memory region. This can
1285 * be used by e.g. KVM to manually clear dirty log when
1286 * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT is declared support by the host
1289 * @mr: the memory region to clear the dirty log upon
1290 * @start: start address offset within the memory region
1291 * @len: length of the memory region to clear dirty bitmap
1293 void memory_region_clear_dirty_bitmap(MemoryRegion *mr, hwaddr start,
1297 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1298 * bitmap and clear it.
1300 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1301 * returns the snapshot. The snapshot can then be used to query dirty
1302 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1303 * querying the same page multiple times, which is especially useful for
1304 * display updates where the scanlines often are not page aligned.
1306 * The dirty bitmap region which gets copyed into the snapshot (and
1307 * cleared afterwards) can be larger than requested. The boundaries
1308 * are rounded up/down so complete bitmap longs (covering 64 pages on
1309 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1310 * isn't a problem for display updates as the extra pages are outside
1311 * the visible area, and in case the visible area changes a full
1312 * display redraw is due anyway. Should other use cases for this
1313 * function emerge we might have to revisit this implementation
1316 * Use g_free to release DirtyBitmapSnapshot.
1318 * @mr: the memory region being queried.
1319 * @addr: the address (relative to the start of the region) being queried.
1320 * @size: the size of the range being queried.
1321 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1323 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
1329 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1330 * in the specified dirty bitmap snapshot.
1332 * @mr: the memory region being queried.
1333 * @snap: the dirty bitmap snapshot
1334 * @addr: the address (relative to the start of the region) being queried.
1335 * @size: the size of the range being queried.
1337 bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
1338 DirtyBitmapSnapshot *snap,
1339 hwaddr addr, hwaddr size);
1342 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1345 * Marks a range of pages as no longer dirty.
1347 * @mr: the region being updated.
1348 * @addr: the start of the subrange being cleaned.
1349 * @size: the size of the subrange being cleaned.
1350 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1351 * %DIRTY_MEMORY_VGA.
1353 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1354 hwaddr size, unsigned client);
1357 * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate
1358 * TBs (for self-modifying code).
1360 * The MemoryRegionOps->write() callback of a ROM device must use this function
1361 * to mark byte ranges that have been modified internally, such as by directly
1362 * accessing the memory returned by memory_region_get_ram_ptr().
1364 * This function marks the range dirty and invalidates TBs so that TCG can
1365 * detect self-modifying code.
1367 * @mr: the region being flushed.
1368 * @addr: the start, relative to the start of the region, of the range being
1370 * @size: the size, in bytes, of the range being flushed.
1372 void memory_region_flush_rom_device(MemoryRegion *mr, hwaddr addr, hwaddr size);
1375 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1377 * Allows a memory region to be marked as read-only (turning it into a ROM).
1378 * only useful on RAM regions.
1380 * @mr: the region being updated.
1381 * @readonly: whether rhe region is to be ROM or RAM.
1383 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
1386 * memory_region_set_nonvolatile: Turn a memory region non-volatile
1388 * Allows a memory region to be marked as non-volatile.
1389 * only useful on RAM regions.
1391 * @mr: the region being updated.
1392 * @nonvolatile: whether rhe region is to be non-volatile.
1394 void memory_region_set_nonvolatile(MemoryRegion *mr, bool nonvolatile);
1397 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1399 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1400 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1401 * device is mapped to guest memory and satisfies read access directly.
1402 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1403 * Writes are always handled by the #MemoryRegion.write function.
1405 * @mr: the memory region to be updated
1406 * @romd_mode: %true to put the region into ROMD mode
1408 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
1411 * memory_region_set_coalescing: Enable memory coalescing for the region.
1413 * Enabled writes to a region to be queued for later processing. MMIO ->write
1414 * callbacks may be delayed until a non-coalesced MMIO is issued.
1415 * Only useful for IO regions. Roughly similar to write-combining hardware.
1417 * @mr: the memory region to be write coalesced
1419 void memory_region_set_coalescing(MemoryRegion *mr);
1422 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1425 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1426 * Multiple calls can be issued coalesced disjoint ranges.
1428 * @mr: the memory region to be updated.
1429 * @offset: the start of the range within the region to be coalesced.
1430 * @size: the size of the subrange to be coalesced.
1432 void memory_region_add_coalescing(MemoryRegion *mr,
1437 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1439 * Disables any coalescing caused by memory_region_set_coalescing() or
1440 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1443 * @mr: the memory region to be updated.
1445 void memory_region_clear_coalescing(MemoryRegion *mr);
1448 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1451 * Ensure that pending coalesced MMIO request are flushed before the memory
1452 * region is accessed. This property is automatically enabled for all regions
1453 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1455 * @mr: the memory region to be updated.
1457 void memory_region_set_flush_coalesced(MemoryRegion *mr);
1460 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1463 * Clear the automatic coalesced MMIO flushing enabled via
1464 * memory_region_set_flush_coalesced. Note that this service has no effect on
1465 * memory regions that have MMIO coalescing enabled for themselves. For them,
1466 * automatic flushing will stop once coalescing is disabled.
1468 * @mr: the memory region to be updated.
1470 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
1473 * memory_region_clear_global_locking: Declares that access processing does
1474 * not depend on the QEMU global lock.
1476 * By clearing this property, accesses to the memory region will be processed
1477 * outside of QEMU's global lock (unless the lock is held on when issuing the
1478 * access request). In this case, the device model implementing the access
1479 * handlers is responsible for synchronization of concurrency.
1481 * @mr: the memory region to be updated.
1483 void memory_region_clear_global_locking(MemoryRegion *mr);
1486 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1487 * is written to a location.
1489 * Marks a word in an IO region (initialized with memory_region_init_io())
1490 * as a trigger for an eventfd event. The I/O callback will not be called.
1491 * The caller must be prepared to handle failure (that is, take the required
1492 * action if the callback _is_ called).
1494 * @mr: the memory region being updated.
1495 * @addr: the address within @mr that is to be monitored
1496 * @size: the size of the access to trigger the eventfd
1497 * @match_data: whether to match against @data, instead of just @addr
1498 * @data: the data to match against the guest write
1499 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1501 void memory_region_add_eventfd(MemoryRegion *mr,
1509 * memory_region_del_eventfd: Cancel an eventfd.
1511 * Cancels an eventfd trigger requested by a previous
1512 * memory_region_add_eventfd() call.
1514 * @mr: the memory region being updated.
1515 * @addr: the address within @mr that is to be monitored
1516 * @size: the size of the access to trigger the eventfd
1517 * @match_data: whether to match against @data, instead of just @addr
1518 * @data: the data to match against the guest write
1519 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1521 void memory_region_del_eventfd(MemoryRegion *mr,
1529 * memory_region_add_subregion: Add a subregion to a container.
1531 * Adds a subregion at @offset. The subregion may not overlap with other
1532 * subregions (except for those explicitly marked as overlapping). A region
1533 * may only be added once as a subregion (unless removed with
1534 * memory_region_del_subregion()); use memory_region_init_alias() if you
1535 * want a region to be a subregion in multiple locations.
1537 * @mr: the region to contain the new subregion; must be a container
1538 * initialized with memory_region_init().
1539 * @offset: the offset relative to @mr where @subregion is added.
1540 * @subregion: the subregion to be added.
1542 void memory_region_add_subregion(MemoryRegion *mr,
1544 MemoryRegion *subregion);
1546 * memory_region_add_subregion_overlap: Add a subregion to a container
1549 * Adds a subregion at @offset. The subregion may overlap with other
1550 * subregions. Conflicts are resolved by having a higher @priority hide a
1551 * lower @priority. Subregions without priority are taken as @priority 0.
1552 * A region may only be added once as a subregion (unless removed with
1553 * memory_region_del_subregion()); use memory_region_init_alias() if you
1554 * want a region to be a subregion in multiple locations.
1556 * @mr: the region to contain the new subregion; must be a container
1557 * initialized with memory_region_init().
1558 * @offset: the offset relative to @mr where @subregion is added.
1559 * @subregion: the subregion to be added.
1560 * @priority: used for resolving overlaps; highest priority wins.
1562 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1564 MemoryRegion *subregion,
1568 * memory_region_get_ram_addr: Get the ram address associated with a memory
1571 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
1573 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
1575 * memory_region_del_subregion: Remove a subregion.
1577 * Removes a subregion from its container.
1579 * @mr: the container to be updated.
1580 * @subregion: the region being removed; must be a current subregion of @mr.
1582 void memory_region_del_subregion(MemoryRegion *mr,
1583 MemoryRegion *subregion);
1586 * memory_region_set_enabled: dynamically enable or disable a region
1588 * Enables or disables a memory region. A disabled memory region
1589 * ignores all accesses to itself and its subregions. It does not
1590 * obscure sibling subregions with lower priority - it simply behaves as
1591 * if it was removed from the hierarchy.
1593 * Regions default to being enabled.
1595 * @mr: the region to be updated
1596 * @enabled: whether to enable or disable the region
1598 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1601 * memory_region_set_address: dynamically update the address of a region
1603 * Dynamically updates the address of a region, relative to its container.
1604 * May be used on regions are currently part of a memory hierarchy.
1606 * @mr: the region to be updated
1607 * @addr: new address, relative to container region
1609 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
1612 * memory_region_set_size: dynamically update the size of a region.
1614 * Dynamically updates the size of a region.
1616 * @mr: the region to be updated
1617 * @size: used size of the region.
1619 void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1622 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1624 * Dynamically updates the offset into the target region that an alias points
1625 * to, as if the fourth argument to memory_region_init_alias() has changed.
1627 * @mr: the #MemoryRegion to be updated; should be an alias.
1628 * @offset: the new offset into the target memory region
1630 void memory_region_set_alias_offset(MemoryRegion *mr,
1634 * memory_region_present: checks if an address relative to a @container
1635 * translates into #MemoryRegion within @container
1637 * Answer whether a #MemoryRegion within @container covers the address
1640 * @container: a #MemoryRegion within which @addr is a relative address
1641 * @addr: the area within @container to be searched
1643 bool memory_region_present(MemoryRegion *container, hwaddr addr);
1646 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1647 * into any address space.
1649 * @mr: a #MemoryRegion which should be checked if it's mapped
1651 bool memory_region_is_mapped(MemoryRegion *mr);
1654 * memory_region_find: translate an address/size relative to a
1655 * MemoryRegion into a #MemoryRegionSection.
1657 * Locates the first #MemoryRegion within @mr that overlaps the range
1658 * given by @addr and @size.
1660 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1661 * It will have the following characteristics:
1662 * .@size = 0 iff no overlap was found
1663 * .@mr is non-%NULL iff an overlap was found
1665 * Remember that in the return value the @offset_within_region is
1666 * relative to the returned region (in the .@mr field), not to the
1669 * Similarly, the .@offset_within_address_space is relative to the
1670 * address space that contains both regions, the passed and the
1671 * returned one. However, in the special case where the @mr argument
1672 * has no container (and thus is the root of the address space), the
1673 * following will hold:
1674 * .@offset_within_address_space >= @addr
1675 * .@offset_within_address_space + .@size <= @addr + @size
1677 * @mr: a MemoryRegion within which @addr is a relative address
1678 * @addr: start of the area within @as to be searched
1679 * @size: size of the area to be searched
1681 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1682 hwaddr addr, uint64_t size);
1685 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1687 * Synchronizes the dirty page log for all address spaces.
1689 void memory_global_dirty_log_sync(void);
1692 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1694 * Synchronizes the vCPUs with a thread that is reading the dirty bitmap.
1695 * This function must be called after the dirty log bitmap is cleared, and
1696 * before dirty guest memory pages are read. If you are using
1697 * #DirtyBitmapSnapshot, memory_region_snapshot_and_clear_dirty() takes
1698 * care of doing this.
1700 void memory_global_after_dirty_log_sync(void);
1703 * memory_region_transaction_begin: Start a transaction.
1705 * During a transaction, changes will be accumulated and made visible
1706 * only when the transaction ends (is committed).
1708 void memory_region_transaction_begin(void);
1711 * memory_region_transaction_commit: Commit a transaction and make changes
1712 * visible to the guest.
1714 void memory_region_transaction_commit(void);
1717 * memory_listener_register: register callbacks to be called when memory
1718 * sections are mapped or unmapped into an address
1721 * @listener: an object containing the callbacks to be called
1722 * @filter: if non-%NULL, only regions in this address space will be observed
1724 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
1727 * memory_listener_unregister: undo the effect of memory_listener_register()
1729 * @listener: an object containing the callbacks to be removed
1731 void memory_listener_unregister(MemoryListener *listener);
1734 * memory_global_dirty_log_start: begin dirty logging for all regions
1736 void memory_global_dirty_log_start(void);
1739 * memory_global_dirty_log_stop: end dirty logging for all regions
1741 void memory_global_dirty_log_stop(void);
1743 void mtree_info(bool flatview, bool dispatch_tree, bool owner);
1746 * memory_region_dispatch_read: perform a read directly to the specified
1749 * @mr: #MemoryRegion to access
1750 * @addr: address within that region
1751 * @pval: pointer to uint64_t which the data is written to
1752 * @size: size of the access in bytes
1753 * @attrs: memory transaction attributes to use for the access
1755 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1761 * memory_region_dispatch_write: perform a write directly to the specified
1764 * @mr: #MemoryRegion to access
1765 * @addr: address within that region
1766 * @data: data to write
1767 * @size: size of the access in bytes
1768 * @attrs: memory transaction attributes to use for the access
1770 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1777 * address_space_init: initializes an address space
1779 * @as: an uninitialized #AddressSpace
1780 * @root: a #MemoryRegion that routes addresses for the address space
1781 * @name: an address space name. The name is only used for debugging
1784 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
1787 * address_space_destroy: destroy an address space
1789 * Releases all resources associated with an address space. After an address space
1790 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1793 * @as: address space to be destroyed
1795 void address_space_destroy(AddressSpace *as);
1798 * address_space_remove_listeners: unregister all listeners of an address space
1800 * Removes all callbacks previously registered with memory_listener_register()
1803 * @as: an initialized #AddressSpace
1805 void address_space_remove_listeners(AddressSpace *as);
1808 * address_space_rw: read from or write to an 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 read or write
1819 * @is_write: indicates the transfer direction
1821 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1822 MemTxAttrs attrs, uint8_t *buf,
1823 hwaddr len, bool is_write);
1826 * address_space_write: write to address space.
1828 * Return a MemTxResult indicating whether the operation succeeded
1829 * or failed (eg unassigned memory, device rejected the transaction,
1832 * @as: #AddressSpace to be accessed
1833 * @addr: address within that address space
1834 * @attrs: memory transaction attributes
1835 * @buf: buffer with the data transferred
1836 * @len: the number of bytes to write
1838 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1840 const uint8_t *buf, hwaddr len);
1843 * address_space_write_rom: write to address space, including ROM.
1845 * This function writes to the specified address space, but will
1846 * write data to both ROM and RAM. This is used for non-guest
1847 * writes like writes from the gdb debug stub or initial loading
1850 * Note that portions of the write which attempt to write data to
1851 * a device will be silently ignored -- only real RAM and ROM will
1854 * Return a MemTxResult indicating whether the operation succeeded
1855 * or failed (eg unassigned memory, device rejected the transaction,
1858 * @as: #AddressSpace to be accessed
1859 * @addr: address within that address space
1860 * @attrs: memory transaction attributes
1861 * @buf: buffer with the data transferred
1862 * @len: the number of bytes to write
1864 MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
1866 const uint8_t *buf, hwaddr len);
1868 /* address_space_ld*: load from an address space
1869 * address_space_st*: store to an address space
1871 * These functions perform a load or store of the byte, word,
1872 * longword or quad to the specified address within the AddressSpace.
1873 * The _le suffixed functions treat the data as little endian;
1874 * _be indicates big endian; no suffix indicates "same endianness
1877 * The "guest CPU endianness" accessors are deprecated for use outside
1878 * target-* code; devices should be CPU-agnostic and use either the LE
1879 * or the BE accessors.
1881 * @as #AddressSpace to be accessed
1882 * @addr: address within that address space
1883 * @val: data value, for stores
1884 * @attrs: memory transaction attributes
1885 * @result: location to write the success/failure of the transaction;
1886 * if NULL, this information is discarded
1891 #define ARG1_DECL AddressSpace *as
1892 #include "exec/memory_ldst.inc.h"
1896 #define ARG1_DECL AddressSpace *as
1897 #include "exec/memory_ldst_phys.inc.h"
1899 struct MemoryRegionCache {
1904 MemoryRegionSection mrs;
1908 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
1911 /* address_space_ld*_cached: load from a cached #MemoryRegion
1912 * address_space_st*_cached: store into a cached #MemoryRegion
1914 * These functions perform a load or store of the byte, word,
1915 * longword or quad to the specified address. The address is
1916 * a physical address in the AddressSpace, but it must lie within
1917 * a #MemoryRegion that was mapped with address_space_cache_init.
1919 * The _le suffixed functions treat the data as little endian;
1920 * _be indicates big endian; no suffix indicates "same endianness
1923 * The "guest CPU endianness" accessors are deprecated for use outside
1924 * target-* code; devices should be CPU-agnostic and use either the LE
1925 * or the BE accessors.
1927 * @cache: previously initialized #MemoryRegionCache to be accessed
1928 * @addr: address within the address space
1929 * @val: data value, for stores
1930 * @attrs: memory transaction attributes
1931 * @result: location to write the success/failure of the transaction;
1932 * if NULL, this information is discarded
1935 #define SUFFIX _cached_slow
1937 #define ARG1_DECL MemoryRegionCache *cache
1938 #include "exec/memory_ldst.inc.h"
1940 /* Inline fast path for direct RAM access. */
1941 static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache,
1942 hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
1944 assert(addr < cache->len);
1945 if (likely(cache->ptr)) {
1946 return ldub_p(cache->ptr + addr);
1948 return address_space_ldub_cached_slow(cache, addr, attrs, result);
1952 static inline void address_space_stb_cached(MemoryRegionCache *cache,
1953 hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result)
1955 assert(addr < cache->len);
1956 if (likely(cache->ptr)) {
1957 stb_p(cache->ptr + addr, val);
1959 address_space_stb_cached_slow(cache, addr, val, attrs, result);
1963 #define ENDIANNESS _le
1964 #include "exec/memory_ldst_cached.inc.h"
1966 #define ENDIANNESS _be
1967 #include "exec/memory_ldst_cached.inc.h"
1969 #define SUFFIX _cached
1971 #define ARG1_DECL MemoryRegionCache *cache
1972 #include "exec/memory_ldst_phys.inc.h"
1974 /* address_space_cache_init: prepare for repeated access to a physical
1977 * @cache: #MemoryRegionCache to be filled
1978 * @as: #AddressSpace to be accessed
1979 * @addr: address within that address space
1980 * @len: length of buffer
1981 * @is_write: indicates the transfer direction
1983 * Will only work with RAM, and may map a subset of the requested range by
1984 * returning a value that is less than @len. On failure, return a negative
1987 * Because it only works with RAM, this function can be used for
1988 * read-modify-write operations. In this case, is_write should be %true.
1990 * Note that addresses passed to the address_space_*_cached functions
1991 * are relative to @addr.
1993 int64_t address_space_cache_init(MemoryRegionCache *cache,
2000 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
2002 * @cache: The #MemoryRegionCache to operate on.
2003 * @addr: The first physical address that was written, relative to the
2004 * address that was passed to @address_space_cache_init.
2005 * @access_len: The number of bytes that were written starting at @addr.
2007 void address_space_cache_invalidate(MemoryRegionCache *cache,
2012 * address_space_cache_destroy: free a #MemoryRegionCache
2014 * @cache: The #MemoryRegionCache whose memory should be released.
2016 void address_space_cache_destroy(MemoryRegionCache *cache);
2018 /* address_space_get_iotlb_entry: translate an address into an IOTLB
2019 * entry. Should be called from an RCU critical section.
2021 IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
2022 bool is_write, MemTxAttrs attrs);
2024 /* address_space_translate: translate an address range into an address space
2025 * into a MemoryRegion and an address range into that section. Should be
2026 * called from an RCU critical section, to avoid that the last reference
2027 * to the returned region disappears after address_space_translate returns.
2029 * @fv: #FlatView to be accessed
2030 * @addr: address within that address space
2031 * @xlat: pointer to address within the returned memory region section's
2033 * @len: pointer to length
2034 * @is_write: indicates the transfer direction
2035 * @attrs: memory attributes
2037 MemoryRegion *flatview_translate(FlatView *fv,
2038 hwaddr addr, hwaddr *xlat,
2039 hwaddr *len, bool is_write,
2042 static inline MemoryRegion *address_space_translate(AddressSpace *as,
2043 hwaddr addr, hwaddr *xlat,
2044 hwaddr *len, bool is_write,
2047 return flatview_translate(address_space_to_flatview(as),
2048 addr, xlat, len, is_write, attrs);
2051 /* address_space_access_valid: check for validity of accessing an address
2054 * Check whether memory is assigned to the given address space range, and
2055 * access is permitted by any IOMMU regions that are active for the address
2058 * For now, addr and len should be aligned to a page size. This limitation
2059 * will be lifted in the future.
2061 * @as: #AddressSpace to be accessed
2062 * @addr: address within that address space
2063 * @len: length of the area to be checked
2064 * @is_write: indicates the transfer direction
2065 * @attrs: memory attributes
2067 bool address_space_access_valid(AddressSpace *as, hwaddr addr, hwaddr len,
2068 bool is_write, MemTxAttrs attrs);
2070 /* address_space_map: map a physical memory region into a host virtual address
2072 * May map a subset of the requested range, given by and returned in @plen.
2073 * May return %NULL if resources needed to perform the mapping are exhausted.
2074 * Use only for reads OR writes - not for read-modify-write operations.
2075 * Use cpu_register_map_client() to know when retrying the map operation is
2076 * likely to succeed.
2078 * @as: #AddressSpace to be accessed
2079 * @addr: address within that address space
2080 * @plen: pointer to length of buffer; updated on return
2081 * @is_write: indicates the transfer direction
2082 * @attrs: memory attributes
2084 void *address_space_map(AddressSpace *as, hwaddr addr,
2085 hwaddr *plen, bool is_write, MemTxAttrs attrs);
2087 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2089 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2090 * the amount of memory that was actually read or written by the caller.
2092 * @as: #AddressSpace used
2093 * @buffer: host pointer as returned by address_space_map()
2094 * @len: buffer length as returned by address_space_map()
2095 * @access_len: amount of data actually transferred
2096 * @is_write: indicates the transfer direction
2098 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2099 int is_write, hwaddr access_len);
2102 /* Internal functions, part of the implementation of address_space_read. */
2103 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
2104 MemTxAttrs attrs, uint8_t *buf, hwaddr len);
2105 MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
2106 MemTxAttrs attrs, uint8_t *buf,
2107 hwaddr len, hwaddr addr1, hwaddr l,
2109 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
2111 /* Internal functions, part of the implementation of address_space_read_cached
2112 * and address_space_write_cached. */
2113 void address_space_read_cached_slow(MemoryRegionCache *cache,
2114 hwaddr addr, void *buf, hwaddr len);
2115 void address_space_write_cached_slow(MemoryRegionCache *cache,
2116 hwaddr addr, const void *buf, hwaddr len);
2118 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
2121 return memory_region_is_ram(mr) &&
2122 !mr->readonly && !memory_region_is_ram_device(mr);
2124 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
2125 memory_region_is_romd(mr);
2130 * address_space_read: read from an address space.
2132 * Return a MemTxResult indicating whether the operation succeeded
2133 * or failed (eg unassigned memory, device rejected the transaction,
2134 * IOMMU fault). Called within RCU critical section.
2136 * @as: #AddressSpace to be accessed
2137 * @addr: address within that address space
2138 * @attrs: memory transaction attributes
2139 * @buf: buffer with the data transferred
2141 static inline __attribute__((__always_inline__))
2142 MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
2143 MemTxAttrs attrs, uint8_t *buf,
2146 MemTxResult result = MEMTX_OK;
2152 if (__builtin_constant_p(len)) {
2155 fv = address_space_to_flatview(as);
2157 mr = flatview_translate(fv, addr, &addr1, &l, false, attrs);
2158 if (len == l && memory_access_is_direct(mr, false)) {
2159 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
2160 memcpy(buf, ptr, len);
2162 result = flatview_read_continue(fv, addr, attrs, buf, len,
2168 result = address_space_read_full(as, addr, attrs, buf, len);
2174 * address_space_read_cached: read from a cached RAM region
2176 * @cache: Cached region to be addressed
2177 * @addr: address relative to the base of the RAM region
2178 * @buf: buffer with the data transferred
2179 * @len: length of the data transferred
2182 address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
2183 void *buf, hwaddr len)
2185 assert(addr < cache->len && len <= cache->len - addr);
2186 if (likely(cache->ptr)) {
2187 memcpy(buf, cache->ptr + addr, len);
2189 address_space_read_cached_slow(cache, addr, buf, len);
2194 * address_space_write_cached: write to a cached RAM region
2196 * @cache: Cached region to be addressed
2197 * @addr: address relative to the base of the RAM region
2198 * @buf: buffer with the data transferred
2199 * @len: length of the data transferred
2202 address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
2203 void *buf, hwaddr len)
2205 assert(addr < cache->len && len <= cache->len - addr);
2206 if (likely(cache->ptr)) {
2207 memcpy(cache->ptr + addr, buf, len);
2209 address_space_write_cached_slow(cache, addr, buf, len);