2 * Physical memory management API
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
17 #ifndef CONFIG_USER_ONLY
19 #define DIRTY_MEMORY_VGA 0
20 #define DIRTY_MEMORY_CODE 1
21 #define DIRTY_MEMORY_MIGRATION 2
22 #define DIRTY_MEMORY_NUM 3 /* num of dirty bits */
24 #include "exec/cpu-common.h"
25 #ifndef CONFIG_USER_ONLY
26 #include "exec/hwaddr.h"
28 #include "exec/memattrs.h"
29 #include "qemu/queue.h"
30 #include "qemu/int128.h"
31 #include "qemu/notify.h"
32 #include "qom/object.h"
35 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
37 #define MAX_PHYS_ADDR_SPACE_BITS 62
38 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
40 #define TYPE_MEMORY_REGION "qemu:memory-region"
41 #define MEMORY_REGION(obj) \
42 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
44 typedef struct MemoryRegionOps MemoryRegionOps
;
45 typedef struct MemoryRegionMmio MemoryRegionMmio
;
47 struct MemoryRegionMmio
{
48 CPUReadMemoryFunc
*read
[3];
49 CPUWriteMemoryFunc
*write
[3];
52 typedef struct IOMMUTLBEntry IOMMUTLBEntry
;
54 /* See address_space_translate: bit 0 is read, bit 1 is write. */
62 struct IOMMUTLBEntry
{
63 AddressSpace
*target_as
;
65 hwaddr translated_addr
;
66 hwaddr addr_mask
; /* 0xfff = 4k translation */
67 IOMMUAccessFlags perm
;
70 /* New-style MMIO accessors can indicate that the transaction failed.
71 * A zero (MEMTX_OK) response means success; anything else is a failure
72 * of some kind. The memory subsystem will bitwise-OR together results
73 * if it is synthesizing an operation from multiple smaller accesses.
76 #define MEMTX_ERROR (1U << 0) /* device returned an error */
77 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
78 typedef uint32_t MemTxResult
;
81 * Memory region callbacks
83 struct MemoryRegionOps
{
84 /* Read from the memory region. @addr is relative to @mr; @size is
86 uint64_t (*read
)(void *opaque
,
89 /* Write to the memory region. @addr is relative to @mr; @size is
91 void (*write
)(void *opaque
,
96 MemTxResult (*read_with_attrs
)(void *opaque
,
101 MemTxResult (*write_with_attrs
)(void *opaque
,
107 enum device_endian endianness
;
108 /* Guest-visible constraints: */
110 /* If nonzero, specify bounds on access sizes beyond which a machine
113 unsigned min_access_size
;
114 unsigned max_access_size
;
115 /* If true, unaligned accesses are supported. Otherwise unaligned
116 * accesses throw machine checks.
120 * If present, and returns #false, the transaction is not accepted
121 * by the device (and results in machine dependent behaviour such
122 * as a machine check exception).
124 bool (*accepts
)(void *opaque
, hwaddr addr
,
125 unsigned size
, bool is_write
);
127 /* Internal implementation constraints: */
129 /* If nonzero, specifies the minimum size implemented. Smaller sizes
130 * will be rounded upwards and a partial result will be returned.
132 unsigned min_access_size
;
133 /* If nonzero, specifies the maximum size implemented. Larger sizes
134 * will be done as a series of accesses with smaller sizes.
136 unsigned max_access_size
;
137 /* If true, unaligned accesses are supported. Otherwise all accesses
138 * are converted to (possibly multiple) naturally aligned accesses.
143 /* If .read and .write are not present, old_mmio may be used for
144 * backwards compatibility with old mmio registration
146 const MemoryRegionMmio old_mmio
;
149 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps
;
151 struct MemoryRegionIOMMUOps
{
152 /* Return a TLB entry that contains a given address. */
153 IOMMUTLBEntry (*translate
)(MemoryRegion
*iommu
, hwaddr addr
, bool is_write
);
154 /* Returns minimum supported page size */
155 uint64_t (*get_min_page_size
)(MemoryRegion
*iommu
);
156 /* Called when the first notifier is set */
157 void (*notify_started
)(MemoryRegion
*iommu
);
158 /* Called when the last notifier is removed */
159 void (*notify_stopped
)(MemoryRegion
*iommu
);
162 typedef struct CoalescedMemoryRange CoalescedMemoryRange
;
163 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd
;
165 struct MemoryRegion
{
168 /* All fields are private - violators will be prosecuted */
170 /* The following fields should fit in a cache line */
174 bool readonly
; /* For RAM regions */
176 bool flush_coalesced_mmio
;
178 uint8_t dirty_log_mask
;
181 const MemoryRegionIOMMUOps
*iommu_ops
;
183 const MemoryRegionOps
*ops
;
185 MemoryRegion
*container
;
188 void (*destructor
)(MemoryRegion
*mr
);
193 bool warning_printed
; /* For reservations */
194 uint8_t vga_logging_count
;
198 QTAILQ_HEAD(subregions
, MemoryRegion
) subregions
;
199 QTAILQ_ENTRY(MemoryRegion
) subregions_link
;
200 QTAILQ_HEAD(coalesced_ranges
, CoalescedMemoryRange
) coalesced
;
202 unsigned ioeventfd_nb
;
203 MemoryRegionIoeventfd
*ioeventfds
;
204 NotifierList iommu_notify
;
208 * MemoryListener: callbacks structure for updates to the physical memory map
210 * Allows a component to adjust to changes in the guest-visible memory map.
211 * Use with memory_listener_register() and memory_listener_unregister().
213 struct MemoryListener
{
214 void (*begin
)(MemoryListener
*listener
);
215 void (*commit
)(MemoryListener
*listener
);
216 void (*region_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
217 void (*region_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
218 void (*region_nop
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
219 void (*log_start
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
221 void (*log_stop
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
223 void (*log_sync
)(MemoryListener
*listener
, MemoryRegionSection
*section
);
224 void (*log_global_start
)(MemoryListener
*listener
);
225 void (*log_global_stop
)(MemoryListener
*listener
);
226 void (*eventfd_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
227 bool match_data
, uint64_t data
, EventNotifier
*e
);
228 void (*eventfd_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
229 bool match_data
, uint64_t data
, EventNotifier
*e
);
230 void (*coalesced_mmio_add
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
231 hwaddr addr
, hwaddr len
);
232 void (*coalesced_mmio_del
)(MemoryListener
*listener
, MemoryRegionSection
*section
,
233 hwaddr addr
, hwaddr len
);
234 /* Lower = earlier (during add), later (during del) */
236 AddressSpace
*address_space_filter
;
237 QTAILQ_ENTRY(MemoryListener
) link
;
241 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
243 struct AddressSpace
{
244 /* All fields are private. */
251 /* Accessed via RCU. */
252 struct FlatView
*current_map
;
255 struct MemoryRegionIoeventfd
*ioeventfds
;
256 struct AddressSpaceDispatch
*dispatch
;
257 struct AddressSpaceDispatch
*next_dispatch
;
258 MemoryListener dispatch_listener
;
260 QTAILQ_ENTRY(AddressSpace
) address_spaces_link
;
264 * MemoryRegionSection: describes a fragment of a #MemoryRegion
266 * @mr: the region, or %NULL if empty
267 * @address_space: the address space the region is mapped in
268 * @offset_within_region: the beginning of the section, relative to @mr's start
269 * @size: the size of the section; will not exceed @mr's boundaries
270 * @offset_within_address_space: the address of the first byte of the section
271 * relative to the region's address space
272 * @readonly: writes to this section are ignored
274 struct MemoryRegionSection
{
276 AddressSpace
*address_space
;
277 hwaddr offset_within_region
;
279 hwaddr offset_within_address_space
;
284 * memory_region_init: Initialize a memory region
286 * The region typically acts as a container for other memory regions. Use
287 * memory_region_add_subregion() to add subregions.
289 * @mr: the #MemoryRegion to be initialized
290 * @owner: the object that tracks the region's reference count
291 * @name: used for debugging; not visible to the user or ABI
292 * @size: size of the region; any subregions beyond this size will be clipped
294 void memory_region_init(MemoryRegion
*mr
,
295 struct Object
*owner
,
300 * memory_region_ref: Add 1 to a memory region's reference count
302 * Whenever memory regions are accessed outside the BQL, they need to be
303 * preserved against hot-unplug. MemoryRegions actually do not have their
304 * own reference count; they piggyback on a QOM object, their "owner".
305 * This function adds a reference to the owner.
307 * All MemoryRegions must have an owner if they can disappear, even if the
308 * device they belong to operates exclusively under the BQL. This is because
309 * the region could be returned at any time by memory_region_find, and this
310 * is usually under guest control.
312 * @mr: the #MemoryRegion
314 void memory_region_ref(MemoryRegion
*mr
);
317 * memory_region_unref: Remove 1 to a memory region's reference count
319 * Whenever memory regions are accessed outside the BQL, they need to be
320 * preserved against hot-unplug. MemoryRegions actually do not have their
321 * own reference count; they piggyback on a QOM object, their "owner".
322 * This function removes a reference to the owner and possibly destroys it.
324 * @mr: the #MemoryRegion
326 void memory_region_unref(MemoryRegion
*mr
);
329 * memory_region_init_io: Initialize an I/O memory region.
331 * Accesses into the region will cause the callbacks in @ops to be called.
332 * if @size is nonzero, subregions will be clipped to @size.
334 * @mr: the #MemoryRegion to be initialized.
335 * @owner: the object that tracks the region's reference count
336 * @ops: a structure containing read and write callbacks to be used when
337 * I/O is performed on the region.
338 * @opaque: passed to the read and write callbacks of the @ops structure.
339 * @name: used for debugging; not visible to the user or ABI
340 * @size: size of the region.
342 void memory_region_init_io(MemoryRegion
*mr
,
343 struct Object
*owner
,
344 const MemoryRegionOps
*ops
,
350 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
351 * region will modify memory directly.
353 * @mr: the #MemoryRegion to be initialized.
354 * @owner: the object that tracks the region's reference count
355 * @name: the name of the region.
356 * @size: size of the region.
357 * @errp: pointer to Error*, to store an error if it happens.
359 void memory_region_init_ram(MemoryRegion
*mr
,
360 struct Object
*owner
,
366 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
367 * RAM. Accesses into the region will
368 * modify memory directly. Only an initial
369 * portion of this RAM is actually used.
370 * The used size can change across reboots.
372 * @mr: the #MemoryRegion to be initialized.
373 * @owner: the object that tracks the region's reference count
374 * @name: the name of the region.
375 * @size: used size of the region.
376 * @max_size: max size of the region.
377 * @resized: callback to notify owner about used size change.
378 * @errp: pointer to Error*, to store an error if it happens.
380 void memory_region_init_resizeable_ram(MemoryRegion
*mr
,
381 struct Object
*owner
,
385 void (*resized
)(const char*,
391 * memory_region_init_ram_from_file: Initialize RAM memory region with a
394 * @mr: the #MemoryRegion to be initialized.
395 * @owner: the object that tracks the region's reference count
396 * @name: the name of the region.
397 * @size: size of the region.
398 * @share: %true if memory must be mmaped with the MAP_SHARED flag
399 * @path: the path in which to allocate the RAM.
400 * @errp: pointer to Error*, to store an error if it happens.
402 void memory_region_init_ram_from_file(MemoryRegion
*mr
,
403 struct Object
*owner
,
412 * memory_region_init_ram_ptr: Initialize RAM memory region from a
413 * user-provided pointer. Accesses into the
414 * region will modify memory directly.
416 * @mr: the #MemoryRegion to be initialized.
417 * @owner: the object that tracks the region's reference count
418 * @name: the name of the region.
419 * @size: size of the region.
420 * @ptr: memory to be mapped; must contain at least @size bytes.
422 void memory_region_init_ram_ptr(MemoryRegion
*mr
,
423 struct Object
*owner
,
429 * memory_region_init_alias: Initialize a memory region that aliases all or a
430 * part of another memory region.
432 * @mr: the #MemoryRegion to be initialized.
433 * @owner: the object that tracks the region's reference count
434 * @name: used for debugging; not visible to the user or ABI
435 * @orig: the region to be referenced; @mr will be equivalent to
436 * @orig between @offset and @offset + @size - 1.
437 * @offset: start of the section in @orig to be referenced.
438 * @size: size of the region.
440 void memory_region_init_alias(MemoryRegion
*mr
,
441 struct Object
*owner
,
448 * memory_region_init_rom: Initialize a ROM memory region.
450 * This has the same effect as calling memory_region_init_ram()
451 * and then marking the resulting region read-only with
452 * memory_region_set_readonly().
454 * @mr: the #MemoryRegion to be initialized.
455 * @owner: the object that tracks the region's reference count
456 * @name: the name of the region.
457 * @size: size of the region.
458 * @errp: pointer to Error*, to store an error if it happens.
460 void memory_region_init_rom(MemoryRegion
*mr
,
461 struct Object
*owner
,
467 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
468 * handled via callbacks.
470 * If NULL callbacks pointer is given, then I/O space is not supposed to be
471 * handled by QEMU itself. Any access via the memory API will cause an abort().
473 * @mr: the #MemoryRegion to be initialized.
474 * @owner: the object that tracks the region's reference count
475 * @ops: callbacks for write access handling.
476 * @name: the name of the region.
477 * @size: size of the region.
478 * @errp: pointer to Error*, to store an error if it happens.
480 void memory_region_init_rom_device(MemoryRegion
*mr
,
481 struct Object
*owner
,
482 const MemoryRegionOps
*ops
,
489 * memory_region_init_reservation: Initialize a memory region that reserves
492 * A reservation region primariy serves debugging purposes. It claims I/O
493 * space that is not supposed to be handled by QEMU itself. Any access via
494 * the memory API will cause an abort().
495 * This function is deprecated. Use memory_region_init_io() with NULL
498 * @mr: the #MemoryRegion to be initialized
499 * @owner: the object that tracks the region's reference count
500 * @name: used for debugging; not visible to the user or ABI
501 * @size: size of the region.
503 static inline void memory_region_init_reservation(MemoryRegion
*mr
,
508 memory_region_init_io(mr
, owner
, NULL
, mr
, name
, size
);
512 * memory_region_init_iommu: Initialize a memory region that translates
515 * An IOMMU region translates addresses and forwards accesses to a target
518 * @mr: the #MemoryRegion to be initialized
519 * @owner: the object that tracks the region's reference count
520 * @ops: a function that translates addresses into the @target region
521 * @name: used for debugging; not visible to the user or ABI
522 * @size: size of the region.
524 void memory_region_init_iommu(MemoryRegion
*mr
,
525 struct Object
*owner
,
526 const MemoryRegionIOMMUOps
*ops
,
531 * memory_region_owner: get a memory region's owner.
533 * @mr: the memory region being queried.
535 struct Object
*memory_region_owner(MemoryRegion
*mr
);
538 * memory_region_size: get a memory region's size.
540 * @mr: the memory region being queried.
542 uint64_t memory_region_size(MemoryRegion
*mr
);
545 * memory_region_is_ram: check whether a memory region is random access
547 * Returns %true is a memory region is random access.
549 * @mr: the memory region being queried
551 static inline bool memory_region_is_ram(MemoryRegion
*mr
)
557 * memory_region_is_skip_dump: check whether a memory region should not be
560 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
562 * @mr: the memory region being queried
564 bool memory_region_is_skip_dump(MemoryRegion
*mr
);
567 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
570 * @mr: the memory region being queried
572 void memory_region_set_skip_dump(MemoryRegion
*mr
);
575 * memory_region_is_romd: check whether a memory region is in ROMD mode
577 * Returns %true if a memory region is a ROM device and currently set to allow
580 * @mr: the memory region being queried
582 static inline bool memory_region_is_romd(MemoryRegion
*mr
)
584 return mr
->rom_device
&& mr
->romd_mode
;
588 * memory_region_is_iommu: check whether a memory region is an iommu
590 * Returns %true is a memory region is an iommu.
592 * @mr: the memory region being queried
594 static inline bool memory_region_is_iommu(MemoryRegion
*mr
)
596 return mr
->iommu_ops
;
601 * memory_region_iommu_get_min_page_size: get minimum supported page size
604 * Returns minimum supported page size for an iommu.
606 * @mr: the memory region being queried
608 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion
*mr
);
611 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
613 * @mr: the memory region that was changed
614 * @entry: the new entry in the IOMMU translation table. The entry
615 * replaces all old entries for the same virtual I/O address range.
616 * Deleted entries have .@perm == 0.
618 void memory_region_notify_iommu(MemoryRegion
*mr
,
619 IOMMUTLBEntry entry
);
622 * memory_region_register_iommu_notifier: register a notifier for changes to
623 * IOMMU translation entries.
625 * @mr: the memory region to observe
626 * @n: the notifier to be added; the notifier receives a pointer to an
627 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
628 * valid on exit from the notifier.
630 void memory_region_register_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
);
633 * memory_region_iommu_replay: replay existing IOMMU translations to
634 * a notifier with the minimum page granularity returned by
635 * mr->iommu_ops->get_page_size().
637 * @mr: the memory region to observe
638 * @n: the notifier to which to replay iommu mappings
639 * @is_write: Whether to treat the replay as a translate "write"
642 void memory_region_iommu_replay(MemoryRegion
*mr
, Notifier
*n
, bool is_write
);
645 * memory_region_unregister_iommu_notifier: unregister a notifier for
646 * changes to IOMMU translation entries.
648 * @mr: the memory region which was observed and for which notity_stopped()
650 * @n: the notifier to be removed.
652 void memory_region_unregister_iommu_notifier(MemoryRegion
*mr
, Notifier
*n
);
655 * memory_region_name: get a memory region's name
657 * Returns the string that was used to initialize the memory region.
659 * @mr: the memory region being queried
661 const char *memory_region_name(const MemoryRegion
*mr
);
664 * memory_region_is_logging: return whether a memory region is logging writes
666 * Returns %true if the memory region is logging writes for the given client
668 * @mr: the memory region being queried
669 * @client: the client being queried
671 bool memory_region_is_logging(MemoryRegion
*mr
, uint8_t client
);
674 * memory_region_get_dirty_log_mask: return the clients for which a
675 * memory region is logging writes.
677 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
678 * are the bit indices.
680 * @mr: the memory region being queried
682 uint8_t memory_region_get_dirty_log_mask(MemoryRegion
*mr
);
685 * memory_region_is_rom: check whether a memory region is ROM
687 * Returns %true is a memory region is read-only memory.
689 * @mr: the memory region being queried
691 static inline bool memory_region_is_rom(MemoryRegion
*mr
)
693 return mr
->ram
&& mr
->readonly
;
698 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
700 * Returns a file descriptor backing a file-based RAM memory region,
701 * or -1 if the region is not a file-based RAM memory region.
703 * @mr: the RAM or alias memory region being queried.
705 int memory_region_get_fd(MemoryRegion
*mr
);
708 * memory_region_set_fd: Mark a RAM memory region as backed by a
711 * This function is typically used after memory_region_init_ram_ptr().
713 * @mr: the memory region being queried.
714 * @fd: the file descriptor that backs @mr.
716 void memory_region_set_fd(MemoryRegion
*mr
, int fd
);
719 * memory_region_from_host: Convert a pointer into a RAM memory region
720 * and an offset within it.
722 * Given a host pointer inside a RAM memory region (created with
723 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
724 * the MemoryRegion and the offset within it.
726 * Use with care; by the time this function returns, the returned pointer is
727 * not protected by RCU anymore. If the caller is not within an RCU critical
728 * section and does not hold the iothread lock, it must have other means of
729 * protecting the pointer, such as a reference to the region that includes
730 * the incoming ram_addr_t.
732 * @mr: the memory region being queried.
734 MemoryRegion
*memory_region_from_host(void *ptr
, ram_addr_t
*offset
);
737 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
739 * Returns a host pointer to a RAM memory region (created with
740 * memory_region_init_ram() or memory_region_init_ram_ptr()).
742 * Use with care; by the time this function returns, the returned pointer is
743 * not protected by RCU anymore. If the caller is not within an RCU critical
744 * section and does not hold the iothread lock, it must have other means of
745 * protecting the pointer, such as a reference to the region that includes
746 * the incoming ram_addr_t.
748 * @mr: the memory region being queried.
750 void *memory_region_get_ram_ptr(MemoryRegion
*mr
);
752 /* memory_region_ram_resize: Resize a RAM region.
754 * Only legal before guest might have detected the memory size: e.g. on
755 * incoming migration, or right after reset.
757 * @mr: a memory region created with @memory_region_init_resizeable_ram.
758 * @newsize: the new size the region
759 * @errp: pointer to Error*, to store an error if it happens.
761 void memory_region_ram_resize(MemoryRegion
*mr
, ram_addr_t newsize
,
765 * memory_region_set_log: Turn dirty logging on or off for a region.
767 * Turns dirty logging on or off for a specified client (display, migration).
768 * Only meaningful for RAM regions.
770 * @mr: the memory region being updated.
771 * @log: whether dirty logging is to be enabled or disabled.
772 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
774 void memory_region_set_log(MemoryRegion
*mr
, bool log
, unsigned client
);
777 * memory_region_get_dirty: Check whether a range of bytes is dirty
778 * for a specified client.
780 * Checks whether a range of bytes has been written to since the last
781 * call to memory_region_reset_dirty() with the same @client. Dirty logging
784 * @mr: the memory region being queried.
785 * @addr: the address (relative to the start of the region) being queried.
786 * @size: the size of the range being queried.
787 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
790 bool memory_region_get_dirty(MemoryRegion
*mr
, hwaddr addr
,
791 hwaddr size
, unsigned client
);
794 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
796 * Marks a range of bytes as dirty, after it has been dirtied outside
799 * @mr: the memory region being dirtied.
800 * @addr: the address (relative to the start of the region) being dirtied.
801 * @size: size of the range being dirtied.
803 void memory_region_set_dirty(MemoryRegion
*mr
, hwaddr addr
,
807 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
808 * for a specified client. It clears them.
810 * Checks whether a range of bytes has been written to since the last
811 * call to memory_region_reset_dirty() with the same @client. Dirty logging
814 * @mr: the memory region being queried.
815 * @addr: the address (relative to the start of the region) being queried.
816 * @size: the size of the range being queried.
817 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
820 bool memory_region_test_and_clear_dirty(MemoryRegion
*mr
, hwaddr addr
,
821 hwaddr size
, unsigned client
);
823 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
824 * any external TLBs (e.g. kvm)
826 * Flushes dirty information from accelerators such as kvm and vhost-net
827 * and makes it available to users of the memory API.
829 * @mr: the region being flushed.
831 void memory_region_sync_dirty_bitmap(MemoryRegion
*mr
);
834 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
837 * Marks a range of pages as no longer dirty.
839 * @mr: the region being updated.
840 * @addr: the start of the subrange being cleaned.
841 * @size: the size of the subrange being cleaned.
842 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
845 void memory_region_reset_dirty(MemoryRegion
*mr
, hwaddr addr
,
846 hwaddr size
, unsigned client
);
849 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
851 * Allows a memory region to be marked as read-only (turning it into a ROM).
852 * only useful on RAM regions.
854 * @mr: the region being updated.
855 * @readonly: whether rhe region is to be ROM or RAM.
857 void memory_region_set_readonly(MemoryRegion
*mr
, bool readonly
);
860 * memory_region_rom_device_set_romd: enable/disable ROMD mode
862 * Allows a ROM device (initialized with memory_region_init_rom_device() to
863 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
864 * device is mapped to guest memory and satisfies read access directly.
865 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
866 * Writes are always handled by the #MemoryRegion.write function.
868 * @mr: the memory region to be updated
869 * @romd_mode: %true to put the region into ROMD mode
871 void memory_region_rom_device_set_romd(MemoryRegion
*mr
, bool romd_mode
);
874 * memory_region_set_coalescing: Enable memory coalescing for the region.
876 * Enabled writes to a region to be queued for later processing. MMIO ->write
877 * callbacks may be delayed until a non-coalesced MMIO is issued.
878 * Only useful for IO regions. Roughly similar to write-combining hardware.
880 * @mr: the memory region to be write coalesced
882 void memory_region_set_coalescing(MemoryRegion
*mr
);
885 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
888 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
889 * Multiple calls can be issued coalesced disjoint ranges.
891 * @mr: the memory region to be updated.
892 * @offset: the start of the range within the region to be coalesced.
893 * @size: the size of the subrange to be coalesced.
895 void memory_region_add_coalescing(MemoryRegion
*mr
,
900 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
902 * Disables any coalescing caused by memory_region_set_coalescing() or
903 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
906 * @mr: the memory region to be updated.
908 void memory_region_clear_coalescing(MemoryRegion
*mr
);
911 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
914 * Ensure that pending coalesced MMIO request are flushed before the memory
915 * region is accessed. This property is automatically enabled for all regions
916 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
918 * @mr: the memory region to be updated.
920 void memory_region_set_flush_coalesced(MemoryRegion
*mr
);
923 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
926 * Clear the automatic coalesced MMIO flushing enabled via
927 * memory_region_set_flush_coalesced. Note that this service has no effect on
928 * memory regions that have MMIO coalescing enabled for themselves. For them,
929 * automatic flushing will stop once coalescing is disabled.
931 * @mr: the memory region to be updated.
933 void memory_region_clear_flush_coalesced(MemoryRegion
*mr
);
936 * memory_region_set_global_locking: Declares the access processing requires
937 * QEMU's global lock.
939 * When this is invoked, accesses to the memory region will be processed while
940 * holding the global lock of QEMU. This is the default behavior of memory
943 * @mr: the memory region to be updated.
945 void memory_region_set_global_locking(MemoryRegion
*mr
);
948 * memory_region_clear_global_locking: Declares that access processing does
949 * not depend on the QEMU global lock.
951 * By clearing this property, accesses to the memory region will be processed
952 * outside of QEMU's global lock (unless the lock is held on when issuing the
953 * access request). In this case, the device model implementing the access
954 * handlers is responsible for synchronization of concurrency.
956 * @mr: the memory region to be updated.
958 void memory_region_clear_global_locking(MemoryRegion
*mr
);
961 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
962 * is written to a location.
964 * Marks a word in an IO region (initialized with memory_region_init_io())
965 * as a trigger for an eventfd event. The I/O callback will not be called.
966 * The caller must be prepared to handle failure (that is, take the required
967 * action if the callback _is_ called).
969 * @mr: the memory region being updated.
970 * @addr: the address within @mr that is to be monitored
971 * @size: the size of the access to trigger the eventfd
972 * @match_data: whether to match against @data, instead of just @addr
973 * @data: the data to match against the guest write
974 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
976 void memory_region_add_eventfd(MemoryRegion
*mr
,
984 * memory_region_del_eventfd: Cancel an eventfd.
986 * Cancels an eventfd trigger requested by a previous
987 * memory_region_add_eventfd() call.
989 * @mr: the memory region being updated.
990 * @addr: the address within @mr that is to be monitored
991 * @size: the size of the access to trigger the eventfd
992 * @match_data: whether to match against @data, instead of just @addr
993 * @data: the data to match against the guest write
994 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
996 void memory_region_del_eventfd(MemoryRegion
*mr
,
1004 * memory_region_add_subregion: Add a subregion to a container.
1006 * Adds a subregion at @offset. The subregion may not overlap with other
1007 * subregions (except for those explicitly marked as overlapping). A region
1008 * may only be added once as a subregion (unless removed with
1009 * memory_region_del_subregion()); use memory_region_init_alias() if you
1010 * want a region to be a subregion in multiple locations.
1012 * @mr: the region to contain the new subregion; must be a container
1013 * initialized with memory_region_init().
1014 * @offset: the offset relative to @mr where @subregion is added.
1015 * @subregion: the subregion to be added.
1017 void memory_region_add_subregion(MemoryRegion
*mr
,
1019 MemoryRegion
*subregion
);
1021 * memory_region_add_subregion_overlap: Add a subregion to a container
1024 * Adds a subregion at @offset. The subregion may overlap with other
1025 * subregions. Conflicts are resolved by having a higher @priority hide a
1026 * lower @priority. Subregions without priority are taken as @priority 0.
1027 * A region may only be added once as a subregion (unless removed with
1028 * memory_region_del_subregion()); use memory_region_init_alias() if you
1029 * want a region to be a subregion in multiple locations.
1031 * @mr: the region to contain the new subregion; must be a container
1032 * initialized with memory_region_init().
1033 * @offset: the offset relative to @mr where @subregion is added.
1034 * @subregion: the subregion to be added.
1035 * @priority: used for resolving overlaps; highest priority wins.
1037 void memory_region_add_subregion_overlap(MemoryRegion
*mr
,
1039 MemoryRegion
*subregion
,
1043 * memory_region_get_ram_addr: Get the ram address associated with a memory
1046 ram_addr_t
memory_region_get_ram_addr(MemoryRegion
*mr
);
1048 uint64_t memory_region_get_alignment(const MemoryRegion
*mr
);
1050 * memory_region_del_subregion: Remove a subregion.
1052 * Removes a subregion from its container.
1054 * @mr: the container to be updated.
1055 * @subregion: the region being removed; must be a current subregion of @mr.
1057 void memory_region_del_subregion(MemoryRegion
*mr
,
1058 MemoryRegion
*subregion
);
1061 * memory_region_set_enabled: dynamically enable or disable a region
1063 * Enables or disables a memory region. A disabled memory region
1064 * ignores all accesses to itself and its subregions. It does not
1065 * obscure sibling subregions with lower priority - it simply behaves as
1066 * if it was removed from the hierarchy.
1068 * Regions default to being enabled.
1070 * @mr: the region to be updated
1071 * @enabled: whether to enable or disable the region
1073 void memory_region_set_enabled(MemoryRegion
*mr
, bool enabled
);
1076 * memory_region_set_address: dynamically update the address of a region
1078 * Dynamically updates the address of a region, relative to its container.
1079 * May be used on regions are currently part of a memory hierarchy.
1081 * @mr: the region to be updated
1082 * @addr: new address, relative to container region
1084 void memory_region_set_address(MemoryRegion
*mr
, hwaddr addr
);
1087 * memory_region_set_size: dynamically update the size of a region.
1089 * Dynamically updates the size of a region.
1091 * @mr: the region to be updated
1092 * @size: used size of the region.
1094 void memory_region_set_size(MemoryRegion
*mr
, uint64_t size
);
1097 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1099 * Dynamically updates the offset into the target region that an alias points
1100 * to, as if the fourth argument to memory_region_init_alias() has changed.
1102 * @mr: the #MemoryRegion to be updated; should be an alias.
1103 * @offset: the new offset into the target memory region
1105 void memory_region_set_alias_offset(MemoryRegion
*mr
,
1109 * memory_region_present: checks if an address relative to a @container
1110 * translates into #MemoryRegion within @container
1112 * Answer whether a #MemoryRegion within @container covers the address
1115 * @container: a #MemoryRegion within which @addr is a relative address
1116 * @addr: the area within @container to be searched
1118 bool memory_region_present(MemoryRegion
*container
, hwaddr addr
);
1121 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1122 * into any address space.
1124 * @mr: a #MemoryRegion which should be checked if it's mapped
1126 bool memory_region_is_mapped(MemoryRegion
*mr
);
1129 * memory_region_find: translate an address/size relative to a
1130 * MemoryRegion into a #MemoryRegionSection.
1132 * Locates the first #MemoryRegion within @mr that overlaps the range
1133 * given by @addr and @size.
1135 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1136 * It will have the following characteristics:
1137 * .@size = 0 iff no overlap was found
1138 * .@mr is non-%NULL iff an overlap was found
1140 * Remember that in the return value the @offset_within_region is
1141 * relative to the returned region (in the .@mr field), not to the
1144 * Similarly, the .@offset_within_address_space is relative to the
1145 * address space that contains both regions, the passed and the
1146 * returned one. However, in the special case where the @mr argument
1147 * has no container (and thus is the root of the address space), the
1148 * following will hold:
1149 * .@offset_within_address_space >= @addr
1150 * .@offset_within_address_space + .@size <= @addr + @size
1152 * @mr: a MemoryRegion within which @addr is a relative address
1153 * @addr: start of the area within @as to be searched
1154 * @size: size of the area to be searched
1156 MemoryRegionSection
memory_region_find(MemoryRegion
*mr
,
1157 hwaddr addr
, uint64_t size
);
1160 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
1162 * Synchronizes the dirty page log for an entire address space.
1163 * @as: the address space that contains the memory being synchronized
1165 void address_space_sync_dirty_bitmap(AddressSpace
*as
);
1168 * memory_region_transaction_begin: Start a transaction.
1170 * During a transaction, changes will be accumulated and made visible
1171 * only when the transaction ends (is committed).
1173 void memory_region_transaction_begin(void);
1176 * memory_region_transaction_commit: Commit a transaction and make changes
1177 * visible to the guest.
1179 void memory_region_transaction_commit(void);
1182 * memory_listener_register: register callbacks to be called when memory
1183 * sections are mapped or unmapped into an address
1186 * @listener: an object containing the callbacks to be called
1187 * @filter: if non-%NULL, only regions in this address space will be observed
1189 void memory_listener_register(MemoryListener
*listener
, AddressSpace
*filter
);
1192 * memory_listener_unregister: undo the effect of memory_listener_register()
1194 * @listener: an object containing the callbacks to be removed
1196 void memory_listener_unregister(MemoryListener
*listener
);
1199 * memory_global_dirty_log_start: begin dirty logging for all regions
1201 void memory_global_dirty_log_start(void);
1204 * memory_global_dirty_log_stop: end dirty logging for all regions
1206 void memory_global_dirty_log_stop(void);
1208 void mtree_info(fprintf_function mon_printf
, void *f
);
1211 * memory_region_dispatch_read: perform a read directly to the specified
1214 * @mr: #MemoryRegion to access
1215 * @addr: address within that region
1216 * @pval: pointer to uint64_t which the data is written to
1217 * @size: size of the access in bytes
1218 * @attrs: memory transaction attributes to use for the access
1220 MemTxResult
memory_region_dispatch_read(MemoryRegion
*mr
,
1226 * memory_region_dispatch_write: perform a write directly to the specified
1229 * @mr: #MemoryRegion to access
1230 * @addr: address within that region
1231 * @data: data to write
1232 * @size: size of the access in bytes
1233 * @attrs: memory transaction attributes to use for the access
1235 MemTxResult
memory_region_dispatch_write(MemoryRegion
*mr
,
1242 * address_space_init: initializes an address space
1244 * @as: an uninitialized #AddressSpace
1245 * @root: a #MemoryRegion that routes addresses for the address space
1246 * @name: an address space name. The name is only used for debugging
1249 void address_space_init(AddressSpace
*as
, MemoryRegion
*root
, const char *name
);
1252 * address_space_init_shareable: return an address space for a memory region,
1253 * creating it if it does not already exist
1255 * @root: a #MemoryRegion that routes addresses for the address space
1256 * @name: an address space name. The name is only used for debugging
1259 * This function will return a pointer to an existing AddressSpace
1260 * which was initialized with the specified MemoryRegion, or it will
1261 * create and initialize one if it does not already exist. The ASes
1262 * are reference-counted, so the memory will be freed automatically
1263 * when the AddressSpace is destroyed via address_space_destroy.
1265 AddressSpace
*address_space_init_shareable(MemoryRegion
*root
,
1269 * address_space_destroy: destroy an address space
1271 * Releases all resources associated with an address space. After an address space
1272 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1275 * @as: address space to be destroyed
1277 void address_space_destroy(AddressSpace
*as
);
1280 * address_space_rw: read from or write to an address space.
1282 * Return a MemTxResult indicating whether the operation succeeded
1283 * or failed (eg unassigned memory, device rejected the transaction,
1286 * @as: #AddressSpace to be accessed
1287 * @addr: address within that address space
1288 * @attrs: memory transaction attributes
1289 * @buf: buffer with the data transferred
1290 * @is_write: indicates the transfer direction
1292 MemTxResult
address_space_rw(AddressSpace
*as
, hwaddr addr
,
1293 MemTxAttrs attrs
, uint8_t *buf
,
1294 int len
, bool is_write
);
1297 * address_space_write: write to address space.
1299 * Return a MemTxResult indicating whether the operation succeeded
1300 * or failed (eg unassigned memory, device rejected the transaction,
1303 * @as: #AddressSpace to be accessed
1304 * @addr: address within that address space
1305 * @attrs: memory transaction attributes
1306 * @buf: buffer with the data transferred
1308 MemTxResult
address_space_write(AddressSpace
*as
, hwaddr addr
,
1310 const uint8_t *buf
, int len
);
1312 /* address_space_ld*: load from an address space
1313 * address_space_st*: store to an address space
1315 * These functions perform a load or store of the byte, word,
1316 * longword or quad to the specified address within the AddressSpace.
1317 * The _le suffixed functions treat the data as little endian;
1318 * _be indicates big endian; no suffix indicates "same endianness
1321 * The "guest CPU endianness" accessors are deprecated for use outside
1322 * target-* code; devices should be CPU-agnostic and use either the LE
1323 * or the BE accessors.
1325 * @as #AddressSpace to be accessed
1326 * @addr: address within that address space
1327 * @val: data value, for stores
1328 * @attrs: memory transaction attributes
1329 * @result: location to write the success/failure of the transaction;
1330 * if NULL, this information is discarded
1332 uint32_t address_space_ldub(AddressSpace
*as
, hwaddr addr
,
1333 MemTxAttrs attrs
, MemTxResult
*result
);
1334 uint32_t address_space_lduw_le(AddressSpace
*as
, hwaddr addr
,
1335 MemTxAttrs attrs
, MemTxResult
*result
);
1336 uint32_t address_space_lduw_be(AddressSpace
*as
, hwaddr addr
,
1337 MemTxAttrs attrs
, MemTxResult
*result
);
1338 uint32_t address_space_ldl_le(AddressSpace
*as
, hwaddr addr
,
1339 MemTxAttrs attrs
, MemTxResult
*result
);
1340 uint32_t address_space_ldl_be(AddressSpace
*as
, hwaddr addr
,
1341 MemTxAttrs attrs
, MemTxResult
*result
);
1342 uint64_t address_space_ldq_le(AddressSpace
*as
, hwaddr addr
,
1343 MemTxAttrs attrs
, MemTxResult
*result
);
1344 uint64_t address_space_ldq_be(AddressSpace
*as
, hwaddr addr
,
1345 MemTxAttrs attrs
, MemTxResult
*result
);
1346 void address_space_stb(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1347 MemTxAttrs attrs
, MemTxResult
*result
);
1348 void address_space_stw_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1349 MemTxAttrs attrs
, MemTxResult
*result
);
1350 void address_space_stw_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1351 MemTxAttrs attrs
, MemTxResult
*result
);
1352 void address_space_stl_le(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1353 MemTxAttrs attrs
, MemTxResult
*result
);
1354 void address_space_stl_be(AddressSpace
*as
, hwaddr addr
, uint32_t val
,
1355 MemTxAttrs attrs
, MemTxResult
*result
);
1356 void address_space_stq_le(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1357 MemTxAttrs attrs
, MemTxResult
*result
);
1358 void address_space_stq_be(AddressSpace
*as
, hwaddr addr
, uint64_t val
,
1359 MemTxAttrs attrs
, MemTxResult
*result
);
1361 /* address_space_translate: translate an address range into an address space
1362 * into a MemoryRegion and an address range into that section. Should be
1363 * called from an RCU critical section, to avoid that the last reference
1364 * to the returned region disappears after address_space_translate returns.
1366 * @as: #AddressSpace to be accessed
1367 * @addr: address within that address space
1368 * @xlat: pointer to address within the returned memory region section's
1370 * @len: pointer to length
1371 * @is_write: indicates the transfer direction
1373 MemoryRegion
*address_space_translate(AddressSpace
*as
, hwaddr addr
,
1374 hwaddr
*xlat
, hwaddr
*len
,
1377 /* address_space_access_valid: check for validity of accessing an address
1380 * Check whether memory is assigned to the given address space range, and
1381 * access is permitted by any IOMMU regions that are active for the address
1384 * For now, addr and len should be aligned to a page size. This limitation
1385 * will be lifted in the future.
1387 * @as: #AddressSpace to be accessed
1388 * @addr: address within that address space
1389 * @len: length of the area to be checked
1390 * @is_write: indicates the transfer direction
1392 bool address_space_access_valid(AddressSpace
*as
, hwaddr addr
, int len
, bool is_write
);
1394 /* address_space_map: map a physical memory region into a host virtual address
1396 * May map a subset of the requested range, given by and returned in @plen.
1397 * May return %NULL if resources needed to perform the mapping are exhausted.
1398 * Use only for reads OR writes - not for read-modify-write operations.
1399 * Use cpu_register_map_client() to know when retrying the map operation is
1400 * likely to succeed.
1402 * @as: #AddressSpace to be accessed
1403 * @addr: address within that address space
1404 * @plen: pointer to length of buffer; updated on return
1405 * @is_write: indicates the transfer direction
1407 void *address_space_map(AddressSpace
*as
, hwaddr addr
,
1408 hwaddr
*plen
, bool is_write
);
1410 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1412 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1413 * the amount of memory that was actually read or written by the caller.
1415 * @as: #AddressSpace used
1416 * @addr: address within that address space
1417 * @len: buffer length as returned by address_space_map()
1418 * @access_len: amount of data actually transferred
1419 * @is_write: indicates the transfer direction
1421 void address_space_unmap(AddressSpace
*as
, void *buffer
, hwaddr len
,
1422 int is_write
, hwaddr access_len
);
1425 /* Internal functions, part of the implementation of address_space_read. */
1426 MemTxResult
address_space_read_continue(AddressSpace
*as
, hwaddr addr
,
1427 MemTxAttrs attrs
, uint8_t *buf
,
1428 int len
, hwaddr addr1
, hwaddr l
,
1430 MemTxResult
address_space_read_full(AddressSpace
*as
, hwaddr addr
,
1431 MemTxAttrs attrs
, uint8_t *buf
, int len
);
1432 void *qemu_map_ram_ptr(RAMBlock
*ram_block
, ram_addr_t addr
);
1434 static inline bool memory_access_is_direct(MemoryRegion
*mr
, bool is_write
)
1437 return memory_region_is_ram(mr
) && !mr
->readonly
;
1439 return memory_region_is_ram(mr
) || memory_region_is_romd(mr
);
1444 * address_space_read: read from an address space.
1446 * Return a MemTxResult indicating whether the operation succeeded
1447 * or failed (eg unassigned memory, device rejected the transaction,
1450 * @as: #AddressSpace to be accessed
1451 * @addr: address within that address space
1452 * @attrs: memory transaction attributes
1453 * @buf: buffer with the data transferred
1455 static inline __attribute__((__always_inline__
))
1456 MemTxResult
address_space_read(AddressSpace
*as
, hwaddr addr
, MemTxAttrs attrs
,
1457 uint8_t *buf
, int len
)
1459 MemTxResult result
= MEMTX_OK
;
1464 if (__builtin_constant_p(len
)) {
1468 mr
= address_space_translate(as
, addr
, &addr1
, &l
, false);
1469 if (len
== l
&& memory_access_is_direct(mr
, false)) {
1470 ptr
= qemu_map_ram_ptr(mr
->ram_block
, addr1
);
1471 memcpy(buf
, ptr
, len
);
1473 result
= address_space_read_continue(as
, addr
, attrs
, buf
, len
,
1479 result
= address_space_read_full(as
, addr
, attrs
, buf
, len
);