2 * Physical memory management API
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
17 #ifndef CONFIG_USER_ONLY
19 #include "exec/cpu-common.h"
20 #ifndef CONFIG_USER_ONLY
21 #include "exec/hwaddr.h"
23 #include "exec/memattrs.h"
24 #include "exec/ramlist.h"
25 #include "qemu/queue.h"
26 #include "qemu/int128.h"
27 #include "qemu/notify.h"
28 #include "qom/object.h"
31 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
33 #define MAX_PHYS_ADDR_SPACE_BITS 62
34 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
36 #define TYPE_MEMORY_REGION "qemu:memory-region"
37 #define MEMORY_REGION(obj) \
38 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
40 typedef struct MemoryRegionOps MemoryRegionOps
;
41 typedef struct MemoryRegionMmio MemoryRegionMmio
;
43 struct MemoryRegionMmio
{
44 CPUReadMemoryFunc
*read
[3];
45 CPUWriteMemoryFunc
*write
[3];
48 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
50 /* See address_space_translate: bit 0 is read, bit 1 is write. */
58 struct IOMMUTLBEntry
{
59 AddressSpace
*target_as
;
61 hwaddr translated_addr
;
62 hwaddr addr_mask
; /* 0xfff = 4k translation */
63 IOMMUAccessFlags perm
;
67 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
68 * register with one or multiple IOMMU Notifier capability bit(s).
71 IOMMU_NOTIFIER_NONE
= 0,
72 /* Notify cache invalidations */
73 IOMMU_NOTIFIER_UNMAP
= 0x1,
74 /* Notify entry changes (newly created entries) */
75 IOMMU_NOTIFIER_MAP
= 0x2,
78 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
81 typedef void (*IOMMUNotify
)(struct IOMMUNotifier
*notifier
,
84 struct IOMMUNotifier
{
86 IOMMUNotifierFlag notifier_flags
;
87 /* Notify for address space range start <= addr <= end */
90 QLIST_ENTRY(IOMMUNotifier
) node
;
92 typedef struct IOMMUNotifier IOMMUNotifier
;
94 static inline void iommu_notifier_init(IOMMUNotifier
*n
, IOMMUNotify fn
,
95 IOMMUNotifierFlag flags
,
96 hwaddr start
, hwaddr end
)
99 n
->notifier_flags
= flags
;
104 /* New-style MMIO accessors can indicate that the transaction failed.
105 * A zero (MEMTX_OK) response means success; anything else is a failure
106 * of some kind. The memory subsystem will bitwise-OR together results
107 * if it is synthesizing an operation from multiple smaller accesses.
110 #define MEMTX_ERROR (1U << 0) /* device returned an error */
111 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
112 typedef uint32_t MemTxResult
;
115 * Memory region callbacks
117 struct MemoryRegionOps
{
118 /* Read from the memory region. @addr is relative to @mr; @size is
120 uint64_t (*read
)(void *opaque
,
123 /* Write to the memory region. @addr is relative to @mr; @size is
125 void (*write
)(void *opaque
,
130 MemTxResult (*read_with_attrs
)(void *opaque
,
135 MemTxResult (*write_with_attrs
)(void *opaque
,
141 enum device_endian endianness
;
142 /* Guest-visible constraints: */
144 /* If nonzero, specify bounds on access sizes beyond which a machine
147 unsigned min_access_size
;
148 unsigned max_access_size
;
149 /* If true, unaligned accesses are supported. Otherwise unaligned
150 * accesses throw machine checks.
154 * If present, and returns #false, the transaction is not accepted
155 * by the device (and results in machine dependent behaviour such
156 * as a machine check exception).
158 bool (*accepts
)(void *opaque
, hwaddr addr
,
159 unsigned size
, bool is_write
);
161 /* Internal implementation constraints: */
163 /* If nonzero, specifies the minimum size implemented. Smaller sizes
164 * will be rounded upwards and a partial result will be returned.
166 unsigned min_access_size
;
167 /* If nonzero, specifies the maximum size implemented. Larger sizes
168 * will be done as a series of accesses with smaller sizes.
170 unsigned max_access_size
;
171 /* If true, unaligned accesses are supported. Otherwise all accesses
172 * are converted to (possibly multiple) naturally aligned accesses.
177 /* If .read and .write are not present, old_mmio may be used for
178 * backwards compatibility with old mmio registration
180 const MemoryRegionMmio old_mmio
;
183 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps
;
185 struct MemoryRegionIOMMUOps
{
186 /* Return a TLB entry that contains a given address. */
187 IOMMUTLBEntry (*translate
)(MemoryRegion
*iommu
, hwaddr addr
, bool is_write
);
188 /* Returns minimum supported page size */
189 uint64_t (*get_min_page_size
)(MemoryRegion
*iommu
);
190 /* Called when IOMMU Notifier flag changed */
191 void (*notify_flag_changed
)(MemoryRegion
*iommu
,
192 IOMMUNotifierFlag old_flags
,
193 IOMMUNotifierFlag new_flags
);
196 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
197 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
199 struct MemoryRegion
{
202 /* All fields are private - violators will be prosecuted */
204 /* The following fields should fit in a cache line */
208 bool readonly
; /* For RAM regions */
210 bool flush_coalesced_mmio
;
212 uint8_t dirty_log_mask
;
215 const MemoryRegionIOMMUOps
*iommu_ops
;
217 const MemoryRegionOps
*ops
;
219 MemoryRegion
*container
;
222 void (*destructor
)(MemoryRegion
*mr
);
227 bool warning_printed
; /* For reservations */
228 uint8_t vga_logging_count
;
232 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
233 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
234 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
236 unsigned ioeventfd_nb
;
237 MemoryRegionIoeventfd
*ioeventfds
;
238 QLIST_HEAD(, IOMMUNotifier
) iommu_notify
;
239 IOMMUNotifierFlag iommu_notify_flags
;
242 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
243 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
246 * MemoryListener: callbacks structure for updates to the physical memory map
248 * Allows a component to adjust to changes in the guest-visible memory map.
249 * Use with memory_listener_register() and memory_listener_unregister().
251 struct MemoryListener
{
252 void (*begin
)(MemoryListener
*listener
);
253 void (*commit
)(MemoryListener
*listener
);
254 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
255 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
256 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
257 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
259 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
261 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
262 void (*log_global_start
)(MemoryListener
*listener
);
263 void (*log_global_stop
)(MemoryListener
*listener
);
264 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
265 bool match_data
, uint64_t data
, EventNotifier
*e
);
266 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
267 bool match_data
, uint64_t data
, EventNotifier
*e
);
268 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
269 hwaddr addr
, hwaddr len
);
270 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
271 hwaddr addr
, hwaddr len
);
272 /* Lower = earlier (during add), later (during del) */
274 AddressSpace
*address_space
;
275 QTAILQ_ENTRY(MemoryListener
) link
;
276 QTAILQ_ENTRY(MemoryListener
) link_as
;
280 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
282 struct AddressSpace
{
283 /* All fields are private. */
290 /* Accessed via RCU. */
291 struct FlatView
*current_map
;
294 struct MemoryRegionIoeventfd
*ioeventfds
;
295 struct AddressSpaceDispatch
*dispatch
;
296 struct AddressSpaceDispatch
*next_dispatch
;
297 MemoryListener dispatch_listener
;
298 QTAILQ_HEAD(memory_listeners_as
, MemoryListener
) listeners
;
299 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
303 * MemoryRegionSection: describes a fragment of a #MemoryRegion
305 * @mr: the region, or %NULL if empty
306 * @address_space: the address space the region is mapped in
307 * @offset_within_region: the beginning of the section, relative to @mr's start
308 * @size: the size of the section; will not exceed @mr's boundaries
309 * @offset_within_address_space: the address of the first byte of the section
310 * relative to the region's address space
311 * @readonly: writes to this section are ignored
313 struct MemoryRegionSection
{
315 AddressSpace
*address_space
;
316 hwaddr offset_within_region
;
318 hwaddr offset_within_address_space
;
323 * memory_region_init: Initialize a memory region
325 * The region typically acts as a container for other memory regions. Use
326 * memory_region_add_subregion() to add subregions.
328 * @mr: the #MemoryRegion to be initialized
329 * @owner: the object that tracks the region's reference count
330 * @name: used for debugging; not visible to the user or ABI
331 * @size: size of the region; any subregions beyond this size will be clipped
333 void memory_region_init(MemoryRegion
*mr
,
334 struct Object
*owner
,
339 * memory_region_ref: Add 1 to a memory region's reference count
341 * Whenever memory regions are accessed outside the BQL, they need to be
342 * preserved against hot-unplug. MemoryRegions actually do not have their
343 * own reference count; they piggyback on a QOM object, their "owner".
344 * This function adds a reference to the owner.
346 * All MemoryRegions must have an owner if they can disappear, even if the
347 * device they belong to operates exclusively under the BQL. This is because
348 * the region could be returned at any time by memory_region_find, and this
349 * is usually under guest control.
351 * @mr: the #MemoryRegion
353 void memory_region_ref(MemoryRegion
*mr
);
356 * memory_region_unref: Remove 1 to a memory region's reference count
358 * Whenever memory regions are accessed outside the BQL, they need to be
359 * preserved against hot-unplug. MemoryRegions actually do not have their
360 * own reference count; they piggyback on a QOM object, their "owner".
361 * This function removes a reference to the owner and possibly destroys it.
363 * @mr: the #MemoryRegion
365 void memory_region_unref(MemoryRegion
*mr
);
368 * memory_region_init_io: Initialize an I/O memory region.
370 * Accesses into the region will cause the callbacks in @ops to be called.
371 * if @size is nonzero, subregions will be clipped to @size.
373 * @mr: the #MemoryRegion to be initialized.
374 * @owner: the object that tracks the region's reference count
375 * @ops: a structure containing read and write callbacks to be used when
376 * I/O is performed on the region.
377 * @opaque: passed to the read and write callbacks of the @ops structure.
378 * @name: used for debugging; not visible to the user or ABI
379 * @size: size of the region.
381 void memory_region_init_io(MemoryRegion
*mr
,
382 struct Object
*owner
,
383 const MemoryRegionOps
*ops
,
389 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
390 * region will modify memory directly.
392 * @mr: the #MemoryRegion to be initialized.
393 * @owner: the object that tracks the region's reference count
394 * @name: Region name, becomes part of RAMBlock name used in migration stream
395 * must be unique within any device
396 * @size: size of the region.
397 * @errp: pointer to Error*, to store an error if it happens.
399 void memory_region_init_ram(MemoryRegion
*mr
,
400 struct Object
*owner
,
406 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
407 * RAM. Accesses into the region will
408 * modify memory directly. Only an initial
409 * portion of this RAM is actually used.
410 * The used size can change across reboots.
412 * @mr: the #MemoryRegion to be initialized.
413 * @owner: the object that tracks the region's reference count
414 * @name: Region name, becomes part of RAMBlock name used in migration stream
415 * must be unique within any device
416 * @size: used size of the region.
417 * @max_size: max size of the region.
418 * @resized: callback to notify owner about used size change.
419 * @errp: pointer to Error*, to store an error if it happens.
421 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
422 struct Object
*owner
,
426 void (*resized
)(const char*,
432 * memory_region_init_ram_from_file: Initialize RAM memory region with a
435 * @mr: the #MemoryRegion to be initialized.
436 * @owner: the object that tracks the region's reference count
437 * @name: Region name, becomes part of RAMBlock name used in migration stream
438 * must be unique within any device
439 * @size: size of the region.
440 * @share: %true if memory must be mmaped with the MAP_SHARED flag
441 * @path: the path in which to allocate the RAM.
442 * @errp: pointer to Error*, to store an error if it happens.
444 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
445 struct Object
*owner
,
454 * memory_region_init_ram_ptr: Initialize RAM memory region from a
455 * user-provided pointer. Accesses into the
456 * region will modify memory directly.
458 * @mr: the #MemoryRegion to be initialized.
459 * @owner: the object that tracks the region's reference count
460 * @name: Region name, becomes part of RAMBlock name used in migration stream
461 * must be unique within any device
462 * @size: size of the region.
463 * @ptr: memory to be mapped; must contain at least @size bytes.
465 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
466 struct Object
*owner
,
472 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
473 * a user-provided pointer.
475 * A RAM device represents a mapping to a physical device, such as to a PCI
476 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
477 * into the VM address space and access to the region will modify memory
478 * directly. However, the memory region should not be included in a memory
479 * dump (device may not be enabled/mapped at the time of the dump), and
480 * operations incompatible with manipulating MMIO should be avoided. Replaces
483 * @mr: the #MemoryRegion to be initialized.
484 * @owner: the object that tracks the region's reference count
485 * @name: the name of the region.
486 * @size: size of the region.
487 * @ptr: memory to be mapped; must contain at least @size bytes.
489 void memory_region_init_ram_device_ptr(MemoryRegion
*mr
,
490 struct Object
*owner
,
496 * memory_region_init_alias: Initialize a memory region that aliases all or a
497 * part of another memory region.
499 * @mr: the #MemoryRegion to be initialized.
500 * @owner: the object that tracks the region's reference count
501 * @name: used for debugging; not visible to the user or ABI
502 * @orig: the region to be referenced; @mr will be equivalent to
503 * @orig between @offset and @offset + @size - 1.
504 * @offset: start of the section in @orig to be referenced.
505 * @size: size of the region.
507 void memory_region_init_alias(MemoryRegion
*mr
,
508 struct Object
*owner
,
515 * memory_region_init_rom: Initialize a ROM memory region.
517 * This has the same effect as calling memory_region_init_ram()
518 * and then marking the resulting region read-only with
519 * memory_region_set_readonly().
521 * @mr: the #MemoryRegion to be initialized.
522 * @owner: the object that tracks the region's reference count
523 * @name: Region name, becomes part of RAMBlock name used in migration stream
524 * must be unique within any device
525 * @size: size of the region.
526 * @errp: pointer to Error*, to store an error if it happens.
528 void memory_region_init_rom(MemoryRegion
*mr
,
529 struct Object
*owner
,
535 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
536 * handled via callbacks.
538 * @mr: the #MemoryRegion to be initialized.
539 * @owner: the object that tracks the region's reference count
540 * @ops: callbacks for write access handling (must not be NULL).
541 * @name: Region name, becomes part of RAMBlock name used in migration stream
542 * must be unique within any device
543 * @size: size of the region.
544 * @errp: pointer to Error*, to store an error if it happens.
546 void memory_region_init_rom_device(MemoryRegion
*mr
,
547 struct Object
*owner
,
548 const MemoryRegionOps
*ops
,
555 * memory_region_init_reservation: Initialize a memory region that reserves
558 * A reservation region primariy serves debugging purposes. It claims I/O
559 * space that is not supposed to be handled by QEMU itself. Any access via
560 * the memory API will cause an abort().
561 * This function is deprecated. Use memory_region_init_io() with NULL
564 * @mr: the #MemoryRegion to be initialized
565 * @owner: the object that tracks the region's reference count
566 * @name: used for debugging; not visible to the user or ABI
567 * @size: size of the region.
569 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
574 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
578 * memory_region_init_iommu: Initialize a memory region that translates
581 * An IOMMU region translates addresses and forwards accesses to a target
584 * @mr: the #MemoryRegion to be initialized
585 * @owner: the object that tracks the region's reference count
586 * @ops: a function that translates addresses into the @target region
587 * @name: used for debugging; not visible to the user or ABI
588 * @size: size of the region.
590 void memory_region_init_iommu(MemoryRegion
*mr
,
591 struct Object
*owner
,
592 const MemoryRegionIOMMUOps
*ops
,
597 * memory_region_owner: get a memory region's owner.
599 * @mr: the memory region being queried.
601 struct Object
*memory_region_owner(MemoryRegion
*mr
);
604 * memory_region_size: get a memory region's size.
606 * @mr: the memory region being queried.
608 uint64_t memory_region_size(MemoryRegion
*mr
);
611 * memory_region_is_ram: check whether a memory region is random access
613 * Returns %true is a memory region is random access.
615 * @mr: the memory region being queried
617 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
623 * memory_region_is_ram_device: check whether a memory region is a ram device
625 * Returns %true is a memory region is a device backed ram region
627 * @mr: the memory region being queried
629 bool memory_region_is_ram_device(MemoryRegion
*mr
);
632 * memory_region_is_romd: check whether a memory region is in ROMD mode
634 * Returns %true if a memory region is a ROM device and currently set to allow
637 * @mr: the memory region being queried
639 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
641 return mr
->rom_device
&& mr
->romd_mode
;
645 * memory_region_is_iommu: check whether a memory region is an iommu
647 * Returns %true is a memory region is an iommu.
649 * @mr: the memory region being queried
651 static inline bool memory_region_is_iommu(MemoryRegion
*mr
)
654 return memory_region_is_iommu(mr
->alias
);
656 return mr
->iommu_ops
;
661 * memory_region_iommu_get_min_page_size: get minimum supported page size
664 * Returns minimum supported page size for an iommu.
666 * @mr: the memory region being queried
668 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion
*mr
);
671 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
673 * The notification type will be decided by entry.perm bits:
675 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
676 * - For MAP (newly added entry) notifies: set entry.perm to the
677 * permission of the page (which is definitely !IOMMU_NONE).
679 * Note: for any IOMMU implementation, an in-place mapping change
680 * should be notified with an UNMAP followed by a MAP.
682 * @mr: the memory region that was changed
683 * @entry: the new entry in the IOMMU translation table. The entry
684 * replaces all old entries for the same virtual I/O address range.
685 * Deleted entries have .@perm == 0.
687 void memory_region_notify_iommu(MemoryRegion
*mr
,
688 IOMMUTLBEntry entry
);
691 * memory_region_register_iommu_notifier: register a notifier for changes to
692 * IOMMU translation entries.
694 * @mr: the memory region to observe
695 * @n: the IOMMUNotifier to be added; the notify callback receives a
696 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
697 * ceases to be valid on exit from the notifier.
699 void memory_region_register_iommu_notifier(MemoryRegion
*mr
,
703 * memory_region_iommu_replay: replay existing IOMMU translations to
704 * a notifier with the minimum page granularity returned by
705 * mr->iommu_ops->get_page_size().
707 * @mr: the memory region to observe
708 * @n: the notifier to which to replay iommu mappings
709 * @is_write: Whether to treat the replay as a translate "write"
712 void memory_region_iommu_replay(MemoryRegion
*mr
, IOMMUNotifier
*n
,
716 * memory_region_iommu_replay_all: replay existing IOMMU translations
717 * to all the notifiers registered.
719 * @mr: the memory region to observe
721 void memory_region_iommu_replay_all(MemoryRegion
*mr
);
724 * memory_region_unregister_iommu_notifier: unregister a notifier for
725 * changes to IOMMU translation entries.
727 * @mr: the memory region which was observed and for which notity_stopped()
729 * @n: the notifier to be removed.
731 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
,
735 * memory_region_name: get a memory region's name
737 * Returns the string that was used to initialize the memory region.
739 * @mr: the memory region being queried
741 const char *memory_region_name(const MemoryRegion
*mr
);
744 * memory_region_is_logging: return whether a memory region is logging writes
746 * Returns %true if the memory region is logging writes for the given client
748 * @mr: the memory region being queried
749 * @client: the client being queried
751 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
754 * memory_region_get_dirty_log_mask: return the clients for which a
755 * memory region is logging writes.
757 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
758 * are the bit indices.
760 * @mr: the memory region being queried
762 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
765 * memory_region_is_rom: check whether a memory region is ROM
767 * Returns %true is a memory region is read-only memory.
769 * @mr: the memory region being queried
771 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
773 return mr
->ram
&& mr
->readonly
;
778 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
780 * Returns a file descriptor backing a file-based RAM memory region,
781 * or -1 if the region is not a file-based RAM memory region.
783 * @mr: the RAM or alias memory region being queried.
785 int memory_region_get_fd(MemoryRegion
*mr
);
788 * memory_region_set_fd: Mark a RAM memory region as backed by a
791 * This function is typically used after memory_region_init_ram_ptr().
793 * @mr: the memory region being queried.
794 * @fd: the file descriptor that backs @mr.
796 void memory_region_set_fd(MemoryRegion
*mr
, int fd
);
799 * memory_region_from_host: Convert a pointer into a RAM memory region
800 * and an offset within it.
802 * Given a host pointer inside a RAM memory region (created with
803 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
804 * the MemoryRegion and the offset within it.
806 * Use with care; by the time this function returns, the returned pointer is
807 * not protected by RCU anymore. If the caller is not within an RCU critical
808 * section and does not hold the iothread lock, it must have other means of
809 * protecting the pointer, such as a reference to the region that includes
810 * the incoming ram_addr_t.
812 * @mr: the memory region being queried.
814 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
817 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
819 * Returns a host pointer to a RAM memory region (created with
820 * memory_region_init_ram() or memory_region_init_ram_ptr()).
822 * Use with care; by the time this function returns, the returned pointer is
823 * not protected by RCU anymore. If the caller is not within an RCU critical
824 * section and does not hold the iothread lock, it must have other means of
825 * protecting the pointer, such as a reference to the region that includes
826 * the incoming ram_addr_t.
828 * @mr: the memory region being queried.
830 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
832 /* memory_region_ram_resize: Resize a RAM region.
834 * Only legal before guest might have detected the memory size: e.g. on
835 * incoming migration, or right after reset.
837 * @mr: a memory region created with @memory_region_init_resizeable_ram.
838 * @newsize: the new size the region
839 * @errp: pointer to Error*, to store an error if it happens.
841 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
845 * memory_region_set_log: Turn dirty logging on or off for a region.
847 * Turns dirty logging on or off for a specified client (display, migration).
848 * Only meaningful for RAM regions.
850 * @mr: the memory region being updated.
851 * @log: whether dirty logging is to be enabled or disabled.
852 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
854 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
857 * memory_region_get_dirty: Check whether a range of bytes is dirty
858 * for a specified client.
860 * Checks whether a range of bytes has been written to since the last
861 * call to memory_region_reset_dirty() with the same @client. Dirty logging
864 * @mr: the memory region being queried.
865 * @addr: the address (relative to the start of the region) being queried.
866 * @size: the size of the range being queried.
867 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
870 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
871 hwaddr size
, unsigned client
);
874 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
876 * Marks a range of bytes as dirty, after it has been dirtied outside
879 * @mr: the memory region being dirtied.
880 * @addr: the address (relative to the start of the region) being dirtied.
881 * @size: size of the range being dirtied.
883 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
887 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
888 * for a specified client. It clears them.
890 * Checks whether a range of bytes has been written to since the last
891 * call to memory_region_reset_dirty() with the same @client. Dirty logging
894 * @mr: the memory region being queried.
895 * @addr: the address (relative to the start of the region) being queried.
896 * @size: the size of the range being queried.
897 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
900 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
901 hwaddr size
, unsigned client
);
903 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
904 * any external TLBs (e.g. kvm)
906 * Flushes dirty information from accelerators such as kvm and vhost-net
907 * and makes it available to users of the memory API.
909 * @mr: the region being flushed.
911 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
914 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
917 * Marks a range of pages as no longer dirty.
919 * @mr: the region being updated.
920 * @addr: the start of the subrange being cleaned.
921 * @size: the size of the subrange being cleaned.
922 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
925 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
926 hwaddr size
, unsigned client
);
929 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
931 * Allows a memory region to be marked as read-only (turning it into a ROM).
932 * only useful on RAM regions.
934 * @mr: the region being updated.
935 * @readonly: whether rhe region is to be ROM or RAM.
937 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
940 * memory_region_rom_device_set_romd: enable/disable ROMD mode
942 * Allows a ROM device (initialized with memory_region_init_rom_device() to
943 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
944 * device is mapped to guest memory and satisfies read access directly.
945 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
946 * Writes are always handled by the #MemoryRegion.write function.
948 * @mr: the memory region to be updated
949 * @romd_mode: %true to put the region into ROMD mode
951 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
954 * memory_region_set_coalescing: Enable memory coalescing for the region.
956 * Enabled writes to a region to be queued for later processing. MMIO ->write
957 * callbacks may be delayed until a non-coalesced MMIO is issued.
958 * Only useful for IO regions. Roughly similar to write-combining hardware.
960 * @mr: the memory region to be write coalesced
962 void memory_region_set_coalescing(MemoryRegion
*mr
);
965 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
968 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
969 * Multiple calls can be issued coalesced disjoint ranges.
971 * @mr: the memory region to be updated.
972 * @offset: the start of the range within the region to be coalesced.
973 * @size: the size of the subrange to be coalesced.
975 void memory_region_add_coalescing(MemoryRegion
*mr
,
980 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
982 * Disables any coalescing caused by memory_region_set_coalescing() or
983 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
986 * @mr: the memory region to be updated.
988 void memory_region_clear_coalescing(MemoryRegion
*mr
);
991 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
994 * Ensure that pending coalesced MMIO request are flushed before the memory
995 * region is accessed. This property is automatically enabled for all regions
996 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
998 * @mr: the memory region to be updated.
1000 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
1003 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1006 * Clear the automatic coalesced MMIO flushing enabled via
1007 * memory_region_set_flush_coalesced. Note that this service has no effect on
1008 * memory regions that have MMIO coalescing enabled for themselves. For them,
1009 * automatic flushing will stop once coalescing is disabled.
1011 * @mr: the memory region to be updated.
1013 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
1016 * memory_region_set_global_locking: Declares the access processing requires
1017 * QEMU's global lock.
1019 * When this is invoked, accesses to the memory region will be processed while
1020 * holding the global lock of QEMU. This is the default behavior of memory
1023 * @mr: the memory region to be updated.
1025 void memory_region_set_global_locking(MemoryRegion
*mr
);
1028 * memory_region_clear_global_locking: Declares that access processing does
1029 * not depend on the QEMU global lock.
1031 * By clearing this property, accesses to the memory region will be processed
1032 * outside of QEMU's global lock (unless the lock is held on when issuing the
1033 * access request). In this case, the device model implementing the access
1034 * handlers is responsible for synchronization of concurrency.
1036 * @mr: the memory region to be updated.
1038 void memory_region_clear_global_locking(MemoryRegion
*mr
);
1041 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1042 * is written to a location.
1044 * Marks a word in an IO region (initialized with memory_region_init_io())
1045 * as a trigger for an eventfd event. The I/O callback will not be called.
1046 * The caller must be prepared to handle failure (that is, take the required
1047 * action if the callback _is_ called).
1049 * @mr: the memory region being updated.
1050 * @addr: the address within @mr that is to be monitored
1051 * @size: the size of the access to trigger the eventfd
1052 * @match_data: whether to match against @data, instead of just @addr
1053 * @data: the data to match against the guest write
1054 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1056 void memory_region_add_eventfd(MemoryRegion
*mr
,
1064 * memory_region_del_eventfd: Cancel an eventfd.
1066 * Cancels an eventfd trigger requested by a previous
1067 * memory_region_add_eventfd() call.
1069 * @mr: the memory region being updated.
1070 * @addr: the address within @mr that is to be monitored
1071 * @size: the size of the access to trigger the eventfd
1072 * @match_data: whether to match against @data, instead of just @addr
1073 * @data: the data to match against the guest write
1074 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
1076 void memory_region_del_eventfd(MemoryRegion
*mr
,
1084 * memory_region_add_subregion: Add a subregion to a container.
1086 * Adds a subregion at @offset. The subregion may not overlap with other
1087 * subregions (except for those explicitly marked as overlapping). A region
1088 * may only be added once as a subregion (unless removed with
1089 * memory_region_del_subregion()); use memory_region_init_alias() if you
1090 * want a region to be a subregion in multiple locations.
1092 * @mr: the region to contain the new subregion; must be a container
1093 * initialized with memory_region_init().
1094 * @offset: the offset relative to @mr where @subregion is added.
1095 * @subregion: the subregion to be added.
1097 void memory_region_add_subregion(MemoryRegion
*mr
,
1099 MemoryRegion
*subregion
);
1101 * memory_region_add_subregion_overlap: Add a subregion to a container
1104 * Adds a subregion at @offset. The subregion may overlap with other
1105 * subregions. Conflicts are resolved by having a higher @priority hide a
1106 * lower @priority. Subregions without priority are taken as @priority 0.
1107 * A region may only be added once as a subregion (unless removed with
1108 * memory_region_del_subregion()); use memory_region_init_alias() if you
1109 * want a region to be a subregion in multiple locations.
1111 * @mr: the region to contain the new subregion; must be a container
1112 * initialized with memory_region_init().
1113 * @offset: the offset relative to @mr where @subregion is added.
1114 * @subregion: the subregion to be added.
1115 * @priority: used for resolving overlaps; highest priority wins.
1117 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1119 MemoryRegion
*subregion
,
1123 * memory_region_get_ram_addr: Get the ram address associated with a memory
1126 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1128 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1130 * memory_region_del_subregion: Remove a subregion.
1132 * Removes a subregion from its container.
1134 * @mr: the container to be updated.
1135 * @subregion: the region being removed; must be a current subregion of @mr.
1137 void memory_region_del_subregion(MemoryRegion
*mr
,
1138 MemoryRegion
*subregion
);
1141 * memory_region_set_enabled: dynamically enable or disable a region
1143 * Enables or disables a memory region. A disabled memory region
1144 * ignores all accesses to itself and its subregions. It does not
1145 * obscure sibling subregions with lower priority - it simply behaves as
1146 * if it was removed from the hierarchy.
1148 * Regions default to being enabled.
1150 * @mr: the region to be updated
1151 * @enabled: whether to enable or disable the region
1153 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1156 * memory_region_set_address: dynamically update the address of a region
1158 * Dynamically updates the address of a region, relative to its container.
1159 * May be used on regions are currently part of a memory hierarchy.
1161 * @mr: the region to be updated
1162 * @addr: new address, relative to container region
1164 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1167 * memory_region_set_size: dynamically update the size of a region.
1169 * Dynamically updates the size of a region.
1171 * @mr: the region to be updated
1172 * @size: used size of the region.
1174 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1177 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1179 * Dynamically updates the offset into the target region that an alias points
1180 * to, as if the fourth argument to memory_region_init_alias() has changed.
1182 * @mr: the #MemoryRegion to be updated; should be an alias.
1183 * @offset: the new offset into the target memory region
1185 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1189 * memory_region_present: checks if an address relative to a @container
1190 * translates into #MemoryRegion within @container
1192 * Answer whether a #MemoryRegion within @container covers the address
1195 * @container: a #MemoryRegion within which @addr is a relative address
1196 * @addr: the area within @container to be searched
1198 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1201 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1202 * into any address space.
1204 * @mr: a #MemoryRegion which should be checked if it's mapped
1206 bool memory_region_is_mapped(MemoryRegion
*mr
);
1209 * memory_region_find: translate an address/size relative to a
1210 * MemoryRegion into a #MemoryRegionSection.
1212 * Locates the first #MemoryRegion within @mr that overlaps the range
1213 * given by @addr and @size.
1215 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1216 * It will have the following characteristics:
1217 * .@size = 0 iff no overlap was found
1218 * .@mr is non-%NULL iff an overlap was found
1220 * Remember that in the return value the @offset_within_region is
1221 * relative to the returned region (in the .@mr field), not to the
1224 * Similarly, the .@offset_within_address_space is relative to the
1225 * address space that contains both regions, the passed and the
1226 * returned one. However, in the special case where the @mr argument
1227 * has no container (and thus is the root of the address space), the
1228 * following will hold:
1229 * .@offset_within_address_space >= @addr
1230 * .@offset_within_address_space + .@size <= @addr + @size
1232 * @mr: a MemoryRegion within which @addr is a relative address
1233 * @addr: start of the area within @as to be searched
1234 * @size: size of the area to be searched
1236 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1237 hwaddr addr
, uint64_t size
);
1240 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1242 * Synchronizes the dirty page log for all address spaces.
1244 void memory_global_dirty_log_sync(void);
1247 * memory_region_transaction_begin: Start a transaction.
1249 * During a transaction, changes will be accumulated and made visible
1250 * only when the transaction ends (is committed).
1252 void memory_region_transaction_begin(void);
1255 * memory_region_transaction_commit: Commit a transaction and make changes
1256 * visible to the guest.
1258 void memory_region_transaction_commit(void);
1261 * memory_listener_register: register callbacks to be called when memory
1262 * sections are mapped or unmapped into an address
1265 * @listener: an object containing the callbacks to be called
1266 * @filter: if non-%NULL, only regions in this address space will be observed
1268 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1271 * memory_listener_unregister: undo the effect of memory_listener_register()
1273 * @listener: an object containing the callbacks to be removed
1275 void memory_listener_unregister(MemoryListener
*listener
);
1278 * memory_global_dirty_log_start: begin dirty logging for all regions
1280 void memory_global_dirty_log_start(void);
1283 * memory_global_dirty_log_stop: end dirty logging for all regions
1285 void memory_global_dirty_log_stop(void);
1287 void mtree_info(fprintf_function mon_printf
, void *f
, bool flatview
);
1290 * memory_region_dispatch_read: perform a read directly to the specified
1293 * @mr: #MemoryRegion to access
1294 * @addr: address within that region
1295 * @pval: pointer to uint64_t which the data is written to
1296 * @size: size of the access in bytes
1297 * @attrs: memory transaction attributes to use for the access
1299 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1305 * memory_region_dispatch_write: perform a write directly to the specified
1308 * @mr: #MemoryRegion to access
1309 * @addr: address within that region
1310 * @data: data to write
1311 * @size: size of the access in bytes
1312 * @attrs: memory transaction attributes to use for the access
1314 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1321 * address_space_init: initializes an address space
1323 * @as: an uninitialized #AddressSpace
1324 * @root: a #MemoryRegion that routes addresses for the address space
1325 * @name: an address space name. The name is only used for debugging
1328 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1331 * address_space_init_shareable: return an address space for a memory region,
1332 * creating it if it does not already exist
1334 * @root: a #MemoryRegion that routes addresses for the address space
1335 * @name: an address space name. The name is only used for debugging
1338 * This function will return a pointer to an existing AddressSpace
1339 * which was initialized with the specified MemoryRegion, or it will
1340 * create and initialize one if it does not already exist. The ASes
1341 * are reference-counted, so the memory will be freed automatically
1342 * when the AddressSpace is destroyed via address_space_destroy.
1344 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
,
1348 * address_space_destroy: destroy an address space
1350 * Releases all resources associated with an address space. After an address space
1351 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1354 * @as: address space to be destroyed
1356 void address_space_destroy(AddressSpace
*as
);
1359 * address_space_rw: read from or write to an address space.
1361 * Return a MemTxResult indicating whether the operation succeeded
1362 * or failed (eg unassigned memory, device rejected the transaction,
1365 * @as: #AddressSpace to be accessed
1366 * @addr: address within that address space
1367 * @attrs: memory transaction attributes
1368 * @buf: buffer with the data transferred
1369 * @is_write: indicates the transfer direction
1371 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1372 MemTxAttrs attrs
, uint8_t *buf
,
1373 int len
, bool is_write
);
1376 * address_space_write: write to address space.
1378 * Return a MemTxResult indicating whether the operation succeeded
1379 * or failed (eg unassigned memory, device rejected the transaction,
1382 * @as: #AddressSpace to be accessed
1383 * @addr: address within that address space
1384 * @attrs: memory transaction attributes
1385 * @buf: buffer with the data transferred
1387 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1389 const uint8_t *buf
, int len
);
1391 /* address_space_ld*: load from an address space
1392 * address_space_st*: store to an address space
1394 * These functions perform a load or store of the byte, word,
1395 * longword or quad to the specified address within the AddressSpace.
1396 * The _le suffixed functions treat the data as little endian;
1397 * _be indicates big endian; no suffix indicates "same endianness
1400 * The "guest CPU endianness" accessors are deprecated for use outside
1401 * target-* code; devices should be CPU-agnostic and use either the LE
1402 * or the BE accessors.
1404 * @as #AddressSpace to be accessed
1405 * @addr: address within that address space
1406 * @val: data value, for stores
1407 * @attrs: memory transaction attributes
1408 * @result: location to write the success/failure of the transaction;
1409 * if NULL, this information is discarded
1411 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1412 MemTxAttrs attrs
, MemTxResult
*result
);
1413 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1414 MemTxAttrs attrs
, MemTxResult
*result
);
1415 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1416 MemTxAttrs attrs
, MemTxResult
*result
);
1417 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1418 MemTxAttrs attrs
, MemTxResult
*result
);
1419 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1420 MemTxAttrs attrs
, MemTxResult
*result
);
1421 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1422 MemTxAttrs attrs
, MemTxResult
*result
);
1423 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1424 MemTxAttrs attrs
, MemTxResult
*result
);
1425 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1426 MemTxAttrs attrs
, MemTxResult
*result
);
1427 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1428 MemTxAttrs attrs
, MemTxResult
*result
);
1429 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1430 MemTxAttrs attrs
, MemTxResult
*result
);
1431 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1432 MemTxAttrs attrs
, MemTxResult
*result
);
1433 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1434 MemTxAttrs attrs
, MemTxResult
*result
);
1435 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1436 MemTxAttrs attrs
, MemTxResult
*result
);
1437 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1438 MemTxAttrs attrs
, MemTxResult
*result
);
1440 uint32_t ldub_phys(AddressSpace
*as
, hwaddr addr
);
1441 uint32_t lduw_le_phys(AddressSpace
*as
, hwaddr addr
);
1442 uint32_t lduw_be_phys(AddressSpace
*as
, hwaddr addr
);
1443 uint32_t ldl_le_phys(AddressSpace
*as
, hwaddr addr
);
1444 uint32_t ldl_be_phys(AddressSpace
*as
, hwaddr addr
);
1445 uint64_t ldq_le_phys(AddressSpace
*as
, hwaddr addr
);
1446 uint64_t ldq_be_phys(AddressSpace
*as
, hwaddr addr
);
1447 void stb_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1448 void stw_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1449 void stw_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1450 void stl_le_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1451 void stl_be_phys(AddressSpace
*as
, hwaddr addr
, uint32_t val
);
1452 void stq_le_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1453 void stq_be_phys(AddressSpace
*as
, hwaddr addr
, uint64_t val
);
1455 struct MemoryRegionCache
{
1461 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .as = NULL })
1463 /* address_space_cache_init: prepare for repeated access to a physical
1466 * @cache: #MemoryRegionCache to be filled
1467 * @as: #AddressSpace to be accessed
1468 * @addr: address within that address space
1469 * @len: length of buffer
1470 * @is_write: indicates the transfer direction
1472 * Will only work with RAM, and may map a subset of the requested range by
1473 * returning a value that is less than @len. On failure, return a negative
1476 * Because it only works with RAM, this function can be used for
1477 * read-modify-write operations. In this case, is_write should be %true.
1479 * Note that addresses passed to the address_space_*_cached functions
1480 * are relative to @addr.
1482 int64_t address_space_cache_init(MemoryRegionCache
*cache
,
1489 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
1491 * @cache: The #MemoryRegionCache to operate on.
1492 * @addr: The first physical address that was written, relative to the
1493 * address that was passed to @address_space_cache_init.
1494 * @access_len: The number of bytes that were written starting at @addr.
1496 void address_space_cache_invalidate(MemoryRegionCache
*cache
,
1501 * address_space_cache_destroy: free a #MemoryRegionCache
1503 * @cache: The #MemoryRegionCache whose memory should be released.
1505 void address_space_cache_destroy(MemoryRegionCache
*cache
);
1507 /* address_space_ld*_cached: load from a cached #MemoryRegion
1508 * address_space_st*_cached: store into a cached #MemoryRegion
1510 * These functions perform a load or store of the byte, word,
1511 * longword or quad to the specified address. The address is
1512 * a physical address in the AddressSpace, but it must lie within
1513 * a #MemoryRegion that was mapped with address_space_cache_init.
1515 * The _le suffixed functions treat the data as little endian;
1516 * _be indicates big endian; no suffix indicates "same endianness
1519 * The "guest CPU endianness" accessors are deprecated for use outside
1520 * target-* code; devices should be CPU-agnostic and use either the LE
1521 * or the BE accessors.
1523 * @cache: previously initialized #MemoryRegionCache to be accessed
1524 * @addr: address within the address space
1525 * @val: data value, for stores
1526 * @attrs: memory transaction attributes
1527 * @result: location to write the success/failure of the transaction;
1528 * if NULL, this information is discarded
1530 uint32_t address_space_ldub_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1531 MemTxAttrs attrs
, MemTxResult
*result
);
1532 uint32_t address_space_lduw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1533 MemTxAttrs attrs
, MemTxResult
*result
);
1534 uint32_t address_space_lduw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1535 MemTxAttrs attrs
, MemTxResult
*result
);
1536 uint32_t address_space_ldl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1537 MemTxAttrs attrs
, MemTxResult
*result
);
1538 uint32_t address_space_ldl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1539 MemTxAttrs attrs
, MemTxResult
*result
);
1540 uint64_t address_space_ldq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1541 MemTxAttrs attrs
, MemTxResult
*result
);
1542 uint64_t address_space_ldq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1543 MemTxAttrs attrs
, MemTxResult
*result
);
1544 void address_space_stb_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1545 MemTxAttrs attrs
, MemTxResult
*result
);
1546 void address_space_stw_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1547 MemTxAttrs attrs
, MemTxResult
*result
);
1548 void address_space_stw_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1549 MemTxAttrs attrs
, MemTxResult
*result
);
1550 void address_space_stl_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1551 MemTxAttrs attrs
, MemTxResult
*result
);
1552 void address_space_stl_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
,
1553 MemTxAttrs attrs
, MemTxResult
*result
);
1554 void address_space_stq_le_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1555 MemTxAttrs attrs
, MemTxResult
*result
);
1556 void address_space_stq_be_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
,
1557 MemTxAttrs attrs
, MemTxResult
*result
);
1559 uint32_t ldub_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1560 uint32_t lduw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1561 uint32_t lduw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1562 uint32_t ldl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1563 uint32_t ldl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1564 uint64_t ldq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1565 uint64_t ldq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
);
1566 void stb_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1567 void stw_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1568 void stw_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1569 void stl_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1570 void stl_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint32_t val
);
1571 void stq_le_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1572 void stq_be_phys_cached(MemoryRegionCache
*cache
, hwaddr addr
, uint64_t val
);
1573 /* address_space_get_iotlb_entry: translate an address into an IOTLB
1574 * entry. Should be called from an RCU critical section.
1576 IOMMUTLBEntry
address_space_get_iotlb_entry(AddressSpace
*as
, hwaddr addr
,
1579 /* address_space_translate: translate an address range into an address space
1580 * into a MemoryRegion and an address range into that section. Should be
1581 * called from an RCU critical section, to avoid that the last reference
1582 * to the returned region disappears after address_space_translate returns.
1584 * @as: #AddressSpace to be accessed
1585 * @addr: address within that address space
1586 * @xlat: pointer to address within the returned memory region section's
1588 * @len: pointer to length
1589 * @is_write: indicates the transfer direction
1591 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1592 hwaddr
*xlat
, hwaddr
*len
,
1595 /* address_space_access_valid: check for validity of accessing an address
1598 * Check whether memory is assigned to the given address space range, and
1599 * access is permitted by any IOMMU regions that are active for the address
1602 * For now, addr and len should be aligned to a page size. This limitation
1603 * will be lifted in the future.
1605 * @as: #AddressSpace to be accessed
1606 * @addr: address within that address space
1607 * @len: length of the area to be checked
1608 * @is_write: indicates the transfer direction
1610 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1612 /* address_space_map: map a physical memory region into a host virtual address
1614 * May map a subset of the requested range, given by and returned in @plen.
1615 * May return %NULL if resources needed to perform the mapping are exhausted.
1616 * Use only for reads OR writes - not for read-modify-write operations.
1617 * Use cpu_register_map_client() to know when retrying the map operation is
1618 * likely to succeed.
1620 * @as: #AddressSpace to be accessed
1621 * @addr: address within that address space
1622 * @plen: pointer to length of buffer; updated on return
1623 * @is_write: indicates the transfer direction
1625 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1626 hwaddr
*plen
, bool is_write
);
1628 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1630 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1631 * the amount of memory that was actually read or written by the caller.
1633 * @as: #AddressSpace used
1634 * @addr: address within that address space
1635 * @len: buffer length as returned by address_space_map()
1636 * @access_len: amount of data actually transferred
1637 * @is_write: indicates the transfer direction
1639 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1640 int is_write
, hwaddr access_len
);
1643 /* Internal functions, part of the implementation of address_space_read. */
1644 MemTxResult
address_space_read_continue(AddressSpace
*as
, hwaddr addr
,
1645 MemTxAttrs attrs
, uint8_t *buf
,
1646 int len
, hwaddr addr1
, hwaddr l
,
1648 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
1649 MemTxAttrs attrs
, uint8_t *buf
, int len
);
1650 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
1652 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
1655 return memory_region_is_ram(mr
) &&
1656 !mr
->readonly
&& !memory_region_is_ram_device(mr
);
1658 return (memory_region_is_ram(mr
) && !memory_region_is_ram_device(mr
)) ||
1659 memory_region_is_romd(mr
);
1664 * address_space_read: read from an address space.
1666 * Return a MemTxResult indicating whether the operation succeeded
1667 * or failed (eg unassigned memory, device rejected the transaction,
1670 * @as: #AddressSpace to be accessed
1671 * @addr: address within that address space
1672 * @attrs: memory transaction attributes
1673 * @buf: buffer with the data transferred
1675 static inline __attribute__((__always_inline__
))
1676 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1677 uint8_t *buf
, int len
)
1679 MemTxResult result
= MEMTX_OK
;
1684 if (__builtin_constant_p(len
)) {
1688 mr
= address_space_translate(as
, addr
, &addr1
, &l
, false);
1689 if (len
== l
&& memory_access_is_direct(mr
, false)) {
1690 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
1691 memcpy(buf
, ptr
, len
);
1693 result
= address_space_read_continue(as
, addr
, attrs
, buf
, len
,
1699 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);
1705 * address_space_read_cached: read from a cached RAM region
1707 * @cache: Cached region to be addressed
1708 * @addr: address relative to the base of the RAM region
1709 * @buf: buffer with the data transferred
1710 * @len: length of the data transferred
1713 address_space_read_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1716 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1717 address_space_read(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);
1721 * address_space_write_cached: write to a cached RAM region
1723 * @cache: Cached region to be addressed
1724 * @addr: address relative to the base of the RAM region
1725 * @buf: buffer with the data transferred
1726 * @len: length of the data transferred
1729 address_space_write_cached(MemoryRegionCache
*cache
, hwaddr addr
,
1732 assert(addr
< cache
->len
&& len
<= cache
->len
- addr
);
1733 address_space_write(cache
->as
, cache
->xlat
+ addr
, MEMTXATTRS_UNSPECIFIED
, buf
, len
);