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 */
26 #include "exec/cpu-common.h"
27 #ifndef CONFIG_USER_ONLY
28 #include "exec/hwaddr.h"
30 #include "exec/memattrs.h"
31 #include "qemu/queue.h"
32 #include "qemu/int128.h"
33 #include "qemu/notify.h"
34 #include "qapi/error.h"
35 #include "qom/object.h"
38 #define MAX_PHYS_ADDR_SPACE_BITS 62
39 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
41 #define TYPE_MEMORY_REGION "qemu:memory-region"
42 #define MEMORY_REGION(obj) \
43 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
45 typedef struct MemoryRegionOps MemoryRegionOps
;
46 typedef struct MemoryRegionMmio MemoryRegionMmio
;
48 struct MemoryRegionMmio
{
49 CPUReadMemoryFunc
*read
[3];
50 CPUWriteMemoryFunc
*write
[3];
53 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
55 /* See address_space_translate: bit 0 is read, bit 1 is write. */
63 struct IOMMUTLBEntry
{
64 AddressSpace
*target_as
;
66 hwaddr translated_addr
;
67 hwaddr addr_mask
; /* 0xfff = 4k translation */
68 IOMMUAccessFlags perm
;
71 /* New-style MMIO accessors can indicate that the transaction failed.
72 * A zero (MEMTX_OK) response means success; anything else is a failure
73 * of some kind. The memory subsystem will bitwise-OR together results
74 * if it is synthesizing an operation from multiple smaller accesses.
77 #define MEMTX_ERROR (1U << 0) /* device returned an error */
78 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
79 typedef uint32_t MemTxResult
;
82 * Memory region callbacks
84 struct MemoryRegionOps
{
85 /* Read from the memory region. @addr is relative to @mr; @size is
87 uint64_t (*read
)(void *opaque
,
90 /* Write to the memory region. @addr is relative to @mr; @size is
92 void (*write
)(void *opaque
,
97 MemTxResult (*read_with_attrs
)(void *opaque
,
102 MemTxResult (*write_with_attrs
)(void *opaque
,
108 enum device_endian endianness
;
109 /* Guest-visible constraints: */
111 /* If nonzero, specify bounds on access sizes beyond which a machine
114 unsigned min_access_size
;
115 unsigned max_access_size
;
116 /* If true, unaligned accesses are supported. Otherwise unaligned
117 * accesses throw machine checks.
121 * If present, and returns #false, the transaction is not accepted
122 * by the device (and results in machine dependent behaviour such
123 * as a machine check exception).
125 bool (*accepts
)(void *opaque
, hwaddr addr
,
126 unsigned size
, bool is_write
);
128 /* Internal implementation constraints: */
130 /* If nonzero, specifies the minimum size implemented. Smaller sizes
131 * will be rounded upwards and a partial result will be returned.
133 unsigned min_access_size
;
134 /* If nonzero, specifies the maximum size implemented. Larger sizes
135 * will be done as a series of accesses with smaller sizes.
137 unsigned max_access_size
;
138 /* If true, unaligned accesses are supported. Otherwise all accesses
139 * are converted to (possibly multiple) naturally aligned accesses.
144 /* If .read and .write are not present, old_mmio may be used for
145 * backwards compatibility with old mmio registration
147 const MemoryRegionMmio old_mmio
;
150 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps
;
152 struct MemoryRegionIOMMUOps
{
153 /* Return a TLB entry that contains a given address. */
154 IOMMUTLBEntry (*translate
)(MemoryRegion
*iommu
, hwaddr addr
, bool is_write
);
157 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
158 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
160 struct MemoryRegion
{
163 /* All fields are private - violators will be prosecuted */
165 /* The following fields should fit in a cache line */
169 bool readonly
; /* For RAM regions */
171 bool flush_coalesced_mmio
;
173 uint8_t dirty_log_mask
;
175 const MemoryRegionIOMMUOps
*iommu_ops
;
177 const MemoryRegionOps
*ops
;
179 MemoryRegion
*container
;
182 void (*destructor
)(MemoryRegion
*mr
);
187 bool warning_printed
; /* For reservations */
188 uint8_t vga_logging_count
;
193 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
194 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
195 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
197 unsigned ioeventfd_nb
;
198 MemoryRegionIoeventfd
*ioeventfds
;
199 NotifierList iommu_notify
;
203 * MemoryListener: callbacks structure for updates to the physical memory map
205 * Allows a component to adjust to changes in the guest-visible memory map.
206 * Use with memory_listener_register() and memory_listener_unregister().
208 struct MemoryListener
{
209 void (*begin
)(MemoryListener
*listener
);
210 void (*commit
)(MemoryListener
*listener
);
211 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
212 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
213 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
214 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
216 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
218 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
219 void (*log_global_start
)(MemoryListener
*listener
);
220 void (*log_global_stop
)(MemoryListener
*listener
);
221 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
222 bool match_data
, uint64_t data
, EventNotifier
*e
);
223 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
224 bool match_data
, uint64_t data
, EventNotifier
*e
);
225 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
226 hwaddr addr
, hwaddr len
);
227 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
228 hwaddr addr
, hwaddr len
);
229 /* Lower = earlier (during add), later (during del) */
231 AddressSpace
*address_space_filter
;
232 QTAILQ_ENTRY(MemoryListener
) link
;
236 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
238 struct AddressSpace
{
239 /* All fields are private. */
244 /* Accessed via RCU. */
245 struct FlatView
*current_map
;
248 struct MemoryRegionIoeventfd
*ioeventfds
;
249 struct AddressSpaceDispatch
*dispatch
;
250 struct AddressSpaceDispatch
*next_dispatch
;
251 MemoryListener dispatch_listener
;
253 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
257 * MemoryRegionSection: describes a fragment of a #MemoryRegion
259 * @mr: the region, or %NULL if empty
260 * @address_space: the address space the region is mapped in
261 * @offset_within_region: the beginning of the section, relative to @mr's start
262 * @size: the size of the section; will not exceed @mr's boundaries
263 * @offset_within_address_space: the address of the first byte of the section
264 * relative to the region's address space
265 * @readonly: writes to this section are ignored
267 struct MemoryRegionSection
{
269 AddressSpace
*address_space
;
270 hwaddr offset_within_region
;
272 hwaddr offset_within_address_space
;
277 * memory_region_init: Initialize a memory region
279 * The region typically acts as a container for other memory regions. Use
280 * memory_region_add_subregion() to add subregions.
282 * @mr: the #MemoryRegion to be initialized
283 * @owner: the object that tracks the region's reference count
284 * @name: used for debugging; not visible to the user or ABI
285 * @size: size of the region; any subregions beyond this size will be clipped
287 void memory_region_init(MemoryRegion
*mr
,
288 struct Object
*owner
,
293 * memory_region_ref: Add 1 to a memory region's reference count
295 * Whenever memory regions are accessed outside the BQL, they need to be
296 * preserved against hot-unplug. MemoryRegions actually do not have their
297 * own reference count; they piggyback on a QOM object, their "owner".
298 * This function adds a reference to the owner.
300 * All MemoryRegions must have an owner if they can disappear, even if the
301 * device they belong to operates exclusively under the BQL. This is because
302 * the region could be returned at any time by memory_region_find, and this
303 * is usually under guest control.
305 * @mr: the #MemoryRegion
307 void memory_region_ref(MemoryRegion
*mr
);
310 * memory_region_unref: Remove 1 to a memory region's reference count
312 * Whenever memory regions are accessed outside the BQL, they need to be
313 * preserved against hot-unplug. MemoryRegions actually do not have their
314 * own reference count; they piggyback on a QOM object, their "owner".
315 * This function removes a reference to the owner and possibly destroys it.
317 * @mr: the #MemoryRegion
319 void memory_region_unref(MemoryRegion
*mr
);
322 * memory_region_init_io: Initialize an I/O memory region.
324 * Accesses into the region will cause the callbacks in @ops to be called.
325 * if @size is nonzero, subregions will be clipped to @size.
327 * @mr: the #MemoryRegion to be initialized.
328 * @owner: the object that tracks the region's reference count
329 * @ops: a structure containing read and write callbacks to be used when
330 * I/O is performed on the region.
331 * @opaque: passed to the read and write callbacks of the @ops structure.
332 * @name: used for debugging; not visible to the user or ABI
333 * @size: size of the region.
335 void memory_region_init_io(MemoryRegion
*mr
,
336 struct Object
*owner
,
337 const MemoryRegionOps
*ops
,
343 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
344 * region will modify memory directly.
346 * @mr: the #MemoryRegion to be initialized.
347 * @owner: the object that tracks the region's reference count
348 * @name: the name of the region.
349 * @size: size of the region.
350 * @errp: pointer to Error*, to store an error if it happens.
352 void memory_region_init_ram(MemoryRegion
*mr
,
353 struct Object
*owner
,
359 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
360 * RAM. Accesses into the region will
361 * modify memory directly. Only an initial
362 * portion of this RAM is actually used.
363 * The used size can change across reboots.
365 * @mr: the #MemoryRegion to be initialized.
366 * @owner: the object that tracks the region's reference count
367 * @name: the name of the region.
368 * @size: used size of the region.
369 * @max_size: max size of the region.
370 * @resized: callback to notify owner about used size change.
371 * @errp: pointer to Error*, to store an error if it happens.
373 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
374 struct Object
*owner
,
378 void (*resized
)(const char*,
384 * memory_region_init_ram_from_file: Initialize RAM memory region with a
387 * @mr: the #MemoryRegion to be initialized.
388 * @owner: the object that tracks the region's reference count
389 * @name: the name of the region.
390 * @size: size of the region.
391 * @share: %true if memory must be mmaped with the MAP_SHARED flag
392 * @path: the path in which to allocate the RAM.
393 * @errp: pointer to Error*, to store an error if it happens.
395 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
396 struct Object
*owner
,
405 * memory_region_init_ram_ptr: Initialize RAM memory region from a
406 * user-provided pointer. Accesses into the
407 * region will modify memory directly.
409 * @mr: the #MemoryRegion to be initialized.
410 * @owner: the object that tracks the region's reference count
411 * @name: the name of the region.
412 * @size: size of the region.
413 * @ptr: memory to be mapped; must contain at least @size bytes.
415 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
416 struct Object
*owner
,
422 * memory_region_init_alias: Initialize a memory region that aliases all or a
423 * part of another memory region.
425 * @mr: the #MemoryRegion to be initialized.
426 * @owner: the object that tracks the region's reference count
427 * @name: used for debugging; not visible to the user or ABI
428 * @orig: the region to be referenced; @mr will be equivalent to
429 * @orig between @offset and @offset + @size - 1.
430 * @offset: start of the section in @orig to be referenced.
431 * @size: size of the region.
433 void memory_region_init_alias(MemoryRegion
*mr
,
434 struct Object
*owner
,
441 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
442 * handled via callbacks.
444 * If NULL callbacks pointer is given, then I/O space is not supposed to be
445 * handled by QEMU itself. Any access via the memory API will cause an abort().
447 * @mr: the #MemoryRegion to be initialized.
448 * @owner: the object that tracks the region's reference count
449 * @ops: callbacks for write access handling.
450 * @name: the name of the region.
451 * @size: size of the region.
452 * @errp: pointer to Error*, to store an error if it happens.
454 void memory_region_init_rom_device(MemoryRegion
*mr
,
455 struct Object
*owner
,
456 const MemoryRegionOps
*ops
,
463 * memory_region_init_reservation: Initialize a memory region that reserves
466 * A reservation region primariy serves debugging purposes. It claims I/O
467 * space that is not supposed to be handled by QEMU itself. Any access via
468 * the memory API will cause an abort().
469 * This function is deprecated. Use memory_region_init_io() with NULL
472 * @mr: the #MemoryRegion to be initialized
473 * @owner: the object that tracks the region's reference count
474 * @name: used for debugging; not visible to the user or ABI
475 * @size: size of the region.
477 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
482 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
486 * memory_region_init_iommu: Initialize a memory region that translates
489 * An IOMMU region translates addresses and forwards accesses to a target
492 * @mr: the #MemoryRegion to be initialized
493 * @owner: the object that tracks the region's reference count
494 * @ops: a function that translates addresses into the @target region
495 * @name: used for debugging; not visible to the user or ABI
496 * @size: size of the region.
498 void memory_region_init_iommu(MemoryRegion
*mr
,
499 struct Object
*owner
,
500 const MemoryRegionIOMMUOps
*ops
,
505 * memory_region_owner: get a memory region's owner.
507 * @mr: the memory region being queried.
509 struct Object
*memory_region_owner(MemoryRegion
*mr
);
512 * memory_region_size: get a memory region's size.
514 * @mr: the memory region being queried.
516 uint64_t memory_region_size(MemoryRegion
*mr
);
519 * memory_region_is_ram: check whether a memory region is random access
521 * Returns %true is a memory region is random access.
523 * @mr: the memory region being queried
525 bool memory_region_is_ram(MemoryRegion
*mr
);
528 * memory_region_is_skip_dump: check whether a memory region should not be
531 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
533 * @mr: the memory region being queried
535 bool memory_region_is_skip_dump(MemoryRegion
*mr
);
538 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
541 * @mr: the memory region being queried
543 void memory_region_set_skip_dump(MemoryRegion
*mr
);
546 * memory_region_is_romd: check whether a memory region is in ROMD mode
548 * Returns %true if a memory region is a ROM device and currently set to allow
551 * @mr: the memory region being queried
553 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
555 return mr
->rom_device
&& mr
->romd_mode
;
559 * memory_region_is_iommu: check whether a memory region is an iommu
561 * Returns %true is a memory region is an iommu.
563 * @mr: the memory region being queried
565 bool memory_region_is_iommu(MemoryRegion
*mr
);
568 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
570 * @mr: the memory region that was changed
571 * @entry: the new entry in the IOMMU translation table. The entry
572 * replaces all old entries for the same virtual I/O address range.
573 * Deleted entries have .@perm == 0.
575 void memory_region_notify_iommu(MemoryRegion
*mr
,
576 IOMMUTLBEntry entry
);
579 * memory_region_register_iommu_notifier: register a notifier for changes to
580 * IOMMU translation entries.
582 * @mr: the memory region to observe
583 * @n: the notifier to be added; the notifier receives a pointer to an
584 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
585 * valid on exit from the notifier.
587 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
);
590 * memory_region_iommu_replay: replay existing IOMMU translations to
593 * @mr: the memory region to observe
594 * @n: the notifier to which to replay iommu mappings
595 * @granularity: Minimum page granularity to replay notifications for
596 * @is_write: Whether to treat the replay as a translate "write"
599 void memory_region_iommu_replay(MemoryRegion
*mr
, Notifier
*n
,
600 hwaddr granularity
, bool is_write
);
603 * memory_region_unregister_iommu_notifier: unregister a notifier for
604 * changes to IOMMU translation entries.
606 * @n: the notifier to be removed.
608 void memory_region_unregister_iommu_notifier(Notifier
*n
);
611 * memory_region_name: get a memory region's name
613 * Returns the string that was used to initialize the memory region.
615 * @mr: the memory region being queried
617 const char *memory_region_name(const MemoryRegion
*mr
);
620 * memory_region_is_logging: return whether a memory region is logging writes
622 * Returns %true if the memory region is logging writes for the given client
624 * @mr: the memory region being queried
625 * @client: the client being queried
627 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
630 * memory_region_get_dirty_log_mask: return the clients for which a
631 * memory region is logging writes.
633 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
634 * are the bit indices.
636 * @mr: the memory region being queried
638 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
641 * memory_region_is_rom: check whether a memory region is ROM
643 * Returns %true is a memory region is read-only memory.
645 * @mr: the memory region being queried
647 bool memory_region_is_rom(MemoryRegion
*mr
);
650 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
652 * Returns a file descriptor backing a file-based RAM memory region,
653 * or -1 if the region is not a file-based RAM memory region.
655 * @mr: the RAM or alias memory region being queried.
657 int memory_region_get_fd(MemoryRegion
*mr
);
660 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
662 * Returns a host pointer to a RAM memory region (created with
663 * memory_region_init_ram() or memory_region_init_ram_ptr()).
665 * Use with care; by the time this function returns, the returned pointer is
666 * not protected by RCU anymore. If the caller is not within an RCU critical
667 * section and does not hold the iothread lock, it must have other means of
668 * protecting the pointer, such as a reference to the region that includes
669 * the incoming ram_addr_t.
671 * @mr: the memory region being queried.
673 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
675 /* memory_region_ram_resize: Resize a RAM region.
677 * Only legal before guest might have detected the memory size: e.g. on
678 * incoming migration, or right after reset.
680 * @mr: a memory region created with @memory_region_init_resizeable_ram.
681 * @newsize: the new size the region
682 * @errp: pointer to Error*, to store an error if it happens.
684 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
688 * memory_region_set_log: Turn dirty logging on or off for a region.
690 * Turns dirty logging on or off for a specified client (display, migration).
691 * Only meaningful for RAM regions.
693 * @mr: the memory region being updated.
694 * @log: whether dirty logging is to be enabled or disabled.
695 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
697 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
700 * memory_region_get_dirty: Check whether a range of bytes is dirty
701 * for a specified client.
703 * Checks whether a range of bytes has been written to since the last
704 * call to memory_region_reset_dirty() with the same @client. Dirty logging
707 * @mr: the memory region being queried.
708 * @addr: the address (relative to the start of the region) being queried.
709 * @size: the size of the range being queried.
710 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
713 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
714 hwaddr size
, unsigned client
);
717 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
719 * Marks a range of bytes as dirty, after it has been dirtied outside
722 * @mr: the memory region being dirtied.
723 * @addr: the address (relative to the start of the region) being dirtied.
724 * @size: size of the range being dirtied.
726 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
730 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
731 * for a specified client. It clears them.
733 * Checks whether a range of bytes has been written to since the last
734 * call to memory_region_reset_dirty() with the same @client. Dirty logging
737 * @mr: the memory region being queried.
738 * @addr: the address (relative to the start of the region) being queried.
739 * @size: the size of the range being queried.
740 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
743 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
744 hwaddr size
, unsigned client
);
746 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
747 * any external TLBs (e.g. kvm)
749 * Flushes dirty information from accelerators such as kvm and vhost-net
750 * and makes it available to users of the memory API.
752 * @mr: the region being flushed.
754 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
757 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
760 * Marks a range of pages as no longer dirty.
762 * @mr: the region being updated.
763 * @addr: the start of the subrange being cleaned.
764 * @size: the size of the subrange being cleaned.
765 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
768 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
769 hwaddr size
, unsigned client
);
772 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
774 * Allows a memory region to be marked as read-only (turning it into a ROM).
775 * only useful on RAM regions.
777 * @mr: the region being updated.
778 * @readonly: whether rhe region is to be ROM or RAM.
780 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
783 * memory_region_rom_device_set_romd: enable/disable ROMD mode
785 * Allows a ROM device (initialized with memory_region_init_rom_device() to
786 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
787 * device is mapped to guest memory and satisfies read access directly.
788 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
789 * Writes are always handled by the #MemoryRegion.write function.
791 * @mr: the memory region to be updated
792 * @romd_mode: %true to put the region into ROMD mode
794 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
797 * memory_region_set_coalescing: Enable memory coalescing for the region.
799 * Enabled writes to a region to be queued for later processing. MMIO ->write
800 * callbacks may be delayed until a non-coalesced MMIO is issued.
801 * Only useful for IO regions. Roughly similar to write-combining hardware.
803 * @mr: the memory region to be write coalesced
805 void memory_region_set_coalescing(MemoryRegion
*mr
);
808 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
811 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
812 * Multiple calls can be issued coalesced disjoint ranges.
814 * @mr: the memory region to be updated.
815 * @offset: the start of the range within the region to be coalesced.
816 * @size: the size of the subrange to be coalesced.
818 void memory_region_add_coalescing(MemoryRegion
*mr
,
823 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
825 * Disables any coalescing caused by memory_region_set_coalescing() or
826 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
829 * @mr: the memory region to be updated.
831 void memory_region_clear_coalescing(MemoryRegion
*mr
);
834 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
837 * Ensure that pending coalesced MMIO request are flushed before the memory
838 * region is accessed. This property is automatically enabled for all regions
839 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
841 * @mr: the memory region to be updated.
843 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
846 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
849 * Clear the automatic coalesced MMIO flushing enabled via
850 * memory_region_set_flush_coalesced. Note that this service has no effect on
851 * memory regions that have MMIO coalescing enabled for themselves. For them,
852 * automatic flushing will stop once coalescing is disabled.
854 * @mr: the memory region to be updated.
856 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
859 * memory_region_set_global_locking: Declares the access processing requires
860 * QEMU's global lock.
862 * When this is invoked, accesses to the memory region will be processed while
863 * holding the global lock of QEMU. This is the default behavior of memory
866 * @mr: the memory region to be updated.
868 void memory_region_set_global_locking(MemoryRegion
*mr
);
871 * memory_region_clear_global_locking: Declares that access processing does
872 * not depend on the QEMU global lock.
874 * By clearing this property, accesses to the memory region will be processed
875 * outside of QEMU's global lock (unless the lock is held on when issuing the
876 * access request). In this case, the device model implementing the access
877 * handlers is responsible for synchronization of concurrency.
879 * @mr: the memory region to be updated.
881 void memory_region_clear_global_locking(MemoryRegion
*mr
);
884 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
885 * is written to a location.
887 * Marks a word in an IO region (initialized with memory_region_init_io())
888 * as a trigger for an eventfd event. The I/O callback will not be called.
889 * The caller must be prepared to handle failure (that is, take the required
890 * action if the callback _is_ called).
892 * @mr: the memory region being updated.
893 * @addr: the address within @mr that is to be monitored
894 * @size: the size of the access to trigger the eventfd
895 * @match_data: whether to match against @data, instead of just @addr
896 * @data: the data to match against the guest write
897 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
899 void memory_region_add_eventfd(MemoryRegion
*mr
,
907 * memory_region_del_eventfd: Cancel an eventfd.
909 * Cancels an eventfd trigger requested by a previous
910 * memory_region_add_eventfd() call.
912 * @mr: the memory region being updated.
913 * @addr: the address within @mr that is to be monitored
914 * @size: the size of the access to trigger the eventfd
915 * @match_data: whether to match against @data, instead of just @addr
916 * @data: the data to match against the guest write
917 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
919 void memory_region_del_eventfd(MemoryRegion
*mr
,
927 * memory_region_add_subregion: Add a subregion to a container.
929 * Adds a subregion at @offset. The subregion may not overlap with other
930 * subregions (except for those explicitly marked as overlapping). A region
931 * may only be added once as a subregion (unless removed with
932 * memory_region_del_subregion()); use memory_region_init_alias() if you
933 * want a region to be a subregion in multiple locations.
935 * @mr: the region to contain the new subregion; must be a container
936 * initialized with memory_region_init().
937 * @offset: the offset relative to @mr where @subregion is added.
938 * @subregion: the subregion to be added.
940 void memory_region_add_subregion(MemoryRegion
*mr
,
942 MemoryRegion
*subregion
);
944 * memory_region_add_subregion_overlap: Add a subregion to a container
947 * Adds a subregion at @offset. The subregion may overlap with other
948 * subregions. Conflicts are resolved by having a higher @priority hide a
949 * lower @priority. Subregions without priority are taken as @priority 0.
950 * A region may only be added once as a subregion (unless removed with
951 * memory_region_del_subregion()); use memory_region_init_alias() if you
952 * want a region to be a subregion in multiple locations.
954 * @mr: the region to contain the new subregion; must be a container
955 * initialized with memory_region_init().
956 * @offset: the offset relative to @mr where @subregion is added.
957 * @subregion: the subregion to be added.
958 * @priority: used for resolving overlaps; highest priority wins.
960 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
962 MemoryRegion
*subregion
,
966 * memory_region_get_ram_addr: Get the ram address associated with a memory
969 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
970 * code is being reworked.
972 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
974 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
976 * memory_region_del_subregion: Remove a subregion.
978 * Removes a subregion from its container.
980 * @mr: the container to be updated.
981 * @subregion: the region being removed; must be a current subregion of @mr.
983 void memory_region_del_subregion(MemoryRegion
*mr
,
984 MemoryRegion
*subregion
);
987 * memory_region_set_enabled: dynamically enable or disable a region
989 * Enables or disables a memory region. A disabled memory region
990 * ignores all accesses to itself and its subregions. It does not
991 * obscure sibling subregions with lower priority - it simply behaves as
992 * if it was removed from the hierarchy.
994 * Regions default to being enabled.
996 * @mr: the region to be updated
997 * @enabled: whether to enable or disable the region
999 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1002 * memory_region_set_address: dynamically update the address of a region
1004 * Dynamically updates the address of a region, relative to its container.
1005 * May be used on regions are currently part of a memory hierarchy.
1007 * @mr: the region to be updated
1008 * @addr: new address, relative to container region
1010 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1013 * memory_region_set_size: dynamically update the size of a region.
1015 * Dynamically updates the size of a region.
1017 * @mr: the region to be updated
1018 * @size: used size of the region.
1020 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1023 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1025 * Dynamically updates the offset into the target region that an alias points
1026 * to, as if the fourth argument to memory_region_init_alias() has changed.
1028 * @mr: the #MemoryRegion to be updated; should be an alias.
1029 * @offset: the new offset into the target memory region
1031 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1035 * memory_region_present: checks if an address relative to a @container
1036 * translates into #MemoryRegion within @container
1038 * Answer whether a #MemoryRegion within @container covers the address
1041 * @container: a #MemoryRegion within which @addr is a relative address
1042 * @addr: the area within @container to be searched
1044 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1047 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1048 * into any address space.
1050 * @mr: a #MemoryRegion which should be checked if it's mapped
1052 bool memory_region_is_mapped(MemoryRegion
*mr
);
1055 * memory_region_find: translate an address/size relative to a
1056 * MemoryRegion into a #MemoryRegionSection.
1058 * Locates the first #MemoryRegion within @mr that overlaps the range
1059 * given by @addr and @size.
1061 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1062 * It will have the following characteristics:
1063 * .@size = 0 iff no overlap was found
1064 * .@mr is non-%NULL iff an overlap was found
1066 * Remember that in the return value the @offset_within_region is
1067 * relative to the returned region (in the .@mr field), not to the
1070 * Similarly, the .@offset_within_address_space is relative to the
1071 * address space that contains both regions, the passed and the
1072 * returned one. However, in the special case where the @mr argument
1073 * has no container (and thus is the root of the address space), the
1074 * following will hold:
1075 * .@offset_within_address_space >= @addr
1076 * .@offset_within_address_space + .@size <= @addr + @size
1078 * @mr: a MemoryRegion within which @addr is a relative address
1079 * @addr: start of the area within @as to be searched
1080 * @size: size of the area to be searched
1082 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1083 hwaddr addr
, uint64_t size
);
1086 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
1088 * Synchronizes the dirty page log for an entire address space.
1089 * @as: the address space that contains the memory being synchronized
1091 void address_space_sync_dirty_bitmap(AddressSpace
*as
);
1094 * memory_region_transaction_begin: Start a transaction.
1096 * During a transaction, changes will be accumulated and made visible
1097 * only when the transaction ends (is committed).
1099 void memory_region_transaction_begin(void);
1102 * memory_region_transaction_commit: Commit a transaction and make changes
1103 * visible to the guest.
1105 void memory_region_transaction_commit(void);
1108 * memory_listener_register: register callbacks to be called when memory
1109 * sections are mapped or unmapped into an address
1112 * @listener: an object containing the callbacks to be called
1113 * @filter: if non-%NULL, only regions in this address space will be observed
1115 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1118 * memory_listener_unregister: undo the effect of memory_listener_register()
1120 * @listener: an object containing the callbacks to be removed
1122 void memory_listener_unregister(MemoryListener
*listener
);
1125 * memory_global_dirty_log_start: begin dirty logging for all regions
1127 void memory_global_dirty_log_start(void);
1130 * memory_global_dirty_log_stop: end dirty logging for all regions
1132 void memory_global_dirty_log_stop(void);
1134 void mtree_info(fprintf_function mon_printf
, void *f
);
1137 * memory_region_dispatch_read: perform a read directly to the specified
1140 * @mr: #MemoryRegion to access
1141 * @addr: address within that region
1142 * @pval: pointer to uint64_t which the data is written to
1143 * @size: size of the access in bytes
1144 * @attrs: memory transaction attributes to use for the access
1146 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1152 * memory_region_dispatch_write: perform a write directly to the specified
1155 * @mr: #MemoryRegion to access
1156 * @addr: address within that region
1157 * @data: data to write
1158 * @size: size of the access in bytes
1159 * @attrs: memory transaction attributes to use for the access
1161 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1168 * address_space_init: initializes an address space
1170 * @as: an uninitialized #AddressSpace
1171 * @root: a #MemoryRegion that routes addresses for the address space
1172 * @name: an address space name. The name is only used for debugging
1175 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1179 * address_space_destroy: destroy an address space
1181 * Releases all resources associated with an address space. After an address space
1182 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1185 * @as: address space to be destroyed
1187 void address_space_destroy(AddressSpace
*as
);
1190 * address_space_rw: read from or write to an address space.
1192 * Return a MemTxResult indicating whether the operation succeeded
1193 * or failed (eg unassigned memory, device rejected the transaction,
1196 * @as: #AddressSpace to be accessed
1197 * @addr: address within that address space
1198 * @attrs: memory transaction attributes
1199 * @buf: buffer with the data transferred
1200 * @is_write: indicates the transfer direction
1202 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1203 MemTxAttrs attrs
, uint8_t *buf
,
1204 int len
, bool is_write
);
1207 * address_space_write: write to address space.
1209 * Return a MemTxResult indicating whether the operation succeeded
1210 * or failed (eg unassigned memory, device rejected the transaction,
1213 * @as: #AddressSpace to be accessed
1214 * @addr: address within that address space
1215 * @attrs: memory transaction attributes
1216 * @buf: buffer with the data transferred
1218 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1220 const uint8_t *buf
, int len
);
1223 * address_space_read: read from an address space.
1225 * Return a MemTxResult indicating whether the operation succeeded
1226 * or failed (eg unassigned memory, device rejected the transaction,
1229 * @as: #AddressSpace to be accessed
1230 * @addr: address within that address space
1231 * @attrs: memory transaction attributes
1232 * @buf: buffer with the data transferred
1234 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1235 uint8_t *buf
, int len
);
1238 * address_space_ld*: load from an address space
1239 * address_space_st*: store to an address space
1241 * These functions perform a load or store of the byte, word,
1242 * longword or quad to the specified address within the AddressSpace.
1243 * The _le suffixed functions treat the data as little endian;
1244 * _be indicates big endian; no suffix indicates "same endianness
1247 * The "guest CPU endianness" accessors are deprecated for use outside
1248 * target-* code; devices should be CPU-agnostic and use either the LE
1249 * or the BE accessors.
1251 * @as #AddressSpace to be accessed
1252 * @addr: address within that address space
1253 * @val: data value, for stores
1254 * @attrs: memory transaction attributes
1255 * @result: location to write the success/failure of the transaction;
1256 * if NULL, this information is discarded
1258 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1259 MemTxAttrs attrs
, MemTxResult
*result
);
1260 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1261 MemTxAttrs attrs
, MemTxResult
*result
);
1262 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1263 MemTxAttrs attrs
, MemTxResult
*result
);
1264 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1265 MemTxAttrs attrs
, MemTxResult
*result
);
1266 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1267 MemTxAttrs attrs
, MemTxResult
*result
);
1268 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1269 MemTxAttrs attrs
, MemTxResult
*result
);
1270 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1271 MemTxAttrs attrs
, MemTxResult
*result
);
1272 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1273 MemTxAttrs attrs
, MemTxResult
*result
);
1274 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1275 MemTxAttrs attrs
, MemTxResult
*result
);
1276 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1277 MemTxAttrs attrs
, MemTxResult
*result
);
1278 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1279 MemTxAttrs attrs
, MemTxResult
*result
);
1280 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1281 MemTxAttrs attrs
, MemTxResult
*result
);
1282 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1283 MemTxAttrs attrs
, MemTxResult
*result
);
1284 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1285 MemTxAttrs attrs
, MemTxResult
*result
);
1288 uint32_t address_space_lduw(AddressSpace
*as
, hwaddr addr
,
1289 MemTxAttrs attrs
, MemTxResult
*result
);
1290 uint32_t address_space_ldl(AddressSpace
*as
, hwaddr addr
,
1291 MemTxAttrs attrs
, MemTxResult
*result
);
1292 uint64_t address_space_ldq(AddressSpace
*as
, hwaddr addr
,
1293 MemTxAttrs attrs
, MemTxResult
*result
);
1294 void address_space_stl_notdirty(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1295 MemTxAttrs attrs
, MemTxResult
*result
);
1296 void address_space_stw(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1297 MemTxAttrs attrs
, MemTxResult
*result
);
1298 void address_space_stl(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1299 MemTxAttrs attrs
, MemTxResult
*result
);
1300 void address_space_stq(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1301 MemTxAttrs attrs
, MemTxResult
*result
);
1304 /* address_space_translate: translate an address range into an address space
1305 * into a MemoryRegion and an address range into that section. Should be
1306 * called from an RCU critical section, to avoid that the last reference
1307 * to the returned region disappears after address_space_translate returns.
1309 * @as: #AddressSpace to be accessed
1310 * @addr: address within that address space
1311 * @xlat: pointer to address within the returned memory region section's
1313 * @len: pointer to length
1314 * @is_write: indicates the transfer direction
1316 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1317 hwaddr
*xlat
, hwaddr
*len
,
1320 /* address_space_access_valid: check for validity of accessing an address
1323 * Check whether memory is assigned to the given address space range, and
1324 * access is permitted by any IOMMU regions that are active for the address
1327 * For now, addr and len should be aligned to a page size. This limitation
1328 * will be lifted in the future.
1330 * @as: #AddressSpace to be accessed
1331 * @addr: address within that address space
1332 * @len: length of the area to be checked
1333 * @is_write: indicates the transfer direction
1335 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1337 /* address_space_map: map a physical memory region into a host virtual address
1339 * May map a subset of the requested range, given by and returned in @plen.
1340 * May return %NULL if resources needed to perform the mapping are exhausted.
1341 * Use only for reads OR writes - not for read-modify-write operations.
1342 * Use cpu_register_map_client() to know when retrying the map operation is
1343 * likely to succeed.
1345 * @as: #AddressSpace to be accessed
1346 * @addr: address within that address space
1347 * @plen: pointer to length of buffer; updated on return
1348 * @is_write: indicates the transfer direction
1350 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1351 hwaddr
*plen
, bool is_write
);
1353 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1355 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1356 * the amount of memory that was actually read or written by the caller.
1358 * @as: #AddressSpace used
1359 * @addr: address within that address space
1360 * @len: buffer length as returned by address_space_map()
1361 * @access_len: amount of data actually transferred
1362 * @is_write: indicates the transfer direction
1364 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1365 int is_write
, hwaddr access_len
);