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
{
162 /* All fields are private - violators will be prosecuted */
163 const MemoryRegionOps
*ops
;
164 const MemoryRegionIOMMUOps
*iommu_ops
;
166 MemoryRegion
*container
;
169 void (*destructor
)(MemoryRegion
*mr
);
177 bool readonly
; /* For RAM regions */
180 bool warning_printed
; /* For reservations */
181 bool flush_coalesced_mmio
;
183 uint8_t vga_logging_count
;
188 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
189 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
190 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
192 uint8_t dirty_log_mask
;
193 unsigned ioeventfd_nb
;
194 MemoryRegionIoeventfd
*ioeventfds
;
195 NotifierList iommu_notify
;
199 * MemoryListener: callbacks structure for updates to the physical memory map
201 * Allows a component to adjust to changes in the guest-visible memory map.
202 * Use with memory_listener_register() and memory_listener_unregister().
204 struct MemoryListener
{
205 void (*begin
)(MemoryListener
*listener
);
206 void (*commit
)(MemoryListener
*listener
);
207 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
208 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
209 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
210 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
212 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
214 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
215 void (*log_global_start
)(MemoryListener
*listener
);
216 void (*log_global_stop
)(MemoryListener
*listener
);
217 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
218 bool match_data
, uint64_t data
, EventNotifier
*e
);
219 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
220 bool match_data
, uint64_t data
, EventNotifier
*e
);
221 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
222 hwaddr addr
, hwaddr len
);
223 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
224 hwaddr addr
, hwaddr len
);
225 /* Lower = earlier (during add), later (during del) */
227 AddressSpace
*address_space_filter
;
228 QTAILQ_ENTRY(MemoryListener
) link
;
232 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
234 struct AddressSpace
{
235 /* All fields are private. */
240 /* Accessed via RCU. */
241 struct FlatView
*current_map
;
244 struct MemoryRegionIoeventfd
*ioeventfds
;
245 struct AddressSpaceDispatch
*dispatch
;
246 struct AddressSpaceDispatch
*next_dispatch
;
247 MemoryListener dispatch_listener
;
249 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
253 * MemoryRegionSection: describes a fragment of a #MemoryRegion
255 * @mr: the region, or %NULL if empty
256 * @address_space: the address space the region is mapped in
257 * @offset_within_region: the beginning of the section, relative to @mr's start
258 * @size: the size of the section; will not exceed @mr's boundaries
259 * @offset_within_address_space: the address of the first byte of the section
260 * relative to the region's address space
261 * @readonly: writes to this section are ignored
263 struct MemoryRegionSection
{
265 AddressSpace
*address_space
;
266 hwaddr offset_within_region
;
268 hwaddr offset_within_address_space
;
273 * memory_region_init: Initialize a memory region
275 * The region typically acts as a container for other memory regions. Use
276 * memory_region_add_subregion() to add subregions.
278 * @mr: the #MemoryRegion to be initialized
279 * @owner: the object that tracks the region's reference count
280 * @name: used for debugging; not visible to the user or ABI
281 * @size: size of the region; any subregions beyond this size will be clipped
283 void memory_region_init(MemoryRegion
*mr
,
284 struct Object
*owner
,
289 * memory_region_ref: Add 1 to a memory region's reference count
291 * Whenever memory regions are accessed outside the BQL, they need to be
292 * preserved against hot-unplug. MemoryRegions actually do not have their
293 * own reference count; they piggyback on a QOM object, their "owner".
294 * This function adds a reference to the owner.
296 * All MemoryRegions must have an owner if they can disappear, even if the
297 * device they belong to operates exclusively under the BQL. This is because
298 * the region could be returned at any time by memory_region_find, and this
299 * is usually under guest control.
301 * @mr: the #MemoryRegion
303 void memory_region_ref(MemoryRegion
*mr
);
306 * memory_region_unref: Remove 1 to a memory region's reference count
308 * Whenever memory regions are accessed outside the BQL, they need to be
309 * preserved against hot-unplug. MemoryRegions actually do not have their
310 * own reference count; they piggyback on a QOM object, their "owner".
311 * This function removes a reference to the owner and possibly destroys it.
313 * @mr: the #MemoryRegion
315 void memory_region_unref(MemoryRegion
*mr
);
318 * memory_region_init_io: Initialize an I/O memory region.
320 * Accesses into the region will cause the callbacks in @ops to be called.
321 * if @size is nonzero, subregions will be clipped to @size.
323 * @mr: the #MemoryRegion to be initialized.
324 * @owner: the object that tracks the region's reference count
325 * @ops: a structure containing read and write callbacks to be used when
326 * I/O is performed on the region.
327 * @opaque: passed to the read and write callbacks of the @ops structure.
328 * @name: used for debugging; not visible to the user or ABI
329 * @size: size of the region.
331 void memory_region_init_io(MemoryRegion
*mr
,
332 struct Object
*owner
,
333 const MemoryRegionOps
*ops
,
339 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
340 * region will modify memory directly.
342 * @mr: the #MemoryRegion to be initialized.
343 * @owner: the object that tracks the region's reference count
344 * @name: the name of the region.
345 * @size: size of the region.
346 * @errp: pointer to Error*, to store an error if it happens.
348 void memory_region_init_ram(MemoryRegion
*mr
,
349 struct Object
*owner
,
355 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
356 * RAM. Accesses into the region will
357 * modify memory directly. Only an initial
358 * portion of this RAM is actually used.
359 * The used size can change across reboots.
361 * @mr: the #MemoryRegion to be initialized.
362 * @owner: the object that tracks the region's reference count
363 * @name: the name of the region.
364 * @size: used size of the region.
365 * @max_size: max size of the region.
366 * @resized: callback to notify owner about used size change.
367 * @errp: pointer to Error*, to store an error if it happens.
369 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
370 struct Object
*owner
,
374 void (*resized
)(const char*,
380 * memory_region_init_ram_from_file: Initialize RAM memory region with a
383 * @mr: the #MemoryRegion to be initialized.
384 * @owner: the object that tracks the region's reference count
385 * @name: the name of the region.
386 * @size: size of the region.
387 * @share: %true if memory must be mmaped with the MAP_SHARED flag
388 * @path: the path in which to allocate the RAM.
389 * @errp: pointer to Error*, to store an error if it happens.
391 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
392 struct Object
*owner
,
401 * memory_region_init_ram_ptr: Initialize RAM memory region from a
402 * user-provided pointer. Accesses into the
403 * region will modify memory directly.
405 * @mr: the #MemoryRegion to be initialized.
406 * @owner: the object that tracks the region's reference count
407 * @name: the name of the region.
408 * @size: size of the region.
409 * @ptr: memory to be mapped; must contain at least @size bytes.
411 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
412 struct Object
*owner
,
418 * memory_region_init_alias: Initialize a memory region that aliases all or a
419 * part of another memory region.
421 * @mr: the #MemoryRegion to be initialized.
422 * @owner: the object that tracks the region's reference count
423 * @name: used for debugging; not visible to the user or ABI
424 * @orig: the region to be referenced; @mr will be equivalent to
425 * @orig between @offset and @offset + @size - 1.
426 * @offset: start of the section in @orig to be referenced.
427 * @size: size of the region.
429 void memory_region_init_alias(MemoryRegion
*mr
,
430 struct Object
*owner
,
437 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
438 * handled via callbacks.
440 * If NULL callbacks pointer is given, then I/O space is not supposed to be
441 * handled by QEMU itself. Any access via the memory API will cause an abort().
443 * @mr: the #MemoryRegion to be initialized.
444 * @owner: the object that tracks the region's reference count
445 * @ops: callbacks for write access handling.
446 * @name: the name of the region.
447 * @size: size of the region.
448 * @errp: pointer to Error*, to store an error if it happens.
450 void memory_region_init_rom_device(MemoryRegion
*mr
,
451 struct Object
*owner
,
452 const MemoryRegionOps
*ops
,
459 * memory_region_init_reservation: Initialize a memory region that reserves
462 * A reservation region primariy serves debugging purposes. It claims I/O
463 * space that is not supposed to be handled by QEMU itself. Any access via
464 * the memory API will cause an abort().
465 * This function is deprecated. Use memory_region_init_io() with NULL
468 * @mr: the #MemoryRegion to be initialized
469 * @owner: the object that tracks the region's reference count
470 * @name: used for debugging; not visible to the user or ABI
471 * @size: size of the region.
473 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
478 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
482 * memory_region_init_iommu: Initialize a memory region that translates
485 * An IOMMU region translates addresses and forwards accesses to a target
488 * @mr: the #MemoryRegion to be initialized
489 * @owner: the object that tracks the region's reference count
490 * @ops: a function that translates addresses into the @target region
491 * @name: used for debugging; not visible to the user or ABI
492 * @size: size of the region.
494 void memory_region_init_iommu(MemoryRegion
*mr
,
495 struct Object
*owner
,
496 const MemoryRegionIOMMUOps
*ops
,
501 * memory_region_owner: get a memory region's owner.
503 * @mr: the memory region being queried.
505 struct Object
*memory_region_owner(MemoryRegion
*mr
);
508 * memory_region_size: get a memory region's size.
510 * @mr: the memory region being queried.
512 uint64_t memory_region_size(MemoryRegion
*mr
);
515 * memory_region_is_ram: check whether a memory region is random access
517 * Returns %true is a memory region is random access.
519 * @mr: the memory region being queried
521 bool memory_region_is_ram(MemoryRegion
*mr
);
524 * memory_region_is_skip_dump: check whether a memory region should not be
527 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
529 * @mr: the memory region being queried
531 bool memory_region_is_skip_dump(MemoryRegion
*mr
);
534 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
537 * @mr: the memory region being queried
539 void memory_region_set_skip_dump(MemoryRegion
*mr
);
542 * memory_region_is_romd: check whether a memory region is in ROMD mode
544 * Returns %true if a memory region is a ROM device and currently set to allow
547 * @mr: the memory region being queried
549 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
551 return mr
->rom_device
&& mr
->romd_mode
;
555 * memory_region_is_iommu: check whether a memory region is an iommu
557 * Returns %true is a memory region is an iommu.
559 * @mr: the memory region being queried
561 bool memory_region_is_iommu(MemoryRegion
*mr
);
564 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
566 * @mr: the memory region that was changed
567 * @entry: the new entry in the IOMMU translation table. The entry
568 * replaces all old entries for the same virtual I/O address range.
569 * Deleted entries have .@perm == 0.
571 void memory_region_notify_iommu(MemoryRegion
*mr
,
572 IOMMUTLBEntry entry
);
575 * memory_region_register_iommu_notifier: register a notifier for changes to
576 * IOMMU translation entries.
578 * @mr: the memory region to observe
579 * @n: the notifier to be added; the notifier receives a pointer to an
580 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
581 * valid on exit from the notifier.
583 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
);
586 * memory_region_iommu_replay: replay existing IOMMU translations to
589 * @mr: the memory region to observe
590 * @n: the notifier to which to replay iommu mappings
591 * @granularity: Minimum page granularity to replay notifications for
592 * @is_write: Whether to treat the replay as a translate "write"
595 void memory_region_iommu_replay(MemoryRegion
*mr
, Notifier
*n
,
596 hwaddr granularity
, bool is_write
);
599 * memory_region_unregister_iommu_notifier: unregister a notifier for
600 * changes to IOMMU translation entries.
602 * @n: the notifier to be removed.
604 void memory_region_unregister_iommu_notifier(Notifier
*n
);
607 * memory_region_name: get a memory region's name
609 * Returns the string that was used to initialize the memory region.
611 * @mr: the memory region being queried
613 const char *memory_region_name(const MemoryRegion
*mr
);
616 * memory_region_is_logging: return whether a memory region is logging writes
618 * Returns %true if the memory region is logging writes for the given client
620 * @mr: the memory region being queried
621 * @client: the client being queried
623 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
626 * memory_region_get_dirty_log_mask: return the clients for which a
627 * memory region is logging writes.
629 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
630 * are the bit indices.
632 * @mr: the memory region being queried
634 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
637 * memory_region_is_rom: check whether a memory region is ROM
639 * Returns %true is a memory region is read-only memory.
641 * @mr: the memory region being queried
643 bool memory_region_is_rom(MemoryRegion
*mr
);
646 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
648 * Returns a file descriptor backing a file-based RAM memory region,
649 * or -1 if the region is not a file-based RAM memory region.
651 * @mr: the RAM or alias memory region being queried.
653 int memory_region_get_fd(MemoryRegion
*mr
);
656 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
658 * Returns a host pointer to a RAM memory region (created with
659 * memory_region_init_ram() or memory_region_init_ram_ptr()).
661 * Use with care; by the time this function returns, the returned pointer is
662 * not protected by RCU anymore. If the caller is not within an RCU critical
663 * section and does not hold the iothread lock, it must have other means of
664 * protecting the pointer, such as a reference to the region that includes
665 * the incoming ram_addr_t.
667 * @mr: the memory region being queried.
669 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
671 /* memory_region_ram_resize: Resize a RAM region.
673 * Only legal before guest might have detected the memory size: e.g. on
674 * incoming migration, or right after reset.
676 * @mr: a memory region created with @memory_region_init_resizeable_ram.
677 * @newsize: the new size the region
678 * @errp: pointer to Error*, to store an error if it happens.
680 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
684 * memory_region_set_log: Turn dirty logging on or off for a region.
686 * Turns dirty logging on or off for a specified client (display, migration).
687 * Only meaningful for RAM regions.
689 * @mr: the memory region being updated.
690 * @log: whether dirty logging is to be enabled or disabled.
691 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
693 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
696 * memory_region_get_dirty: Check whether a range of bytes is dirty
697 * for a specified client.
699 * Checks whether a range of bytes has been written to since the last
700 * call to memory_region_reset_dirty() with the same @client. Dirty logging
703 * @mr: the memory region being queried.
704 * @addr: the address (relative to the start of the region) being queried.
705 * @size: the size of the range being queried.
706 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
709 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
710 hwaddr size
, unsigned client
);
713 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
715 * Marks a range of bytes as dirty, after it has been dirtied outside
718 * @mr: the memory region being dirtied.
719 * @addr: the address (relative to the start of the region) being dirtied.
720 * @size: size of the range being dirtied.
722 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
726 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
727 * for a specified client. It clears them.
729 * Checks whether a range of bytes has been written to since the last
730 * call to memory_region_reset_dirty() with the same @client. Dirty logging
733 * @mr: the memory region being queried.
734 * @addr: the address (relative to the start of the region) being queried.
735 * @size: the size of the range being queried.
736 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
739 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
740 hwaddr size
, unsigned client
);
742 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
743 * any external TLBs (e.g. kvm)
745 * Flushes dirty information from accelerators such as kvm and vhost-net
746 * and makes it available to users of the memory API.
748 * @mr: the region being flushed.
750 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
753 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
756 * Marks a range of pages as no longer dirty.
758 * @mr: the region being updated.
759 * @addr: the start of the subrange being cleaned.
760 * @size: the size of the subrange being cleaned.
761 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
764 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
765 hwaddr size
, unsigned client
);
768 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
770 * Allows a memory region to be marked as read-only (turning it into a ROM).
771 * only useful on RAM regions.
773 * @mr: the region being updated.
774 * @readonly: whether rhe region is to be ROM or RAM.
776 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
779 * memory_region_rom_device_set_romd: enable/disable ROMD mode
781 * Allows a ROM device (initialized with memory_region_init_rom_device() to
782 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
783 * device is mapped to guest memory and satisfies read access directly.
784 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
785 * Writes are always handled by the #MemoryRegion.write function.
787 * @mr: the memory region to be updated
788 * @romd_mode: %true to put the region into ROMD mode
790 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
793 * memory_region_set_coalescing: Enable memory coalescing for the region.
795 * Enabled writes to a region to be queued for later processing. MMIO ->write
796 * callbacks may be delayed until a non-coalesced MMIO is issued.
797 * Only useful for IO regions. Roughly similar to write-combining hardware.
799 * @mr: the memory region to be write coalesced
801 void memory_region_set_coalescing(MemoryRegion
*mr
);
804 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
807 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
808 * Multiple calls can be issued coalesced disjoint ranges.
810 * @mr: the memory region to be updated.
811 * @offset: the start of the range within the region to be coalesced.
812 * @size: the size of the subrange to be coalesced.
814 void memory_region_add_coalescing(MemoryRegion
*mr
,
819 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
821 * Disables any coalescing caused by memory_region_set_coalescing() or
822 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
825 * @mr: the memory region to be updated.
827 void memory_region_clear_coalescing(MemoryRegion
*mr
);
830 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
833 * Ensure that pending coalesced MMIO request are flushed before the memory
834 * region is accessed. This property is automatically enabled for all regions
835 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
837 * @mr: the memory region to be updated.
839 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
842 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
845 * Clear the automatic coalesced MMIO flushing enabled via
846 * memory_region_set_flush_coalesced. Note that this service has no effect on
847 * memory regions that have MMIO coalescing enabled for themselves. For them,
848 * automatic flushing will stop once coalescing is disabled.
850 * @mr: the memory region to be updated.
852 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
855 * memory_region_set_global_locking: Declares the access processing requires
856 * QEMU's global lock.
858 * When this is invoked, accesses to the memory region will be processed while
859 * holding the global lock of QEMU. This is the default behavior of memory
862 * @mr: the memory region to be updated.
864 void memory_region_set_global_locking(MemoryRegion
*mr
);
867 * memory_region_clear_global_locking: Declares that access processing does
868 * not depend on the QEMU global lock.
870 * By clearing this property, accesses to the memory region will be processed
871 * outside of QEMU's global lock (unless the lock is held on when issuing the
872 * access request). In this case, the device model implementing the access
873 * handlers is responsible for synchronization of concurrency.
875 * @mr: the memory region to be updated.
877 void memory_region_clear_global_locking(MemoryRegion
*mr
);
880 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
881 * is written to a location.
883 * Marks a word in an IO region (initialized with memory_region_init_io())
884 * as a trigger for an eventfd event. The I/O callback will not be called.
885 * The caller must be prepared to handle failure (that is, take the required
886 * action if the callback _is_ called).
888 * @mr: the memory region being updated.
889 * @addr: the address within @mr that is to be monitored
890 * @size: the size of the access to trigger the eventfd
891 * @match_data: whether to match against @data, instead of just @addr
892 * @data: the data to match against the guest write
893 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
895 void memory_region_add_eventfd(MemoryRegion
*mr
,
903 * memory_region_del_eventfd: Cancel an eventfd.
905 * Cancels an eventfd trigger requested by a previous
906 * memory_region_add_eventfd() call.
908 * @mr: the memory region being updated.
909 * @addr: the address within @mr that is to be monitored
910 * @size: the size of the access to trigger the eventfd
911 * @match_data: whether to match against @data, instead of just @addr
912 * @data: the data to match against the guest write
913 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
915 void memory_region_del_eventfd(MemoryRegion
*mr
,
923 * memory_region_add_subregion: Add a subregion to a container.
925 * Adds a subregion at @offset. The subregion may not overlap with other
926 * subregions (except for those explicitly marked as overlapping). A region
927 * may only be added once as a subregion (unless removed with
928 * memory_region_del_subregion()); use memory_region_init_alias() if you
929 * want a region to be a subregion in multiple locations.
931 * @mr: the region to contain the new subregion; must be a container
932 * initialized with memory_region_init().
933 * @offset: the offset relative to @mr where @subregion is added.
934 * @subregion: the subregion to be added.
936 void memory_region_add_subregion(MemoryRegion
*mr
,
938 MemoryRegion
*subregion
);
940 * memory_region_add_subregion_overlap: Add a subregion to a container
943 * Adds a subregion at @offset. The subregion may overlap with other
944 * subregions. Conflicts are resolved by having a higher @priority hide a
945 * lower @priority. Subregions without priority are taken as @priority 0.
946 * A region may only be added once as a subregion (unless removed with
947 * memory_region_del_subregion()); use memory_region_init_alias() if you
948 * want a region to be a subregion in multiple locations.
950 * @mr: the region to contain the new subregion; must be a container
951 * initialized with memory_region_init().
952 * @offset: the offset relative to @mr where @subregion is added.
953 * @subregion: the subregion to be added.
954 * @priority: used for resolving overlaps; highest priority wins.
956 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
958 MemoryRegion
*subregion
,
962 * memory_region_get_ram_addr: Get the ram address associated with a memory
965 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
966 * code is being reworked.
968 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
970 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
972 * memory_region_del_subregion: Remove a subregion.
974 * Removes a subregion from its container.
976 * @mr: the container to be updated.
977 * @subregion: the region being removed; must be a current subregion of @mr.
979 void memory_region_del_subregion(MemoryRegion
*mr
,
980 MemoryRegion
*subregion
);
983 * memory_region_set_enabled: dynamically enable or disable a region
985 * Enables or disables a memory region. A disabled memory region
986 * ignores all accesses to itself and its subregions. It does not
987 * obscure sibling subregions with lower priority - it simply behaves as
988 * if it was removed from the hierarchy.
990 * Regions default to being enabled.
992 * @mr: the region to be updated
993 * @enabled: whether to enable or disable the region
995 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
998 * memory_region_set_address: dynamically update the address of a region
1000 * Dynamically updates the address of a region, relative to its container.
1001 * May be used on regions are currently part of a memory hierarchy.
1003 * @mr: the region to be updated
1004 * @addr: new address, relative to container region
1006 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1009 * memory_region_set_size: dynamically update the size of a region.
1011 * Dynamically updates the size of a region.
1013 * @mr: the region to be updated
1014 * @size: used size of the region.
1016 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1019 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1021 * Dynamically updates the offset into the target region that an alias points
1022 * to, as if the fourth argument to memory_region_init_alias() has changed.
1024 * @mr: the #MemoryRegion to be updated; should be an alias.
1025 * @offset: the new offset into the target memory region
1027 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1031 * memory_region_present: checks if an address relative to a @container
1032 * translates into #MemoryRegion within @container
1034 * Answer whether a #MemoryRegion within @container covers the address
1037 * @container: a #MemoryRegion within which @addr is a relative address
1038 * @addr: the area within @container to be searched
1040 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1043 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1044 * into any address space.
1046 * @mr: a #MemoryRegion which should be checked if it's mapped
1048 bool memory_region_is_mapped(MemoryRegion
*mr
);
1051 * memory_region_find: translate an address/size relative to a
1052 * MemoryRegion into a #MemoryRegionSection.
1054 * Locates the first #MemoryRegion within @mr that overlaps the range
1055 * given by @addr and @size.
1057 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1058 * It will have the following characteristics:
1059 * .@size = 0 iff no overlap was found
1060 * .@mr is non-%NULL iff an overlap was found
1062 * Remember that in the return value the @offset_within_region is
1063 * relative to the returned region (in the .@mr field), not to the
1066 * Similarly, the .@offset_within_address_space is relative to the
1067 * address space that contains both regions, the passed and the
1068 * returned one. However, in the special case where the @mr argument
1069 * has no container (and thus is the root of the address space), the
1070 * following will hold:
1071 * .@offset_within_address_space >= @addr
1072 * .@offset_within_address_space + .@size <= @addr + @size
1074 * @mr: a MemoryRegion within which @addr is a relative address
1075 * @addr: start of the area within @as to be searched
1076 * @size: size of the area to be searched
1078 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1079 hwaddr addr
, uint64_t size
);
1082 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
1084 * Synchronizes the dirty page log for an entire address space.
1085 * @as: the address space that contains the memory being synchronized
1087 void address_space_sync_dirty_bitmap(AddressSpace
*as
);
1090 * memory_region_transaction_begin: Start a transaction.
1092 * During a transaction, changes will be accumulated and made visible
1093 * only when the transaction ends (is committed).
1095 void memory_region_transaction_begin(void);
1098 * memory_region_transaction_commit: Commit a transaction and make changes
1099 * visible to the guest.
1101 void memory_region_transaction_commit(void);
1104 * memory_listener_register: register callbacks to be called when memory
1105 * sections are mapped or unmapped into an address
1108 * @listener: an object containing the callbacks to be called
1109 * @filter: if non-%NULL, only regions in this address space will be observed
1111 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1114 * memory_listener_unregister: undo the effect of memory_listener_register()
1116 * @listener: an object containing the callbacks to be removed
1118 void memory_listener_unregister(MemoryListener
*listener
);
1121 * memory_global_dirty_log_start: begin dirty logging for all regions
1123 void memory_global_dirty_log_start(void);
1126 * memory_global_dirty_log_stop: end dirty logging for all regions
1128 void memory_global_dirty_log_stop(void);
1130 void mtree_info(fprintf_function mon_printf
, void *f
);
1133 * memory_region_dispatch_read: perform a read directly to the specified
1136 * @mr: #MemoryRegion to access
1137 * @addr: address within that region
1138 * @pval: pointer to uint64_t which the data is written to
1139 * @size: size of the access in bytes
1140 * @attrs: memory transaction attributes to use for the access
1142 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1148 * memory_region_dispatch_write: perform a write directly to the specified
1151 * @mr: #MemoryRegion to access
1152 * @addr: address within that region
1153 * @data: data to write
1154 * @size: size of the access in bytes
1155 * @attrs: memory transaction attributes to use for the access
1157 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1164 * address_space_init: initializes an address space
1166 * @as: an uninitialized #AddressSpace
1167 * @root: a #MemoryRegion that routes addresses for the address space
1168 * @name: an address space name. The name is only used for debugging
1171 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1175 * address_space_destroy: destroy an address space
1177 * Releases all resources associated with an address space. After an address space
1178 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1181 * @as: address space to be destroyed
1183 void address_space_destroy(AddressSpace
*as
);
1186 * address_space_rw: read from or write to an address space.
1188 * Return a MemTxResult indicating whether the operation succeeded
1189 * or failed (eg unassigned memory, device rejected the transaction,
1192 * @as: #AddressSpace to be accessed
1193 * @addr: address within that address space
1194 * @attrs: memory transaction attributes
1195 * @buf: buffer with the data transferred
1196 * @is_write: indicates the transfer direction
1198 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1199 MemTxAttrs attrs
, uint8_t *buf
,
1200 int len
, bool is_write
);
1203 * address_space_write: write to address space.
1205 * Return a MemTxResult indicating whether the operation succeeded
1206 * or failed (eg unassigned memory, device rejected the transaction,
1209 * @as: #AddressSpace to be accessed
1210 * @addr: address within that address space
1211 * @attrs: memory transaction attributes
1212 * @buf: buffer with the data transferred
1214 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1216 const uint8_t *buf
, int len
);
1219 * address_space_read: read from an address space.
1221 * Return a MemTxResult indicating whether the operation succeeded
1222 * or failed (eg unassigned memory, device rejected the transaction,
1225 * @as: #AddressSpace to be accessed
1226 * @addr: address within that address space
1227 * @attrs: memory transaction attributes
1228 * @buf: buffer with the data transferred
1230 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1231 uint8_t *buf
, int len
);
1234 * address_space_ld*: load from an address space
1235 * address_space_st*: store to an address space
1237 * These functions perform a load or store of the byte, word,
1238 * longword or quad to the specified address within the AddressSpace.
1239 * The _le suffixed functions treat the data as little endian;
1240 * _be indicates big endian; no suffix indicates "same endianness
1243 * The "guest CPU endianness" accessors are deprecated for use outside
1244 * target-* code; devices should be CPU-agnostic and use either the LE
1245 * or the BE accessors.
1247 * @as #AddressSpace to be accessed
1248 * @addr: address within that address space
1249 * @val: data value, for stores
1250 * @attrs: memory transaction attributes
1251 * @result: location to write the success/failure of the transaction;
1252 * if NULL, this information is discarded
1254 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1255 MemTxAttrs attrs
, MemTxResult
*result
);
1256 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1257 MemTxAttrs attrs
, MemTxResult
*result
);
1258 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1259 MemTxAttrs attrs
, MemTxResult
*result
);
1260 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1261 MemTxAttrs attrs
, MemTxResult
*result
);
1262 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1263 MemTxAttrs attrs
, MemTxResult
*result
);
1264 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1265 MemTxAttrs attrs
, MemTxResult
*result
);
1266 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1267 MemTxAttrs attrs
, MemTxResult
*result
);
1268 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1269 MemTxAttrs attrs
, MemTxResult
*result
);
1270 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1271 MemTxAttrs attrs
, MemTxResult
*result
);
1272 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1273 MemTxAttrs attrs
, MemTxResult
*result
);
1274 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1275 MemTxAttrs attrs
, MemTxResult
*result
);
1276 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1277 MemTxAttrs attrs
, MemTxResult
*result
);
1278 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1279 MemTxAttrs attrs
, MemTxResult
*result
);
1280 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1281 MemTxAttrs attrs
, MemTxResult
*result
);
1284 uint32_t address_space_lduw(AddressSpace
*as
, hwaddr addr
,
1285 MemTxAttrs attrs
, MemTxResult
*result
);
1286 uint32_t address_space_ldl(AddressSpace
*as
, hwaddr addr
,
1287 MemTxAttrs attrs
, MemTxResult
*result
);
1288 uint64_t address_space_ldq(AddressSpace
*as
, hwaddr addr
,
1289 MemTxAttrs attrs
, MemTxResult
*result
);
1290 void address_space_stl_notdirty(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1291 MemTxAttrs attrs
, MemTxResult
*result
);
1292 void address_space_stw(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1293 MemTxAttrs attrs
, MemTxResult
*result
);
1294 void address_space_stl(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1295 MemTxAttrs attrs
, MemTxResult
*result
);
1296 void address_space_stq(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1297 MemTxAttrs attrs
, MemTxResult
*result
);
1300 /* address_space_translate: translate an address range into an address space
1301 * into a MemoryRegion and an address range into that section. Should be
1302 * called from an RCU critical section, to avoid that the last reference
1303 * to the returned region disappears after address_space_translate returns.
1305 * @as: #AddressSpace to be accessed
1306 * @addr: address within that address space
1307 * @xlat: pointer to address within the returned memory region section's
1309 * @len: pointer to length
1310 * @is_write: indicates the transfer direction
1312 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1313 hwaddr
*xlat
, hwaddr
*len
,
1316 /* address_space_access_valid: check for validity of accessing an address
1319 * Check whether memory is assigned to the given address space range, and
1320 * access is permitted by any IOMMU regions that are active for the address
1323 * For now, addr and len should be aligned to a page size. This limitation
1324 * will be lifted in the future.
1326 * @as: #AddressSpace to be accessed
1327 * @addr: address within that address space
1328 * @len: length of the area to be checked
1329 * @is_write: indicates the transfer direction
1331 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1333 /* address_space_map: map a physical memory region into a host virtual address
1335 * May map a subset of the requested range, given by and returned in @plen.
1336 * May return %NULL if resources needed to perform the mapping are exhausted.
1337 * Use only for reads OR writes - not for read-modify-write operations.
1338 * Use cpu_register_map_client() to know when retrying the map operation is
1339 * likely to succeed.
1341 * @as: #AddressSpace to be accessed
1342 * @addr: address within that address space
1343 * @plen: pointer to length of buffer; updated on return
1344 * @is_write: indicates the transfer direction
1346 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1347 hwaddr
*plen
, bool is_write
);
1349 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1351 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1352 * the amount of memory that was actually read or written by the caller.
1354 * @as: #AddressSpace used
1355 * @addr: address within that address space
1356 * @len: buffer length as returned by address_space_map()
1357 * @access_len: amount of data actually transferred
1358 * @is_write: indicates the transfer direction
1360 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1361 int is_write
, hwaddr access_len
);