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 */
101 QLIST_ENTRY(IOMMUNotifier
) node
;
103 typedef struct IOMMUNotifier IOMMUNotifier
;
105 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
106 IOMMUNotifierFlag flags
,
107 hwaddr start
, hwaddr end
)
110 n
->notifier_flags
= flags
;
116 * Memory region callbacks
118 struct MemoryRegionOps
{
119 /* Read from the memory region. @addr is relative to @mr; @size is
121 uint64_t (*read
)(void *opaque
,
124 /* Write to the memory region. @addr is relative to @mr; @size is
126 void (*write
)(void *opaque
,
131 MemTxResult (*read_with_attrs
)(void *opaque
,
136 MemTxResult (*write_with_attrs
)(void *opaque
,
141 /* Instruction execution pre-callback:
142 * @addr is the address of the access relative to the @mr.
143 * @size is the size of the area returned by the callback.
144 * @offset is the location of the pointer inside @mr.
146 * Returns a pointer to a location which contains guest code.
148 void *(*request_ptr
)(void *opaque
, hwaddr addr
, unsigned *size
,
151 enum device_endian endianness
;
152 /* Guest-visible constraints: */
154 /* If nonzero, specify bounds on access sizes beyond which a machine
157 unsigned min_access_size
;
158 unsigned max_access_size
;
159 /* If true, unaligned accesses are supported. Otherwise unaligned
160 * accesses throw machine checks.
164 * If present, and returns #false, the transaction is not accepted
165 * by the device (and results in machine dependent behaviour such
166 * as a machine check exception).
168 bool (*accepts
)(void *opaque
, hwaddr addr
,
169 unsigned size
, bool is_write
);
171 /* Internal implementation constraints: */
173 /* If nonzero, specifies the minimum size implemented. Smaller sizes
174 * will be rounded upwards and a partial result will be returned.
176 unsigned min_access_size
;
177 /* If nonzero, specifies the maximum size implemented. Larger sizes
178 * will be done as a series of accesses with smaller sizes.
180 unsigned max_access_size
;
181 /* If true, unaligned accesses are supported. Otherwise all accesses
182 * are converted to (possibly multiple) naturally aligned accesses.
187 /* If .read and .write are not present, old_mmio may be used for
188 * backwards compatibility with old mmio registration
190 const MemoryRegionMmio old_mmio
;
193 typedef struct IOMMUMemoryRegionClass
{
195 struct DeviceClass parent_class
;
198 * Return a TLB entry that contains a given address. Flag should
199 * be the access permission of this translation operation. We can
200 * set flag to IOMMU_NONE to mean that we don't need any
201 * read/write permission checks, like, when for region replay.
203 IOMMUTLBEntry (*translate
)(IOMMUMemoryRegion
*iommu
, hwaddr addr
,
204 IOMMUAccessFlags flag
);
205 /* Returns minimum supported page size */
206 uint64_t (*get_min_page_size
)(IOMMUMemoryRegion
*iommu
);
207 /* Called when IOMMU Notifier flag changed */
208 void (*notify_flag_changed
)(IOMMUMemoryRegion
*iommu
,
209 IOMMUNotifierFlag old_flags
,
210 IOMMUNotifierFlag new_flags
);
211 /* Set this up to provide customized IOMMU replay function */
212 void (*replay
)(IOMMUMemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
213 } IOMMUMemoryRegionClass
;
215 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
216 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
218 struct MemoryRegion
{
221 /* All fields are private - violators will be prosecuted */
223 /* The following fields should fit in a cache line */
227 bool readonly
; /* For RAM regions */
229 bool flush_coalesced_mmio
;
231 uint8_t dirty_log_mask
;
236 const MemoryRegionOps
*ops
;
238 MemoryRegion
*container
;
241 void (*destructor
)(MemoryRegion
*mr
);
246 bool warning_printed
; /* For reservations */
247 uint8_t vga_logging_count
;
251 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
252 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
253 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
255 unsigned ioeventfd_nb
;
256 MemoryRegionIoeventfd
*ioeventfds
;
259 struct IOMMUMemoryRegion
{
260 MemoryRegion parent_obj
;
262 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
263 IOMMUNotifierFlag iommu_notify_flags
;
266 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
267 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
270 * MemoryListener: callbacks structure for updates to the physical memory map
272 * Allows a component to adjust to changes in the guest-visible memory map.
273 * Use with memory_listener_register() and memory_listener_unregister().
275 struct MemoryListener
{
276 void (*begin
)(MemoryListener
*listener
);
277 void (*commit
)(MemoryListener
*listener
);
278 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
279 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
280 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
281 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
283 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
285 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
286 void (*log_global_start
)(MemoryListener
*listener
);
287 void (*log_global_stop
)(MemoryListener
*listener
);
288 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
289 bool match_data
, uint64_t data
, EventNotifier
*e
);
290 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
291 bool match_data
, uint64_t data
, EventNotifier
*e
);
292 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
293 hwaddr addr
, hwaddr len
);
294 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
295 hwaddr addr
, hwaddr len
);
296 /* Lower = earlier (during add), later (during del) */
298 AddressSpace
*address_space
;
299 QTAILQ_ENTRY(MemoryListener
) link
;
300 QTAILQ_ENTRY(MemoryListener
) link_as
;
304 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
306 struct AddressSpace
{
307 /* All fields are private. */
312 /* Accessed via RCU. */
313 struct FlatView
*current_map
;
316 struct MemoryRegionIoeventfd
*ioeventfds
;
317 QTAILQ_HEAD(memory_listeners_as
, MemoryListener
) listeners
;
318 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
321 FlatView
*address_space_to_flatview(AddressSpace
*as
);
324 * MemoryRegionSection: describes a fragment of a #MemoryRegion
326 * @mr: the region, or %NULL if empty
327 * @address_space: the address space the region is mapped in
328 * @offset_within_region: the beginning of the section, relative to @mr's start
329 * @size: the size of the section; will not exceed @mr's boundaries
330 * @offset_within_address_space: the address of the first byte of the section
331 * relative to the region's address space
332 * @readonly: writes to this section are ignored
334 struct MemoryRegionSection
{
337 hwaddr offset_within_region
;
339 hwaddr offset_within_address_space
;
344 * memory_region_init: Initialize a memory region
346 * The region typically acts as a container for other memory regions. Use
347 * memory_region_add_subregion() to add subregions.
349 * @mr: the #MemoryRegion to be initialized
350 * @owner: the object that tracks the region's reference count
351 * @name: used for debugging; not visible to the user or ABI
352 * @size: size of the region; any subregions beyond this size will be clipped
354 void memory_region_init(MemoryRegion
*mr
,
355 struct Object
*owner
,
360 * memory_region_ref: Add 1 to a memory region's reference count
362 * Whenever memory regions are accessed outside the BQL, they need to be
363 * preserved against hot-unplug. MemoryRegions actually do not have their
364 * own reference count; they piggyback on a QOM object, their "owner".
365 * This function adds a reference to the owner.
367 * All MemoryRegions must have an owner if they can disappear, even if the
368 * device they belong to operates exclusively under the BQL. This is because
369 * the region could be returned at any time by memory_region_find, and this
370 * is usually under guest control.
372 * @mr: the #MemoryRegion
374 void memory_region_ref(MemoryRegion
*mr
);
377 * memory_region_unref: Remove 1 to a memory region's reference count
379 * Whenever memory regions are accessed outside the BQL, they need to be
380 * preserved against hot-unplug. MemoryRegions actually do not have their
381 * own reference count; they piggyback on a QOM object, their "owner".
382 * This function removes a reference to the owner and possibly destroys it.
384 * @mr: the #MemoryRegion
386 void memory_region_unref(MemoryRegion
*mr
);
389 * memory_region_init_io: Initialize an I/O memory region.
391 * Accesses into the region will cause the callbacks in @ops to be called.
392 * if @size is nonzero, subregions will be clipped to @size.
394 * @mr: the #MemoryRegion to be initialized.
395 * @owner: the object that tracks the region's reference count
396 * @ops: a structure containing read and write callbacks to be used when
397 * I/O is performed on the region.
398 * @opaque: passed to the read and write callbacks of the @ops structure.
399 * @name: used for debugging; not visible to the user or ABI
400 * @size: size of the region.
402 void memory_region_init_io(MemoryRegion
*mr
,
403 struct Object
*owner
,
404 const MemoryRegionOps
*ops
,
410 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
411 * into the region will modify memory
414 * @mr: the #MemoryRegion to be initialized.
415 * @owner: the object that tracks the region's reference count
416 * @name: Region name, becomes part of RAMBlock name used in migration stream
417 * must be unique within any device
418 * @size: size of the region.
419 * @errp: pointer to Error*, to store an error if it happens.
421 * Note that this function does not do anything to cause the data in the
422 * RAM memory region to be migrated; that is the responsibility of the caller.
424 void memory_region_init_ram_nomigrate(MemoryRegion
*mr
,
425 struct Object
*owner
,
431 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
432 * RAM. Accesses into the region will
433 * modify memory directly. Only an initial
434 * portion of this RAM is actually used.
435 * The used size can change across reboots.
437 * @mr: the #MemoryRegion to be initialized.
438 * @owner: the object that tracks the region's reference count
439 * @name: Region name, becomes part of RAMBlock name used in migration stream
440 * must be unique within any device
441 * @size: used size of the region.
442 * @max_size: max size of the region.
443 * @resized: callback to notify owner about used size change.
444 * @errp: pointer to Error*, to store an error if it happens.
446 * Note that this function does not do anything to cause the data in the
447 * RAM memory region to be migrated; that is the responsibility of the caller.
449 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
450 struct Object
*owner
,
454 void (*resized
)(const char*,
460 * memory_region_init_ram_from_file: Initialize RAM memory region with a
463 * @mr: the #MemoryRegion to be initialized.
464 * @owner: the object that tracks the region's reference count
465 * @name: Region name, becomes part of RAMBlock name used in migration stream
466 * must be unique within any device
467 * @size: size of the region.
468 * @align: alignment of the region base address; if 0, the default alignment
469 * (getpagesize()) will be used.
470 * @share: %true if memory must be mmaped with the MAP_SHARED flag
471 * @path: the path in which to allocate the RAM.
472 * @errp: pointer to Error*, to store an error if it happens.
474 * Note that this function does not do anything to cause the data in the
475 * RAM memory region to be migrated; that is the responsibility of the caller.
477 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
478 struct Object
*owner
,
487 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
490 * @mr: the #MemoryRegion to be initialized.
491 * @owner: the object that tracks the region's reference count
492 * @name: the name of the region.
493 * @size: size of the region.
494 * @share: %true if memory must be mmaped with the MAP_SHARED flag
495 * @fd: the fd to mmap.
496 * @errp: pointer to Error*, to store an error if it happens.
498 * Note that this function does not do anything to cause the data in the
499 * RAM memory region to be migrated; that is the responsibility of the caller.
501 void memory_region_init_ram_from_fd(MemoryRegion
*mr
,
502 struct Object
*owner
,
511 * memory_region_init_ram_ptr: Initialize RAM memory region from a
512 * user-provided pointer. Accesses into the
513 * region will modify memory directly.
515 * @mr: the #MemoryRegion to be initialized.
516 * @owner: the object that tracks the region's reference count
517 * @name: Region name, becomes part of RAMBlock name used in migration stream
518 * must be unique within any device
519 * @size: size of the region.
520 * @ptr: memory to be mapped; must contain at least @size bytes.
522 * Note that this function does not do anything to cause the data in the
523 * RAM memory region to be migrated; that is the responsibility of the caller.
525 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
526 struct Object
*owner
,
532 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
533 * a user-provided pointer.
535 * A RAM device represents a mapping to a physical device, such as to a PCI
536 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
537 * into the VM address space and access to the region will modify memory
538 * directly. However, the memory region should not be included in a memory
539 * dump (device may not be enabled/mapped at the time of the dump), and
540 * operations incompatible with manipulating MMIO should be avoided. Replaces
543 * @mr: the #MemoryRegion to be initialized.
544 * @owner: the object that tracks the region's reference count
545 * @name: the name of the region.
546 * @size: size of the region.
547 * @ptr: memory to be mapped; must contain at least @size bytes.
549 * Note that this function does not do anything to cause the data in the
550 * RAM memory region to be migrated; that is the responsibility of the caller.
551 * (For RAM device memory regions, migrating the contents rarely makes sense.)
553 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
554 struct Object
*owner
,
560 * memory_region_init_alias: Initialize a memory region that aliases all or a
561 * part of another memory region.
563 * @mr: the #MemoryRegion to be initialized.
564 * @owner: the object that tracks the region's reference count
565 * @name: used for debugging; not visible to the user or ABI
566 * @orig: the region to be referenced; @mr will be equivalent to
567 * @orig between @offset and @offset + @size - 1.
568 * @offset: start of the section in @orig to be referenced.
569 * @size: size of the region.
571 void memory_region_init_alias(MemoryRegion
*mr
,
572 struct Object
*owner
,
579 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
581 * This has the same effect as calling memory_region_init_ram_nomigrate()
582 * and then marking the resulting region read-only with
583 * memory_region_set_readonly().
585 * Note that this function does not do anything to cause the data in the
586 * RAM side of the memory region to be migrated; that is the responsibility
589 * @mr: the #MemoryRegion to be initialized.
590 * @owner: the object that tracks the region's reference count
591 * @name: Region name, becomes part of RAMBlock name used in migration stream
592 * must be unique within any device
593 * @size: size of the region.
594 * @errp: pointer to Error*, to store an error if it happens.
596 void memory_region_init_rom_nomigrate(MemoryRegion
*mr
,
597 struct Object
*owner
,
603 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
604 * Writes are handled via callbacks.
606 * Note that this function does not do anything to cause the data in the
607 * RAM side of the memory region to be migrated; that is the responsibility
610 * @mr: the #MemoryRegion to be initialized.
611 * @owner: the object that tracks the region's reference count
612 * @ops: callbacks for write access handling (must not be NULL).
613 * @name: Region name, becomes part of RAMBlock name used in migration stream
614 * must be unique within any device
615 * @size: size of the region.
616 * @errp: pointer to Error*, to store an error if it happens.
618 void memory_region_init_rom_device_nomigrate(MemoryRegion
*mr
,
619 struct Object
*owner
,
620 const MemoryRegionOps
*ops
,
627 * memory_region_init_reservation: Initialize a memory region that reserves
630 * A reservation region primariy serves debugging purposes. It claims I/O
631 * space that is not supposed to be handled by QEMU itself. Any access via
632 * the memory API will cause an abort().
633 * This function is deprecated. Use memory_region_init_io() with NULL
636 * @mr: the #MemoryRegion to be initialized
637 * @owner: the object that tracks the region's reference count
638 * @name: used for debugging; not visible to the user or ABI
639 * @size: size of the region.
641 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
646 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
650 * memory_region_init_iommu: Initialize a memory region of a custom type
651 * that translates addresses
653 * An IOMMU region translates addresses and forwards accesses to a target
656 * @typename: QOM class name
657 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
658 * @instance_size: the IOMMUMemoryRegion subclass instance size
659 * @owner: the object that tracks the region's reference count
660 * @ops: a function that translates addresses into the @target region
661 * @name: used for debugging; not visible to the user or ABI
662 * @size: size of the region.
664 void memory_region_init_iommu(void *_iommu_mr
,
665 size_t instance_size
,
666 const char *mrtypename
,
672 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
673 * region will modify memory directly.
675 * @mr: the #MemoryRegion to be initialized
676 * @owner: the object that tracks the region's reference count (must be
677 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
678 * @name: name of the memory region
679 * @size: size of the region in bytes
680 * @errp: pointer to Error*, to store an error if it happens.
682 * This function allocates RAM for a board model or device, and
683 * arranges for it to be migrated (by calling vmstate_register_ram()
684 * if @owner is a DeviceState, or vmstate_register_ram_global() if
687 * TODO: Currently we restrict @owner to being either NULL (for
688 * global RAM regions with no owner) or devices, so that we can
689 * give the RAM block a unique name for migration purposes.
690 * We should lift this restriction and allow arbitrary Objects.
691 * If you pass a non-NULL non-device @owner then we will assert.
693 void memory_region_init_ram(MemoryRegion
*mr
,
694 struct Object
*owner
,
700 * memory_region_init_rom: Initialize a ROM memory region.
702 * This has the same effect as calling memory_region_init_ram()
703 * and then marking the resulting region read-only with
704 * memory_region_set_readonly(). This includes arranging for the
705 * contents to be migrated.
707 * TODO: Currently we restrict @owner to being either NULL (for
708 * global RAM regions with no owner) or devices, so that we can
709 * give the RAM block a unique name for migration purposes.
710 * We should lift this restriction and allow arbitrary Objects.
711 * If you pass a non-NULL non-device @owner then we will assert.
713 * @mr: the #MemoryRegion to be initialized.
714 * @owner: the object that tracks the region's reference count
715 * @name: Region name, becomes part of RAMBlock name used in migration stream
716 * must be unique within any device
717 * @size: size of the region.
718 * @errp: pointer to Error*, to store an error if it happens.
720 void memory_region_init_rom(MemoryRegion
*mr
,
721 struct Object
*owner
,
727 * memory_region_init_rom_device: Initialize a ROM memory region.
728 * Writes are handled via callbacks.
730 * This function initializes a memory region backed by RAM for reads
731 * and callbacks for writes, and arranges for the RAM backing to
732 * be migrated (by calling vmstate_register_ram()
733 * if @owner is a DeviceState, or vmstate_register_ram_global() if
736 * TODO: Currently we restrict @owner to being either NULL (for
737 * global RAM regions with no owner) or devices, so that we can
738 * give the RAM block a unique name for migration purposes.
739 * We should lift this restriction and allow arbitrary Objects.
740 * If you pass a non-NULL non-device @owner then we will assert.
742 * @mr: the #MemoryRegion to be initialized.
743 * @owner: the object that tracks the region's reference count
744 * @ops: callbacks for write access handling (must not be NULL).
745 * @name: Region name, becomes part of RAMBlock name used in migration stream
746 * must be unique within any device
747 * @size: size of the region.
748 * @errp: pointer to Error*, to store an error if it happens.
750 void memory_region_init_rom_device(MemoryRegion
*mr
,
751 struct Object
*owner
,
752 const MemoryRegionOps
*ops
,
760 * memory_region_owner: get a memory region's owner.
762 * @mr: the memory region being queried.
764 struct Object
*memory_region_owner(MemoryRegion
*mr
);
767 * memory_region_size: get a memory region's size.
769 * @mr: the memory region being queried.
771 uint64_t memory_region_size(MemoryRegion
*mr
);
774 * memory_region_is_ram: check whether a memory region is random access
776 * Returns %true is a memory region is random access.
778 * @mr: the memory region being queried
780 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
786 * memory_region_is_ram_device: check whether a memory region is a ram device
788 * Returns %true is a memory region is a device backed ram region
790 * @mr: the memory region being queried
792 bool memory_region_is_ram_device(MemoryRegion
*mr
);
795 * memory_region_is_romd: check whether a memory region is in ROMD mode
797 * Returns %true if a memory region is a ROM device and currently set to allow
800 * @mr: the memory region being queried
802 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
804 return mr
->rom_device
&& mr
->romd_mode
;
808 * memory_region_get_iommu: check whether a memory region is an iommu
810 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
813 * @mr: the memory region being queried
815 static inline IOMMUMemoryRegion
*memory_region_get_iommu(MemoryRegion
*mr
)
818 return memory_region_get_iommu(mr
->alias
);
821 return (IOMMUMemoryRegion
*) mr
;
827 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
828 * if an iommu or NULL if not
830 * Returns pointer to IOMMUMemoryRegioniClass if a memory region is an iommu,
831 * otherwise NULL. This is fast path avoinding QOM checking, use with caution.
833 * @mr: the memory region being queried
835 static inline IOMMUMemoryRegionClass
*memory_region_get_iommu_class_nocheck(
836 IOMMUMemoryRegion
*iommu_mr
)
838 return (IOMMUMemoryRegionClass
*) (((Object
*)iommu_mr
)->class);
841 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
844 * memory_region_iommu_get_min_page_size: get minimum supported page size
847 * Returns minimum supported page size for an iommu.
849 * @iommu_mr: the memory region being queried
851 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion
*iommu_mr
);
854 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
856 * The notification type will be decided by entry.perm bits:
858 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
859 * - For MAP (newly added entry) notifies: set entry.perm to the
860 * permission of the page (which is definitely !IOMMU_NONE).
862 * Note: for any IOMMU implementation, an in-place mapping change
863 * should be notified with an UNMAP followed by a MAP.
865 * @iommu_mr: the memory region that was changed
866 * @entry: the new entry in the IOMMU translation table. The entry
867 * replaces all old entries for the same virtual I/O address range.
868 * Deleted entries have .@perm == 0.
870 void memory_region_notify_iommu(IOMMUMemoryRegion
*iommu_mr
,
871 IOMMUTLBEntry entry
);
874 * memory_region_notify_one: notify a change in an IOMMU translation
875 * entry to a single notifier
877 * This works just like memory_region_notify_iommu(), but it only
878 * notifies a specific notifier, not all of them.
880 * @notifier: the notifier to be notified
881 * @entry: the new entry in the IOMMU translation table. The entry
882 * replaces all old entries for the same virtual I/O address range.
883 * Deleted entries have .@perm == 0.
885 void memory_region_notify_one(IOMMUNotifier
*notifier
,
886 IOMMUTLBEntry
*entry
);
889 * memory_region_register_iommu_notifier: register a notifier for changes to
890 * IOMMU translation entries.
892 * @mr: the memory region to observe
893 * @n: the IOMMUNotifier to be added; the notify callback receives a
894 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
895 * ceases to be valid on exit from the notifier.
897 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
901 * memory_region_iommu_replay: replay existing IOMMU translations to
902 * a notifier with the minimum page granularity returned by
903 * mr->iommu_ops->get_page_size().
905 * @iommu_mr: the memory region to observe
906 * @n: the notifier to which to replay iommu mappings
908 void memory_region_iommu_replay(IOMMUMemoryRegion
*iommu_mr
, IOMMUNotifier
*n
);
911 * memory_region_iommu_replay_all: replay existing IOMMU translations
912 * to all the notifiers registered.
914 * @iommu_mr: the memory region to observe
916 void memory_region_iommu_replay_all(IOMMUMemoryRegion
*iommu_mr
);
919 * memory_region_unregister_iommu_notifier: unregister a notifier for
920 * changes to IOMMU translation entries.
922 * @mr: the memory region which was observed and for which notity_stopped()
924 * @n: the notifier to be removed.
926 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
930 * memory_region_name: get a memory region's name
932 * Returns the string that was used to initialize the memory region.
934 * @mr: the memory region being queried
936 const char *memory_region_name(const MemoryRegion
*mr
);
939 * memory_region_is_logging: return whether a memory region is logging writes
941 * Returns %true if the memory region is logging writes for the given client
943 * @mr: the memory region being queried
944 * @client: the client being queried
946 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
949 * memory_region_get_dirty_log_mask: return the clients for which a
950 * memory region is logging writes.
952 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
953 * are the bit indices.
955 * @mr: the memory region being queried
957 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
960 * memory_region_is_rom: check whether a memory region is ROM
962 * Returns %true is a memory region is read-only memory.
964 * @mr: the memory region being queried
966 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
968 return mr
->ram
&& mr
->readonly
;
973 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
975 * Returns a file descriptor backing a file-based RAM memory region,
976 * or -1 if the region is not a file-based RAM memory region.
978 * @mr: the RAM or alias memory region being queried.
980 int memory_region_get_fd(MemoryRegion
*mr
);
983 * memory_region_from_host: Convert a pointer into a RAM memory region
984 * and an offset within it.
986 * Given a host pointer inside a RAM memory region (created with
987 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
988 * the MemoryRegion and the offset within it.
990 * Use with care; by the time this function returns, the returned pointer is
991 * not protected by RCU anymore. If the caller is not within an RCU critical
992 * section and does not hold the iothread lock, it must have other means of
993 * protecting the pointer, such as a reference to the region that includes
994 * the incoming ram_addr_t.
996 * @mr: the memory region being queried.
998 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
1001 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1003 * Returns a host pointer to a RAM memory region (created with
1004 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1006 * Use with care; by the time this function returns, the returned pointer is
1007 * not protected by RCU anymore. If the caller is not within an RCU critical
1008 * section and does not hold the iothread lock, it must have other means of
1009 * protecting the pointer, such as a reference to the region that includes
1010 * the incoming ram_addr_t.
1012 * @mr: the memory region being queried.
1014 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
1016 /* memory_region_ram_resize: Resize a RAM region.
1018 * Only legal before guest might have detected the memory size: e.g. on
1019 * incoming migration, or right after reset.
1021 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1022 * @newsize: the new size the region
1023 * @errp: pointer to Error*, to store an error if it happens.
1025 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
1029 * memory_region_set_log: Turn dirty logging on or off for a region.
1031 * Turns dirty logging on or off for a specified client (display, migration).
1032 * Only meaningful for RAM regions.
1034 * @mr: the memory region being updated.
1035 * @log: whether dirty logging is to be enabled or disabled.
1036 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1038 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
1041 * memory_region_get_dirty: Check whether a range of bytes is dirty
1042 * for a specified client.
1044 * Checks whether a range of bytes has been written to since the last
1045 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1048 * @mr: the memory region being queried.
1049 * @addr: the address (relative to the start of the region) being queried.
1050 * @size: the size of the range being queried.
1051 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1052 * %DIRTY_MEMORY_VGA.
1054 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
1055 hwaddr size
, unsigned client
);
1058 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1060 * Marks a range of bytes as dirty, after it has been dirtied outside
1063 * @mr: the memory region being dirtied.
1064 * @addr: the address (relative to the start of the region) being dirtied.
1065 * @size: size of the range being dirtied.
1067 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
1071 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
1072 * for a specified client. It clears them.
1074 * Checks whether a range of bytes has been written to since the last
1075 * call to memory_region_reset_dirty() with the same @client. Dirty logging
1078 * @mr: the memory region being queried.
1079 * @addr: the address (relative to the start of the region) being queried.
1080 * @size: the size of the range being queried.
1081 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1082 * %DIRTY_MEMORY_VGA.
1084 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
1085 hwaddr size
, unsigned client
);
1088 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1089 * bitmap and clear it.
1091 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1092 * returns the snapshot. The snapshot can then be used to query dirty
1093 * status, using memory_region_snapshot_get_dirty. Unlike
1094 * memory_region_test_and_clear_dirty this allows to query the same
1095 * page multiple times, which is especially useful for display updates
1096 * where the scanlines often are not page aligned.
1098 * The dirty bitmap region which gets copyed into the snapshot (and
1099 * cleared afterwards) can be larger than requested. The boundaries
1100 * are rounded up/down so complete bitmap longs (covering 64 pages on
1101 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1102 * isn't a problem for display updates as the extra pages are outside
1103 * the visible area, and in case the visible area changes a full
1104 * display redraw is due anyway. Should other use cases for this
1105 * function emerge we might have to revisit this implementation
1108 * Use g_free to release DirtyBitmapSnapshot.
1110 * @mr: the memory region being queried.
1111 * @addr: the address (relative to the start of the region) being queried.
1112 * @size: the size of the range being queried.
1113 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1115 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
1121 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1122 * in the specified dirty bitmap snapshot.
1124 * @mr: the memory region being queried.
1125 * @snap: the dirty bitmap snapshot
1126 * @addr: the address (relative to the start of the region) being queried.
1127 * @size: the size of the range being queried.
1129 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
1130 DirtyBitmapSnapshot
*snap
,
1131 hwaddr addr
, hwaddr size
);
1134 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
1135 * any external TLBs (e.g. kvm)
1137 * Flushes dirty information from accelerators such as kvm and vhost-net
1138 * and makes it available to users of the memory API.
1140 * @mr: the region being flushed.
1142 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
1145 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1148 * Marks a range of pages as no longer dirty.
1150 * @mr: the region being updated.
1151 * @addr: the start of the subrange being cleaned.
1152 * @size: the size of the subrange being cleaned.
1153 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1154 * %DIRTY_MEMORY_VGA.
1156 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
1157 hwaddr size
, unsigned client
);
1160 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1162 * Allows a memory region to be marked as read-only (turning it into a ROM).
1163 * only useful on RAM regions.
1165 * @mr: the region being updated.
1166 * @readonly: whether rhe region is to be ROM or RAM.
1168 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
1171 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1173 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1174 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1175 * device is mapped to guest memory and satisfies read access directly.
1176 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1177 * Writes are always handled by the #MemoryRegion.write function.
1179 * @mr: the memory region to be updated
1180 * @romd_mode: %true to put the region into ROMD mode
1182 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
1185 * memory_region_set_coalescing: Enable memory coalescing for the region.
1187 * Enabled writes to a region to be queued for later processing. MMIO ->write
1188 * callbacks may be delayed until a non-coalesced MMIO is issued.
1189 * Only useful for IO regions. Roughly similar to write-combining hardware.
1191 * @mr: the memory region to be write coalesced
1193 void memory_region_set_coalescing(MemoryRegion
*mr
);
1196 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1199 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1200 * Multiple calls can be issued coalesced disjoint ranges.
1202 * @mr: the memory region to be updated.
1203 * @offset: the start of the range within the region to be coalesced.
1204 * @size: the size of the subrange to be coalesced.
1206 void memory_region_add_coalescing(MemoryRegion
*mr
,
1211 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1213 * Disables any coalescing caused by memory_region_set_coalescing() or
1214 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1217 * @mr: the memory region to be updated.
1219 void memory_region_clear_coalescing(MemoryRegion
*mr
);
1222 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1225 * Ensure that pending coalesced MMIO request are flushed before the memory
1226 * region is accessed. This property is automatically enabled for all regions
1227 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1229 * @mr: the memory region to be updated.
1231 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
1234 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1237 * Clear the automatic coalesced MMIO flushing enabled via
1238 * memory_region_set_flush_coalesced. Note that this service has no effect on
1239 * memory regions that have MMIO coalescing enabled for themselves. For them,
1240 * automatic flushing will stop once coalescing is disabled.
1242 * @mr: the memory region to be updated.
1244 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1247 * memory_region_clear_global_locking: Declares that access processing does
1248 * not depend on the QEMU global lock.
1250 * By clearing this property, accesses to the memory region will be processed
1251 * outside of QEMU's global lock (unless the lock is held on when issuing the
1252 * access request). In this case, the device model implementing the access
1253 * handlers is responsible for synchronization of concurrency.
1255 * @mr: the memory region to be updated.
1257 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1260 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1261 * is written to a location.
1263 * Marks a word in an IO region (initialized with memory_region_init_io())
1264 * as a trigger for an eventfd event. The I/O callback will not be called.
1265 * The caller must be prepared to handle failure (that is, take the required
1266 * action if the callback _is_ called).
1268 * @mr: the memory region being updated.
1269 * @addr: the address within @mr that is to be monitored
1270 * @size: the size of the access to trigger the eventfd
1271 * @match_data: whether to match against @data, instead of just @addr
1272 * @data: the data to match against the guest write
1273 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1275 void memory_region_add_eventfd(MemoryRegion
*mr
,
1283 * memory_region_del_eventfd: Cancel an eventfd.
1285 * Cancels an eventfd trigger requested by a previous
1286 * memory_region_add_eventfd() call.
1288 * @mr: the memory region being updated.
1289 * @addr: the address within @mr that is to be monitored
1290 * @size: the size of the access to trigger the eventfd
1291 * @match_data: whether to match against @data, instead of just @addr
1292 * @data: the data to match against the guest write
1293 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1295 void memory_region_del_eventfd(MemoryRegion
*mr
,
1303 * memory_region_add_subregion: Add a subregion to a container.
1305 * Adds a subregion at @offset. The subregion may not overlap with other
1306 * subregions (except for those explicitly marked as overlapping). A region
1307 * may only be added once as a subregion (unless removed with
1308 * memory_region_del_subregion()); use memory_region_init_alias() if you
1309 * want a region to be a subregion in multiple locations.
1311 * @mr: the region to contain the new subregion; must be a container
1312 * initialized with memory_region_init().
1313 * @offset: the offset relative to @mr where @subregion is added.
1314 * @subregion: the subregion to be added.
1316 void memory_region_add_subregion(MemoryRegion
*mr
,
1318 MemoryRegion
*subregion
);
1320 * memory_region_add_subregion_overlap: Add a subregion to a container
1323 * Adds a subregion at @offset. The subregion may overlap with other
1324 * subregions. Conflicts are resolved by having a higher @priority hide a
1325 * lower @priority. Subregions without priority are taken as @priority 0.
1326 * A region may only be added once as a subregion (unless removed with
1327 * memory_region_del_subregion()); use memory_region_init_alias() if you
1328 * want a region to be a subregion in multiple locations.
1330 * @mr: the region to contain the new subregion; must be a container
1331 * initialized with memory_region_init().
1332 * @offset: the offset relative to @mr where @subregion is added.
1333 * @subregion: the subregion to be added.
1334 * @priority: used for resolving overlaps; highest priority wins.
1336 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1338 MemoryRegion
*subregion
,
1342 * memory_region_get_ram_addr: Get the ram address associated with a memory
1345 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1347 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1349 * memory_region_del_subregion: Remove a subregion.
1351 * Removes a subregion from its container.
1353 * @mr: the container to be updated.
1354 * @subregion: the region being removed; must be a current subregion of @mr.
1356 void memory_region_del_subregion(MemoryRegion
*mr
,
1357 MemoryRegion
*subregion
);
1360 * memory_region_set_enabled: dynamically enable or disable a region
1362 * Enables or disables a memory region. A disabled memory region
1363 * ignores all accesses to itself and its subregions. It does not
1364 * obscure sibling subregions with lower priority - it simply behaves as
1365 * if it was removed from the hierarchy.
1367 * Regions default to being enabled.
1369 * @mr: the region to be updated
1370 * @enabled: whether to enable or disable the region
1372 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1375 * memory_region_set_address: dynamically update the address of a region
1377 * Dynamically updates the address of a region, relative to its container.
1378 * May be used on regions are currently part of a memory hierarchy.
1380 * @mr: the region to be updated
1381 * @addr: new address, relative to container region
1383 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1386 * memory_region_set_size: dynamically update the size of a region.
1388 * Dynamically updates the size of a region.
1390 * @mr: the region to be updated
1391 * @size: used size of the region.
1393 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1396 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1398 * Dynamically updates the offset into the target region that an alias points
1399 * to, as if the fourth argument to memory_region_init_alias() has changed.
1401 * @mr: the #MemoryRegion to be updated; should be an alias.
1402 * @offset: the new offset into the target memory region
1404 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1408 * memory_region_present: checks if an address relative to a @container
1409 * translates into #MemoryRegion within @container
1411 * Answer whether a #MemoryRegion within @container covers the address
1414 * @container: a #MemoryRegion within which @addr is a relative address
1415 * @addr: the area within @container to be searched
1417 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1420 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1421 * into any address space.
1423 * @mr: a #MemoryRegion which should be checked if it's mapped
1425 bool memory_region_is_mapped(MemoryRegion
*mr
);
1428 * memory_region_find: translate an address/size relative to a
1429 * MemoryRegion into a #MemoryRegionSection.
1431 * Locates the first #MemoryRegion within @mr that overlaps the range
1432 * given by @addr and @size.
1434 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1435 * It will have the following characteristics:
1436 * .@size = 0 iff no overlap was found
1437 * .@mr is non-%NULL iff an overlap was found
1439 * Remember that in the return value the @offset_within_region is
1440 * relative to the returned region (in the .@mr field), not to the
1443 * Similarly, the .@offset_within_address_space is relative to the
1444 * address space that contains both regions, the passed and the
1445 * returned one. However, in the special case where the @mr argument
1446 * has no container (and thus is the root of the address space), the
1447 * following will hold:
1448 * .@offset_within_address_space >= @addr
1449 * .@offset_within_address_space + .@size <= @addr + @size
1451 * @mr: a MemoryRegion within which @addr is a relative address
1452 * @addr: start of the area within @as to be searched
1453 * @size: size of the area to be searched
1455 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1456 hwaddr addr
, uint64_t size
);
1459 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1461 * Synchronizes the dirty page log for all address spaces.
1463 void memory_global_dirty_log_sync(void);
1466 * memory_region_transaction_begin: Start a transaction.
1468 * During a transaction, changes will be accumulated and made visible
1469 * only when the transaction ends (is committed).
1471 void memory_region_transaction_begin(void);
1474 * memory_region_transaction_commit: Commit a transaction and make changes
1475 * visible to the guest.
1477 void memory_region_transaction_commit(void);
1480 * memory_listener_register: register callbacks to be called when memory
1481 * sections are mapped or unmapped into an address
1484 * @listener: an object containing the callbacks to be called
1485 * @filter: if non-%NULL, only regions in this address space will be observed
1487 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1490 * memory_listener_unregister: undo the effect of memory_listener_register()
1492 * @listener: an object containing the callbacks to be removed
1494 void memory_listener_unregister(MemoryListener
*listener
);
1497 * memory_global_dirty_log_start: begin dirty logging for all regions
1499 void memory_global_dirty_log_start(void);
1502 * memory_global_dirty_log_stop: end dirty logging for all regions
1504 void memory_global_dirty_log_stop(void);
1506 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
,
1507 bool dispatch_tree
);
1510 * memory_region_request_mmio_ptr: request a pointer to an mmio
1511 * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer.
1512 * When the device wants to invalidate the pointer it will call
1513 * memory_region_invalidate_mmio_ptr.
1515 * @mr: #MemoryRegion to check
1516 * @addr: address within that region
1518 * Returns true on success, false otherwise.
1520 bool memory_region_request_mmio_ptr(MemoryRegion
*mr
, hwaddr addr
);
1523 * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio
1524 * previously requested.
1525 * In the end that means that if something wants to execute from this area it
1526 * will need to request the pointer again.
1528 * @mr: #MemoryRegion associated to the pointer.
1529 * @addr: address within that region
1530 * @size: size of that area.
1532 void memory_region_invalidate_mmio_ptr(MemoryRegion
*mr
, hwaddr offset
,
1536 * memory_region_dispatch_read: perform a read directly to the specified
1539 * @mr: #MemoryRegion to access
1540 * @addr: address within that region
1541 * @pval: pointer to uint64_t which the data is written to
1542 * @size: size of the access in bytes
1543 * @attrs: memory transaction attributes to use for the access
1545 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1551 * memory_region_dispatch_write: perform a write directly to the specified
1554 * @mr: #MemoryRegion to access
1555 * @addr: address within that region
1556 * @data: data to write
1557 * @size: size of the access in bytes
1558 * @attrs: memory transaction attributes to use for the access
1560 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1567 * address_space_init: initializes an address space
1569 * @as: an uninitialized #AddressSpace
1570 * @root: a #MemoryRegion that routes addresses for the address space
1571 * @name: an address space name. The name is only used for debugging
1574 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1577 * address_space_destroy: destroy an address space
1579 * Releases all resources associated with an address space. After an address space
1580 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1583 * @as: address space to be destroyed
1585 void address_space_destroy(AddressSpace
*as
);
1588 * address_space_rw: read from or write to an address space.
1590 * Return a MemTxResult indicating whether the operation succeeded
1591 * or failed (eg unassigned memory, device rejected the transaction,
1594 * @as: #AddressSpace to be accessed
1595 * @addr: address within that address space
1596 * @attrs: memory transaction attributes
1597 * @buf: buffer with the data transferred
1598 * @is_write: indicates the transfer direction
1600 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1601 MemTxAttrs attrs
, uint8_t *buf
,
1602 int len
, bool is_write
);
1605 * address_space_write: write to address space.
1607 * Return a MemTxResult indicating whether the operation succeeded
1608 * or failed (eg unassigned memory, device rejected the transaction,
1611 * @as: #AddressSpace to be accessed
1612 * @addr: address within that address space
1613 * @attrs: memory transaction attributes
1614 * @buf: buffer with the data transferred
1616 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1618 const uint8_t *buf
, int len
);
1620 /* address_space_ld*: load from an address space
1621 * address_space_st*: store to an address space
1623 * These functions perform a load or store of the byte, word,
1624 * longword or quad to the specified address within the AddressSpace.
1625 * The _le suffixed functions treat the data as little endian;
1626 * _be indicates big endian; no suffix indicates "same endianness
1629 * The "guest CPU endianness" accessors are deprecated for use outside
1630 * target-* code; devices should be CPU-agnostic and use either the LE
1631 * or the BE accessors.
1633 * @as #AddressSpace to be accessed
1634 * @addr: address within that address space
1635 * @val: data value, for stores
1636 * @attrs: memory transaction attributes
1637 * @result: location to write the success/failure of the transaction;
1638 * if NULL, this information is discarded
1640 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1641 MemTxAttrs attrs
, MemTxResult
*result
);
1642 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1643 MemTxAttrs attrs
, MemTxResult
*result
);
1644 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1645 MemTxAttrs attrs
, MemTxResult
*result
);
1646 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1647 MemTxAttrs attrs
, MemTxResult
*result
);
1648 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1649 MemTxAttrs attrs
, MemTxResult
*result
);
1650 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1651 MemTxAttrs attrs
, MemTxResult
*result
);
1652 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1653 MemTxAttrs attrs
, MemTxResult
*result
);
1654 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1655 MemTxAttrs attrs
, MemTxResult
*result
);
1656 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1657 MemTxAttrs attrs
, MemTxResult
*result
);
1658 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1659 MemTxAttrs attrs
, MemTxResult
*result
);
1660 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1661 MemTxAttrs attrs
, MemTxResult
*result
);
1662 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1663 MemTxAttrs attrs
, MemTxResult
*result
);
1664 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1665 MemTxAttrs attrs
, MemTxResult
*result
);
1666 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1667 MemTxAttrs attrs
, MemTxResult
*result
);
1669 uint32_t ldub_phys(AddressSpace
*as
, hwaddr addr
);
1670 uint32_t lduw_le_phys(AddressSpace
*as
, hwaddr addr
);
1671 uint32_t lduw_be_phys(AddressSpace
*as
, hwaddr addr
);
1672 uint32_t ldl_le_phys(AddressSpace
*as
, hwaddr addr
);
1673 uint32_t ldl_be_phys(AddressSpace
*as
, hwaddr addr
);
1674 uint64_t ldq_le_phys(AddressSpace
*as
, hwaddr addr
);
1675 uint64_t ldq_be_phys(AddressSpace
*as
, hwaddr addr
);
1676 void stb_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1677 void stw_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1678 void stw_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1679 void stl_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1680 void stl_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1681 void stq_le_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1682 void stq_be_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1684 struct MemoryRegionCache
{
1690 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .as = NULL })
1692 /* address_space_cache_init: prepare for repeated access to a physical
1695 * @cache: #MemoryRegionCache to be filled
1696 * @as: #AddressSpace to be accessed
1697 * @addr: address within that address space
1698 * @len: length of buffer
1699 * @is_write: indicates the transfer direction
1701 * Will only work with RAM, and may map a subset of the requested range by
1702 * returning a value that is less than @len. On failure, return a negative
1705 * Because it only works with RAM, this function can be used for
1706 * read-modify-write operations. In this case, is_write should be %true.
1708 * Note that addresses passed to the address_space_*_cached functions
1709 * are relative to @addr.
1711 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1718 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1720 * @cache: The #MemoryRegionCache to operate on.
1721 * @addr: The first physical address that was written, relative to the
1722 * address that was passed to @address_space_cache_init.
1723 * @access_len: The number of bytes that were written starting at @addr.
1725 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1730 * address_space_cache_destroy: free a #MemoryRegionCache
1732 * @cache: The #MemoryRegionCache whose memory should be released.
1734 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1736 /* address_space_ld*_cached: load from a cached #MemoryRegion
1737 * address_space_st*_cached: store into a cached #MemoryRegion
1739 * These functions perform a load or store of the byte, word,
1740 * longword or quad to the specified address. The address is
1741 * a physical address in the AddressSpace, but it must lie within
1742 * a #MemoryRegion that was mapped with address_space_cache_init.
1744 * The _le suffixed functions treat the data as little endian;
1745 * _be indicates big endian; no suffix indicates "same endianness
1748 * The "guest CPU endianness" accessors are deprecated for use outside
1749 * target-* code; devices should be CPU-agnostic and use either the LE
1750 * or the BE accessors.
1752 * @cache: previously initialized #MemoryRegionCache to be accessed
1753 * @addr: address within the address space
1754 * @val: data value, for stores
1755 * @attrs: memory transaction attributes
1756 * @result: location to write the success/failure of the transaction;
1757 * if NULL, this information is discarded
1759 uint32_t address_space_ldub_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1760 MemTxAttrs attrs
, MemTxResult
*result
);
1761 uint32_t address_space_lduw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1762 MemTxAttrs attrs
, MemTxResult
*result
);
1763 uint32_t address_space_lduw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1764 MemTxAttrs attrs
, MemTxResult
*result
);
1765 uint32_t address_space_ldl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1766 MemTxAttrs attrs
, MemTxResult
*result
);
1767 uint32_t address_space_ldl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1768 MemTxAttrs attrs
, MemTxResult
*result
);
1769 uint64_t address_space_ldq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1770 MemTxAttrs attrs
, MemTxResult
*result
);
1771 uint64_t address_space_ldq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1772 MemTxAttrs attrs
, MemTxResult
*result
);
1773 void address_space_stb_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1774 MemTxAttrs attrs
, MemTxResult
*result
);
1775 void address_space_stw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1776 MemTxAttrs attrs
, MemTxResult
*result
);
1777 void address_space_stw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1778 MemTxAttrs attrs
, MemTxResult
*result
);
1779 void address_space_stl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1780 MemTxAttrs attrs
, MemTxResult
*result
);
1781 void address_space_stl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1782 MemTxAttrs attrs
, MemTxResult
*result
);
1783 void address_space_stq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1784 MemTxAttrs attrs
, MemTxResult
*result
);
1785 void address_space_stq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1786 MemTxAttrs attrs
, MemTxResult
*result
);
1788 uint32_t ldub_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1789 uint32_t lduw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1790 uint32_t lduw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1791 uint32_t ldl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1792 uint32_t ldl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1793 uint64_t ldq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1794 uint64_t ldq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1795 void stb_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1796 void stw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1797 void stw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1798 void stl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1799 void stl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1800 void stq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1801 void stq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1802 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1803 * entry. Should be called from an RCU critical section.
1805 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1808 /* address_space_translate: translate an address range into an address space
1809 * into a MemoryRegion and an address range into that section. Should be
1810 * called from an RCU critical section, to avoid that the last reference
1811 * to the returned region disappears after address_space_translate returns.
1813 * @as: #AddressSpace to be accessed
1814 * @addr: address within that address space
1815 * @xlat: pointer to address within the returned memory region section's
1817 * @len: pointer to length
1818 * @is_write: indicates the transfer direction
1820 MemoryRegion
*flatview_translate(FlatView
*fv
,
1821 hwaddr addr
, hwaddr
*xlat
,
1822 hwaddr
*len
, bool is_write
);
1824 static inline MemoryRegion
*address_space_translate(AddressSpace
*as
,
1825 hwaddr addr
, hwaddr
*xlat
,
1826 hwaddr
*len
, bool is_write
)
1828 return flatview_translate(address_space_to_flatview(as
),
1829 addr
, xlat
, len
, is_write
);
1832 /* address_space_access_valid: check for validity of accessing an address
1835 * Check whether memory is assigned to the given address space range, and
1836 * access is permitted by any IOMMU regions that are active for the address
1839 * For now, addr and len should be aligned to a page size. This limitation
1840 * will be lifted in the future.
1842 * @as: #AddressSpace to be accessed
1843 * @addr: address within that address space
1844 * @len: length of the area to be checked
1845 * @is_write: indicates the transfer direction
1847 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1849 /* address_space_map: map a physical memory region into a host virtual address
1851 * May map a subset of the requested range, given by and returned in @plen.
1852 * May return %NULL if resources needed to perform the mapping are exhausted.
1853 * Use only for reads OR writes - not for read-modify-write operations.
1854 * Use cpu_register_map_client() to know when retrying the map operation is
1855 * likely to succeed.
1857 * @as: #AddressSpace to be accessed
1858 * @addr: address within that address space
1859 * @plen: pointer to length of buffer; updated on return
1860 * @is_write: indicates the transfer direction
1862 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1863 hwaddr
*plen
, bool is_write
);
1865 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1867 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1868 * the amount of memory that was actually read or written by the caller.
1870 * @as: #AddressSpace used
1871 * @addr: address within that address space
1872 * @len: buffer length as returned by address_space_map()
1873 * @access_len: amount of data actually transferred
1874 * @is_write: indicates the transfer direction
1876 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1877 int is_write
, hwaddr access_len
);
1880 /* Internal functions, part of the implementation of address_space_read. */
1881 MemTxResult
flatview_read_continue(FlatView
*fv
, hwaddr addr
,
1882 MemTxAttrs attrs
, uint8_t *buf
,
1883 int len
, hwaddr addr1
, hwaddr l
,
1886 MemTxResult
flatview_read_full(FlatView
*fv
, hwaddr addr
,
1887 MemTxAttrs attrs
, uint8_t *buf
, int len
);
1888 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
1890 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
1893 return memory_region_is_ram(mr
) &&
1894 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
1896 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
1897 memory_region_is_romd(mr
);
1902 * address_space_read: read from an address space.
1904 * Return a MemTxResult indicating whether the operation succeeded
1905 * or failed (eg unassigned memory, device rejected the transaction,
1908 * @as: #AddressSpace to be accessed
1909 * @addr: address within that address space
1910 * @attrs: memory transaction attributes
1911 * @buf: buffer with the data transferred
1913 static inline __attribute__((__always_inline__
))
1914 MemTxResult
flatview_read(FlatView
*fv
, hwaddr addr
, MemTxAttrs attrs
,
1915 uint8_t *buf
, int len
)
1917 MemTxResult result
= MEMTX_OK
;
1922 if (__builtin_constant_p(len
)) {
1926 mr
= flatview_translate(fv
, addr
, &addr1
, &l
, false);
1927 if (len
== l
&& memory_access_is_direct(mr
, false)) {
1928 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
1929 memcpy(buf
, ptr
, len
);
1931 result
= flatview_read_continue(fv
, addr
, attrs
, buf
, len
,
1937 result
= flatview_read_full(fv
, addr
, attrs
, buf
, len
);
1942 static inline MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
,
1943 MemTxAttrs attrs
, uint8_t *buf
,
1946 return flatview_read(address_space_to_flatview(as
), addr
, attrs
, buf
, len
);
1950 * address_space_read_cached: read from a cached RAM region
1952 * @cache: Cached region to be addressed
1953 * @addr: address relative to the base of the RAM region
1954 * @buf: buffer with the data transferred
1955 * @len: length of the data transferred
1958 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1961 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1962 address_space_read(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);
1966 * address_space_write_cached: write to a cached RAM region
1968 * @cache: Cached region to be addressed
1969 * @addr: address relative to the base of the RAM region
1970 * @buf: buffer with the data transferred
1971 * @len: length of the data transferred
1974 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1977 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1978 address_space_write(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);