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: Region name, becomes part of RAMBlock name used in migration stream
375 * must be unique within any device
376 * @size: size of the region.
377 * @errp: pointer to Error*, to store an error if it happens.
379 void memory_region_init_ram(MemoryRegion
*mr
,
380 struct Object
*owner
,
386 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
387 * RAM. Accesses into the region will
388 * modify memory directly. Only an initial
389 * portion of this RAM is actually used.
390 * The used size can change across reboots.
392 * @mr: the #MemoryRegion to be initialized.
393 * @owner: the object that tracks the region's reference count
394 * @name: Region name, becomes part of RAMBlock name used in migration stream
395 * must be unique within any device
396 * @size: used size of the region.
397 * @max_size: max size of the region.
398 * @resized: callback to notify owner about used size change.
399 * @errp: pointer to Error*, to store an error if it happens.
401 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
402 struct Object
*owner
,
406 void (*resized
)(const char*,
412 * memory_region_init_ram_from_file: Initialize RAM memory region with a
415 * @mr: the #MemoryRegion to be initialized.
416 * @owner: the object that tracks the region's reference count
417 * @name: Region name, becomes part of RAMBlock name used in migration stream
418 * must be unique within any device
419 * @size: size of the region.
420 * @share: %true if memory must be mmaped with the MAP_SHARED flag
421 * @path: the path in which to allocate the RAM.
422 * @errp: pointer to Error*, to store an error if it happens.
424 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
425 struct Object
*owner
,
434 * memory_region_init_ram_ptr: Initialize RAM memory region from a
435 * user-provided pointer. Accesses into the
436 * region will modify memory directly.
438 * @mr: the #MemoryRegion to be initialized.
439 * @owner: the object that tracks the region's reference count
440 * @name: Region name, becomes part of RAMBlock name used in migration stream
441 * must be unique within any device
442 * @size: size of the region.
443 * @ptr: memory to be mapped; must contain at least @size bytes.
445 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
446 struct Object
*owner
,
452 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
453 * a user-provided pointer.
455 * A RAM device represents a mapping to a physical device, such as to a PCI
456 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
457 * into the VM address space and access to the region will modify memory
458 * directly. However, the memory region should not be included in a memory
459 * dump (device may not be enabled/mapped at the time of the dump), and
460 * operations incompatible with manipulating MMIO should be avoided. Replaces
463 * @mr: the #MemoryRegion to be initialized.
464 * @owner: the object that tracks the region's reference count
465 * @name: the name of the region.
466 * @size: size of the region.
467 * @ptr: memory to be mapped; must contain at least @size bytes.
469 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
470 struct Object
*owner
,
476 * memory_region_init_alias: Initialize a memory region that aliases all or a
477 * part of another memory region.
479 * @mr: the #MemoryRegion to be initialized.
480 * @owner: the object that tracks the region's reference count
481 * @name: used for debugging; not visible to the user or ABI
482 * @orig: the region to be referenced; @mr will be equivalent to
483 * @orig between @offset and @offset + @size - 1.
484 * @offset: start of the section in @orig to be referenced.
485 * @size: size of the region.
487 void memory_region_init_alias(MemoryRegion
*mr
,
488 struct Object
*owner
,
495 * memory_region_init_rom: Initialize a ROM memory region.
497 * This has the same effect as calling memory_region_init_ram()
498 * and then marking the resulting region read-only with
499 * memory_region_set_readonly().
501 * @mr: the #MemoryRegion to be initialized.
502 * @owner: the object that tracks the region's reference count
503 * @name: Region name, becomes part of RAMBlock name used in migration stream
504 * must be unique within any device
505 * @size: size of the region.
506 * @errp: pointer to Error*, to store an error if it happens.
508 void memory_region_init_rom(MemoryRegion
*mr
,
509 struct Object
*owner
,
515 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
516 * handled via callbacks.
518 * @mr: the #MemoryRegion to be initialized.
519 * @owner: the object that tracks the region's reference count
520 * @ops: callbacks for write access handling (must not be NULL).
521 * @name: Region name, becomes part of RAMBlock name used in migration stream
522 * must be unique within any device
523 * @size: size of the region.
524 * @errp: pointer to Error*, to store an error if it happens.
526 void memory_region_init_rom_device(MemoryRegion
*mr
,
527 struct Object
*owner
,
528 const MemoryRegionOps
*ops
,
535 * memory_region_init_reservation: Initialize a memory region that reserves
538 * A reservation region primariy serves debugging purposes. It claims I/O
539 * space that is not supposed to be handled by QEMU itself. Any access via
540 * the memory API will cause an abort().
541 * This function is deprecated. Use memory_region_init_io() with NULL
544 * @mr: the #MemoryRegion to be initialized
545 * @owner: the object that tracks the region's reference count
546 * @name: used for debugging; not visible to the user or ABI
547 * @size: size of the region.
549 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
554 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
558 * memory_region_init_iommu: Initialize a memory region that translates
561 * An IOMMU region translates addresses and forwards accesses to a target
564 * @mr: the #MemoryRegion to be initialized
565 * @owner: the object that tracks the region's reference count
566 * @ops: a function that translates addresses into the @target region
567 * @name: used for debugging; not visible to the user or ABI
568 * @size: size of the region.
570 void memory_region_init_iommu(MemoryRegion
*mr
,
571 struct Object
*owner
,
572 const MemoryRegionIOMMUOps
*ops
,
577 * memory_region_owner: get a memory region's owner.
579 * @mr: the memory region being queried.
581 struct Object
*memory_region_owner(MemoryRegion
*mr
);
584 * memory_region_size: get a memory region's size.
586 * @mr: the memory region being queried.
588 uint64_t memory_region_size(MemoryRegion
*mr
);
591 * memory_region_is_ram: check whether a memory region is random access
593 * Returns %true is a memory region is random access.
595 * @mr: the memory region being queried
597 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
603 * memory_region_is_ram_device: check whether a memory region is a ram device
605 * Returns %true is a memory region is a device backed ram region
607 * @mr: the memory region being queried
609 bool memory_region_is_ram_device(MemoryRegion
*mr
);
612 * memory_region_is_romd: check whether a memory region is in ROMD mode
614 * Returns %true if a memory region is a ROM device and currently set to allow
617 * @mr: the memory region being queried
619 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
621 return mr
->rom_device
&& mr
->romd_mode
;
625 * memory_region_is_iommu: check whether a memory region is an iommu
627 * Returns %true is a memory region is an iommu.
629 * @mr: the memory region being queried
631 static inline bool memory_region_is_iommu(MemoryRegion
*mr
)
634 return memory_region_is_iommu(mr
->alias
);
636 return mr
->iommu_ops
;
641 * memory_region_iommu_get_min_page_size: get minimum supported page size
644 * Returns minimum supported page size for an iommu.
646 * @mr: the memory region being queried
648 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion
*mr
);
651 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
653 * The notification type will be decided by entry.perm bits:
655 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
656 * - For MAP (newly added entry) notifies: set entry.perm to the
657 * permission of the page (which is definitely !IOMMU_NONE).
659 * Note: for any IOMMU implementation, an in-place mapping change
660 * should be notified with an UNMAP followed by a MAP.
662 * @mr: the memory region that was changed
663 * @entry: the new entry in the IOMMU translation table. The entry
664 * replaces all old entries for the same virtual I/O address range.
665 * Deleted entries have .@perm == 0.
667 void memory_region_notify_iommu(MemoryRegion
*mr
,
668 IOMMUTLBEntry entry
);
671 * memory_region_register_iommu_notifier: register a notifier for changes to
672 * IOMMU translation entries.
674 * @mr: the memory region to observe
675 * @n: the IOMMUNotifier to be added; the notify callback receives a
676 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
677 * ceases to be valid on exit from the notifier.
679 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
683 * memory_region_iommu_replay: replay existing IOMMU translations to
684 * a notifier with the minimum page granularity returned by
685 * mr->iommu_ops->get_page_size().
687 * @mr: the memory region to observe
688 * @n: the notifier to which to replay iommu mappings
689 * @is_write: Whether to treat the replay as a translate "write"
692 void memory_region_iommu_replay(MemoryRegion
*mr
, IOMMUNotifier
*n
,
696 * memory_region_unregister_iommu_notifier: unregister a notifier for
697 * changes to IOMMU translation entries.
699 * @mr: the memory region which was observed and for which notity_stopped()
701 * @n: the notifier to be removed.
703 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
707 * memory_region_name: get a memory region's name
709 * Returns the string that was used to initialize the memory region.
711 * @mr: the memory region being queried
713 const char *memory_region_name(const MemoryRegion
*mr
);
716 * memory_region_is_logging: return whether a memory region is logging writes
718 * Returns %true if the memory region is logging writes for the given client
720 * @mr: the memory region being queried
721 * @client: the client being queried
723 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
726 * memory_region_get_dirty_log_mask: return the clients for which a
727 * memory region is logging writes.
729 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
730 * are the bit indices.
732 * @mr: the memory region being queried
734 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
737 * memory_region_is_rom: check whether a memory region is ROM
739 * Returns %true is a memory region is read-only memory.
741 * @mr: the memory region being queried
743 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
745 return mr
->ram
&& mr
->readonly
;
750 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
752 * Returns a file descriptor backing a file-based RAM memory region,
753 * or -1 if the region is not a file-based RAM memory region.
755 * @mr: the RAM or alias memory region being queried.
757 int memory_region_get_fd(MemoryRegion
*mr
);
760 * memory_region_set_fd: Mark a RAM memory region as backed by a
763 * This function is typically used after memory_region_init_ram_ptr().
765 * @mr: the memory region being queried.
766 * @fd: the file descriptor that backs @mr.
768 void memory_region_set_fd(MemoryRegion
*mr
, int fd
);
771 * memory_region_from_host: Convert a pointer into a RAM memory region
772 * and an offset within it.
774 * Given a host pointer inside a RAM memory region (created with
775 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
776 * the MemoryRegion and the offset within it.
778 * Use with care; by the time this function returns, the returned pointer is
779 * not protected by RCU anymore. If the caller is not within an RCU critical
780 * section and does not hold the iothread lock, it must have other means of
781 * protecting the pointer, such as a reference to the region that includes
782 * the incoming ram_addr_t.
784 * @mr: the memory region being queried.
786 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
789 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
791 * Returns a host pointer to a RAM memory region (created with
792 * memory_region_init_ram() or memory_region_init_ram_ptr()).
794 * Use with care; by the time this function returns, the returned pointer is
795 * not protected by RCU anymore. If the caller is not within an RCU critical
796 * section and does not hold the iothread lock, it must have other means of
797 * protecting the pointer, such as a reference to the region that includes
798 * the incoming ram_addr_t.
800 * @mr: the memory region being queried.
802 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
804 /* memory_region_ram_resize: Resize a RAM region.
806 * Only legal before guest might have detected the memory size: e.g. on
807 * incoming migration, or right after reset.
809 * @mr: a memory region created with @memory_region_init_resizeable_ram.
810 * @newsize: the new size the region
811 * @errp: pointer to Error*, to store an error if it happens.
813 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
817 * memory_region_set_log: Turn dirty logging on or off for a region.
819 * Turns dirty logging on or off for a specified client (display, migration).
820 * Only meaningful for RAM regions.
822 * @mr: the memory region being updated.
823 * @log: whether dirty logging is to be enabled or disabled.
824 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
826 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
829 * memory_region_get_dirty: Check whether a range of bytes is dirty
830 * for a specified client.
832 * Checks whether a range of bytes has been written to since the last
833 * call to memory_region_reset_dirty() with the same @client. Dirty logging
836 * @mr: the memory region being queried.
837 * @addr: the address (relative to the start of the region) being queried.
838 * @size: the size of the range being queried.
839 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
842 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
843 hwaddr size
, unsigned client
);
846 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
848 * Marks a range of bytes as dirty, after it has been dirtied outside
851 * @mr: the memory region being dirtied.
852 * @addr: the address (relative to the start of the region) being dirtied.
853 * @size: size of the range being dirtied.
855 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
859 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
860 * for a specified client. It clears them.
862 * Checks whether a range of bytes has been written to since the last
863 * call to memory_region_reset_dirty() with the same @client. Dirty logging
866 * @mr: the memory region being queried.
867 * @addr: the address (relative to the start of the region) being queried.
868 * @size: the size of the range being queried.
869 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
872 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
873 hwaddr size
, unsigned client
);
875 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
876 * any external TLBs (e.g. kvm)
878 * Flushes dirty information from accelerators such as kvm and vhost-net
879 * and makes it available to users of the memory API.
881 * @mr: the region being flushed.
883 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
886 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
889 * Marks a range of pages as no longer dirty.
891 * @mr: the region being updated.
892 * @addr: the start of the subrange being cleaned.
893 * @size: the size of the subrange being cleaned.
894 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
897 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
898 hwaddr size
, unsigned client
);
901 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
903 * Allows a memory region to be marked as read-only (turning it into a ROM).
904 * only useful on RAM regions.
906 * @mr: the region being updated.
907 * @readonly: whether rhe region is to be ROM or RAM.
909 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
912 * memory_region_rom_device_set_romd: enable/disable ROMD mode
914 * Allows a ROM device (initialized with memory_region_init_rom_device() to
915 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
916 * device is mapped to guest memory and satisfies read access directly.
917 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
918 * Writes are always handled by the #MemoryRegion.write function.
920 * @mr: the memory region to be updated
921 * @romd_mode: %true to put the region into ROMD mode
923 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
926 * memory_region_set_coalescing: Enable memory coalescing for the region.
928 * Enabled writes to a region to be queued for later processing. MMIO ->write
929 * callbacks may be delayed until a non-coalesced MMIO is issued.
930 * Only useful for IO regions. Roughly similar to write-combining hardware.
932 * @mr: the memory region to be write coalesced
934 void memory_region_set_coalescing(MemoryRegion
*mr
);
937 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
940 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
941 * Multiple calls can be issued coalesced disjoint ranges.
943 * @mr: the memory region to be updated.
944 * @offset: the start of the range within the region to be coalesced.
945 * @size: the size of the subrange to be coalesced.
947 void memory_region_add_coalescing(MemoryRegion
*mr
,
952 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
954 * Disables any coalescing caused by memory_region_set_coalescing() or
955 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
958 * @mr: the memory region to be updated.
960 void memory_region_clear_coalescing(MemoryRegion
*mr
);
963 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
966 * Ensure that pending coalesced MMIO request are flushed before the memory
967 * region is accessed. This property is automatically enabled for all regions
968 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
970 * @mr: the memory region to be updated.
972 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
975 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
978 * Clear the automatic coalesced MMIO flushing enabled via
979 * memory_region_set_flush_coalesced. Note that this service has no effect on
980 * memory regions that have MMIO coalescing enabled for themselves. For them,
981 * automatic flushing will stop once coalescing is disabled.
983 * @mr: the memory region to be updated.
985 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
988 * memory_region_set_global_locking: Declares the access processing requires
989 * QEMU's global lock.
991 * When this is invoked, accesses to the memory region will be processed while
992 * holding the global lock of QEMU. This is the default behavior of memory
995 * @mr: the memory region to be updated.
997 void memory_region_set_global_locking(MemoryRegion
*mr
);
1000 * memory_region_clear_global_locking: Declares that access processing does
1001 * not depend on the QEMU global lock.
1003 * By clearing this property, accesses to the memory region will be processed
1004 * outside of QEMU's global lock (unless the lock is held on when issuing the
1005 * access request). In this case, the device model implementing the access
1006 * handlers is responsible for synchronization of concurrency.
1008 * @mr: the memory region to be updated.
1010 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1013 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1014 * is written to a location.
1016 * Marks a word in an IO region (initialized with memory_region_init_io())
1017 * as a trigger for an eventfd event. The I/O callback will not be called.
1018 * The caller must be prepared to handle failure (that is, take the required
1019 * action if the callback _is_ called).
1021 * @mr: the memory region being updated.
1022 * @addr: the address within @mr that is to be monitored
1023 * @size: the size of the access to trigger the eventfd
1024 * @match_data: whether to match against @data, instead of just @addr
1025 * @data: the data to match against the guest write
1026 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1028 void memory_region_add_eventfd(MemoryRegion
*mr
,
1036 * memory_region_del_eventfd: Cancel an eventfd.
1038 * Cancels an eventfd trigger requested by a previous
1039 * memory_region_add_eventfd() call.
1041 * @mr: the memory region being updated.
1042 * @addr: the address within @mr that is to be monitored
1043 * @size: the size of the access to trigger the eventfd
1044 * @match_data: whether to match against @data, instead of just @addr
1045 * @data: the data to match against the guest write
1046 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1048 void memory_region_del_eventfd(MemoryRegion
*mr
,
1056 * memory_region_add_subregion: Add a subregion to a container.
1058 * Adds a subregion at @offset. The subregion may not overlap with other
1059 * subregions (except for those explicitly marked as overlapping). A region
1060 * may only be added once as a subregion (unless removed with
1061 * memory_region_del_subregion()); use memory_region_init_alias() if you
1062 * want a region to be a subregion in multiple locations.
1064 * @mr: the region to contain the new subregion; must be a container
1065 * initialized with memory_region_init().
1066 * @offset: the offset relative to @mr where @subregion is added.
1067 * @subregion: the subregion to be added.
1069 void memory_region_add_subregion(MemoryRegion
*mr
,
1071 MemoryRegion
*subregion
);
1073 * memory_region_add_subregion_overlap: Add a subregion to a container
1076 * Adds a subregion at @offset. The subregion may overlap with other
1077 * subregions. Conflicts are resolved by having a higher @priority hide a
1078 * lower @priority. Subregions without priority are taken as @priority 0.
1079 * A region may only be added once as a subregion (unless removed with
1080 * memory_region_del_subregion()); use memory_region_init_alias() if you
1081 * want a region to be a subregion in multiple locations.
1083 * @mr: the region to contain the new subregion; must be a container
1084 * initialized with memory_region_init().
1085 * @offset: the offset relative to @mr where @subregion is added.
1086 * @subregion: the subregion to be added.
1087 * @priority: used for resolving overlaps; highest priority wins.
1089 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1091 MemoryRegion
*subregion
,
1095 * memory_region_get_ram_addr: Get the ram address associated with a memory
1098 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1100 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1102 * memory_region_del_subregion: Remove a subregion.
1104 * Removes a subregion from its container.
1106 * @mr: the container to be updated.
1107 * @subregion: the region being removed; must be a current subregion of @mr.
1109 void memory_region_del_subregion(MemoryRegion
*mr
,
1110 MemoryRegion
*subregion
);
1113 * memory_region_set_enabled: dynamically enable or disable a region
1115 * Enables or disables a memory region. A disabled memory region
1116 * ignores all accesses to itself and its subregions. It does not
1117 * obscure sibling subregions with lower priority - it simply behaves as
1118 * if it was removed from the hierarchy.
1120 * Regions default to being enabled.
1122 * @mr: the region to be updated
1123 * @enabled: whether to enable or disable the region
1125 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1128 * memory_region_set_address: dynamically update the address of a region
1130 * Dynamically updates the address of a region, relative to its container.
1131 * May be used on regions are currently part of a memory hierarchy.
1133 * @mr: the region to be updated
1134 * @addr: new address, relative to container region
1136 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1139 * memory_region_set_size: dynamically update the size of a region.
1141 * Dynamically updates the size of a region.
1143 * @mr: the region to be updated
1144 * @size: used size of the region.
1146 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1149 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1151 * Dynamically updates the offset into the target region that an alias points
1152 * to, as if the fourth argument to memory_region_init_alias() has changed.
1154 * @mr: the #MemoryRegion to be updated; should be an alias.
1155 * @offset: the new offset into the target memory region
1157 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1161 * memory_region_present: checks if an address relative to a @container
1162 * translates into #MemoryRegion within @container
1164 * Answer whether a #MemoryRegion within @container covers the address
1167 * @container: a #MemoryRegion within which @addr is a relative address
1168 * @addr: the area within @container to be searched
1170 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1173 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1174 * into any address space.
1176 * @mr: a #MemoryRegion which should be checked if it's mapped
1178 bool memory_region_is_mapped(MemoryRegion
*mr
);
1181 * memory_region_find: translate an address/size relative to a
1182 * MemoryRegion into a #MemoryRegionSection.
1184 * Locates the first #MemoryRegion within @mr that overlaps the range
1185 * given by @addr and @size.
1187 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1188 * It will have the following characteristics:
1189 * .@size = 0 iff no overlap was found
1190 * .@mr is non-%NULL iff an overlap was found
1192 * Remember that in the return value the @offset_within_region is
1193 * relative to the returned region (in the .@mr field), not to the
1196 * Similarly, the .@offset_within_address_space is relative to the
1197 * address space that contains both regions, the passed and the
1198 * returned one. However, in the special case where the @mr argument
1199 * has no container (and thus is the root of the address space), the
1200 * following will hold:
1201 * .@offset_within_address_space >= @addr
1202 * .@offset_within_address_space + .@size <= @addr + @size
1204 * @mr: a MemoryRegion within which @addr is a relative address
1205 * @addr: start of the area within @as to be searched
1206 * @size: size of the area to be searched
1208 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1209 hwaddr addr
, uint64_t size
);
1212 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1214 * Synchronizes the dirty page log for all address spaces.
1216 void memory_global_dirty_log_sync(void);
1219 * memory_region_transaction_begin: Start a transaction.
1221 * During a transaction, changes will be accumulated and made visible
1222 * only when the transaction ends (is committed).
1224 void memory_region_transaction_begin(void);
1227 * memory_region_transaction_commit: Commit a transaction and make changes
1228 * visible to the guest.
1230 void memory_region_transaction_commit(void);
1233 * memory_listener_register: register callbacks to be called when memory
1234 * sections are mapped or unmapped into an address
1237 * @listener: an object containing the callbacks to be called
1238 * @filter: if non-%NULL, only regions in this address space will be observed
1240 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1243 * memory_listener_unregister: undo the effect of memory_listener_register()
1245 * @listener: an object containing the callbacks to be removed
1247 void memory_listener_unregister(MemoryListener
*listener
);
1250 * memory_global_dirty_log_start: begin dirty logging for all regions
1252 void memory_global_dirty_log_start(void);
1255 * memory_global_dirty_log_stop: end dirty logging for all regions
1257 void memory_global_dirty_log_stop(void);
1259 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
);
1262 * memory_region_dispatch_read: perform a read directly to the specified
1265 * @mr: #MemoryRegion to access
1266 * @addr: address within that region
1267 * @pval: pointer to uint64_t which the data is written to
1268 * @size: size of the access in bytes
1269 * @attrs: memory transaction attributes to use for the access
1271 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1277 * memory_region_dispatch_write: perform a write directly to the specified
1280 * @mr: #MemoryRegion to access
1281 * @addr: address within that region
1282 * @data: data to write
1283 * @size: size of the access in bytes
1284 * @attrs: memory transaction attributes to use for the access
1286 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1293 * address_space_init: initializes an address space
1295 * @as: an uninitialized #AddressSpace
1296 * @root: a #MemoryRegion that routes addresses for the address space
1297 * @name: an address space name. The name is only used for debugging
1300 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1303 * address_space_init_shareable: return an address space for a memory region,
1304 * creating it if it does not already exist
1306 * @root: a #MemoryRegion that routes addresses for the address space
1307 * @name: an address space name. The name is only used for debugging
1310 * This function will return a pointer to an existing AddressSpace
1311 * which was initialized with the specified MemoryRegion, or it will
1312 * create and initialize one if it does not already exist. The ASes
1313 * are reference-counted, so the memory will be freed automatically
1314 * when the AddressSpace is destroyed via address_space_destroy.
1316 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
,
1320 * address_space_destroy: destroy an address space
1322 * Releases all resources associated with an address space. After an address space
1323 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1326 * @as: address space to be destroyed
1328 void address_space_destroy(AddressSpace
*as
);
1331 * address_space_rw: read from or write to an address space.
1333 * Return a MemTxResult indicating whether the operation succeeded
1334 * or failed (eg unassigned memory, device rejected the transaction,
1337 * @as: #AddressSpace to be accessed
1338 * @addr: address within that address space
1339 * @attrs: memory transaction attributes
1340 * @buf: buffer with the data transferred
1341 * @is_write: indicates the transfer direction
1343 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1344 MemTxAttrs attrs
, uint8_t *buf
,
1345 int len
, bool is_write
);
1348 * address_space_write: write to address space.
1350 * Return a MemTxResult indicating whether the operation succeeded
1351 * or failed (eg unassigned memory, device rejected the transaction,
1354 * @as: #AddressSpace to be accessed
1355 * @addr: address within that address space
1356 * @attrs: memory transaction attributes
1357 * @buf: buffer with the data transferred
1359 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1361 const uint8_t *buf
, int len
);
1363 /* address_space_ld*: load from an address space
1364 * address_space_st*: store to an address space
1366 * These functions perform a load or store of the byte, word,
1367 * longword or quad to the specified address within the AddressSpace.
1368 * The _le suffixed functions treat the data as little endian;
1369 * _be indicates big endian; no suffix indicates "same endianness
1372 * The "guest CPU endianness" accessors are deprecated for use outside
1373 * target-* code; devices should be CPU-agnostic and use either the LE
1374 * or the BE accessors.
1376 * @as #AddressSpace to be accessed
1377 * @addr: address within that address space
1378 * @val: data value, for stores
1379 * @attrs: memory transaction attributes
1380 * @result: location to write the success/failure of the transaction;
1381 * if NULL, this information is discarded
1383 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1384 MemTxAttrs attrs
, MemTxResult
*result
);
1385 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1386 MemTxAttrs attrs
, MemTxResult
*result
);
1387 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1388 MemTxAttrs attrs
, MemTxResult
*result
);
1389 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1390 MemTxAttrs attrs
, MemTxResult
*result
);
1391 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1392 MemTxAttrs attrs
, MemTxResult
*result
);
1393 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1394 MemTxAttrs attrs
, MemTxResult
*result
);
1395 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1396 MemTxAttrs attrs
, MemTxResult
*result
);
1397 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1398 MemTxAttrs attrs
, MemTxResult
*result
);
1399 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1400 MemTxAttrs attrs
, MemTxResult
*result
);
1401 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1402 MemTxAttrs attrs
, MemTxResult
*result
);
1403 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1404 MemTxAttrs attrs
, MemTxResult
*result
);
1405 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1406 MemTxAttrs attrs
, MemTxResult
*result
);
1407 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1408 MemTxAttrs attrs
, MemTxResult
*result
);
1409 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1410 MemTxAttrs attrs
, MemTxResult
*result
);
1412 uint32_t ldub_phys(AddressSpace
*as
, hwaddr addr
);
1413 uint32_t lduw_le_phys(AddressSpace
*as
, hwaddr addr
);
1414 uint32_t lduw_be_phys(AddressSpace
*as
, hwaddr addr
);
1415 uint32_t ldl_le_phys(AddressSpace
*as
, hwaddr addr
);
1416 uint32_t ldl_be_phys(AddressSpace
*as
, hwaddr addr
);
1417 uint64_t ldq_le_phys(AddressSpace
*as
, hwaddr addr
);
1418 uint64_t ldq_be_phys(AddressSpace
*as
, hwaddr addr
);
1419 void stb_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1420 void stw_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1421 void stw_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1422 void stl_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1423 void stl_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1424 void stq_le_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1425 void stq_be_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1427 struct MemoryRegionCache
{
1433 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .as = NULL })
1435 /* address_space_cache_init: prepare for repeated access to a physical
1438 * @cache: #MemoryRegionCache to be filled
1439 * @as: #AddressSpace to be accessed
1440 * @addr: address within that address space
1441 * @len: length of buffer
1442 * @is_write: indicates the transfer direction
1444 * Will only work with RAM, and may map a subset of the requested range by
1445 * returning a value that is less than @len. On failure, return a negative
1448 * Because it only works with RAM, this function can be used for
1449 * read-modify-write operations. In this case, is_write should be %true.
1451 * Note that addresses passed to the address_space_*_cached functions
1452 * are relative to @addr.
1454 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1461 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1463 * @cache: The #MemoryRegionCache to operate on.
1464 * @addr: The first physical address that was written, relative to the
1465 * address that was passed to @address_space_cache_init.
1466 * @access_len: The number of bytes that were written starting at @addr.
1468 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1473 * address_space_cache_destroy: free a #MemoryRegionCache
1475 * @cache: The #MemoryRegionCache whose memory should be released.
1477 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1479 /* address_space_ld*_cached: load from a cached #MemoryRegion
1480 * address_space_st*_cached: store into a cached #MemoryRegion
1482 * These functions perform a load or store of the byte, word,
1483 * longword or quad to the specified address. The address is
1484 * a physical address in the AddressSpace, but it must lie within
1485 * a #MemoryRegion that was mapped with address_space_cache_init.
1487 * The _le suffixed functions treat the data as little endian;
1488 * _be indicates big endian; no suffix indicates "same endianness
1491 * The "guest CPU endianness" accessors are deprecated for use outside
1492 * target-* code; devices should be CPU-agnostic and use either the LE
1493 * or the BE accessors.
1495 * @cache: previously initialized #MemoryRegionCache to be accessed
1496 * @addr: address within the address space
1497 * @val: data value, for stores
1498 * @attrs: memory transaction attributes
1499 * @result: location to write the success/failure of the transaction;
1500 * if NULL, this information is discarded
1502 uint32_t address_space_ldub_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1503 MemTxAttrs attrs
, MemTxResult
*result
);
1504 uint32_t address_space_lduw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1505 MemTxAttrs attrs
, MemTxResult
*result
);
1506 uint32_t address_space_lduw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1507 MemTxAttrs attrs
, MemTxResult
*result
);
1508 uint32_t address_space_ldl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1509 MemTxAttrs attrs
, MemTxResult
*result
);
1510 uint32_t address_space_ldl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1511 MemTxAttrs attrs
, MemTxResult
*result
);
1512 uint64_t address_space_ldq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1513 MemTxAttrs attrs
, MemTxResult
*result
);
1514 uint64_t address_space_ldq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1515 MemTxAttrs attrs
, MemTxResult
*result
);
1516 void address_space_stb_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1517 MemTxAttrs attrs
, MemTxResult
*result
);
1518 void address_space_stw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1519 MemTxAttrs attrs
, MemTxResult
*result
);
1520 void address_space_stw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1521 MemTxAttrs attrs
, MemTxResult
*result
);
1522 void address_space_stl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1523 MemTxAttrs attrs
, MemTxResult
*result
);
1524 void address_space_stl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1525 MemTxAttrs attrs
, MemTxResult
*result
);
1526 void address_space_stq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1527 MemTxAttrs attrs
, MemTxResult
*result
);
1528 void address_space_stq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1529 MemTxAttrs attrs
, MemTxResult
*result
);
1531 uint32_t ldub_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1532 uint32_t lduw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1533 uint32_t lduw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1534 uint32_t ldl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1535 uint32_t ldl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1536 uint64_t ldq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1537 uint64_t ldq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1538 void stb_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1539 void stw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1540 void stw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1541 void stl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1542 void stl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1543 void stq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1544 void stq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1545 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1546 * entry. Should be called from an RCU critical section.
1548 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1551 /* address_space_translate: translate an address range into an address space
1552 * into a MemoryRegion and an address range into that section. Should be
1553 * called from an RCU critical section, to avoid that the last reference
1554 * to the returned region disappears after address_space_translate returns.
1556 * @as: #AddressSpace to be accessed
1557 * @addr: address within that address space
1558 * @xlat: pointer to address within the returned memory region section's
1560 * @len: pointer to length
1561 * @is_write: indicates the transfer direction
1563 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1564 hwaddr
*xlat
, hwaddr
*len
,
1567 /* address_space_access_valid: check for validity of accessing an address
1570 * Check whether memory is assigned to the given address space range, and
1571 * access is permitted by any IOMMU regions that are active for the address
1574 * For now, addr and len should be aligned to a page size. This limitation
1575 * will be lifted in the future.
1577 * @as: #AddressSpace to be accessed
1578 * @addr: address within that address space
1579 * @len: length of the area to be checked
1580 * @is_write: indicates the transfer direction
1582 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1584 /* address_space_map: map a physical memory region into a host virtual address
1586 * May map a subset of the requested range, given by and returned in @plen.
1587 * May return %NULL if resources needed to perform the mapping are exhausted.
1588 * Use only for reads OR writes - not for read-modify-write operations.
1589 * Use cpu_register_map_client() to know when retrying the map operation is
1590 * likely to succeed.
1592 * @as: #AddressSpace to be accessed
1593 * @addr: address within that address space
1594 * @plen: pointer to length of buffer; updated on return
1595 * @is_write: indicates the transfer direction
1597 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1598 hwaddr
*plen
, bool is_write
);
1600 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1602 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1603 * the amount of memory that was actually read or written by the caller.
1605 * @as: #AddressSpace used
1606 * @addr: address within that address space
1607 * @len: buffer length as returned by address_space_map()
1608 * @access_len: amount of data actually transferred
1609 * @is_write: indicates the transfer direction
1611 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1612 int is_write
, hwaddr access_len
);
1615 /* Internal functions, part of the implementation of address_space_read. */
1616 MemTxResult
address_space_read_continue(AddressSpace
*as
, hwaddr addr
,
1617 MemTxAttrs attrs
, uint8_t *buf
,
1618 int len
, hwaddr addr1
, hwaddr l
,
1620 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
1621 MemTxAttrs attrs
, uint8_t *buf
, int len
);
1622 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
1624 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
1627 return memory_region_is_ram(mr
) &&
1628 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
1630 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
1631 memory_region_is_romd(mr
);
1636 * address_space_read: read from an address space.
1638 * Return a MemTxResult indicating whether the operation succeeded
1639 * or failed (eg unassigned memory, device rejected the transaction,
1642 * @as: #AddressSpace to be accessed
1643 * @addr: address within that address space
1644 * @attrs: memory transaction attributes
1645 * @buf: buffer with the data transferred
1647 static inline __attribute__((__always_inline__
))
1648 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1649 uint8_t *buf
, int len
)
1651 MemTxResult result
= MEMTX_OK
;
1656 if (__builtin_constant_p(len
)) {
1660 mr
= address_space_translate(as
, addr
, &addr1
, &l
, false);
1661 if (len
== l
&& memory_access_is_direct(mr
, false)) {
1662 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
1663 memcpy(buf
, ptr
, len
);
1665 result
= address_space_read_continue(as
, addr
, attrs
, buf
, len
,
1671 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
1677 * address_space_read_cached: read from a cached RAM region
1679 * @cache: Cached region to be addressed
1680 * @addr: address relative to the base of the RAM region
1681 * @buf: buffer with the data transferred
1682 * @len: length of the data transferred
1685 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1688 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1689 address_space_read(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);
1693 * address_space_write_cached: write to a cached RAM region
1695 * @cache: Cached region to be addressed
1696 * @addr: address relative to the base of the RAM region
1697 * @buf: buffer with the data transferred
1698 * @len: length of the data transferred
1701 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1704 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1705 address_space_write(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);