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 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
60 struct IOMMUTLBEntry
{
61 AddressSpace
*target_as
;
63 hwaddr translated_addr
;
64 hwaddr addr_mask
; /* 0xfff = 4k translation */
65 IOMMUAccessFlags perm
;
69 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
70 * register with one or multiple IOMMU Notifier capability bit(s).
73 IOMMU_NOTIFIER_NONE
= 0,
74 /* Notify cache invalidations */
75 IOMMU_NOTIFIER_UNMAP
= 0x1,
76 /* Notify entry changes (newly created entries) */
77 IOMMU_NOTIFIER_MAP
= 0x2,
80 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
83 typedef void (*IOMMUNotify
)(struct IOMMUNotifier
*notifier
,
86 struct IOMMUNotifier
{
88 IOMMUNotifierFlag notifier_flags
;
89 /* Notify for address space range start <= addr <= end */
92 QLIST_ENTRY(IOMMUNotifier
) node
;
94 typedef struct IOMMUNotifier IOMMUNotifier
;
96 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
97 IOMMUNotifierFlag flags
,
98 hwaddr start
, hwaddr end
)
101 n
->notifier_flags
= flags
;
106 /* New-style MMIO accessors can indicate that the transaction failed.
107 * A zero (MEMTX_OK) response means success; anything else is a failure
108 * of some kind. The memory subsystem will bitwise-OR together results
109 * if it is synthesizing an operation from multiple smaller accesses.
112 #define MEMTX_ERROR (1U << 0) /* device returned an error */
113 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
114 typedef uint32_t MemTxResult
;
117 * Memory region callbacks
119 struct MemoryRegionOps
{
120 /* Read from the memory region. @addr is relative to @mr; @size is
122 uint64_t (*read
)(void *opaque
,
125 /* Write to the memory region. @addr is relative to @mr; @size is
127 void (*write
)(void *opaque
,
132 MemTxResult (*read_with_attrs
)(void *opaque
,
137 MemTxResult (*write_with_attrs
)(void *opaque
,
143 enum device_endian endianness
;
144 /* Guest-visible constraints: */
146 /* If nonzero, specify bounds on access sizes beyond which a machine
149 unsigned min_access_size
;
150 unsigned max_access_size
;
151 /* If true, unaligned accesses are supported. Otherwise unaligned
152 * accesses throw machine checks.
156 * If present, and returns #false, the transaction is not accepted
157 * by the device (and results in machine dependent behaviour such
158 * as a machine check exception).
160 bool (*accepts
)(void *opaque
, hwaddr addr
,
161 unsigned size
, bool is_write
);
163 /* Internal implementation constraints: */
165 /* If nonzero, specifies the minimum size implemented. Smaller sizes
166 * will be rounded upwards and a partial result will be returned.
168 unsigned min_access_size
;
169 /* If nonzero, specifies the maximum size implemented. Larger sizes
170 * will be done as a series of accesses with smaller sizes.
172 unsigned max_access_size
;
173 /* If true, unaligned accesses are supported. Otherwise all accesses
174 * are converted to (possibly multiple) naturally aligned accesses.
179 /* If .read and .write are not present, old_mmio may be used for
180 * backwards compatibility with old mmio registration
182 const MemoryRegionMmio old_mmio
;
185 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps
;
187 struct MemoryRegionIOMMUOps
{
189 * Return a TLB entry that contains a given address. Flag should
190 * be the access permission of this translation operation. We can
191 * set flag to IOMMU_NONE to mean that we don't need any
192 * read/write permission checks, like, when for region replay.
194 IOMMUTLBEntry (*translate
)(MemoryRegion
*iommu
, hwaddr addr
,
195 IOMMUAccessFlags flag
);
196 /* Returns minimum supported page size */
197 uint64_t (*get_min_page_size
)(MemoryRegion
*iommu
);
198 /* Called when IOMMU Notifier flag changed */
199 void (*notify_flag_changed
)(MemoryRegion
*iommu
,
200 IOMMUNotifierFlag old_flags
,
201 IOMMUNotifierFlag new_flags
);
202 /* Set this up to provide customized IOMMU replay function */
203 void (*replay
)(MemoryRegion
*iommu
, IOMMUNotifier
*notifier
);
206 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
207 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
209 struct MemoryRegion
{
212 /* All fields are private - violators will be prosecuted */
214 /* The following fields should fit in a cache line */
218 bool readonly
; /* For RAM regions */
220 bool flush_coalesced_mmio
;
222 uint8_t dirty_log_mask
;
225 const MemoryRegionIOMMUOps
*iommu_ops
;
227 const MemoryRegionOps
*ops
;
229 MemoryRegion
*container
;
232 void (*destructor
)(MemoryRegion
*mr
);
237 bool warning_printed
; /* For reservations */
238 uint8_t vga_logging_count
;
242 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
243 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
244 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
246 unsigned ioeventfd_nb
;
247 MemoryRegionIoeventfd
*ioeventfds
;
248 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
249 IOMMUNotifierFlag iommu_notify_flags
;
252 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
253 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
256 * MemoryListener: callbacks structure for updates to the physical memory map
258 * Allows a component to adjust to changes in the guest-visible memory map.
259 * Use with memory_listener_register() and memory_listener_unregister().
261 struct MemoryListener
{
262 void (*begin
)(MemoryListener
*listener
);
263 void (*commit
)(MemoryListener
*listener
);
264 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
265 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
266 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
267 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
269 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
271 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
272 void (*log_global_start
)(MemoryListener
*listener
);
273 void (*log_global_stop
)(MemoryListener
*listener
);
274 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
275 bool match_data
, uint64_t data
, EventNotifier
*e
);
276 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
277 bool match_data
, uint64_t data
, EventNotifier
*e
);
278 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
279 hwaddr addr
, hwaddr len
);
280 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
281 hwaddr addr
, hwaddr len
);
282 /* Lower = earlier (during add), later (during del) */
284 AddressSpace
*address_space
;
285 QTAILQ_ENTRY(MemoryListener
) link
;
286 QTAILQ_ENTRY(MemoryListener
) link_as
;
290 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
292 struct AddressSpace
{
293 /* All fields are private. */
300 /* Accessed via RCU. */
301 struct FlatView
*current_map
;
304 struct MemoryRegionIoeventfd
*ioeventfds
;
305 struct AddressSpaceDispatch
*dispatch
;
306 struct AddressSpaceDispatch
*next_dispatch
;
307 MemoryListener dispatch_listener
;
308 QTAILQ_HEAD(memory_listeners_as
, MemoryListener
) listeners
;
309 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
313 * MemoryRegionSection: describes a fragment of a #MemoryRegion
315 * @mr: the region, or %NULL if empty
316 * @address_space: the address space the region is mapped in
317 * @offset_within_region: the beginning of the section, relative to @mr's start
318 * @size: the size of the section; will not exceed @mr's boundaries
319 * @offset_within_address_space: the address of the first byte of the section
320 * relative to the region's address space
321 * @readonly: writes to this section are ignored
323 struct MemoryRegionSection
{
325 AddressSpace
*address_space
;
326 hwaddr offset_within_region
;
328 hwaddr offset_within_address_space
;
333 * memory_region_init: Initialize a memory region
335 * The region typically acts as a container for other memory regions. Use
336 * memory_region_add_subregion() to add subregions.
338 * @mr: the #MemoryRegion to be initialized
339 * @owner: the object that tracks the region's reference count
340 * @name: used for debugging; not visible to the user or ABI
341 * @size: size of the region; any subregions beyond this size will be clipped
343 void memory_region_init(MemoryRegion
*mr
,
344 struct Object
*owner
,
349 * memory_region_ref: Add 1 to a memory region's reference count
351 * Whenever memory regions are accessed outside the BQL, they need to be
352 * preserved against hot-unplug. MemoryRegions actually do not have their
353 * own reference count; they piggyback on a QOM object, their "owner".
354 * This function adds a reference to the owner.
356 * All MemoryRegions must have an owner if they can disappear, even if the
357 * device they belong to operates exclusively under the BQL. This is because
358 * the region could be returned at any time by memory_region_find, and this
359 * is usually under guest control.
361 * @mr: the #MemoryRegion
363 void memory_region_ref(MemoryRegion
*mr
);
366 * memory_region_unref: Remove 1 to a memory region's reference count
368 * Whenever memory regions are accessed outside the BQL, they need to be
369 * preserved against hot-unplug. MemoryRegions actually do not have their
370 * own reference count; they piggyback on a QOM object, their "owner".
371 * This function removes a reference to the owner and possibly destroys it.
373 * @mr: the #MemoryRegion
375 void memory_region_unref(MemoryRegion
*mr
);
378 * memory_region_init_io: Initialize an I/O memory region.
380 * Accesses into the region will cause the callbacks in @ops to be called.
381 * if @size is nonzero, subregions will be clipped to @size.
383 * @mr: the #MemoryRegion to be initialized.
384 * @owner: the object that tracks the region's reference count
385 * @ops: a structure containing read and write callbacks to be used when
386 * I/O is performed on the region.
387 * @opaque: passed to the read and write callbacks of the @ops structure.
388 * @name: used for debugging; not visible to the user or ABI
389 * @size: size of the region.
391 void memory_region_init_io(MemoryRegion
*mr
,
392 struct Object
*owner
,
393 const MemoryRegionOps
*ops
,
399 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
400 * region will modify memory directly.
402 * @mr: the #MemoryRegion to be initialized.
403 * @owner: the object that tracks the region's reference count
404 * @name: Region name, becomes part of RAMBlock name used in migration stream
405 * must be unique within any device
406 * @size: size of the region.
407 * @errp: pointer to Error*, to store an error if it happens.
409 void memory_region_init_ram(MemoryRegion
*mr
,
410 struct Object
*owner
,
416 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
417 * RAM. Accesses into the region will
418 * modify memory directly. Only an initial
419 * portion of this RAM is actually used.
420 * The used size can change across reboots.
422 * @mr: the #MemoryRegion to be initialized.
423 * @owner: the object that tracks the region's reference count
424 * @name: Region name, becomes part of RAMBlock name used in migration stream
425 * must be unique within any device
426 * @size: used size of the region.
427 * @max_size: max size of the region.
428 * @resized: callback to notify owner about used size change.
429 * @errp: pointer to Error*, to store an error if it happens.
431 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
432 struct Object
*owner
,
436 void (*resized
)(const char*,
442 * memory_region_init_ram_from_file: Initialize RAM memory region with a
445 * @mr: the #MemoryRegion to be initialized.
446 * @owner: the object that tracks the region's reference count
447 * @name: Region name, becomes part of RAMBlock name used in migration stream
448 * must be unique within any device
449 * @size: size of the region.
450 * @share: %true if memory must be mmaped with the MAP_SHARED flag
451 * @path: the path in which to allocate the RAM.
452 * @errp: pointer to Error*, to store an error if it happens.
454 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
455 struct Object
*owner
,
464 * memory_region_init_ram_ptr: Initialize RAM memory region from a
465 * user-provided pointer. Accesses into the
466 * region will modify memory directly.
468 * @mr: the #MemoryRegion to be initialized.
469 * @owner: the object that tracks the region's reference count
470 * @name: Region name, becomes part of RAMBlock name used in migration stream
471 * must be unique within any device
472 * @size: size of the region.
473 * @ptr: memory to be mapped; must contain at least @size bytes.
475 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
476 struct Object
*owner
,
482 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
483 * a user-provided pointer.
485 * A RAM device represents a mapping to a physical device, such as to a PCI
486 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
487 * into the VM address space and access to the region will modify memory
488 * directly. However, the memory region should not be included in a memory
489 * dump (device may not be enabled/mapped at the time of the dump), and
490 * operations incompatible with manipulating MMIO should be avoided. Replaces
493 * @mr: the #MemoryRegion to be initialized.
494 * @owner: the object that tracks the region's reference count
495 * @name: the name of the region.
496 * @size: size of the region.
497 * @ptr: memory to be mapped; must contain at least @size bytes.
499 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
500 struct Object
*owner
,
506 * memory_region_init_alias: Initialize a memory region that aliases all or a
507 * part of another memory region.
509 * @mr: the #MemoryRegion to be initialized.
510 * @owner: the object that tracks the region's reference count
511 * @name: used for debugging; not visible to the user or ABI
512 * @orig: the region to be referenced; @mr will be equivalent to
513 * @orig between @offset and @offset + @size - 1.
514 * @offset: start of the section in @orig to be referenced.
515 * @size: size of the region.
517 void memory_region_init_alias(MemoryRegion
*mr
,
518 struct Object
*owner
,
525 * memory_region_init_rom: Initialize a ROM memory region.
527 * This has the same effect as calling memory_region_init_ram()
528 * and then marking the resulting region read-only with
529 * memory_region_set_readonly().
531 * @mr: the #MemoryRegion to be initialized.
532 * @owner: the object that tracks the region's reference count
533 * @name: Region name, becomes part of RAMBlock name used in migration stream
534 * must be unique within any device
535 * @size: size of the region.
536 * @errp: pointer to Error*, to store an error if it happens.
538 void memory_region_init_rom(MemoryRegion
*mr
,
539 struct Object
*owner
,
545 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
546 * handled via callbacks.
548 * @mr: the #MemoryRegion to be initialized.
549 * @owner: the object that tracks the region's reference count
550 * @ops: callbacks for write access handling (must not be NULL).
551 * @name: Region name, becomes part of RAMBlock name used in migration stream
552 * must be unique within any device
553 * @size: size of the region.
554 * @errp: pointer to Error*, to store an error if it happens.
556 void memory_region_init_rom_device(MemoryRegion
*mr
,
557 struct Object
*owner
,
558 const MemoryRegionOps
*ops
,
565 * memory_region_init_reservation: Initialize a memory region that reserves
568 * A reservation region primariy serves debugging purposes. It claims I/O
569 * space that is not supposed to be handled by QEMU itself. Any access via
570 * the memory API will cause an abort().
571 * This function is deprecated. Use memory_region_init_io() with NULL
574 * @mr: the #MemoryRegion to be initialized
575 * @owner: the object that tracks the region's reference count
576 * @name: used for debugging; not visible to the user or ABI
577 * @size: size of the region.
579 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
584 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
588 * memory_region_init_iommu: Initialize a memory region that translates
591 * An IOMMU region translates addresses and forwards accesses to a target
594 * @mr: the #MemoryRegion to be initialized
595 * @owner: the object that tracks the region's reference count
596 * @ops: a function that translates addresses into the @target region
597 * @name: used for debugging; not visible to the user or ABI
598 * @size: size of the region.
600 void memory_region_init_iommu(MemoryRegion
*mr
,
601 struct Object
*owner
,
602 const MemoryRegionIOMMUOps
*ops
,
607 * memory_region_owner: get a memory region's owner.
609 * @mr: the memory region being queried.
611 struct Object
*memory_region_owner(MemoryRegion
*mr
);
614 * memory_region_size: get a memory region's size.
616 * @mr: the memory region being queried.
618 uint64_t memory_region_size(MemoryRegion
*mr
);
621 * memory_region_is_ram: check whether a memory region is random access
623 * Returns %true is a memory region is random access.
625 * @mr: the memory region being queried
627 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
633 * memory_region_is_ram_device: check whether a memory region is a ram device
635 * Returns %true is a memory region is a device backed ram region
637 * @mr: the memory region being queried
639 bool memory_region_is_ram_device(MemoryRegion
*mr
);
642 * memory_region_is_romd: check whether a memory region is in ROMD mode
644 * Returns %true if a memory region is a ROM device and currently set to allow
647 * @mr: the memory region being queried
649 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
651 return mr
->rom_device
&& mr
->romd_mode
;
655 * memory_region_is_iommu: check whether a memory region is an iommu
657 * Returns %true is a memory region is an iommu.
659 * @mr: the memory region being queried
661 static inline bool memory_region_is_iommu(MemoryRegion
*mr
)
664 return memory_region_is_iommu(mr
->alias
);
666 return mr
->iommu_ops
;
671 * memory_region_iommu_get_min_page_size: get minimum supported page size
674 * Returns minimum supported page size for an iommu.
676 * @mr: the memory region being queried
678 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion
*mr
);
681 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
683 * The notification type will be decided by entry.perm bits:
685 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
686 * - For MAP (newly added entry) notifies: set entry.perm to the
687 * permission of the page (which is definitely !IOMMU_NONE).
689 * Note: for any IOMMU implementation, an in-place mapping change
690 * should be notified with an UNMAP followed by a MAP.
692 * @mr: the memory region that was changed
693 * @entry: the new entry in the IOMMU translation table. The entry
694 * replaces all old entries for the same virtual I/O address range.
695 * Deleted entries have .@perm == 0.
697 void memory_region_notify_iommu(MemoryRegion
*mr
,
698 IOMMUTLBEntry entry
);
701 * memory_region_notify_one: notify a change in an IOMMU translation
702 * entry to a single notifier
704 * This works just like memory_region_notify_iommu(), but it only
705 * notifies a specific notifier, not all of them.
707 * @notifier: the notifier to be notified
708 * @entry: the new entry in the IOMMU translation table. The entry
709 * replaces all old entries for the same virtual I/O address range.
710 * Deleted entries have .@perm == 0.
712 void memory_region_notify_one(IOMMUNotifier
*notifier
,
713 IOMMUTLBEntry
*entry
);
716 * memory_region_register_iommu_notifier: register a notifier for changes to
717 * IOMMU translation entries.
719 * @mr: the memory region to observe
720 * @n: the IOMMUNotifier to be added; the notify callback receives a
721 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
722 * ceases to be valid on exit from the notifier.
724 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
728 * memory_region_iommu_replay: replay existing IOMMU translations to
729 * a notifier with the minimum page granularity returned by
730 * mr->iommu_ops->get_page_size().
732 * @mr: the memory region to observe
733 * @n: the notifier to which to replay iommu mappings
735 void memory_region_iommu_replay(MemoryRegion
*mr
, IOMMUNotifier
*n
);
738 * memory_region_iommu_replay_all: replay existing IOMMU translations
739 * to all the notifiers registered.
741 * @mr: the memory region to observe
743 void memory_region_iommu_replay_all(MemoryRegion
*mr
);
746 * memory_region_unregister_iommu_notifier: unregister a notifier for
747 * changes to IOMMU translation entries.
749 * @mr: the memory region which was observed and for which notity_stopped()
751 * @n: the notifier to be removed.
753 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
757 * memory_region_name: get a memory region's name
759 * Returns the string that was used to initialize the memory region.
761 * @mr: the memory region being queried
763 const char *memory_region_name(const MemoryRegion
*mr
);
766 * memory_region_is_logging: return whether a memory region is logging writes
768 * Returns %true if the memory region is logging writes for the given client
770 * @mr: the memory region being queried
771 * @client: the client being queried
773 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
776 * memory_region_get_dirty_log_mask: return the clients for which a
777 * memory region is logging writes.
779 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
780 * are the bit indices.
782 * @mr: the memory region being queried
784 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
787 * memory_region_is_rom: check whether a memory region is ROM
789 * Returns %true is a memory region is read-only memory.
791 * @mr: the memory region being queried
793 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
795 return mr
->ram
&& mr
->readonly
;
800 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
802 * Returns a file descriptor backing a file-based RAM memory region,
803 * or -1 if the region is not a file-based RAM memory region.
805 * @mr: the RAM or alias memory region being queried.
807 int memory_region_get_fd(MemoryRegion
*mr
);
810 * memory_region_set_fd: Mark a RAM memory region as backed by a
813 * This function is typically used after memory_region_init_ram_ptr().
815 * @mr: the memory region being queried.
816 * @fd: the file descriptor that backs @mr.
818 void memory_region_set_fd(MemoryRegion
*mr
, int fd
);
821 * memory_region_from_host: Convert a pointer into a RAM memory region
822 * and an offset within it.
824 * Given a host pointer inside a RAM memory region (created with
825 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
826 * the MemoryRegion and the offset within it.
828 * Use with care; by the time this function returns, the returned pointer is
829 * not protected by RCU anymore. If the caller is not within an RCU critical
830 * section and does not hold the iothread lock, it must have other means of
831 * protecting the pointer, such as a reference to the region that includes
832 * the incoming ram_addr_t.
834 * @mr: the memory region being queried.
836 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
839 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
841 * Returns a host pointer to a RAM memory region (created with
842 * memory_region_init_ram() or memory_region_init_ram_ptr()).
844 * Use with care; by the time this function returns, the returned pointer is
845 * not protected by RCU anymore. If the caller is not within an RCU critical
846 * section and does not hold the iothread lock, it must have other means of
847 * protecting the pointer, such as a reference to the region that includes
848 * the incoming ram_addr_t.
850 * @mr: the memory region being queried.
852 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
854 /* memory_region_ram_resize: Resize a RAM region.
856 * Only legal before guest might have detected the memory size: e.g. on
857 * incoming migration, or right after reset.
859 * @mr: a memory region created with @memory_region_init_resizeable_ram.
860 * @newsize: the new size the region
861 * @errp: pointer to Error*, to store an error if it happens.
863 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
867 * memory_region_set_log: Turn dirty logging on or off for a region.
869 * Turns dirty logging on or off for a specified client (display, migration).
870 * Only meaningful for RAM regions.
872 * @mr: the memory region being updated.
873 * @log: whether dirty logging is to be enabled or disabled.
874 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
876 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
879 * memory_region_get_dirty: Check whether a range of bytes is dirty
880 * for a specified client.
882 * Checks whether a range of bytes has been written to since the last
883 * call to memory_region_reset_dirty() with the same @client. Dirty logging
886 * @mr: the memory region being queried.
887 * @addr: the address (relative to the start of the region) being queried.
888 * @size: the size of the range being queried.
889 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
892 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
893 hwaddr size
, unsigned client
);
896 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
898 * Marks a range of bytes as dirty, after it has been dirtied outside
901 * @mr: the memory region being dirtied.
902 * @addr: the address (relative to the start of the region) being dirtied.
903 * @size: size of the range being dirtied.
905 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
909 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
910 * for a specified client. It clears them.
912 * Checks whether a range of bytes has been written to since the last
913 * call to memory_region_reset_dirty() with the same @client. Dirty logging
916 * @mr: the memory region being queried.
917 * @addr: the address (relative to the start of the region) being queried.
918 * @size: the size of the range being queried.
919 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
922 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
923 hwaddr size
, unsigned client
);
926 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
927 * bitmap and clear it.
929 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
930 * returns the snapshot. The snapshot can then be used to query dirty
931 * status, using memory_region_snapshot_get_dirty. Unlike
932 * memory_region_test_and_clear_dirty this allows to query the same
933 * page multiple times, which is especially useful for display updates
934 * where the scanlines often are not page aligned.
936 * The dirty bitmap region which gets copyed into the snapshot (and
937 * cleared afterwards) can be larger than requested. The boundaries
938 * are rounded up/down so complete bitmap longs (covering 64 pages on
939 * 64bit hosts) can be copied over into the bitmap snapshot. Which
940 * isn't a problem for display updates as the extra pages are outside
941 * the visible area, and in case the visible area changes a full
942 * display redraw is due anyway. Should other use cases for this
943 * function emerge we might have to revisit this implementation
946 * Use g_free to release DirtyBitmapSnapshot.
948 * @mr: the memory region being queried.
949 * @addr: the address (relative to the start of the region) being queried.
950 * @size: the size of the range being queried.
951 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
953 DirtyBitmapSnapshot
*memory_region_snapshot_and_clear_dirty(MemoryRegion
*mr
,
959 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
960 * in the specified dirty bitmap snapshot.
962 * @mr: the memory region being queried.
963 * @snap: the dirty bitmap snapshot
964 * @addr: the address (relative to the start of the region) being queried.
965 * @size: the size of the range being queried.
967 bool memory_region_snapshot_get_dirty(MemoryRegion
*mr
,
968 DirtyBitmapSnapshot
*snap
,
969 hwaddr addr
, hwaddr size
);
972 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
973 * any external TLBs (e.g. kvm)
975 * Flushes dirty information from accelerators such as kvm and vhost-net
976 * and makes it available to users of the memory API.
978 * @mr: the region being flushed.
980 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
983 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
986 * Marks a range of pages as no longer dirty.
988 * @mr: the region being updated.
989 * @addr: the start of the subrange being cleaned.
990 * @size: the size of the subrange being cleaned.
991 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
994 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
995 hwaddr size
, unsigned client
);
998 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1000 * Allows a memory region to be marked as read-only (turning it into a ROM).
1001 * only useful on RAM regions.
1003 * @mr: the region being updated.
1004 * @readonly: whether rhe region is to be ROM or RAM.
1006 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
1009 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1011 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1012 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1013 * device is mapped to guest memory and satisfies read access directly.
1014 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1015 * Writes are always handled by the #MemoryRegion.write function.
1017 * @mr: the memory region to be updated
1018 * @romd_mode: %true to put the region into ROMD mode
1020 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
1023 * memory_region_set_coalescing: Enable memory coalescing for the region.
1025 * Enabled writes to a region to be queued for later processing. MMIO ->write
1026 * callbacks may be delayed until a non-coalesced MMIO is issued.
1027 * Only useful for IO regions. Roughly similar to write-combining hardware.
1029 * @mr: the memory region to be write coalesced
1031 void memory_region_set_coalescing(MemoryRegion
*mr
);
1034 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1037 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1038 * Multiple calls can be issued coalesced disjoint ranges.
1040 * @mr: the memory region to be updated.
1041 * @offset: the start of the range within the region to be coalesced.
1042 * @size: the size of the subrange to be coalesced.
1044 void memory_region_add_coalescing(MemoryRegion
*mr
,
1049 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1051 * Disables any coalescing caused by memory_region_set_coalescing() or
1052 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1055 * @mr: the memory region to be updated.
1057 void memory_region_clear_coalescing(MemoryRegion
*mr
);
1060 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1063 * Ensure that pending coalesced MMIO request are flushed before the memory
1064 * region is accessed. This property is automatically enabled for all regions
1065 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1067 * @mr: the memory region to be updated.
1069 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
1072 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1075 * Clear the automatic coalesced MMIO flushing enabled via
1076 * memory_region_set_flush_coalesced. Note that this service has no effect on
1077 * memory regions that have MMIO coalescing enabled for themselves. For them,
1078 * automatic flushing will stop once coalescing is disabled.
1080 * @mr: the memory region to be updated.
1082 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1085 * memory_region_set_global_locking: Declares the access processing requires
1086 * QEMU's global lock.
1088 * When this is invoked, accesses to the memory region will be processed while
1089 * holding the global lock of QEMU. This is the default behavior of memory
1092 * @mr: the memory region to be updated.
1094 void memory_region_set_global_locking(MemoryRegion
*mr
);
1097 * memory_region_clear_global_locking: Declares that access processing does
1098 * not depend on the QEMU global lock.
1100 * By clearing this property, accesses to the memory region will be processed
1101 * outside of QEMU's global lock (unless the lock is held on when issuing the
1102 * access request). In this case, the device model implementing the access
1103 * handlers is responsible for synchronization of concurrency.
1105 * @mr: the memory region to be updated.
1107 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1110 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1111 * is written to a location.
1113 * Marks a word in an IO region (initialized with memory_region_init_io())
1114 * as a trigger for an eventfd event. The I/O callback will not be called.
1115 * The caller must be prepared to handle failure (that is, take the required
1116 * action if the callback _is_ called).
1118 * @mr: the memory region being updated.
1119 * @addr: the address within @mr that is to be monitored
1120 * @size: the size of the access to trigger the eventfd
1121 * @match_data: whether to match against @data, instead of just @addr
1122 * @data: the data to match against the guest write
1123 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1125 void memory_region_add_eventfd(MemoryRegion
*mr
,
1133 * memory_region_del_eventfd: Cancel an eventfd.
1135 * Cancels an eventfd trigger requested by a previous
1136 * memory_region_add_eventfd() call.
1138 * @mr: the memory region being updated.
1139 * @addr: the address within @mr that is to be monitored
1140 * @size: the size of the access to trigger the eventfd
1141 * @match_data: whether to match against @data, instead of just @addr
1142 * @data: the data to match against the guest write
1143 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1145 void memory_region_del_eventfd(MemoryRegion
*mr
,
1153 * memory_region_add_subregion: Add a subregion to a container.
1155 * Adds a subregion at @offset. The subregion may not overlap with other
1156 * subregions (except for those explicitly marked as overlapping). A region
1157 * may only be added once as a subregion (unless removed with
1158 * memory_region_del_subregion()); use memory_region_init_alias() if you
1159 * want a region to be a subregion in multiple locations.
1161 * @mr: the region to contain the new subregion; must be a container
1162 * initialized with memory_region_init().
1163 * @offset: the offset relative to @mr where @subregion is added.
1164 * @subregion: the subregion to be added.
1166 void memory_region_add_subregion(MemoryRegion
*mr
,
1168 MemoryRegion
*subregion
);
1170 * memory_region_add_subregion_overlap: Add a subregion to a container
1173 * Adds a subregion at @offset. The subregion may overlap with other
1174 * subregions. Conflicts are resolved by having a higher @priority hide a
1175 * lower @priority. Subregions without priority are taken as @priority 0.
1176 * A region may only be added once as a subregion (unless removed with
1177 * memory_region_del_subregion()); use memory_region_init_alias() if you
1178 * want a region to be a subregion in multiple locations.
1180 * @mr: the region to contain the new subregion; must be a container
1181 * initialized with memory_region_init().
1182 * @offset: the offset relative to @mr where @subregion is added.
1183 * @subregion: the subregion to be added.
1184 * @priority: used for resolving overlaps; highest priority wins.
1186 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1188 MemoryRegion
*subregion
,
1192 * memory_region_get_ram_addr: Get the ram address associated with a memory
1195 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1197 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1199 * memory_region_del_subregion: Remove a subregion.
1201 * Removes a subregion from its container.
1203 * @mr: the container to be updated.
1204 * @subregion: the region being removed; must be a current subregion of @mr.
1206 void memory_region_del_subregion(MemoryRegion
*mr
,
1207 MemoryRegion
*subregion
);
1210 * memory_region_set_enabled: dynamically enable or disable a region
1212 * Enables or disables a memory region. A disabled memory region
1213 * ignores all accesses to itself and its subregions. It does not
1214 * obscure sibling subregions with lower priority - it simply behaves as
1215 * if it was removed from the hierarchy.
1217 * Regions default to being enabled.
1219 * @mr: the region to be updated
1220 * @enabled: whether to enable or disable the region
1222 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1225 * memory_region_set_address: dynamically update the address of a region
1227 * Dynamically updates the address of a region, relative to its container.
1228 * May be used on regions are currently part of a memory hierarchy.
1230 * @mr: the region to be updated
1231 * @addr: new address, relative to container region
1233 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1236 * memory_region_set_size: dynamically update the size of a region.
1238 * Dynamically updates the size of a region.
1240 * @mr: the region to be updated
1241 * @size: used size of the region.
1243 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1246 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1248 * Dynamically updates the offset into the target region that an alias points
1249 * to, as if the fourth argument to memory_region_init_alias() has changed.
1251 * @mr: the #MemoryRegion to be updated; should be an alias.
1252 * @offset: the new offset into the target memory region
1254 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1258 * memory_region_present: checks if an address relative to a @container
1259 * translates into #MemoryRegion within @container
1261 * Answer whether a #MemoryRegion within @container covers the address
1264 * @container: a #MemoryRegion within which @addr is a relative address
1265 * @addr: the area within @container to be searched
1267 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1270 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1271 * into any address space.
1273 * @mr: a #MemoryRegion which should be checked if it's mapped
1275 bool memory_region_is_mapped(MemoryRegion
*mr
);
1278 * memory_region_find: translate an address/size relative to a
1279 * MemoryRegion into a #MemoryRegionSection.
1281 * Locates the first #MemoryRegion within @mr that overlaps the range
1282 * given by @addr and @size.
1284 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1285 * It will have the following characteristics:
1286 * .@size = 0 iff no overlap was found
1287 * .@mr is non-%NULL iff an overlap was found
1289 * Remember that in the return value the @offset_within_region is
1290 * relative to the returned region (in the .@mr field), not to the
1293 * Similarly, the .@offset_within_address_space is relative to the
1294 * address space that contains both regions, the passed and the
1295 * returned one. However, in the special case where the @mr argument
1296 * has no container (and thus is the root of the address space), the
1297 * following will hold:
1298 * .@offset_within_address_space >= @addr
1299 * .@offset_within_address_space + .@size <= @addr + @size
1301 * @mr: a MemoryRegion within which @addr is a relative address
1302 * @addr: start of the area within @as to be searched
1303 * @size: size of the area to be searched
1305 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1306 hwaddr addr
, uint64_t size
);
1309 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1311 * Synchronizes the dirty page log for all address spaces.
1313 void memory_global_dirty_log_sync(void);
1316 * memory_region_transaction_begin: Start a transaction.
1318 * During a transaction, changes will be accumulated and made visible
1319 * only when the transaction ends (is committed).
1321 void memory_region_transaction_begin(void);
1324 * memory_region_transaction_commit: Commit a transaction and make changes
1325 * visible to the guest.
1327 void memory_region_transaction_commit(void);
1330 * memory_listener_register: register callbacks to be called when memory
1331 * sections are mapped or unmapped into an address
1334 * @listener: an object containing the callbacks to be called
1335 * @filter: if non-%NULL, only regions in this address space will be observed
1337 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1340 * memory_listener_unregister: undo the effect of memory_listener_register()
1342 * @listener: an object containing the callbacks to be removed
1344 void memory_listener_unregister(MemoryListener
*listener
);
1347 * memory_global_dirty_log_start: begin dirty logging for all regions
1349 void memory_global_dirty_log_start(void);
1352 * memory_global_dirty_log_stop: end dirty logging for all regions
1354 void memory_global_dirty_log_stop(void);
1356 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
);
1359 * memory_region_dispatch_read: perform a read directly to the specified
1362 * @mr: #MemoryRegion to access
1363 * @addr: address within that region
1364 * @pval: pointer to uint64_t which the data is written to
1365 * @size: size of the access in bytes
1366 * @attrs: memory transaction attributes to use for the access
1368 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1374 * memory_region_dispatch_write: perform a write directly to the specified
1377 * @mr: #MemoryRegion to access
1378 * @addr: address within that region
1379 * @data: data to write
1380 * @size: size of the access in bytes
1381 * @attrs: memory transaction attributes to use for the access
1383 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1390 * address_space_init: initializes an address space
1392 * @as: an uninitialized #AddressSpace
1393 * @root: a #MemoryRegion that routes addresses for the address space
1394 * @name: an address space name. The name is only used for debugging
1397 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1400 * address_space_init_shareable: return an address space for a memory region,
1401 * creating it if it does not already exist
1403 * @root: a #MemoryRegion that routes addresses for the address space
1404 * @name: an address space name. The name is only used for debugging
1407 * This function will return a pointer to an existing AddressSpace
1408 * which was initialized with the specified MemoryRegion, or it will
1409 * create and initialize one if it does not already exist. The ASes
1410 * are reference-counted, so the memory will be freed automatically
1411 * when the AddressSpace is destroyed via address_space_destroy.
1413 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
,
1417 * address_space_destroy: destroy an address space
1419 * Releases all resources associated with an address space. After an address space
1420 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1423 * @as: address space to be destroyed
1425 void address_space_destroy(AddressSpace
*as
);
1428 * address_space_rw: read from or write to an address space.
1430 * Return a MemTxResult indicating whether the operation succeeded
1431 * or failed (eg unassigned memory, device rejected the transaction,
1434 * @as: #AddressSpace to be accessed
1435 * @addr: address within that address space
1436 * @attrs: memory transaction attributes
1437 * @buf: buffer with the data transferred
1438 * @is_write: indicates the transfer direction
1440 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1441 MemTxAttrs attrs
, uint8_t *buf
,
1442 int len
, bool is_write
);
1445 * address_space_write: write to address space.
1447 * Return a MemTxResult indicating whether the operation succeeded
1448 * or failed (eg unassigned memory, device rejected the transaction,
1451 * @as: #AddressSpace to be accessed
1452 * @addr: address within that address space
1453 * @attrs: memory transaction attributes
1454 * @buf: buffer with the data transferred
1456 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1458 const uint8_t *buf
, int len
);
1460 /* address_space_ld*: load from an address space
1461 * address_space_st*: store to an address space
1463 * These functions perform a load or store of the byte, word,
1464 * longword or quad to the specified address within the AddressSpace.
1465 * The _le suffixed functions treat the data as little endian;
1466 * _be indicates big endian; no suffix indicates "same endianness
1469 * The "guest CPU endianness" accessors are deprecated for use outside
1470 * target-* code; devices should be CPU-agnostic and use either the LE
1471 * or the BE accessors.
1473 * @as #AddressSpace to be accessed
1474 * @addr: address within that address space
1475 * @val: data value, for stores
1476 * @attrs: memory transaction attributes
1477 * @result: location to write the success/failure of the transaction;
1478 * if NULL, this information is discarded
1480 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1481 MemTxAttrs attrs
, MemTxResult
*result
);
1482 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1483 MemTxAttrs attrs
, MemTxResult
*result
);
1484 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1485 MemTxAttrs attrs
, MemTxResult
*result
);
1486 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1487 MemTxAttrs attrs
, MemTxResult
*result
);
1488 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1489 MemTxAttrs attrs
, MemTxResult
*result
);
1490 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1491 MemTxAttrs attrs
, MemTxResult
*result
);
1492 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1493 MemTxAttrs attrs
, MemTxResult
*result
);
1494 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1495 MemTxAttrs attrs
, MemTxResult
*result
);
1496 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1497 MemTxAttrs attrs
, MemTxResult
*result
);
1498 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1499 MemTxAttrs attrs
, MemTxResult
*result
);
1500 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1501 MemTxAttrs attrs
, MemTxResult
*result
);
1502 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1503 MemTxAttrs attrs
, MemTxResult
*result
);
1504 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1505 MemTxAttrs attrs
, MemTxResult
*result
);
1506 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1507 MemTxAttrs attrs
, MemTxResult
*result
);
1509 uint32_t ldub_phys(AddressSpace
*as
, hwaddr addr
);
1510 uint32_t lduw_le_phys(AddressSpace
*as
, hwaddr addr
);
1511 uint32_t lduw_be_phys(AddressSpace
*as
, hwaddr addr
);
1512 uint32_t ldl_le_phys(AddressSpace
*as
, hwaddr addr
);
1513 uint32_t ldl_be_phys(AddressSpace
*as
, hwaddr addr
);
1514 uint64_t ldq_le_phys(AddressSpace
*as
, hwaddr addr
);
1515 uint64_t ldq_be_phys(AddressSpace
*as
, hwaddr addr
);
1516 void stb_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1517 void stw_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1518 void stw_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1519 void stl_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1520 void stl_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1521 void stq_le_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1522 void stq_be_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1524 struct MemoryRegionCache
{
1530 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .as = NULL })
1532 /* address_space_cache_init: prepare for repeated access to a physical
1535 * @cache: #MemoryRegionCache to be filled
1536 * @as: #AddressSpace to be accessed
1537 * @addr: address within that address space
1538 * @len: length of buffer
1539 * @is_write: indicates the transfer direction
1541 * Will only work with RAM, and may map a subset of the requested range by
1542 * returning a value that is less than @len. On failure, return a negative
1545 * Because it only works with RAM, this function can be used for
1546 * read-modify-write operations. In this case, is_write should be %true.
1548 * Note that addresses passed to the address_space_*_cached functions
1549 * are relative to @addr.
1551 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1558 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1560 * @cache: The #MemoryRegionCache to operate on.
1561 * @addr: The first physical address that was written, relative to the
1562 * address that was passed to @address_space_cache_init.
1563 * @access_len: The number of bytes that were written starting at @addr.
1565 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1570 * address_space_cache_destroy: free a #MemoryRegionCache
1572 * @cache: The #MemoryRegionCache whose memory should be released.
1574 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1576 /* address_space_ld*_cached: load from a cached #MemoryRegion
1577 * address_space_st*_cached: store into a cached #MemoryRegion
1579 * These functions perform a load or store of the byte, word,
1580 * longword or quad to the specified address. The address is
1581 * a physical address in the AddressSpace, but it must lie within
1582 * a #MemoryRegion that was mapped with address_space_cache_init.
1584 * The _le suffixed functions treat the data as little endian;
1585 * _be indicates big endian; no suffix indicates "same endianness
1588 * The "guest CPU endianness" accessors are deprecated for use outside
1589 * target-* code; devices should be CPU-agnostic and use either the LE
1590 * or the BE accessors.
1592 * @cache: previously initialized #MemoryRegionCache to be accessed
1593 * @addr: address within the address space
1594 * @val: data value, for stores
1595 * @attrs: memory transaction attributes
1596 * @result: location to write the success/failure of the transaction;
1597 * if NULL, this information is discarded
1599 uint32_t address_space_ldub_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1600 MemTxAttrs attrs
, MemTxResult
*result
);
1601 uint32_t address_space_lduw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1602 MemTxAttrs attrs
, MemTxResult
*result
);
1603 uint32_t address_space_lduw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1604 MemTxAttrs attrs
, MemTxResult
*result
);
1605 uint32_t address_space_ldl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1606 MemTxAttrs attrs
, MemTxResult
*result
);
1607 uint32_t address_space_ldl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1608 MemTxAttrs attrs
, MemTxResult
*result
);
1609 uint64_t address_space_ldq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1610 MemTxAttrs attrs
, MemTxResult
*result
);
1611 uint64_t address_space_ldq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1612 MemTxAttrs attrs
, MemTxResult
*result
);
1613 void address_space_stb_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1614 MemTxAttrs attrs
, MemTxResult
*result
);
1615 void address_space_stw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1616 MemTxAttrs attrs
, MemTxResult
*result
);
1617 void address_space_stw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1618 MemTxAttrs attrs
, MemTxResult
*result
);
1619 void address_space_stl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1620 MemTxAttrs attrs
, MemTxResult
*result
);
1621 void address_space_stl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1622 MemTxAttrs attrs
, MemTxResult
*result
);
1623 void address_space_stq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1624 MemTxAttrs attrs
, MemTxResult
*result
);
1625 void address_space_stq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1626 MemTxAttrs attrs
, MemTxResult
*result
);
1628 uint32_t ldub_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1629 uint32_t lduw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1630 uint32_t lduw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1631 uint32_t ldl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1632 uint32_t ldl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1633 uint64_t ldq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1634 uint64_t ldq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1635 void stb_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1636 void stw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1637 void stw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1638 void stl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1639 void stl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1640 void stq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1641 void stq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1642 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1643 * entry. Should be called from an RCU critical section.
1645 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1648 /* address_space_translate: translate an address range into an address space
1649 * into a MemoryRegion and an address range into that section. Should be
1650 * called from an RCU critical section, to avoid that the last reference
1651 * to the returned region disappears after address_space_translate returns.
1653 * @as: #AddressSpace to be accessed
1654 * @addr: address within that address space
1655 * @xlat: pointer to address within the returned memory region section's
1657 * @len: pointer to length
1658 * @is_write: indicates the transfer direction
1660 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1661 hwaddr
*xlat
, hwaddr
*len
,
1664 /* address_space_access_valid: check for validity of accessing an address
1667 * Check whether memory is assigned to the given address space range, and
1668 * access is permitted by any IOMMU regions that are active for the address
1671 * For now, addr and len should be aligned to a page size. This limitation
1672 * will be lifted in the future.
1674 * @as: #AddressSpace to be accessed
1675 * @addr: address within that address space
1676 * @len: length of the area to be checked
1677 * @is_write: indicates the transfer direction
1679 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1681 /* address_space_map: map a physical memory region into a host virtual address
1683 * May map a subset of the requested range, given by and returned in @plen.
1684 * May return %NULL if resources needed to perform the mapping are exhausted.
1685 * Use only for reads OR writes - not for read-modify-write operations.
1686 * Use cpu_register_map_client() to know when retrying the map operation is
1687 * likely to succeed.
1689 * @as: #AddressSpace to be accessed
1690 * @addr: address within that address space
1691 * @plen: pointer to length of buffer; updated on return
1692 * @is_write: indicates the transfer direction
1694 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1695 hwaddr
*plen
, bool is_write
);
1697 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1699 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1700 * the amount of memory that was actually read or written by the caller.
1702 * @as: #AddressSpace used
1703 * @addr: address within that address space
1704 * @len: buffer length as returned by address_space_map()
1705 * @access_len: amount of data actually transferred
1706 * @is_write: indicates the transfer direction
1708 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1709 int is_write
, hwaddr access_len
);
1712 /* Internal functions, part of the implementation of address_space_read. */
1713 MemTxResult
address_space_read_continue(AddressSpace
*as
, hwaddr addr
,
1714 MemTxAttrs attrs
, uint8_t *buf
,
1715 int len
, hwaddr addr1
, hwaddr l
,
1717 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
1718 MemTxAttrs attrs
, uint8_t *buf
, int len
);
1719 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
1721 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
1724 return memory_region_is_ram(mr
) &&
1725 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
1727 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
1728 memory_region_is_romd(mr
);
1733 * address_space_read: read from an address space.
1735 * Return a MemTxResult indicating whether the operation succeeded
1736 * or failed (eg unassigned memory, device rejected the transaction,
1739 * @as: #AddressSpace to be accessed
1740 * @addr: address within that address space
1741 * @attrs: memory transaction attributes
1742 * @buf: buffer with the data transferred
1744 static inline __attribute__((__always_inline__
))
1745 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1746 uint8_t *buf
, int len
)
1748 MemTxResult result
= MEMTX_OK
;
1753 if (__builtin_constant_p(len
)) {
1757 mr
= address_space_translate(as
, addr
, &addr1
, &l
, false);
1758 if (len
== l
&& memory_access_is_direct(mr
, false)) {
1759 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
1760 memcpy(buf
, ptr
, len
);
1762 result
= address_space_read_continue(as
, addr
, attrs
, buf
, len
,
1768 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
1774 * address_space_read_cached: read from a cached RAM region
1776 * @cache: Cached region to be addressed
1777 * @addr: address relative to the base of the RAM region
1778 * @buf: buffer with the data transferred
1779 * @len: length of the data transferred
1782 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1785 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1786 address_space_read(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);
1790 * address_space_write_cached: write to a cached RAM region
1792 * @cache: Cached region to be addressed
1793 * @addr: address relative to the base of the RAM region
1794 * @buf: buffer with the data transferred
1795 * @len: length of the data transferred
1798 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1801 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1802 address_space_write(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);