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 "qemu-common.h"
27 #include "exec/cpu-common.h"
28 #ifndef CONFIG_USER_ONLY
29 #include "exec/hwaddr.h"
31 #include "exec/memattrs.h"
32 #include "qemu/queue.h"
33 #include "qemu/int128.h"
34 #include "qemu/notify.h"
35 #include "qapi/error.h"
36 #include "qom/object.h"
39 #define MAX_PHYS_ADDR_SPACE_BITS 62
40 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
42 #define TYPE_MEMORY_REGION "qemu:memory-region"
43 #define MEMORY_REGION(obj) \
44 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
46 typedef struct MemoryRegionOps MemoryRegionOps
;
47 typedef struct MemoryRegionMmio MemoryRegionMmio
;
49 struct MemoryRegionMmio
{
50 CPUReadMemoryFunc
*read
[3];
51 CPUWriteMemoryFunc
*write
[3];
54 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
56 /* See address_space_translate: bit 0 is read, bit 1 is write. */
64 struct IOMMUTLBEntry
{
65 AddressSpace
*target_as
;
67 hwaddr translated_addr
;
68 hwaddr addr_mask
; /* 0xfff = 4k translation */
69 IOMMUAccessFlags perm
;
72 /* New-style MMIO accessors can indicate that the transaction failed.
73 * A zero (MEMTX_OK) response means success; anything else is a failure
74 * of some kind. The memory subsystem will bitwise-OR together results
75 * if it is synthesizing an operation from multiple smaller accesses.
78 #define MEMTX_ERROR (1U << 0) /* device returned an error */
79 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
80 typedef uint32_t MemTxResult
;
83 * Memory region callbacks
85 struct MemoryRegionOps
{
86 /* Read from the memory region. @addr is relative to @mr; @size is
88 uint64_t (*read
)(void *opaque
,
91 /* Write to the memory region. @addr is relative to @mr; @size is
93 void (*write
)(void *opaque
,
98 MemTxResult (*read_with_attrs
)(void *opaque
,
103 MemTxResult (*write_with_attrs
)(void *opaque
,
109 enum device_endian endianness
;
110 /* Guest-visible constraints: */
112 /* If nonzero, specify bounds on access sizes beyond which a machine
115 unsigned min_access_size
;
116 unsigned max_access_size
;
117 /* If true, unaligned accesses are supported. Otherwise unaligned
118 * accesses throw machine checks.
122 * If present, and returns #false, the transaction is not accepted
123 * by the device (and results in machine dependent behaviour such
124 * as a machine check exception).
126 bool (*accepts
)(void *opaque
, hwaddr addr
,
127 unsigned size
, bool is_write
);
129 /* Internal implementation constraints: */
131 /* If nonzero, specifies the minimum size implemented. Smaller sizes
132 * will be rounded upwards and a partial result will be returned.
134 unsigned min_access_size
;
135 /* If nonzero, specifies the maximum size implemented. Larger sizes
136 * will be done as a series of accesses with smaller sizes.
138 unsigned max_access_size
;
139 /* If true, unaligned accesses are supported. Otherwise all accesses
140 * are converted to (possibly multiple) naturally aligned accesses.
145 /* If .read and .write are not present, old_mmio may be used for
146 * backwards compatibility with old mmio registration
148 const MemoryRegionMmio old_mmio
;
151 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps
;
153 struct MemoryRegionIOMMUOps
{
154 /* Return a TLB entry that contains a given address. */
155 IOMMUTLBEntry (*translate
)(MemoryRegion
*iommu
, hwaddr addr
, bool is_write
);
158 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
159 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
161 struct MemoryRegion
{
163 /* All fields are private - violators will be prosecuted */
164 const MemoryRegionOps
*ops
;
165 const MemoryRegionIOMMUOps
*iommu_ops
;
167 MemoryRegion
*container
;
170 void (*destructor
)(MemoryRegion
*mr
);
178 bool readonly
; /* For RAM regions */
181 bool warning_printed
; /* For reservations */
182 bool flush_coalesced_mmio
;
187 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
188 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
189 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
191 uint8_t dirty_log_mask
;
192 unsigned ioeventfd_nb
;
193 MemoryRegionIoeventfd
*ioeventfds
;
194 NotifierList iommu_notify
;
198 * MemoryListener: callbacks structure for updates to the physical memory map
200 * Allows a component to adjust to changes in the guest-visible memory map.
201 * Use with memory_listener_register() and memory_listener_unregister().
203 struct MemoryListener
{
204 void (*begin
)(MemoryListener
*listener
);
205 void (*commit
)(MemoryListener
*listener
);
206 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
207 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
208 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
209 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
210 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
211 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
212 void (*log_global_start
)(MemoryListener
*listener
);
213 void (*log_global_stop
)(MemoryListener
*listener
);
214 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
215 bool match_data
, uint64_t data
, EventNotifier
*e
);
216 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
217 bool match_data
, uint64_t data
, EventNotifier
*e
);
218 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
219 hwaddr addr
, hwaddr len
);
220 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
221 hwaddr addr
, hwaddr len
);
222 /* Lower = earlier (during add), later (during del) */
224 AddressSpace
*address_space_filter
;
225 QTAILQ_ENTRY(MemoryListener
) link
;
229 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
231 struct AddressSpace
{
232 /* All fields are private. */
237 /* Accessed via RCU. */
238 struct FlatView
*current_map
;
241 struct MemoryRegionIoeventfd
*ioeventfds
;
242 struct AddressSpaceDispatch
*dispatch
;
243 struct AddressSpaceDispatch
*next_dispatch
;
244 MemoryListener dispatch_listener
;
246 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
250 * MemoryRegionSection: describes a fragment of a #MemoryRegion
252 * @mr: the region, or %NULL if empty
253 * @address_space: the address space the region is mapped in
254 * @offset_within_region: the beginning of the section, relative to @mr's start
255 * @size: the size of the section; will not exceed @mr's boundaries
256 * @offset_within_address_space: the address of the first byte of the section
257 * relative to the region's address space
258 * @readonly: writes to this section are ignored
260 struct MemoryRegionSection
{
262 AddressSpace
*address_space
;
263 hwaddr offset_within_region
;
265 hwaddr offset_within_address_space
;
270 * memory_region_init: Initialize a memory region
272 * The region typically acts as a container for other memory regions. Use
273 * memory_region_add_subregion() to add subregions.
275 * @mr: the #MemoryRegion to be initialized
276 * @owner: the object that tracks the region's reference count
277 * @name: used for debugging; not visible to the user or ABI
278 * @size: size of the region; any subregions beyond this size will be clipped
280 void memory_region_init(MemoryRegion
*mr
,
281 struct Object
*owner
,
286 * memory_region_ref: Add 1 to a memory region's reference count
288 * Whenever memory regions are accessed outside the BQL, they need to be
289 * preserved against hot-unplug. MemoryRegions actually do not have their
290 * own reference count; they piggyback on a QOM object, their "owner".
291 * This function adds a reference to the owner.
293 * All MemoryRegions must have an owner if they can disappear, even if the
294 * device they belong to operates exclusively under the BQL. This is because
295 * the region could be returned at any time by memory_region_find, and this
296 * is usually under guest control.
298 * @mr: the #MemoryRegion
300 void memory_region_ref(MemoryRegion
*mr
);
303 * memory_region_unref: Remove 1 to a memory region's reference count
305 * Whenever memory regions are accessed outside the BQL, they need to be
306 * preserved against hot-unplug. MemoryRegions actually do not have their
307 * own reference count; they piggyback on a QOM object, their "owner".
308 * This function removes a reference to the owner and possibly destroys it.
310 * @mr: the #MemoryRegion
312 void memory_region_unref(MemoryRegion
*mr
);
315 * memory_region_init_io: Initialize an I/O memory region.
317 * Accesses into the region will cause the callbacks in @ops to be called.
318 * if @size is nonzero, subregions will be clipped to @size.
320 * @mr: the #MemoryRegion to be initialized.
321 * @owner: the object that tracks the region's reference count
322 * @ops: a structure containing read and write callbacks to be used when
323 * I/O is performed on the region.
324 * @opaque: passed to to the read and write callbacks of the @ops structure.
325 * @name: used for debugging; not visible to the user or ABI
326 * @size: size of the region.
328 void memory_region_init_io(MemoryRegion
*mr
,
329 struct Object
*owner
,
330 const MemoryRegionOps
*ops
,
336 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
337 * region will modify memory directly.
339 * @mr: the #MemoryRegion to be initialized.
340 * @owner: the object that tracks the region's reference count
341 * @name: the name of the region.
342 * @size: size of the region.
343 * @errp: pointer to Error*, to store an error if it happens.
345 void memory_region_init_ram(MemoryRegion
*mr
,
346 struct Object
*owner
,
352 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
353 * RAM. Accesses into the region will
354 * modify memory directly. Only an initial
355 * portion of this RAM is actually used.
356 * The used size can change across reboots.
358 * @mr: the #MemoryRegion to be initialized.
359 * @owner: the object that tracks the region's reference count
360 * @name: the name of the region.
361 * @size: used size of the region.
362 * @max_size: max size of the region.
363 * @resized: callback to notify owner about used size change.
364 * @errp: pointer to Error*, to store an error if it happens.
366 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
367 struct Object
*owner
,
371 void (*resized
)(const char*,
377 * memory_region_init_ram_from_file: Initialize RAM memory region with a
380 * @mr: the #MemoryRegion to be initialized.
381 * @owner: the object that tracks the region's reference count
382 * @name: the name of the region.
383 * @size: size of the region.
384 * @share: %true if memory must be mmaped with the MAP_SHARED flag
385 * @path: the path in which to allocate the RAM.
386 * @errp: pointer to Error*, to store an error if it happens.
388 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
389 struct Object
*owner
,
398 * memory_region_init_ram_ptr: Initialize RAM memory region from a
399 * user-provided pointer. Accesses into the
400 * region will modify memory directly.
402 * @mr: the #MemoryRegion to be initialized.
403 * @owner: the object that tracks the region's reference count
404 * @name: the name of the region.
405 * @size: size of the region.
406 * @ptr: memory to be mapped; must contain at least @size bytes.
408 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
409 struct Object
*owner
,
415 * memory_region_init_alias: Initialize a memory region that aliases all or a
416 * part of another memory region.
418 * @mr: the #MemoryRegion to be initialized.
419 * @owner: the object that tracks the region's reference count
420 * @name: used for debugging; not visible to the user or ABI
421 * @orig: the region to be referenced; @mr will be equivalent to
422 * @orig between @offset and @offset + @size - 1.
423 * @offset: start of the section in @orig to be referenced.
424 * @size: size of the region.
426 void memory_region_init_alias(MemoryRegion
*mr
,
427 struct Object
*owner
,
434 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
435 * handled via callbacks.
437 * @mr: the #MemoryRegion to be initialized.
438 * @owner: the object that tracks the region's reference count
439 * @ops: callbacks for write access handling.
440 * @name: the name of the region.
441 * @size: size of the region.
442 * @errp: pointer to Error*, to store an error if it happens.
444 void memory_region_init_rom_device(MemoryRegion
*mr
,
445 struct Object
*owner
,
446 const MemoryRegionOps
*ops
,
453 * memory_region_init_reservation: Initialize a memory region that reserves
456 * A reservation region primariy serves debugging purposes. It claims I/O
457 * space that is not supposed to be handled by QEMU itself. Any access via
458 * the memory API will cause an abort().
460 * @mr: the #MemoryRegion to be initialized
461 * @owner: the object that tracks the region's reference count
462 * @name: used for debugging; not visible to the user or ABI
463 * @size: size of the region.
465 void memory_region_init_reservation(MemoryRegion
*mr
,
466 struct Object
*owner
,
471 * memory_region_init_iommu: Initialize a memory region that translates
474 * An IOMMU region translates addresses and forwards accesses to a target
477 * @mr: the #MemoryRegion to be initialized
478 * @owner: the object that tracks the region's reference count
479 * @ops: a function that translates addresses into the @target region
480 * @name: used for debugging; not visible to the user or ABI
481 * @size: size of the region.
483 void memory_region_init_iommu(MemoryRegion
*mr
,
484 struct Object
*owner
,
485 const MemoryRegionIOMMUOps
*ops
,
490 * memory_region_owner: get a memory region's owner.
492 * @mr: the memory region being queried.
494 struct Object
*memory_region_owner(MemoryRegion
*mr
);
497 * memory_region_size: get a memory region's size.
499 * @mr: the memory region being queried.
501 uint64_t memory_region_size(MemoryRegion
*mr
);
504 * memory_region_is_ram: check whether a memory region is random access
506 * Returns %true is a memory region is random access.
508 * @mr: the memory region being queried
510 bool memory_region_is_ram(MemoryRegion
*mr
);
513 * memory_region_is_skip_dump: check whether a memory region should not be
516 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
518 * @mr: the memory region being queried
520 bool memory_region_is_skip_dump(MemoryRegion
*mr
);
523 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
526 * @mr: the memory region being queried
528 void memory_region_set_skip_dump(MemoryRegion
*mr
);
531 * memory_region_is_romd: check whether a memory region is in ROMD mode
533 * Returns %true if a memory region is a ROM device and currently set to allow
536 * @mr: the memory region being queried
538 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
540 return mr
->rom_device
&& mr
->romd_mode
;
544 * memory_region_is_iommu: check whether a memory region is an iommu
546 * Returns %true is a memory region is an iommu.
548 * @mr: the memory region being queried
550 bool memory_region_is_iommu(MemoryRegion
*mr
);
553 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
555 * @mr: the memory region that was changed
556 * @entry: the new entry in the IOMMU translation table. The entry
557 * replaces all old entries for the same virtual I/O address range.
558 * Deleted entries have .@perm == 0.
560 void memory_region_notify_iommu(MemoryRegion
*mr
,
561 IOMMUTLBEntry entry
);
564 * memory_region_register_iommu_notifier: register a notifier for changes to
565 * IOMMU translation entries.
567 * @mr: the memory region to observe
568 * @n: the notifier to be added; the notifier receives a pointer to an
569 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
570 * valid on exit from the notifier.
572 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
);
575 * memory_region_unregister_iommu_notifier: unregister a notifier for
576 * changes to IOMMU translation entries.
578 * @n: the notifier to be removed.
580 void memory_region_unregister_iommu_notifier(Notifier
*n
);
583 * memory_region_name: get a memory region's name
585 * Returns the string that was used to initialize the memory region.
587 * @mr: the memory region being queried
589 const char *memory_region_name(const MemoryRegion
*mr
);
592 * memory_region_is_logging: return whether a memory region is logging writes
594 * Returns %true if the memory region is logging writes for the given client
596 * @mr: the memory region being queried
597 * @client: the client being queried
599 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
602 * memory_region_get_dirty_log_mask: return the clients for which a
603 * memory region is logging writes.
605 * Returns a bitmap of clients, which right now will be either 0 or
606 * (1 << DIRTY_MEMORY_VGA).
608 * @mr: the memory region being queried
610 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
613 * memory_region_is_rom: check whether a memory region is ROM
615 * Returns %true is a memory region is read-only memory.
617 * @mr: the memory region being queried
619 bool memory_region_is_rom(MemoryRegion
*mr
);
622 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
624 * Returns a file descriptor backing a file-based RAM memory region,
625 * or -1 if the region is not a file-based RAM memory region.
627 * @mr: the RAM or alias memory region being queried.
629 int memory_region_get_fd(MemoryRegion
*mr
);
632 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
634 * Returns a host pointer to a RAM memory region (created with
635 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
638 * @mr: the memory region being queried.
640 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
642 /* memory_region_ram_resize: Resize a RAM region.
644 * Only legal before guest might have detected the memory size: e.g. on
645 * incoming migration, or right after reset.
647 * @mr: a memory region created with @memory_region_init_resizeable_ram.
648 * @newsize: the new size the region
649 * @errp: pointer to Error*, to store an error if it happens.
651 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
655 * memory_region_set_log: Turn dirty logging on or off for a region.
657 * Turns dirty logging on or off for a specified client (display, migration).
658 * Only meaningful for RAM regions.
660 * @mr: the memory region being updated.
661 * @log: whether dirty logging is to be enabled or disabled.
662 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
664 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
667 * memory_region_get_dirty: Check whether a range of bytes is dirty
668 * for a specified client.
670 * Checks whether a range of bytes has been written to since the last
671 * call to memory_region_reset_dirty() with the same @client. Dirty logging
674 * @mr: the memory region being queried.
675 * @addr: the address (relative to the start of the region) being queried.
676 * @size: the size of the range being queried.
677 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
680 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
681 hwaddr size
, unsigned client
);
684 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
686 * Marks a range of bytes as dirty, after it has been dirtied outside
689 * @mr: the memory region being dirtied.
690 * @addr: the address (relative to the start of the region) being dirtied.
691 * @size: size of the range being dirtied.
693 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
697 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
698 * for a specified client. It clears them.
700 * Checks whether a range of bytes has been written to since the last
701 * call to memory_region_reset_dirty() with the same @client. Dirty logging
704 * @mr: the memory region being queried.
705 * @addr: the address (relative to the start of the region) being queried.
706 * @size: the size of the range being queried.
707 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
710 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
711 hwaddr size
, unsigned client
);
713 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
714 * any external TLBs (e.g. kvm)
716 * Flushes dirty information from accelerators such as kvm and vhost-net
717 * and makes it available to users of the memory API.
719 * @mr: the region being flushed.
721 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
724 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
727 * Marks a range of pages as no longer dirty.
729 * @mr: the region being updated.
730 * @addr: the start of the subrange being cleaned.
731 * @size: the size of the subrange being cleaned.
732 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
735 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
736 hwaddr size
, unsigned client
);
739 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
741 * Allows a memory region to be marked as read-only (turning it into a ROM).
742 * only useful on RAM regions.
744 * @mr: the region being updated.
745 * @readonly: whether rhe region is to be ROM or RAM.
747 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
750 * memory_region_rom_device_set_romd: enable/disable ROMD mode
752 * Allows a ROM device (initialized with memory_region_init_rom_device() to
753 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
754 * device is mapped to guest memory and satisfies read access directly.
755 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
756 * Writes are always handled by the #MemoryRegion.write function.
758 * @mr: the memory region to be updated
759 * @romd_mode: %true to put the region into ROMD mode
761 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
764 * memory_region_set_coalescing: Enable memory coalescing for the region.
766 * Enabled writes to a region to be queued for later processing. MMIO ->write
767 * callbacks may be delayed until a non-coalesced MMIO is issued.
768 * Only useful for IO regions. Roughly similar to write-combining hardware.
770 * @mr: the memory region to be write coalesced
772 void memory_region_set_coalescing(MemoryRegion
*mr
);
775 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
778 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
779 * Multiple calls can be issued coalesced disjoint ranges.
781 * @mr: the memory region to be updated.
782 * @offset: the start of the range within the region to be coalesced.
783 * @size: the size of the subrange to be coalesced.
785 void memory_region_add_coalescing(MemoryRegion
*mr
,
790 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
792 * Disables any coalescing caused by memory_region_set_coalescing() or
793 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
796 * @mr: the memory region to be updated.
798 void memory_region_clear_coalescing(MemoryRegion
*mr
);
801 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
804 * Ensure that pending coalesced MMIO request are flushed before the memory
805 * region is accessed. This property is automatically enabled for all regions
806 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
808 * @mr: the memory region to be updated.
810 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
813 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
816 * Clear the automatic coalesced MMIO flushing enabled via
817 * memory_region_set_flush_coalesced. Note that this service has no effect on
818 * memory regions that have MMIO coalescing enabled for themselves. For them,
819 * automatic flushing will stop once coalescing is disabled.
821 * @mr: the memory region to be updated.
823 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
826 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
827 * is written to a location.
829 * Marks a word in an IO region (initialized with memory_region_init_io())
830 * as a trigger for an eventfd event. The I/O callback will not be called.
831 * The caller must be prepared to handle failure (that is, take the required
832 * action if the callback _is_ called).
834 * @mr: the memory region being updated.
835 * @addr: the address within @mr that is to be monitored
836 * @size: the size of the access to trigger the eventfd
837 * @match_data: whether to match against @data, instead of just @addr
838 * @data: the data to match against the guest write
839 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
841 void memory_region_add_eventfd(MemoryRegion
*mr
,
849 * memory_region_del_eventfd: Cancel an eventfd.
851 * Cancels an eventfd trigger requested by a previous
852 * memory_region_add_eventfd() call.
854 * @mr: the memory region being updated.
855 * @addr: the address within @mr that is to be monitored
856 * @size: the size of the access to trigger the eventfd
857 * @match_data: whether to match against @data, instead of just @addr
858 * @data: the data to match against the guest write
859 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
861 void memory_region_del_eventfd(MemoryRegion
*mr
,
869 * memory_region_add_subregion: Add a subregion to a container.
871 * Adds a subregion at @offset. The subregion may not overlap with other
872 * subregions (except for those explicitly marked as overlapping). A region
873 * may only be added once as a subregion (unless removed with
874 * memory_region_del_subregion()); use memory_region_init_alias() if you
875 * want a region to be a subregion in multiple locations.
877 * @mr: the region to contain the new subregion; must be a container
878 * initialized with memory_region_init().
879 * @offset: the offset relative to @mr where @subregion is added.
880 * @subregion: the subregion to be added.
882 void memory_region_add_subregion(MemoryRegion
*mr
,
884 MemoryRegion
*subregion
);
886 * memory_region_add_subregion_overlap: Add a subregion to a container
889 * Adds a subregion at @offset. The subregion may overlap with other
890 * subregions. Conflicts are resolved by having a higher @priority hide a
891 * lower @priority. Subregions without priority are taken as @priority 0.
892 * A region may only be added once as a subregion (unless removed with
893 * memory_region_del_subregion()); use memory_region_init_alias() if you
894 * want a region to be a subregion in multiple locations.
896 * @mr: the region to contain the new subregion; must be a container
897 * initialized with memory_region_init().
898 * @offset: the offset relative to @mr where @subregion is added.
899 * @subregion: the subregion to be added.
900 * @priority: used for resolving overlaps; highest priority wins.
902 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
904 MemoryRegion
*subregion
,
908 * memory_region_get_ram_addr: Get the ram address associated with a memory
911 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
912 * code is being reworked.
914 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
916 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
918 * memory_region_del_subregion: Remove a subregion.
920 * Removes a subregion from its container.
922 * @mr: the container to be updated.
923 * @subregion: the region being removed; must be a current subregion of @mr.
925 void memory_region_del_subregion(MemoryRegion
*mr
,
926 MemoryRegion
*subregion
);
929 * memory_region_set_enabled: dynamically enable or disable a region
931 * Enables or disables a memory region. A disabled memory region
932 * ignores all accesses to itself and its subregions. It does not
933 * obscure sibling subregions with lower priority - it simply behaves as
934 * if it was removed from the hierarchy.
936 * Regions default to being enabled.
938 * @mr: the region to be updated
939 * @enabled: whether to enable or disable the region
941 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
944 * memory_region_set_address: dynamically update the address of a region
946 * Dynamically updates the address of a region, relative to its container.
947 * May be used on regions are currently part of a memory hierarchy.
949 * @mr: the region to be updated
950 * @addr: new address, relative to container region
952 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
955 * memory_region_set_size: dynamically update the size of a region.
957 * Dynamically updates the size of a region.
959 * @mr: the region to be updated
960 * @size: used size of the region.
962 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
965 * memory_region_set_alias_offset: dynamically update a memory alias's offset
967 * Dynamically updates the offset into the target region that an alias points
968 * to, as if the fourth argument to memory_region_init_alias() has changed.
970 * @mr: the #MemoryRegion to be updated; should be an alias.
971 * @offset: the new offset into the target memory region
973 void memory_region_set_alias_offset(MemoryRegion
*mr
,
977 * memory_region_present: checks if an address relative to a @container
978 * translates into #MemoryRegion within @container
980 * Answer whether a #MemoryRegion within @container covers the address
983 * @container: a #MemoryRegion within which @addr is a relative address
984 * @addr: the area within @container to be searched
986 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
989 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
990 * into any address space.
992 * @mr: a #MemoryRegion which should be checked if it's mapped
994 bool memory_region_is_mapped(MemoryRegion
*mr
);
997 * memory_region_find: translate an address/size relative to a
998 * MemoryRegion into a #MemoryRegionSection.
1000 * Locates the first #MemoryRegion within @mr that overlaps the range
1001 * given by @addr and @size.
1003 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1004 * It will have the following characteristics:
1005 * .@size = 0 iff no overlap was found
1006 * .@mr is non-%NULL iff an overlap was found
1008 * Remember that in the return value the @offset_within_region is
1009 * relative to the returned region (in the .@mr field), not to the
1012 * Similarly, the .@offset_within_address_space is relative to the
1013 * address space that contains both regions, the passed and the
1014 * returned one. However, in the special case where the @mr argument
1015 * has no container (and thus is the root of the address space), the
1016 * following will hold:
1017 * .@offset_within_address_space >= @addr
1018 * .@offset_within_address_space + .@size <= @addr + @size
1020 * @mr: a MemoryRegion within which @addr is a relative address
1021 * @addr: start of the area within @as to be searched
1022 * @size: size of the area to be searched
1024 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1025 hwaddr addr
, uint64_t size
);
1028 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
1030 * Synchronizes the dirty page log for an entire address space.
1031 * @as: the address space that contains the memory being synchronized
1033 void address_space_sync_dirty_bitmap(AddressSpace
*as
);
1036 * memory_region_transaction_begin: Start a transaction.
1038 * During a transaction, changes will be accumulated and made visible
1039 * only when the transaction ends (is committed).
1041 void memory_region_transaction_begin(void);
1044 * memory_region_transaction_commit: Commit a transaction and make changes
1045 * visible to the guest.
1047 void memory_region_transaction_commit(void);
1050 * memory_listener_register: register callbacks to be called when memory
1051 * sections are mapped or unmapped into an address
1054 * @listener: an object containing the callbacks to be called
1055 * @filter: if non-%NULL, only regions in this address space will be observed
1057 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1060 * memory_listener_unregister: undo the effect of memory_listener_register()
1062 * @listener: an object containing the callbacks to be removed
1064 void memory_listener_unregister(MemoryListener
*listener
);
1067 * memory_global_dirty_log_start: begin dirty logging for all regions
1069 void memory_global_dirty_log_start(void);
1072 * memory_global_dirty_log_stop: end dirty logging for all regions
1074 void memory_global_dirty_log_stop(void);
1076 void mtree_info(fprintf_function mon_printf
, void *f
);
1079 * memory_region_dispatch_read: perform a read directly to the specified
1082 * @mr: #MemoryRegion to access
1083 * @addr: address within that region
1084 * @pval: pointer to uint64_t which the data is written to
1085 * @size: size of the access in bytes
1086 * @attrs: memory transaction attributes to use for the access
1088 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1094 * memory_region_dispatch_write: perform a write directly to the specified
1097 * @mr: #MemoryRegion to access
1098 * @addr: address within that region
1099 * @data: data to write
1100 * @size: size of the access in bytes
1101 * @attrs: memory transaction attributes to use for the access
1103 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1110 * address_space_init: initializes an address space
1112 * @as: an uninitialized #AddressSpace
1113 * @root: a #MemoryRegion that routes addesses for the address space
1114 * @name: an address space name. The name is only used for debugging
1117 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1121 * address_space_destroy: destroy an address space
1123 * Releases all resources associated with an address space. After an address space
1124 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1127 * @as: address space to be destroyed
1129 void address_space_destroy(AddressSpace
*as
);
1132 * address_space_rw: read from or write to an address space.
1134 * Return a MemTxResult indicating whether the operation succeeded
1135 * or failed (eg unassigned memory, device rejected the transaction,
1138 * @as: #AddressSpace to be accessed
1139 * @addr: address within that address space
1140 * @attrs: memory transaction attributes
1141 * @buf: buffer with the data transferred
1142 * @is_write: indicates the transfer direction
1144 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1145 MemTxAttrs attrs
, uint8_t *buf
,
1146 int len
, bool is_write
);
1149 * address_space_write: write to address space.
1151 * Return a MemTxResult indicating whether the operation succeeded
1152 * or failed (eg unassigned memory, device rejected the transaction,
1155 * @as: #AddressSpace to be accessed
1156 * @addr: address within that address space
1157 * @attrs: memory transaction attributes
1158 * @buf: buffer with the data transferred
1160 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1162 const uint8_t *buf
, int len
);
1165 * address_space_read: read from an address space.
1167 * Return a MemTxResult indicating whether the operation succeeded
1168 * or failed (eg unassigned memory, device rejected the transaction,
1171 * @as: #AddressSpace to be accessed
1172 * @addr: address within that address space
1173 * @attrs: memory transaction attributes
1174 * @buf: buffer with the data transferred
1176 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1177 uint8_t *buf
, int len
);
1180 * address_space_ld*: load from an address space
1181 * address_space_st*: store to an address space
1183 * These functions perform a load or store of the byte, word,
1184 * longword or quad to the specified address within the AddressSpace.
1185 * The _le suffixed functions treat the data as little endian;
1186 * _be indicates big endian; no suffix indicates "same endianness
1189 * The "guest CPU endianness" accessors are deprecated for use outside
1190 * target-* code; devices should be CPU-agnostic and use either the LE
1191 * or the BE accessors.
1193 * @as #AddressSpace to be accessed
1194 * @addr: address within that address space
1195 * @val: data value, for stores
1196 * @attrs: memory transaction attributes
1197 * @result: location to write the success/failure of the transaction;
1198 * if NULL, this information is discarded
1200 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1201 MemTxAttrs attrs
, MemTxResult
*result
);
1202 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1203 MemTxAttrs attrs
, MemTxResult
*result
);
1204 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1205 MemTxAttrs attrs
, MemTxResult
*result
);
1206 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1207 MemTxAttrs attrs
, MemTxResult
*result
);
1208 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1209 MemTxAttrs attrs
, MemTxResult
*result
);
1210 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1211 MemTxAttrs attrs
, MemTxResult
*result
);
1212 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1213 MemTxAttrs attrs
, MemTxResult
*result
);
1214 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1215 MemTxAttrs attrs
, MemTxResult
*result
);
1216 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1217 MemTxAttrs attrs
, MemTxResult
*result
);
1218 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1219 MemTxAttrs attrs
, MemTxResult
*result
);
1220 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1221 MemTxAttrs attrs
, MemTxResult
*result
);
1222 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1223 MemTxAttrs attrs
, MemTxResult
*result
);
1224 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1225 MemTxAttrs attrs
, MemTxResult
*result
);
1226 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1227 MemTxAttrs attrs
, MemTxResult
*result
);
1230 uint32_t address_space_lduw(AddressSpace
*as
, hwaddr addr
,
1231 MemTxAttrs attrs
, MemTxResult
*result
);
1232 uint32_t address_space_ldl(AddressSpace
*as
, hwaddr addr
,
1233 MemTxAttrs attrs
, MemTxResult
*result
);
1234 uint64_t address_space_ldq(AddressSpace
*as
, hwaddr addr
,
1235 MemTxAttrs attrs
, MemTxResult
*result
);
1236 void address_space_stl_notdirty(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1237 MemTxAttrs attrs
, MemTxResult
*result
);
1238 void address_space_stw(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1239 MemTxAttrs attrs
, MemTxResult
*result
);
1240 void address_space_stl(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1241 MemTxAttrs attrs
, MemTxResult
*result
);
1242 void address_space_stq(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1243 MemTxAttrs attrs
, MemTxResult
*result
);
1246 /* address_space_translate: translate an address range into an address space
1247 * into a MemoryRegion and an address range into that section. Should be
1248 * called from an RCU critical section, to avoid that the last reference
1249 * to the returned region disappears after address_space_translate returns.
1251 * @as: #AddressSpace to be accessed
1252 * @addr: address within that address space
1253 * @xlat: pointer to address within the returned memory region section's
1255 * @len: pointer to length
1256 * @is_write: indicates the transfer direction
1258 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1259 hwaddr
*xlat
, hwaddr
*len
,
1262 /* address_space_access_valid: check for validity of accessing an address
1265 * Check whether memory is assigned to the given address space range, and
1266 * access is permitted by any IOMMU regions that are active for the address
1269 * For now, addr and len should be aligned to a page size. This limitation
1270 * will be lifted in the future.
1272 * @as: #AddressSpace to be accessed
1273 * @addr: address within that address space
1274 * @len: length of the area to be checked
1275 * @is_write: indicates the transfer direction
1277 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1279 /* address_space_map: map a physical memory region into a host virtual address
1281 * May map a subset of the requested range, given by and returned in @plen.
1282 * May return %NULL if resources needed to perform the mapping are exhausted.
1283 * Use only for reads OR writes - not for read-modify-write operations.
1284 * Use cpu_register_map_client() to know when retrying the map operation is
1285 * likely to succeed.
1287 * @as: #AddressSpace to be accessed
1288 * @addr: address within that address space
1289 * @plen: pointer to length of buffer; updated on return
1290 * @is_write: indicates the transfer direction
1292 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1293 hwaddr
*plen
, bool is_write
);
1295 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1297 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1298 * the amount of memory that was actually read or written by the caller.
1300 * @as: #AddressSpace used
1301 * @addr: address within that address space
1302 * @len: buffer length as returned by address_space_map()
1303 * @access_len: amount of data actually transferred
1304 * @is_write: indicates the transfer direction
1306 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1307 int is_write
, hwaddr access_len
);