2 * Physical memory management API
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
17 #ifndef CONFIG_USER_ONLY
19 #include "exec/cpu-common.h"
20 #include "exec/hwaddr.h"
21 #include "exec/memattrs.h"
22 #include "exec/ramlist.h"
23 #include "qemu/queue.h"
24 #include "qemu/int128.h"
25 #include "qemu/notify.h"
26 #include "qom/object.h"
28 #include "hw/qdev-core.h"
30 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
32 #define MAX_PHYS_ADDR_SPACE_BITS 62
33 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
35 #define TYPE_MEMORY_REGION "qemu:memory-region"
36 #define MEMORY_REGION(obj) \
37 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
39 #define TYPE_IOMMU_MEMORY_REGION "qemu:iommu-memory-region"
40 #define IOMMU_MEMORY_REGION(obj) \
41 OBJECT_CHECK(IOMMUMemoryRegion, (obj), TYPE_IOMMU_MEMORY_REGION)
42 #define IOMMU_MEMORY_REGION_CLASS(klass) \
43 OBJECT_CLASS_CHECK(IOMMUMemoryRegionClass, (klass), \
44 TYPE_IOMMU_MEMORY_REGION)
45 #define IOMMU_MEMORY_REGION_GET_CLASS(obj) \
46 OBJECT_GET_CLASS(IOMMUMemoryRegionClass, (obj), \
47 TYPE_IOMMU_MEMORY_REGION)
49 typedef struct MemoryRegionOps MemoryRegionOps
;
50 typedef struct MemoryRegionMmio MemoryRegionMmio
;
52 struct MemoryRegionMmio
{
53 CPUReadMemoryFunc
*read
[3];
54 CPUWriteMemoryFunc
*write
[3];
57 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
59 /* See address_space_translate: bit 0 is read, bit 1 is write. */
67 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
69 struct IOMMUTLBEntry
{
70 AddressSpace
*target_as
;
72 hwaddr translated_addr
;
73 hwaddr addr_mask
; /* 0xfff = 4k translation */
74 IOMMUAccessFlags perm
;
78 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
79 * register with one or multiple IOMMU Notifier capability bit(s).
82 IOMMU_NOTIFIER_NONE
= 0,
83 /* Notify cache invalidations */
84 IOMMU_NOTIFIER_UNMAP
= 0x1,
85 /* Notify entry changes (newly created entries) */
86 IOMMU_NOTIFIER_MAP
= 0x2,
89 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
92 typedef void (*IOMMUNotify
)(struct IOMMUNotifier
*notifier
,
95 struct IOMMUNotifier
{
97 IOMMUNotifierFlag notifier_flags
;
98 /* Notify for address space range start <= addr <= end */
102 QLIST_ENTRY(IOMMUNotifier
) node
;
104 typedef struct IOMMUNotifier IOMMUNotifier
;
106 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
107 IOMMUNotifierFlag flags
,
108 hwaddr start
, hwaddr end
,
112 n
->notifier_flags
= flags
;
115 n
->iommu_idx
= iommu_idx
;
119 * Memory region callbacks
121 struct MemoryRegionOps
{
122 /* Read from the memory region. @addr is relative to @mr; @size is
124 uint64_t (*read
)(void *opaque
,
127 /* Write to the memory region. @addr is relative to @mr; @size is
129 void (*write
)(void *opaque
,
134 MemTxResult (*read_with_attrs
)(void *opaque
,
139 MemTxResult (*write_with_attrs
)(void *opaque
,
144 /* Instruction execution pre-callback:
145 * @addr is the address of the access relative to the @mr.
146 * @size is the size of the area returned by the callback.
147 * @offset is the location of the pointer inside @mr.
149 * Returns a pointer to a location which contains guest code.
151 void *(*request_ptr
)(void *opaque
, hwaddr addr
, unsigned *size
,
154 enum device_endian endianness
;
155 /* Guest-visible constraints: */
157 /* If nonzero, specify bounds on access sizes beyond which a machine
160 unsigned min_access_size
;
161 unsigned max_access_size
;
162 /* If true, unaligned accesses are supported. Otherwise unaligned
163 * accesses throw machine checks.
167 * If present, and returns #false, the transaction is not accepted
168 * by the device (and results in machine dependent behaviour such
169 * as a machine check exception).
171 bool (*accepts
)(void *opaque
, hwaddr addr
,
172 unsigned size
, bool is_write
,
175 /* Internal implementation constraints: */
177 /* If nonzero, specifies the minimum size implemented. Smaller sizes
178 * will be rounded upwards and a partial result will be returned.
180 unsigned min_access_size
;
181 /* If nonzero, specifies the maximum size implemented. Larger sizes
182 * will be done as a series of accesses with smaller sizes.
184 unsigned max_access_size
;
185 /* If true, unaligned accesses are supported. Otherwise all accesses
186 * are converted to (possibly multiple) naturally aligned accesses.
191 /* If .read and .write are not present, old_mmio may be used for
192 * backwards compatibility with old mmio registration
194 const MemoryRegionMmio old_mmio
;
197 enum IOMMUMemoryRegionAttr
{
198 IOMMU_ATTR_SPAPR_TCE_FD
202 * IOMMUMemoryRegionClass:
204 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
205 * and provide an implementation of at least the @translate method here
206 * to handle requests to the memory region. Other methods are optional.
208 * The IOMMU implementation must use the IOMMU notifier infrastructure
209 * to report whenever mappings are changed, by calling
210 * memory_region_notify_iommu() (or, if necessary, by calling
211 * memory_region_notify_one() for each registered notifier).
213 * Conceptually an IOMMU provides a mapping from input address
214 * to an output TLB entry. If the IOMMU is aware of memory transaction
215 * attributes and the output TLB entry depends on the transaction
216 * attributes, we represent this using IOMMU indexes. Each index
217 * selects a particular translation table that the IOMMU has:
218 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
219 * @translate takes an input address and an IOMMU index
220 * and the mapping returned can only depend on the input address and the
223 * Most IOMMUs don't care about the transaction attributes and support
224 * only a single IOMMU index. A more complex IOMMU might have one index
225 * for secure transactions and one for non-secure transactions.
227 typedef struct IOMMUMemoryRegionClass
{
229 struct DeviceClass parent_class
;
232 * Return a TLB entry that contains a given address.
234 * The IOMMUAccessFlags indicated via @flag are optional and may
235 * be specified as IOMMU_NONE to indicate that the caller needs
236 * the full translation information for both reads and writes. If
237 * the access flags are specified then the IOMMU implementation
238 * may use this as an optimization, to stop doing a page table
239 * walk as soon as it knows that the requested permissions are not
240 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
241 * full page table walk and report the permissions in the returned
242 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
243 * return different mappings for reads and writes.)
245 * The returned information remains valid while the caller is
246 * holding the big QEMU lock or is inside an RCU critical section;
247 * if the caller wishes to cache the mapping beyond that it must
248 * register an IOMMU notifier so it can invalidate its cached
249 * information when the IOMMU mapping changes.
251 * @iommu: the IOMMUMemoryRegion
252 * @hwaddr: address to be translated within the memory region
253 * @flag: requested access permissions
254 * @iommu_idx: IOMMU index for the translation
256 IOMMUTLBEntry (*translate
)(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
257 IOMMUAccessFlags flag
, int iommu_idx
);
258 /* Returns minimum supported page size in bytes.
259 * If this method is not provided then the minimum is assumed to
260 * be TARGET_PAGE_SIZE.
262 * @iommu: the IOMMUMemoryRegion
264 uint64_t (*get_min_page_size
)(IOMMUMemoryRegion
*iommu
);
265 /* Called when IOMMU Notifier flag changes (ie when the set of
266 * events which IOMMU users are requesting notification for changes).
267 * Optional method -- need not be provided if the IOMMU does not
268 * need to know exactly which events must be notified.
270 * @iommu: the IOMMUMemoryRegion
271 * @old_flags: events which previously needed to be notified
272 * @new_flags: events which now need to be notified
274 void (*notify_flag_changed
)(IOMMUMemoryRegion
*iommu
,
275 IOMMUNotifierFlag old_flags
,
276 IOMMUNotifierFlag new_flags
);
277 /* Called to handle memory_region_iommu_replay().
279 * The default implementation of memory_region_iommu_replay() is to
280 * call the IOMMU translate method for every page in the address space
281 * with flag == IOMMU_NONE and then call the notifier if translate
282 * returns a valid mapping. If this method is implemented then it
283 * overrides the default behaviour, and must provide the full semantics
284 * of memory_region_iommu_replay(), by calling @notifier for every
285 * translation present in the IOMMU.
287 * Optional method -- an IOMMU only needs to provide this method
288 * if the default is inefficient or produces undesirable side effects.
290 * Note: this is not related to record-and-replay functionality.
292 void (*replay
)(IOMMUMemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
294 /* Get IOMMU misc attributes. This is an optional method that
295 * can be used to allow users of the IOMMU to get implementation-specific
296 * information. The IOMMU implements this method to handle calls
297 * by IOMMU users to memory_region_iommu_get_attr() by filling in
298 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
299 * the IOMMU supports. If the method is unimplemented then
300 * memory_region_iommu_get_attr() will always return -EINVAL.
302 * @iommu: the IOMMUMemoryRegion
303 * @attr: attribute being queried
304 * @data: memory to fill in with the attribute data
306 * Returns 0 on success, or a negative errno; in particular
307 * returns -EINVAL for unrecognized or unimplemented attribute types.
309 int (*get_attr
)(IOMMUMemoryRegion
*iommu
, enum IOMMUMemoryRegionAttr attr
,
312 /* Return the IOMMU index to use for a given set of transaction attributes.
314 * Optional method: if an IOMMU only supports a single IOMMU index then
315 * the default implementation of memory_region_iommu_attrs_to_index()
318 * The indexes supported by an IOMMU must be contiguous, starting at 0.
320 * @iommu: the IOMMUMemoryRegion
321 * @attrs: memory transaction attributes
323 int (*attrs_to_index
)(IOMMUMemoryRegion
*iommu
, MemTxAttrs attrs
);
325 /* Return the number of IOMMU indexes this IOMMU supports.
327 * Optional method: if this method is not provided, then
328 * memory_region_iommu_num_indexes() will return 1, indicating that
329 * only a single IOMMU index is supported.
331 * @iommu: the IOMMUMemoryRegion
333 int (*num_indexes
)(IOMMUMemoryRegion
*iommu
);
334 } IOMMUMemoryRegionClass
;
336 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
337 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
339 struct MemoryRegion
{
342 /* All fields are private - violators will be prosecuted */
344 /* The following fields should fit in a cache line */
348 bool readonly
; /* For RAM regions */
350 bool flush_coalesced_mmio
;
352 uint8_t dirty_log_mask
;
357 const MemoryRegionOps
*ops
;
359 MemoryRegion
*container
;
362 void (*destructor
)(MemoryRegion
*mr
);
367 bool warning_printed
; /* For reservations */
368 uint8_t vga_logging_count
;
372 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
373 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
374 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
376 unsigned ioeventfd_nb
;
377 MemoryRegionIoeventfd
*ioeventfds
;
380 struct IOMMUMemoryRegion
{
381 MemoryRegion parent_obj
;
383 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
384 IOMMUNotifierFlag iommu_notify_flags
;
387 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
388 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
391 * MemoryListener: callbacks structure for updates to the physical memory map
393 * Allows a component to adjust to changes in the guest-visible memory map.
394 * Use with memory_listener_register() and memory_listener_unregister().
396 struct MemoryListener
{
397 void (*begin
)(MemoryListener
*listener
);
398 void (*commit
)(MemoryListener
*listener
);
399 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
400 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
401 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
402 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
404 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
406 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
407 void (*log_global_start
)(MemoryListener
*listener
);
408 void (*log_global_stop
)(MemoryListener
*listener
);
409 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
410 bool match_data
, uint64_t data
, EventNotifier
*e
);
411 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
412 bool match_data
, uint64_t data
, EventNotifier
*e
);
413 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
414 hwaddr addr
, hwaddr len
);
415 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
416 hwaddr addr
, hwaddr len
);
417 /* Lower = earlier (during add), later (during del) */
419 AddressSpace
*address_space
;
420 QTAILQ_ENTRY(MemoryListener
) link
;
421 QTAILQ_ENTRY(MemoryListener
) link_as
;
425 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
427 struct AddressSpace
{
428 /* All fields are private. */
433 /* Accessed via RCU. */
434 struct FlatView
*current_map
;
437 struct MemoryRegionIoeventfd
*ioeventfds
;
438 QTAILQ_HEAD(memory_listeners_as
, MemoryListener
) listeners
;
439 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
442 typedef struct AddressSpaceDispatch AddressSpaceDispatch
;
443 typedef struct FlatRange FlatRange
;
445 /* Flattened global view of current active memory hierarchy. Kept in sorted
453 unsigned nr_allocated
;
454 struct AddressSpaceDispatch
*dispatch
;
458 static inline FlatView
*address_space_to_flatview(AddressSpace
*as
)
460 return atomic_rcu_read(&as
->current_map
);
465 * MemoryRegionSection: describes a fragment of a #MemoryRegion
467 * @mr: the region, or %NULL if empty
468 * @fv: the flat view of the address space the region is mapped in
469 * @offset_within_region: the beginning of the section, relative to @mr's start
470 * @size: the size of the section; will not exceed @mr's boundaries
471 * @offset_within_address_space: the address of the first byte of the section
472 * relative to the region's address space
473 * @readonly: writes to this section are ignored
475 struct MemoryRegionSection
{
478 hwaddr offset_within_region
;
480 hwaddr offset_within_address_space
;
485 * memory_region_init: Initialize a memory region
487 * The region typically acts as a container for other memory regions. Use
488 * memory_region_add_subregion() to add subregions.
490 * @mr: the #MemoryRegion to be initialized
491 * @owner: the object that tracks the region's reference count
492 * @name: used for debugging; not visible to the user or ABI
493 * @size: size of the region; any subregions beyond this size will be clipped
495 void memory_region_init(MemoryRegion
*mr
,
496 struct Object
*owner
,
501 * memory_region_ref: Add 1 to a memory region's reference count
503 * Whenever memory regions are accessed outside the BQL, they need to be
504 * preserved against hot-unplug. MemoryRegions actually do not have their
505 * own reference count; they piggyback on a QOM object, their "owner".
506 * This function adds a reference to the owner.
508 * All MemoryRegions must have an owner if they can disappear, even if the
509 * device they belong to operates exclusively under the BQL. This is because
510 * the region could be returned at any time by memory_region_find, and this
511 * is usually under guest control.
513 * @mr: the #MemoryRegion
515 void memory_region_ref(MemoryRegion
*mr
);
518 * memory_region_unref: Remove 1 to a memory region's reference count
520 * Whenever memory regions are accessed outside the BQL, they need to be
521 * preserved against hot-unplug. MemoryRegions actually do not have their
522 * own reference count; they piggyback on a QOM object, their "owner".
523 * This function removes a reference to the owner and possibly destroys it.
525 * @mr: the #MemoryRegion
527 void memory_region_unref(MemoryRegion
*mr
);
530 * memory_region_init_io: Initialize an I/O memory region.
532 * Accesses into the region will cause the callbacks in @ops to be called.
533 * if @size is nonzero, subregions will be clipped to @size.
535 * @mr: the #MemoryRegion to be initialized.
536 * @owner: the object that tracks the region's reference count
537 * @ops: a structure containing read and write callbacks to be used when
538 * I/O is performed on the region.
539 * @opaque: passed to the read and write callbacks of the @ops structure.
540 * @name: used for debugging; not visible to the user or ABI
541 * @size: size of the region.
543 void memory_region_init_io(MemoryRegion
*mr
,
544 struct Object
*owner
,
545 const MemoryRegionOps
*ops
,
551 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
552 * into the region will modify memory
555 * @mr: the #MemoryRegion to be initialized.
556 * @owner: the object that tracks the region's reference count
557 * @name: Region name, becomes part of RAMBlock name used in migration stream
558 * must be unique within any device
559 * @size: size of the region.
560 * @errp: pointer to Error*, to store an error if it happens.
562 * Note that this function does not do anything to cause the data in the
563 * RAM memory region to be migrated; that is the responsibility of the caller.
565 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
566 struct Object
*owner
,
572 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
573 * Accesses into the region will
574 * modify memory directly.
576 * @mr: the #MemoryRegion to be initialized.
577 * @owner: the object that tracks the region's reference count
578 * @name: Region name, becomes part of RAMBlock name used in migration stream
579 * must be unique within any device
580 * @size: size of the region.
581 * @share: allow remapping RAM to different addresses
582 * @errp: pointer to Error*, to store an error if it happens.
584 * Note that this function is similar to memory_region_init_ram_nomigrate.
585 * The only difference is part of the RAM region can be remapped.
587 void memory_region_init_ram_shared_nomigrate(MemoryRegion
*mr
,
588 struct Object
*owner
,
595 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
596 * RAM. Accesses into the region will
597 * modify memory directly. Only an initial
598 * portion of this RAM is actually used.
599 * The used size can change across reboots.
601 * @mr: the #MemoryRegion to be initialized.
602 * @owner: the object that tracks the region's reference count
603 * @name: Region name, becomes part of RAMBlock name used in migration stream
604 * must be unique within any device
605 * @size: used size of the region.
606 * @max_size: max size of the region.
607 * @resized: callback to notify owner about used size change.
608 * @errp: pointer to Error*, to store an error if it happens.
610 * Note that this function does not do anything to cause the data in the
611 * RAM memory region to be migrated; that is the responsibility of the caller.
613 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
614 struct Object
*owner
,
618 void (*resized
)(const char*,
624 * memory_region_init_ram_from_file: Initialize RAM memory region with a
627 * @mr: the #MemoryRegion to be initialized.
628 * @owner: the object that tracks the region's reference count
629 * @name: Region name, becomes part of RAMBlock name used in migration stream
630 * must be unique within any device
631 * @size: size of the region.
632 * @align: alignment of the region base address; if 0, the default alignment
633 * (getpagesize()) will be used.
634 * @share: %true if memory must be mmaped with the MAP_SHARED flag
635 * @path: the path in which to allocate the RAM.
636 * @errp: pointer to Error*, to store an error if it happens.
638 * Note that this function does not do anything to cause the data in the
639 * RAM memory region to be migrated; that is the responsibility of the caller.
641 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
642 struct Object
*owner
,
651 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
654 * @mr: the #MemoryRegion to be initialized.
655 * @owner: the object that tracks the region's reference count
656 * @name: the name of the region.
657 * @size: size of the region.
658 * @share: %true if memory must be mmaped with the MAP_SHARED flag
659 * @fd: the fd to mmap.
660 * @errp: pointer to Error*, to store an error if it happens.
662 * Note that this function does not do anything to cause the data in the
663 * RAM memory region to be migrated; that is the responsibility of the caller.
665 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
666 struct Object
*owner
,
675 * memory_region_init_ram_ptr: Initialize RAM memory region from a
676 * user-provided pointer. Accesses into the
677 * region will modify memory directly.
679 * @mr: the #MemoryRegion to be initialized.
680 * @owner: the object that tracks the region's reference count
681 * @name: Region name, becomes part of RAMBlock name used in migration stream
682 * must be unique within any device
683 * @size: size of the region.
684 * @ptr: memory to be mapped; must contain at least @size bytes.
686 * Note that this function does not do anything to cause the data in the
687 * RAM memory region to be migrated; that is the responsibility of the caller.
689 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
690 struct Object
*owner
,
696 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
697 * a user-provided pointer.
699 * A RAM device represents a mapping to a physical device, such as to a PCI
700 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
701 * into the VM address space and access to the region will modify memory
702 * directly. However, the memory region should not be included in a memory
703 * dump (device may not be enabled/mapped at the time of the dump), and
704 * operations incompatible with manipulating MMIO should be avoided. Replaces
707 * @mr: the #MemoryRegion to be initialized.
708 * @owner: the object that tracks the region's reference count
709 * @name: the name of the region.
710 * @size: size of the region.
711 * @ptr: memory to be mapped; must contain at least @size bytes.
713 * Note that this function does not do anything to cause the data in the
714 * RAM memory region to be migrated; that is the responsibility of the caller.
715 * (For RAM device memory regions, migrating the contents rarely makes sense.)
717 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
718 struct Object
*owner
,
724 * memory_region_init_alias: Initialize a memory region that aliases all or a
725 * part of another memory region.
727 * @mr: the #MemoryRegion to be initialized.
728 * @owner: the object that tracks the region's reference count
729 * @name: used for debugging; not visible to the user or ABI
730 * @orig: the region to be referenced; @mr will be equivalent to
731 * @orig between @offset and @offset + @size - 1.
732 * @offset: start of the section in @orig to be referenced.
733 * @size: size of the region.
735 void memory_region_init_alias(MemoryRegion
*mr
,
736 struct Object
*owner
,
743 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
745 * This has the same effect as calling memory_region_init_ram_nomigrate()
746 * and then marking the resulting region read-only with
747 * memory_region_set_readonly().
749 * Note that this function does not do anything to cause the data in the
750 * RAM side of the memory region to be migrated; that is the responsibility
753 * @mr: the #MemoryRegion to be initialized.
754 * @owner: the object that tracks the region's reference count
755 * @name: Region name, becomes part of RAMBlock name used in migration stream
756 * must be unique within any device
757 * @size: size of the region.
758 * @errp: pointer to Error*, to store an error if it happens.
760 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
761 struct Object
*owner
,
767 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
768 * Writes are handled via callbacks.
770 * Note that this function does not do anything to cause the data in the
771 * RAM side of the memory region to be migrated; that is the responsibility
774 * @mr: the #MemoryRegion to be initialized.
775 * @owner: the object that tracks the region's reference count
776 * @ops: callbacks for write access handling (must not be NULL).
777 * @opaque: passed to the read and write callbacks of the @ops structure.
778 * @name: Region name, becomes part of RAMBlock name used in migration stream
779 * must be unique within any device
780 * @size: size of the region.
781 * @errp: pointer to Error*, to store an error if it happens.
783 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
784 struct Object
*owner
,
785 const MemoryRegionOps
*ops
,
792 * memory_region_init_iommu: Initialize a memory region of a custom type
793 * that translates addresses
795 * An IOMMU region translates addresses and forwards accesses to a target
798 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
799 * @_iommu_mr should be a pointer to enough memory for an instance of
800 * that subclass, @instance_size is the size of that subclass, and
801 * @mrtypename is its name. This function will initialize @_iommu_mr as an
802 * instance of the subclass, and its methods will then be called to handle
803 * accesses to the memory region. See the documentation of
804 * #IOMMUMemoryRegionClass for further details.
806 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
807 * @instance_size: the IOMMUMemoryRegion subclass instance size
808 * @mrtypename: the type name of the #IOMMUMemoryRegion
809 * @owner: the object that tracks the region's reference count
810 * @name: used for debugging; not visible to the user or ABI
811 * @size: size of the region.
813 void memory_region_init_iommu(void *_iommu_mr
,
814 size_t instance_size
,
815 const char *mrtypename
,
821 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
822 * region will modify memory directly.
824 * @mr: the #MemoryRegion to be initialized
825 * @owner: the object that tracks the region's reference count (must be
826 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
827 * @name: name of the memory region
828 * @size: size of the region in bytes
829 * @errp: pointer to Error*, to store an error if it happens.
831 * This function allocates RAM for a board model or device, and
832 * arranges for it to be migrated (by calling vmstate_register_ram()
833 * if @owner is a DeviceState, or vmstate_register_ram_global() if
836 * TODO: Currently we restrict @owner to being either NULL (for
837 * global RAM regions with no owner) or devices, so that we can
838 * give the RAM block a unique name for migration purposes.
839 * We should lift this restriction and allow arbitrary Objects.
840 * If you pass a non-NULL non-device @owner then we will assert.
842 void memory_region_init_ram(MemoryRegion
*mr
,
843 struct Object
*owner
,
849 * memory_region_init_rom: Initialize a ROM memory region.
851 * This has the same effect as calling memory_region_init_ram()
852 * and then marking the resulting region read-only with
853 * memory_region_set_readonly(). This includes arranging for the
854 * contents to be migrated.
856 * TODO: Currently we restrict @owner to being either NULL (for
857 * global RAM regions with no owner) or devices, so that we can
858 * give the RAM block a unique name for migration purposes.
859 * We should lift this restriction and allow arbitrary Objects.
860 * If you pass a non-NULL non-device @owner then we will assert.
862 * @mr: the #MemoryRegion to be initialized.
863 * @owner: the object that tracks the region's reference count
864 * @name: Region name, becomes part of RAMBlock name used in migration stream
865 * must be unique within any device
866 * @size: size of the region.
867 * @errp: pointer to Error*, to store an error if it happens.
869 void memory_region_init_rom(MemoryRegion
*mr
,
870 struct Object
*owner
,
876 * memory_region_init_rom_device: Initialize a ROM memory region.
877 * Writes are handled via callbacks.
879 * This function initializes a memory region backed by RAM for reads
880 * and callbacks for writes, and arranges for the RAM backing to
881 * be migrated (by calling vmstate_register_ram()
882 * if @owner is a DeviceState, or vmstate_register_ram_global() if
885 * TODO: Currently we restrict @owner to being either NULL (for
886 * global RAM regions with no owner) or devices, so that we can
887 * give the RAM block a unique name for migration purposes.
888 * We should lift this restriction and allow arbitrary Objects.
889 * If you pass a non-NULL non-device @owner then we will assert.
891 * @mr: the #MemoryRegion to be initialized.
892 * @owner: the object that tracks the region's reference count
893 * @ops: callbacks for write access handling (must not be NULL).
894 * @name: Region name, becomes part of RAMBlock name used in migration stream
895 * must be unique within any device
896 * @size: size of the region.
897 * @errp: pointer to Error*, to store an error if it happens.
899 void memory_region_init_rom_device(MemoryRegion
*mr
,
900 struct Object
*owner
,
901 const MemoryRegionOps
*ops
,
909 * memory_region_owner: get a memory region's owner.
911 * @mr: the memory region being queried.
913 struct Object
*memory_region_owner(MemoryRegion
*mr
);
916 * memory_region_size: get a memory region's size.
918 * @mr: the memory region being queried.
920 uint64_t memory_region_size(MemoryRegion
*mr
);
923 * memory_region_is_ram: check whether a memory region is random access
925 * Returns %true is a memory region is random access.
927 * @mr: the memory region being queried
929 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
935 * memory_region_is_ram_device: check whether a memory region is a ram device
937 * Returns %true is a memory region is a device backed ram region
939 * @mr: the memory region being queried
941 bool memory_region_is_ram_device(MemoryRegion
*mr
);
944 * memory_region_is_romd: check whether a memory region is in ROMD mode
946 * Returns %true if a memory region is a ROM device and currently set to allow
949 * @mr: the memory region being queried
951 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
953 return mr
->rom_device
&& mr
->romd_mode
;
957 * memory_region_get_iommu: check whether a memory region is an iommu
959 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
962 * @mr: the memory region being queried
964 static inline IOMMUMemoryRegion
*memory_region_get_iommu(MemoryRegion
*mr
)
967 return memory_region_get_iommu(mr
->alias
);
970 return (IOMMUMemoryRegion
*) mr
;
976 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
977 * if an iommu or NULL if not
979 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
980 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
982 * @mr: the memory region being queried
984 static inline IOMMUMemoryRegionClass
*memory_region_get_iommu_class_nocheck(
985 IOMMUMemoryRegion
*iommu_mr
)
987 return (IOMMUMemoryRegionClass
*) (((Object
*)iommu_mr
)->class);
990 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
993 * memory_region_iommu_get_min_page_size: get minimum supported page size
996 * Returns minimum supported page size for an iommu.
998 * @iommu_mr: the memory region being queried
1000 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
);
1003 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1005 * The notification type will be decided by entry.perm bits:
1007 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
1008 * - For MAP (newly added entry) notifies: set entry.perm to the
1009 * permission of the page (which is definitely !IOMMU_NONE).
1011 * Note: for any IOMMU implementation, an in-place mapping change
1012 * should be notified with an UNMAP followed by a MAP.
1014 * @iommu_mr: the memory region that was changed
1015 * @iommu_idx: the IOMMU index for the translation table which has changed
1016 * @entry: the new entry in the IOMMU translation table. The entry
1017 * replaces all old entries for the same virtual I/O address range.
1018 * Deleted entries have .@perm == 0.
1020 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
1022 IOMMUTLBEntry entry
);
1025 * memory_region_notify_one: notify a change in an IOMMU translation
1026 * entry to a single notifier
1028 * This works just like memory_region_notify_iommu(), but it only
1029 * notifies a specific notifier, not all of them.
1031 * @notifier: the notifier to be notified
1032 * @entry: the new entry in the IOMMU translation table. The entry
1033 * replaces all old entries for the same virtual I/O address range.
1034 * Deleted entries have .@perm == 0.
1036 void memory_region_notify_one(IOMMUNotifier
*notifier
,
1037 IOMMUTLBEntry
*entry
);
1040 * memory_region_register_iommu_notifier: register a notifier for changes to
1041 * IOMMU translation entries.
1043 * @mr: the memory region to observe
1044 * @n: the IOMMUNotifier to be added; the notify callback receives a
1045 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1046 * ceases to be valid on exit from the notifier.
1048 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
1052 * memory_region_iommu_replay: replay existing IOMMU translations to
1053 * a notifier with the minimum page granularity returned by
1054 * mr->iommu_ops->get_page_size().
1056 * Note: this is not related to record-and-replay functionality.
1058 * @iommu_mr: the memory region to observe
1059 * @n: the notifier to which to replay iommu mappings
1061 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
);
1064 * memory_region_iommu_replay_all: replay existing IOMMU translations
1065 * to all the notifiers registered.
1067 * Note: this is not related to record-and-replay functionality.
1069 * @iommu_mr: the memory region to observe
1071 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
);
1074 * memory_region_unregister_iommu_notifier: unregister a notifier for
1075 * changes to IOMMU translation entries.
1077 * @mr: the memory region which was observed and for which notity_stopped()
1078 * needs to be called
1079 * @n: the notifier to be removed.
1081 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
1085 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1086 * defined on the IOMMU.
1088 * Returns 0 on success, or a negative errno otherwise. In particular,
1089 * -EINVAL indicates that the IOMMU does not support the requested
1092 * @iommu_mr: the memory region
1093 * @attr: the requested attribute
1094 * @data: a pointer to the requested attribute data
1096 int memory_region_iommu_get_attr(IOMMUMemoryRegion
*iommu_mr
,
1097 enum IOMMUMemoryRegionAttr attr
,
1101 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1102 * use for translations with the given memory transaction attributes.
1104 * @iommu_mr: the memory region
1105 * @attrs: the memory transaction attributes
1107 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion
*iommu_mr
,
1111 * memory_region_iommu_num_indexes: return the total number of IOMMU
1112 * indexes that this IOMMU supports.
1114 * @iommu_mr: the memory region
1116 int memory_region_iommu_num_indexes(IOMMUMemoryRegion
*iommu_mr
);
1119 * memory_region_name: get a memory region's name
1121 * Returns the string that was used to initialize the memory region.
1123 * @mr: the memory region being queried
1125 const char *memory_region_name(const MemoryRegion
*mr
);
1128 * memory_region_is_logging: return whether a memory region is logging writes
1130 * Returns %true if the memory region is logging writes for the given client
1132 * @mr: the memory region being queried
1133 * @client: the client being queried
1135 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
1138 * memory_region_get_dirty_log_mask: return the clients for which a
1139 * memory region is logging writes.
1141 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1142 * are the bit indices.
1144 * @mr: the memory region being queried
1146 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
1149 * memory_region_is_rom: check whether a memory region is ROM
1151 * Returns %true is a memory region is read-only memory.
1153 * @mr: the memory region being queried
1155 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
1157 return mr
->ram
&& mr
->readonly
;
1162 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1164 * Returns a file descriptor backing a file-based RAM memory region,
1165 * or -1 if the region is not a file-based RAM memory region.
1167 * @mr: the RAM or alias memory region being queried.
1169 int memory_region_get_fd(MemoryRegion
*mr
);
1172 * memory_region_from_host: Convert a pointer into a RAM memory region
1173 * and an offset within it.
1175 * Given a host pointer inside a RAM memory region (created with
1176 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1177 * the MemoryRegion and the offset within it.
1179 * Use with care; by the time this function returns, the returned pointer is
1180 * not protected by RCU anymore. If the caller is not within an RCU critical
1181 * section and does not hold the iothread lock, it must have other means of
1182 * protecting the pointer, such as a reference to the region that includes
1183 * the incoming ram_addr_t.
1185 * @ptr: the host pointer to be converted
1186 * @offset: the offset within memory region
1188 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
1191 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1193 * Returns a host pointer to a RAM memory region (created with
1194 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1196 * Use with care; by the time this function returns, the returned pointer is
1197 * not protected by RCU anymore. If the caller is not within an RCU critical
1198 * section and does not hold the iothread lock, it must have other means of
1199 * protecting the pointer, such as a reference to the region that includes
1200 * the incoming ram_addr_t.
1202 * @mr: the memory region being queried.
1204 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
1206 /* memory_region_ram_resize: Resize a RAM region.
1208 * Only legal before guest might have detected the memory size: e.g. on
1209 * incoming migration, or right after reset.
1211 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1212 * @newsize: the new size the region
1213 * @errp: pointer to Error*, to store an error if it happens.
1215 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
1219 * memory_region_set_log: Turn dirty logging on or off for a region.
1221 * Turns dirty logging on or off for a specified client (display, migration).
1222 * Only meaningful for RAM regions.
1224 * @mr: the memory region being updated.
1225 * @log: whether dirty logging is to be enabled or disabled.
1226 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1228 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
1231 * memory_region_get_dirty: Check whether a range of bytes is dirty
1232 * for a specified client.
1234 * Checks whether a range of bytes has been written to since the last
1235 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1238 * @mr: the memory region being queried.
1239 * @addr: the address (relative to the start of the region) being queried.
1240 * @size: the size of the range being queried.
1241 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1242 * %DIRTY_MEMORY_VGA.
1244 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1245 hwaddr size
, unsigned client
);
1248 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1250 * Marks a range of bytes as dirty, after it has been dirtied outside
1253 * @mr: the memory region being dirtied.
1254 * @addr: the address (relative to the start of the region) being dirtied.
1255 * @size: size of the range being dirtied.
1257 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1261 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1262 * bitmap and clear it.
1264 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1265 * returns the snapshot. The snapshot can then be used to query dirty
1266 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1267 * querying the same page multiple times, which is especially useful for
1268 * display updates where the scanlines often are not page aligned.
1270 * The dirty bitmap region which gets copyed into the snapshot (and
1271 * cleared afterwards) can be larger than requested. The boundaries
1272 * are rounded up/down so complete bitmap longs (covering 64 pages on
1273 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1274 * isn't a problem for display updates as the extra pages are outside
1275 * the visible area, and in case the visible area changes a full
1276 * display redraw is due anyway. Should other use cases for this
1277 * function emerge we might have to revisit this implementation
1280 * Use g_free to release DirtyBitmapSnapshot.
1282 * @mr: the memory region being queried.
1283 * @addr: the address (relative to the start of the region) being queried.
1284 * @size: the size of the range being queried.
1285 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1287 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1293 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1294 * in the specified dirty bitmap snapshot.
1296 * @mr: the memory region being queried.
1297 * @snap: the dirty bitmap snapshot
1298 * @addr: the address (relative to the start of the region) being queried.
1299 * @size: the size of the range being queried.
1301 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
1302 DirtyBitmapSnapshot
*snap
,
1303 hwaddr addr
, hwaddr size
);
1306 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1309 * Marks a range of pages as no longer dirty.
1311 * @mr: the region being updated.
1312 * @addr: the start of the subrange being cleaned.
1313 * @size: the size of the subrange being cleaned.
1314 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1315 * %DIRTY_MEMORY_VGA.
1317 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1318 hwaddr size
, unsigned client
);
1321 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1323 * Allows a memory region to be marked as read-only (turning it into a ROM).
1324 * only useful on RAM regions.
1326 * @mr: the region being updated.
1327 * @readonly: whether rhe region is to be ROM or RAM.
1329 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
1332 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1334 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1335 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1336 * device is mapped to guest memory and satisfies read access directly.
1337 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1338 * Writes are always handled by the #MemoryRegion.write function.
1340 * @mr: the memory region to be updated
1341 * @romd_mode: %true to put the region into ROMD mode
1343 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
1346 * memory_region_set_coalescing: Enable memory coalescing for the region.
1348 * Enabled writes to a region to be queued for later processing. MMIO ->write
1349 * callbacks may be delayed until a non-coalesced MMIO is issued.
1350 * Only useful for IO regions. Roughly similar to write-combining hardware.
1352 * @mr: the memory region to be write coalesced
1354 void memory_region_set_coalescing(MemoryRegion
*mr
);
1357 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1360 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1361 * Multiple calls can be issued coalesced disjoint ranges.
1363 * @mr: the memory region to be updated.
1364 * @offset: the start of the range within the region to be coalesced.
1365 * @size: the size of the subrange to be coalesced.
1367 void memory_region_add_coalescing(MemoryRegion
*mr
,
1372 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1374 * Disables any coalescing caused by memory_region_set_coalescing() or
1375 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1378 * @mr: the memory region to be updated.
1380 void memory_region_clear_coalescing(MemoryRegion
*mr
);
1383 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1386 * Ensure that pending coalesced MMIO request are flushed before the memory
1387 * region is accessed. This property is automatically enabled for all regions
1388 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1390 * @mr: the memory region to be updated.
1392 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
1395 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1398 * Clear the automatic coalesced MMIO flushing enabled via
1399 * memory_region_set_flush_coalesced. Note that this service has no effect on
1400 * memory regions that have MMIO coalescing enabled for themselves. For them,
1401 * automatic flushing will stop once coalescing is disabled.
1403 * @mr: the memory region to be updated.
1405 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1408 * memory_region_clear_global_locking: Declares that access processing does
1409 * not depend on the QEMU global lock.
1411 * By clearing this property, accesses to the memory region will be processed
1412 * outside of QEMU's global lock (unless the lock is held on when issuing the
1413 * access request). In this case, the device model implementing the access
1414 * handlers is responsible for synchronization of concurrency.
1416 * @mr: the memory region to be updated.
1418 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1421 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1422 * is written to a location.
1424 * Marks a word in an IO region (initialized with memory_region_init_io())
1425 * as a trigger for an eventfd event. The I/O callback will not be called.
1426 * The caller must be prepared to handle failure (that is, take the required
1427 * action if the callback _is_ called).
1429 * @mr: the memory region being updated.
1430 * @addr: the address within @mr that is to be monitored
1431 * @size: the size of the access to trigger the eventfd
1432 * @match_data: whether to match against @data, instead of just @addr
1433 * @data: the data to match against the guest write
1434 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1436 void memory_region_add_eventfd(MemoryRegion
*mr
,
1444 * memory_region_del_eventfd: Cancel an eventfd.
1446 * Cancels an eventfd trigger requested by a previous
1447 * memory_region_add_eventfd() call.
1449 * @mr: the memory region being updated.
1450 * @addr: the address within @mr that is to be monitored
1451 * @size: the size of the access to trigger the eventfd
1452 * @match_data: whether to match against @data, instead of just @addr
1453 * @data: the data to match against the guest write
1454 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1456 void memory_region_del_eventfd(MemoryRegion
*mr
,
1464 * memory_region_add_subregion: Add a subregion to a container.
1466 * Adds a subregion at @offset. The subregion may not overlap with other
1467 * subregions (except for those explicitly marked as overlapping). A region
1468 * may only be added once as a subregion (unless removed with
1469 * memory_region_del_subregion()); use memory_region_init_alias() if you
1470 * want a region to be a subregion in multiple locations.
1472 * @mr: the region to contain the new subregion; must be a container
1473 * initialized with memory_region_init().
1474 * @offset: the offset relative to @mr where @subregion is added.
1475 * @subregion: the subregion to be added.
1477 void memory_region_add_subregion(MemoryRegion
*mr
,
1479 MemoryRegion
*subregion
);
1481 * memory_region_add_subregion_overlap: Add a subregion to a container
1484 * Adds a subregion at @offset. The subregion may overlap with other
1485 * subregions. Conflicts are resolved by having a higher @priority hide a
1486 * lower @priority. Subregions without priority are taken as @priority 0.
1487 * A region may only be added once as a subregion (unless removed with
1488 * memory_region_del_subregion()); use memory_region_init_alias() if you
1489 * want a region to be a subregion in multiple locations.
1491 * @mr: the region to contain the new subregion; must be a container
1492 * initialized with memory_region_init().
1493 * @offset: the offset relative to @mr where @subregion is added.
1494 * @subregion: the subregion to be added.
1495 * @priority: used for resolving overlaps; highest priority wins.
1497 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1499 MemoryRegion
*subregion
,
1503 * memory_region_get_ram_addr: Get the ram address associated with a memory
1506 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1508 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1510 * memory_region_del_subregion: Remove a subregion.
1512 * Removes a subregion from its container.
1514 * @mr: the container to be updated.
1515 * @subregion: the region being removed; must be a current subregion of @mr.
1517 void memory_region_del_subregion(MemoryRegion
*mr
,
1518 MemoryRegion
*subregion
);
1521 * memory_region_set_enabled: dynamically enable or disable a region
1523 * Enables or disables a memory region. A disabled memory region
1524 * ignores all accesses to itself and its subregions. It does not
1525 * obscure sibling subregions with lower priority - it simply behaves as
1526 * if it was removed from the hierarchy.
1528 * Regions default to being enabled.
1530 * @mr: the region to be updated
1531 * @enabled: whether to enable or disable the region
1533 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1536 * memory_region_set_address: dynamically update the address of a region
1538 * Dynamically updates the address of a region, relative to its container.
1539 * May be used on regions are currently part of a memory hierarchy.
1541 * @mr: the region to be updated
1542 * @addr: new address, relative to container region
1544 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1547 * memory_region_set_size: dynamically update the size of a region.
1549 * Dynamically updates the size of a region.
1551 * @mr: the region to be updated
1552 * @size: used size of the region.
1554 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1557 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1559 * Dynamically updates the offset into the target region that an alias points
1560 * to, as if the fourth argument to memory_region_init_alias() has changed.
1562 * @mr: the #MemoryRegion to be updated; should be an alias.
1563 * @offset: the new offset into the target memory region
1565 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1569 * memory_region_present: checks if an address relative to a @container
1570 * translates into #MemoryRegion within @container
1572 * Answer whether a #MemoryRegion within @container covers the address
1575 * @container: a #MemoryRegion within which @addr is a relative address
1576 * @addr: the area within @container to be searched
1578 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1581 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1582 * into any address space.
1584 * @mr: a #MemoryRegion which should be checked if it's mapped
1586 bool memory_region_is_mapped(MemoryRegion
*mr
);
1589 * memory_region_find: translate an address/size relative to a
1590 * MemoryRegion into a #MemoryRegionSection.
1592 * Locates the first #MemoryRegion within @mr that overlaps the range
1593 * given by @addr and @size.
1595 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1596 * It will have the following characteristics:
1597 * .@size = 0 iff no overlap was found
1598 * .@mr is non-%NULL iff an overlap was found
1600 * Remember that in the return value the @offset_within_region is
1601 * relative to the returned region (in the .@mr field), not to the
1604 * Similarly, the .@offset_within_address_space is relative to the
1605 * address space that contains both regions, the passed and the
1606 * returned one. However, in the special case where the @mr argument
1607 * has no container (and thus is the root of the address space), the
1608 * following will hold:
1609 * .@offset_within_address_space >= @addr
1610 * .@offset_within_address_space + .@size <= @addr + @size
1612 * @mr: a MemoryRegion within which @addr is a relative address
1613 * @addr: start of the area within @as to be searched
1614 * @size: size of the area to be searched
1616 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1617 hwaddr addr
, uint64_t size
);
1620 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1622 * Synchronizes the dirty page log for all address spaces.
1624 void memory_global_dirty_log_sync(void);
1627 * memory_region_transaction_begin: Start a transaction.
1629 * During a transaction, changes will be accumulated and made visible
1630 * only when the transaction ends (is committed).
1632 void memory_region_transaction_begin(void);
1635 * memory_region_transaction_commit: Commit a transaction and make changes
1636 * visible to the guest.
1638 void memory_region_transaction_commit(void);
1641 * memory_listener_register: register callbacks to be called when memory
1642 * sections are mapped or unmapped into an address
1645 * @listener: an object containing the callbacks to be called
1646 * @filter: if non-%NULL, only regions in this address space will be observed
1648 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1651 * memory_listener_unregister: undo the effect of memory_listener_register()
1653 * @listener: an object containing the callbacks to be removed
1655 void memory_listener_unregister(MemoryListener
*listener
);
1658 * memory_global_dirty_log_start: begin dirty logging for all regions
1660 void memory_global_dirty_log_start(void);
1663 * memory_global_dirty_log_stop: end dirty logging for all regions
1665 void memory_global_dirty_log_stop(void);
1667 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
,
1668 bool dispatch_tree
, bool owner
);
1671 * memory_region_request_mmio_ptr: request a pointer to an mmio
1672 * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer.
1673 * When the device wants to invalidate the pointer it will call
1674 * memory_region_invalidate_mmio_ptr.
1676 * @mr: #MemoryRegion to check
1677 * @addr: address within that region
1679 * Returns true on success, false otherwise.
1681 bool memory_region_request_mmio_ptr(MemoryRegion
*mr
, hwaddr addr
);
1684 * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio
1685 * previously requested.
1686 * In the end that means that if something wants to execute from this area it
1687 * will need to request the pointer again.
1689 * @mr: #MemoryRegion associated to the pointer.
1690 * @offset: offset within the memory region
1691 * @size: size of that area.
1693 void memory_region_invalidate_mmio_ptr(MemoryRegion
*mr
, hwaddr offset
,
1697 * memory_region_dispatch_read: perform a read directly to the specified
1700 * @mr: #MemoryRegion to access
1701 * @addr: address within that region
1702 * @pval: pointer to uint64_t which the data is written to
1703 * @size: size of the access in bytes
1704 * @attrs: memory transaction attributes to use for the access
1706 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1712 * memory_region_dispatch_write: perform a write directly to the specified
1715 * @mr: #MemoryRegion to access
1716 * @addr: address within that region
1717 * @data: data to write
1718 * @size: size of the access in bytes
1719 * @attrs: memory transaction attributes to use for the access
1721 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1728 * address_space_init: initializes an address space
1730 * @as: an uninitialized #AddressSpace
1731 * @root: a #MemoryRegion that routes addresses for the address space
1732 * @name: an address space name. The name is only used for debugging
1735 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1738 * address_space_destroy: destroy an address space
1740 * Releases all resources associated with an address space. After an address space
1741 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1744 * @as: address space to be destroyed
1746 void address_space_destroy(AddressSpace
*as
);
1749 * address_space_rw: read from or write to an address space.
1751 * Return a MemTxResult indicating whether the operation succeeded
1752 * or failed (eg unassigned memory, device rejected the transaction,
1755 * @as: #AddressSpace to be accessed
1756 * @addr: address within that address space
1757 * @attrs: memory transaction attributes
1758 * @buf: buffer with the data transferred
1759 * @len: the number of bytes to read or write
1760 * @is_write: indicates the transfer direction
1762 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1763 MemTxAttrs attrs
, uint8_t *buf
,
1764 int len
, bool is_write
);
1767 * address_space_write: write to address space.
1769 * Return a MemTxResult indicating whether the operation succeeded
1770 * or failed (eg unassigned memory, device rejected the transaction,
1773 * @as: #AddressSpace to be accessed
1774 * @addr: address within that address space
1775 * @attrs: memory transaction attributes
1776 * @buf: buffer with the data transferred
1777 * @len: the number of bytes to write
1779 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1781 const uint8_t *buf
, int len
);
1783 /* address_space_ld*: load from an address space
1784 * address_space_st*: store to an address space
1786 * These functions perform a load or store of the byte, word,
1787 * longword or quad to the specified address within the AddressSpace.
1788 * The _le suffixed functions treat the data as little endian;
1789 * _be indicates big endian; no suffix indicates "same endianness
1792 * The "guest CPU endianness" accessors are deprecated for use outside
1793 * target-* code; devices should be CPU-agnostic and use either the LE
1794 * or the BE accessors.
1796 * @as #AddressSpace to be accessed
1797 * @addr: address within that address space
1798 * @val: data value, for stores
1799 * @attrs: memory transaction attributes
1800 * @result: location to write the success/failure of the transaction;
1801 * if NULL, this information is discarded
1806 #define ARG1_DECL AddressSpace *as
1807 #include "exec/memory_ldst.inc.h"
1811 #define ARG1_DECL AddressSpace *as
1812 #include "exec/memory_ldst_phys.inc.h"
1814 struct MemoryRegionCache
{
1819 MemoryRegionSection mrs
;
1823 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
1826 /* address_space_ld*_cached: load from a cached #MemoryRegion
1827 * address_space_st*_cached: store into a cached #MemoryRegion
1829 * These functions perform a load or store of the byte, word,
1830 * longword or quad to the specified address. The address is
1831 * a physical address in the AddressSpace, but it must lie within
1832 * a #MemoryRegion that was mapped with address_space_cache_init.
1834 * The _le suffixed functions treat the data as little endian;
1835 * _be indicates big endian; no suffix indicates "same endianness
1838 * The "guest CPU endianness" accessors are deprecated for use outside
1839 * target-* code; devices should be CPU-agnostic and use either the LE
1840 * or the BE accessors.
1842 * @cache: previously initialized #MemoryRegionCache to be accessed
1843 * @addr: address within the address space
1844 * @val: data value, for stores
1845 * @attrs: memory transaction attributes
1846 * @result: location to write the success/failure of the transaction;
1847 * if NULL, this information is discarded
1850 #define SUFFIX _cached_slow
1852 #define ARG1_DECL MemoryRegionCache *cache
1853 #include "exec/memory_ldst.inc.h"
1855 /* Inline fast path for direct RAM access. */
1856 static inline uint8_t address_space_ldub_cached(MemoryRegionCache
*cache
,
1857 hwaddr addr
, MemTxAttrs attrs
, MemTxResult
*result
)
1859 assert(addr
< cache
->len
);
1860 if (likely(cache
->ptr
)) {
1861 return ldub_p(cache
->ptr
+ addr
);
1863 return address_space_ldub_cached_slow(cache
, addr
, attrs
, result
);
1867 static inline void address_space_stb_cached(MemoryRegionCache
*cache
,
1868 hwaddr addr
, uint32_t val
, MemTxAttrs attrs
, MemTxResult
*result
)
1870 assert(addr
< cache
->len
);
1871 if (likely(cache
->ptr
)) {
1872 stb_p(cache
->ptr
+ addr
, val
);
1874 address_space_stb_cached_slow(cache
, addr
, val
, attrs
, result
);
1878 #define ENDIANNESS _le
1879 #include "exec/memory_ldst_cached.inc.h"
1881 #define ENDIANNESS _be
1882 #include "exec/memory_ldst_cached.inc.h"
1884 #define SUFFIX _cached
1886 #define ARG1_DECL MemoryRegionCache *cache
1887 #include "exec/memory_ldst_phys.inc.h"
1889 /* address_space_cache_init: prepare for repeated access to a physical
1892 * @cache: #MemoryRegionCache to be filled
1893 * @as: #AddressSpace to be accessed
1894 * @addr: address within that address space
1895 * @len: length of buffer
1896 * @is_write: indicates the transfer direction
1898 * Will only work with RAM, and may map a subset of the requested range by
1899 * returning a value that is less than @len. On failure, return a negative
1902 * Because it only works with RAM, this function can be used for
1903 * read-modify-write operations. In this case, is_write should be %true.
1905 * Note that addresses passed to the address_space_*_cached functions
1906 * are relative to @addr.
1908 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1915 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1917 * @cache: The #MemoryRegionCache to operate on.
1918 * @addr: The first physical address that was written, relative to the
1919 * address that was passed to @address_space_cache_init.
1920 * @access_len: The number of bytes that were written starting at @addr.
1922 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1927 * address_space_cache_destroy: free a #MemoryRegionCache
1929 * @cache: The #MemoryRegionCache whose memory should be released.
1931 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1933 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1934 * entry. Should be called from an RCU critical section.
1936 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1937 bool is_write
, MemTxAttrs attrs
);
1939 /* address_space_translate: translate an address range into an address space
1940 * into a MemoryRegion and an address range into that section. Should be
1941 * called from an RCU critical section, to avoid that the last reference
1942 * to the returned region disappears after address_space_translate returns.
1944 * @fv: #FlatView to be accessed
1945 * @addr: address within that address space
1946 * @xlat: pointer to address within the returned memory region section's
1948 * @len: pointer to length
1949 * @is_write: indicates the transfer direction
1950 * @attrs: memory attributes
1952 MemoryRegion
*flatview_translate(FlatView
*fv
,
1953 hwaddr addr
, hwaddr
*xlat
,
1954 hwaddr
*len
, bool is_write
,
1957 static inline MemoryRegion
*address_space_translate(AddressSpace
*as
,
1958 hwaddr addr
, hwaddr
*xlat
,
1959 hwaddr
*len
, bool is_write
,
1962 return flatview_translate(address_space_to_flatview(as
),
1963 addr
, xlat
, len
, is_write
, attrs
);
1966 /* address_space_access_valid: check for validity of accessing an address
1969 * Check whether memory is assigned to the given address space range, and
1970 * access is permitted by any IOMMU regions that are active for the address
1973 * For now, addr and len should be aligned to a page size. This limitation
1974 * will be lifted in the future.
1976 * @as: #AddressSpace to be accessed
1977 * @addr: address within that address space
1978 * @len: length of the area to be checked
1979 * @is_write: indicates the transfer direction
1980 * @attrs: memory attributes
1982 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
,
1983 bool is_write
, MemTxAttrs attrs
);
1985 /* address_space_map: map a physical memory region into a host virtual address
1987 * May map a subset of the requested range, given by and returned in @plen.
1988 * May return %NULL if resources needed to perform the mapping are exhausted.
1989 * Use only for reads OR writes - not for read-modify-write operations.
1990 * Use cpu_register_map_client() to know when retrying the map operation is
1991 * likely to succeed.
1993 * @as: #AddressSpace to be accessed
1994 * @addr: address within that address space
1995 * @plen: pointer to length of buffer; updated on return
1996 * @is_write: indicates the transfer direction
1997 * @attrs: memory attributes
1999 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
2000 hwaddr
*plen
, bool is_write
, MemTxAttrs attrs
);
2002 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2004 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2005 * the amount of memory that was actually read or written by the caller.
2007 * @as: #AddressSpace used
2008 * @buffer: host pointer as returned by address_space_map()
2009 * @len: buffer length as returned by address_space_map()
2010 * @access_len: amount of data actually transferred
2011 * @is_write: indicates the transfer direction
2013 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
2014 int is_write
, hwaddr access_len
);
2017 /* Internal functions, part of the implementation of address_space_read. */
2018 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
2019 MemTxAttrs attrs
, uint8_t *buf
, int len
);
2020 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
2021 MemTxAttrs attrs
, uint8_t *buf
,
2022 int len
, hwaddr addr1
, hwaddr l
,
2024 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
2026 /* Internal functions, part of the implementation of address_space_read_cached
2027 * and address_space_write_cached. */
2028 void address_space_read_cached_slow(MemoryRegionCache
*cache
,
2029 hwaddr addr
, void *buf
, int len
);
2030 void address_space_write_cached_slow(MemoryRegionCache
*cache
,
2031 hwaddr addr
, const void *buf
, int len
);
2033 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
2036 return memory_region_is_ram(mr
) &&
2037 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
2039 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
2040 memory_region_is_romd(mr
);
2045 * address_space_read: read from an address space.
2047 * Return a MemTxResult indicating whether the operation succeeded
2048 * or failed (eg unassigned memory, device rejected the transaction,
2049 * IOMMU fault). Called within RCU critical section.
2051 * @as: #AddressSpace to be accessed
2052 * @addr: address within that address space
2053 * @attrs: memory transaction attributes
2054 * @buf: buffer with the data transferred
2056 static inline __attribute__((__always_inline__
))
2057 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
2058 MemTxAttrs attrs
, uint8_t *buf
,
2061 MemTxResult result
= MEMTX_OK
;
2067 if (__builtin_constant_p(len
)) {
2070 fv
= address_space_to_flatview(as
);
2072 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false, attrs
);
2073 if (len
== l
&& memory_access_is_direct(mr
, false)) {
2074 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
2075 memcpy(buf
, ptr
, len
);
2077 result
= flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
2083 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
2089 * address_space_read_cached: read from a cached RAM region
2091 * @cache: Cached region to be addressed
2092 * @addr: address relative to the base of the RAM region
2093 * @buf: buffer with the data transferred
2094 * @len: length of the data transferred
2097 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2100 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2101 if (likely(cache
->ptr
)) {
2102 memcpy(buf
, cache
->ptr
+ addr
, len
);
2104 address_space_read_cached_slow(cache
, addr
, buf
, len
);
2109 * address_space_write_cached: write to a cached RAM region
2111 * @cache: Cached region to be addressed
2112 * @addr: address relative to the base of the RAM region
2113 * @buf: buffer with the data transferred
2114 * @len: length of the data transferred
2117 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
2120 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
2121 if (likely(cache
->ptr
)) {
2122 memcpy(cache
->ptr
+ addr
, buf
, len
);
2124 address_space_write_cached_slow(cache
, addr
, buf
, len
);