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
;
242 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
243 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
246 * MemoryListener: callbacks structure for updates to the physical memory map
248 * Allows a component to adjust to changes in the guest-visible memory map.
249 * Use with memory_listener_register() and memory_listener_unregister().
251 struct MemoryListener
{
252 void (*begin
)(MemoryListener
*listener
);
253 void (*commit
)(MemoryListener
*listener
);
254 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
255 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
256 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
257 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
259 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
261 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
262 void (*log_global_start
)(MemoryListener
*listener
);
263 void (*log_global_stop
)(MemoryListener
*listener
);
264 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
265 bool match_data
, uint64_t data
, EventNotifier
*e
);
266 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
267 bool match_data
, uint64_t data
, EventNotifier
*e
);
268 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
269 hwaddr addr
, hwaddr len
);
270 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
271 hwaddr addr
, hwaddr len
);
272 /* Lower = earlier (during add), later (during del) */
274 AddressSpace
*address_space
;
275 QTAILQ_ENTRY(MemoryListener
) link
;
276 QTAILQ_ENTRY(MemoryListener
) link_as
;
280 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
282 struct AddressSpace
{
283 /* All fields are private. */
290 /* Accessed via RCU. */
291 struct FlatView
*current_map
;
294 struct MemoryRegionIoeventfd
*ioeventfds
;
295 struct AddressSpaceDispatch
*dispatch
;
296 struct AddressSpaceDispatch
*next_dispatch
;
297 MemoryListener dispatch_listener
;
298 QTAILQ_HEAD(memory_listeners_as
, MemoryListener
) listeners
;
299 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
303 * MemoryRegionSection: describes a fragment of a #MemoryRegion
305 * @mr: the region, or %NULL if empty
306 * @address_space: the address space the region is mapped in
307 * @offset_within_region: the beginning of the section, relative to @mr's start
308 * @size: the size of the section; will not exceed @mr's boundaries
309 * @offset_within_address_space: the address of the first byte of the section
310 * relative to the region's address space
311 * @readonly: writes to this section are ignored
313 struct MemoryRegionSection
{
315 AddressSpace
*address_space
;
316 hwaddr offset_within_region
;
318 hwaddr offset_within_address_space
;
323 * memory_region_init: Initialize a memory region
325 * The region typically acts as a container for other memory regions. Use
326 * memory_region_add_subregion() to add subregions.
328 * @mr: the #MemoryRegion to be initialized
329 * @owner: the object that tracks the region's reference count
330 * @name: used for debugging; not visible to the user or ABI
331 * @size: size of the region; any subregions beyond this size will be clipped
333 void memory_region_init(MemoryRegion
*mr
,
334 struct Object
*owner
,
339 * memory_region_ref: Add 1 to a memory region's reference count
341 * Whenever memory regions are accessed outside the BQL, they need to be
342 * preserved against hot-unplug. MemoryRegions actually do not have their
343 * own reference count; they piggyback on a QOM object, their "owner".
344 * This function adds a reference to the owner.
346 * All MemoryRegions must have an owner if they can disappear, even if the
347 * device they belong to operates exclusively under the BQL. This is because
348 * the region could be returned at any time by memory_region_find, and this
349 * is usually under guest control.
351 * @mr: the #MemoryRegion
353 void memory_region_ref(MemoryRegion
*mr
);
356 * memory_region_unref: Remove 1 to a memory region's reference count
358 * Whenever memory regions are accessed outside the BQL, they need to be
359 * preserved against hot-unplug. MemoryRegions actually do not have their
360 * own reference count; they piggyback on a QOM object, their "owner".
361 * This function removes a reference to the owner and possibly destroys it.
363 * @mr: the #MemoryRegion
365 void memory_region_unref(MemoryRegion
*mr
);
368 * memory_region_init_io: Initialize an I/O memory region.
370 * Accesses into the region will cause the callbacks in @ops to be called.
371 * if @size is nonzero, subregions will be clipped to @size.
373 * @mr: the #MemoryRegion to be initialized.
374 * @owner: the object that tracks the region's reference count
375 * @ops: a structure containing read and write callbacks to be used when
376 * I/O is performed on the region.
377 * @opaque: passed to the read and write callbacks of the @ops structure.
378 * @name: used for debugging; not visible to the user or ABI
379 * @size: size of the region.
381 void memory_region_init_io(MemoryRegion
*mr
,
382 struct Object
*owner
,
383 const MemoryRegionOps
*ops
,
389 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
390 * region will modify memory directly.
392 * @mr: the #MemoryRegion to be initialized.
393 * @owner: the object that tracks the region's reference count
394 * @name: Region name, becomes part of RAMBlock name used in migration stream
395 * must be unique within any device
396 * @size: size of the region.
397 * @errp: pointer to Error*, to store an error if it happens.
399 void memory_region_init_ram(MemoryRegion
*mr
,
400 struct Object
*owner
,
406 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
407 * RAM. Accesses into the region will
408 * modify memory directly. Only an initial
409 * portion of this RAM is actually used.
410 * The used size can change across reboots.
412 * @mr: the #MemoryRegion to be initialized.
413 * @owner: the object that tracks the region's reference count
414 * @name: Region name, becomes part of RAMBlock name used in migration stream
415 * must be unique within any device
416 * @size: used size of the region.
417 * @max_size: max size of the region.
418 * @resized: callback to notify owner about used size change.
419 * @errp: pointer to Error*, to store an error if it happens.
421 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
422 struct Object
*owner
,
426 void (*resized
)(const char*,
432 * memory_region_init_ram_from_file: Initialize RAM memory region with a
435 * @mr: the #MemoryRegion to be initialized.
436 * @owner: the object that tracks the region's reference count
437 * @name: Region name, becomes part of RAMBlock name used in migration stream
438 * must be unique within any device
439 * @size: size of the region.
440 * @share: %true if memory must be mmaped with the MAP_SHARED flag
441 * @path: the path in which to allocate the RAM.
442 * @errp: pointer to Error*, to store an error if it happens.
444 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
445 struct Object
*owner
,
454 * memory_region_init_ram_ptr: Initialize RAM memory region from a
455 * user-provided pointer. Accesses into the
456 * region will modify memory directly.
458 * @mr: the #MemoryRegion to be initialized.
459 * @owner: the object that tracks the region's reference count
460 * @name: Region name, becomes part of RAMBlock name used in migration stream
461 * must be unique within any device
462 * @size: size of the region.
463 * @ptr: memory to be mapped; must contain at least @size bytes.
465 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
466 struct Object
*owner
,
472 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
473 * a user-provided pointer.
475 * A RAM device represents a mapping to a physical device, such as to a PCI
476 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
477 * into the VM address space and access to the region will modify memory
478 * directly. However, the memory region should not be included in a memory
479 * dump (device may not be enabled/mapped at the time of the dump), and
480 * operations incompatible with manipulating MMIO should be avoided. Replaces
483 * @mr: the #MemoryRegion to be initialized.
484 * @owner: the object that tracks the region's reference count
485 * @name: the name of the region.
486 * @size: size of the region.
487 * @ptr: memory to be mapped; must contain at least @size bytes.
489 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
490 struct Object
*owner
,
496 * memory_region_init_alias: Initialize a memory region that aliases all or a
497 * part of another memory region.
499 * @mr: the #MemoryRegion to be initialized.
500 * @owner: the object that tracks the region's reference count
501 * @name: used for debugging; not visible to the user or ABI
502 * @orig: the region to be referenced; @mr will be equivalent to
503 * @orig between @offset and @offset + @size - 1.
504 * @offset: start of the section in @orig to be referenced.
505 * @size: size of the region.
507 void memory_region_init_alias(MemoryRegion
*mr
,
508 struct Object
*owner
,
515 * memory_region_init_rom: Initialize a ROM memory region.
517 * This has the same effect as calling memory_region_init_ram()
518 * and then marking the resulting region read-only with
519 * memory_region_set_readonly().
521 * @mr: the #MemoryRegion to be initialized.
522 * @owner: the object that tracks the region's reference count
523 * @name: Region name, becomes part of RAMBlock name used in migration stream
524 * must be unique within any device
525 * @size: size of the region.
526 * @errp: pointer to Error*, to store an error if it happens.
528 void memory_region_init_rom(MemoryRegion
*mr
,
529 struct Object
*owner
,
535 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
536 * handled via callbacks.
538 * @mr: the #MemoryRegion to be initialized.
539 * @owner: the object that tracks the region's reference count
540 * @ops: callbacks for write access handling (must not be NULL).
541 * @name: Region name, becomes part of RAMBlock name used in migration stream
542 * must be unique within any device
543 * @size: size of the region.
544 * @errp: pointer to Error*, to store an error if it happens.
546 void memory_region_init_rom_device(MemoryRegion
*mr
,
547 struct Object
*owner
,
548 const MemoryRegionOps
*ops
,
555 * memory_region_init_reservation: Initialize a memory region that reserves
558 * A reservation region primariy serves debugging purposes. It claims I/O
559 * space that is not supposed to be handled by QEMU itself. Any access via
560 * the memory API will cause an abort().
561 * This function is deprecated. Use memory_region_init_io() with NULL
564 * @mr: the #MemoryRegion to be initialized
565 * @owner: the object that tracks the region's reference count
566 * @name: used for debugging; not visible to the user or ABI
567 * @size: size of the region.
569 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
574 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
578 * memory_region_init_iommu: Initialize a memory region that translates
581 * An IOMMU region translates addresses and forwards accesses to a target
584 * @mr: the #MemoryRegion to be initialized
585 * @owner: the object that tracks the region's reference count
586 * @ops: a function that translates addresses into the @target region
587 * @name: used for debugging; not visible to the user or ABI
588 * @size: size of the region.
590 void memory_region_init_iommu(MemoryRegion
*mr
,
591 struct Object
*owner
,
592 const MemoryRegionIOMMUOps
*ops
,
597 * memory_region_owner: get a memory region's owner.
599 * @mr: the memory region being queried.
601 struct Object
*memory_region_owner(MemoryRegion
*mr
);
604 * memory_region_size: get a memory region's size.
606 * @mr: the memory region being queried.
608 uint64_t memory_region_size(MemoryRegion
*mr
);
611 * memory_region_is_ram: check whether a memory region is random access
613 * Returns %true is a memory region is random access.
615 * @mr: the memory region being queried
617 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
623 * memory_region_is_ram_device: check whether a memory region is a ram device
625 * Returns %true is a memory region is a device backed ram region
627 * @mr: the memory region being queried
629 bool memory_region_is_ram_device(MemoryRegion
*mr
);
632 * memory_region_is_romd: check whether a memory region is in ROMD mode
634 * Returns %true if a memory region is a ROM device and currently set to allow
637 * @mr: the memory region being queried
639 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
641 return mr
->rom_device
&& mr
->romd_mode
;
645 * memory_region_is_iommu: check whether a memory region is an iommu
647 * Returns %true is a memory region is an iommu.
649 * @mr: the memory region being queried
651 static inline bool memory_region_is_iommu(MemoryRegion
*mr
)
654 return memory_region_is_iommu(mr
->alias
);
656 return mr
->iommu_ops
;
661 * memory_region_iommu_get_min_page_size: get minimum supported page size
664 * Returns minimum supported page size for an iommu.
666 * @mr: the memory region being queried
668 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion
*mr
);
671 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
673 * The notification type will be decided by entry.perm bits:
675 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
676 * - For MAP (newly added entry) notifies: set entry.perm to the
677 * permission of the page (which is definitely !IOMMU_NONE).
679 * Note: for any IOMMU implementation, an in-place mapping change
680 * should be notified with an UNMAP followed by a MAP.
682 * @mr: the memory region that was changed
683 * @entry: the new entry in the IOMMU translation table. The entry
684 * replaces all old entries for the same virtual I/O address range.
685 * Deleted entries have .@perm == 0.
687 void memory_region_notify_iommu(MemoryRegion
*mr
,
688 IOMMUTLBEntry entry
);
691 * memory_region_notify_one: notify a change in an IOMMU translation
692 * entry to a single notifier
694 * This works just like memory_region_notify_iommu(), but it only
695 * notifies a specific notifier, not all of them.
697 * @notifier: the notifier to be notified
698 * @entry: the new entry in the IOMMU translation table. The entry
699 * replaces all old entries for the same virtual I/O address range.
700 * Deleted entries have .@perm == 0.
702 void memory_region_notify_one(IOMMUNotifier
*notifier
,
703 IOMMUTLBEntry
*entry
);
706 * memory_region_register_iommu_notifier: register a notifier for changes to
707 * IOMMU translation entries.
709 * @mr: the memory region to observe
710 * @n: the IOMMUNotifier to be added; the notify callback receives a
711 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
712 * ceases to be valid on exit from the notifier.
714 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
718 * memory_region_iommu_replay: replay existing IOMMU translations to
719 * a notifier with the minimum page granularity returned by
720 * mr->iommu_ops->get_page_size().
722 * @mr: the memory region to observe
723 * @n: the notifier to which to replay iommu mappings
724 * @is_write: Whether to treat the replay as a translate "write"
727 void memory_region_iommu_replay(MemoryRegion
*mr
, IOMMUNotifier
*n
,
731 * memory_region_iommu_replay_all: replay existing IOMMU translations
732 * to all the notifiers registered.
734 * @mr: the memory region to observe
736 void memory_region_iommu_replay_all(MemoryRegion
*mr
);
739 * memory_region_unregister_iommu_notifier: unregister a notifier for
740 * changes to IOMMU translation entries.
742 * @mr: the memory region which was observed and for which notity_stopped()
744 * @n: the notifier to be removed.
746 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
750 * memory_region_name: get a memory region's name
752 * Returns the string that was used to initialize the memory region.
754 * @mr: the memory region being queried
756 const char *memory_region_name(const MemoryRegion
*mr
);
759 * memory_region_is_logging: return whether a memory region is logging writes
761 * Returns %true if the memory region is logging writes for the given client
763 * @mr: the memory region being queried
764 * @client: the client being queried
766 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
769 * memory_region_get_dirty_log_mask: return the clients for which a
770 * memory region is logging writes.
772 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
773 * are the bit indices.
775 * @mr: the memory region being queried
777 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
780 * memory_region_is_rom: check whether a memory region is ROM
782 * Returns %true is a memory region is read-only memory.
784 * @mr: the memory region being queried
786 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
788 return mr
->ram
&& mr
->readonly
;
793 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
795 * Returns a file descriptor backing a file-based RAM memory region,
796 * or -1 if the region is not a file-based RAM memory region.
798 * @mr: the RAM or alias memory region being queried.
800 int memory_region_get_fd(MemoryRegion
*mr
);
803 * memory_region_set_fd: Mark a RAM memory region as backed by a
806 * This function is typically used after memory_region_init_ram_ptr().
808 * @mr: the memory region being queried.
809 * @fd: the file descriptor that backs @mr.
811 void memory_region_set_fd(MemoryRegion
*mr
, int fd
);
814 * memory_region_from_host: Convert a pointer into a RAM memory region
815 * and an offset within it.
817 * Given a host pointer inside a RAM memory region (created with
818 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
819 * the MemoryRegion and the offset within it.
821 * Use with care; by the time this function returns, the returned pointer is
822 * not protected by RCU anymore. If the caller is not within an RCU critical
823 * section and does not hold the iothread lock, it must have other means of
824 * protecting the pointer, such as a reference to the region that includes
825 * the incoming ram_addr_t.
827 * @mr: the memory region being queried.
829 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
832 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
834 * Returns a host pointer to a RAM memory region (created with
835 * memory_region_init_ram() or memory_region_init_ram_ptr()).
837 * Use with care; by the time this function returns, the returned pointer is
838 * not protected by RCU anymore. If the caller is not within an RCU critical
839 * section and does not hold the iothread lock, it must have other means of
840 * protecting the pointer, such as a reference to the region that includes
841 * the incoming ram_addr_t.
843 * @mr: the memory region being queried.
845 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
847 /* memory_region_ram_resize: Resize a RAM region.
849 * Only legal before guest might have detected the memory size: e.g. on
850 * incoming migration, or right after reset.
852 * @mr: a memory region created with @memory_region_init_resizeable_ram.
853 * @newsize: the new size the region
854 * @errp: pointer to Error*, to store an error if it happens.
856 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
860 * memory_region_set_log: Turn dirty logging on or off for a region.
862 * Turns dirty logging on or off for a specified client (display, migration).
863 * Only meaningful for RAM regions.
865 * @mr: the memory region being updated.
866 * @log: whether dirty logging is to be enabled or disabled.
867 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
869 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
872 * memory_region_get_dirty: Check whether a range of bytes is dirty
873 * for a specified client.
875 * Checks whether a range of bytes has been written to since the last
876 * call to memory_region_reset_dirty() with the same @client. Dirty logging
879 * @mr: the memory region being queried.
880 * @addr: the address (relative to the start of the region) being queried.
881 * @size: the size of the range being queried.
882 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
885 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
886 hwaddr size
, unsigned client
);
889 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
891 * Marks a range of bytes as dirty, after it has been dirtied outside
894 * @mr: the memory region being dirtied.
895 * @addr: the address (relative to the start of the region) being dirtied.
896 * @size: size of the range being dirtied.
898 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
902 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
903 * for a specified client. It clears them.
905 * Checks whether a range of bytes has been written to since the last
906 * call to memory_region_reset_dirty() with the same @client. Dirty logging
909 * @mr: the memory region being queried.
910 * @addr: the address (relative to the start of the region) being queried.
911 * @size: the size of the range being queried.
912 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
915 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
916 hwaddr size
, unsigned client
);
918 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
919 * any external TLBs (e.g. kvm)
921 * Flushes dirty information from accelerators such as kvm and vhost-net
922 * and makes it available to users of the memory API.
924 * @mr: the region being flushed.
926 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
929 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
932 * Marks a range of pages as no longer dirty.
934 * @mr: the region being updated.
935 * @addr: the start of the subrange being cleaned.
936 * @size: the size of the subrange being cleaned.
937 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
940 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
941 hwaddr size
, unsigned client
);
944 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
946 * Allows a memory region to be marked as read-only (turning it into a ROM).
947 * only useful on RAM regions.
949 * @mr: the region being updated.
950 * @readonly: whether rhe region is to be ROM or RAM.
952 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
955 * memory_region_rom_device_set_romd: enable/disable ROMD mode
957 * Allows a ROM device (initialized with memory_region_init_rom_device() to
958 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
959 * device is mapped to guest memory and satisfies read access directly.
960 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
961 * Writes are always handled by the #MemoryRegion.write function.
963 * @mr: the memory region to be updated
964 * @romd_mode: %true to put the region into ROMD mode
966 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
969 * memory_region_set_coalescing: Enable memory coalescing for the region.
971 * Enabled writes to a region to be queued for later processing. MMIO ->write
972 * callbacks may be delayed until a non-coalesced MMIO is issued.
973 * Only useful for IO regions. Roughly similar to write-combining hardware.
975 * @mr: the memory region to be write coalesced
977 void memory_region_set_coalescing(MemoryRegion
*mr
);
980 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
983 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
984 * Multiple calls can be issued coalesced disjoint ranges.
986 * @mr: the memory region to be updated.
987 * @offset: the start of the range within the region to be coalesced.
988 * @size: the size of the subrange to be coalesced.
990 void memory_region_add_coalescing(MemoryRegion
*mr
,
995 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
997 * Disables any coalescing caused by memory_region_set_coalescing() or
998 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1001 * @mr: the memory region to be updated.
1003 void memory_region_clear_coalescing(MemoryRegion
*mr
);
1006 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1009 * Ensure that pending coalesced MMIO request are flushed before the memory
1010 * region is accessed. This property is automatically enabled for all regions
1011 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1013 * @mr: the memory region to be updated.
1015 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
1018 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1021 * Clear the automatic coalesced MMIO flushing enabled via
1022 * memory_region_set_flush_coalesced. Note that this service has no effect on
1023 * memory regions that have MMIO coalescing enabled for themselves. For them,
1024 * automatic flushing will stop once coalescing is disabled.
1026 * @mr: the memory region to be updated.
1028 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1031 * memory_region_set_global_locking: Declares the access processing requires
1032 * QEMU's global lock.
1034 * When this is invoked, accesses to the memory region will be processed while
1035 * holding the global lock of QEMU. This is the default behavior of memory
1038 * @mr: the memory region to be updated.
1040 void memory_region_set_global_locking(MemoryRegion
*mr
);
1043 * memory_region_clear_global_locking: Declares that access processing does
1044 * not depend on the QEMU global lock.
1046 * By clearing this property, accesses to the memory region will be processed
1047 * outside of QEMU's global lock (unless the lock is held on when issuing the
1048 * access request). In this case, the device model implementing the access
1049 * handlers is responsible for synchronization of concurrency.
1051 * @mr: the memory region to be updated.
1053 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1056 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1057 * is written to a location.
1059 * Marks a word in an IO region (initialized with memory_region_init_io())
1060 * as a trigger for an eventfd event. The I/O callback will not be called.
1061 * The caller must be prepared to handle failure (that is, take the required
1062 * action if the callback _is_ called).
1064 * @mr: the memory region being updated.
1065 * @addr: the address within @mr that is to be monitored
1066 * @size: the size of the access to trigger the eventfd
1067 * @match_data: whether to match against @data, instead of just @addr
1068 * @data: the data to match against the guest write
1069 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1071 void memory_region_add_eventfd(MemoryRegion
*mr
,
1079 * memory_region_del_eventfd: Cancel an eventfd.
1081 * Cancels an eventfd trigger requested by a previous
1082 * memory_region_add_eventfd() call.
1084 * @mr: the memory region being updated.
1085 * @addr: the address within @mr that is to be monitored
1086 * @size: the size of the access to trigger the eventfd
1087 * @match_data: whether to match against @data, instead of just @addr
1088 * @data: the data to match against the guest write
1089 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1091 void memory_region_del_eventfd(MemoryRegion
*mr
,
1099 * memory_region_add_subregion: Add a subregion to a container.
1101 * Adds a subregion at @offset. The subregion may not overlap with other
1102 * subregions (except for those explicitly marked as overlapping). A region
1103 * may only be added once as a subregion (unless removed with
1104 * memory_region_del_subregion()); use memory_region_init_alias() if you
1105 * want a region to be a subregion in multiple locations.
1107 * @mr: the region to contain the new subregion; must be a container
1108 * initialized with memory_region_init().
1109 * @offset: the offset relative to @mr where @subregion is added.
1110 * @subregion: the subregion to be added.
1112 void memory_region_add_subregion(MemoryRegion
*mr
,
1114 MemoryRegion
*subregion
);
1116 * memory_region_add_subregion_overlap: Add a subregion to a container
1119 * Adds a subregion at @offset. The subregion may overlap with other
1120 * subregions. Conflicts are resolved by having a higher @priority hide a
1121 * lower @priority. Subregions without priority are taken as @priority 0.
1122 * A region may only be added once as a subregion (unless removed with
1123 * memory_region_del_subregion()); use memory_region_init_alias() if you
1124 * want a region to be a subregion in multiple locations.
1126 * @mr: the region to contain the new subregion; must be a container
1127 * initialized with memory_region_init().
1128 * @offset: the offset relative to @mr where @subregion is added.
1129 * @subregion: the subregion to be added.
1130 * @priority: used for resolving overlaps; highest priority wins.
1132 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1134 MemoryRegion
*subregion
,
1138 * memory_region_get_ram_addr: Get the ram address associated with a memory
1141 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1143 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1145 * memory_region_del_subregion: Remove a subregion.
1147 * Removes a subregion from its container.
1149 * @mr: the container to be updated.
1150 * @subregion: the region being removed; must be a current subregion of @mr.
1152 void memory_region_del_subregion(MemoryRegion
*mr
,
1153 MemoryRegion
*subregion
);
1156 * memory_region_set_enabled: dynamically enable or disable a region
1158 * Enables or disables a memory region. A disabled memory region
1159 * ignores all accesses to itself and its subregions. It does not
1160 * obscure sibling subregions with lower priority - it simply behaves as
1161 * if it was removed from the hierarchy.
1163 * Regions default to being enabled.
1165 * @mr: the region to be updated
1166 * @enabled: whether to enable or disable the region
1168 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1171 * memory_region_set_address: dynamically update the address of a region
1173 * Dynamically updates the address of a region, relative to its container.
1174 * May be used on regions are currently part of a memory hierarchy.
1176 * @mr: the region to be updated
1177 * @addr: new address, relative to container region
1179 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1182 * memory_region_set_size: dynamically update the size of a region.
1184 * Dynamically updates the size of a region.
1186 * @mr: the region to be updated
1187 * @size: used size of the region.
1189 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1192 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1194 * Dynamically updates the offset into the target region that an alias points
1195 * to, as if the fourth argument to memory_region_init_alias() has changed.
1197 * @mr: the #MemoryRegion to be updated; should be an alias.
1198 * @offset: the new offset into the target memory region
1200 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1204 * memory_region_present: checks if an address relative to a @container
1205 * translates into #MemoryRegion within @container
1207 * Answer whether a #MemoryRegion within @container covers the address
1210 * @container: a #MemoryRegion within which @addr is a relative address
1211 * @addr: the area within @container to be searched
1213 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1216 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1217 * into any address space.
1219 * @mr: a #MemoryRegion which should be checked if it's mapped
1221 bool memory_region_is_mapped(MemoryRegion
*mr
);
1224 * memory_region_find: translate an address/size relative to a
1225 * MemoryRegion into a #MemoryRegionSection.
1227 * Locates the first #MemoryRegion within @mr that overlaps the range
1228 * given by @addr and @size.
1230 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1231 * It will have the following characteristics:
1232 * .@size = 0 iff no overlap was found
1233 * .@mr is non-%NULL iff an overlap was found
1235 * Remember that in the return value the @offset_within_region is
1236 * relative to the returned region (in the .@mr field), not to the
1239 * Similarly, the .@offset_within_address_space is relative to the
1240 * address space that contains both regions, the passed and the
1241 * returned one. However, in the special case where the @mr argument
1242 * has no container (and thus is the root of the address space), the
1243 * following will hold:
1244 * .@offset_within_address_space >= @addr
1245 * .@offset_within_address_space + .@size <= @addr + @size
1247 * @mr: a MemoryRegion within which @addr is a relative address
1248 * @addr: start of the area within @as to be searched
1249 * @size: size of the area to be searched
1251 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1252 hwaddr addr
, uint64_t size
);
1255 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1257 * Synchronizes the dirty page log for all address spaces.
1259 void memory_global_dirty_log_sync(void);
1262 * memory_region_transaction_begin: Start a transaction.
1264 * During a transaction, changes will be accumulated and made visible
1265 * only when the transaction ends (is committed).
1267 void memory_region_transaction_begin(void);
1270 * memory_region_transaction_commit: Commit a transaction and make changes
1271 * visible to the guest.
1273 void memory_region_transaction_commit(void);
1276 * memory_listener_register: register callbacks to be called when memory
1277 * sections are mapped or unmapped into an address
1280 * @listener: an object containing the callbacks to be called
1281 * @filter: if non-%NULL, only regions in this address space will be observed
1283 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1286 * memory_listener_unregister: undo the effect of memory_listener_register()
1288 * @listener: an object containing the callbacks to be removed
1290 void memory_listener_unregister(MemoryListener
*listener
);
1293 * memory_global_dirty_log_start: begin dirty logging for all regions
1295 void memory_global_dirty_log_start(void);
1298 * memory_global_dirty_log_stop: end dirty logging for all regions
1300 void memory_global_dirty_log_stop(void);
1302 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
);
1305 * memory_region_dispatch_read: perform a read directly to the specified
1308 * @mr: #MemoryRegion to access
1309 * @addr: address within that region
1310 * @pval: pointer to uint64_t which the data is written to
1311 * @size: size of the access in bytes
1312 * @attrs: memory transaction attributes to use for the access
1314 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1320 * memory_region_dispatch_write: perform a write directly to the specified
1323 * @mr: #MemoryRegion to access
1324 * @addr: address within that region
1325 * @data: data to write
1326 * @size: size of the access in bytes
1327 * @attrs: memory transaction attributes to use for the access
1329 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1336 * address_space_init: initializes an address space
1338 * @as: an uninitialized #AddressSpace
1339 * @root: a #MemoryRegion that routes addresses for the address space
1340 * @name: an address space name. The name is only used for debugging
1343 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1346 * address_space_init_shareable: return an address space for a memory region,
1347 * creating it if it does not already exist
1349 * @root: a #MemoryRegion that routes addresses for the address space
1350 * @name: an address space name. The name is only used for debugging
1353 * This function will return a pointer to an existing AddressSpace
1354 * which was initialized with the specified MemoryRegion, or it will
1355 * create and initialize one if it does not already exist. The ASes
1356 * are reference-counted, so the memory will be freed automatically
1357 * when the AddressSpace is destroyed via address_space_destroy.
1359 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
,
1363 * address_space_destroy: destroy an address space
1365 * Releases all resources associated with an address space. After an address space
1366 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1369 * @as: address space to be destroyed
1371 void address_space_destroy(AddressSpace
*as
);
1374 * address_space_rw: read from or write to an address space.
1376 * Return a MemTxResult indicating whether the operation succeeded
1377 * or failed (eg unassigned memory, device rejected the transaction,
1380 * @as: #AddressSpace to be accessed
1381 * @addr: address within that address space
1382 * @attrs: memory transaction attributes
1383 * @buf: buffer with the data transferred
1384 * @is_write: indicates the transfer direction
1386 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1387 MemTxAttrs attrs
, uint8_t *buf
,
1388 int len
, bool is_write
);
1391 * address_space_write: write to address space.
1393 * Return a MemTxResult indicating whether the operation succeeded
1394 * or failed (eg unassigned memory, device rejected the transaction,
1397 * @as: #AddressSpace to be accessed
1398 * @addr: address within that address space
1399 * @attrs: memory transaction attributes
1400 * @buf: buffer with the data transferred
1402 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1404 const uint8_t *buf
, int len
);
1406 /* address_space_ld*: load from an address space
1407 * address_space_st*: store to an address space
1409 * These functions perform a load or store of the byte, word,
1410 * longword or quad to the specified address within the AddressSpace.
1411 * The _le suffixed functions treat the data as little endian;
1412 * _be indicates big endian; no suffix indicates "same endianness
1415 * The "guest CPU endianness" accessors are deprecated for use outside
1416 * target-* code; devices should be CPU-agnostic and use either the LE
1417 * or the BE accessors.
1419 * @as #AddressSpace to be accessed
1420 * @addr: address within that address space
1421 * @val: data value, for stores
1422 * @attrs: memory transaction attributes
1423 * @result: location to write the success/failure of the transaction;
1424 * if NULL, this information is discarded
1426 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1427 MemTxAttrs attrs
, MemTxResult
*result
);
1428 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1429 MemTxAttrs attrs
, MemTxResult
*result
);
1430 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1431 MemTxAttrs attrs
, MemTxResult
*result
);
1432 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1433 MemTxAttrs attrs
, MemTxResult
*result
);
1434 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1435 MemTxAttrs attrs
, MemTxResult
*result
);
1436 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1437 MemTxAttrs attrs
, MemTxResult
*result
);
1438 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1439 MemTxAttrs attrs
, MemTxResult
*result
);
1440 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1441 MemTxAttrs attrs
, MemTxResult
*result
);
1442 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1443 MemTxAttrs attrs
, MemTxResult
*result
);
1444 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1445 MemTxAttrs attrs
, MemTxResult
*result
);
1446 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1447 MemTxAttrs attrs
, MemTxResult
*result
);
1448 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1449 MemTxAttrs attrs
, MemTxResult
*result
);
1450 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1451 MemTxAttrs attrs
, MemTxResult
*result
);
1452 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1453 MemTxAttrs attrs
, MemTxResult
*result
);
1455 uint32_t ldub_phys(AddressSpace
*as
, hwaddr addr
);
1456 uint32_t lduw_le_phys(AddressSpace
*as
, hwaddr addr
);
1457 uint32_t lduw_be_phys(AddressSpace
*as
, hwaddr addr
);
1458 uint32_t ldl_le_phys(AddressSpace
*as
, hwaddr addr
);
1459 uint32_t ldl_be_phys(AddressSpace
*as
, hwaddr addr
);
1460 uint64_t ldq_le_phys(AddressSpace
*as
, hwaddr addr
);
1461 uint64_t ldq_be_phys(AddressSpace
*as
, hwaddr addr
);
1462 void stb_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1463 void stw_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1464 void stw_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1465 void stl_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1466 void stl_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1467 void stq_le_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1468 void stq_be_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1470 struct MemoryRegionCache
{
1476 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .as = NULL })
1478 /* address_space_cache_init: prepare for repeated access to a physical
1481 * @cache: #MemoryRegionCache to be filled
1482 * @as: #AddressSpace to be accessed
1483 * @addr: address within that address space
1484 * @len: length of buffer
1485 * @is_write: indicates the transfer direction
1487 * Will only work with RAM, and may map a subset of the requested range by
1488 * returning a value that is less than @len. On failure, return a negative
1491 * Because it only works with RAM, this function can be used for
1492 * read-modify-write operations. In this case, is_write should be %true.
1494 * Note that addresses passed to the address_space_*_cached functions
1495 * are relative to @addr.
1497 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1504 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1506 * @cache: The #MemoryRegionCache to operate on.
1507 * @addr: The first physical address that was written, relative to the
1508 * address that was passed to @address_space_cache_init.
1509 * @access_len: The number of bytes that were written starting at @addr.
1511 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1516 * address_space_cache_destroy: free a #MemoryRegionCache
1518 * @cache: The #MemoryRegionCache whose memory should be released.
1520 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1522 /* address_space_ld*_cached: load from a cached #MemoryRegion
1523 * address_space_st*_cached: store into a cached #MemoryRegion
1525 * These functions perform a load or store of the byte, word,
1526 * longword or quad to the specified address. The address is
1527 * a physical address in the AddressSpace, but it must lie within
1528 * a #MemoryRegion that was mapped with address_space_cache_init.
1530 * The _le suffixed functions treat the data as little endian;
1531 * _be indicates big endian; no suffix indicates "same endianness
1534 * The "guest CPU endianness" accessors are deprecated for use outside
1535 * target-* code; devices should be CPU-agnostic and use either the LE
1536 * or the BE accessors.
1538 * @cache: previously initialized #MemoryRegionCache to be accessed
1539 * @addr: address within the address space
1540 * @val: data value, for stores
1541 * @attrs: memory transaction attributes
1542 * @result: location to write the success/failure of the transaction;
1543 * if NULL, this information is discarded
1545 uint32_t address_space_ldub_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1546 MemTxAttrs attrs
, MemTxResult
*result
);
1547 uint32_t address_space_lduw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1548 MemTxAttrs attrs
, MemTxResult
*result
);
1549 uint32_t address_space_lduw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1550 MemTxAttrs attrs
, MemTxResult
*result
);
1551 uint32_t address_space_ldl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1552 MemTxAttrs attrs
, MemTxResult
*result
);
1553 uint32_t address_space_ldl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1554 MemTxAttrs attrs
, MemTxResult
*result
);
1555 uint64_t address_space_ldq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1556 MemTxAttrs attrs
, MemTxResult
*result
);
1557 uint64_t address_space_ldq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1558 MemTxAttrs attrs
, MemTxResult
*result
);
1559 void address_space_stb_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1560 MemTxAttrs attrs
, MemTxResult
*result
);
1561 void address_space_stw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1562 MemTxAttrs attrs
, MemTxResult
*result
);
1563 void address_space_stw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1564 MemTxAttrs attrs
, MemTxResult
*result
);
1565 void address_space_stl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1566 MemTxAttrs attrs
, MemTxResult
*result
);
1567 void address_space_stl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1568 MemTxAttrs attrs
, MemTxResult
*result
);
1569 void address_space_stq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1570 MemTxAttrs attrs
, MemTxResult
*result
);
1571 void address_space_stq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1572 MemTxAttrs attrs
, MemTxResult
*result
);
1574 uint32_t ldub_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1575 uint32_t lduw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1576 uint32_t lduw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1577 uint32_t ldl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1578 uint32_t ldl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1579 uint64_t ldq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1580 uint64_t ldq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1581 void stb_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1582 void stw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1583 void stw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1584 void stl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1585 void stl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1586 void stq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1587 void stq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1588 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1589 * entry. Should be called from an RCU critical section.
1591 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1594 /* address_space_translate: translate an address range into an address space
1595 * into a MemoryRegion and an address range into that section. Should be
1596 * called from an RCU critical section, to avoid that the last reference
1597 * to the returned region disappears after address_space_translate returns.
1599 * @as: #AddressSpace to be accessed
1600 * @addr: address within that address space
1601 * @xlat: pointer to address within the returned memory region section's
1603 * @len: pointer to length
1604 * @is_write: indicates the transfer direction
1606 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1607 hwaddr
*xlat
, hwaddr
*len
,
1610 /* address_space_access_valid: check for validity of accessing an address
1613 * Check whether memory is assigned to the given address space range, and
1614 * access is permitted by any IOMMU regions that are active for the address
1617 * For now, addr and len should be aligned to a page size. This limitation
1618 * will be lifted in the future.
1620 * @as: #AddressSpace to be accessed
1621 * @addr: address within that address space
1622 * @len: length of the area to be checked
1623 * @is_write: indicates the transfer direction
1625 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1627 /* address_space_map: map a physical memory region into a host virtual address
1629 * May map a subset of the requested range, given by and returned in @plen.
1630 * May return %NULL if resources needed to perform the mapping are exhausted.
1631 * Use only for reads OR writes - not for read-modify-write operations.
1632 * Use cpu_register_map_client() to know when retrying the map operation is
1633 * likely to succeed.
1635 * @as: #AddressSpace to be accessed
1636 * @addr: address within that address space
1637 * @plen: pointer to length of buffer; updated on return
1638 * @is_write: indicates the transfer direction
1640 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1641 hwaddr
*plen
, bool is_write
);
1643 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1645 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1646 * the amount of memory that was actually read or written by the caller.
1648 * @as: #AddressSpace used
1649 * @addr: address within that address space
1650 * @len: buffer length as returned by address_space_map()
1651 * @access_len: amount of data actually transferred
1652 * @is_write: indicates the transfer direction
1654 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1655 int is_write
, hwaddr access_len
);
1658 /* Internal functions, part of the implementation of address_space_read. */
1659 MemTxResult
address_space_read_continue(AddressSpace
*as
, hwaddr addr
,
1660 MemTxAttrs attrs
, uint8_t *buf
,
1661 int len
, hwaddr addr1
, hwaddr l
,
1663 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
1664 MemTxAttrs attrs
, uint8_t *buf
, int len
);
1665 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
1667 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
1670 return memory_region_is_ram(mr
) &&
1671 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
1673 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
1674 memory_region_is_romd(mr
);
1679 * address_space_read: read from an address space.
1681 * Return a MemTxResult indicating whether the operation succeeded
1682 * or failed (eg unassigned memory, device rejected the transaction,
1685 * @as: #AddressSpace to be accessed
1686 * @addr: address within that address space
1687 * @attrs: memory transaction attributes
1688 * @buf: buffer with the data transferred
1690 static inline __attribute__((__always_inline__
))
1691 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1692 uint8_t *buf
, int len
)
1694 MemTxResult result
= MEMTX_OK
;
1699 if (__builtin_constant_p(len
)) {
1703 mr
= address_space_translate(as
, addr
, &addr1
, &l
, false);
1704 if (len
== l
&& memory_access_is_direct(mr
, false)) {
1705 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
1706 memcpy(buf
, ptr
, len
);
1708 result
= address_space_read_continue(as
, addr
, attrs
, buf
, len
,
1714 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
1720 * address_space_read_cached: read from a cached RAM region
1722 * @cache: Cached region to be addressed
1723 * @addr: address relative to the base of the RAM region
1724 * @buf: buffer with the data transferred
1725 * @len: length of the data transferred
1728 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1731 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1732 address_space_read(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);
1736 * address_space_write_cached: write to a cached RAM region
1738 * @cache: Cached region to be addressed
1739 * @addr: address relative to the base of the RAM region
1740 * @buf: buffer with the data transferred
1741 * @len: length of the data transferred
1744 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1747 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1748 address_space_write(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);