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 #define DIRTY_MEMORY_VGA 0
20 #define DIRTY_MEMORY_CODE 1
21 #define DIRTY_MEMORY_MIGRATION 2
22 #define DIRTY_MEMORY_NUM 3 /* num of dirty bits */
24 #include "exec/cpu-common.h"
25 #ifndef CONFIG_USER_ONLY
26 #include "exec/hwaddr.h"
28 #include "exec/memattrs.h"
29 #include "qemu/queue.h"
30 #include "qemu/int128.h"
31 #include "qemu/notify.h"
32 #include "qom/object.h"
35 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
37 #define MAX_PHYS_ADDR_SPACE_BITS 62
38 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
40 #define TYPE_MEMORY_REGION "qemu:memory-region"
41 #define MEMORY_REGION(obj) \
42 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
44 typedef struct MemoryRegionOps MemoryRegionOps
;
45 typedef struct MemoryRegionMmio MemoryRegionMmio
;
47 struct MemoryRegionMmio
{
48 CPUReadMemoryFunc
*read
[3];
49 CPUWriteMemoryFunc
*write
[3];
52 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
54 /* See address_space_translate: bit 0 is read, bit 1 is write. */
62 struct IOMMUTLBEntry
{
63 AddressSpace
*target_as
;
65 hwaddr translated_addr
;
66 hwaddr addr_mask
; /* 0xfff = 4k translation */
67 IOMMUAccessFlags perm
;
70 /* New-style MMIO accessors can indicate that the transaction failed.
71 * A zero (MEMTX_OK) response means success; anything else is a failure
72 * of some kind. The memory subsystem will bitwise-OR together results
73 * if it is synthesizing an operation from multiple smaller accesses.
76 #define MEMTX_ERROR (1U << 0) /* device returned an error */
77 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
78 typedef uint32_t MemTxResult
;
81 * Memory region callbacks
83 struct MemoryRegionOps
{
84 /* Read from the memory region. @addr is relative to @mr; @size is
86 uint64_t (*read
)(void *opaque
,
89 /* Write to the memory region. @addr is relative to @mr; @size is
91 void (*write
)(void *opaque
,
96 MemTxResult (*read_with_attrs
)(void *opaque
,
101 MemTxResult (*write_with_attrs
)(void *opaque
,
107 enum device_endian endianness
;
108 /* Guest-visible constraints: */
110 /* If nonzero, specify bounds on access sizes beyond which a machine
113 unsigned min_access_size
;
114 unsigned max_access_size
;
115 /* If true, unaligned accesses are supported. Otherwise unaligned
116 * accesses throw machine checks.
120 * If present, and returns #false, the transaction is not accepted
121 * by the device (and results in machine dependent behaviour such
122 * as a machine check exception).
124 bool (*accepts
)(void *opaque
, hwaddr addr
,
125 unsigned size
, bool is_write
);
127 /* Internal implementation constraints: */
129 /* If nonzero, specifies the minimum size implemented. Smaller sizes
130 * will be rounded upwards and a partial result will be returned.
132 unsigned min_access_size
;
133 /* If nonzero, specifies the maximum size implemented. Larger sizes
134 * will be done as a series of accesses with smaller sizes.
136 unsigned max_access_size
;
137 /* If true, unaligned accesses are supported. Otherwise all accesses
138 * are converted to (possibly multiple) naturally aligned accesses.
143 /* If .read and .write are not present, old_mmio may be used for
144 * backwards compatibility with old mmio registration
146 const MemoryRegionMmio old_mmio
;
149 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps
;
151 struct MemoryRegionIOMMUOps
{
152 /* Return a TLB entry that contains a given address. */
153 IOMMUTLBEntry (*translate
)(MemoryRegion
*iommu
, hwaddr addr
, bool is_write
);
154 /* Returns minimum supported page size */
155 uint64_t (*get_min_page_size
)(MemoryRegion
*iommu
);
156 /* Called when the first notifier is set */
157 void (*notify_started
)(MemoryRegion
*iommu
);
158 /* Called when the last notifier is removed */
159 void (*notify_stopped
)(MemoryRegion
*iommu
);
162 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
163 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
165 struct MemoryRegion
{
168 /* All fields are private - violators will be prosecuted */
170 /* The following fields should fit in a cache line */
174 bool readonly
; /* For RAM regions */
176 bool flush_coalesced_mmio
;
178 uint8_t dirty_log_mask
;
181 const MemoryRegionIOMMUOps
*iommu_ops
;
183 const MemoryRegionOps
*ops
;
185 MemoryRegion
*container
;
188 void (*destructor
)(MemoryRegion
*mr
);
193 bool warning_printed
; /* For reservations */
194 uint8_t vga_logging_count
;
198 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
199 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
200 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
202 unsigned ioeventfd_nb
;
203 MemoryRegionIoeventfd
*ioeventfds
;
204 NotifierList iommu_notify
;
208 * MemoryListener: callbacks structure for updates to the physical memory map
210 * Allows a component to adjust to changes in the guest-visible memory map.
211 * Use with memory_listener_register() and memory_listener_unregister().
213 struct MemoryListener
{
214 void (*begin
)(MemoryListener
*listener
);
215 void (*commit
)(MemoryListener
*listener
);
216 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
217 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
218 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
219 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
221 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
223 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
224 void (*log_global_start
)(MemoryListener
*listener
);
225 void (*log_global_stop
)(MemoryListener
*listener
);
226 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
227 bool match_data
, uint64_t data
, EventNotifier
*e
);
228 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
229 bool match_data
, uint64_t data
, EventNotifier
*e
);
230 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
231 hwaddr addr
, hwaddr len
);
232 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
233 hwaddr addr
, hwaddr len
);
234 /* Lower = earlier (during add), later (during del) */
236 AddressSpace
*address_space_filter
;
237 QTAILQ_ENTRY(MemoryListener
) link
;
241 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
243 struct AddressSpace
{
244 /* All fields are private. */
251 /* Accessed via RCU. */
252 struct FlatView
*current_map
;
255 struct MemoryRegionIoeventfd
*ioeventfds
;
256 struct AddressSpaceDispatch
*dispatch
;
257 struct AddressSpaceDispatch
*next_dispatch
;
258 MemoryListener dispatch_listener
;
260 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
264 * MemoryRegionSection: describes a fragment of a #MemoryRegion
266 * @mr: the region, or %NULL if empty
267 * @address_space: the address space the region is mapped in
268 * @offset_within_region: the beginning of the section, relative to @mr's start
269 * @size: the size of the section; will not exceed @mr's boundaries
270 * @offset_within_address_space: the address of the first byte of the section
271 * relative to the region's address space
272 * @readonly: writes to this section are ignored
274 struct MemoryRegionSection
{
276 AddressSpace
*address_space
;
277 hwaddr offset_within_region
;
279 hwaddr offset_within_address_space
;
284 * memory_region_init: Initialize a memory region
286 * The region typically acts as a container for other memory regions. Use
287 * memory_region_add_subregion() to add subregions.
289 * @mr: the #MemoryRegion to be initialized
290 * @owner: the object that tracks the region's reference count
291 * @name: used for debugging; not visible to the user or ABI
292 * @size: size of the region; any subregions beyond this size will be clipped
294 void memory_region_init(MemoryRegion
*mr
,
295 struct Object
*owner
,
300 * memory_region_ref: Add 1 to a memory region's reference count
302 * Whenever memory regions are accessed outside the BQL, they need to be
303 * preserved against hot-unplug. MemoryRegions actually do not have their
304 * own reference count; they piggyback on a QOM object, their "owner".
305 * This function adds a reference to the owner.
307 * All MemoryRegions must have an owner if they can disappear, even if the
308 * device they belong to operates exclusively under the BQL. This is because
309 * the region could be returned at any time by memory_region_find, and this
310 * is usually under guest control.
312 * @mr: the #MemoryRegion
314 void memory_region_ref(MemoryRegion
*mr
);
317 * memory_region_unref: Remove 1 to a memory region's reference count
319 * Whenever memory regions are accessed outside the BQL, they need to be
320 * preserved against hot-unplug. MemoryRegions actually do not have their
321 * own reference count; they piggyback on a QOM object, their "owner".
322 * This function removes a reference to the owner and possibly destroys it.
324 * @mr: the #MemoryRegion
326 void memory_region_unref(MemoryRegion
*mr
);
329 * memory_region_init_io: Initialize an I/O memory region.
331 * Accesses into the region will cause the callbacks in @ops to be called.
332 * if @size is nonzero, subregions will be clipped to @size.
334 * @mr: the #MemoryRegion to be initialized.
335 * @owner: the object that tracks the region's reference count
336 * @ops: a structure containing read and write callbacks to be used when
337 * I/O is performed on the region.
338 * @opaque: passed to the read and write callbacks of the @ops structure.
339 * @name: used for debugging; not visible to the user or ABI
340 * @size: size of the region.
342 void memory_region_init_io(MemoryRegion
*mr
,
343 struct Object
*owner
,
344 const MemoryRegionOps
*ops
,
350 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
351 * region will modify memory directly.
353 * @mr: the #MemoryRegion to be initialized.
354 * @owner: the object that tracks the region's reference count
355 * @name: the name of the region.
356 * @size: size of the region.
357 * @errp: pointer to Error*, to store an error if it happens.
359 void memory_region_init_ram(MemoryRegion
*mr
,
360 struct Object
*owner
,
366 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
367 * RAM. Accesses into the region will
368 * modify memory directly. Only an initial
369 * portion of this RAM is actually used.
370 * The used size can change across reboots.
372 * @mr: the #MemoryRegion to be initialized.
373 * @owner: the object that tracks the region's reference count
374 * @name: the name of the region.
375 * @size: used size of the region.
376 * @max_size: max size of the region.
377 * @resized: callback to notify owner about used size change.
378 * @errp: pointer to Error*, to store an error if it happens.
380 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
381 struct Object
*owner
,
385 void (*resized
)(const char*,
391 * memory_region_init_ram_from_file: Initialize RAM memory region with a
394 * @mr: the #MemoryRegion to be initialized.
395 * @owner: the object that tracks the region's reference count
396 * @name: the name of the region.
397 * @size: size of the region.
398 * @share: %true if memory must be mmaped with the MAP_SHARED flag
399 * @path: the path in which to allocate the RAM.
400 * @errp: pointer to Error*, to store an error if it happens.
402 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
403 struct Object
*owner
,
412 * memory_region_init_ram_ptr: Initialize RAM memory region from a
413 * user-provided pointer. Accesses into the
414 * region will modify memory directly.
416 * @mr: the #MemoryRegion to be initialized.
417 * @owner: the object that tracks the region's reference count
418 * @name: the name of the region.
419 * @size: size of the region.
420 * @ptr: memory to be mapped; must contain at least @size bytes.
422 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
423 struct Object
*owner
,
429 * memory_region_init_alias: Initialize a memory region that aliases all or a
430 * part of another memory region.
432 * @mr: the #MemoryRegion to be initialized.
433 * @owner: the object that tracks the region's reference count
434 * @name: used for debugging; not visible to the user or ABI
435 * @orig: the region to be referenced; @mr will be equivalent to
436 * @orig between @offset and @offset + @size - 1.
437 * @offset: start of the section in @orig to be referenced.
438 * @size: size of the region.
440 void memory_region_init_alias(MemoryRegion
*mr
,
441 struct Object
*owner
,
448 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
449 * handled via callbacks.
451 * If NULL callbacks pointer is given, then I/O space is not supposed to be
452 * handled by QEMU itself. Any access via the memory API will cause an abort().
454 * @mr: the #MemoryRegion to be initialized.
455 * @owner: the object that tracks the region's reference count
456 * @ops: callbacks for write access handling.
457 * @name: the name of the region.
458 * @size: size of the region.
459 * @errp: pointer to Error*, to store an error if it happens.
461 void memory_region_init_rom_device(MemoryRegion
*mr
,
462 struct Object
*owner
,
463 const MemoryRegionOps
*ops
,
470 * memory_region_init_reservation: Initialize a memory region that reserves
473 * A reservation region primariy serves debugging purposes. It claims I/O
474 * space that is not supposed to be handled by QEMU itself. Any access via
475 * the memory API will cause an abort().
476 * This function is deprecated. Use memory_region_init_io() with NULL
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 * @size: size of the region.
484 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
489 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
493 * memory_region_init_iommu: Initialize a memory region that translates
496 * An IOMMU region translates addresses and forwards accesses to a target
499 * @mr: the #MemoryRegion to be initialized
500 * @owner: the object that tracks the region's reference count
501 * @ops: a function that translates addresses into the @target region
502 * @name: used for debugging; not visible to the user or ABI
503 * @size: size of the region.
505 void memory_region_init_iommu(MemoryRegion
*mr
,
506 struct Object
*owner
,
507 const MemoryRegionIOMMUOps
*ops
,
512 * memory_region_owner: get a memory region's owner.
514 * @mr: the memory region being queried.
516 struct Object
*memory_region_owner(MemoryRegion
*mr
);
519 * memory_region_size: get a memory region's size.
521 * @mr: the memory region being queried.
523 uint64_t memory_region_size(MemoryRegion
*mr
);
526 * memory_region_is_ram: check whether a memory region is random access
528 * Returns %true is a memory region is random access.
530 * @mr: the memory region being queried
532 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
538 * memory_region_is_skip_dump: check whether a memory region should not be
541 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
543 * @mr: the memory region being queried
545 bool memory_region_is_skip_dump(MemoryRegion
*mr
);
548 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
551 * @mr: the memory region being queried
553 void memory_region_set_skip_dump(MemoryRegion
*mr
);
556 * memory_region_is_romd: check whether a memory region is in ROMD mode
558 * Returns %true if a memory region is a ROM device and currently set to allow
561 * @mr: the memory region being queried
563 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
565 return mr
->rom_device
&& mr
->romd_mode
;
569 * memory_region_is_iommu: check whether a memory region is an iommu
571 * Returns %true is a memory region is an iommu.
573 * @mr: the memory region being queried
575 static inline bool memory_region_is_iommu(MemoryRegion
*mr
)
577 return mr
->iommu_ops
;
582 * memory_region_iommu_get_min_page_size: get minimum supported page size
585 * Returns minimum supported page size for an iommu.
587 * @mr: the memory region being queried
589 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion
*mr
);
592 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
594 * @mr: the memory region that was changed
595 * @entry: the new entry in the IOMMU translation table. The entry
596 * replaces all old entries for the same virtual I/O address range.
597 * Deleted entries have .@perm == 0.
599 void memory_region_notify_iommu(MemoryRegion
*mr
,
600 IOMMUTLBEntry entry
);
603 * memory_region_register_iommu_notifier: register a notifier for changes to
604 * IOMMU translation entries.
606 * @mr: the memory region to observe
607 * @n: the notifier to be added; the notifier receives a pointer to an
608 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
609 * valid on exit from the notifier.
611 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
);
614 * memory_region_iommu_replay: replay existing IOMMU translations to
615 * a notifier with the minimum page granularity returned by
616 * mr->iommu_ops->get_page_size().
618 * @mr: the memory region to observe
619 * @n: the notifier to which to replay iommu mappings
620 * @is_write: Whether to treat the replay as a translate "write"
623 void memory_region_iommu_replay(MemoryRegion
*mr
, Notifier
*n
, bool is_write
);
626 * memory_region_unregister_iommu_notifier: unregister a notifier for
627 * changes to IOMMU translation entries.
629 * @mr: the memory region which was observed and for which notity_stopped()
631 * @n: the notifier to be removed.
633 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
);
636 * memory_region_name: get a memory region's name
638 * Returns the string that was used to initialize the memory region.
640 * @mr: the memory region being queried
642 const char *memory_region_name(const MemoryRegion
*mr
);
645 * memory_region_is_logging: return whether a memory region is logging writes
647 * Returns %true if the memory region is logging writes for the given client
649 * @mr: the memory region being queried
650 * @client: the client being queried
652 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
655 * memory_region_get_dirty_log_mask: return the clients for which a
656 * memory region is logging writes.
658 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
659 * are the bit indices.
661 * @mr: the memory region being queried
663 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
666 * memory_region_is_rom: check whether a memory region is ROM
668 * Returns %true is a memory region is read-only memory.
670 * @mr: the memory region being queried
672 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
674 return mr
->ram
&& mr
->readonly
;
679 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
681 * Returns a file descriptor backing a file-based RAM memory region,
682 * or -1 if the region is not a file-based RAM memory region.
684 * @mr: the RAM or alias memory region being queried.
686 int memory_region_get_fd(MemoryRegion
*mr
);
689 * memory_region_set_fd: Mark a RAM memory region as backed by a
692 * This function is typically used after memory_region_init_ram_ptr().
694 * @mr: the memory region being queried.
695 * @fd: the file descriptor that backs @mr.
697 void memory_region_set_fd(MemoryRegion
*mr
, int fd
);
700 * memory_region_from_host: Convert a pointer into a RAM memory region
701 * and an offset within it.
703 * Given a host pointer inside a RAM memory region (created with
704 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
705 * the MemoryRegion and the offset within it.
707 * Use with care; by the time this function returns, the returned pointer is
708 * not protected by RCU anymore. If the caller is not within an RCU critical
709 * section and does not hold the iothread lock, it must have other means of
710 * protecting the pointer, such as a reference to the region that includes
711 * the incoming ram_addr_t.
713 * @mr: the memory region being queried.
715 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
718 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
720 * Returns a host pointer to a RAM memory region (created with
721 * memory_region_init_ram() or memory_region_init_ram_ptr()).
723 * Use with care; by the time this function returns, the returned pointer is
724 * not protected by RCU anymore. If the caller is not within an RCU critical
725 * section and does not hold the iothread lock, it must have other means of
726 * protecting the pointer, such as a reference to the region that includes
727 * the incoming ram_addr_t.
729 * @mr: the memory region being queried.
731 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
733 /* memory_region_ram_resize: Resize a RAM region.
735 * Only legal before guest might have detected the memory size: e.g. on
736 * incoming migration, or right after reset.
738 * @mr: a memory region created with @memory_region_init_resizeable_ram.
739 * @newsize: the new size the region
740 * @errp: pointer to Error*, to store an error if it happens.
742 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
746 * memory_region_set_log: Turn dirty logging on or off for a region.
748 * Turns dirty logging on or off for a specified client (display, migration).
749 * Only meaningful for RAM regions.
751 * @mr: the memory region being updated.
752 * @log: whether dirty logging is to be enabled or disabled.
753 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
755 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
758 * memory_region_get_dirty: Check whether a range of bytes is dirty
759 * for a specified client.
761 * Checks whether a range of bytes has been written to since the last
762 * call to memory_region_reset_dirty() with the same @client. Dirty logging
765 * @mr: the memory region being queried.
766 * @addr: the address (relative to the start of the region) being queried.
767 * @size: the size of the range being queried.
768 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
771 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
772 hwaddr size
, unsigned client
);
775 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
777 * Marks a range of bytes as dirty, after it has been dirtied outside
780 * @mr: the memory region being dirtied.
781 * @addr: the address (relative to the start of the region) being dirtied.
782 * @size: size of the range being dirtied.
784 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
788 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
789 * for a specified client. It clears them.
791 * Checks whether a range of bytes has been written to since the last
792 * call to memory_region_reset_dirty() with the same @client. Dirty logging
795 * @mr: the memory region being queried.
796 * @addr: the address (relative to the start of the region) being queried.
797 * @size: the size of the range being queried.
798 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
801 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
802 hwaddr size
, unsigned client
);
804 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
805 * any external TLBs (e.g. kvm)
807 * Flushes dirty information from accelerators such as kvm and vhost-net
808 * and makes it available to users of the memory API.
810 * @mr: the region being flushed.
812 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
815 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
818 * Marks a range of pages as no longer dirty.
820 * @mr: the region being updated.
821 * @addr: the start of the subrange being cleaned.
822 * @size: the size of the subrange being cleaned.
823 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
826 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
827 hwaddr size
, unsigned client
);
830 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
832 * Allows a memory region to be marked as read-only (turning it into a ROM).
833 * only useful on RAM regions.
835 * @mr: the region being updated.
836 * @readonly: whether rhe region is to be ROM or RAM.
838 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
841 * memory_region_rom_device_set_romd: enable/disable ROMD mode
843 * Allows a ROM device (initialized with memory_region_init_rom_device() to
844 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
845 * device is mapped to guest memory and satisfies read access directly.
846 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
847 * Writes are always handled by the #MemoryRegion.write function.
849 * @mr: the memory region to be updated
850 * @romd_mode: %true to put the region into ROMD mode
852 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
855 * memory_region_set_coalescing: Enable memory coalescing for the region.
857 * Enabled writes to a region to be queued for later processing. MMIO ->write
858 * callbacks may be delayed until a non-coalesced MMIO is issued.
859 * Only useful for IO regions. Roughly similar to write-combining hardware.
861 * @mr: the memory region to be write coalesced
863 void memory_region_set_coalescing(MemoryRegion
*mr
);
866 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
869 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
870 * Multiple calls can be issued coalesced disjoint ranges.
872 * @mr: the memory region to be updated.
873 * @offset: the start of the range within the region to be coalesced.
874 * @size: the size of the subrange to be coalesced.
876 void memory_region_add_coalescing(MemoryRegion
*mr
,
881 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
883 * Disables any coalescing caused by memory_region_set_coalescing() or
884 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
887 * @mr: the memory region to be updated.
889 void memory_region_clear_coalescing(MemoryRegion
*mr
);
892 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
895 * Ensure that pending coalesced MMIO request are flushed before the memory
896 * region is accessed. This property is automatically enabled for all regions
897 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
899 * @mr: the memory region to be updated.
901 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
904 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
907 * Clear the automatic coalesced MMIO flushing enabled via
908 * memory_region_set_flush_coalesced. Note that this service has no effect on
909 * memory regions that have MMIO coalescing enabled for themselves. For them,
910 * automatic flushing will stop once coalescing is disabled.
912 * @mr: the memory region to be updated.
914 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
917 * memory_region_set_global_locking: Declares the access processing requires
918 * QEMU's global lock.
920 * When this is invoked, accesses to the memory region will be processed while
921 * holding the global lock of QEMU. This is the default behavior of memory
924 * @mr: the memory region to be updated.
926 void memory_region_set_global_locking(MemoryRegion
*mr
);
929 * memory_region_clear_global_locking: Declares that access processing does
930 * not depend on the QEMU global lock.
932 * By clearing this property, accesses to the memory region will be processed
933 * outside of QEMU's global lock (unless the lock is held on when issuing the
934 * access request). In this case, the device model implementing the access
935 * handlers is responsible for synchronization of concurrency.
937 * @mr: the memory region to be updated.
939 void memory_region_clear_global_locking(MemoryRegion
*mr
);
942 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
943 * is written to a location.
945 * Marks a word in an IO region (initialized with memory_region_init_io())
946 * as a trigger for an eventfd event. The I/O callback will not be called.
947 * The caller must be prepared to handle failure (that is, take the required
948 * action if the callback _is_ called).
950 * @mr: the memory region being updated.
951 * @addr: the address within @mr that is to be monitored
952 * @size: the size of the access to trigger the eventfd
953 * @match_data: whether to match against @data, instead of just @addr
954 * @data: the data to match against the guest write
955 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
957 void memory_region_add_eventfd(MemoryRegion
*mr
,
965 * memory_region_del_eventfd: Cancel an eventfd.
967 * Cancels an eventfd trigger requested by a previous
968 * memory_region_add_eventfd() call.
970 * @mr: the memory region being updated.
971 * @addr: the address within @mr that is to be monitored
972 * @size: the size of the access to trigger the eventfd
973 * @match_data: whether to match against @data, instead of just @addr
974 * @data: the data to match against the guest write
975 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
977 void memory_region_del_eventfd(MemoryRegion
*mr
,
985 * memory_region_add_subregion: Add a subregion to a container.
987 * Adds a subregion at @offset. The subregion may not overlap with other
988 * subregions (except for those explicitly marked as overlapping). A region
989 * may only be added once as a subregion (unless removed with
990 * memory_region_del_subregion()); use memory_region_init_alias() if you
991 * want a region to be a subregion in multiple locations.
993 * @mr: the region to contain the new subregion; must be a container
994 * initialized with memory_region_init().
995 * @offset: the offset relative to @mr where @subregion is added.
996 * @subregion: the subregion to be added.
998 void memory_region_add_subregion(MemoryRegion
*mr
,
1000 MemoryRegion
*subregion
);
1002 * memory_region_add_subregion_overlap: Add a subregion to a container
1005 * Adds a subregion at @offset. The subregion may overlap with other
1006 * subregions. Conflicts are resolved by having a higher @priority hide a
1007 * lower @priority. Subregions without priority are taken as @priority 0.
1008 * A region may only be added once as a subregion (unless removed with
1009 * memory_region_del_subregion()); use memory_region_init_alias() if you
1010 * want a region to be a subregion in multiple locations.
1012 * @mr: the region to contain the new subregion; must be a container
1013 * initialized with memory_region_init().
1014 * @offset: the offset relative to @mr where @subregion is added.
1015 * @subregion: the subregion to be added.
1016 * @priority: used for resolving overlaps; highest priority wins.
1018 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1020 MemoryRegion
*subregion
,
1024 * memory_region_get_ram_addr: Get the ram address associated with a memory
1027 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1029 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1031 * memory_region_del_subregion: Remove a subregion.
1033 * Removes a subregion from its container.
1035 * @mr: the container to be updated.
1036 * @subregion: the region being removed; must be a current subregion of @mr.
1038 void memory_region_del_subregion(MemoryRegion
*mr
,
1039 MemoryRegion
*subregion
);
1042 * memory_region_set_enabled: dynamically enable or disable a region
1044 * Enables or disables a memory region. A disabled memory region
1045 * ignores all accesses to itself and its subregions. It does not
1046 * obscure sibling subregions with lower priority - it simply behaves as
1047 * if it was removed from the hierarchy.
1049 * Regions default to being enabled.
1051 * @mr: the region to be updated
1052 * @enabled: whether to enable or disable the region
1054 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1057 * memory_region_set_address: dynamically update the address of a region
1059 * Dynamically updates the address of a region, relative to its container.
1060 * May be used on regions are currently part of a memory hierarchy.
1062 * @mr: the region to be updated
1063 * @addr: new address, relative to container region
1065 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1068 * memory_region_set_size: dynamically update the size of a region.
1070 * Dynamically updates the size of a region.
1072 * @mr: the region to be updated
1073 * @size: used size of the region.
1075 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1078 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1080 * Dynamically updates the offset into the target region that an alias points
1081 * to, as if the fourth argument to memory_region_init_alias() has changed.
1083 * @mr: the #MemoryRegion to be updated; should be an alias.
1084 * @offset: the new offset into the target memory region
1086 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1090 * memory_region_present: checks if an address relative to a @container
1091 * translates into #MemoryRegion within @container
1093 * Answer whether a #MemoryRegion within @container covers the address
1096 * @container: a #MemoryRegion within which @addr is a relative address
1097 * @addr: the area within @container to be searched
1099 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1102 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1103 * into any address space.
1105 * @mr: a #MemoryRegion which should be checked if it's mapped
1107 bool memory_region_is_mapped(MemoryRegion
*mr
);
1110 * memory_region_find: translate an address/size relative to a
1111 * MemoryRegion into a #MemoryRegionSection.
1113 * Locates the first #MemoryRegion within @mr that overlaps the range
1114 * given by @addr and @size.
1116 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1117 * It will have the following characteristics:
1118 * .@size = 0 iff no overlap was found
1119 * .@mr is non-%NULL iff an overlap was found
1121 * Remember that in the return value the @offset_within_region is
1122 * relative to the returned region (in the .@mr field), not to the
1125 * Similarly, the .@offset_within_address_space is relative to the
1126 * address space that contains both regions, the passed and the
1127 * returned one. However, in the special case where the @mr argument
1128 * has no container (and thus is the root of the address space), the
1129 * following will hold:
1130 * .@offset_within_address_space >= @addr
1131 * .@offset_within_address_space + .@size <= @addr + @size
1133 * @mr: a MemoryRegion within which @addr is a relative address
1134 * @addr: start of the area within @as to be searched
1135 * @size: size of the area to be searched
1137 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1138 hwaddr addr
, uint64_t size
);
1141 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
1143 * Synchronizes the dirty page log for an entire address space.
1144 * @as: the address space that contains the memory being synchronized
1146 void address_space_sync_dirty_bitmap(AddressSpace
*as
);
1149 * memory_region_transaction_begin: Start a transaction.
1151 * During a transaction, changes will be accumulated and made visible
1152 * only when the transaction ends (is committed).
1154 void memory_region_transaction_begin(void);
1157 * memory_region_transaction_commit: Commit a transaction and make changes
1158 * visible to the guest.
1160 void memory_region_transaction_commit(void);
1163 * memory_listener_register: register callbacks to be called when memory
1164 * sections are mapped or unmapped into an address
1167 * @listener: an object containing the callbacks to be called
1168 * @filter: if non-%NULL, only regions in this address space will be observed
1170 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1173 * memory_listener_unregister: undo the effect of memory_listener_register()
1175 * @listener: an object containing the callbacks to be removed
1177 void memory_listener_unregister(MemoryListener
*listener
);
1180 * memory_global_dirty_log_start: begin dirty logging for all regions
1182 void memory_global_dirty_log_start(void);
1185 * memory_global_dirty_log_stop: end dirty logging for all regions
1187 void memory_global_dirty_log_stop(void);
1189 void mtree_info(fprintf_function mon_printf
, void *f
);
1192 * memory_region_dispatch_read: perform a read directly to the specified
1195 * @mr: #MemoryRegion to access
1196 * @addr: address within that region
1197 * @pval: pointer to uint64_t which the data is written to
1198 * @size: size of the access in bytes
1199 * @attrs: memory transaction attributes to use for the access
1201 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1207 * memory_region_dispatch_write: perform a write directly to the specified
1210 * @mr: #MemoryRegion to access
1211 * @addr: address within that region
1212 * @data: data to write
1213 * @size: size of the access in bytes
1214 * @attrs: memory transaction attributes to use for the access
1216 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1223 * address_space_init: initializes an address space
1225 * @as: an uninitialized #AddressSpace
1226 * @root: a #MemoryRegion that routes addresses for the address space
1227 * @name: an address space name. The name is only used for debugging
1230 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1233 * address_space_init_shareable: return an address space for a memory region,
1234 * creating it if it does not already exist
1236 * @root: a #MemoryRegion that routes addresses for the address space
1237 * @name: an address space name. The name is only used for debugging
1240 * This function will return a pointer to an existing AddressSpace
1241 * which was initialized with the specified MemoryRegion, or it will
1242 * create and initialize one if it does not already exist. The ASes
1243 * are reference-counted, so the memory will be freed automatically
1244 * when the AddressSpace is destroyed via address_space_destroy.
1246 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
,
1250 * address_space_destroy: destroy an address space
1252 * Releases all resources associated with an address space. After an address space
1253 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1256 * @as: address space to be destroyed
1258 void address_space_destroy(AddressSpace
*as
);
1261 * address_space_rw: read from or write to an address space.
1263 * Return a MemTxResult indicating whether the operation succeeded
1264 * or failed (eg unassigned memory, device rejected the transaction,
1267 * @as: #AddressSpace to be accessed
1268 * @addr: address within that address space
1269 * @attrs: memory transaction attributes
1270 * @buf: buffer with the data transferred
1271 * @is_write: indicates the transfer direction
1273 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1274 MemTxAttrs attrs
, uint8_t *buf
,
1275 int len
, bool is_write
);
1278 * address_space_write: write to address space.
1280 * Return a MemTxResult indicating whether the operation succeeded
1281 * or failed (eg unassigned memory, device rejected the transaction,
1284 * @as: #AddressSpace to be accessed
1285 * @addr: address within that address space
1286 * @attrs: memory transaction attributes
1287 * @buf: buffer with the data transferred
1289 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1291 const uint8_t *buf
, int len
);
1293 /* address_space_ld*: load from an address space
1294 * address_space_st*: store to an address space
1296 * These functions perform a load or store of the byte, word,
1297 * longword or quad to the specified address within the AddressSpace.
1298 * The _le suffixed functions treat the data as little endian;
1299 * _be indicates big endian; no suffix indicates "same endianness
1302 * The "guest CPU endianness" accessors are deprecated for use outside
1303 * target-* code; devices should be CPU-agnostic and use either the LE
1304 * or the BE accessors.
1306 * @as #AddressSpace to be accessed
1307 * @addr: address within that address space
1308 * @val: data value, for stores
1309 * @attrs: memory transaction attributes
1310 * @result: location to write the success/failure of the transaction;
1311 * if NULL, this information is discarded
1313 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1314 MemTxAttrs attrs
, MemTxResult
*result
);
1315 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1316 MemTxAttrs attrs
, MemTxResult
*result
);
1317 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1318 MemTxAttrs attrs
, MemTxResult
*result
);
1319 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1320 MemTxAttrs attrs
, MemTxResult
*result
);
1321 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1322 MemTxAttrs attrs
, MemTxResult
*result
);
1323 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1324 MemTxAttrs attrs
, MemTxResult
*result
);
1325 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1326 MemTxAttrs attrs
, MemTxResult
*result
);
1327 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1328 MemTxAttrs attrs
, MemTxResult
*result
);
1329 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1330 MemTxAttrs attrs
, MemTxResult
*result
);
1331 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1332 MemTxAttrs attrs
, MemTxResult
*result
);
1333 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1334 MemTxAttrs attrs
, MemTxResult
*result
);
1335 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1336 MemTxAttrs attrs
, MemTxResult
*result
);
1337 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1338 MemTxAttrs attrs
, MemTxResult
*result
);
1339 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1340 MemTxAttrs attrs
, MemTxResult
*result
);
1342 /* address_space_translate: translate an address range into an address space
1343 * into a MemoryRegion and an address range into that section. Should be
1344 * called from an RCU critical section, to avoid that the last reference
1345 * to the returned region disappears after address_space_translate returns.
1347 * @as: #AddressSpace to be accessed
1348 * @addr: address within that address space
1349 * @xlat: pointer to address within the returned memory region section's
1351 * @len: pointer to length
1352 * @is_write: indicates the transfer direction
1354 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1355 hwaddr
*xlat
, hwaddr
*len
,
1358 /* address_space_access_valid: check for validity of accessing an address
1361 * Check whether memory is assigned to the given address space range, and
1362 * access is permitted by any IOMMU regions that are active for the address
1365 * For now, addr and len should be aligned to a page size. This limitation
1366 * will be lifted in the future.
1368 * @as: #AddressSpace to be accessed
1369 * @addr: address within that address space
1370 * @len: length of the area to be checked
1371 * @is_write: indicates the transfer direction
1373 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1375 /* address_space_map: map a physical memory region into a host virtual address
1377 * May map a subset of the requested range, given by and returned in @plen.
1378 * May return %NULL if resources needed to perform the mapping are exhausted.
1379 * Use only for reads OR writes - not for read-modify-write operations.
1380 * Use cpu_register_map_client() to know when retrying the map operation is
1381 * likely to succeed.
1383 * @as: #AddressSpace to be accessed
1384 * @addr: address within that address space
1385 * @plen: pointer to length of buffer; updated on return
1386 * @is_write: indicates the transfer direction
1388 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1389 hwaddr
*plen
, bool is_write
);
1391 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1393 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1394 * the amount of memory that was actually read or written by the caller.
1396 * @as: #AddressSpace used
1397 * @addr: address within that address space
1398 * @len: buffer length as returned by address_space_map()
1399 * @access_len: amount of data actually transferred
1400 * @is_write: indicates the transfer direction
1402 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1403 int is_write
, hwaddr access_len
);
1406 /* Internal functions, part of the implementation of address_space_read. */
1407 MemTxResult
address_space_read_continue(AddressSpace
*as
, hwaddr addr
,
1408 MemTxAttrs attrs
, uint8_t *buf
,
1409 int len
, hwaddr addr1
, hwaddr l
,
1411 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
1412 MemTxAttrs attrs
, uint8_t *buf
, int len
);
1413 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
1415 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
1418 return memory_region_is_ram(mr
) && !mr
->readonly
;
1420 return memory_region_is_ram(mr
) || memory_region_is_romd(mr
);
1425 * address_space_read: read from an address space.
1427 * Return a MemTxResult indicating whether the operation succeeded
1428 * or failed (eg unassigned memory, device rejected the transaction,
1431 * @as: #AddressSpace to be accessed
1432 * @addr: address within that address space
1433 * @attrs: memory transaction attributes
1434 * @buf: buffer with the data transferred
1436 static inline __attribute__((__always_inline__
))
1437 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1438 uint8_t *buf
, int len
)
1440 MemTxResult result
= MEMTX_OK
;
1445 if (__builtin_constant_p(len
)) {
1449 mr
= address_space_translate(as
, addr
, &addr1
, &l
, false);
1450 if (len
== l
&& memory_access_is_direct(mr
, false)) {
1451 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
1452 memcpy(buf
, ptr
, len
);
1454 result
= address_space_read_continue(as
, addr
, attrs
, buf
, len
,
1460 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);