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)
80 struct IOMMUNotifier
{
81 void (*notify
)(struct IOMMUNotifier
*notifier
, IOMMUTLBEntry
*data
);
82 IOMMUNotifierFlag notifier_flags
;
83 QLIST_ENTRY(IOMMUNotifier
) node
;
85 typedef struct IOMMUNotifier IOMMUNotifier
;
87 /* New-style MMIO accessors can indicate that the transaction failed.
88 * A zero (MEMTX_OK) response means success; anything else is a failure
89 * of some kind. The memory subsystem will bitwise-OR together results
90 * if it is synthesizing an operation from multiple smaller accesses.
93 #define MEMTX_ERROR (1U << 0) /* device returned an error */
94 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
95 typedef uint32_t MemTxResult
;
98 * Memory region callbacks
100 struct MemoryRegionOps
{
101 /* Read from the memory region. @addr is relative to @mr; @size is
103 uint64_t (*read
)(void *opaque
,
106 /* Write to the memory region. @addr is relative to @mr; @size is
108 void (*write
)(void *opaque
,
113 MemTxResult (*read_with_attrs
)(void *opaque
,
118 MemTxResult (*write_with_attrs
)(void *opaque
,
124 enum device_endian endianness
;
125 /* Guest-visible constraints: */
127 /* If nonzero, specify bounds on access sizes beyond which a machine
130 unsigned min_access_size
;
131 unsigned max_access_size
;
132 /* If true, unaligned accesses are supported. Otherwise unaligned
133 * accesses throw machine checks.
137 * If present, and returns #false, the transaction is not accepted
138 * by the device (and results in machine dependent behaviour such
139 * as a machine check exception).
141 bool (*accepts
)(void *opaque
, hwaddr addr
,
142 unsigned size
, bool is_write
);
144 /* Internal implementation constraints: */
146 /* If nonzero, specifies the minimum size implemented. Smaller sizes
147 * will be rounded upwards and a partial result will be returned.
149 unsigned min_access_size
;
150 /* If nonzero, specifies the maximum size implemented. Larger sizes
151 * will be done as a series of accesses with smaller sizes.
153 unsigned max_access_size
;
154 /* If true, unaligned accesses are supported. Otherwise all accesses
155 * are converted to (possibly multiple) naturally aligned accesses.
160 /* If .read and .write are not present, old_mmio may be used for
161 * backwards compatibility with old mmio registration
163 const MemoryRegionMmio old_mmio
;
166 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps
;
168 struct MemoryRegionIOMMUOps
{
169 /* Return a TLB entry that contains a given address. */
170 IOMMUTLBEntry (*translate
)(MemoryRegion
*iommu
, hwaddr addr
, bool is_write
);
171 /* Returns minimum supported page size */
172 uint64_t (*get_min_page_size
)(MemoryRegion
*iommu
);
173 /* Called when IOMMU Notifier flag changed */
174 void (*notify_flag_changed
)(MemoryRegion
*iommu
,
175 IOMMUNotifierFlag old_flags
,
176 IOMMUNotifierFlag new_flags
);
179 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
180 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
182 struct MemoryRegion
{
185 /* All fields are private - violators will be prosecuted */
187 /* The following fields should fit in a cache line */
191 bool readonly
; /* For RAM regions */
193 bool flush_coalesced_mmio
;
195 uint8_t dirty_log_mask
;
198 const MemoryRegionIOMMUOps
*iommu_ops
;
200 const MemoryRegionOps
*ops
;
202 MemoryRegion
*container
;
205 void (*destructor
)(MemoryRegion
*mr
);
210 bool warning_printed
; /* For reservations */
211 uint8_t vga_logging_count
;
215 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
216 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
217 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
219 unsigned ioeventfd_nb
;
220 MemoryRegionIoeventfd
*ioeventfds
;
221 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
222 IOMMUNotifierFlag iommu_notify_flags
;
226 * MemoryListener: callbacks structure for updates to the physical memory map
228 * Allows a component to adjust to changes in the guest-visible memory map.
229 * Use with memory_listener_register() and memory_listener_unregister().
231 struct MemoryListener
{
232 void (*begin
)(MemoryListener
*listener
);
233 void (*commit
)(MemoryListener
*listener
);
234 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
235 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
236 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
237 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
239 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
241 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
242 void (*log_global_start
)(MemoryListener
*listener
);
243 void (*log_global_stop
)(MemoryListener
*listener
);
244 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
245 bool match_data
, uint64_t data
, EventNotifier
*e
);
246 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
247 bool match_data
, uint64_t data
, EventNotifier
*e
);
248 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
249 hwaddr addr
, hwaddr len
);
250 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
251 hwaddr addr
, hwaddr len
);
252 /* Lower = earlier (during add), later (during del) */
254 AddressSpace
*address_space
;
255 QTAILQ_ENTRY(MemoryListener
) link
;
256 QTAILQ_ENTRY(MemoryListener
) link_as
;
260 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
262 struct AddressSpace
{
263 /* All fields are private. */
270 /* Accessed via RCU. */
271 struct FlatView
*current_map
;
274 struct MemoryRegionIoeventfd
*ioeventfds
;
275 struct AddressSpaceDispatch
*dispatch
;
276 struct AddressSpaceDispatch
*next_dispatch
;
277 MemoryListener dispatch_listener
;
278 QTAILQ_HEAD(memory_listeners_as
, MemoryListener
) listeners
;
279 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
283 * MemoryRegionSection: describes a fragment of a #MemoryRegion
285 * @mr: the region, or %NULL if empty
286 * @address_space: the address space the region is mapped in
287 * @offset_within_region: the beginning of the section, relative to @mr's start
288 * @size: the size of the section; will not exceed @mr's boundaries
289 * @offset_within_address_space: the address of the first byte of the section
290 * relative to the region's address space
291 * @readonly: writes to this section are ignored
293 struct MemoryRegionSection
{
295 AddressSpace
*address_space
;
296 hwaddr offset_within_region
;
298 hwaddr offset_within_address_space
;
303 * memory_region_init: Initialize a memory region
305 * The region typically acts as a container for other memory regions. Use
306 * memory_region_add_subregion() to add subregions.
308 * @mr: the #MemoryRegion to be initialized
309 * @owner: the object that tracks the region's reference count
310 * @name: used for debugging; not visible to the user or ABI
311 * @size: size of the region; any subregions beyond this size will be clipped
313 void memory_region_init(MemoryRegion
*mr
,
314 struct Object
*owner
,
319 * memory_region_ref: Add 1 to a memory region's reference count
321 * Whenever memory regions are accessed outside the BQL, they need to be
322 * preserved against hot-unplug. MemoryRegions actually do not have their
323 * own reference count; they piggyback on a QOM object, their "owner".
324 * This function adds a reference to the owner.
326 * All MemoryRegions must have an owner if they can disappear, even if the
327 * device they belong to operates exclusively under the BQL. This is because
328 * the region could be returned at any time by memory_region_find, and this
329 * is usually under guest control.
331 * @mr: the #MemoryRegion
333 void memory_region_ref(MemoryRegion
*mr
);
336 * memory_region_unref: Remove 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 removes a reference to the owner and possibly destroys it.
343 * @mr: the #MemoryRegion
345 void memory_region_unref(MemoryRegion
*mr
);
348 * memory_region_init_io: Initialize an I/O memory region.
350 * Accesses into the region will cause the callbacks in @ops to be called.
351 * if @size is nonzero, subregions will be clipped to @size.
353 * @mr: the #MemoryRegion to be initialized.
354 * @owner: the object that tracks the region's reference count
355 * @ops: a structure containing read and write callbacks to be used when
356 * I/O is performed on the region.
357 * @opaque: passed to the read and write callbacks of the @ops structure.
358 * @name: used for debugging; not visible to the user or ABI
359 * @size: size of the region.
361 void memory_region_init_io(MemoryRegion
*mr
,
362 struct Object
*owner
,
363 const MemoryRegionOps
*ops
,
369 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
370 * region will modify memory directly.
372 * @mr: the #MemoryRegion to be initialized.
373 * @owner: the object that tracks the region's reference count
374 * @name: the name of the region.
375 * @size: size of the region.
376 * @errp: pointer to Error*, to store an error if it happens.
378 void memory_region_init_ram(MemoryRegion
*mr
,
379 struct Object
*owner
,
385 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
386 * RAM. Accesses into the region will
387 * modify memory directly. Only an initial
388 * portion of this RAM is actually used.
389 * The used size can change across reboots.
391 * @mr: the #MemoryRegion to be initialized.
392 * @owner: the object that tracks the region's reference count
393 * @name: the name of the region.
394 * @size: used size of the region.
395 * @max_size: max size of the region.
396 * @resized: callback to notify owner about used size change.
397 * @errp: pointer to Error*, to store an error if it happens.
399 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
400 struct Object
*owner
,
404 void (*resized
)(const char*,
410 * memory_region_init_ram_from_file: Initialize RAM memory region with a
413 * @mr: the #MemoryRegion to be initialized.
414 * @owner: the object that tracks the region's reference count
415 * @name: the name of the region.
416 * @size: size of the region.
417 * @share: %true if memory must be mmaped with the MAP_SHARED flag
418 * @path: the path in which to allocate the RAM.
419 * @errp: pointer to Error*, to store an error if it happens.
421 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
422 struct Object
*owner
,
431 * memory_region_init_ram_ptr: Initialize RAM memory region from a
432 * user-provided pointer. Accesses into the
433 * region will modify memory directly.
435 * @mr: the #MemoryRegion to be initialized.
436 * @owner: the object that tracks the region's reference count
437 * @name: the name of the region.
438 * @size: size of the region.
439 * @ptr: memory to be mapped; must contain at least @size bytes.
441 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
442 struct Object
*owner
,
448 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
449 * a user-provided pointer.
451 * A RAM device represents a mapping to a physical device, such as to a PCI
452 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
453 * into the VM address space and access to the region will modify memory
454 * directly. However, the memory region should not be included in a memory
455 * dump (device may not be enabled/mapped at the time of the dump), and
456 * operations incompatible with manipulating MMIO should be avoided. Replaces
459 * @mr: the #MemoryRegion to be initialized.
460 * @owner: the object that tracks the region's reference count
461 * @name: the name of the region.
462 * @size: size of the region.
463 * @ptr: memory to be mapped; must contain at least @size bytes.
465 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
466 struct Object
*owner
,
472 * memory_region_init_alias: Initialize a memory region that aliases all or a
473 * part of another memory region.
475 * @mr: the #MemoryRegion to be initialized.
476 * @owner: the object that tracks the region's reference count
477 * @name: used for debugging; not visible to the user or ABI
478 * @orig: the region to be referenced; @mr will be equivalent to
479 * @orig between @offset and @offset + @size - 1.
480 * @offset: start of the section in @orig to be referenced.
481 * @size: size of the region.
483 void memory_region_init_alias(MemoryRegion
*mr
,
484 struct Object
*owner
,
491 * memory_region_init_rom: Initialize a ROM memory region.
493 * This has the same effect as calling memory_region_init_ram()
494 * and then marking the resulting region read-only with
495 * memory_region_set_readonly().
497 * @mr: the #MemoryRegion to be initialized.
498 * @owner: the object that tracks the region's reference count
499 * @name: the name of the region.
500 * @size: size of the region.
501 * @errp: pointer to Error*, to store an error if it happens.
503 void memory_region_init_rom(MemoryRegion
*mr
,
504 struct Object
*owner
,
510 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
511 * handled via callbacks.
513 * @mr: the #MemoryRegion to be initialized.
514 * @owner: the object that tracks the region's reference count
515 * @ops: callbacks for write access handling (must not be NULL).
516 * @name: the name of the region.
517 * @size: size of the region.
518 * @errp: pointer to Error*, to store an error if it happens.
520 void memory_region_init_rom_device(MemoryRegion
*mr
,
521 struct Object
*owner
,
522 const MemoryRegionOps
*ops
,
529 * memory_region_init_reservation: Initialize a memory region that reserves
532 * A reservation region primariy serves debugging purposes. It claims I/O
533 * space that is not supposed to be handled by QEMU itself. Any access via
534 * the memory API will cause an abort().
535 * This function is deprecated. Use memory_region_init_io() with NULL
538 * @mr: the #MemoryRegion to be initialized
539 * @owner: the object that tracks the region's reference count
540 * @name: used for debugging; not visible to the user or ABI
541 * @size: size of the region.
543 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
548 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
552 * memory_region_init_iommu: Initialize a memory region that translates
555 * An IOMMU region translates addresses and forwards accesses to a target
558 * @mr: the #MemoryRegion to be initialized
559 * @owner: the object that tracks the region's reference count
560 * @ops: a function that translates addresses into the @target region
561 * @name: used for debugging; not visible to the user or ABI
562 * @size: size of the region.
564 void memory_region_init_iommu(MemoryRegion
*mr
,
565 struct Object
*owner
,
566 const MemoryRegionIOMMUOps
*ops
,
571 * memory_region_owner: get a memory region's owner.
573 * @mr: the memory region being queried.
575 struct Object
*memory_region_owner(MemoryRegion
*mr
);
578 * memory_region_size: get a memory region's size.
580 * @mr: the memory region being queried.
582 uint64_t memory_region_size(MemoryRegion
*mr
);
585 * memory_region_is_ram: check whether a memory region is random access
587 * Returns %true is a memory region is random access.
589 * @mr: the memory region being queried
591 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
597 * memory_region_is_ram_device: check whether a memory region is a ram device
599 * Returns %true is a memory region is a device backed ram region
601 * @mr: the memory region being queried
603 bool memory_region_is_ram_device(MemoryRegion
*mr
);
606 * memory_region_is_romd: check whether a memory region is in ROMD mode
608 * Returns %true if a memory region is a ROM device and currently set to allow
611 * @mr: the memory region being queried
613 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
615 return mr
->rom_device
&& mr
->romd_mode
;
619 * memory_region_is_iommu: check whether a memory region is an iommu
621 * Returns %true is a memory region is an iommu.
623 * @mr: the memory region being queried
625 static inline bool memory_region_is_iommu(MemoryRegion
*mr
)
628 return memory_region_is_iommu(mr
->alias
);
630 return mr
->iommu_ops
;
635 * memory_region_iommu_get_min_page_size: get minimum supported page size
638 * Returns minimum supported page size for an iommu.
640 * @mr: the memory region being queried
642 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion
*mr
);
645 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
647 * The notification type will be decided by entry.perm bits:
649 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
650 * - For MAP (newly added entry) notifies: set entry.perm to the
651 * permission of the page (which is definitely !IOMMU_NONE).
653 * Note: for any IOMMU implementation, an in-place mapping change
654 * should be notified with an UNMAP followed by a MAP.
656 * @mr: the memory region that was changed
657 * @entry: the new entry in the IOMMU translation table. The entry
658 * replaces all old entries for the same virtual I/O address range.
659 * Deleted entries have .@perm == 0.
661 void memory_region_notify_iommu(MemoryRegion
*mr
,
662 IOMMUTLBEntry entry
);
665 * memory_region_register_iommu_notifier: register a notifier for changes to
666 * IOMMU translation entries.
668 * @mr: the memory region to observe
669 * @n: the IOMMUNotifier to be added; the notify callback receives a
670 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
671 * ceases to be valid on exit from the notifier.
673 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
677 * memory_region_iommu_replay: replay existing IOMMU translations to
678 * a notifier with the minimum page granularity returned by
679 * mr->iommu_ops->get_page_size().
681 * @mr: the memory region to observe
682 * @n: the notifier to which to replay iommu mappings
683 * @is_write: Whether to treat the replay as a translate "write"
686 void memory_region_iommu_replay(MemoryRegion
*mr
, IOMMUNotifier
*n
,
690 * memory_region_unregister_iommu_notifier: unregister a notifier for
691 * changes to IOMMU translation entries.
693 * @mr: the memory region which was observed and for which notity_stopped()
695 * @n: the notifier to be removed.
697 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
701 * memory_region_name: get a memory region's name
703 * Returns the string that was used to initialize the memory region.
705 * @mr: the memory region being queried
707 const char *memory_region_name(const MemoryRegion
*mr
);
710 * memory_region_is_logging: return whether a memory region is logging writes
712 * Returns %true if the memory region is logging writes for the given client
714 * @mr: the memory region being queried
715 * @client: the client being queried
717 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
720 * memory_region_get_dirty_log_mask: return the clients for which a
721 * memory region is logging writes.
723 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
724 * are the bit indices.
726 * @mr: the memory region being queried
728 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
731 * memory_region_is_rom: check whether a memory region is ROM
733 * Returns %true is a memory region is read-only memory.
735 * @mr: the memory region being queried
737 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
739 return mr
->ram
&& mr
->readonly
;
744 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
746 * Returns a file descriptor backing a file-based RAM memory region,
747 * or -1 if the region is not a file-based RAM memory region.
749 * @mr: the RAM or alias memory region being queried.
751 int memory_region_get_fd(MemoryRegion
*mr
);
754 * memory_region_set_fd: Mark a RAM memory region as backed by a
757 * This function is typically used after memory_region_init_ram_ptr().
759 * @mr: the memory region being queried.
760 * @fd: the file descriptor that backs @mr.
762 void memory_region_set_fd(MemoryRegion
*mr
, int fd
);
765 * memory_region_from_host: Convert a pointer into a RAM memory region
766 * and an offset within it.
768 * Given a host pointer inside a RAM memory region (created with
769 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
770 * the MemoryRegion and the offset within it.
772 * Use with care; by the time this function returns, the returned pointer is
773 * not protected by RCU anymore. If the caller is not within an RCU critical
774 * section and does not hold the iothread lock, it must have other means of
775 * protecting the pointer, such as a reference to the region that includes
776 * the incoming ram_addr_t.
778 * @mr: the memory region being queried.
780 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
783 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
785 * Returns a host pointer to a RAM memory region (created with
786 * memory_region_init_ram() or memory_region_init_ram_ptr()).
788 * Use with care; by the time this function returns, the returned pointer is
789 * not protected by RCU anymore. If the caller is not within an RCU critical
790 * section and does not hold the iothread lock, it must have other means of
791 * protecting the pointer, such as a reference to the region that includes
792 * the incoming ram_addr_t.
794 * @mr: the memory region being queried.
796 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
798 /* memory_region_ram_resize: Resize a RAM region.
800 * Only legal before guest might have detected the memory size: e.g. on
801 * incoming migration, or right after reset.
803 * @mr: a memory region created with @memory_region_init_resizeable_ram.
804 * @newsize: the new size the region
805 * @errp: pointer to Error*, to store an error if it happens.
807 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
811 * memory_region_set_log: Turn dirty logging on or off for a region.
813 * Turns dirty logging on or off for a specified client (display, migration).
814 * Only meaningful for RAM regions.
816 * @mr: the memory region being updated.
817 * @log: whether dirty logging is to be enabled or disabled.
818 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
820 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
823 * memory_region_get_dirty: Check whether a range of bytes is dirty
824 * for a specified client.
826 * Checks whether a range of bytes has been written to since the last
827 * call to memory_region_reset_dirty() with the same @client. Dirty logging
830 * @mr: the memory region being queried.
831 * @addr: the address (relative to the start of the region) being queried.
832 * @size: the size of the range being queried.
833 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
836 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
837 hwaddr size
, unsigned client
);
840 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
842 * Marks a range of bytes as dirty, after it has been dirtied outside
845 * @mr: the memory region being dirtied.
846 * @addr: the address (relative to the start of the region) being dirtied.
847 * @size: size of the range being dirtied.
849 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
853 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
854 * for a specified client. It clears them.
856 * Checks whether a range of bytes has been written to since the last
857 * call to memory_region_reset_dirty() with the same @client. Dirty logging
860 * @mr: the memory region being queried.
861 * @addr: the address (relative to the start of the region) being queried.
862 * @size: the size of the range being queried.
863 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
866 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
867 hwaddr size
, unsigned client
);
869 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
870 * any external TLBs (e.g. kvm)
872 * Flushes dirty information from accelerators such as kvm and vhost-net
873 * and makes it available to users of the memory API.
875 * @mr: the region being flushed.
877 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
880 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
883 * Marks a range of pages as no longer dirty.
885 * @mr: the region being updated.
886 * @addr: the start of the subrange being cleaned.
887 * @size: the size of the subrange being cleaned.
888 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
891 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
892 hwaddr size
, unsigned client
);
895 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
897 * Allows a memory region to be marked as read-only (turning it into a ROM).
898 * only useful on RAM regions.
900 * @mr: the region being updated.
901 * @readonly: whether rhe region is to be ROM or RAM.
903 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
906 * memory_region_rom_device_set_romd: enable/disable ROMD mode
908 * Allows a ROM device (initialized with memory_region_init_rom_device() to
909 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
910 * device is mapped to guest memory and satisfies read access directly.
911 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
912 * Writes are always handled by the #MemoryRegion.write function.
914 * @mr: the memory region to be updated
915 * @romd_mode: %true to put the region into ROMD mode
917 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
920 * memory_region_set_coalescing: Enable memory coalescing for the region.
922 * Enabled writes to a region to be queued for later processing. MMIO ->write
923 * callbacks may be delayed until a non-coalesced MMIO is issued.
924 * Only useful for IO regions. Roughly similar to write-combining hardware.
926 * @mr: the memory region to be write coalesced
928 void memory_region_set_coalescing(MemoryRegion
*mr
);
931 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
934 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
935 * Multiple calls can be issued coalesced disjoint ranges.
937 * @mr: the memory region to be updated.
938 * @offset: the start of the range within the region to be coalesced.
939 * @size: the size of the subrange to be coalesced.
941 void memory_region_add_coalescing(MemoryRegion
*mr
,
946 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
948 * Disables any coalescing caused by memory_region_set_coalescing() or
949 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
952 * @mr: the memory region to be updated.
954 void memory_region_clear_coalescing(MemoryRegion
*mr
);
957 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
960 * Ensure that pending coalesced MMIO request are flushed before the memory
961 * region is accessed. This property is automatically enabled for all regions
962 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
964 * @mr: the memory region to be updated.
966 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
969 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
972 * Clear the automatic coalesced MMIO flushing enabled via
973 * memory_region_set_flush_coalesced. Note that this service has no effect on
974 * memory regions that have MMIO coalescing enabled for themselves. For them,
975 * automatic flushing will stop once coalescing is disabled.
977 * @mr: the memory region to be updated.
979 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
982 * memory_region_set_global_locking: Declares the access processing requires
983 * QEMU's global lock.
985 * When this is invoked, accesses to the memory region will be processed while
986 * holding the global lock of QEMU. This is the default behavior of memory
989 * @mr: the memory region to be updated.
991 void memory_region_set_global_locking(MemoryRegion
*mr
);
994 * memory_region_clear_global_locking: Declares that access processing does
995 * not depend on the QEMU global lock.
997 * By clearing this property, accesses to the memory region will be processed
998 * outside of QEMU's global lock (unless the lock is held on when issuing the
999 * access request). In this case, the device model implementing the access
1000 * handlers is responsible for synchronization of concurrency.
1002 * @mr: the memory region to be updated.
1004 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1007 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1008 * is written to a location.
1010 * Marks a word in an IO region (initialized with memory_region_init_io())
1011 * as a trigger for an eventfd event. The I/O callback will not be called.
1012 * The caller must be prepared to handle failure (that is, take the required
1013 * action if the callback _is_ called).
1015 * @mr: the memory region being updated.
1016 * @addr: the address within @mr that is to be monitored
1017 * @size: the size of the access to trigger the eventfd
1018 * @match_data: whether to match against @data, instead of just @addr
1019 * @data: the data to match against the guest write
1020 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1022 void memory_region_add_eventfd(MemoryRegion
*mr
,
1030 * memory_region_del_eventfd: Cancel an eventfd.
1032 * Cancels an eventfd trigger requested by a previous
1033 * memory_region_add_eventfd() call.
1035 * @mr: the memory region being updated.
1036 * @addr: the address within @mr that is to be monitored
1037 * @size: the size of the access to trigger the eventfd
1038 * @match_data: whether to match against @data, instead of just @addr
1039 * @data: the data to match against the guest write
1040 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1042 void memory_region_del_eventfd(MemoryRegion
*mr
,
1050 * memory_region_add_subregion: Add a subregion to a container.
1052 * Adds a subregion at @offset. The subregion may not overlap with other
1053 * subregions (except for those explicitly marked as overlapping). A region
1054 * may only be added once as a subregion (unless removed with
1055 * memory_region_del_subregion()); use memory_region_init_alias() if you
1056 * want a region to be a subregion in multiple locations.
1058 * @mr: the region to contain the new subregion; must be a container
1059 * initialized with memory_region_init().
1060 * @offset: the offset relative to @mr where @subregion is added.
1061 * @subregion: the subregion to be added.
1063 void memory_region_add_subregion(MemoryRegion
*mr
,
1065 MemoryRegion
*subregion
);
1067 * memory_region_add_subregion_overlap: Add a subregion to a container
1070 * Adds a subregion at @offset. The subregion may overlap with other
1071 * subregions. Conflicts are resolved by having a higher @priority hide a
1072 * lower @priority. Subregions without priority are taken as @priority 0.
1073 * A region may only be added once as a subregion (unless removed with
1074 * memory_region_del_subregion()); use memory_region_init_alias() if you
1075 * want a region to be a subregion in multiple locations.
1077 * @mr: the region to contain the new subregion; must be a container
1078 * initialized with memory_region_init().
1079 * @offset: the offset relative to @mr where @subregion is added.
1080 * @subregion: the subregion to be added.
1081 * @priority: used for resolving overlaps; highest priority wins.
1083 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1085 MemoryRegion
*subregion
,
1089 * memory_region_get_ram_addr: Get the ram address associated with a memory
1092 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1094 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1096 * memory_region_del_subregion: Remove a subregion.
1098 * Removes a subregion from its container.
1100 * @mr: the container to be updated.
1101 * @subregion: the region being removed; must be a current subregion of @mr.
1103 void memory_region_del_subregion(MemoryRegion
*mr
,
1104 MemoryRegion
*subregion
);
1107 * memory_region_set_enabled: dynamically enable or disable a region
1109 * Enables or disables a memory region. A disabled memory region
1110 * ignores all accesses to itself and its subregions. It does not
1111 * obscure sibling subregions with lower priority - it simply behaves as
1112 * if it was removed from the hierarchy.
1114 * Regions default to being enabled.
1116 * @mr: the region to be updated
1117 * @enabled: whether to enable or disable the region
1119 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1122 * memory_region_set_address: dynamically update the address of a region
1124 * Dynamically updates the address of a region, relative to its container.
1125 * May be used on regions are currently part of a memory hierarchy.
1127 * @mr: the region to be updated
1128 * @addr: new address, relative to container region
1130 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1133 * memory_region_set_size: dynamically update the size of a region.
1135 * Dynamically updates the size of a region.
1137 * @mr: the region to be updated
1138 * @size: used size of the region.
1140 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1143 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1145 * Dynamically updates the offset into the target region that an alias points
1146 * to, as if the fourth argument to memory_region_init_alias() has changed.
1148 * @mr: the #MemoryRegion to be updated; should be an alias.
1149 * @offset: the new offset into the target memory region
1151 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1155 * memory_region_present: checks if an address relative to a @container
1156 * translates into #MemoryRegion within @container
1158 * Answer whether a #MemoryRegion within @container covers the address
1161 * @container: a #MemoryRegion within which @addr is a relative address
1162 * @addr: the area within @container to be searched
1164 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1167 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1168 * into any address space.
1170 * @mr: a #MemoryRegion which should be checked if it's mapped
1172 bool memory_region_is_mapped(MemoryRegion
*mr
);
1175 * memory_region_find: translate an address/size relative to a
1176 * MemoryRegion into a #MemoryRegionSection.
1178 * Locates the first #MemoryRegion within @mr that overlaps the range
1179 * given by @addr and @size.
1181 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1182 * It will have the following characteristics:
1183 * .@size = 0 iff no overlap was found
1184 * .@mr is non-%NULL iff an overlap was found
1186 * Remember that in the return value the @offset_within_region is
1187 * relative to the returned region (in the .@mr field), not to the
1190 * Similarly, the .@offset_within_address_space is relative to the
1191 * address space that contains both regions, the passed and the
1192 * returned one. However, in the special case where the @mr argument
1193 * has no container (and thus is the root of the address space), the
1194 * following will hold:
1195 * .@offset_within_address_space >= @addr
1196 * .@offset_within_address_space + .@size <= @addr + @size
1198 * @mr: a MemoryRegion within which @addr is a relative address
1199 * @addr: start of the area within @as to be searched
1200 * @size: size of the area to be searched
1202 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1203 hwaddr addr
, uint64_t size
);
1206 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1208 * Synchronizes the dirty page log for all address spaces.
1210 void memory_global_dirty_log_sync(void);
1213 * memory_region_transaction_begin: Start a transaction.
1215 * During a transaction, changes will be accumulated and made visible
1216 * only when the transaction ends (is committed).
1218 void memory_region_transaction_begin(void);
1221 * memory_region_transaction_commit: Commit a transaction and make changes
1222 * visible to the guest.
1224 void memory_region_transaction_commit(void);
1227 * memory_listener_register: register callbacks to be called when memory
1228 * sections are mapped or unmapped into an address
1231 * @listener: an object containing the callbacks to be called
1232 * @filter: if non-%NULL, only regions in this address space will be observed
1234 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1237 * memory_listener_unregister: undo the effect of memory_listener_register()
1239 * @listener: an object containing the callbacks to be removed
1241 void memory_listener_unregister(MemoryListener
*listener
);
1244 * memory_global_dirty_log_start: begin dirty logging for all regions
1246 void memory_global_dirty_log_start(void);
1249 * memory_global_dirty_log_stop: end dirty logging for all regions
1251 void memory_global_dirty_log_stop(void);
1253 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
);
1256 * memory_region_dispatch_read: perform a read directly to the specified
1259 * @mr: #MemoryRegion to access
1260 * @addr: address within that region
1261 * @pval: pointer to uint64_t which the data is written to
1262 * @size: size of the access in bytes
1263 * @attrs: memory transaction attributes to use for the access
1265 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1271 * memory_region_dispatch_write: perform a write directly to the specified
1274 * @mr: #MemoryRegion to access
1275 * @addr: address within that region
1276 * @data: data to write
1277 * @size: size of the access in bytes
1278 * @attrs: memory transaction attributes to use for the access
1280 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1287 * address_space_init: initializes an address space
1289 * @as: an uninitialized #AddressSpace
1290 * @root: a #MemoryRegion that routes addresses for the address space
1291 * @name: an address space name. The name is only used for debugging
1294 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1297 * address_space_init_shareable: return an address space for a memory region,
1298 * creating it if it does not already exist
1300 * @root: a #MemoryRegion that routes addresses for the address space
1301 * @name: an address space name. The name is only used for debugging
1304 * This function will return a pointer to an existing AddressSpace
1305 * which was initialized with the specified MemoryRegion, or it will
1306 * create and initialize one if it does not already exist. The ASes
1307 * are reference-counted, so the memory will be freed automatically
1308 * when the AddressSpace is destroyed via address_space_destroy.
1310 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
,
1314 * address_space_destroy: destroy an address space
1316 * Releases all resources associated with an address space. After an address space
1317 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1320 * @as: address space to be destroyed
1322 void address_space_destroy(AddressSpace
*as
);
1325 * address_space_rw: read from or write to an address space.
1327 * Return a MemTxResult indicating whether the operation succeeded
1328 * or failed (eg unassigned memory, device rejected the transaction,
1331 * @as: #AddressSpace to be accessed
1332 * @addr: address within that address space
1333 * @attrs: memory transaction attributes
1334 * @buf: buffer with the data transferred
1335 * @is_write: indicates the transfer direction
1337 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1338 MemTxAttrs attrs
, uint8_t *buf
,
1339 int len
, bool is_write
);
1342 * address_space_write: write to address space.
1344 * Return a MemTxResult indicating whether the operation succeeded
1345 * or failed (eg unassigned memory, device rejected the transaction,
1348 * @as: #AddressSpace to be accessed
1349 * @addr: address within that address space
1350 * @attrs: memory transaction attributes
1351 * @buf: buffer with the data transferred
1353 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1355 const uint8_t *buf
, int len
);
1357 /* address_space_ld*: load from an address space
1358 * address_space_st*: store to an address space
1360 * These functions perform a load or store of the byte, word,
1361 * longword or quad to the specified address within the AddressSpace.
1362 * The _le suffixed functions treat the data as little endian;
1363 * _be indicates big endian; no suffix indicates "same endianness
1366 * The "guest CPU endianness" accessors are deprecated for use outside
1367 * target-* code; devices should be CPU-agnostic and use either the LE
1368 * or the BE accessors.
1370 * @as #AddressSpace to be accessed
1371 * @addr: address within that address space
1372 * @val: data value, for stores
1373 * @attrs: memory transaction attributes
1374 * @result: location to write the success/failure of the transaction;
1375 * if NULL, this information is discarded
1377 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1378 MemTxAttrs attrs
, MemTxResult
*result
);
1379 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1380 MemTxAttrs attrs
, MemTxResult
*result
);
1381 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1382 MemTxAttrs attrs
, MemTxResult
*result
);
1383 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1384 MemTxAttrs attrs
, MemTxResult
*result
);
1385 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1386 MemTxAttrs attrs
, MemTxResult
*result
);
1387 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1388 MemTxAttrs attrs
, MemTxResult
*result
);
1389 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1390 MemTxAttrs attrs
, MemTxResult
*result
);
1391 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1392 MemTxAttrs attrs
, MemTxResult
*result
);
1393 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1394 MemTxAttrs attrs
, MemTxResult
*result
);
1395 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1396 MemTxAttrs attrs
, MemTxResult
*result
);
1397 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1398 MemTxAttrs attrs
, MemTxResult
*result
);
1399 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1400 MemTxAttrs attrs
, MemTxResult
*result
);
1401 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1402 MemTxAttrs attrs
, MemTxResult
*result
);
1403 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1404 MemTxAttrs attrs
, MemTxResult
*result
);
1406 uint32_t ldub_phys(AddressSpace
*as
, hwaddr addr
);
1407 uint32_t lduw_le_phys(AddressSpace
*as
, hwaddr addr
);
1408 uint32_t lduw_be_phys(AddressSpace
*as
, hwaddr addr
);
1409 uint32_t ldl_le_phys(AddressSpace
*as
, hwaddr addr
);
1410 uint32_t ldl_be_phys(AddressSpace
*as
, hwaddr addr
);
1411 uint64_t ldq_le_phys(AddressSpace
*as
, hwaddr addr
);
1412 uint64_t ldq_be_phys(AddressSpace
*as
, hwaddr addr
);
1413 void stb_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1414 void stw_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1415 void stw_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1416 void stl_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1417 void stl_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1418 void stq_le_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1419 void stq_be_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1421 struct MemoryRegionCache
{
1429 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mr = NULL })
1431 /* address_space_cache_init: prepare for repeated access to a physical
1434 * @cache: #MemoryRegionCache to be filled
1435 * @as: #AddressSpace to be accessed
1436 * @addr: address within that address space
1437 * @len: length of buffer
1438 * @is_write: indicates the transfer direction
1440 * Will only work with RAM, and may map a subset of the requested range by
1441 * returning a value that is less than @len. On failure, return a negative
1444 * Because it only works with RAM, this function can be used for
1445 * read-modify-write operations. In this case, is_write should be %true.
1447 * Note that addresses passed to the address_space_*_cached functions
1448 * are relative to @addr.
1450 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1457 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1459 * @cache: The #MemoryRegionCache to operate on.
1460 * @addr: The first physical address that was written, relative to the
1461 * address that was passed to @address_space_cache_init.
1462 * @access_len: The number of bytes that were written starting at @addr.
1464 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1469 * address_space_cache_destroy: free a #MemoryRegionCache
1471 * @cache: The #MemoryRegionCache whose memory should be released.
1473 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1475 /* address_space_ld*_cached: load from a cached #MemoryRegion
1476 * address_space_st*_cached: store into a cached #MemoryRegion
1478 * These functions perform a load or store of the byte, word,
1479 * longword or quad to the specified address. The address is
1480 * a physical address in the AddressSpace, but it must lie within
1481 * a #MemoryRegion that was mapped with address_space_cache_init.
1483 * The _le suffixed functions treat the data as little endian;
1484 * _be indicates big endian; no suffix indicates "same endianness
1487 * The "guest CPU endianness" accessors are deprecated for use outside
1488 * target-* code; devices should be CPU-agnostic and use either the LE
1489 * or the BE accessors.
1491 * @cache: previously initialized #MemoryRegionCache to be accessed
1492 * @addr: address within the address space
1493 * @val: data value, for stores
1494 * @attrs: memory transaction attributes
1495 * @result: location to write the success/failure of the transaction;
1496 * if NULL, this information is discarded
1498 uint32_t address_space_ldub_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1499 MemTxAttrs attrs
, MemTxResult
*result
);
1500 uint32_t address_space_lduw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1501 MemTxAttrs attrs
, MemTxResult
*result
);
1502 uint32_t address_space_lduw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1503 MemTxAttrs attrs
, MemTxResult
*result
);
1504 uint32_t address_space_ldl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1505 MemTxAttrs attrs
, MemTxResult
*result
);
1506 uint32_t address_space_ldl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1507 MemTxAttrs attrs
, MemTxResult
*result
);
1508 uint64_t address_space_ldq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1509 MemTxAttrs attrs
, MemTxResult
*result
);
1510 uint64_t address_space_ldq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1511 MemTxAttrs attrs
, MemTxResult
*result
);
1512 void address_space_stb_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1513 MemTxAttrs attrs
, MemTxResult
*result
);
1514 void address_space_stw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1515 MemTxAttrs attrs
, MemTxResult
*result
);
1516 void address_space_stw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1517 MemTxAttrs attrs
, MemTxResult
*result
);
1518 void address_space_stl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1519 MemTxAttrs attrs
, MemTxResult
*result
);
1520 void address_space_stl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1521 MemTxAttrs attrs
, MemTxResult
*result
);
1522 void address_space_stq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1523 MemTxAttrs attrs
, MemTxResult
*result
);
1524 void address_space_stq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1525 MemTxAttrs attrs
, MemTxResult
*result
);
1527 uint32_t ldub_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1528 uint32_t lduw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1529 uint32_t lduw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1530 uint32_t ldl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1531 uint32_t ldl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1532 uint64_t ldq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1533 uint64_t ldq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1534 void stb_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1535 void stw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1536 void stw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1537 void stl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1538 void stl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1539 void stq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1540 void stq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1541 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1542 * entry. Should be called from an RCU critical section.
1544 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1547 /* address_space_translate: translate an address range into an address space
1548 * into a MemoryRegion and an address range into that section. Should be
1549 * called from an RCU critical section, to avoid that the last reference
1550 * to the returned region disappears after address_space_translate returns.
1552 * @as: #AddressSpace to be accessed
1553 * @addr: address within that address space
1554 * @xlat: pointer to address within the returned memory region section's
1556 * @len: pointer to length
1557 * @is_write: indicates the transfer direction
1559 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1560 hwaddr
*xlat
, hwaddr
*len
,
1563 /* address_space_access_valid: check for validity of accessing an address
1566 * Check whether memory is assigned to the given address space range, and
1567 * access is permitted by any IOMMU regions that are active for the address
1570 * For now, addr and len should be aligned to a page size. This limitation
1571 * will be lifted in the future.
1573 * @as: #AddressSpace to be accessed
1574 * @addr: address within that address space
1575 * @len: length of the area to be checked
1576 * @is_write: indicates the transfer direction
1578 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1580 /* address_space_map: map a physical memory region into a host virtual address
1582 * May map a subset of the requested range, given by and returned in @plen.
1583 * May return %NULL if resources needed to perform the mapping are exhausted.
1584 * Use only for reads OR writes - not for read-modify-write operations.
1585 * Use cpu_register_map_client() to know when retrying the map operation is
1586 * likely to succeed.
1588 * @as: #AddressSpace to be accessed
1589 * @addr: address within that address space
1590 * @plen: pointer to length of buffer; updated on return
1591 * @is_write: indicates the transfer direction
1593 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1594 hwaddr
*plen
, bool is_write
);
1596 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1598 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1599 * the amount of memory that was actually read or written by the caller.
1601 * @as: #AddressSpace used
1602 * @addr: address within that address space
1603 * @len: buffer length as returned by address_space_map()
1604 * @access_len: amount of data actually transferred
1605 * @is_write: indicates the transfer direction
1607 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1608 int is_write
, hwaddr access_len
);
1611 /* Internal functions, part of the implementation of address_space_read. */
1612 MemTxResult
address_space_read_continue(AddressSpace
*as
, hwaddr addr
,
1613 MemTxAttrs attrs
, uint8_t *buf
,
1614 int len
, hwaddr addr1
, hwaddr l
,
1616 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
1617 MemTxAttrs attrs
, uint8_t *buf
, int len
);
1618 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
1620 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
1623 return memory_region_is_ram(mr
) &&
1624 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
1626 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
1627 memory_region_is_romd(mr
);
1632 * address_space_read: read from an address space.
1634 * Return a MemTxResult indicating whether the operation succeeded
1635 * or failed (eg unassigned memory, device rejected the transaction,
1638 * @as: #AddressSpace to be accessed
1639 * @addr: address within that address space
1640 * @attrs: memory transaction attributes
1641 * @buf: buffer with the data transferred
1643 static inline __attribute__((__always_inline__
))
1644 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1645 uint8_t *buf
, int len
)
1647 MemTxResult result
= MEMTX_OK
;
1652 if (__builtin_constant_p(len
)) {
1656 mr
= address_space_translate(as
, addr
, &addr1
, &l
, false);
1657 if (len
== l
&& memory_access_is_direct(mr
, false)) {
1658 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
1659 memcpy(buf
, ptr
, len
);
1661 result
= address_space_read_continue(as
, addr
, attrs
, buf
, len
,
1667 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
1673 * address_space_read_cached: read from a cached RAM region
1675 * @cache: Cached region to be addressed
1676 * @addr: address relative to the base of the RAM region
1677 * @buf: buffer with the data transferred
1678 * @len: length of the data transferred
1681 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1684 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1685 memcpy(buf
, cache
->ptr
+ addr
, len
);
1689 * address_space_write_cached: write to a cached RAM region
1691 * @cache: Cached region to be addressed
1692 * @addr: address relative to the base of the RAM region
1693 * @buf: buffer with the data transferred
1694 * @len: length of the data transferred
1697 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1700 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1701 memcpy(cache
->ptr
+ addr
, buf
, len
);