gitlab: always build container images
[qemu/ar7.git] / include / exec / memory.h
blobc6fb714e499cbb1c49e31cac1d11cfe9dbd4d2ce
1 /*
2 * Physical memory management API
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
6 * Authors:
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.
14 #ifndef MEMORY_H
15 #define MEMORY_H
17 #ifndef CONFIG_USER_ONLY
19 #include "exec/cpu-common.h"
20 #include "exec/hwaddr.h"
21 #include "exec/memattrs.h"
22 #include "exec/memop.h"
23 #include "exec/ramlist.h"
24 #include "qemu/bswap.h"
25 #include "qemu/queue.h"
26 #include "qemu/int128.h"
27 #include "qemu/notify.h"
28 #include "qom/object.h"
29 #include "qemu/rcu.h"
31 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
33 #define MAX_PHYS_ADDR_SPACE_BITS 62
34 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
36 #define TYPE_MEMORY_REGION "qemu:memory-region"
37 DECLARE_INSTANCE_CHECKER(MemoryRegion, MEMORY_REGION,
38 TYPE_MEMORY_REGION)
40 #define TYPE_IOMMU_MEMORY_REGION "qemu:iommu-memory-region"
41 typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionClass;
42 DECLARE_OBJ_CHECKERS(IOMMUMemoryRegion, IOMMUMemoryRegionClass,
43 IOMMU_MEMORY_REGION, TYPE_IOMMU_MEMORY_REGION)
45 #ifdef CONFIG_FUZZ
46 void fuzz_dma_read_cb(size_t addr,
47 size_t len,
48 MemoryRegion *mr);
49 #else
50 static inline void fuzz_dma_read_cb(size_t addr,
51 size_t len,
52 MemoryRegion *mr)
54 /* Do Nothing */
56 #endif
58 extern bool global_dirty_log;
60 typedef struct MemoryRegionOps MemoryRegionOps;
62 struct ReservedRegion {
63 hwaddr low;
64 hwaddr high;
65 unsigned type;
68 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
70 /* See address_space_translate: bit 0 is read, bit 1 is write. */
71 typedef enum {
72 IOMMU_NONE = 0,
73 IOMMU_RO = 1,
74 IOMMU_WO = 2,
75 IOMMU_RW = 3,
76 } IOMMUAccessFlags;
78 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
80 struct IOMMUTLBEntry {
81 AddressSpace *target_as;
82 hwaddr iova;
83 hwaddr translated_addr;
84 hwaddr addr_mask; /* 0xfff = 4k translation */
85 IOMMUAccessFlags perm;
89 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
90 * register with one or multiple IOMMU Notifier capability bit(s).
92 typedef enum {
93 IOMMU_NOTIFIER_NONE = 0,
94 /* Notify cache invalidations */
95 IOMMU_NOTIFIER_UNMAP = 0x1,
96 /* Notify entry changes (newly created entries) */
97 IOMMU_NOTIFIER_MAP = 0x2,
98 /* Notify changes on device IOTLB entries */
99 IOMMU_NOTIFIER_DEVIOTLB_UNMAP = 0x04,
100 } IOMMUNotifierFlag;
102 #define IOMMU_NOTIFIER_IOTLB_EVENTS (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
103 #define IOMMU_NOTIFIER_DEVIOTLB_EVENTS IOMMU_NOTIFIER_DEVIOTLB_UNMAP
104 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_IOTLB_EVENTS | \
105 IOMMU_NOTIFIER_DEVIOTLB_EVENTS)
107 struct IOMMUNotifier;
108 typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier,
109 IOMMUTLBEntry *data);
111 struct IOMMUNotifier {
112 IOMMUNotify notify;
113 IOMMUNotifierFlag notifier_flags;
114 /* Notify for address space range start <= addr <= end */
115 hwaddr start;
116 hwaddr end;
117 int iommu_idx;
118 QLIST_ENTRY(IOMMUNotifier) node;
120 typedef struct IOMMUNotifier IOMMUNotifier;
122 typedef struct IOMMUTLBEvent {
123 IOMMUNotifierFlag type;
124 IOMMUTLBEntry entry;
125 } IOMMUTLBEvent;
127 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
128 #define RAM_PREALLOC (1 << 0)
130 /* RAM is mmap-ed with MAP_SHARED */
131 #define RAM_SHARED (1 << 1)
133 /* Only a portion of RAM (used_length) is actually used, and migrated.
134 * This used_length size can change across reboots.
136 #define RAM_RESIZEABLE (1 << 2)
138 /* UFFDIO_ZEROPAGE is available on this RAMBlock to atomically
139 * zero the page and wake waiting processes.
140 * (Set during postcopy)
142 #define RAM_UF_ZEROPAGE (1 << 3)
144 /* RAM can be migrated */
145 #define RAM_MIGRATABLE (1 << 4)
147 /* RAM is a persistent kind memory */
148 #define RAM_PMEM (1 << 5)
152 * UFFDIO_WRITEPROTECT is used on this RAMBlock to
153 * support 'write-tracking' migration type.
154 * Implies ram_state->ram_wt_enabled.
156 #define RAM_UF_WRITEPROTECT (1 << 6)
158 static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
159 IOMMUNotifierFlag flags,
160 hwaddr start, hwaddr end,
161 int iommu_idx)
163 n->notify = fn;
164 n->notifier_flags = flags;
165 n->start = start;
166 n->end = end;
167 n->iommu_idx = iommu_idx;
171 * Memory region callbacks
173 struct MemoryRegionOps {
174 /* Read from the memory region. @addr is relative to @mr; @size is
175 * in bytes. */
176 uint64_t (*read)(void *opaque,
177 hwaddr addr,
178 unsigned size);
179 /* Write to the memory region. @addr is relative to @mr; @size is
180 * in bytes. */
181 void (*write)(void *opaque,
182 hwaddr addr,
183 uint64_t data,
184 unsigned size);
186 MemTxResult (*read_with_attrs)(void *opaque,
187 hwaddr addr,
188 uint64_t *data,
189 unsigned size,
190 MemTxAttrs attrs);
191 MemTxResult (*write_with_attrs)(void *opaque,
192 hwaddr addr,
193 uint64_t data,
194 unsigned size,
195 MemTxAttrs attrs);
197 enum device_endian endianness;
198 /* Guest-visible constraints: */
199 struct {
200 /* If nonzero, specify bounds on access sizes beyond which a machine
201 * check is thrown.
203 unsigned min_access_size;
204 unsigned max_access_size;
205 /* If true, unaligned accesses are supported. Otherwise unaligned
206 * accesses throw machine checks.
208 bool unaligned;
210 * If present, and returns #false, the transaction is not accepted
211 * by the device (and results in machine dependent behaviour such
212 * as a machine check exception).
214 bool (*accepts)(void *opaque, hwaddr addr,
215 unsigned size, bool is_write,
216 MemTxAttrs attrs);
217 } valid;
218 /* Internal implementation constraints: */
219 struct {
220 /* If nonzero, specifies the minimum size implemented. Smaller sizes
221 * will be rounded upwards and a partial result will be returned.
223 unsigned min_access_size;
224 /* If nonzero, specifies the maximum size implemented. Larger sizes
225 * will be done as a series of accesses with smaller sizes.
227 unsigned max_access_size;
228 /* If true, unaligned accesses are supported. Otherwise all accesses
229 * are converted to (possibly multiple) naturally aligned accesses.
231 bool unaligned;
232 } impl;
235 typedef struct MemoryRegionClass {
236 /* private */
237 ObjectClass parent_class;
238 } MemoryRegionClass;
241 enum IOMMUMemoryRegionAttr {
242 IOMMU_ATTR_SPAPR_TCE_FD
246 * IOMMUMemoryRegionClass:
248 * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
249 * and provide an implementation of at least the @translate method here
250 * to handle requests to the memory region. Other methods are optional.
252 * The IOMMU implementation must use the IOMMU notifier infrastructure
253 * to report whenever mappings are changed, by calling
254 * memory_region_notify_iommu() (or, if necessary, by calling
255 * memory_region_notify_iommu_one() for each registered notifier).
257 * Conceptually an IOMMU provides a mapping from input address
258 * to an output TLB entry. If the IOMMU is aware of memory transaction
259 * attributes and the output TLB entry depends on the transaction
260 * attributes, we represent this using IOMMU indexes. Each index
261 * selects a particular translation table that the IOMMU has:
263 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
265 * @translate takes an input address and an IOMMU index
267 * and the mapping returned can only depend on the input address and the
268 * IOMMU index.
270 * Most IOMMUs don't care about the transaction attributes and support
271 * only a single IOMMU index. A more complex IOMMU might have one index
272 * for secure transactions and one for non-secure transactions.
274 struct IOMMUMemoryRegionClass {
275 /* private: */
276 MemoryRegionClass parent_class;
278 /* public: */
280 * @translate:
282 * Return a TLB entry that contains a given address.
284 * The IOMMUAccessFlags indicated via @flag are optional and may
285 * be specified as IOMMU_NONE to indicate that the caller needs
286 * the full translation information for both reads and writes. If
287 * the access flags are specified then the IOMMU implementation
288 * may use this as an optimization, to stop doing a page table
289 * walk as soon as it knows that the requested permissions are not
290 * allowed. If IOMMU_NONE is passed then the IOMMU must do the
291 * full page table walk and report the permissions in the returned
292 * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
293 * return different mappings for reads and writes.)
295 * The returned information remains valid while the caller is
296 * holding the big QEMU lock or is inside an RCU critical section;
297 * if the caller wishes to cache the mapping beyond that it must
298 * register an IOMMU notifier so it can invalidate its cached
299 * information when the IOMMU mapping changes.
301 * @iommu: the IOMMUMemoryRegion
303 * @hwaddr: address to be translated within the memory region
305 * @flag: requested access permission
307 * @iommu_idx: IOMMU index for the translation
309 IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr,
310 IOMMUAccessFlags flag, int iommu_idx);
312 * @get_min_page_size:
314 * Returns minimum supported page size in bytes.
316 * If this method is not provided then the minimum is assumed to
317 * be TARGET_PAGE_SIZE.
319 * @iommu: the IOMMUMemoryRegion
321 uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu);
323 * @notify_flag_changed:
325 * Called when IOMMU Notifier flag changes (ie when the set of
326 * events which IOMMU users are requesting notification for changes).
327 * Optional method -- need not be provided if the IOMMU does not
328 * need to know exactly which events must be notified.
330 * @iommu: the IOMMUMemoryRegion
332 * @old_flags: events which previously needed to be notified
334 * @new_flags: events which now need to be notified
336 * Returns 0 on success, or a negative errno; in particular
337 * returns -EINVAL if the new flag bitmap is not supported by the
338 * IOMMU memory region. In case of failure, the error object
339 * must be created
341 int (*notify_flag_changed)(IOMMUMemoryRegion *iommu,
342 IOMMUNotifierFlag old_flags,
343 IOMMUNotifierFlag new_flags,
344 Error **errp);
346 * @replay:
348 * Called to handle memory_region_iommu_replay().
350 * The default implementation of memory_region_iommu_replay() is to
351 * call the IOMMU translate method for every page in the address space
352 * with flag == IOMMU_NONE and then call the notifier if translate
353 * returns a valid mapping. If this method is implemented then it
354 * overrides the default behaviour, and must provide the full semantics
355 * of memory_region_iommu_replay(), by calling @notifier for every
356 * translation present in the IOMMU.
358 * Optional method -- an IOMMU only needs to provide this method
359 * if the default is inefficient or produces undesirable side effects.
361 * Note: this is not related to record-and-replay functionality.
363 void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier);
366 * @get_attr:
368 * Get IOMMU misc attributes. This is an optional method that
369 * can be used to allow users of the IOMMU to get implementation-specific
370 * information. The IOMMU implements this method to handle calls
371 * by IOMMU users to memory_region_iommu_get_attr() by filling in
372 * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
373 * the IOMMU supports. If the method is unimplemented then
374 * memory_region_iommu_get_attr() will always return -EINVAL.
376 * @iommu: the IOMMUMemoryRegion
378 * @attr: attribute being queried
380 * @data: memory to fill in with the attribute data
382 * Returns 0 on success, or a negative errno; in particular
383 * returns -EINVAL for unrecognized or unimplemented attribute types.
385 int (*get_attr)(IOMMUMemoryRegion *iommu, enum IOMMUMemoryRegionAttr attr,
386 void *data);
389 * @attrs_to_index:
391 * Return the IOMMU index to use for a given set of transaction attributes.
393 * Optional method: if an IOMMU only supports a single IOMMU index then
394 * the default implementation of memory_region_iommu_attrs_to_index()
395 * will return 0.
397 * The indexes supported by an IOMMU must be contiguous, starting at 0.
399 * @iommu: the IOMMUMemoryRegion
400 * @attrs: memory transaction attributes
402 int (*attrs_to_index)(IOMMUMemoryRegion *iommu, MemTxAttrs attrs);
405 * @num_indexes:
407 * Return the number of IOMMU indexes this IOMMU supports.
409 * Optional method: if this method is not provided, then
410 * memory_region_iommu_num_indexes() will return 1, indicating that
411 * only a single IOMMU index is supported.
413 * @iommu: the IOMMUMemoryRegion
415 int (*num_indexes)(IOMMUMemoryRegion *iommu);
418 * @iommu_set_page_size_mask:
420 * Restrict the page size mask that can be supported with a given IOMMU
421 * memory region. Used for example to propagate host physical IOMMU page
422 * size mask limitations to the virtual IOMMU.
424 * Optional method: if this method is not provided, then the default global
425 * page mask is used.
427 * @iommu: the IOMMUMemoryRegion
429 * @page_size_mask: a bitmask of supported page sizes. At least one bit,
430 * representing the smallest page size, must be set. Additional set bits
431 * represent supported block sizes. For example a host physical IOMMU that
432 * uses page tables with a page size of 4kB, and supports 2MB and 4GB
433 * blocks, will set mask 0x40201000. A granule of 4kB with indiscriminate
434 * block sizes is specified with mask 0xfffffffffffff000.
436 * Returns 0 on success, or a negative error. In case of failure, the error
437 * object must be created.
439 int (*iommu_set_page_size_mask)(IOMMUMemoryRegion *iommu,
440 uint64_t page_size_mask,
441 Error **errp);
444 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
445 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
447 /** MemoryRegion:
449 * A struct representing a memory region.
451 struct MemoryRegion {
452 Object parent_obj;
454 /* private: */
456 /* The following fields should fit in a cache line */
457 bool romd_mode;
458 bool ram;
459 bool subpage;
460 bool readonly; /* For RAM regions */
461 bool nonvolatile;
462 bool rom_device;
463 bool flush_coalesced_mmio;
464 uint8_t dirty_log_mask;
465 bool is_iommu;
466 RAMBlock *ram_block;
467 Object *owner;
469 const MemoryRegionOps *ops;
470 void *opaque;
471 MemoryRegion *container;
472 Int128 size;
473 hwaddr addr;
474 void (*destructor)(MemoryRegion *mr);
475 uint64_t align;
476 bool terminates;
477 bool ram_device;
478 bool enabled;
479 bool warning_printed; /* For reservations */
480 uint8_t vga_logging_count;
481 MemoryRegion *alias;
482 hwaddr alias_offset;
483 int32_t priority;
484 QTAILQ_HEAD(, MemoryRegion) subregions;
485 QTAILQ_ENTRY(MemoryRegion) subregions_link;
486 QTAILQ_HEAD(, CoalescedMemoryRange) coalesced;
487 const char *name;
488 unsigned ioeventfd_nb;
489 MemoryRegionIoeventfd *ioeventfds;
492 struct IOMMUMemoryRegion {
493 MemoryRegion parent_obj;
495 QLIST_HEAD(, IOMMUNotifier) iommu_notify;
496 IOMMUNotifierFlag iommu_notify_flags;
499 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
500 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
503 * struct MemoryListener: callbacks structure for updates to the physical memory map
505 * Allows a component to adjust to changes in the guest-visible memory map.
506 * Use with memory_listener_register() and memory_listener_unregister().
508 struct MemoryListener {
510 * @begin:
512 * Called at the beginning of an address space update transaction.
513 * Followed by calls to #MemoryListener.region_add(),
514 * #MemoryListener.region_del(), #MemoryListener.region_nop(),
515 * #MemoryListener.log_start() and #MemoryListener.log_stop() in
516 * increasing address order.
518 * @listener: The #MemoryListener.
520 void (*begin)(MemoryListener *listener);
523 * @commit:
525 * Called at the end of an address space update transaction,
526 * after the last call to #MemoryListener.region_add(),
527 * #MemoryListener.region_del() or #MemoryListener.region_nop(),
528 * #MemoryListener.log_start() and #MemoryListener.log_stop().
530 * @listener: The #MemoryListener.
532 void (*commit)(MemoryListener *listener);
535 * @region_add:
537 * Called during an address space update transaction,
538 * for a section of the address space that is new in this address space
539 * space since the last transaction.
541 * @listener: The #MemoryListener.
542 * @section: The new #MemoryRegionSection.
544 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
547 * @region_del:
549 * Called during an address space update transaction,
550 * for a section of the address space that has disappeared in the address
551 * space since the last transaction.
553 * @listener: The #MemoryListener.
554 * @section: The old #MemoryRegionSection.
556 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
559 * @region_nop:
561 * Called during an address space update transaction,
562 * for a section of the address space that is in the same place in the address
563 * space as in the last transaction.
565 * @listener: The #MemoryListener.
566 * @section: The #MemoryRegionSection.
568 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
571 * @log_start:
573 * Called during an address space update transaction, after
574 * one of #MemoryListener.region_add(),#MemoryListener.region_del() or
575 * #MemoryListener.region_nop(), if dirty memory logging clients have
576 * become active since the last transaction.
578 * @listener: The #MemoryListener.
579 * @section: The #MemoryRegionSection.
580 * @old: A bitmap of dirty memory logging clients that were active in
581 * the previous transaction.
582 * @new: A bitmap of dirty memory logging clients that are active in
583 * the current transaction.
585 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
586 int old, int new);
589 * @log_stop:
591 * Called during an address space update transaction, after
592 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or
593 * #MemoryListener.region_nop() and possibly after
594 * #MemoryListener.log_start(), if dirty memory logging clients have
595 * become inactive since the last transaction.
597 * @listener: The #MemoryListener.
598 * @section: The #MemoryRegionSection.
599 * @old: A bitmap of dirty memory logging clients that were active in
600 * the previous transaction.
601 * @new: A bitmap of dirty memory logging clients that are active in
602 * the current transaction.
604 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
605 int old, int new);
608 * @log_sync:
610 * Called by memory_region_snapshot_and_clear_dirty() and
611 * memory_global_dirty_log_sync(), before accessing QEMU's "official"
612 * copy of the dirty memory bitmap for a #MemoryRegionSection.
614 * @listener: The #MemoryListener.
615 * @section: The #MemoryRegionSection.
617 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
620 * @log_clear:
622 * Called before reading the dirty memory bitmap for a
623 * #MemoryRegionSection.
625 * @listener: The #MemoryListener.
626 * @section: The #MemoryRegionSection.
628 void (*log_clear)(MemoryListener *listener, MemoryRegionSection *section);
631 * @log_global_start:
633 * Called by memory_global_dirty_log_start(), which
634 * enables the %DIRTY_LOG_MIGRATION client on all memory regions in
635 * the address space. #MemoryListener.log_global_start() is also
636 * called when a #MemoryListener is added, if global dirty logging is
637 * active at that time.
639 * @listener: The #MemoryListener.
641 void (*log_global_start)(MemoryListener *listener);
644 * @log_global_stop:
646 * Called by memory_global_dirty_log_stop(), which
647 * disables the %DIRTY_LOG_MIGRATION client on all memory regions in
648 * the address space.
650 * @listener: The #MemoryListener.
652 void (*log_global_stop)(MemoryListener *listener);
655 * @log_global_after_sync:
657 * Called after reading the dirty memory bitmap
658 * for any #MemoryRegionSection.
660 * @listener: The #MemoryListener.
662 void (*log_global_after_sync)(MemoryListener *listener);
665 * @eventfd_add:
667 * Called during an address space update transaction,
668 * for a section of the address space that has had a new ioeventfd
669 * registration since the last transaction.
671 * @listener: The #MemoryListener.
672 * @section: The new #MemoryRegionSection.
673 * @match_data: The @match_data parameter for the new ioeventfd.
674 * @data: The @data parameter for the new ioeventfd.
675 * @e: The #EventNotifier parameter for the new ioeventfd.
677 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
678 bool match_data, uint64_t data, EventNotifier *e);
681 * @eventfd_del:
683 * Called during an address space update transaction,
684 * for a section of the address space that has dropped an ioeventfd
685 * registration since the last transaction.
687 * @listener: The #MemoryListener.
688 * @section: The new #MemoryRegionSection.
689 * @match_data: The @match_data parameter for the dropped ioeventfd.
690 * @data: The @data parameter for the dropped ioeventfd.
691 * @e: The #EventNotifier parameter for the dropped ioeventfd.
693 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
694 bool match_data, uint64_t data, EventNotifier *e);
697 * @coalesced_io_add:
699 * Called during an address space update transaction,
700 * for a section of the address space that has had a new coalesced
701 * MMIO range registration since the last transaction.
703 * @listener: The #MemoryListener.
704 * @section: The new #MemoryRegionSection.
705 * @addr: The starting address for the coalesced MMIO range.
706 * @len: The length of the coalesced MMIO range.
708 void (*coalesced_io_add)(MemoryListener *listener, MemoryRegionSection *section,
709 hwaddr addr, hwaddr len);
712 * @coalesced_io_del:
714 * Called during an address space update transaction,
715 * for a section of the address space that has dropped a coalesced
716 * MMIO range since the last transaction.
718 * @listener: The #MemoryListener.
719 * @section: The new #MemoryRegionSection.
720 * @addr: The starting address for the coalesced MMIO range.
721 * @len: The length of the coalesced MMIO range.
723 void (*coalesced_io_del)(MemoryListener *listener, MemoryRegionSection *section,
724 hwaddr addr, hwaddr len);
726 * @priority:
728 * Govern the order in which memory listeners are invoked. Lower priorities
729 * are invoked earlier for "add" or "start" callbacks, and later for "delete"
730 * or "stop" callbacks.
732 unsigned priority;
734 /* private: */
735 AddressSpace *address_space;
736 QTAILQ_ENTRY(MemoryListener) link;
737 QTAILQ_ENTRY(MemoryListener) link_as;
741 * struct AddressSpace: describes a mapping of addresses to #MemoryRegion objects
743 struct AddressSpace {
744 /* private: */
745 struct rcu_head rcu;
746 char *name;
747 MemoryRegion *root;
749 /* Accessed via RCU. */
750 struct FlatView *current_map;
752 int ioeventfd_nb;
753 struct MemoryRegionIoeventfd *ioeventfds;
754 QTAILQ_HEAD(, MemoryListener) listeners;
755 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
758 typedef struct AddressSpaceDispatch AddressSpaceDispatch;
759 typedef struct FlatRange FlatRange;
761 /* Flattened global view of current active memory hierarchy. Kept in sorted
762 * order.
764 struct FlatView {
765 struct rcu_head rcu;
766 unsigned ref;
767 FlatRange *ranges;
768 unsigned nr;
769 unsigned nr_allocated;
770 struct AddressSpaceDispatch *dispatch;
771 MemoryRegion *root;
774 static inline FlatView *address_space_to_flatview(AddressSpace *as)
776 return qatomic_rcu_read(&as->current_map);
779 typedef int (*flatview_cb)(Int128 start,
780 Int128 len,
781 const MemoryRegion*, void*);
783 void flatview_for_each_range(FlatView *fv, flatview_cb cb , void *opaque);
786 * struct MemoryRegionSection: describes a fragment of a #MemoryRegion
788 * @mr: the region, or %NULL if empty
789 * @fv: the flat view of the address space the region is mapped in
790 * @offset_within_region: the beginning of the section, relative to @mr's start
791 * @size: the size of the section; will not exceed @mr's boundaries
792 * @offset_within_address_space: the address of the first byte of the section
793 * relative to the region's address space
794 * @readonly: writes to this section are ignored
795 * @nonvolatile: this section is non-volatile
797 struct MemoryRegionSection {
798 Int128 size;
799 MemoryRegion *mr;
800 FlatView *fv;
801 hwaddr offset_within_region;
802 hwaddr offset_within_address_space;
803 bool readonly;
804 bool nonvolatile;
807 static inline bool MemoryRegionSection_eq(MemoryRegionSection *a,
808 MemoryRegionSection *b)
810 return a->mr == b->mr &&
811 a->fv == b->fv &&
812 a->offset_within_region == b->offset_within_region &&
813 a->offset_within_address_space == b->offset_within_address_space &&
814 int128_eq(a->size, b->size) &&
815 a->readonly == b->readonly &&
816 a->nonvolatile == b->nonvolatile;
820 * memory_region_init: Initialize a memory region
822 * The region typically acts as a container for other memory regions. Use
823 * memory_region_add_subregion() to add subregions.
825 * @mr: the #MemoryRegion to be initialized
826 * @owner: the object that tracks the region's reference count
827 * @name: used for debugging; not visible to the user or ABI
828 * @size: size of the region; any subregions beyond this size will be clipped
830 void memory_region_init(MemoryRegion *mr,
831 struct Object *owner,
832 const char *name,
833 uint64_t size);
836 * memory_region_ref: Add 1 to a memory region's reference count
838 * Whenever memory regions are accessed outside the BQL, they need to be
839 * preserved against hot-unplug. MemoryRegions actually do not have their
840 * own reference count; they piggyback on a QOM object, their "owner".
841 * This function adds a reference to the owner.
843 * All MemoryRegions must have an owner if they can disappear, even if the
844 * device they belong to operates exclusively under the BQL. This is because
845 * the region could be returned at any time by memory_region_find, and this
846 * is usually under guest control.
848 * @mr: the #MemoryRegion
850 void memory_region_ref(MemoryRegion *mr);
853 * memory_region_unref: Remove 1 to a memory region's reference count
855 * Whenever memory regions are accessed outside the BQL, they need to be
856 * preserved against hot-unplug. MemoryRegions actually do not have their
857 * own reference count; they piggyback on a QOM object, their "owner".
858 * This function removes a reference to the owner and possibly destroys it.
860 * @mr: the #MemoryRegion
862 void memory_region_unref(MemoryRegion *mr);
865 * memory_region_init_io: Initialize an I/O memory region.
867 * Accesses into the region will cause the callbacks in @ops to be called.
868 * if @size is nonzero, subregions will be clipped to @size.
870 * @mr: the #MemoryRegion to be initialized.
871 * @owner: the object that tracks the region's reference count
872 * @ops: a structure containing read and write callbacks to be used when
873 * I/O is performed on the region.
874 * @opaque: passed to the read and write callbacks of the @ops structure.
875 * @name: used for debugging; not visible to the user or ABI
876 * @size: size of the region.
878 void memory_region_init_io(MemoryRegion *mr,
879 struct Object *owner,
880 const MemoryRegionOps *ops,
881 void *opaque,
882 const char *name,
883 uint64_t size);
886 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
887 * into the region will modify memory
888 * directly.
890 * @mr: the #MemoryRegion to be initialized.
891 * @owner: the object that tracks the region's reference count
892 * @name: Region name, becomes part of RAMBlock name used in migration stream
893 * must be unique within any device
894 * @size: size of the region.
895 * @errp: pointer to Error*, to store an error if it happens.
897 * Note that this function does not do anything to cause the data in the
898 * RAM memory region to be migrated; that is the responsibility of the caller.
900 void memory_region_init_ram_nomigrate(MemoryRegion *mr,
901 struct Object *owner,
902 const char *name,
903 uint64_t size,
904 Error **errp);
907 * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region.
908 * Accesses into the region will
909 * modify memory directly.
911 * @mr: the #MemoryRegion to be initialized.
912 * @owner: the object that tracks the region's reference count
913 * @name: Region name, becomes part of RAMBlock name used in migration stream
914 * must be unique within any device
915 * @size: size of the region.
916 * @share: allow remapping RAM to different addresses
917 * @errp: pointer to Error*, to store an error if it happens.
919 * Note that this function is similar to memory_region_init_ram_nomigrate.
920 * The only difference is part of the RAM region can be remapped.
922 void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
923 struct Object *owner,
924 const char *name,
925 uint64_t size,
926 bool share,
927 Error **errp);
930 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
931 * RAM. Accesses into the region will
932 * modify memory directly. Only an initial
933 * portion of this RAM is actually used.
934 * The used size can change across reboots.
936 * @mr: the #MemoryRegion to be initialized.
937 * @owner: the object that tracks the region's reference count
938 * @name: Region name, becomes part of RAMBlock name used in migration stream
939 * must be unique within any device
940 * @size: used size of the region.
941 * @max_size: max size of the region.
942 * @resized: callback to notify owner about used size change.
943 * @errp: pointer to Error*, to store an error if it happens.
945 * Note that this function does not do anything to cause the data in the
946 * RAM memory region to be migrated; that is the responsibility of the caller.
948 void memory_region_init_resizeable_ram(MemoryRegion *mr,
949 struct Object *owner,
950 const char *name,
951 uint64_t size,
952 uint64_t max_size,
953 void (*resized)(const char*,
954 uint64_t length,
955 void *host),
956 Error **errp);
957 #ifdef CONFIG_POSIX
960 * memory_region_init_ram_from_file: Initialize RAM memory region with a
961 * mmap-ed backend.
963 * @mr: the #MemoryRegion to be initialized.
964 * @owner: the object that tracks the region's reference count
965 * @name: Region name, becomes part of RAMBlock name used in migration stream
966 * must be unique within any device
967 * @size: size of the region.
968 * @align: alignment of the region base address; if 0, the default alignment
969 * (getpagesize()) will be used.
970 * @ram_flags: Memory region features:
971 * - RAM_SHARED: memory must be mmaped with the MAP_SHARED flag
972 * - RAM_PMEM: the memory is persistent memory
973 * Other bits are ignored now.
974 * @path: the path in which to allocate the RAM.
975 * @readonly: true to open @path for reading, false for read/write.
976 * @errp: pointer to Error*, to store an error if it happens.
978 * Note that this function does not do anything to cause the data in the
979 * RAM memory region to be migrated; that is the responsibility of the caller.
981 void memory_region_init_ram_from_file(MemoryRegion *mr,
982 struct Object *owner,
983 const char *name,
984 uint64_t size,
985 uint64_t align,
986 uint32_t ram_flags,
987 const char *path,
988 bool readonly,
989 Error **errp);
992 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
993 * mmap-ed backend.
995 * @mr: the #MemoryRegion to be initialized.
996 * @owner: the object that tracks the region's reference count
997 * @name: the name of the region.
998 * @size: size of the region.
999 * @share: %true if memory must be mmaped with the MAP_SHARED flag
1000 * @fd: the fd to mmap.
1001 * @offset: offset within the file referenced by fd
1002 * @errp: pointer to Error*, to store an error if it happens.
1004 * Note that this function does not do anything to cause the data in the
1005 * RAM memory region to be migrated; that is the responsibility of the caller.
1007 void memory_region_init_ram_from_fd(MemoryRegion *mr,
1008 struct Object *owner,
1009 const char *name,
1010 uint64_t size,
1011 bool share,
1012 int fd,
1013 ram_addr_t offset,
1014 Error **errp);
1015 #endif
1018 * memory_region_init_ram_ptr: Initialize RAM memory region from a
1019 * user-provided pointer. Accesses into the
1020 * region will modify memory directly.
1022 * @mr: the #MemoryRegion to be initialized.
1023 * @owner: the object that tracks the region's reference count
1024 * @name: Region name, becomes part of RAMBlock name used in migration stream
1025 * must be unique within any device
1026 * @size: size of the region.
1027 * @ptr: memory to be mapped; must contain at least @size bytes.
1029 * Note that this function does not do anything to cause the data in the
1030 * RAM memory region to be migrated; that is the responsibility of the caller.
1032 void memory_region_init_ram_ptr(MemoryRegion *mr,
1033 struct Object *owner,
1034 const char *name,
1035 uint64_t size,
1036 void *ptr);
1039 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
1040 * a user-provided pointer.
1042 * A RAM device represents a mapping to a physical device, such as to a PCI
1043 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
1044 * into the VM address space and access to the region will modify memory
1045 * directly. However, the memory region should not be included in a memory
1046 * dump (device may not be enabled/mapped at the time of the dump), and
1047 * operations incompatible with manipulating MMIO should be avoided. Replaces
1048 * skip_dump flag.
1050 * @mr: the #MemoryRegion to be initialized.
1051 * @owner: the object that tracks the region's reference count
1052 * @name: the name of the region.
1053 * @size: size of the region.
1054 * @ptr: memory to be mapped; must contain at least @size bytes.
1056 * Note that this function does not do anything to cause the data in the
1057 * RAM memory region to be migrated; that is the responsibility of the caller.
1058 * (For RAM device memory regions, migrating the contents rarely makes sense.)
1060 void memory_region_init_ram_device_ptr(MemoryRegion *mr,
1061 struct Object *owner,
1062 const char *name,
1063 uint64_t size,
1064 void *ptr);
1067 * memory_region_init_alias: Initialize a memory region that aliases all or a
1068 * part of another memory region.
1070 * @mr: the #MemoryRegion to be initialized.
1071 * @owner: the object that tracks the region's reference count
1072 * @name: used for debugging; not visible to the user or ABI
1073 * @orig: the region to be referenced; @mr will be equivalent to
1074 * @orig between @offset and @offset + @size - 1.
1075 * @offset: start of the section in @orig to be referenced.
1076 * @size: size of the region.
1078 void memory_region_init_alias(MemoryRegion *mr,
1079 struct Object *owner,
1080 const char *name,
1081 MemoryRegion *orig,
1082 hwaddr offset,
1083 uint64_t size);
1086 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
1088 * This has the same effect as calling memory_region_init_ram_nomigrate()
1089 * and then marking the resulting region read-only with
1090 * memory_region_set_readonly().
1092 * Note that this function does not do anything to cause the data in the
1093 * RAM side of the memory region to be migrated; that is the responsibility
1094 * of the caller.
1096 * @mr: the #MemoryRegion to be initialized.
1097 * @owner: the object that tracks the region's reference count
1098 * @name: Region name, becomes part of RAMBlock name used in migration stream
1099 * must be unique within any device
1100 * @size: size of the region.
1101 * @errp: pointer to Error*, to store an error if it happens.
1103 void memory_region_init_rom_nomigrate(MemoryRegion *mr,
1104 struct Object *owner,
1105 const char *name,
1106 uint64_t size,
1107 Error **errp);
1110 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
1111 * Writes are handled via callbacks.
1113 * Note that this function does not do anything to cause the data in the
1114 * RAM side of the memory region to be migrated; that is the responsibility
1115 * of the caller.
1117 * @mr: the #MemoryRegion to be initialized.
1118 * @owner: the object that tracks the region's reference count
1119 * @ops: callbacks for write access handling (must not be NULL).
1120 * @opaque: passed to the read and write callbacks of the @ops structure.
1121 * @name: Region name, becomes part of RAMBlock name used in migration stream
1122 * must be unique within any device
1123 * @size: size of the region.
1124 * @errp: pointer to Error*, to store an error if it happens.
1126 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
1127 struct Object *owner,
1128 const MemoryRegionOps *ops,
1129 void *opaque,
1130 const char *name,
1131 uint64_t size,
1132 Error **errp);
1135 * memory_region_init_iommu: Initialize a memory region of a custom type
1136 * that translates addresses
1138 * An IOMMU region translates addresses and forwards accesses to a target
1139 * memory region.
1141 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
1142 * @_iommu_mr should be a pointer to enough memory for an instance of
1143 * that subclass, @instance_size is the size of that subclass, and
1144 * @mrtypename is its name. This function will initialize @_iommu_mr as an
1145 * instance of the subclass, and its methods will then be called to handle
1146 * accesses to the memory region. See the documentation of
1147 * #IOMMUMemoryRegionClass for further details.
1149 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
1150 * @instance_size: the IOMMUMemoryRegion subclass instance size
1151 * @mrtypename: the type name of the #IOMMUMemoryRegion
1152 * @owner: the object that tracks the region's reference count
1153 * @name: used for debugging; not visible to the user or ABI
1154 * @size: size of the region.
1156 void memory_region_init_iommu(void *_iommu_mr,
1157 size_t instance_size,
1158 const char *mrtypename,
1159 Object *owner,
1160 const char *name,
1161 uint64_t size);
1164 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
1165 * region will modify memory directly.
1167 * @mr: the #MemoryRegion to be initialized
1168 * @owner: the object that tracks the region's reference count (must be
1169 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
1170 * @name: name of the memory region
1171 * @size: size of the region in bytes
1172 * @errp: pointer to Error*, to store an error if it happens.
1174 * This function allocates RAM for a board model or device, and
1175 * arranges for it to be migrated (by calling vmstate_register_ram()
1176 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1177 * @owner is NULL).
1179 * TODO: Currently we restrict @owner to being either NULL (for
1180 * global RAM regions with no owner) or devices, so that we can
1181 * give the RAM block a unique name for migration purposes.
1182 * We should lift this restriction and allow arbitrary Objects.
1183 * If you pass a non-NULL non-device @owner then we will assert.
1185 void memory_region_init_ram(MemoryRegion *mr,
1186 struct Object *owner,
1187 const char *name,
1188 uint64_t size,
1189 Error **errp);
1192 * memory_region_init_rom: Initialize a ROM memory region.
1194 * This has the same effect as calling memory_region_init_ram()
1195 * and then marking the resulting region read-only with
1196 * memory_region_set_readonly(). This includes arranging for the
1197 * contents to be migrated.
1199 * TODO: Currently we restrict @owner to being either NULL (for
1200 * global RAM regions with no owner) or devices, so that we can
1201 * give the RAM block a unique name for migration purposes.
1202 * We should lift this restriction and allow arbitrary Objects.
1203 * If you pass a non-NULL non-device @owner then we will assert.
1205 * @mr: the #MemoryRegion to be initialized.
1206 * @owner: the object that tracks the region's reference count
1207 * @name: Region name, becomes part of RAMBlock name used in migration stream
1208 * must be unique within any device
1209 * @size: size of the region.
1210 * @errp: pointer to Error*, to store an error if it happens.
1212 void memory_region_init_rom(MemoryRegion *mr,
1213 struct Object *owner,
1214 const char *name,
1215 uint64_t size,
1216 Error **errp);
1219 * memory_region_init_rom_device: Initialize a ROM memory region.
1220 * Writes are handled via callbacks.
1222 * This function initializes a memory region backed by RAM for reads
1223 * and callbacks for writes, and arranges for the RAM backing to
1224 * be migrated (by calling vmstate_register_ram()
1225 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1226 * @owner is NULL).
1228 * TODO: Currently we restrict @owner to being either NULL (for
1229 * global RAM regions with no owner) or devices, so that we can
1230 * give the RAM block a unique name for migration purposes.
1231 * We should lift this restriction and allow arbitrary Objects.
1232 * If you pass a non-NULL non-device @owner then we will assert.
1234 * @mr: the #MemoryRegion to be initialized.
1235 * @owner: the object that tracks the region's reference count
1236 * @ops: callbacks for write access handling (must not be NULL).
1237 * @opaque: passed to the read and write callbacks of the @ops structure.
1238 * @name: Region name, becomes part of RAMBlock name used in migration stream
1239 * must be unique within any device
1240 * @size: size of the region.
1241 * @errp: pointer to Error*, to store an error if it happens.
1243 void memory_region_init_rom_device(MemoryRegion *mr,
1244 struct Object *owner,
1245 const MemoryRegionOps *ops,
1246 void *opaque,
1247 const char *name,
1248 uint64_t size,
1249 Error **errp);
1253 * memory_region_owner: get a memory region's owner.
1255 * @mr: the memory region being queried.
1257 struct Object *memory_region_owner(MemoryRegion *mr);
1260 * memory_region_size: get a memory region's size.
1262 * @mr: the memory region being queried.
1264 uint64_t memory_region_size(MemoryRegion *mr);
1267 * memory_region_is_ram: check whether a memory region is random access
1269 * Returns %true if a memory region is random access.
1271 * @mr: the memory region being queried
1273 static inline bool memory_region_is_ram(MemoryRegion *mr)
1275 return mr->ram;
1279 * memory_region_is_ram_device: check whether a memory region is a ram device
1281 * Returns %true if a memory region is a device backed ram region
1283 * @mr: the memory region being queried
1285 bool memory_region_is_ram_device(MemoryRegion *mr);
1288 * memory_region_is_romd: check whether a memory region is in ROMD mode
1290 * Returns %true if a memory region is a ROM device and currently set to allow
1291 * direct reads.
1293 * @mr: the memory region being queried
1295 static inline bool memory_region_is_romd(MemoryRegion *mr)
1297 return mr->rom_device && mr->romd_mode;
1301 * memory_region_get_iommu: check whether a memory region is an iommu
1303 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
1304 * otherwise NULL.
1306 * @mr: the memory region being queried
1308 static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
1310 if (mr->alias) {
1311 return memory_region_get_iommu(mr->alias);
1313 if (mr->is_iommu) {
1314 return (IOMMUMemoryRegion *) mr;
1316 return NULL;
1320 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
1321 * if an iommu or NULL if not
1323 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
1324 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1326 * @iommu_mr: the memory region being queried
1328 static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck(
1329 IOMMUMemoryRegion *iommu_mr)
1331 return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class);
1334 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1337 * memory_region_iommu_get_min_page_size: get minimum supported page size
1338 * for an iommu
1340 * Returns minimum supported page size for an iommu.
1342 * @iommu_mr: the memory region being queried
1344 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
1347 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1349 * Note: for any IOMMU implementation, an in-place mapping change
1350 * should be notified with an UNMAP followed by a MAP.
1352 * @iommu_mr: the memory region that was changed
1353 * @iommu_idx: the IOMMU index for the translation table which has changed
1354 * @event: TLB event with the new entry in the IOMMU translation table.
1355 * The entry replaces all old entries for the same virtual I/O address
1356 * range.
1358 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1359 int iommu_idx,
1360 IOMMUTLBEvent event);
1363 * memory_region_notify_iommu_one: notify a change in an IOMMU translation
1364 * entry to a single notifier
1366 * This works just like memory_region_notify_iommu(), but it only
1367 * notifies a specific notifier, not all of them.
1369 * @notifier: the notifier to be notified
1370 * @event: TLB event with the new entry in the IOMMU translation table.
1371 * The entry replaces all old entries for the same virtual I/O address
1372 * range.
1374 void memory_region_notify_iommu_one(IOMMUNotifier *notifier,
1375 IOMMUTLBEvent *event);
1378 * memory_region_register_iommu_notifier: register a notifier for changes to
1379 * IOMMU translation entries.
1381 * Returns 0 on success, or a negative errno otherwise. In particular,
1382 * -EINVAL indicates that at least one of the attributes of the notifier
1383 * is not supported (flag/range) by the IOMMU memory region. In case of error
1384 * the error object must be created.
1386 * @mr: the memory region to observe
1387 * @n: the IOMMUNotifier to be added; the notify callback receives a
1388 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1389 * ceases to be valid on exit from the notifier.
1390 * @errp: pointer to Error*, to store an error if it happens.
1392 int memory_region_register_iommu_notifier(MemoryRegion *mr,
1393 IOMMUNotifier *n, Error **errp);
1396 * memory_region_iommu_replay: replay existing IOMMU translations to
1397 * a notifier with the minimum page granularity returned by
1398 * mr->iommu_ops->get_page_size().
1400 * Note: this is not related to record-and-replay functionality.
1402 * @iommu_mr: the memory region to observe
1403 * @n: the notifier to which to replay iommu mappings
1405 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
1408 * memory_region_unregister_iommu_notifier: unregister a notifier for
1409 * changes to IOMMU translation entries.
1411 * @mr: the memory region which was observed and for which notity_stopped()
1412 * needs to be called
1413 * @n: the notifier to be removed.
1415 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1416 IOMMUNotifier *n);
1419 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1420 * defined on the IOMMU.
1422 * Returns 0 on success, or a negative errno otherwise. In particular,
1423 * -EINVAL indicates that the IOMMU does not support the requested
1424 * attribute.
1426 * @iommu_mr: the memory region
1427 * @attr: the requested attribute
1428 * @data: a pointer to the requested attribute data
1430 int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1431 enum IOMMUMemoryRegionAttr attr,
1432 void *data);
1435 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1436 * use for translations with the given memory transaction attributes.
1438 * @iommu_mr: the memory region
1439 * @attrs: the memory transaction attributes
1441 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr,
1442 MemTxAttrs attrs);
1445 * memory_region_iommu_num_indexes: return the total number of IOMMU
1446 * indexes that this IOMMU supports.
1448 * @iommu_mr: the memory region
1450 int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr);
1453 * memory_region_iommu_set_page_size_mask: set the supported page
1454 * sizes for a given IOMMU memory region
1456 * @iommu_mr: IOMMU memory region
1457 * @page_size_mask: supported page size mask
1458 * @errp: pointer to Error*, to store an error if it happens.
1460 int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion *iommu_mr,
1461 uint64_t page_size_mask,
1462 Error **errp);
1465 * memory_region_name: get a memory region's name
1467 * Returns the string that was used to initialize the memory region.
1469 * @mr: the memory region being queried
1471 const char *memory_region_name(const MemoryRegion *mr);
1474 * memory_region_is_logging: return whether a memory region is logging writes
1476 * Returns %true if the memory region is logging writes for the given client
1478 * @mr: the memory region being queried
1479 * @client: the client being queried
1481 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
1484 * memory_region_get_dirty_log_mask: return the clients for which a
1485 * memory region is logging writes.
1487 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1488 * are the bit indices.
1490 * @mr: the memory region being queried
1492 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
1495 * memory_region_is_rom: check whether a memory region is ROM
1497 * Returns %true if a memory region is read-only memory.
1499 * @mr: the memory region being queried
1501 static inline bool memory_region_is_rom(MemoryRegion *mr)
1503 return mr->ram && mr->readonly;
1507 * memory_region_is_nonvolatile: check whether a memory region is non-volatile
1509 * Returns %true is a memory region is non-volatile memory.
1511 * @mr: the memory region being queried
1513 static inline bool memory_region_is_nonvolatile(MemoryRegion *mr)
1515 return mr->nonvolatile;
1519 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1521 * Returns a file descriptor backing a file-based RAM memory region,
1522 * or -1 if the region is not a file-based RAM memory region.
1524 * @mr: the RAM or alias memory region being queried.
1526 int memory_region_get_fd(MemoryRegion *mr);
1529 * memory_region_from_host: Convert a pointer into a RAM memory region
1530 * and an offset within it.
1532 * Given a host pointer inside a RAM memory region (created with
1533 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1534 * the MemoryRegion and the offset within it.
1536 * Use with care; by the time this function returns, the returned pointer is
1537 * not protected by RCU anymore. If the caller is not within an RCU critical
1538 * section and does not hold the iothread lock, it must have other means of
1539 * protecting the pointer, such as a reference to the region that includes
1540 * the incoming ram_addr_t.
1542 * @ptr: the host pointer to be converted
1543 * @offset: the offset within memory region
1545 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
1548 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1550 * Returns a host pointer to a RAM memory region (created with
1551 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1553 * Use with care; by the time this function returns, the returned pointer is
1554 * not protected by RCU anymore. If the caller is not within an RCU critical
1555 * section and does not hold the iothread lock, it must have other means of
1556 * protecting the pointer, such as a reference to the region that includes
1557 * the incoming ram_addr_t.
1559 * @mr: the memory region being queried.
1561 void *memory_region_get_ram_ptr(MemoryRegion *mr);
1563 /* memory_region_ram_resize: Resize a RAM region.
1565 * Only legal before guest might have detected the memory size: e.g. on
1566 * incoming migration, or right after reset.
1568 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1569 * @newsize: the new size the region
1570 * @errp: pointer to Error*, to store an error if it happens.
1572 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
1573 Error **errp);
1576 * memory_region_msync: Synchronize selected address range of
1577 * a memory mapped region
1579 * @mr: the memory region to be msync
1580 * @addr: the initial address of the range to be sync
1581 * @size: the size of the range to be sync
1583 void memory_region_msync(MemoryRegion *mr, hwaddr addr, hwaddr size);
1586 * memory_region_writeback: Trigger cache writeback for
1587 * selected address range
1589 * @mr: the memory region to be updated
1590 * @addr: the initial address of the range to be written back
1591 * @size: the size of the range to be written back
1593 void memory_region_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size);
1596 * memory_region_set_log: Turn dirty logging on or off for a region.
1598 * Turns dirty logging on or off for a specified client (display, migration).
1599 * Only meaningful for RAM regions.
1601 * @mr: the memory region being updated.
1602 * @log: whether dirty logging is to be enabled or disabled.
1603 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1605 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
1608 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1610 * Marks a range of bytes as dirty, after it has been dirtied outside
1611 * guest code.
1613 * @mr: the memory region being dirtied.
1614 * @addr: the address (relative to the start of the region) being dirtied.
1615 * @size: size of the range being dirtied.
1617 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1618 hwaddr size);
1621 * memory_region_clear_dirty_bitmap - clear dirty bitmap for memory range
1623 * This function is called when the caller wants to clear the remote
1624 * dirty bitmap of a memory range within the memory region. This can
1625 * be used by e.g. KVM to manually clear dirty log when
1626 * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT is declared support by the host
1627 * kernel.
1629 * @mr: the memory region to clear the dirty log upon
1630 * @start: start address offset within the memory region
1631 * @len: length of the memory region to clear dirty bitmap
1633 void memory_region_clear_dirty_bitmap(MemoryRegion *mr, hwaddr start,
1634 hwaddr len);
1637 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1638 * bitmap and clear it.
1640 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1641 * returns the snapshot. The snapshot can then be used to query dirty
1642 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
1643 * querying the same page multiple times, which is especially useful for
1644 * display updates where the scanlines often are not page aligned.
1646 * The dirty bitmap region which gets copyed into the snapshot (and
1647 * cleared afterwards) can be larger than requested. The boundaries
1648 * are rounded up/down so complete bitmap longs (covering 64 pages on
1649 * 64bit hosts) can be copied over into the bitmap snapshot. Which
1650 * isn't a problem for display updates as the extra pages are outside
1651 * the visible area, and in case the visible area changes a full
1652 * display redraw is due anyway. Should other use cases for this
1653 * function emerge we might have to revisit this implementation
1654 * detail.
1656 * Use g_free to release DirtyBitmapSnapshot.
1658 * @mr: the memory region being queried.
1659 * @addr: the address (relative to the start of the region) being queried.
1660 * @size: the size of the range being queried.
1661 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1663 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
1664 hwaddr addr,
1665 hwaddr size,
1666 unsigned client);
1669 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1670 * in the specified dirty bitmap snapshot.
1672 * @mr: the memory region being queried.
1673 * @snap: the dirty bitmap snapshot
1674 * @addr: the address (relative to the start of the region) being queried.
1675 * @size: the size of the range being queried.
1677 bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
1678 DirtyBitmapSnapshot *snap,
1679 hwaddr addr, hwaddr size);
1682 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1683 * client.
1685 * Marks a range of pages as no longer dirty.
1687 * @mr: the region being updated.
1688 * @addr: the start of the subrange being cleaned.
1689 * @size: the size of the subrange being cleaned.
1690 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1691 * %DIRTY_MEMORY_VGA.
1693 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1694 hwaddr size, unsigned client);
1697 * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate
1698 * TBs (for self-modifying code).
1700 * The MemoryRegionOps->write() callback of a ROM device must use this function
1701 * to mark byte ranges that have been modified internally, such as by directly
1702 * accessing the memory returned by memory_region_get_ram_ptr().
1704 * This function marks the range dirty and invalidates TBs so that TCG can
1705 * detect self-modifying code.
1707 * @mr: the region being flushed.
1708 * @addr: the start, relative to the start of the region, of the range being
1709 * flushed.
1710 * @size: the size, in bytes, of the range being flushed.
1712 void memory_region_flush_rom_device(MemoryRegion *mr, hwaddr addr, hwaddr size);
1715 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1717 * Allows a memory region to be marked as read-only (turning it into a ROM).
1718 * only useful on RAM regions.
1720 * @mr: the region being updated.
1721 * @readonly: whether rhe region is to be ROM or RAM.
1723 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
1726 * memory_region_set_nonvolatile: Turn a memory region non-volatile
1728 * Allows a memory region to be marked as non-volatile.
1729 * only useful on RAM regions.
1731 * @mr: the region being updated.
1732 * @nonvolatile: whether rhe region is to be non-volatile.
1734 void memory_region_set_nonvolatile(MemoryRegion *mr, bool nonvolatile);
1737 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1739 * Allows a ROM device (initialized with memory_region_init_rom_device() to
1740 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
1741 * device is mapped to guest memory and satisfies read access directly.
1742 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1743 * Writes are always handled by the #MemoryRegion.write function.
1745 * @mr: the memory region to be updated
1746 * @romd_mode: %true to put the region into ROMD mode
1748 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
1751 * memory_region_set_coalescing: Enable memory coalescing for the region.
1753 * Enabled writes to a region to be queued for later processing. MMIO ->write
1754 * callbacks may be delayed until a non-coalesced MMIO is issued.
1755 * Only useful for IO regions. Roughly similar to write-combining hardware.
1757 * @mr: the memory region to be write coalesced
1759 void memory_region_set_coalescing(MemoryRegion *mr);
1762 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1763 * a region.
1765 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1766 * Multiple calls can be issued coalesced disjoint ranges.
1768 * @mr: the memory region to be updated.
1769 * @offset: the start of the range within the region to be coalesced.
1770 * @size: the size of the subrange to be coalesced.
1772 void memory_region_add_coalescing(MemoryRegion *mr,
1773 hwaddr offset,
1774 uint64_t size);
1777 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1779 * Disables any coalescing caused by memory_region_set_coalescing() or
1780 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
1781 * hardware.
1783 * @mr: the memory region to be updated.
1785 void memory_region_clear_coalescing(MemoryRegion *mr);
1788 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1789 * accesses.
1791 * Ensure that pending coalesced MMIO request are flushed before the memory
1792 * region is accessed. This property is automatically enabled for all regions
1793 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1795 * @mr: the memory region to be updated.
1797 void memory_region_set_flush_coalesced(MemoryRegion *mr);
1800 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1801 * accesses.
1803 * Clear the automatic coalesced MMIO flushing enabled via
1804 * memory_region_set_flush_coalesced. Note that this service has no effect on
1805 * memory regions that have MMIO coalescing enabled for themselves. For them,
1806 * automatic flushing will stop once coalescing is disabled.
1808 * @mr: the memory region to be updated.
1810 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
1813 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1814 * is written to a location.
1816 * Marks a word in an IO region (initialized with memory_region_init_io())
1817 * as a trigger for an eventfd event. The I/O callback will not be called.
1818 * The caller must be prepared to handle failure (that is, take the required
1819 * action if the callback _is_ called).
1821 * @mr: the memory region being updated.
1822 * @addr: the address within @mr that is to be monitored
1823 * @size: the size of the access to trigger the eventfd
1824 * @match_data: whether to match against @data, instead of just @addr
1825 * @data: the data to match against the guest write
1826 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1828 void memory_region_add_eventfd(MemoryRegion *mr,
1829 hwaddr addr,
1830 unsigned size,
1831 bool match_data,
1832 uint64_t data,
1833 EventNotifier *e);
1836 * memory_region_del_eventfd: Cancel an eventfd.
1838 * Cancels an eventfd trigger requested by a previous
1839 * memory_region_add_eventfd() call.
1841 * @mr: the memory region being updated.
1842 * @addr: the address within @mr that is to be monitored
1843 * @size: the size of the access to trigger the eventfd
1844 * @match_data: whether to match against @data, instead of just @addr
1845 * @data: the data to match against the guest write
1846 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1848 void memory_region_del_eventfd(MemoryRegion *mr,
1849 hwaddr addr,
1850 unsigned size,
1851 bool match_data,
1852 uint64_t data,
1853 EventNotifier *e);
1856 * memory_region_add_subregion: Add a subregion to a container.
1858 * Adds a subregion at @offset. The subregion may not overlap with other
1859 * subregions (except for those explicitly marked as overlapping). A region
1860 * may only be added once as a subregion (unless removed with
1861 * memory_region_del_subregion()); use memory_region_init_alias() if you
1862 * want a region to be a subregion in multiple locations.
1864 * @mr: the region to contain the new subregion; must be a container
1865 * initialized with memory_region_init().
1866 * @offset: the offset relative to @mr where @subregion is added.
1867 * @subregion: the subregion to be added.
1869 void memory_region_add_subregion(MemoryRegion *mr,
1870 hwaddr offset,
1871 MemoryRegion *subregion);
1873 * memory_region_add_subregion_overlap: Add a subregion to a container
1874 * with overlap.
1876 * Adds a subregion at @offset. The subregion may overlap with other
1877 * subregions. Conflicts are resolved by having a higher @priority hide a
1878 * lower @priority. Subregions without priority are taken as @priority 0.
1879 * A region may only be added once as a subregion (unless removed with
1880 * memory_region_del_subregion()); use memory_region_init_alias() if you
1881 * want a region to be a subregion in multiple locations.
1883 * @mr: the region to contain the new subregion; must be a container
1884 * initialized with memory_region_init().
1885 * @offset: the offset relative to @mr where @subregion is added.
1886 * @subregion: the subregion to be added.
1887 * @priority: used for resolving overlaps; highest priority wins.
1889 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1890 hwaddr offset,
1891 MemoryRegion *subregion,
1892 int priority);
1895 * memory_region_get_ram_addr: Get the ram address associated with a memory
1896 * region
1898 * @mr: the region to be queried
1900 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
1902 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
1904 * memory_region_del_subregion: Remove a subregion.
1906 * Removes a subregion from its container.
1908 * @mr: the container to be updated.
1909 * @subregion: the region being removed; must be a current subregion of @mr.
1911 void memory_region_del_subregion(MemoryRegion *mr,
1912 MemoryRegion *subregion);
1915 * memory_region_set_enabled: dynamically enable or disable a region
1917 * Enables or disables a memory region. A disabled memory region
1918 * ignores all accesses to itself and its subregions. It does not
1919 * obscure sibling subregions with lower priority - it simply behaves as
1920 * if it was removed from the hierarchy.
1922 * Regions default to being enabled.
1924 * @mr: the region to be updated
1925 * @enabled: whether to enable or disable the region
1927 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1930 * memory_region_set_address: dynamically update the address of a region
1932 * Dynamically updates the address of a region, relative to its container.
1933 * May be used on regions are currently part of a memory hierarchy.
1935 * @mr: the region to be updated
1936 * @addr: new address, relative to container region
1938 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
1941 * memory_region_set_size: dynamically update the size of a region.
1943 * Dynamically updates the size of a region.
1945 * @mr: the region to be updated
1946 * @size: used size of the region.
1948 void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1951 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1953 * Dynamically updates the offset into the target region that an alias points
1954 * to, as if the fourth argument to memory_region_init_alias() has changed.
1956 * @mr: the #MemoryRegion to be updated; should be an alias.
1957 * @offset: the new offset into the target memory region
1959 void memory_region_set_alias_offset(MemoryRegion *mr,
1960 hwaddr offset);
1963 * memory_region_present: checks if an address relative to a @container
1964 * translates into #MemoryRegion within @container
1966 * Answer whether a #MemoryRegion within @container covers the address
1967 * @addr.
1969 * @container: a #MemoryRegion within which @addr is a relative address
1970 * @addr: the area within @container to be searched
1972 bool memory_region_present(MemoryRegion *container, hwaddr addr);
1975 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1976 * into any address space.
1978 * @mr: a #MemoryRegion which should be checked if it's mapped
1980 bool memory_region_is_mapped(MemoryRegion *mr);
1983 * memory_region_find: translate an address/size relative to a
1984 * MemoryRegion into a #MemoryRegionSection.
1986 * Locates the first #MemoryRegion within @mr that overlaps the range
1987 * given by @addr and @size.
1989 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1990 * It will have the following characteristics:
1991 * - @size = 0 iff no overlap was found
1992 * - @mr is non-%NULL iff an overlap was found
1994 * Remember that in the return value the @offset_within_region is
1995 * relative to the returned region (in the .@mr field), not to the
1996 * @mr argument.
1998 * Similarly, the .@offset_within_address_space is relative to the
1999 * address space that contains both regions, the passed and the
2000 * returned one. However, in the special case where the @mr argument
2001 * has no container (and thus is the root of the address space), the
2002 * following will hold:
2003 * - @offset_within_address_space >= @addr
2004 * - @offset_within_address_space + .@size <= @addr + @size
2006 * @mr: a MemoryRegion within which @addr is a relative address
2007 * @addr: start of the area within @as to be searched
2008 * @size: size of the area to be searched
2010 MemoryRegionSection memory_region_find(MemoryRegion *mr,
2011 hwaddr addr, uint64_t size);
2014 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
2016 * Synchronizes the dirty page log for all address spaces.
2018 void memory_global_dirty_log_sync(void);
2021 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
2023 * Synchronizes the vCPUs with a thread that is reading the dirty bitmap.
2024 * This function must be called after the dirty log bitmap is cleared, and
2025 * before dirty guest memory pages are read. If you are using
2026 * #DirtyBitmapSnapshot, memory_region_snapshot_and_clear_dirty() takes
2027 * care of doing this.
2029 void memory_global_after_dirty_log_sync(void);
2032 * memory_region_transaction_begin: Start a transaction.
2034 * During a transaction, changes will be accumulated and made visible
2035 * only when the transaction ends (is committed).
2037 void memory_region_transaction_begin(void);
2040 * memory_region_transaction_commit: Commit a transaction and make changes
2041 * visible to the guest.
2043 void memory_region_transaction_commit(void);
2046 * memory_listener_register: register callbacks to be called when memory
2047 * sections are mapped or unmapped into an address
2048 * space
2050 * @listener: an object containing the callbacks to be called
2051 * @filter: if non-%NULL, only regions in this address space will be observed
2053 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
2056 * memory_listener_unregister: undo the effect of memory_listener_register()
2058 * @listener: an object containing the callbacks to be removed
2060 void memory_listener_unregister(MemoryListener *listener);
2063 * memory_global_dirty_log_start: begin dirty logging for all regions
2065 void memory_global_dirty_log_start(void);
2068 * memory_global_dirty_log_stop: end dirty logging for all regions
2070 void memory_global_dirty_log_stop(void);
2072 void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled);
2075 * memory_region_dispatch_read: perform a read directly to the specified
2076 * MemoryRegion.
2078 * @mr: #MemoryRegion to access
2079 * @addr: address within that region
2080 * @pval: pointer to uint64_t which the data is written to
2081 * @op: size, sign, and endianness of the memory operation
2082 * @attrs: memory transaction attributes to use for the access
2084 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
2085 hwaddr addr,
2086 uint64_t *pval,
2087 MemOp op,
2088 MemTxAttrs attrs);
2090 * memory_region_dispatch_write: perform a write directly to the specified
2091 * MemoryRegion.
2093 * @mr: #MemoryRegion to access
2094 * @addr: address within that region
2095 * @data: data to write
2096 * @op: size, sign, and endianness of the memory operation
2097 * @attrs: memory transaction attributes to use for the access
2099 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
2100 hwaddr addr,
2101 uint64_t data,
2102 MemOp op,
2103 MemTxAttrs attrs);
2106 * address_space_init: initializes an address space
2108 * @as: an uninitialized #AddressSpace
2109 * @root: a #MemoryRegion that routes addresses for the address space
2110 * @name: an address space name. The name is only used for debugging
2111 * output.
2113 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
2116 * address_space_destroy: destroy an address space
2118 * Releases all resources associated with an address space. After an address space
2119 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
2120 * as well.
2122 * @as: address space to be destroyed
2124 void address_space_destroy(AddressSpace *as);
2127 * address_space_remove_listeners: unregister all listeners of an address space
2129 * Removes all callbacks previously registered with memory_listener_register()
2130 * for @as.
2132 * @as: an initialized #AddressSpace
2134 void address_space_remove_listeners(AddressSpace *as);
2137 * address_space_rw: read from or write to an address space.
2139 * Return a MemTxResult indicating whether the operation succeeded
2140 * or failed (eg unassigned memory, device rejected the transaction,
2141 * IOMMU fault).
2143 * @as: #AddressSpace to be accessed
2144 * @addr: address within that address space
2145 * @attrs: memory transaction attributes
2146 * @buf: buffer with the data transferred
2147 * @len: the number of bytes to read or write
2148 * @is_write: indicates the transfer direction
2150 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
2151 MemTxAttrs attrs, void *buf,
2152 hwaddr len, bool is_write);
2155 * address_space_write: write to address space.
2157 * Return a MemTxResult indicating whether the operation succeeded
2158 * or failed (eg unassigned memory, device rejected the transaction,
2159 * IOMMU fault).
2161 * @as: #AddressSpace to be accessed
2162 * @addr: address within that address space
2163 * @attrs: memory transaction attributes
2164 * @buf: buffer with the data transferred
2165 * @len: the number of bytes to write
2167 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
2168 MemTxAttrs attrs,
2169 const void *buf, hwaddr len);
2172 * address_space_write_rom: write to address space, including ROM.
2174 * This function writes to the specified address space, but will
2175 * write data to both ROM and RAM. This is used for non-guest
2176 * writes like writes from the gdb debug stub or initial loading
2177 * of ROM contents.
2179 * Note that portions of the write which attempt to write data to
2180 * a device will be silently ignored -- only real RAM and ROM will
2181 * be written to.
2183 * Return a MemTxResult indicating whether the operation succeeded
2184 * or failed (eg unassigned memory, device rejected the transaction,
2185 * IOMMU fault).
2187 * @as: #AddressSpace to be accessed
2188 * @addr: address within that address space
2189 * @attrs: memory transaction attributes
2190 * @buf: buffer with the data transferred
2191 * @len: the number of bytes to write
2193 MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
2194 MemTxAttrs attrs,
2195 const void *buf, hwaddr len);
2197 /* address_space_ld*: load from an address space
2198 * address_space_st*: store to an address space
2200 * These functions perform a load or store of the byte, word,
2201 * longword or quad to the specified address within the AddressSpace.
2202 * The _le suffixed functions treat the data as little endian;
2203 * _be indicates big endian; no suffix indicates "same endianness
2204 * as guest CPU".
2206 * The "guest CPU endianness" accessors are deprecated for use outside
2207 * target-* code; devices should be CPU-agnostic and use either the LE
2208 * or the BE accessors.
2210 * @as #AddressSpace to be accessed
2211 * @addr: address within that address space
2212 * @val: data value, for stores
2213 * @attrs: memory transaction attributes
2214 * @result: location to write the success/failure of the transaction;
2215 * if NULL, this information is discarded
2218 #define SUFFIX
2219 #define ARG1 as
2220 #define ARG1_DECL AddressSpace *as
2221 #include "exec/memory_ldst.h.inc"
2223 #define SUFFIX
2224 #define ARG1 as
2225 #define ARG1_DECL AddressSpace *as
2226 #include "exec/memory_ldst_phys.h.inc"
2228 struct MemoryRegionCache {
2229 void *ptr;
2230 hwaddr xlat;
2231 hwaddr len;
2232 FlatView *fv;
2233 MemoryRegionSection mrs;
2234 bool is_write;
2237 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
2240 /* address_space_ld*_cached: load from a cached #MemoryRegion
2241 * address_space_st*_cached: store into a cached #MemoryRegion
2243 * These functions perform a load or store of the byte, word,
2244 * longword or quad to the specified address. The address is
2245 * a physical address in the AddressSpace, but it must lie within
2246 * a #MemoryRegion that was mapped with address_space_cache_init.
2248 * The _le suffixed functions treat the data as little endian;
2249 * _be indicates big endian; no suffix indicates "same endianness
2250 * as guest CPU".
2252 * The "guest CPU endianness" accessors are deprecated for use outside
2253 * target-* code; devices should be CPU-agnostic and use either the LE
2254 * or the BE accessors.
2256 * @cache: previously initialized #MemoryRegionCache to be accessed
2257 * @addr: address within the address space
2258 * @val: data value, for stores
2259 * @attrs: memory transaction attributes
2260 * @result: location to write the success/failure of the transaction;
2261 * if NULL, this information is discarded
2264 #define SUFFIX _cached_slow
2265 #define ARG1 cache
2266 #define ARG1_DECL MemoryRegionCache *cache
2267 #include "exec/memory_ldst.h.inc"
2269 /* Inline fast path for direct RAM access. */
2270 static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache,
2271 hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
2273 assert(addr < cache->len);
2274 if (likely(cache->ptr)) {
2275 return ldub_p(cache->ptr + addr);
2276 } else {
2277 return address_space_ldub_cached_slow(cache, addr, attrs, result);
2281 static inline void address_space_stb_cached(MemoryRegionCache *cache,
2282 hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result)
2284 assert(addr < cache->len);
2285 if (likely(cache->ptr)) {
2286 stb_p(cache->ptr + addr, val);
2287 } else {
2288 address_space_stb_cached_slow(cache, addr, val, attrs, result);
2292 #define ENDIANNESS _le
2293 #include "exec/memory_ldst_cached.h.inc"
2295 #define ENDIANNESS _be
2296 #include "exec/memory_ldst_cached.h.inc"
2298 #define SUFFIX _cached
2299 #define ARG1 cache
2300 #define ARG1_DECL MemoryRegionCache *cache
2301 #include "exec/memory_ldst_phys.h.inc"
2303 /* address_space_cache_init: prepare for repeated access to a physical
2304 * memory region
2306 * @cache: #MemoryRegionCache to be filled
2307 * @as: #AddressSpace to be accessed
2308 * @addr: address within that address space
2309 * @len: length of buffer
2310 * @is_write: indicates the transfer direction
2312 * Will only work with RAM, and may map a subset of the requested range by
2313 * returning a value that is less than @len. On failure, return a negative
2314 * errno value.
2316 * Because it only works with RAM, this function can be used for
2317 * read-modify-write operations. In this case, is_write should be %true.
2319 * Note that addresses passed to the address_space_*_cached functions
2320 * are relative to @addr.
2322 int64_t address_space_cache_init(MemoryRegionCache *cache,
2323 AddressSpace *as,
2324 hwaddr addr,
2325 hwaddr len,
2326 bool is_write);
2329 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
2331 * @cache: The #MemoryRegionCache to operate on.
2332 * @addr: The first physical address that was written, relative to the
2333 * address that was passed to @address_space_cache_init.
2334 * @access_len: The number of bytes that were written starting at @addr.
2336 void address_space_cache_invalidate(MemoryRegionCache *cache,
2337 hwaddr addr,
2338 hwaddr access_len);
2341 * address_space_cache_destroy: free a #MemoryRegionCache
2343 * @cache: The #MemoryRegionCache whose memory should be released.
2345 void address_space_cache_destroy(MemoryRegionCache *cache);
2347 /* address_space_get_iotlb_entry: translate an address into an IOTLB
2348 * entry. Should be called from an RCU critical section.
2350 IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
2351 bool is_write, MemTxAttrs attrs);
2353 /* address_space_translate: translate an address range into an address space
2354 * into a MemoryRegion and an address range into that section. Should be
2355 * called from an RCU critical section, to avoid that the last reference
2356 * to the returned region disappears after address_space_translate returns.
2358 * @fv: #FlatView to be accessed
2359 * @addr: address within that address space
2360 * @xlat: pointer to address within the returned memory region section's
2361 * #MemoryRegion.
2362 * @len: pointer to length
2363 * @is_write: indicates the transfer direction
2364 * @attrs: memory attributes
2366 MemoryRegion *flatview_translate(FlatView *fv,
2367 hwaddr addr, hwaddr *xlat,
2368 hwaddr *len, bool is_write,
2369 MemTxAttrs attrs);
2371 static inline MemoryRegion *address_space_translate(AddressSpace *as,
2372 hwaddr addr, hwaddr *xlat,
2373 hwaddr *len, bool is_write,
2374 MemTxAttrs attrs)
2376 return flatview_translate(address_space_to_flatview(as),
2377 addr, xlat, len, is_write, attrs);
2380 /* address_space_access_valid: check for validity of accessing an address
2381 * space range
2383 * Check whether memory is assigned to the given address space range, and
2384 * access is permitted by any IOMMU regions that are active for the address
2385 * space.
2387 * For now, addr and len should be aligned to a page size. This limitation
2388 * will be lifted in the future.
2390 * @as: #AddressSpace to be accessed
2391 * @addr: address within that address space
2392 * @len: length of the area to be checked
2393 * @is_write: indicates the transfer direction
2394 * @attrs: memory attributes
2396 bool address_space_access_valid(AddressSpace *as, hwaddr addr, hwaddr len,
2397 bool is_write, MemTxAttrs attrs);
2399 /* address_space_map: map a physical memory region into a host virtual address
2401 * May map a subset of the requested range, given by and returned in @plen.
2402 * May return %NULL and set *@plen to zero(0), if resources needed to perform
2403 * the mapping are exhausted.
2404 * Use only for reads OR writes - not for read-modify-write operations.
2405 * Use cpu_register_map_client() to know when retrying the map operation is
2406 * likely to succeed.
2408 * @as: #AddressSpace to be accessed
2409 * @addr: address within that address space
2410 * @plen: pointer to length of buffer; updated on return
2411 * @is_write: indicates the transfer direction
2412 * @attrs: memory attributes
2414 void *address_space_map(AddressSpace *as, hwaddr addr,
2415 hwaddr *plen, bool is_write, MemTxAttrs attrs);
2417 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2419 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2420 * the amount of memory that was actually read or written by the caller.
2422 * @as: #AddressSpace used
2423 * @buffer: host pointer as returned by address_space_map()
2424 * @len: buffer length as returned by address_space_map()
2425 * @access_len: amount of data actually transferred
2426 * @is_write: indicates the transfer direction
2428 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2429 bool is_write, hwaddr access_len);
2432 /* Internal functions, part of the implementation of address_space_read. */
2433 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
2434 MemTxAttrs attrs, void *buf, hwaddr len);
2435 MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
2436 MemTxAttrs attrs, void *buf,
2437 hwaddr len, hwaddr addr1, hwaddr l,
2438 MemoryRegion *mr);
2439 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
2441 /* Internal functions, part of the implementation of address_space_read_cached
2442 * and address_space_write_cached. */
2443 MemTxResult address_space_read_cached_slow(MemoryRegionCache *cache,
2444 hwaddr addr, void *buf, hwaddr len);
2445 MemTxResult address_space_write_cached_slow(MemoryRegionCache *cache,
2446 hwaddr addr, const void *buf,
2447 hwaddr len);
2449 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
2451 if (is_write) {
2452 return memory_region_is_ram(mr) && !mr->readonly &&
2453 !mr->rom_device && !memory_region_is_ram_device(mr);
2454 } else {
2455 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
2456 memory_region_is_romd(mr);
2461 * address_space_read: read from an address space.
2463 * Return a MemTxResult indicating whether the operation succeeded
2464 * or failed (eg unassigned memory, device rejected the transaction,
2465 * IOMMU fault). Called within RCU critical section.
2467 * @as: #AddressSpace to be accessed
2468 * @addr: address within that address space
2469 * @attrs: memory transaction attributes
2470 * @buf: buffer with the data transferred
2471 * @len: length of the data transferred
2473 static inline __attribute__((__always_inline__))
2474 MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
2475 MemTxAttrs attrs, void *buf,
2476 hwaddr len)
2478 MemTxResult result = MEMTX_OK;
2479 hwaddr l, addr1;
2480 void *ptr;
2481 MemoryRegion *mr;
2482 FlatView *fv;
2484 if (__builtin_constant_p(len)) {
2485 if (len) {
2486 RCU_READ_LOCK_GUARD();
2487 fv = address_space_to_flatview(as);
2488 l = len;
2489 mr = flatview_translate(fv, addr, &addr1, &l, false, attrs);
2490 if (len == l && memory_access_is_direct(mr, false)) {
2491 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
2492 memcpy(buf, ptr, len);
2493 } else {
2494 result = flatview_read_continue(fv, addr, attrs, buf, len,
2495 addr1, l, mr);
2498 } else {
2499 result = address_space_read_full(as, addr, attrs, buf, len);
2501 return result;
2505 * address_space_read_cached: read from a cached RAM region
2507 * @cache: Cached region to be addressed
2508 * @addr: address relative to the base of the RAM region
2509 * @buf: buffer with the data transferred
2510 * @len: length of the data transferred
2512 static inline MemTxResult
2513 address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
2514 void *buf, hwaddr len)
2516 assert(addr < cache->len && len <= cache->len - addr);
2517 fuzz_dma_read_cb(cache->xlat + addr, len, cache->mrs.mr);
2518 if (likely(cache->ptr)) {
2519 memcpy(buf, cache->ptr + addr, len);
2520 return MEMTX_OK;
2521 } else {
2522 return address_space_read_cached_slow(cache, addr, buf, len);
2527 * address_space_write_cached: write to a cached RAM region
2529 * @cache: Cached region to be addressed
2530 * @addr: address relative to the base of the RAM region
2531 * @buf: buffer with the data transferred
2532 * @len: length of the data transferred
2534 static inline MemTxResult
2535 address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
2536 const void *buf, hwaddr len)
2538 assert(addr < cache->len && len <= cache->len - addr);
2539 if (likely(cache->ptr)) {
2540 memcpy(cache->ptr + addr, buf, len);
2541 return MEMTX_OK;
2542 } else {
2543 return address_space_write_cached_slow(cache, addr, buf, len);
2547 #ifdef NEED_CPU_H
2548 /* enum device_endian to MemOp. */
2549 static inline MemOp devend_memop(enum device_endian end)
2551 QEMU_BUILD_BUG_ON(DEVICE_HOST_ENDIAN != DEVICE_LITTLE_ENDIAN &&
2552 DEVICE_HOST_ENDIAN != DEVICE_BIG_ENDIAN);
2554 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
2555 /* Swap if non-host endianness or native (target) endianness */
2556 return (end == DEVICE_HOST_ENDIAN) ? 0 : MO_BSWAP;
2557 #else
2558 const int non_host_endianness =
2559 DEVICE_LITTLE_ENDIAN ^ DEVICE_BIG_ENDIAN ^ DEVICE_HOST_ENDIAN;
2561 /* In this case, native (target) endianness needs no swap. */
2562 return (end == non_host_endianness) ? MO_BSWAP : 0;
2563 #endif
2565 #endif
2568 * Inhibit technologies that require discarding of pages in RAM blocks, e.g.,
2569 * to manage the actual amount of memory consumed by the VM (then, the memory
2570 * provided by RAM blocks might be bigger than the desired memory consumption).
2571 * This *must* be set if:
2572 * - Discarding parts of a RAM blocks does not result in the change being
2573 * reflected in the VM and the pages getting freed.
2574 * - All memory in RAM blocks is pinned or duplicated, invaldiating any previous
2575 * discards blindly.
2576 * - Discarding parts of a RAM blocks will result in integrity issues (e.g.,
2577 * encrypted VMs).
2578 * Technologies that only temporarily pin the current working set of a
2579 * driver are fine, because we don't expect such pages to be discarded
2580 * (esp. based on guest action like balloon inflation).
2582 * This is *not* to be used to protect from concurrent discards (esp.,
2583 * postcopy).
2585 * Returns 0 if successful. Returns -EBUSY if a technology that relies on
2586 * discards to work reliably is active.
2588 int ram_block_discard_disable(bool state);
2591 * Inhibit technologies that disable discarding of pages in RAM blocks.
2593 * Returns 0 if successful. Returns -EBUSY if discards are already set to
2594 * broken.
2596 int ram_block_discard_require(bool state);
2599 * Test if discarding of memory in ram blocks is disabled.
2601 bool ram_block_discard_is_disabled(void);
2604 * Test if discarding of memory in ram blocks is required to work reliably.
2606 bool ram_block_discard_is_required(void);
2608 #endif
2610 #endif