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 #ifndef CONFIG_USER_ONLY
21 #include "exec/hwaddr.h"
23 #include "exec/memattrs.h"
24 #include "exec/ramlist.h"
25 #include "qemu/queue.h"
26 #include "qemu/int128.h"
27 #include "qemu/notify.h"
28 #include "qom/object.h"
31 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
33 #define MAX_PHYS_ADDR_SPACE_BITS 62
34 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
36 #define TYPE_MEMORY_REGION "qemu:memory-region"
37 #define MEMORY_REGION(obj) \
38 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
40 typedef struct MemoryRegionOps MemoryRegionOps
;
41 typedef struct MemoryRegionMmio MemoryRegionMmio
;
43 struct MemoryRegionMmio
{
44 CPUReadMemoryFunc
*read
[3];
45 CPUWriteMemoryFunc
*write
[3];
48 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
50 /* See address_space_translate: bit 0 is read, bit 1 is write. */
58 struct IOMMUTLBEntry
{
59 AddressSpace
*target_as
;
61 hwaddr translated_addr
;
62 hwaddr addr_mask
; /* 0xfff = 4k translation */
63 IOMMUAccessFlags perm
;
67 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
68 * register with one or multiple IOMMU Notifier capability bit(s).
71 IOMMU_NOTIFIER_NONE
= 0,
72 /* Notify cache invalidations */
73 IOMMU_NOTIFIER_UNMAP
= 0x1,
74 /* Notify entry changes (newly created entries) */
75 IOMMU_NOTIFIER_MAP
= 0x2,
78 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
81 typedef void (*IOMMUNotify
)(struct IOMMUNotifier
*notifier
,
84 struct IOMMUNotifier
{
86 IOMMUNotifierFlag notifier_flags
;
87 /* Notify for address space range start <= addr <= end */
90 QLIST_ENTRY(IOMMUNotifier
) node
;
92 typedef struct IOMMUNotifier IOMMUNotifier
;
94 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
95 IOMMUNotifierFlag flags
,
96 hwaddr start
, hwaddr end
)
99 n
->notifier_flags
= flags
;
104 /* New-style MMIO accessors can indicate that the transaction failed.
105 * A zero (MEMTX_OK) response means success; anything else is a failure
106 * of some kind. The memory subsystem will bitwise-OR together results
107 * if it is synthesizing an operation from multiple smaller accesses.
110 #define MEMTX_ERROR (1U << 0) /* device returned an error */
111 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
112 typedef uint32_t MemTxResult
;
115 * Memory region callbacks
117 struct MemoryRegionOps
{
118 /* Read from the memory region. @addr is relative to @mr; @size is
120 uint64_t (*read
)(void *opaque
,
123 /* Write to the memory region. @addr is relative to @mr; @size is
125 void (*write
)(void *opaque
,
130 MemTxResult (*read_with_attrs
)(void *opaque
,
135 MemTxResult (*write_with_attrs
)(void *opaque
,
141 enum device_endian endianness
;
142 /* Guest-visible constraints: */
144 /* If nonzero, specify bounds on access sizes beyond which a machine
147 unsigned min_access_size
;
148 unsigned max_access_size
;
149 /* If true, unaligned accesses are supported. Otherwise unaligned
150 * accesses throw machine checks.
154 * If present, and returns #false, the transaction is not accepted
155 * by the device (and results in machine dependent behaviour such
156 * as a machine check exception).
158 bool (*accepts
)(void *opaque
, hwaddr addr
,
159 unsigned size
, bool is_write
);
161 /* Internal implementation constraints: */
163 /* If nonzero, specifies the minimum size implemented. Smaller sizes
164 * will be rounded upwards and a partial result will be returned.
166 unsigned min_access_size
;
167 /* If nonzero, specifies the maximum size implemented. Larger sizes
168 * will be done as a series of accesses with smaller sizes.
170 unsigned max_access_size
;
171 /* If true, unaligned accesses are supported. Otherwise all accesses
172 * are converted to (possibly multiple) naturally aligned accesses.
177 /* If .read and .write are not present, old_mmio may be used for
178 * backwards compatibility with old mmio registration
180 const MemoryRegionMmio old_mmio
;
183 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps
;
185 struct MemoryRegionIOMMUOps
{
186 /* Return a TLB entry that contains a given address. */
187 IOMMUTLBEntry (*translate
)(MemoryRegion
*iommu
, hwaddr addr
, bool is_write
);
188 /* Returns minimum supported page size */
189 uint64_t (*get_min_page_size
)(MemoryRegion
*iommu
);
190 /* Called when IOMMU Notifier flag changed */
191 void (*notify_flag_changed
)(MemoryRegion
*iommu
,
192 IOMMUNotifierFlag old_flags
,
193 IOMMUNotifierFlag new_flags
);
196 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
197 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
199 struct MemoryRegion
{
202 /* All fields are private - violators will be prosecuted */
204 /* The following fields should fit in a cache line */
208 bool readonly
; /* For RAM regions */
210 bool flush_coalesced_mmio
;
212 uint8_t dirty_log_mask
;
215 const MemoryRegionIOMMUOps
*iommu_ops
;
217 const MemoryRegionOps
*ops
;
219 MemoryRegion
*container
;
222 void (*destructor
)(MemoryRegion
*mr
);
227 bool warning_printed
; /* For reservations */
228 uint8_t vga_logging_count
;
232 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
233 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
234 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
236 unsigned ioeventfd_nb
;
237 MemoryRegionIoeventfd
*ioeventfds
;
238 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
239 IOMMUNotifierFlag iommu_notify_flags
;
243 * MemoryListener: callbacks structure for updates to the physical memory map
245 * Allows a component to adjust to changes in the guest-visible memory map.
246 * Use with memory_listener_register() and memory_listener_unregister().
248 struct MemoryListener
{
249 void (*begin
)(MemoryListener
*listener
);
250 void (*commit
)(MemoryListener
*listener
);
251 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
252 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
253 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
254 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
256 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
258 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
259 void (*log_global_start
)(MemoryListener
*listener
);
260 void (*log_global_stop
)(MemoryListener
*listener
);
261 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
262 bool match_data
, uint64_t data
, EventNotifier
*e
);
263 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
264 bool match_data
, uint64_t data
, EventNotifier
*e
);
265 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
266 hwaddr addr
, hwaddr len
);
267 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
268 hwaddr addr
, hwaddr len
);
269 /* Lower = earlier (during add), later (during del) */
271 AddressSpace
*address_space
;
272 QTAILQ_ENTRY(MemoryListener
) link
;
273 QTAILQ_ENTRY(MemoryListener
) link_as
;
277 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
279 struct AddressSpace
{
280 /* All fields are private. */
287 /* Accessed via RCU. */
288 struct FlatView
*current_map
;
291 struct MemoryRegionIoeventfd
*ioeventfds
;
292 struct AddressSpaceDispatch
*dispatch
;
293 struct AddressSpaceDispatch
*next_dispatch
;
294 MemoryListener dispatch_listener
;
295 QTAILQ_HEAD(memory_listeners_as
, MemoryListener
) listeners
;
296 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
300 * MemoryRegionSection: describes a fragment of a #MemoryRegion
302 * @mr: the region, or %NULL if empty
303 * @address_space: the address space the region is mapped in
304 * @offset_within_region: the beginning of the section, relative to @mr's start
305 * @size: the size of the section; will not exceed @mr's boundaries
306 * @offset_within_address_space: the address of the first byte of the section
307 * relative to the region's address space
308 * @readonly: writes to this section are ignored
310 struct MemoryRegionSection
{
312 AddressSpace
*address_space
;
313 hwaddr offset_within_region
;
315 hwaddr offset_within_address_space
;
320 * memory_region_init: Initialize a memory region
322 * The region typically acts as a container for other memory regions. Use
323 * memory_region_add_subregion() to add subregions.
325 * @mr: the #MemoryRegion to be initialized
326 * @owner: the object that tracks the region's reference count
327 * @name: used for debugging; not visible to the user or ABI
328 * @size: size of the region; any subregions beyond this size will be clipped
330 void memory_region_init(MemoryRegion
*mr
,
331 struct Object
*owner
,
336 * memory_region_ref: Add 1 to a memory region's reference count
338 * Whenever memory regions are accessed outside the BQL, they need to be
339 * preserved against hot-unplug. MemoryRegions actually do not have their
340 * own reference count; they piggyback on a QOM object, their "owner".
341 * This function adds a reference to the owner.
343 * All MemoryRegions must have an owner if they can disappear, even if the
344 * device they belong to operates exclusively under the BQL. This is because
345 * the region could be returned at any time by memory_region_find, and this
346 * is usually under guest control.
348 * @mr: the #MemoryRegion
350 void memory_region_ref(MemoryRegion
*mr
);
353 * memory_region_unref: Remove 1 to a memory region's reference count
355 * Whenever memory regions are accessed outside the BQL, they need to be
356 * preserved against hot-unplug. MemoryRegions actually do not have their
357 * own reference count; they piggyback on a QOM object, their "owner".
358 * This function removes a reference to the owner and possibly destroys it.
360 * @mr: the #MemoryRegion
362 void memory_region_unref(MemoryRegion
*mr
);
365 * memory_region_init_io: Initialize an I/O memory region.
367 * Accesses into the region will cause the callbacks in @ops to be called.
368 * if @size is nonzero, subregions will be clipped to @size.
370 * @mr: the #MemoryRegion to be initialized.
371 * @owner: the object that tracks the region's reference count
372 * @ops: a structure containing read and write callbacks to be used when
373 * I/O is performed on the region.
374 * @opaque: passed to the read and write callbacks of the @ops structure.
375 * @name: used for debugging; not visible to the user or ABI
376 * @size: size of the region.
378 void memory_region_init_io(MemoryRegion
*mr
,
379 struct Object
*owner
,
380 const MemoryRegionOps
*ops
,
386 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
387 * region will modify memory directly.
389 * @mr: the #MemoryRegion to be initialized.
390 * @owner: the object that tracks the region's reference count
391 * @name: Region name, becomes part of RAMBlock name used in migration stream
392 * must be unique within any device
393 * @size: size of the region.
394 * @errp: pointer to Error*, to store an error if it happens.
396 void memory_region_init_ram(MemoryRegion
*mr
,
397 struct Object
*owner
,
403 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
404 * RAM. Accesses into the region will
405 * modify memory directly. Only an initial
406 * portion of this RAM is actually used.
407 * The used size can change across reboots.
409 * @mr: the #MemoryRegion to be initialized.
410 * @owner: the object that tracks the region's reference count
411 * @name: Region name, becomes part of RAMBlock name used in migration stream
412 * must be unique within any device
413 * @size: used size of the region.
414 * @max_size: max size of the region.
415 * @resized: callback to notify owner about used size change.
416 * @errp: pointer to Error*, to store an error if it happens.
418 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
419 struct Object
*owner
,
423 void (*resized
)(const char*,
429 * memory_region_init_ram_from_file: Initialize RAM memory region with a
432 * @mr: the #MemoryRegion to be initialized.
433 * @owner: the object that tracks the region's reference count
434 * @name: Region name, becomes part of RAMBlock name used in migration stream
435 * must be unique within any device
436 * @size: size of the region.
437 * @share: %true if memory must be mmaped with the MAP_SHARED flag
438 * @path: the path in which to allocate the RAM.
439 * @errp: pointer to Error*, to store an error if it happens.
441 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
442 struct Object
*owner
,
451 * memory_region_init_ram_ptr: Initialize RAM memory region from a
452 * user-provided pointer. Accesses into the
453 * region will modify memory directly.
455 * @mr: the #MemoryRegion to be initialized.
456 * @owner: the object that tracks the region's reference count
457 * @name: Region name, becomes part of RAMBlock name used in migration stream
458 * must be unique within any device
459 * @size: size of the region.
460 * @ptr: memory to be mapped; must contain at least @size bytes.
462 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
463 struct Object
*owner
,
469 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
470 * a user-provided pointer.
472 * A RAM device represents a mapping to a physical device, such as to a PCI
473 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
474 * into the VM address space and access to the region will modify memory
475 * directly. However, the memory region should not be included in a memory
476 * dump (device may not be enabled/mapped at the time of the dump), and
477 * operations incompatible with manipulating MMIO should be avoided. Replaces
480 * @mr: the #MemoryRegion to be initialized.
481 * @owner: the object that tracks the region's reference count
482 * @name: the name of the region.
483 * @size: size of the region.
484 * @ptr: memory to be mapped; must contain at least @size bytes.
486 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
487 struct Object
*owner
,
493 * memory_region_init_alias: Initialize a memory region that aliases all or a
494 * part of another memory region.
496 * @mr: the #MemoryRegion to be initialized.
497 * @owner: the object that tracks the region's reference count
498 * @name: used for debugging; not visible to the user or ABI
499 * @orig: the region to be referenced; @mr will be equivalent to
500 * @orig between @offset and @offset + @size - 1.
501 * @offset: start of the section in @orig to be referenced.
502 * @size: size of the region.
504 void memory_region_init_alias(MemoryRegion
*mr
,
505 struct Object
*owner
,
512 * memory_region_init_rom: Initialize a ROM memory region.
514 * This has the same effect as calling memory_region_init_ram()
515 * and then marking the resulting region read-only with
516 * memory_region_set_readonly().
518 * @mr: the #MemoryRegion to be initialized.
519 * @owner: the object that tracks the region's reference count
520 * @name: Region name, becomes part of RAMBlock name used in migration stream
521 * must be unique within any device
522 * @size: size of the region.
523 * @errp: pointer to Error*, to store an error if it happens.
525 void memory_region_init_rom(MemoryRegion
*mr
,
526 struct Object
*owner
,
532 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
533 * handled via callbacks.
535 * @mr: the #MemoryRegion to be initialized.
536 * @owner: the object that tracks the region's reference count
537 * @ops: callbacks for write access handling (must not be NULL).
538 * @name: Region name, becomes part of RAMBlock name used in migration stream
539 * must be unique within any device
540 * @size: size of the region.
541 * @errp: pointer to Error*, to store an error if it happens.
543 void memory_region_init_rom_device(MemoryRegion
*mr
,
544 struct Object
*owner
,
545 const MemoryRegionOps
*ops
,
552 * memory_region_init_reservation: Initialize a memory region that reserves
555 * A reservation region primariy serves debugging purposes. It claims I/O
556 * space that is not supposed to be handled by QEMU itself. Any access via
557 * the memory API will cause an abort().
558 * This function is deprecated. Use memory_region_init_io() with NULL
561 * @mr: the #MemoryRegion to be initialized
562 * @owner: the object that tracks the region's reference count
563 * @name: used for debugging; not visible to the user or ABI
564 * @size: size of the region.
566 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
571 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
575 * memory_region_init_iommu: Initialize a memory region that translates
578 * An IOMMU region translates addresses and forwards accesses to a target
581 * @mr: the #MemoryRegion to be initialized
582 * @owner: the object that tracks the region's reference count
583 * @ops: a function that translates addresses into the @target region
584 * @name: used for debugging; not visible to the user or ABI
585 * @size: size of the region.
587 void memory_region_init_iommu(MemoryRegion
*mr
,
588 struct Object
*owner
,
589 const MemoryRegionIOMMUOps
*ops
,
594 * memory_region_owner: get a memory region's owner.
596 * @mr: the memory region being queried.
598 struct Object
*memory_region_owner(MemoryRegion
*mr
);
601 * memory_region_size: get a memory region's size.
603 * @mr: the memory region being queried.
605 uint64_t memory_region_size(MemoryRegion
*mr
);
608 * memory_region_is_ram: check whether a memory region is random access
610 * Returns %true is a memory region is random access.
612 * @mr: the memory region being queried
614 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
620 * memory_region_is_ram_device: check whether a memory region is a ram device
622 * Returns %true is a memory region is a device backed ram region
624 * @mr: the memory region being queried
626 bool memory_region_is_ram_device(MemoryRegion
*mr
);
629 * memory_region_is_romd: check whether a memory region is in ROMD mode
631 * Returns %true if a memory region is a ROM device and currently set to allow
634 * @mr: the memory region being queried
636 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
638 return mr
->rom_device
&& mr
->romd_mode
;
642 * memory_region_is_iommu: check whether a memory region is an iommu
644 * Returns %true is a memory region is an iommu.
646 * @mr: the memory region being queried
648 static inline bool memory_region_is_iommu(MemoryRegion
*mr
)
651 return memory_region_is_iommu(mr
->alias
);
653 return mr
->iommu_ops
;
658 * memory_region_iommu_get_min_page_size: get minimum supported page size
661 * Returns minimum supported page size for an iommu.
663 * @mr: the memory region being queried
665 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion
*mr
);
668 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
670 * The notification type will be decided by entry.perm bits:
672 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
673 * - For MAP (newly added entry) notifies: set entry.perm to the
674 * permission of the page (which is definitely !IOMMU_NONE).
676 * Note: for any IOMMU implementation, an in-place mapping change
677 * should be notified with an UNMAP followed by a MAP.
679 * @mr: the memory region that was changed
680 * @entry: the new entry in the IOMMU translation table. The entry
681 * replaces all old entries for the same virtual I/O address range.
682 * Deleted entries have .@perm == 0.
684 void memory_region_notify_iommu(MemoryRegion
*mr
,
685 IOMMUTLBEntry entry
);
688 * memory_region_register_iommu_notifier: register a notifier for changes to
689 * IOMMU translation entries.
691 * @mr: the memory region to observe
692 * @n: the IOMMUNotifier to be added; the notify callback receives a
693 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
694 * ceases to be valid on exit from the notifier.
696 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
700 * memory_region_iommu_replay: replay existing IOMMU translations to
701 * a notifier with the minimum page granularity returned by
702 * mr->iommu_ops->get_page_size().
704 * @mr: the memory region to observe
705 * @n: the notifier to which to replay iommu mappings
706 * @is_write: Whether to treat the replay as a translate "write"
709 void memory_region_iommu_replay(MemoryRegion
*mr
, IOMMUNotifier
*n
,
713 * memory_region_unregister_iommu_notifier: unregister a notifier for
714 * changes to IOMMU translation entries.
716 * @mr: the memory region which was observed and for which notity_stopped()
718 * @n: the notifier to be removed.
720 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
724 * memory_region_name: get a memory region's name
726 * Returns the string that was used to initialize the memory region.
728 * @mr: the memory region being queried
730 const char *memory_region_name(const MemoryRegion
*mr
);
733 * memory_region_is_logging: return whether a memory region is logging writes
735 * Returns %true if the memory region is logging writes for the given client
737 * @mr: the memory region being queried
738 * @client: the client being queried
740 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
743 * memory_region_get_dirty_log_mask: return the clients for which a
744 * memory region is logging writes.
746 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
747 * are the bit indices.
749 * @mr: the memory region being queried
751 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
754 * memory_region_is_rom: check whether a memory region is ROM
756 * Returns %true is a memory region is read-only memory.
758 * @mr: the memory region being queried
760 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
762 return mr
->ram
&& mr
->readonly
;
767 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
769 * Returns a file descriptor backing a file-based RAM memory region,
770 * or -1 if the region is not a file-based RAM memory region.
772 * @mr: the RAM or alias memory region being queried.
774 int memory_region_get_fd(MemoryRegion
*mr
);
777 * memory_region_set_fd: Mark a RAM memory region as backed by a
780 * This function is typically used after memory_region_init_ram_ptr().
782 * @mr: the memory region being queried.
783 * @fd: the file descriptor that backs @mr.
785 void memory_region_set_fd(MemoryRegion
*mr
, int fd
);
788 * memory_region_from_host: Convert a pointer into a RAM memory region
789 * and an offset within it.
791 * Given a host pointer inside a RAM memory region (created with
792 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
793 * the MemoryRegion and the offset within it.
795 * Use with care; by the time this function returns, the returned pointer is
796 * not protected by RCU anymore. If the caller is not within an RCU critical
797 * section and does not hold the iothread lock, it must have other means of
798 * protecting the pointer, such as a reference to the region that includes
799 * the incoming ram_addr_t.
801 * @mr: the memory region being queried.
803 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
806 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
808 * Returns a host pointer to a RAM memory region (created with
809 * memory_region_init_ram() or memory_region_init_ram_ptr()).
811 * Use with care; by the time this function returns, the returned pointer is
812 * not protected by RCU anymore. If the caller is not within an RCU critical
813 * section and does not hold the iothread lock, it must have other means of
814 * protecting the pointer, such as a reference to the region that includes
815 * the incoming ram_addr_t.
817 * @mr: the memory region being queried.
819 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
821 /* memory_region_ram_resize: Resize a RAM region.
823 * Only legal before guest might have detected the memory size: e.g. on
824 * incoming migration, or right after reset.
826 * @mr: a memory region created with @memory_region_init_resizeable_ram.
827 * @newsize: the new size the region
828 * @errp: pointer to Error*, to store an error if it happens.
830 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
834 * memory_region_set_log: Turn dirty logging on or off for a region.
836 * Turns dirty logging on or off for a specified client (display, migration).
837 * Only meaningful for RAM regions.
839 * @mr: the memory region being updated.
840 * @log: whether dirty logging is to be enabled or disabled.
841 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
843 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
846 * memory_region_get_dirty: Check whether a range of bytes is dirty
847 * for a specified client.
849 * Checks whether a range of bytes has been written to since the last
850 * call to memory_region_reset_dirty() with the same @client. Dirty logging
853 * @mr: the memory region being queried.
854 * @addr: the address (relative to the start of the region) being queried.
855 * @size: the size of the range being queried.
856 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
859 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
860 hwaddr size
, unsigned client
);
863 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
865 * Marks a range of bytes as dirty, after it has been dirtied outside
868 * @mr: the memory region being dirtied.
869 * @addr: the address (relative to the start of the region) being dirtied.
870 * @size: size of the range being dirtied.
872 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
876 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
877 * for a specified client. It clears them.
879 * Checks whether a range of bytes has been written to since the last
880 * call to memory_region_reset_dirty() with the same @client. Dirty logging
883 * @mr: the memory region being queried.
884 * @addr: the address (relative to the start of the region) being queried.
885 * @size: the size of the range being queried.
886 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
889 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
890 hwaddr size
, unsigned client
);
892 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
893 * any external TLBs (e.g. kvm)
895 * Flushes dirty information from accelerators such as kvm and vhost-net
896 * and makes it available to users of the memory API.
898 * @mr: the region being flushed.
900 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
903 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
906 * Marks a range of pages as no longer dirty.
908 * @mr: the region being updated.
909 * @addr: the start of the subrange being cleaned.
910 * @size: the size of the subrange being cleaned.
911 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
914 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
915 hwaddr size
, unsigned client
);
918 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
920 * Allows a memory region to be marked as read-only (turning it into a ROM).
921 * only useful on RAM regions.
923 * @mr: the region being updated.
924 * @readonly: whether rhe region is to be ROM or RAM.
926 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
929 * memory_region_rom_device_set_romd: enable/disable ROMD mode
931 * Allows a ROM device (initialized with memory_region_init_rom_device() to
932 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
933 * device is mapped to guest memory and satisfies read access directly.
934 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
935 * Writes are always handled by the #MemoryRegion.write function.
937 * @mr: the memory region to be updated
938 * @romd_mode: %true to put the region into ROMD mode
940 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
943 * memory_region_set_coalescing: Enable memory coalescing for the region.
945 * Enabled writes to a region to be queued for later processing. MMIO ->write
946 * callbacks may be delayed until a non-coalesced MMIO is issued.
947 * Only useful for IO regions. Roughly similar to write-combining hardware.
949 * @mr: the memory region to be write coalesced
951 void memory_region_set_coalescing(MemoryRegion
*mr
);
954 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
957 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
958 * Multiple calls can be issued coalesced disjoint ranges.
960 * @mr: the memory region to be updated.
961 * @offset: the start of the range within the region to be coalesced.
962 * @size: the size of the subrange to be coalesced.
964 void memory_region_add_coalescing(MemoryRegion
*mr
,
969 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
971 * Disables any coalescing caused by memory_region_set_coalescing() or
972 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
975 * @mr: the memory region to be updated.
977 void memory_region_clear_coalescing(MemoryRegion
*mr
);
980 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
983 * Ensure that pending coalesced MMIO request are flushed before the memory
984 * region is accessed. This property is automatically enabled for all regions
985 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
987 * @mr: the memory region to be updated.
989 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
992 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
995 * Clear the automatic coalesced MMIO flushing enabled via
996 * memory_region_set_flush_coalesced. Note that this service has no effect on
997 * memory regions that have MMIO coalescing enabled for themselves. For them,
998 * automatic flushing will stop once coalescing is disabled.
1000 * @mr: the memory region to be updated.
1002 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1005 * memory_region_set_global_locking: Declares the access processing requires
1006 * QEMU's global lock.
1008 * When this is invoked, accesses to the memory region will be processed while
1009 * holding the global lock of QEMU. This is the default behavior of memory
1012 * @mr: the memory region to be updated.
1014 void memory_region_set_global_locking(MemoryRegion
*mr
);
1017 * memory_region_clear_global_locking: Declares that access processing does
1018 * not depend on the QEMU global lock.
1020 * By clearing this property, accesses to the memory region will be processed
1021 * outside of QEMU's global lock (unless the lock is held on when issuing the
1022 * access request). In this case, the device model implementing the access
1023 * handlers is responsible for synchronization of concurrency.
1025 * @mr: the memory region to be updated.
1027 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1030 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1031 * is written to a location.
1033 * Marks a word in an IO region (initialized with memory_region_init_io())
1034 * as a trigger for an eventfd event. The I/O callback will not be called.
1035 * The caller must be prepared to handle failure (that is, take the required
1036 * action if the callback _is_ called).
1038 * @mr: the memory region being updated.
1039 * @addr: the address within @mr that is to be monitored
1040 * @size: the size of the access to trigger the eventfd
1041 * @match_data: whether to match against @data, instead of just @addr
1042 * @data: the data to match against the guest write
1043 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1045 void memory_region_add_eventfd(MemoryRegion
*mr
,
1053 * memory_region_del_eventfd: Cancel an eventfd.
1055 * Cancels an eventfd trigger requested by a previous
1056 * memory_region_add_eventfd() call.
1058 * @mr: the memory region being updated.
1059 * @addr: the address within @mr that is to be monitored
1060 * @size: the size of the access to trigger the eventfd
1061 * @match_data: whether to match against @data, instead of just @addr
1062 * @data: the data to match against the guest write
1063 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1065 void memory_region_del_eventfd(MemoryRegion
*mr
,
1073 * memory_region_add_subregion: Add a subregion to a container.
1075 * Adds a subregion at @offset. The subregion may not overlap with other
1076 * subregions (except for those explicitly marked as overlapping). A region
1077 * may only be added once as a subregion (unless removed with
1078 * memory_region_del_subregion()); use memory_region_init_alias() if you
1079 * want a region to be a subregion in multiple locations.
1081 * @mr: the region to contain the new subregion; must be a container
1082 * initialized with memory_region_init().
1083 * @offset: the offset relative to @mr where @subregion is added.
1084 * @subregion: the subregion to be added.
1086 void memory_region_add_subregion(MemoryRegion
*mr
,
1088 MemoryRegion
*subregion
);
1090 * memory_region_add_subregion_overlap: Add a subregion to a container
1093 * Adds a subregion at @offset. The subregion may overlap with other
1094 * subregions. Conflicts are resolved by having a higher @priority hide a
1095 * lower @priority. Subregions without priority are taken as @priority 0.
1096 * A region may only be added once as a subregion (unless removed with
1097 * memory_region_del_subregion()); use memory_region_init_alias() if you
1098 * want a region to be a subregion in multiple locations.
1100 * @mr: the region to contain the new subregion; must be a container
1101 * initialized with memory_region_init().
1102 * @offset: the offset relative to @mr where @subregion is added.
1103 * @subregion: the subregion to be added.
1104 * @priority: used for resolving overlaps; highest priority wins.
1106 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1108 MemoryRegion
*subregion
,
1112 * memory_region_get_ram_addr: Get the ram address associated with a memory
1115 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1117 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1119 * memory_region_del_subregion: Remove a subregion.
1121 * Removes a subregion from its container.
1123 * @mr: the container to be updated.
1124 * @subregion: the region being removed; must be a current subregion of @mr.
1126 void memory_region_del_subregion(MemoryRegion
*mr
,
1127 MemoryRegion
*subregion
);
1130 * memory_region_set_enabled: dynamically enable or disable a region
1132 * Enables or disables a memory region. A disabled memory region
1133 * ignores all accesses to itself and its subregions. It does not
1134 * obscure sibling subregions with lower priority - it simply behaves as
1135 * if it was removed from the hierarchy.
1137 * Regions default to being enabled.
1139 * @mr: the region to be updated
1140 * @enabled: whether to enable or disable the region
1142 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1145 * memory_region_set_address: dynamically update the address of a region
1147 * Dynamically updates the address of a region, relative to its container.
1148 * May be used on regions are currently part of a memory hierarchy.
1150 * @mr: the region to be updated
1151 * @addr: new address, relative to container region
1153 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1156 * memory_region_set_size: dynamically update the size of a region.
1158 * Dynamically updates the size of a region.
1160 * @mr: the region to be updated
1161 * @size: used size of the region.
1163 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1166 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1168 * Dynamically updates the offset into the target region that an alias points
1169 * to, as if the fourth argument to memory_region_init_alias() has changed.
1171 * @mr: the #MemoryRegion to be updated; should be an alias.
1172 * @offset: the new offset into the target memory region
1174 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1178 * memory_region_present: checks if an address relative to a @container
1179 * translates into #MemoryRegion within @container
1181 * Answer whether a #MemoryRegion within @container covers the address
1184 * @container: a #MemoryRegion within which @addr is a relative address
1185 * @addr: the area within @container to be searched
1187 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1190 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1191 * into any address space.
1193 * @mr: a #MemoryRegion which should be checked if it's mapped
1195 bool memory_region_is_mapped(MemoryRegion
*mr
);
1198 * memory_region_find: translate an address/size relative to a
1199 * MemoryRegion into a #MemoryRegionSection.
1201 * Locates the first #MemoryRegion within @mr that overlaps the range
1202 * given by @addr and @size.
1204 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1205 * It will have the following characteristics:
1206 * .@size = 0 iff no overlap was found
1207 * .@mr is non-%NULL iff an overlap was found
1209 * Remember that in the return value the @offset_within_region is
1210 * relative to the returned region (in the .@mr field), not to the
1213 * Similarly, the .@offset_within_address_space is relative to the
1214 * address space that contains both regions, the passed and the
1215 * returned one. However, in the special case where the @mr argument
1216 * has no container (and thus is the root of the address space), the
1217 * following will hold:
1218 * .@offset_within_address_space >= @addr
1219 * .@offset_within_address_space + .@size <= @addr + @size
1221 * @mr: a MemoryRegion within which @addr is a relative address
1222 * @addr: start of the area within @as to be searched
1223 * @size: size of the area to be searched
1225 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1226 hwaddr addr
, uint64_t size
);
1229 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1231 * Synchronizes the dirty page log for all address spaces.
1233 void memory_global_dirty_log_sync(void);
1236 * memory_region_transaction_begin: Start a transaction.
1238 * During a transaction, changes will be accumulated and made visible
1239 * only when the transaction ends (is committed).
1241 void memory_region_transaction_begin(void);
1244 * memory_region_transaction_commit: Commit a transaction and make changes
1245 * visible to the guest.
1247 void memory_region_transaction_commit(void);
1250 * memory_listener_register: register callbacks to be called when memory
1251 * sections are mapped or unmapped into an address
1254 * @listener: an object containing the callbacks to be called
1255 * @filter: if non-%NULL, only regions in this address space will be observed
1257 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1260 * memory_listener_unregister: undo the effect of memory_listener_register()
1262 * @listener: an object containing the callbacks to be removed
1264 void memory_listener_unregister(MemoryListener
*listener
);
1267 * memory_global_dirty_log_start: begin dirty logging for all regions
1269 void memory_global_dirty_log_start(void);
1272 * memory_global_dirty_log_stop: end dirty logging for all regions
1274 void memory_global_dirty_log_stop(void);
1276 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
);
1279 * memory_region_dispatch_read: perform a read directly to the specified
1282 * @mr: #MemoryRegion to access
1283 * @addr: address within that region
1284 * @pval: pointer to uint64_t which the data is written to
1285 * @size: size of the access in bytes
1286 * @attrs: memory transaction attributes to use for the access
1288 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1294 * memory_region_dispatch_write: perform a write directly to the specified
1297 * @mr: #MemoryRegion to access
1298 * @addr: address within that region
1299 * @data: data to write
1300 * @size: size of the access in bytes
1301 * @attrs: memory transaction attributes to use for the access
1303 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1310 * address_space_init: initializes an address space
1312 * @as: an uninitialized #AddressSpace
1313 * @root: a #MemoryRegion that routes addresses for the address space
1314 * @name: an address space name. The name is only used for debugging
1317 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1320 * address_space_init_shareable: return an address space for a memory region,
1321 * creating it if it does not already exist
1323 * @root: a #MemoryRegion that routes addresses for the address space
1324 * @name: an address space name. The name is only used for debugging
1327 * This function will return a pointer to an existing AddressSpace
1328 * which was initialized with the specified MemoryRegion, or it will
1329 * create and initialize one if it does not already exist. The ASes
1330 * are reference-counted, so the memory will be freed automatically
1331 * when the AddressSpace is destroyed via address_space_destroy.
1333 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
,
1337 * address_space_destroy: destroy an address space
1339 * Releases all resources associated with an address space. After an address space
1340 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1343 * @as: address space to be destroyed
1345 void address_space_destroy(AddressSpace
*as
);
1348 * address_space_rw: read from or write to an address space.
1350 * Return a MemTxResult indicating whether the operation succeeded
1351 * or failed (eg unassigned memory, device rejected the transaction,
1354 * @as: #AddressSpace to be accessed
1355 * @addr: address within that address space
1356 * @attrs: memory transaction attributes
1357 * @buf: buffer with the data transferred
1358 * @is_write: indicates the transfer direction
1360 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1361 MemTxAttrs attrs
, uint8_t *buf
,
1362 int len
, bool is_write
);
1365 * address_space_write: write to address space.
1367 * Return a MemTxResult indicating whether the operation succeeded
1368 * or failed (eg unassigned memory, device rejected the transaction,
1371 * @as: #AddressSpace to be accessed
1372 * @addr: address within that address space
1373 * @attrs: memory transaction attributes
1374 * @buf: buffer with the data transferred
1376 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1378 const uint8_t *buf
, int len
);
1380 /* address_space_ld*: load from an address space
1381 * address_space_st*: store to an address space
1383 * These functions perform a load or store of the byte, word,
1384 * longword or quad to the specified address within the AddressSpace.
1385 * The _le suffixed functions treat the data as little endian;
1386 * _be indicates big endian; no suffix indicates "same endianness
1389 * The "guest CPU endianness" accessors are deprecated for use outside
1390 * target-* code; devices should be CPU-agnostic and use either the LE
1391 * or the BE accessors.
1393 * @as #AddressSpace to be accessed
1394 * @addr: address within that address space
1395 * @val: data value, for stores
1396 * @attrs: memory transaction attributes
1397 * @result: location to write the success/failure of the transaction;
1398 * if NULL, this information is discarded
1400 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1401 MemTxAttrs attrs
, MemTxResult
*result
);
1402 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1403 MemTxAttrs attrs
, MemTxResult
*result
);
1404 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1405 MemTxAttrs attrs
, MemTxResult
*result
);
1406 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1407 MemTxAttrs attrs
, MemTxResult
*result
);
1408 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1409 MemTxAttrs attrs
, MemTxResult
*result
);
1410 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1411 MemTxAttrs attrs
, MemTxResult
*result
);
1412 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1413 MemTxAttrs attrs
, MemTxResult
*result
);
1414 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1415 MemTxAttrs attrs
, MemTxResult
*result
);
1416 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1417 MemTxAttrs attrs
, MemTxResult
*result
);
1418 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1419 MemTxAttrs attrs
, MemTxResult
*result
);
1420 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1421 MemTxAttrs attrs
, MemTxResult
*result
);
1422 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1423 MemTxAttrs attrs
, MemTxResult
*result
);
1424 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1425 MemTxAttrs attrs
, MemTxResult
*result
);
1426 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1427 MemTxAttrs attrs
, MemTxResult
*result
);
1429 uint32_t ldub_phys(AddressSpace
*as
, hwaddr addr
);
1430 uint32_t lduw_le_phys(AddressSpace
*as
, hwaddr addr
);
1431 uint32_t lduw_be_phys(AddressSpace
*as
, hwaddr addr
);
1432 uint32_t ldl_le_phys(AddressSpace
*as
, hwaddr addr
);
1433 uint32_t ldl_be_phys(AddressSpace
*as
, hwaddr addr
);
1434 uint64_t ldq_le_phys(AddressSpace
*as
, hwaddr addr
);
1435 uint64_t ldq_be_phys(AddressSpace
*as
, hwaddr addr
);
1436 void stb_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1437 void stw_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1438 void stw_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1439 void stl_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1440 void stl_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1441 void stq_le_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1442 void stq_be_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1444 struct MemoryRegionCache
{
1450 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .as = NULL })
1452 /* address_space_cache_init: prepare for repeated access to a physical
1455 * @cache: #MemoryRegionCache to be filled
1456 * @as: #AddressSpace to be accessed
1457 * @addr: address within that address space
1458 * @len: length of buffer
1459 * @is_write: indicates the transfer direction
1461 * Will only work with RAM, and may map a subset of the requested range by
1462 * returning a value that is less than @len. On failure, return a negative
1465 * Because it only works with RAM, this function can be used for
1466 * read-modify-write operations. In this case, is_write should be %true.
1468 * Note that addresses passed to the address_space_*_cached functions
1469 * are relative to @addr.
1471 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1478 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1480 * @cache: The #MemoryRegionCache to operate on.
1481 * @addr: The first physical address that was written, relative to the
1482 * address that was passed to @address_space_cache_init.
1483 * @access_len: The number of bytes that were written starting at @addr.
1485 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1490 * address_space_cache_destroy: free a #MemoryRegionCache
1492 * @cache: The #MemoryRegionCache whose memory should be released.
1494 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1496 /* address_space_ld*_cached: load from a cached #MemoryRegion
1497 * address_space_st*_cached: store into a cached #MemoryRegion
1499 * These functions perform a load or store of the byte, word,
1500 * longword or quad to the specified address. The address is
1501 * a physical address in the AddressSpace, but it must lie within
1502 * a #MemoryRegion that was mapped with address_space_cache_init.
1504 * The _le suffixed functions treat the data as little endian;
1505 * _be indicates big endian; no suffix indicates "same endianness
1508 * The "guest CPU endianness" accessors are deprecated for use outside
1509 * target-* code; devices should be CPU-agnostic and use either the LE
1510 * or the BE accessors.
1512 * @cache: previously initialized #MemoryRegionCache to be accessed
1513 * @addr: address within the address space
1514 * @val: data value, for stores
1515 * @attrs: memory transaction attributes
1516 * @result: location to write the success/failure of the transaction;
1517 * if NULL, this information is discarded
1519 uint32_t address_space_ldub_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1520 MemTxAttrs attrs
, MemTxResult
*result
);
1521 uint32_t address_space_lduw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1522 MemTxAttrs attrs
, MemTxResult
*result
);
1523 uint32_t address_space_lduw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1524 MemTxAttrs attrs
, MemTxResult
*result
);
1525 uint32_t address_space_ldl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1526 MemTxAttrs attrs
, MemTxResult
*result
);
1527 uint32_t address_space_ldl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1528 MemTxAttrs attrs
, MemTxResult
*result
);
1529 uint64_t address_space_ldq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1530 MemTxAttrs attrs
, MemTxResult
*result
);
1531 uint64_t address_space_ldq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1532 MemTxAttrs attrs
, MemTxResult
*result
);
1533 void address_space_stb_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1534 MemTxAttrs attrs
, MemTxResult
*result
);
1535 void address_space_stw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1536 MemTxAttrs attrs
, MemTxResult
*result
);
1537 void address_space_stw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1538 MemTxAttrs attrs
, MemTxResult
*result
);
1539 void address_space_stl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1540 MemTxAttrs attrs
, MemTxResult
*result
);
1541 void address_space_stl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1542 MemTxAttrs attrs
, MemTxResult
*result
);
1543 void address_space_stq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1544 MemTxAttrs attrs
, MemTxResult
*result
);
1545 void address_space_stq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1546 MemTxAttrs attrs
, MemTxResult
*result
);
1548 uint32_t ldub_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1549 uint32_t lduw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1550 uint32_t lduw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1551 uint32_t ldl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1552 uint32_t ldl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1553 uint64_t ldq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1554 uint64_t ldq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1555 void stb_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1556 void stw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1557 void stw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1558 void stl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1559 void stl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1560 void stq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1561 void stq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1562 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1563 * entry. Should be called from an RCU critical section.
1565 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1568 /* address_space_translate: translate an address range into an address space
1569 * into a MemoryRegion and an address range into that section. Should be
1570 * called from an RCU critical section, to avoid that the last reference
1571 * to the returned region disappears after address_space_translate returns.
1573 * @as: #AddressSpace to be accessed
1574 * @addr: address within that address space
1575 * @xlat: pointer to address within the returned memory region section's
1577 * @len: pointer to length
1578 * @is_write: indicates the transfer direction
1580 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1581 hwaddr
*xlat
, hwaddr
*len
,
1584 /* address_space_access_valid: check for validity of accessing an address
1587 * Check whether memory is assigned to the given address space range, and
1588 * access is permitted by any IOMMU regions that are active for the address
1591 * For now, addr and len should be aligned to a page size. This limitation
1592 * will be lifted in the future.
1594 * @as: #AddressSpace to be accessed
1595 * @addr: address within that address space
1596 * @len: length of the area to be checked
1597 * @is_write: indicates the transfer direction
1599 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1601 /* address_space_map: map a physical memory region into a host virtual address
1603 * May map a subset of the requested range, given by and returned in @plen.
1604 * May return %NULL if resources needed to perform the mapping are exhausted.
1605 * Use only for reads OR writes - not for read-modify-write operations.
1606 * Use cpu_register_map_client() to know when retrying the map operation is
1607 * likely to succeed.
1609 * @as: #AddressSpace to be accessed
1610 * @addr: address within that address space
1611 * @plen: pointer to length of buffer; updated on return
1612 * @is_write: indicates the transfer direction
1614 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1615 hwaddr
*plen
, bool is_write
);
1617 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1619 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1620 * the amount of memory that was actually read or written by the caller.
1622 * @as: #AddressSpace used
1623 * @addr: address within that address space
1624 * @len: buffer length as returned by address_space_map()
1625 * @access_len: amount of data actually transferred
1626 * @is_write: indicates the transfer direction
1628 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1629 int is_write
, hwaddr access_len
);
1632 /* Internal functions, part of the implementation of address_space_read. */
1633 MemTxResult
address_space_read_continue(AddressSpace
*as
, hwaddr addr
,
1634 MemTxAttrs attrs
, uint8_t *buf
,
1635 int len
, hwaddr addr1
, hwaddr l
,
1637 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
1638 MemTxAttrs attrs
, uint8_t *buf
, int len
);
1639 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
1641 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
1644 return memory_region_is_ram(mr
) &&
1645 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
1647 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
1648 memory_region_is_romd(mr
);
1653 * address_space_read: read from an address space.
1655 * Return a MemTxResult indicating whether the operation succeeded
1656 * or failed (eg unassigned memory, device rejected the transaction,
1659 * @as: #AddressSpace to be accessed
1660 * @addr: address within that address space
1661 * @attrs: memory transaction attributes
1662 * @buf: buffer with the data transferred
1664 static inline __attribute__((__always_inline__
))
1665 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1666 uint8_t *buf
, int len
)
1668 MemTxResult result
= MEMTX_OK
;
1673 if (__builtin_constant_p(len
)) {
1677 mr
= address_space_translate(as
, addr
, &addr1
, &l
, false);
1678 if (len
== l
&& memory_access_is_direct(mr
, false)) {
1679 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
1680 memcpy(buf
, ptr
, len
);
1682 result
= address_space_read_continue(as
, addr
, attrs
, buf
, len
,
1688 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
1694 * address_space_read_cached: read from a cached RAM region
1696 * @cache: Cached region to be addressed
1697 * @addr: address relative to the base of the RAM region
1698 * @buf: buffer with the data transferred
1699 * @len: length of the data transferred
1702 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1705 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1706 address_space_read(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);
1710 * address_space_write_cached: write to a cached RAM region
1712 * @cache: Cached region to be addressed
1713 * @addr: address relative to the base of the RAM region
1714 * @buf: buffer with the data transferred
1715 * @len: length of the data transferred
1718 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1721 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1722 address_space_write(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);