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 "qemu/queue.h"
32 #include "qemu/int128.h"
33 #include "qemu/notify.h"
34 #include "qapi/error.h"
35 #include "qom/object.h"
37 #define MAX_PHYS_ADDR_SPACE_BITS 62
38 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
40 #define TYPE_MEMORY_REGION "qemu:memory-region"
41 #define MEMORY_REGION(obj) \
42 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
44 typedef struct MemoryRegionOps MemoryRegionOps
;
45 typedef struct MemoryRegionMmio MemoryRegionMmio
;
47 struct MemoryRegionMmio
{
48 CPUReadMemoryFunc
*read
[3];
49 CPUWriteMemoryFunc
*write
[3];
52 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
54 /* See address_space_translate: bit 0 is read, bit 1 is write. */
62 struct IOMMUTLBEntry
{
63 AddressSpace
*target_as
;
65 hwaddr translated_addr
;
66 hwaddr addr_mask
; /* 0xfff = 4k translation */
67 IOMMUAccessFlags perm
;
71 * Memory region callbacks
73 struct MemoryRegionOps
{
74 /* Read from the memory region. @addr is relative to @mr; @size is
76 uint64_t (*read
)(void *opaque
,
79 /* Write to the memory region. @addr is relative to @mr; @size is
81 void (*write
)(void *opaque
,
86 enum device_endian endianness
;
87 /* Guest-visible constraints: */
89 /* If nonzero, specify bounds on access sizes beyond which a machine
92 unsigned min_access_size
;
93 unsigned max_access_size
;
94 /* If true, unaligned accesses are supported. Otherwise unaligned
95 * accesses throw machine checks.
99 * If present, and returns #false, the transaction is not accepted
100 * by the device (and results in machine dependent behaviour such
101 * as a machine check exception).
103 bool (*accepts
)(void *opaque
, hwaddr addr
,
104 unsigned size
, bool is_write
);
106 /* Internal implementation constraints: */
108 /* If nonzero, specifies the minimum size implemented. Smaller sizes
109 * will be rounded upwards and a partial result will be returned.
111 unsigned min_access_size
;
112 /* If nonzero, specifies the maximum size implemented. Larger sizes
113 * will be done as a series of accesses with smaller sizes.
115 unsigned max_access_size
;
116 /* If true, unaligned accesses are supported. Otherwise all accesses
117 * are converted to (possibly multiple) naturally aligned accesses.
122 /* If .read and .write are not present, old_mmio may be used for
123 * backwards compatibility with old mmio registration
125 const MemoryRegionMmio old_mmio
;
128 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps
;
130 struct MemoryRegionIOMMUOps
{
131 /* Return a TLB entry that contains a given address. */
132 IOMMUTLBEntry (*translate
)(MemoryRegion
*iommu
, hwaddr addr
, bool is_write
);
135 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
136 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
138 struct MemoryRegion
{
140 /* All fields are private - violators will be prosecuted */
141 const MemoryRegionOps
*ops
;
142 const MemoryRegionIOMMUOps
*iommu_ops
;
144 MemoryRegion
*container
;
147 void (*destructor
)(MemoryRegion
*mr
);
153 bool readonly
; /* For RAM regions */
156 bool warning_printed
; /* For reservations */
157 bool flush_coalesced_mmio
;
162 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
163 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
164 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
166 uint8_t dirty_log_mask
;
167 unsigned ioeventfd_nb
;
168 MemoryRegionIoeventfd
*ioeventfds
;
169 NotifierList iommu_notify
;
173 * MemoryListener: callbacks structure for updates to the physical memory map
175 * Allows a component to adjust to changes in the guest-visible memory map.
176 * Use with memory_listener_register() and memory_listener_unregister().
178 struct MemoryListener
{
179 void (*begin
)(MemoryListener
*listener
);
180 void (*commit
)(MemoryListener
*listener
);
181 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
182 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
183 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
184 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
185 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
186 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
187 void (*log_global_start
)(MemoryListener
*listener
);
188 void (*log_global_stop
)(MemoryListener
*listener
);
189 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
190 bool match_data
, uint64_t data
, EventNotifier
*e
);
191 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
192 bool match_data
, uint64_t data
, EventNotifier
*e
);
193 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
194 hwaddr addr
, hwaddr len
);
195 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
196 hwaddr addr
, hwaddr len
);
197 /* Lower = earlier (during add), later (during del) */
199 AddressSpace
*address_space_filter
;
200 QTAILQ_ENTRY(MemoryListener
) link
;
204 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
206 struct AddressSpace
{
207 /* All fields are private. */
210 struct FlatView
*current_map
;
212 struct MemoryRegionIoeventfd
*ioeventfds
;
213 struct AddressSpaceDispatch
*dispatch
;
214 struct AddressSpaceDispatch
*next_dispatch
;
215 MemoryListener dispatch_listener
;
217 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
221 * MemoryRegionSection: describes a fragment of a #MemoryRegion
223 * @mr: the region, or %NULL if empty
224 * @address_space: the address space the region is mapped in
225 * @offset_within_region: the beginning of the section, relative to @mr's start
226 * @size: the size of the section; will not exceed @mr's boundaries
227 * @offset_within_address_space: the address of the first byte of the section
228 * relative to the region's address space
229 * @readonly: writes to this section are ignored
231 struct MemoryRegionSection
{
233 AddressSpace
*address_space
;
234 hwaddr offset_within_region
;
236 hwaddr offset_within_address_space
;
241 * memory_region_init: Initialize a memory region
243 * The region typically acts as a container for other memory regions. Use
244 * memory_region_add_subregion() to add subregions.
246 * @mr: the #MemoryRegion to be initialized
247 * @owner: the object that tracks the region's reference count
248 * @name: used for debugging; not visible to the user or ABI
249 * @size: size of the region; any subregions beyond this size will be clipped
251 void memory_region_init(MemoryRegion
*mr
,
252 struct Object
*owner
,
257 * memory_region_ref: Add 1 to a memory region's reference count
259 * Whenever memory regions are accessed outside the BQL, they need to be
260 * preserved against hot-unplug. MemoryRegions actually do not have their
261 * own reference count; they piggyback on a QOM object, their "owner".
262 * This function adds a reference to the owner.
264 * All MemoryRegions must have an owner if they can disappear, even if the
265 * device they belong to operates exclusively under the BQL. This is because
266 * the region could be returned at any time by memory_region_find, and this
267 * is usually under guest control.
269 * @mr: the #MemoryRegion
271 void memory_region_ref(MemoryRegion
*mr
);
274 * memory_region_unref: Remove 1 to a memory region's reference count
276 * Whenever memory regions are accessed outside the BQL, they need to be
277 * preserved against hot-unplug. MemoryRegions actually do not have their
278 * own reference count; they piggyback on a QOM object, their "owner".
279 * This function removes a reference to the owner and possibly destroys it.
281 * @mr: the #MemoryRegion
283 void memory_region_unref(MemoryRegion
*mr
);
286 * memory_region_init_io: Initialize an I/O memory region.
288 * Accesses into the region will cause the callbacks in @ops to be called.
289 * if @size is nonzero, subregions will be clipped to @size.
291 * @mr: the #MemoryRegion to be initialized.
292 * @owner: the object that tracks the region's reference count
293 * @ops: a structure containing read and write callbacks to be used when
294 * I/O is performed on the region.
295 * @opaque: passed to to the read and write callbacks of the @ops structure.
296 * @name: used for debugging; not visible to the user or ABI
297 * @size: size of the region.
299 void memory_region_init_io(MemoryRegion
*mr
,
300 struct Object
*owner
,
301 const MemoryRegionOps
*ops
,
307 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
308 * region will modify memory directly.
310 * @mr: the #MemoryRegion to be initialized.
311 * @owner: the object that tracks the region's reference count
312 * @name: the name of the region.
313 * @size: size of the region.
314 * @errp: pointer to Error*, to store an error if it happens.
316 void memory_region_init_ram(MemoryRegion
*mr
,
317 struct Object
*owner
,
324 * memory_region_init_ram_from_file: Initialize RAM memory region with a
327 * @mr: the #MemoryRegion to be initialized.
328 * @owner: the object that tracks the region's reference count
329 * @name: the name of the region.
330 * @size: size of the region.
331 * @share: %true if memory must be mmaped with the MAP_SHARED flag
332 * @path: the path in which to allocate the RAM.
333 * @errp: pointer to Error*, to store an error if it happens.
335 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
336 struct Object
*owner
,
345 * memory_region_init_ram_ptr: Initialize RAM memory region from a
346 * user-provided pointer. Accesses into the
347 * region will modify memory directly.
349 * @mr: the #MemoryRegion to be initialized.
350 * @owner: the object that tracks the region's reference count
351 * @name: the name of the region.
352 * @size: size of the region.
353 * @ptr: memory to be mapped; must contain at least @size bytes.
355 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
356 struct Object
*owner
,
362 * memory_region_init_alias: Initialize a memory region that aliases all or a
363 * part of another memory region.
365 * @mr: the #MemoryRegion to be initialized.
366 * @owner: the object that tracks the region's reference count
367 * @name: used for debugging; not visible to the user or ABI
368 * @orig: the region to be referenced; @mr will be equivalent to
369 * @orig between @offset and @offset + @size - 1.
370 * @offset: start of the section in @orig to be referenced.
371 * @size: size of the region.
373 void memory_region_init_alias(MemoryRegion
*mr
,
374 struct Object
*owner
,
381 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
382 * handled via callbacks.
384 * @mr: the #MemoryRegion to be initialized.
385 * @owner: the object that tracks the region's reference count
386 * @ops: callbacks for write access handling.
387 * @name: the name of the region.
388 * @size: size of the region.
390 void memory_region_init_rom_device(MemoryRegion
*mr
,
391 struct Object
*owner
,
392 const MemoryRegionOps
*ops
,
398 * memory_region_init_reservation: Initialize a memory region that reserves
401 * A reservation region primariy serves debugging purposes. It claims I/O
402 * space that is not supposed to be handled by QEMU itself. Any access via
403 * the memory API will cause an abort().
405 * @mr: the #MemoryRegion to be initialized
406 * @owner: the object that tracks the region's reference count
407 * @name: used for debugging; not visible to the user or ABI
408 * @size: size of the region.
410 void memory_region_init_reservation(MemoryRegion
*mr
,
411 struct Object
*owner
,
416 * memory_region_init_iommu: Initialize a memory region that translates
419 * An IOMMU region translates addresses and forwards accesses to a target
422 * @mr: the #MemoryRegion to be initialized
423 * @owner: the object that tracks the region's reference count
424 * @ops: a function that translates addresses into the @target region
425 * @name: used for debugging; not visible to the user or ABI
426 * @size: size of the region.
428 void memory_region_init_iommu(MemoryRegion
*mr
,
429 struct Object
*owner
,
430 const MemoryRegionIOMMUOps
*ops
,
435 * memory_region_owner: get a memory region's owner.
437 * @mr: the memory region being queried.
439 struct Object
*memory_region_owner(MemoryRegion
*mr
);
442 * memory_region_size: get a memory region's size.
444 * @mr: the memory region being queried.
446 uint64_t memory_region_size(MemoryRegion
*mr
);
449 * memory_region_is_ram: check whether a memory region is random access
451 * Returns %true is a memory region is random access.
453 * @mr: the memory region being queried
455 bool memory_region_is_ram(MemoryRegion
*mr
);
458 * memory_region_is_romd: check whether a memory region is in ROMD mode
460 * Returns %true if a memory region is a ROM device and currently set to allow
463 * @mr: the memory region being queried
465 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
467 return mr
->rom_device
&& mr
->romd_mode
;
471 * memory_region_is_iommu: check whether a memory region is an iommu
473 * Returns %true is a memory region is an iommu.
475 * @mr: the memory region being queried
477 bool memory_region_is_iommu(MemoryRegion
*mr
);
480 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
482 * @mr: the memory region that was changed
483 * @entry: the new entry in the IOMMU translation table. The entry
484 * replaces all old entries for the same virtual I/O address range.
485 * Deleted entries have .@perm == 0.
487 void memory_region_notify_iommu(MemoryRegion
*mr
,
488 IOMMUTLBEntry entry
);
491 * memory_region_register_iommu_notifier: register a notifier for changes to
492 * IOMMU translation entries.
494 * @mr: the memory region to observe
495 * @n: the notifier to be added; the notifier receives a pointer to an
496 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
497 * valid on exit from the notifier.
499 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
);
502 * memory_region_unregister_iommu_notifier: unregister a notifier for
503 * changes to IOMMU translation entries.
505 * @n: the notifier to be removed.
507 void memory_region_unregister_iommu_notifier(Notifier
*n
);
510 * memory_region_name: get a memory region's name
512 * Returns the string that was used to initialize the memory region.
514 * @mr: the memory region being queried
516 const char *memory_region_name(const MemoryRegion
*mr
);
519 * memory_region_is_logging: return whether a memory region is logging writes
521 * Returns %true if the memory region is logging writes
523 * @mr: the memory region being queried
525 bool memory_region_is_logging(MemoryRegion
*mr
);
528 * memory_region_is_rom: check whether a memory region is ROM
530 * Returns %true is a memory region is read-only memory.
532 * @mr: the memory region being queried
534 bool memory_region_is_rom(MemoryRegion
*mr
);
537 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
539 * Returns a file descriptor backing a file-based RAM memory region,
540 * or -1 if the region is not a file-based RAM memory region.
542 * @mr: the RAM or alias memory region being queried.
544 int memory_region_get_fd(MemoryRegion
*mr
);
547 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
549 * Returns a host pointer to a RAM memory region (created with
550 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
553 * @mr: the memory region being queried.
555 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
558 * memory_region_set_log: Turn dirty logging on or off for a region.
560 * Turns dirty logging on or off for a specified client (display, migration).
561 * Only meaningful for RAM regions.
563 * @mr: the memory region being updated.
564 * @log: whether dirty logging is to be enabled or disabled.
565 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
568 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
571 * memory_region_get_dirty: Check whether a range of bytes is dirty
572 * for a specified client.
574 * Checks whether a range of bytes has been written to since the last
575 * call to memory_region_reset_dirty() with the same @client. Dirty logging
578 * @mr: the memory region being queried.
579 * @addr: the address (relative to the start of the region) being queried.
580 * @size: the size of the range being queried.
581 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
584 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
585 hwaddr size
, unsigned client
);
588 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
590 * Marks a range of bytes as dirty, after it has been dirtied outside
593 * @mr: the memory region being dirtied.
594 * @addr: the address (relative to the start of the region) being dirtied.
595 * @size: size of the range being dirtied.
597 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
601 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
602 * for a specified client. It clears them.
604 * Checks whether a range of bytes has been written to since the last
605 * call to memory_region_reset_dirty() with the same @client. Dirty logging
608 * @mr: the memory region being queried.
609 * @addr: the address (relative to the start of the region) being queried.
610 * @size: the size of the range being queried.
611 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
614 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
615 hwaddr size
, unsigned client
);
617 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
618 * any external TLBs (e.g. kvm)
620 * Flushes dirty information from accelerators such as kvm and vhost-net
621 * and makes it available to users of the memory API.
623 * @mr: the region being flushed.
625 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
628 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
631 * Marks a range of pages as no longer dirty.
633 * @mr: the region being updated.
634 * @addr: the start of the subrange being cleaned.
635 * @size: the size of the subrange being cleaned.
636 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
639 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
640 hwaddr size
, unsigned client
);
643 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
645 * Allows a memory region to be marked as read-only (turning it into a ROM).
646 * only useful on RAM regions.
648 * @mr: the region being updated.
649 * @readonly: whether rhe region is to be ROM or RAM.
651 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
654 * memory_region_rom_device_set_romd: enable/disable ROMD mode
656 * Allows a ROM device (initialized with memory_region_init_rom_device() to
657 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
658 * device is mapped to guest memory and satisfies read access directly.
659 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
660 * Writes are always handled by the #MemoryRegion.write function.
662 * @mr: the memory region to be updated
663 * @romd_mode: %true to put the region into ROMD mode
665 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
668 * memory_region_set_coalescing: Enable memory coalescing for the region.
670 * Enabled writes to a region to be queued for later processing. MMIO ->write
671 * callbacks may be delayed until a non-coalesced MMIO is issued.
672 * Only useful for IO regions. Roughly similar to write-combining hardware.
674 * @mr: the memory region to be write coalesced
676 void memory_region_set_coalescing(MemoryRegion
*mr
);
679 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
682 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
683 * Multiple calls can be issued coalesced disjoint ranges.
685 * @mr: the memory region to be updated.
686 * @offset: the start of the range within the region to be coalesced.
687 * @size: the size of the subrange to be coalesced.
689 void memory_region_add_coalescing(MemoryRegion
*mr
,
694 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
696 * Disables any coalescing caused by memory_region_set_coalescing() or
697 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
700 * @mr: the memory region to be updated.
702 void memory_region_clear_coalescing(MemoryRegion
*mr
);
705 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
708 * Ensure that pending coalesced MMIO request are flushed before the memory
709 * region is accessed. This property is automatically enabled for all regions
710 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
712 * @mr: the memory region to be updated.
714 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
717 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
720 * Clear the automatic coalesced MMIO flushing enabled via
721 * memory_region_set_flush_coalesced. Note that this service has no effect on
722 * memory regions that have MMIO coalescing enabled for themselves. For them,
723 * automatic flushing will stop once coalescing is disabled.
725 * @mr: the memory region to be updated.
727 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
730 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
731 * is written to a location.
733 * Marks a word in an IO region (initialized with memory_region_init_io())
734 * as a trigger for an eventfd event. The I/O callback will not be called.
735 * The caller must be prepared to handle failure (that is, take the required
736 * action if the callback _is_ called).
738 * @mr: the memory region being updated.
739 * @addr: the address within @mr that is to be monitored
740 * @size: the size of the access to trigger the eventfd
741 * @match_data: whether to match against @data, instead of just @addr
742 * @data: the data to match against the guest write
743 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
745 void memory_region_add_eventfd(MemoryRegion
*mr
,
753 * memory_region_del_eventfd: Cancel an eventfd.
755 * Cancels an eventfd trigger requested by a previous
756 * memory_region_add_eventfd() call.
758 * @mr: the memory region being updated.
759 * @addr: the address within @mr that is to be monitored
760 * @size: the size of the access to trigger the eventfd
761 * @match_data: whether to match against @data, instead of just @addr
762 * @data: the data to match against the guest write
763 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
765 void memory_region_del_eventfd(MemoryRegion
*mr
,
773 * memory_region_add_subregion: Add a subregion to a container.
775 * Adds a subregion at @offset. The subregion may not overlap with other
776 * subregions (except for those explicitly marked as overlapping). A region
777 * may only be added once as a subregion (unless removed with
778 * memory_region_del_subregion()); use memory_region_init_alias() if you
779 * want a region to be a subregion in multiple locations.
781 * @mr: the region to contain the new subregion; must be a container
782 * initialized with memory_region_init().
783 * @offset: the offset relative to @mr where @subregion is added.
784 * @subregion: the subregion to be added.
786 void memory_region_add_subregion(MemoryRegion
*mr
,
788 MemoryRegion
*subregion
);
790 * memory_region_add_subregion_overlap: Add a subregion to a container
793 * Adds a subregion at @offset. The subregion may overlap with other
794 * subregions. Conflicts are resolved by having a higher @priority hide a
795 * lower @priority. Subregions without priority are taken as @priority 0.
796 * A region may only be added once as a subregion (unless removed with
797 * memory_region_del_subregion()); use memory_region_init_alias() if you
798 * want a region to be a subregion in multiple locations.
800 * @mr: the region to contain the new subregion; must be a container
801 * initialized with memory_region_init().
802 * @offset: the offset relative to @mr where @subregion is added.
803 * @subregion: the subregion to be added.
804 * @priority: used for resolving overlaps; highest priority wins.
806 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
808 MemoryRegion
*subregion
,
812 * memory_region_get_ram_addr: Get the ram address associated with a memory
815 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
816 * code is being reworked.
818 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
821 * memory_region_del_subregion: Remove a subregion.
823 * Removes a subregion from its container.
825 * @mr: the container to be updated.
826 * @subregion: the region being removed; must be a current subregion of @mr.
828 void memory_region_del_subregion(MemoryRegion
*mr
,
829 MemoryRegion
*subregion
);
832 * memory_region_set_enabled: dynamically enable or disable a region
834 * Enables or disables a memory region. A disabled memory region
835 * ignores all accesses to itself and its subregions. It does not
836 * obscure sibling subregions with lower priority - it simply behaves as
837 * if it was removed from the hierarchy.
839 * Regions default to being enabled.
841 * @mr: the region to be updated
842 * @enabled: whether to enable or disable the region
844 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
847 * memory_region_set_address: dynamically update the address of a region
849 * Dynamically updates the address of a region, relative to its container.
850 * May be used on regions are currently part of a memory hierarchy.
852 * @mr: the region to be updated
853 * @addr: new address, relative to container region
855 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
858 * memory_region_set_alias_offset: dynamically update a memory alias's offset
860 * Dynamically updates the offset into the target region that an alias points
861 * to, as if the fourth argument to memory_region_init_alias() has changed.
863 * @mr: the #MemoryRegion to be updated; should be an alias.
864 * @offset: the new offset into the target memory region
866 void memory_region_set_alias_offset(MemoryRegion
*mr
,
870 * memory_region_present: checks if an address relative to a @container
871 * translates into #MemoryRegion within @container
873 * Answer whether a #MemoryRegion within @container covers the address
876 * @container: a #MemoryRegion within which @addr is a relative address
877 * @addr: the area within @container to be searched
879 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
882 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
883 * into any address space.
885 * @mr: a #MemoryRegion which should be checked if it's mapped
887 bool memory_region_is_mapped(MemoryRegion
*mr
);
890 * memory_region_find: translate an address/size relative to a
891 * MemoryRegion into a #MemoryRegionSection.
893 * Locates the first #MemoryRegion within @mr that overlaps the range
894 * given by @addr and @size.
896 * Returns a #MemoryRegionSection that describes a contiguous overlap.
897 * It will have the following characteristics:
898 * .@size = 0 iff no overlap was found
899 * .@mr is non-%NULL iff an overlap was found
901 * Remember that in the return value the @offset_within_region is
902 * relative to the returned region (in the .@mr field), not to the
905 * Similarly, the .@offset_within_address_space is relative to the
906 * address space that contains both regions, the passed and the
907 * returned one. However, in the special case where the @mr argument
908 * has no container (and thus is the root of the address space), the
909 * following will hold:
910 * .@offset_within_address_space >= @addr
911 * .@offset_within_address_space + .@size <= @addr + @size
913 * @mr: a MemoryRegion within which @addr is a relative address
914 * @addr: start of the area within @as to be searched
915 * @size: size of the area to be searched
917 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
918 hwaddr addr
, uint64_t size
);
921 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
923 * Synchronizes the dirty page log for an entire address space.
924 * @as: the address space that contains the memory being synchronized
926 void address_space_sync_dirty_bitmap(AddressSpace
*as
);
929 * memory_region_transaction_begin: Start a transaction.
931 * During a transaction, changes will be accumulated and made visible
932 * only when the transaction ends (is committed).
934 void memory_region_transaction_begin(void);
937 * memory_region_transaction_commit: Commit a transaction and make changes
938 * visible to the guest.
940 void memory_region_transaction_commit(void);
943 * memory_listener_register: register callbacks to be called when memory
944 * sections are mapped or unmapped into an address
947 * @listener: an object containing the callbacks to be called
948 * @filter: if non-%NULL, only regions in this address space will be observed
950 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
953 * memory_listener_unregister: undo the effect of memory_listener_register()
955 * @listener: an object containing the callbacks to be removed
957 void memory_listener_unregister(MemoryListener
*listener
);
960 * memory_global_dirty_log_start: begin dirty logging for all regions
962 void memory_global_dirty_log_start(void);
965 * memory_global_dirty_log_stop: end dirty logging for all regions
967 void memory_global_dirty_log_stop(void);
969 void mtree_info(fprintf_function mon_printf
, void *f
);
972 * address_space_init: initializes an address space
974 * @as: an uninitialized #AddressSpace
975 * @root: a #MemoryRegion that routes addesses for the address space
976 * @name: an address space name. The name is only used for debugging
979 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
983 * address_space_destroy: destroy an address space
985 * Releases all resources associated with an address space. After an address space
986 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
989 * @as: address space to be destroyed
991 void address_space_destroy(AddressSpace
*as
);
994 * address_space_rw: read from or write to an address space.
996 * Return true if the operation hit any unassigned memory or encountered an
999 * @as: #AddressSpace to be accessed
1000 * @addr: address within that address space
1001 * @buf: buffer with the data transferred
1002 * @is_write: indicates the transfer direction
1004 bool address_space_rw(AddressSpace
*as
, hwaddr addr
, uint8_t *buf
,
1005 int len
, bool is_write
);
1008 * address_space_write: write to address space.
1010 * Return true if the operation hit any unassigned memory or encountered an
1013 * @as: #AddressSpace to be accessed
1014 * @addr: address within that address space
1015 * @buf: buffer with the data transferred
1017 bool address_space_write(AddressSpace
*as
, hwaddr addr
,
1018 const uint8_t *buf
, int len
);
1021 * address_space_read: read from an address space.
1023 * Return true if the operation hit any unassigned memory or encountered an
1026 * @as: #AddressSpace to be accessed
1027 * @addr: address within that address space
1028 * @buf: buffer with the data transferred
1030 bool address_space_read(AddressSpace
*as
, hwaddr addr
, uint8_t *buf
, int len
);
1032 /* address_space_translate: translate an address range into an address space
1033 * into a MemoryRegion and an address range into that section
1035 * @as: #AddressSpace to be accessed
1036 * @addr: address within that address space
1037 * @xlat: pointer to address within the returned memory region section's
1039 * @len: pointer to length
1040 * @is_write: indicates the transfer direction
1042 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1043 hwaddr
*xlat
, hwaddr
*len
,
1046 /* address_space_access_valid: check for validity of accessing an address
1049 * Check whether memory is assigned to the given address space range, and
1050 * access is permitted by any IOMMU regions that are active for the address
1053 * For now, addr and len should be aligned to a page size. This limitation
1054 * will be lifted in the future.
1056 * @as: #AddressSpace to be accessed
1057 * @addr: address within that address space
1058 * @len: length of the area to be checked
1059 * @is_write: indicates the transfer direction
1061 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1063 /* address_space_map: map a physical memory region into a host virtual address
1065 * May map a subset of the requested range, given by and returned in @plen.
1066 * May return %NULL if resources needed to perform the mapping are exhausted.
1067 * Use only for reads OR writes - not for read-modify-write operations.
1068 * Use cpu_register_map_client() to know when retrying the map operation is
1069 * likely to succeed.
1071 * @as: #AddressSpace to be accessed
1072 * @addr: address within that address space
1073 * @plen: pointer to length of buffer; updated on return
1074 * @is_write: indicates the transfer direction
1076 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1077 hwaddr
*plen
, bool is_write
);
1079 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1081 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1082 * the amount of memory that was actually read or written by the caller.
1084 * @as: #AddressSpace used
1085 * @addr: address within that address space
1086 * @len: buffer length as returned by address_space_map()
1087 * @access_len: amount of data actually transferred
1088 * @is_write: indicates the transfer direction
1090 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1091 int is_write
, hwaddr access_len
);