xref: /qemu/include/exec/memory.h (revision 53ba2eee)
1 /*
2  * Physical memory management API
3  *
4  * Copyright 2011 Red Hat, Inc. and/or its affiliates
5  *
6  * Authors:
7  *  Avi Kivity <avi@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #ifndef MEMORY_H
15 #define MEMORY_H
16 
17 #ifndef CONFIG_USER_ONLY
18 
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"
30 
31 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
32 
33 #define MAX_PHYS_ADDR_SPACE_BITS 62
34 #define MAX_PHYS_ADDR            (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
35 
36 #define TYPE_MEMORY_REGION "qemu:memory-region"
37 DECLARE_INSTANCE_CHECKER(MemoryRegion, MEMORY_REGION,
38                          TYPE_MEMORY_REGION)
39 
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)
44 
45 #ifdef CONFIG_FUZZ
46 void fuzz_dma_read_cb(size_t addr,
47                       size_t len,
48                       MemoryRegion *mr,
49                       bool is_write);
50 #else
51 static inline void fuzz_dma_read_cb(size_t addr,
52                                     size_t len,
53                                     MemoryRegion *mr,
54                                     bool is_write)
55 {
56     /* Do Nothing */
57 }
58 #endif
59 
60 extern bool global_dirty_log;
61 
62 typedef struct MemoryRegionOps MemoryRegionOps;
63 
64 struct ReservedRegion {
65     hwaddr low;
66     hwaddr high;
67     unsigned type;
68 };
69 
70 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
71 
72 /* See address_space_translate: bit 0 is read, bit 1 is write.  */
73 typedef enum {
74     IOMMU_NONE = 0,
75     IOMMU_RO   = 1,
76     IOMMU_WO   = 2,
77     IOMMU_RW   = 3,
78 } IOMMUAccessFlags;
79 
80 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
81 
82 struct IOMMUTLBEntry {
83     AddressSpace    *target_as;
84     hwaddr           iova;
85     hwaddr           translated_addr;
86     hwaddr           addr_mask;  /* 0xfff = 4k translation */
87     IOMMUAccessFlags perm;
88 };
89 
90 /*
91  * Bitmap for different IOMMUNotifier capabilities. Each notifier can
92  * register with one or multiple IOMMU Notifier capability bit(s).
93  */
94 typedef enum {
95     IOMMU_NOTIFIER_NONE = 0,
96     /* Notify cache invalidations */
97     IOMMU_NOTIFIER_UNMAP = 0x1,
98     /* Notify entry changes (newly created entries) */
99     IOMMU_NOTIFIER_MAP = 0x2,
100 } IOMMUNotifierFlag;
101 
102 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP)
103 
104 struct IOMMUNotifier;
105 typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier,
106                             IOMMUTLBEntry *data);
107 
108 struct IOMMUNotifier {
109     IOMMUNotify notify;
110     IOMMUNotifierFlag notifier_flags;
111     /* Notify for address space range start <= addr <= end */
112     hwaddr start;
113     hwaddr end;
114     int iommu_idx;
115     QLIST_ENTRY(IOMMUNotifier) node;
116 };
117 typedef struct IOMMUNotifier IOMMUNotifier;
118 
119 /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */
120 #define RAM_PREALLOC   (1 << 0)
121 
122 /* RAM is mmap-ed with MAP_SHARED */
123 #define RAM_SHARED     (1 << 1)
124 
125 /* Only a portion of RAM (used_length) is actually used, and migrated.
126  * This used_length size can change across reboots.
127  */
128 #define RAM_RESIZEABLE (1 << 2)
129 
130 /* UFFDIO_ZEROPAGE is available on this RAMBlock to atomically
131  * zero the page and wake waiting processes.
132  * (Set during postcopy)
133  */
134 #define RAM_UF_ZEROPAGE (1 << 3)
135 
136 /* RAM can be migrated */
137 #define RAM_MIGRATABLE (1 << 4)
138 
139 /* RAM is a persistent kind memory */
140 #define RAM_PMEM (1 << 5)
141 
142 static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
143                                        IOMMUNotifierFlag flags,
144                                        hwaddr start, hwaddr end,
145                                        int iommu_idx)
146 {
147     n->notify = fn;
148     n->notifier_flags = flags;
149     n->start = start;
150     n->end = end;
151     n->iommu_idx = iommu_idx;
152 }
153 
154 /*
155  * Memory region callbacks
156  */
157 struct MemoryRegionOps {
158     /* Read from the memory region. @addr is relative to @mr; @size is
159      * in bytes. */
160     uint64_t (*read)(void *opaque,
161                      hwaddr addr,
162                      unsigned size);
163     /* Write to the memory region. @addr is relative to @mr; @size is
164      * in bytes. */
165     void (*write)(void *opaque,
166                   hwaddr addr,
167                   uint64_t data,
168                   unsigned size);
169 
170     MemTxResult (*read_with_attrs)(void *opaque,
171                                    hwaddr addr,
172                                    uint64_t *data,
173                                    unsigned size,
174                                    MemTxAttrs attrs);
175     MemTxResult (*write_with_attrs)(void *opaque,
176                                     hwaddr addr,
177                                     uint64_t data,
178                                     unsigned size,
179                                     MemTxAttrs attrs);
180 
181     enum device_endian endianness;
182     /* Guest-visible constraints: */
183     struct {
184         /* If nonzero, specify bounds on access sizes beyond which a machine
185          * check is thrown.
186          */
187         unsigned min_access_size;
188         unsigned max_access_size;
189         /* If true, unaligned accesses are supported.  Otherwise unaligned
190          * accesses throw machine checks.
191          */
192          bool unaligned;
193         /*
194          * If present, and returns #false, the transaction is not accepted
195          * by the device (and results in machine dependent behaviour such
196          * as a machine check exception).
197          */
198         bool (*accepts)(void *opaque, hwaddr addr,
199                         unsigned size, bool is_write,
200                         MemTxAttrs attrs);
201     } valid;
202     /* Internal implementation constraints: */
203     struct {
204         /* If nonzero, specifies the minimum size implemented.  Smaller sizes
205          * will be rounded upwards and a partial result will be returned.
206          */
207         unsigned min_access_size;
208         /* If nonzero, specifies the maximum size implemented.  Larger sizes
209          * will be done as a series of accesses with smaller sizes.
210          */
211         unsigned max_access_size;
212         /* If true, unaligned accesses are supported.  Otherwise all accesses
213          * are converted to (possibly multiple) naturally aligned accesses.
214          */
215         bool unaligned;
216     } impl;
217 };
218 
219 typedef struct MemoryRegionClass {
220     /* private */
221     ObjectClass parent_class;
222 } MemoryRegionClass;
223 
224 
225 enum IOMMUMemoryRegionAttr {
226     IOMMU_ATTR_SPAPR_TCE_FD
227 };
228 
229 /*
230  * IOMMUMemoryRegionClass:
231  *
232  * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION
233  * and provide an implementation of at least the @translate method here
234  * to handle requests to the memory region. Other methods are optional.
235  *
236  * The IOMMU implementation must use the IOMMU notifier infrastructure
237  * to report whenever mappings are changed, by calling
238  * memory_region_notify_iommu() (or, if necessary, by calling
239  * memory_region_notify_one() for each registered notifier).
240  *
241  * Conceptually an IOMMU provides a mapping from input address
242  * to an output TLB entry. If the IOMMU is aware of memory transaction
243  * attributes and the output TLB entry depends on the transaction
244  * attributes, we represent this using IOMMU indexes. Each index
245  * selects a particular translation table that the IOMMU has:
246  *
247  *   @attrs_to_index returns the IOMMU index for a set of transaction attributes
248  *
249  *   @translate takes an input address and an IOMMU index
250  *
251  * and the mapping returned can only depend on the input address and the
252  * IOMMU index.
253  *
254  * Most IOMMUs don't care about the transaction attributes and support
255  * only a single IOMMU index. A more complex IOMMU might have one index
256  * for secure transactions and one for non-secure transactions.
257  */
258 struct IOMMUMemoryRegionClass {
259     /* private: */
260     MemoryRegionClass parent_class;
261 
262     /* public: */
263     /**
264      * @translate:
265      *
266      * Return a TLB entry that contains a given address.
267      *
268      * The IOMMUAccessFlags indicated via @flag are optional and may
269      * be specified as IOMMU_NONE to indicate that the caller needs
270      * the full translation information for both reads and writes. If
271      * the access flags are specified then the IOMMU implementation
272      * may use this as an optimization, to stop doing a page table
273      * walk as soon as it knows that the requested permissions are not
274      * allowed. If IOMMU_NONE is passed then the IOMMU must do the
275      * full page table walk and report the permissions in the returned
276      * IOMMUTLBEntry. (Note that this implies that an IOMMU may not
277      * return different mappings for reads and writes.)
278      *
279      * The returned information remains valid while the caller is
280      * holding the big QEMU lock or is inside an RCU critical section;
281      * if the caller wishes to cache the mapping beyond that it must
282      * register an IOMMU notifier so it can invalidate its cached
283      * information when the IOMMU mapping changes.
284      *
285      * @iommu: the IOMMUMemoryRegion
286      *
287      * @hwaddr: address to be translated within the memory region
288      *
289      * @flag: requested access permission
290      *
291      * @iommu_idx: IOMMU index for the translation
292      */
293     IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr,
294                                IOMMUAccessFlags flag, int iommu_idx);
295     /**
296      * @get_min_page_size:
297      *
298      * Returns minimum supported page size in bytes.
299      *
300      * If this method is not provided then the minimum is assumed to
301      * be TARGET_PAGE_SIZE.
302      *
303      * @iommu: the IOMMUMemoryRegion
304      */
305     uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu);
306     /**
307      * @notify_flag_changed:
308      *
309      * Called when IOMMU Notifier flag changes (ie when the set of
310      * events which IOMMU users are requesting notification for changes).
311      * Optional method -- need not be provided if the IOMMU does not
312      * need to know exactly which events must be notified.
313      *
314      * @iommu: the IOMMUMemoryRegion
315      *
316      * @old_flags: events which previously needed to be notified
317      *
318      * @new_flags: events which now need to be notified
319      *
320      * Returns 0 on success, or a negative errno; in particular
321      * returns -EINVAL if the new flag bitmap is not supported by the
322      * IOMMU memory region. In case of failure, the error object
323      * must be created
324      */
325     int (*notify_flag_changed)(IOMMUMemoryRegion *iommu,
326                                IOMMUNotifierFlag old_flags,
327                                IOMMUNotifierFlag new_flags,
328                                Error **errp);
329     /**
330      * @replay:
331      *
332      * Called to handle memory_region_iommu_replay().
333      *
334      * The default implementation of memory_region_iommu_replay() is to
335      * call the IOMMU translate method for every page in the address space
336      * with flag == IOMMU_NONE and then call the notifier if translate
337      * returns a valid mapping. If this method is implemented then it
338      * overrides the default behaviour, and must provide the full semantics
339      * of memory_region_iommu_replay(), by calling @notifier for every
340      * translation present in the IOMMU.
341      *
342      * Optional method -- an IOMMU only needs to provide this method
343      * if the default is inefficient or produces undesirable side effects.
344      *
345      * Note: this is not related to record-and-replay functionality.
346      */
347     void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier);
348 
349     /**
350      * @get_attr:
351      *
352      * Get IOMMU misc attributes. This is an optional method that
353      * can be used to allow users of the IOMMU to get implementation-specific
354      * information. The IOMMU implements this method to handle calls
355      * by IOMMU users to memory_region_iommu_get_attr() by filling in
356      * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that
357      * the IOMMU supports. If the method is unimplemented then
358      * memory_region_iommu_get_attr() will always return -EINVAL.
359      *
360      * @iommu: the IOMMUMemoryRegion
361      *
362      * @attr: attribute being queried
363      *
364      * @data: memory to fill in with the attribute data
365      *
366      * Returns 0 on success, or a negative errno; in particular
367      * returns -EINVAL for unrecognized or unimplemented attribute types.
368      */
369     int (*get_attr)(IOMMUMemoryRegion *iommu, enum IOMMUMemoryRegionAttr attr,
370                     void *data);
371 
372     /**
373      * @attrs_to_index:
374      *
375      * Return the IOMMU index to use for a given set of transaction attributes.
376      *
377      * Optional method: if an IOMMU only supports a single IOMMU index then
378      * the default implementation of memory_region_iommu_attrs_to_index()
379      * will return 0.
380      *
381      * The indexes supported by an IOMMU must be contiguous, starting at 0.
382      *
383      * @iommu: the IOMMUMemoryRegion
384      * @attrs: memory transaction attributes
385      */
386     int (*attrs_to_index)(IOMMUMemoryRegion *iommu, MemTxAttrs attrs);
387 
388     /**
389      * @num_indexes:
390      *
391      * Return the number of IOMMU indexes this IOMMU supports.
392      *
393      * Optional method: if this method is not provided, then
394      * memory_region_iommu_num_indexes() will return 1, indicating that
395      * only a single IOMMU index is supported.
396      *
397      * @iommu: the IOMMUMemoryRegion
398      */
399     int (*num_indexes)(IOMMUMemoryRegion *iommu);
400 };
401 
402 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
403 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
404 
405 /** MemoryRegion:
406  *
407  * A struct representing a memory region.
408  */
409 struct MemoryRegion {
410     Object parent_obj;
411 
412     /* private: */
413 
414     /* The following fields should fit in a cache line */
415     bool romd_mode;
416     bool ram;
417     bool subpage;
418     bool readonly; /* For RAM regions */
419     bool nonvolatile;
420     bool rom_device;
421     bool flush_coalesced_mmio;
422     uint8_t dirty_log_mask;
423     bool is_iommu;
424     RAMBlock *ram_block;
425     Object *owner;
426 
427     const MemoryRegionOps *ops;
428     void *opaque;
429     MemoryRegion *container;
430     Int128 size;
431     hwaddr addr;
432     void (*destructor)(MemoryRegion *mr);
433     uint64_t align;
434     bool terminates;
435     bool ram_device;
436     bool enabled;
437     bool warning_printed; /* For reservations */
438     uint8_t vga_logging_count;
439     MemoryRegion *alias;
440     hwaddr alias_offset;
441     int32_t priority;
442     QTAILQ_HEAD(, MemoryRegion) subregions;
443     QTAILQ_ENTRY(MemoryRegion) subregions_link;
444     QTAILQ_HEAD(, CoalescedMemoryRange) coalesced;
445     const char *name;
446     unsigned ioeventfd_nb;
447     MemoryRegionIoeventfd *ioeventfds;
448 };
449 
450 struct IOMMUMemoryRegion {
451     MemoryRegion parent_obj;
452 
453     QLIST_HEAD(, IOMMUNotifier) iommu_notify;
454     IOMMUNotifierFlag iommu_notify_flags;
455 };
456 
457 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
458     QLIST_FOREACH((n), &(mr)->iommu_notify, node)
459 
460 /**
461  * struct MemoryListener: callbacks structure for updates to the physical memory map
462  *
463  * Allows a component to adjust to changes in the guest-visible memory map.
464  * Use with memory_listener_register() and memory_listener_unregister().
465  */
466 struct MemoryListener {
467     /**
468      * @begin:
469      *
470      * Called at the beginning of an address space update transaction.
471      * Followed by calls to #MemoryListener.region_add(),
472      * #MemoryListener.region_del(), #MemoryListener.region_nop(),
473      * #MemoryListener.log_start() and #MemoryListener.log_stop() in
474      * increasing address order.
475      *
476      * @listener: The #MemoryListener.
477      */
478     void (*begin)(MemoryListener *listener);
479 
480     /**
481      * @commit:
482      *
483      * Called at the end of an address space update transaction,
484      * after the last call to #MemoryListener.region_add(),
485      * #MemoryListener.region_del() or #MemoryListener.region_nop(),
486      * #MemoryListener.log_start() and #MemoryListener.log_stop().
487      *
488      * @listener: The #MemoryListener.
489      */
490     void (*commit)(MemoryListener *listener);
491 
492     /**
493      * @region_add:
494      *
495      * Called during an address space update transaction,
496      * for a section of the address space that is new in this address space
497      * space since the last transaction.
498      *
499      * @listener: The #MemoryListener.
500      * @section: The new #MemoryRegionSection.
501      */
502     void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
503 
504     /**
505      * @region_del:
506      *
507      * Called during an address space update transaction,
508      * for a section of the address space that has disappeared in the address
509      * space since the last transaction.
510      *
511      * @listener: The #MemoryListener.
512      * @section: The old #MemoryRegionSection.
513      */
514     void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
515 
516     /**
517      * @region_nop:
518      *
519      * Called during an address space update transaction,
520      * for a section of the address space that is in the same place in the address
521      * space as in the last transaction.
522      *
523      * @listener: The #MemoryListener.
524      * @section: The #MemoryRegionSection.
525      */
526     void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
527 
528     /**
529      * @log_start:
530      *
531      * Called during an address space update transaction, after
532      * one of #MemoryListener.region_add(),#MemoryListener.region_del() or
533      * #MemoryListener.region_nop(), if dirty memory logging clients have
534      * become active since the last transaction.
535      *
536      * @listener: The #MemoryListener.
537      * @section: The #MemoryRegionSection.
538      * @old: A bitmap of dirty memory logging clients that were active in
539      * the previous transaction.
540      * @new: A bitmap of dirty memory logging clients that are active in
541      * the current transaction.
542      */
543     void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
544                       int old, int new);
545 
546     /**
547      * @log_stop:
548      *
549      * Called during an address space update transaction, after
550      * one of #MemoryListener.region_add(), #MemoryListener.region_del() or
551      * #MemoryListener.region_nop() and possibly after
552      * #MemoryListener.log_start(), if dirty memory logging clients have
553      * become inactive since the last transaction.
554      *
555      * @listener: The #MemoryListener.
556      * @section: The #MemoryRegionSection.
557      * @old: A bitmap of dirty memory logging clients that were active in
558      * the previous transaction.
559      * @new: A bitmap of dirty memory logging clients that are active in
560      * the current transaction.
561      */
562     void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
563                      int old, int new);
564 
565     /**
566      * @log_sync:
567      *
568      * Called by memory_region_snapshot_and_clear_dirty() and
569      * memory_global_dirty_log_sync(), before accessing QEMU's "official"
570      * copy of the dirty memory bitmap for a #MemoryRegionSection.
571      *
572      * @listener: The #MemoryListener.
573      * @section: The #MemoryRegionSection.
574      */
575     void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
576 
577     /**
578      * @log_clear:
579      *
580      * Called before reading the dirty memory bitmap for a
581      * #MemoryRegionSection.
582      *
583      * @listener: The #MemoryListener.
584      * @section: The #MemoryRegionSection.
585      */
586     void (*log_clear)(MemoryListener *listener, MemoryRegionSection *section);
587 
588     /**
589      * @log_global_start:
590      *
591      * Called by memory_global_dirty_log_start(), which
592      * enables the %DIRTY_LOG_MIGRATION client on all memory regions in
593      * the address space.  #MemoryListener.log_global_start() is also
594      * called when a #MemoryListener is added, if global dirty logging is
595      * active at that time.
596      *
597      * @listener: The #MemoryListener.
598      */
599     void (*log_global_start)(MemoryListener *listener);
600 
601     /**
602      * @log_global_stop:
603      *
604      * Called by memory_global_dirty_log_stop(), which
605      * disables the %DIRTY_LOG_MIGRATION client on all memory regions in
606      * the address space.
607      *
608      * @listener: The #MemoryListener.
609      */
610     void (*log_global_stop)(MemoryListener *listener);
611 
612     /**
613      * @log_global_after_sync:
614      *
615      * Called after reading the dirty memory bitmap
616      * for any #MemoryRegionSection.
617      *
618      * @listener: The #MemoryListener.
619      */
620     void (*log_global_after_sync)(MemoryListener *listener);
621 
622     /**
623      * @eventfd_add:
624      *
625      * Called during an address space update transaction,
626      * for a section of the address space that has had a new ioeventfd
627      * registration since the last transaction.
628      *
629      * @listener: The #MemoryListener.
630      * @section: The new #MemoryRegionSection.
631      * @match_data: The @match_data parameter for the new ioeventfd.
632      * @data: The @data parameter for the new ioeventfd.
633      * @e: The #EventNotifier parameter for the new ioeventfd.
634      */
635     void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
636                         bool match_data, uint64_t data, EventNotifier *e);
637 
638     /**
639      * @eventfd_del:
640      *
641      * Called during an address space update transaction,
642      * for a section of the address space that has dropped an ioeventfd
643      * registration since the last transaction.
644      *
645      * @listener: The #MemoryListener.
646      * @section: The new #MemoryRegionSection.
647      * @match_data: The @match_data parameter for the dropped ioeventfd.
648      * @data: The @data parameter for the dropped ioeventfd.
649      * @e: The #EventNotifier parameter for the dropped ioeventfd.
650      */
651     void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
652                         bool match_data, uint64_t data, EventNotifier *e);
653 
654     /**
655      * @coalesced_io_add:
656      *
657      * Called during an address space update transaction,
658      * for a section of the address space that has had a new coalesced
659      * MMIO range registration since the last transaction.
660      *
661      * @listener: The #MemoryListener.
662      * @section: The new #MemoryRegionSection.
663      * @addr: The starting address for the coalesced MMIO range.
664      * @len: The length of the coalesced MMIO range.
665      */
666     void (*coalesced_io_add)(MemoryListener *listener, MemoryRegionSection *section,
667                                hwaddr addr, hwaddr len);
668 
669     /**
670      * @coalesced_io_del:
671      *
672      * Called during an address space update transaction,
673      * for a section of the address space that has dropped a coalesced
674      * MMIO range since the last transaction.
675      *
676      * @listener: The #MemoryListener.
677      * @section: The new #MemoryRegionSection.
678      * @addr: The starting address for the coalesced MMIO range.
679      * @len: The length of the coalesced MMIO range.
680      */
681     void (*coalesced_io_del)(MemoryListener *listener, MemoryRegionSection *section,
682                                hwaddr addr, hwaddr len);
683     /**
684      * @priority:
685      *
686      * Govern the order in which memory listeners are invoked. Lower priorities
687      * are invoked earlier for "add" or "start" callbacks, and later for "delete"
688      * or "stop" callbacks.
689      */
690     unsigned priority;
691 
692     /* private: */
693     AddressSpace *address_space;
694     QTAILQ_ENTRY(MemoryListener) link;
695     QTAILQ_ENTRY(MemoryListener) link_as;
696 };
697 
698 /**
699  * struct AddressSpace: describes a mapping of addresses to #MemoryRegion objects
700  */
701 struct AddressSpace {
702     /* private: */
703     struct rcu_head rcu;
704     char *name;
705     MemoryRegion *root;
706 
707     /* Accessed via RCU.  */
708     struct FlatView *current_map;
709 
710     int ioeventfd_nb;
711     struct MemoryRegionIoeventfd *ioeventfds;
712     QTAILQ_HEAD(, MemoryListener) listeners;
713     QTAILQ_ENTRY(AddressSpace) address_spaces_link;
714 };
715 
716 typedef struct AddressSpaceDispatch AddressSpaceDispatch;
717 typedef struct FlatRange FlatRange;
718 
719 /* Flattened global view of current active memory hierarchy.  Kept in sorted
720  * order.
721  */
722 struct FlatView {
723     struct rcu_head rcu;
724     unsigned ref;
725     FlatRange *ranges;
726     unsigned nr;
727     unsigned nr_allocated;
728     struct AddressSpaceDispatch *dispatch;
729     MemoryRegion *root;
730 };
731 
732 static inline FlatView *address_space_to_flatview(AddressSpace *as)
733 {
734     return qatomic_rcu_read(&as->current_map);
735 }
736 
737 typedef int (*flatview_cb)(Int128 start,
738                            Int128 len,
739                            const MemoryRegion*, void*);
740 
741 void flatview_for_each_range(FlatView *fv, flatview_cb cb , void *opaque);
742 
743 /**
744  * struct MemoryRegionSection: describes a fragment of a #MemoryRegion
745  *
746  * @mr: the region, or %NULL if empty
747  * @fv: the flat view of the address space the region is mapped in
748  * @offset_within_region: the beginning of the section, relative to @mr's start
749  * @size: the size of the section; will not exceed @mr's boundaries
750  * @offset_within_address_space: the address of the first byte of the section
751  *     relative to the region's address space
752  * @readonly: writes to this section are ignored
753  * @nonvolatile: this section is non-volatile
754  */
755 struct MemoryRegionSection {
756     Int128 size;
757     MemoryRegion *mr;
758     FlatView *fv;
759     hwaddr offset_within_region;
760     hwaddr offset_within_address_space;
761     bool readonly;
762     bool nonvolatile;
763 };
764 
765 static inline bool MemoryRegionSection_eq(MemoryRegionSection *a,
766                                           MemoryRegionSection *b)
767 {
768     return a->mr == b->mr &&
769            a->fv == b->fv &&
770            a->offset_within_region == b->offset_within_region &&
771            a->offset_within_address_space == b->offset_within_address_space &&
772            int128_eq(a->size, b->size) &&
773            a->readonly == b->readonly &&
774            a->nonvolatile == b->nonvolatile;
775 }
776 
777 /**
778  * memory_region_init: Initialize a memory region
779  *
780  * The region typically acts as a container for other memory regions.  Use
781  * memory_region_add_subregion() to add subregions.
782  *
783  * @mr: the #MemoryRegion to be initialized
784  * @owner: the object that tracks the region's reference count
785  * @name: used for debugging; not visible to the user or ABI
786  * @size: size of the region; any subregions beyond this size will be clipped
787  */
788 void memory_region_init(MemoryRegion *mr,
789                         struct Object *owner,
790                         const char *name,
791                         uint64_t size);
792 
793 /**
794  * memory_region_ref: Add 1 to a memory region's reference count
795  *
796  * Whenever memory regions are accessed outside the BQL, they need to be
797  * preserved against hot-unplug.  MemoryRegions actually do not have their
798  * own reference count; they piggyback on a QOM object, their "owner".
799  * This function adds a reference to the owner.
800  *
801  * All MemoryRegions must have an owner if they can disappear, even if the
802  * device they belong to operates exclusively under the BQL.  This is because
803  * the region could be returned at any time by memory_region_find, and this
804  * is usually under guest control.
805  *
806  * @mr: the #MemoryRegion
807  */
808 void memory_region_ref(MemoryRegion *mr);
809 
810 /**
811  * memory_region_unref: Remove 1 to a memory region's reference count
812  *
813  * Whenever memory regions are accessed outside the BQL, they need to be
814  * preserved against hot-unplug.  MemoryRegions actually do not have their
815  * own reference count; they piggyback on a QOM object, their "owner".
816  * This function removes a reference to the owner and possibly destroys it.
817  *
818  * @mr: the #MemoryRegion
819  */
820 void memory_region_unref(MemoryRegion *mr);
821 
822 /**
823  * memory_region_init_io: Initialize an I/O memory region.
824  *
825  * Accesses into the region will cause the callbacks in @ops to be called.
826  * if @size is nonzero, subregions will be clipped to @size.
827  *
828  * @mr: the #MemoryRegion to be initialized.
829  * @owner: the object that tracks the region's reference count
830  * @ops: a structure containing read and write callbacks to be used when
831  *       I/O is performed on the region.
832  * @opaque: passed to the read and write callbacks of the @ops structure.
833  * @name: used for debugging; not visible to the user or ABI
834  * @size: size of the region.
835  */
836 void memory_region_init_io(MemoryRegion *mr,
837                            struct Object *owner,
838                            const MemoryRegionOps *ops,
839                            void *opaque,
840                            const char *name,
841                            uint64_t size);
842 
843 /**
844  * memory_region_init_ram_nomigrate:  Initialize RAM memory region.  Accesses
845  *                                    into the region will modify memory
846  *                                    directly.
847  *
848  * @mr: the #MemoryRegion to be initialized.
849  * @owner: the object that tracks the region's reference count
850  * @name: Region name, becomes part of RAMBlock name used in migration stream
851  *        must be unique within any device
852  * @size: size of the region.
853  * @errp: pointer to Error*, to store an error if it happens.
854  *
855  * Note that this function does not do anything to cause the data in the
856  * RAM memory region to be migrated; that is the responsibility of the caller.
857  */
858 void memory_region_init_ram_nomigrate(MemoryRegion *mr,
859                                       struct Object *owner,
860                                       const char *name,
861                                       uint64_t size,
862                                       Error **errp);
863 
864 /**
865  * memory_region_init_ram_shared_nomigrate:  Initialize RAM memory region.
866  *                                           Accesses into the region will
867  *                                           modify memory directly.
868  *
869  * @mr: the #MemoryRegion to be initialized.
870  * @owner: the object that tracks the region's reference count
871  * @name: Region name, becomes part of RAMBlock name used in migration stream
872  *        must be unique within any device
873  * @size: size of the region.
874  * @share: allow remapping RAM to different addresses
875  * @errp: pointer to Error*, to store an error if it happens.
876  *
877  * Note that this function is similar to memory_region_init_ram_nomigrate.
878  * The only difference is part of the RAM region can be remapped.
879  */
880 void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr,
881                                              struct Object *owner,
882                                              const char *name,
883                                              uint64_t size,
884                                              bool share,
885                                              Error **errp);
886 
887 /**
888  * memory_region_init_resizeable_ram:  Initialize memory region with resizeable
889  *                                     RAM.  Accesses into the region will
890  *                                     modify memory directly.  Only an initial
891  *                                     portion of this RAM is actually used.
892  *                                     The used size can change across reboots.
893  *
894  * @mr: the #MemoryRegion to be initialized.
895  * @owner: the object that tracks the region's reference count
896  * @name: Region name, becomes part of RAMBlock name used in migration stream
897  *        must be unique within any device
898  * @size: used size of the region.
899  * @max_size: max size of the region.
900  * @resized: callback to notify owner about used size change.
901  * @errp: pointer to Error*, to store an error if it happens.
902  *
903  * Note that this function does not do anything to cause the data in the
904  * RAM memory region to be migrated; that is the responsibility of the caller.
905  */
906 void memory_region_init_resizeable_ram(MemoryRegion *mr,
907                                        struct Object *owner,
908                                        const char *name,
909                                        uint64_t size,
910                                        uint64_t max_size,
911                                        void (*resized)(const char*,
912                                                        uint64_t length,
913                                                        void *host),
914                                        Error **errp);
915 #ifdef CONFIG_POSIX
916 
917 /**
918  * memory_region_init_ram_from_file:  Initialize RAM memory region with a
919  *                                    mmap-ed backend.
920  *
921  * @mr: the #MemoryRegion to be initialized.
922  * @owner: the object that tracks the region's reference count
923  * @name: Region name, becomes part of RAMBlock name used in migration stream
924  *        must be unique within any device
925  * @size: size of the region.
926  * @align: alignment of the region base address; if 0, the default alignment
927  *         (getpagesize()) will be used.
928  * @ram_flags: Memory region features:
929  *             - RAM_SHARED: memory must be mmaped with the MAP_SHARED flag
930  *             - RAM_PMEM: the memory is persistent memory
931  *             Other bits are ignored now.
932  * @path: the path in which to allocate the RAM.
933  * @errp: pointer to Error*, to store an error if it happens.
934  *
935  * Note that this function does not do anything to cause the data in the
936  * RAM memory region to be migrated; that is the responsibility of the caller.
937  */
938 void memory_region_init_ram_from_file(MemoryRegion *mr,
939                                       struct Object *owner,
940                                       const char *name,
941                                       uint64_t size,
942                                       uint64_t align,
943                                       uint32_t ram_flags,
944                                       const char *path,
945                                       Error **errp);
946 
947 /**
948  * memory_region_init_ram_from_fd:  Initialize RAM memory region with a
949  *                                  mmap-ed backend.
950  *
951  * @mr: the #MemoryRegion to be initialized.
952  * @owner: the object that tracks the region's reference count
953  * @name: the name of the region.
954  * @size: size of the region.
955  * @share: %true if memory must be mmaped with the MAP_SHARED flag
956  * @fd: the fd to mmap.
957  * @errp: pointer to Error*, to store an error if it happens.
958  *
959  * Note that this function does not do anything to cause the data in the
960  * RAM memory region to be migrated; that is the responsibility of the caller.
961  */
962 void memory_region_init_ram_from_fd(MemoryRegion *mr,
963                                     struct Object *owner,
964                                     const char *name,
965                                     uint64_t size,
966                                     bool share,
967                                     int fd,
968                                     Error **errp);
969 #endif
970 
971 /**
972  * memory_region_init_ram_ptr:  Initialize RAM memory region from a
973  *                              user-provided pointer.  Accesses into the
974  *                              region will modify memory directly.
975  *
976  * @mr: the #MemoryRegion to be initialized.
977  * @owner: the object that tracks the region's reference count
978  * @name: Region name, becomes part of RAMBlock name used in migration stream
979  *        must be unique within any device
980  * @size: size of the region.
981  * @ptr: memory to be mapped; must contain at least @size bytes.
982  *
983  * Note that this function does not do anything to cause the data in the
984  * RAM memory region to be migrated; that is the responsibility of the caller.
985  */
986 void memory_region_init_ram_ptr(MemoryRegion *mr,
987                                 struct Object *owner,
988                                 const char *name,
989                                 uint64_t size,
990                                 void *ptr);
991 
992 /**
993  * memory_region_init_ram_device_ptr:  Initialize RAM device memory region from
994  *                                     a user-provided pointer.
995  *
996  * A RAM device represents a mapping to a physical device, such as to a PCI
997  * MMIO BAR of an vfio-pci assigned device.  The memory region may be mapped
998  * into the VM address space and access to the region will modify memory
999  * directly.  However, the memory region should not be included in a memory
1000  * dump (device may not be enabled/mapped at the time of the dump), and
1001  * operations incompatible with manipulating MMIO should be avoided.  Replaces
1002  * skip_dump flag.
1003  *
1004  * @mr: the #MemoryRegion to be initialized.
1005  * @owner: the object that tracks the region's reference count
1006  * @name: the name of the region.
1007  * @size: size of the region.
1008  * @ptr: memory to be mapped; must contain at least @size bytes.
1009  *
1010  * Note that this function does not do anything to cause the data in the
1011  * RAM memory region to be migrated; that is the responsibility of the caller.
1012  * (For RAM device memory regions, migrating the contents rarely makes sense.)
1013  */
1014 void memory_region_init_ram_device_ptr(MemoryRegion *mr,
1015                                        struct Object *owner,
1016                                        const char *name,
1017                                        uint64_t size,
1018                                        void *ptr);
1019 
1020 /**
1021  * memory_region_init_alias: Initialize a memory region that aliases all or a
1022  *                           part of another memory region.
1023  *
1024  * @mr: the #MemoryRegion to be initialized.
1025  * @owner: the object that tracks the region's reference count
1026  * @name: used for debugging; not visible to the user or ABI
1027  * @orig: the region to be referenced; @mr will be equivalent to
1028  *        @orig between @offset and @offset + @size - 1.
1029  * @offset: start of the section in @orig to be referenced.
1030  * @size: size of the region.
1031  */
1032 void memory_region_init_alias(MemoryRegion *mr,
1033                               struct Object *owner,
1034                               const char *name,
1035                               MemoryRegion *orig,
1036                               hwaddr offset,
1037                               uint64_t size);
1038 
1039 /**
1040  * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
1041  *
1042  * This has the same effect as calling memory_region_init_ram_nomigrate()
1043  * and then marking the resulting region read-only with
1044  * memory_region_set_readonly().
1045  *
1046  * Note that this function does not do anything to cause the data in the
1047  * RAM side of the memory region to be migrated; that is the responsibility
1048  * of the caller.
1049  *
1050  * @mr: the #MemoryRegion to be initialized.
1051  * @owner: the object that tracks the region's reference count
1052  * @name: Region name, becomes part of RAMBlock name used in migration stream
1053  *        must be unique within any device
1054  * @size: size of the region.
1055  * @errp: pointer to Error*, to store an error if it happens.
1056  */
1057 void memory_region_init_rom_nomigrate(MemoryRegion *mr,
1058                                       struct Object *owner,
1059                                       const char *name,
1060                                       uint64_t size,
1061                                       Error **errp);
1062 
1063 /**
1064  * memory_region_init_rom_device_nomigrate:  Initialize a ROM memory region.
1065  *                                 Writes are handled via callbacks.
1066  *
1067  * Note that this function does not do anything to cause the data in the
1068  * RAM side of the memory region to be migrated; that is the responsibility
1069  * of the caller.
1070  *
1071  * @mr: the #MemoryRegion to be initialized.
1072  * @owner: the object that tracks the region's reference count
1073  * @ops: callbacks for write access handling (must not be NULL).
1074  * @opaque: passed to the read and write callbacks of the @ops structure.
1075  * @name: Region name, becomes part of RAMBlock name used in migration stream
1076  *        must be unique within any device
1077  * @size: size of the region.
1078  * @errp: pointer to Error*, to store an error if it happens.
1079  */
1080 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
1081                                              struct Object *owner,
1082                                              const MemoryRegionOps *ops,
1083                                              void *opaque,
1084                                              const char *name,
1085                                              uint64_t size,
1086                                              Error **errp);
1087 
1088 /**
1089  * memory_region_init_iommu: Initialize a memory region of a custom type
1090  * that translates addresses
1091  *
1092  * An IOMMU region translates addresses and forwards accesses to a target
1093  * memory region.
1094  *
1095  * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
1096  * @_iommu_mr should be a pointer to enough memory for an instance of
1097  * that subclass, @instance_size is the size of that subclass, and
1098  * @mrtypename is its name. This function will initialize @_iommu_mr as an
1099  * instance of the subclass, and its methods will then be called to handle
1100  * accesses to the memory region. See the documentation of
1101  * #IOMMUMemoryRegionClass for further details.
1102  *
1103  * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
1104  * @instance_size: the IOMMUMemoryRegion subclass instance size
1105  * @mrtypename: the type name of the #IOMMUMemoryRegion
1106  * @owner: the object that tracks the region's reference count
1107  * @name: used for debugging; not visible to the user or ABI
1108  * @size: size of the region.
1109  */
1110 void memory_region_init_iommu(void *_iommu_mr,
1111                               size_t instance_size,
1112                               const char *mrtypename,
1113                               Object *owner,
1114                               const char *name,
1115                               uint64_t size);
1116 
1117 /**
1118  * memory_region_init_ram - Initialize RAM memory region.  Accesses into the
1119  *                          region will modify memory directly.
1120  *
1121  * @mr: the #MemoryRegion to be initialized
1122  * @owner: the object that tracks the region's reference count (must be
1123  *         TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
1124  * @name: name of the memory region
1125  * @size: size of the region in bytes
1126  * @errp: pointer to Error*, to store an error if it happens.
1127  *
1128  * This function allocates RAM for a board model or device, and
1129  * arranges for it to be migrated (by calling vmstate_register_ram()
1130  * if @owner is a DeviceState, or vmstate_register_ram_global() if
1131  * @owner is NULL).
1132  *
1133  * TODO: Currently we restrict @owner to being either NULL (for
1134  * global RAM regions with no owner) or devices, so that we can
1135  * give the RAM block a unique name for migration purposes.
1136  * We should lift this restriction and allow arbitrary Objects.
1137  * If you pass a non-NULL non-device @owner then we will assert.
1138  */
1139 void memory_region_init_ram(MemoryRegion *mr,
1140                             struct Object *owner,
1141                             const char *name,
1142                             uint64_t size,
1143                             Error **errp);
1144 
1145 /**
1146  * memory_region_init_rom: Initialize a ROM memory region.
1147  *
1148  * This has the same effect as calling memory_region_init_ram()
1149  * and then marking the resulting region read-only with
1150  * memory_region_set_readonly(). This includes arranging for the
1151  * contents to be migrated.
1152  *
1153  * TODO: Currently we restrict @owner to being either NULL (for
1154  * global RAM regions with no owner) or devices, so that we can
1155  * give the RAM block a unique name for migration purposes.
1156  * We should lift this restriction and allow arbitrary Objects.
1157  * If you pass a non-NULL non-device @owner then we will assert.
1158  *
1159  * @mr: the #MemoryRegion to be initialized.
1160  * @owner: the object that tracks the region's reference count
1161  * @name: Region name, becomes part of RAMBlock name used in migration stream
1162  *        must be unique within any device
1163  * @size: size of the region.
1164  * @errp: pointer to Error*, to store an error if it happens.
1165  */
1166 void memory_region_init_rom(MemoryRegion *mr,
1167                             struct Object *owner,
1168                             const char *name,
1169                             uint64_t size,
1170                             Error **errp);
1171 
1172 /**
1173  * memory_region_init_rom_device:  Initialize a ROM memory region.
1174  *                                 Writes are handled via callbacks.
1175  *
1176  * This function initializes a memory region backed by RAM for reads
1177  * and callbacks for writes, and arranges for the RAM backing to
1178  * be migrated (by calling vmstate_register_ram()
1179  * if @owner is a DeviceState, or vmstate_register_ram_global() if
1180  * @owner is NULL).
1181  *
1182  * TODO: Currently we restrict @owner to being either NULL (for
1183  * global RAM regions with no owner) or devices, so that we can
1184  * give the RAM block a unique name for migration purposes.
1185  * We should lift this restriction and allow arbitrary Objects.
1186  * If you pass a non-NULL non-device @owner then we will assert.
1187  *
1188  * @mr: the #MemoryRegion to be initialized.
1189  * @owner: the object that tracks the region's reference count
1190  * @ops: callbacks for write access handling (must not be NULL).
1191  * @opaque: passed to the read and write callbacks of the @ops structure.
1192  * @name: Region name, becomes part of RAMBlock name used in migration stream
1193  *        must be unique within any device
1194  * @size: size of the region.
1195  * @errp: pointer to Error*, to store an error if it happens.
1196  */
1197 void memory_region_init_rom_device(MemoryRegion *mr,
1198                                    struct Object *owner,
1199                                    const MemoryRegionOps *ops,
1200                                    void *opaque,
1201                                    const char *name,
1202                                    uint64_t size,
1203                                    Error **errp);
1204 
1205 
1206 /**
1207  * memory_region_owner: get a memory region's owner.
1208  *
1209  * @mr: the memory region being queried.
1210  */
1211 struct Object *memory_region_owner(MemoryRegion *mr);
1212 
1213 /**
1214  * memory_region_size: get a memory region's size.
1215  *
1216  * @mr: the memory region being queried.
1217  */
1218 uint64_t memory_region_size(MemoryRegion *mr);
1219 
1220 /**
1221  * memory_region_is_ram: check whether a memory region is random access
1222  *
1223  * Returns %true if a memory region is random access.
1224  *
1225  * @mr: the memory region being queried
1226  */
1227 static inline bool memory_region_is_ram(MemoryRegion *mr)
1228 {
1229     return mr->ram;
1230 }
1231 
1232 /**
1233  * memory_region_is_ram_device: check whether a memory region is a ram device
1234  *
1235  * Returns %true if a memory region is a device backed ram region
1236  *
1237  * @mr: the memory region being queried
1238  */
1239 bool memory_region_is_ram_device(MemoryRegion *mr);
1240 
1241 /**
1242  * memory_region_is_romd: check whether a memory region is in ROMD mode
1243  *
1244  * Returns %true if a memory region is a ROM device and currently set to allow
1245  * direct reads.
1246  *
1247  * @mr: the memory region being queried
1248  */
1249 static inline bool memory_region_is_romd(MemoryRegion *mr)
1250 {
1251     return mr->rom_device && mr->romd_mode;
1252 }
1253 
1254 /**
1255  * memory_region_get_iommu: check whether a memory region is an iommu
1256  *
1257  * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
1258  * otherwise NULL.
1259  *
1260  * @mr: the memory region being queried
1261  */
1262 static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
1263 {
1264     if (mr->alias) {
1265         return memory_region_get_iommu(mr->alias);
1266     }
1267     if (mr->is_iommu) {
1268         return (IOMMUMemoryRegion *) mr;
1269     }
1270     return NULL;
1271 }
1272 
1273 /**
1274  * memory_region_get_iommu_class_nocheck: returns iommu memory region class
1275  *   if an iommu or NULL if not
1276  *
1277  * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
1278  * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1279  *
1280  * @iommu_mr: the memory region being queried
1281  */
1282 static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck(
1283         IOMMUMemoryRegion *iommu_mr)
1284 {
1285     return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class);
1286 }
1287 
1288 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1289 
1290 /**
1291  * memory_region_iommu_get_min_page_size: get minimum supported page size
1292  * for an iommu
1293  *
1294  * Returns minimum supported page size for an iommu.
1295  *
1296  * @iommu_mr: the memory region being queried
1297  */
1298 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
1299 
1300 /**
1301  * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1302  *
1303  * The notification type will be decided by entry.perm bits:
1304  *
1305  * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE.
1306  * - For MAP (newly added entry) notifies: set entry.perm to the
1307  *   permission of the page (which is definitely !IOMMU_NONE).
1308  *
1309  * Note: for any IOMMU implementation, an in-place mapping change
1310  * should be notified with an UNMAP followed by a MAP.
1311  *
1312  * @iommu_mr: the memory region that was changed
1313  * @iommu_idx: the IOMMU index for the translation table which has changed
1314  * @entry: the new entry in the IOMMU translation table.  The entry
1315  *         replaces all old entries for the same virtual I/O address range.
1316  *         Deleted entries have .@perm == 0.
1317  */
1318 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1319                                 int iommu_idx,
1320                                 IOMMUTLBEntry entry);
1321 
1322 /**
1323  * memory_region_notify_one: notify a change in an IOMMU translation
1324  *                           entry to a single notifier
1325  *
1326  * This works just like memory_region_notify_iommu(), but it only
1327  * notifies a specific notifier, not all of them.
1328  *
1329  * @notifier: the notifier to be notified
1330  * @entry: the new entry in the IOMMU translation table.  The entry
1331  *         replaces all old entries for the same virtual I/O address range.
1332  *         Deleted entries have .@perm == 0.
1333  */
1334 void memory_region_notify_one(IOMMUNotifier *notifier,
1335                               IOMMUTLBEntry *entry);
1336 
1337 /**
1338  * memory_region_register_iommu_notifier: register a notifier for changes to
1339  * IOMMU translation entries.
1340  *
1341  * Returns 0 on success, or a negative errno otherwise. In particular,
1342  * -EINVAL indicates that at least one of the attributes of the notifier
1343  * is not supported (flag/range) by the IOMMU memory region. In case of error
1344  * the error object must be created.
1345  *
1346  * @mr: the memory region to observe
1347  * @n: the IOMMUNotifier to be added; the notify callback receives a
1348  *     pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1349  *     ceases to be valid on exit from the notifier.
1350  * @errp: pointer to Error*, to store an error if it happens.
1351  */
1352 int memory_region_register_iommu_notifier(MemoryRegion *mr,
1353                                           IOMMUNotifier *n, Error **errp);
1354 
1355 /**
1356  * memory_region_iommu_replay: replay existing IOMMU translations to
1357  * a notifier with the minimum page granularity returned by
1358  * mr->iommu_ops->get_page_size().
1359  *
1360  * Note: this is not related to record-and-replay functionality.
1361  *
1362  * @iommu_mr: the memory region to observe
1363  * @n: the notifier to which to replay iommu mappings
1364  */
1365 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
1366 
1367 /**
1368  * memory_region_unregister_iommu_notifier: unregister a notifier for
1369  * changes to IOMMU translation entries.
1370  *
1371  * @mr: the memory region which was observed and for which notity_stopped()
1372  *      needs to be called
1373  * @n: the notifier to be removed.
1374  */
1375 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1376                                              IOMMUNotifier *n);
1377 
1378 /**
1379  * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1380  * defined on the IOMMU.
1381  *
1382  * Returns 0 on success, or a negative errno otherwise. In particular,
1383  * -EINVAL indicates that the IOMMU does not support the requested
1384  * attribute.
1385  *
1386  * @iommu_mr: the memory region
1387  * @attr: the requested attribute
1388  * @data: a pointer to the requested attribute data
1389  */
1390 int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1391                                  enum IOMMUMemoryRegionAttr attr,
1392                                  void *data);
1393 
1394 /**
1395  * memory_region_iommu_attrs_to_index: return the IOMMU index to
1396  * use for translations with the given memory transaction attributes.
1397  *
1398  * @iommu_mr: the memory region
1399  * @attrs: the memory transaction attributes
1400  */
1401 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr,
1402                                        MemTxAttrs attrs);
1403 
1404 /**
1405  * memory_region_iommu_num_indexes: return the total number of IOMMU
1406  * indexes that this IOMMU supports.
1407  *
1408  * @iommu_mr: the memory region
1409  */
1410 int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr);
1411 
1412 /**
1413  * memory_region_name: get a memory region's name
1414  *
1415  * Returns the string that was used to initialize the memory region.
1416  *
1417  * @mr: the memory region being queried
1418  */
1419 const char *memory_region_name(const MemoryRegion *mr);
1420 
1421 /**
1422  * memory_region_is_logging: return whether a memory region is logging writes
1423  *
1424  * Returns %true if the memory region is logging writes for the given client
1425  *
1426  * @mr: the memory region being queried
1427  * @client: the client being queried
1428  */
1429 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
1430 
1431 /**
1432  * memory_region_get_dirty_log_mask: return the clients for which a
1433  * memory region is logging writes.
1434  *
1435  * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1436  * are the bit indices.
1437  *
1438  * @mr: the memory region being queried
1439  */
1440 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
1441 
1442 /**
1443  * memory_region_is_rom: check whether a memory region is ROM
1444  *
1445  * Returns %true if a memory region is read-only memory.
1446  *
1447  * @mr: the memory region being queried
1448  */
1449 static inline bool memory_region_is_rom(MemoryRegion *mr)
1450 {
1451     return mr->ram && mr->readonly;
1452 }
1453 
1454 /**
1455  * memory_region_is_nonvolatile: check whether a memory region is non-volatile
1456  *
1457  * Returns %true is a memory region is non-volatile memory.
1458  *
1459  * @mr: the memory region being queried
1460  */
1461 static inline bool memory_region_is_nonvolatile(MemoryRegion *mr)
1462 {
1463     return mr->nonvolatile;
1464 }
1465 
1466 /**
1467  * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1468  *
1469  * Returns a file descriptor backing a file-based RAM memory region,
1470  * or -1 if the region is not a file-based RAM memory region.
1471  *
1472  * @mr: the RAM or alias memory region being queried.
1473  */
1474 int memory_region_get_fd(MemoryRegion *mr);
1475 
1476 /**
1477  * memory_region_from_host: Convert a pointer into a RAM memory region
1478  * and an offset within it.
1479  *
1480  * Given a host pointer inside a RAM memory region (created with
1481  * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1482  * the MemoryRegion and the offset within it.
1483  *
1484  * Use with care; by the time this function returns, the returned pointer is
1485  * not protected by RCU anymore.  If the caller is not within an RCU critical
1486  * section and does not hold the iothread lock, it must have other means of
1487  * protecting the pointer, such as a reference to the region that includes
1488  * the incoming ram_addr_t.
1489  *
1490  * @ptr: the host pointer to be converted
1491  * @offset: the offset within memory region
1492  */
1493 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
1494 
1495 /**
1496  * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1497  *
1498  * Returns a host pointer to a RAM memory region (created with
1499  * memory_region_init_ram() or memory_region_init_ram_ptr()).
1500  *
1501  * Use with care; by the time this function returns, the returned pointer is
1502  * not protected by RCU anymore.  If the caller is not within an RCU critical
1503  * section and does not hold the iothread lock, it must have other means of
1504  * protecting the pointer, such as a reference to the region that includes
1505  * the incoming ram_addr_t.
1506  *
1507  * @mr: the memory region being queried.
1508  */
1509 void *memory_region_get_ram_ptr(MemoryRegion *mr);
1510 
1511 /* memory_region_ram_resize: Resize a RAM region.
1512  *
1513  * Only legal before guest might have detected the memory size: e.g. on
1514  * incoming migration, or right after reset.
1515  *
1516  * @mr: a memory region created with @memory_region_init_resizeable_ram.
1517  * @newsize: the new size the region
1518  * @errp: pointer to Error*, to store an error if it happens.
1519  */
1520 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
1521                               Error **errp);
1522 
1523 /**
1524  * memory_region_msync: Synchronize selected address range of
1525  * a memory mapped region
1526  *
1527  * @mr: the memory region to be msync
1528  * @addr: the initial address of the range to be sync
1529  * @size: the size of the range to be sync
1530  */
1531 void memory_region_msync(MemoryRegion *mr, hwaddr addr, hwaddr size);
1532 
1533 /**
1534  * memory_region_writeback: Trigger cache writeback for
1535  * selected address range
1536  *
1537  * @mr: the memory region to be updated
1538  * @addr: the initial address of the range to be written back
1539  * @size: the size of the range to be written back
1540  */
1541 void memory_region_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size);
1542 
1543 /**
1544  * memory_region_set_log: Turn dirty logging on or off for a region.
1545  *
1546  * Turns dirty logging on or off for a specified client (display, migration).
1547  * Only meaningful for RAM regions.
1548  *
1549  * @mr: the memory region being updated.
1550  * @log: whether dirty logging is to be enabled or disabled.
1551  * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1552  */
1553 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
1554 
1555 /**
1556  * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1557  *
1558  * Marks a range of bytes as dirty, after it has been dirtied outside
1559  * guest code.
1560  *
1561  * @mr: the memory region being dirtied.
1562  * @addr: the address (relative to the start of the region) being dirtied.
1563  * @size: size of the range being dirtied.
1564  */
1565 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1566                              hwaddr size);
1567 
1568 /**
1569  * memory_region_clear_dirty_bitmap - clear dirty bitmap for memory range
1570  *
1571  * This function is called when the caller wants to clear the remote
1572  * dirty bitmap of a memory range within the memory region.  This can
1573  * be used by e.g. KVM to manually clear dirty log when
1574  * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT is declared support by the host
1575  * kernel.
1576  *
1577  * @mr:     the memory region to clear the dirty log upon
1578  * @start:  start address offset within the memory region
1579  * @len:    length of the memory region to clear dirty bitmap
1580  */
1581 void memory_region_clear_dirty_bitmap(MemoryRegion *mr, hwaddr start,
1582                                       hwaddr len);
1583 
1584 /**
1585  * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
1586  *                                         bitmap and clear it.
1587  *
1588  * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
1589  * returns the snapshot.  The snapshot can then be used to query dirty
1590  * status, using memory_region_snapshot_get_dirty.  Snapshotting allows
1591  * querying the same page multiple times, which is especially useful for
1592  * display updates where the scanlines often are not page aligned.
1593  *
1594  * The dirty bitmap region which gets copyed into the snapshot (and
1595  * cleared afterwards) can be larger than requested.  The boundaries
1596  * are rounded up/down so complete bitmap longs (covering 64 pages on
1597  * 64bit hosts) can be copied over into the bitmap snapshot.  Which
1598  * isn't a problem for display updates as the extra pages are outside
1599  * the visible area, and in case the visible area changes a full
1600  * display redraw is due anyway.  Should other use cases for this
1601  * function emerge we might have to revisit this implementation
1602  * detail.
1603  *
1604  * Use g_free to release DirtyBitmapSnapshot.
1605  *
1606  * @mr: the memory region being queried.
1607  * @addr: the address (relative to the start of the region) being queried.
1608  * @size: the size of the range being queried.
1609  * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
1610  */
1611 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
1612                                                             hwaddr addr,
1613                                                             hwaddr size,
1614                                                             unsigned client);
1615 
1616 /**
1617  * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
1618  *                                   in the specified dirty bitmap snapshot.
1619  *
1620  * @mr: the memory region being queried.
1621  * @snap: the dirty bitmap snapshot
1622  * @addr: the address (relative to the start of the region) being queried.
1623  * @size: the size of the range being queried.
1624  */
1625 bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
1626                                       DirtyBitmapSnapshot *snap,
1627                                       hwaddr addr, hwaddr size);
1628 
1629 /**
1630  * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
1631  *                            client.
1632  *
1633  * Marks a range of pages as no longer dirty.
1634  *
1635  * @mr: the region being updated.
1636  * @addr: the start of the subrange being cleaned.
1637  * @size: the size of the subrange being cleaned.
1638  * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
1639  *          %DIRTY_MEMORY_VGA.
1640  */
1641 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1642                                hwaddr size, unsigned client);
1643 
1644 /**
1645  * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate
1646  *                                 TBs (for self-modifying code).
1647  *
1648  * The MemoryRegionOps->write() callback of a ROM device must use this function
1649  * to mark byte ranges that have been modified internally, such as by directly
1650  * accessing the memory returned by memory_region_get_ram_ptr().
1651  *
1652  * This function marks the range dirty and invalidates TBs so that TCG can
1653  * detect self-modifying code.
1654  *
1655  * @mr: the region being flushed.
1656  * @addr: the start, relative to the start of the region, of the range being
1657  *        flushed.
1658  * @size: the size, in bytes, of the range being flushed.
1659  */
1660 void memory_region_flush_rom_device(MemoryRegion *mr, hwaddr addr, hwaddr size);
1661 
1662 /**
1663  * memory_region_set_readonly: Turn a memory region read-only (or read-write)
1664  *
1665  * Allows a memory region to be marked as read-only (turning it into a ROM).
1666  * only useful on RAM regions.
1667  *
1668  * @mr: the region being updated.
1669  * @readonly: whether rhe region is to be ROM or RAM.
1670  */
1671 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
1672 
1673 /**
1674  * memory_region_set_nonvolatile: Turn a memory region non-volatile
1675  *
1676  * Allows a memory region to be marked as non-volatile.
1677  * only useful on RAM regions.
1678  *
1679  * @mr: the region being updated.
1680  * @nonvolatile: whether rhe region is to be non-volatile.
1681  */
1682 void memory_region_set_nonvolatile(MemoryRegion *mr, bool nonvolatile);
1683 
1684 /**
1685  * memory_region_rom_device_set_romd: enable/disable ROMD mode
1686  *
1687  * Allows a ROM device (initialized with memory_region_init_rom_device() to
1688  * set to ROMD mode (default) or MMIO mode.  When it is in ROMD mode, the
1689  * device is mapped to guest memory and satisfies read access directly.
1690  * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
1691  * Writes are always handled by the #MemoryRegion.write function.
1692  *
1693  * @mr: the memory region to be updated
1694  * @romd_mode: %true to put the region into ROMD mode
1695  */
1696 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
1697 
1698 /**
1699  * memory_region_set_coalescing: Enable memory coalescing for the region.
1700  *
1701  * Enabled writes to a region to be queued for later processing. MMIO ->write
1702  * callbacks may be delayed until a non-coalesced MMIO is issued.
1703  * Only useful for IO regions.  Roughly similar to write-combining hardware.
1704  *
1705  * @mr: the memory region to be write coalesced
1706  */
1707 void memory_region_set_coalescing(MemoryRegion *mr);
1708 
1709 /**
1710  * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
1711  *                               a region.
1712  *
1713  * Like memory_region_set_coalescing(), but works on a sub-range of a region.
1714  * Multiple calls can be issued coalesced disjoint ranges.
1715  *
1716  * @mr: the memory region to be updated.
1717  * @offset: the start of the range within the region to be coalesced.
1718  * @size: the size of the subrange to be coalesced.
1719  */
1720 void memory_region_add_coalescing(MemoryRegion *mr,
1721                                   hwaddr offset,
1722                                   uint64_t size);
1723 
1724 /**
1725  * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
1726  *
1727  * Disables any coalescing caused by memory_region_set_coalescing() or
1728  * memory_region_add_coalescing().  Roughly equivalent to uncacheble memory
1729  * hardware.
1730  *
1731  * @mr: the memory region to be updated.
1732  */
1733 void memory_region_clear_coalescing(MemoryRegion *mr);
1734 
1735 /**
1736  * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
1737  *                                    accesses.
1738  *
1739  * Ensure that pending coalesced MMIO request are flushed before the memory
1740  * region is accessed. This property is automatically enabled for all regions
1741  * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
1742  *
1743  * @mr: the memory region to be updated.
1744  */
1745 void memory_region_set_flush_coalesced(MemoryRegion *mr);
1746 
1747 /**
1748  * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
1749  *                                      accesses.
1750  *
1751  * Clear the automatic coalesced MMIO flushing enabled via
1752  * memory_region_set_flush_coalesced. Note that this service has no effect on
1753  * memory regions that have MMIO coalescing enabled for themselves. For them,
1754  * automatic flushing will stop once coalescing is disabled.
1755  *
1756  * @mr: the memory region to be updated.
1757  */
1758 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
1759 
1760 /**
1761  * memory_region_add_eventfd: Request an eventfd to be triggered when a word
1762  *                            is written to a location.
1763  *
1764  * Marks a word in an IO region (initialized with memory_region_init_io())
1765  * as a trigger for an eventfd event.  The I/O callback will not be called.
1766  * The caller must be prepared to handle failure (that is, take the required
1767  * action if the callback _is_ called).
1768  *
1769  * @mr: the memory region being updated.
1770  * @addr: the address within @mr that is to be monitored
1771  * @size: the size of the access to trigger the eventfd
1772  * @match_data: whether to match against @data, instead of just @addr
1773  * @data: the data to match against the guest write
1774  * @e: event notifier to be triggered when @addr, @size, and @data all match.
1775  **/
1776 void memory_region_add_eventfd(MemoryRegion *mr,
1777                                hwaddr addr,
1778                                unsigned size,
1779                                bool match_data,
1780                                uint64_t data,
1781                                EventNotifier *e);
1782 
1783 /**
1784  * memory_region_del_eventfd: Cancel an eventfd.
1785  *
1786  * Cancels an eventfd trigger requested by a previous
1787  * memory_region_add_eventfd() call.
1788  *
1789  * @mr: the memory region being updated.
1790  * @addr: the address within @mr that is to be monitored
1791  * @size: the size of the access to trigger the eventfd
1792  * @match_data: whether to match against @data, instead of just @addr
1793  * @data: the data to match against the guest write
1794  * @e: event notifier to be triggered when @addr, @size, and @data all match.
1795  */
1796 void memory_region_del_eventfd(MemoryRegion *mr,
1797                                hwaddr addr,
1798                                unsigned size,
1799                                bool match_data,
1800                                uint64_t data,
1801                                EventNotifier *e);
1802 
1803 /**
1804  * memory_region_add_subregion: Add a subregion to a container.
1805  *
1806  * Adds a subregion at @offset.  The subregion may not overlap with other
1807  * subregions (except for those explicitly marked as overlapping).  A region
1808  * may only be added once as a subregion (unless removed with
1809  * memory_region_del_subregion()); use memory_region_init_alias() if you
1810  * want a region to be a subregion in multiple locations.
1811  *
1812  * @mr: the region to contain the new subregion; must be a container
1813  *      initialized with memory_region_init().
1814  * @offset: the offset relative to @mr where @subregion is added.
1815  * @subregion: the subregion to be added.
1816  */
1817 void memory_region_add_subregion(MemoryRegion *mr,
1818                                  hwaddr offset,
1819                                  MemoryRegion *subregion);
1820 /**
1821  * memory_region_add_subregion_overlap: Add a subregion to a container
1822  *                                      with overlap.
1823  *
1824  * Adds a subregion at @offset.  The subregion may overlap with other
1825  * subregions.  Conflicts are resolved by having a higher @priority hide a
1826  * lower @priority. Subregions without priority are taken as @priority 0.
1827  * A region may only be added once as a subregion (unless removed with
1828  * memory_region_del_subregion()); use memory_region_init_alias() if you
1829  * want a region to be a subregion in multiple locations.
1830  *
1831  * @mr: the region to contain the new subregion; must be a container
1832  *      initialized with memory_region_init().
1833  * @offset: the offset relative to @mr where @subregion is added.
1834  * @subregion: the subregion to be added.
1835  * @priority: used for resolving overlaps; highest priority wins.
1836  */
1837 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1838                                          hwaddr offset,
1839                                          MemoryRegion *subregion,
1840                                          int priority);
1841 
1842 /**
1843  * memory_region_get_ram_addr: Get the ram address associated with a memory
1844  *                             region
1845  *
1846  * @mr: the region to be queried
1847  */
1848 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
1849 
1850 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
1851 /**
1852  * memory_region_del_subregion: Remove a subregion.
1853  *
1854  * Removes a subregion from its container.
1855  *
1856  * @mr: the container to be updated.
1857  * @subregion: the region being removed; must be a current subregion of @mr.
1858  */
1859 void memory_region_del_subregion(MemoryRegion *mr,
1860                                  MemoryRegion *subregion);
1861 
1862 /*
1863  * memory_region_set_enabled: dynamically enable or disable a region
1864  *
1865  * Enables or disables a memory region.  A disabled memory region
1866  * ignores all accesses to itself and its subregions.  It does not
1867  * obscure sibling subregions with lower priority - it simply behaves as
1868  * if it was removed from the hierarchy.
1869  *
1870  * Regions default to being enabled.
1871  *
1872  * @mr: the region to be updated
1873  * @enabled: whether to enable or disable the region
1874  */
1875 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1876 
1877 /*
1878  * memory_region_set_address: dynamically update the address of a region
1879  *
1880  * Dynamically updates the address of a region, relative to its container.
1881  * May be used on regions are currently part of a memory hierarchy.
1882  *
1883  * @mr: the region to be updated
1884  * @addr: new address, relative to container region
1885  */
1886 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
1887 
1888 /*
1889  * memory_region_set_size: dynamically update the size of a region.
1890  *
1891  * Dynamically updates the size of a region.
1892  *
1893  * @mr: the region to be updated
1894  * @size: used size of the region.
1895  */
1896 void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1897 
1898 /*
1899  * memory_region_set_alias_offset: dynamically update a memory alias's offset
1900  *
1901  * Dynamically updates the offset into the target region that an alias points
1902  * to, as if the fourth argument to memory_region_init_alias() has changed.
1903  *
1904  * @mr: the #MemoryRegion to be updated; should be an alias.
1905  * @offset: the new offset into the target memory region
1906  */
1907 void memory_region_set_alias_offset(MemoryRegion *mr,
1908                                     hwaddr offset);
1909 
1910 /**
1911  * memory_region_present: checks if an address relative to a @container
1912  * translates into #MemoryRegion within @container
1913  *
1914  * Answer whether a #MemoryRegion within @container covers the address
1915  * @addr.
1916  *
1917  * @container: a #MemoryRegion within which @addr is a relative address
1918  * @addr: the area within @container to be searched
1919  */
1920 bool memory_region_present(MemoryRegion *container, hwaddr addr);
1921 
1922 /**
1923  * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1924  * into any address space.
1925  *
1926  * @mr: a #MemoryRegion which should be checked if it's mapped
1927  */
1928 bool memory_region_is_mapped(MemoryRegion *mr);
1929 
1930 /**
1931  * memory_region_find: translate an address/size relative to a
1932  * MemoryRegion into a #MemoryRegionSection.
1933  *
1934  * Locates the first #MemoryRegion within @mr that overlaps the range
1935  * given by @addr and @size.
1936  *
1937  * Returns a #MemoryRegionSection that describes a contiguous overlap.
1938  * It will have the following characteristics:
1939  * - @size = 0 iff no overlap was found
1940  * - @mr is non-%NULL iff an overlap was found
1941  *
1942  * Remember that in the return value the @offset_within_region is
1943  * relative to the returned region (in the .@mr field), not to the
1944  * @mr argument.
1945  *
1946  * Similarly, the .@offset_within_address_space is relative to the
1947  * address space that contains both regions, the passed and the
1948  * returned one.  However, in the special case where the @mr argument
1949  * has no container (and thus is the root of the address space), the
1950  * following will hold:
1951  * - @offset_within_address_space >= @addr
1952  * - @offset_within_address_space + .@size <= @addr + @size
1953  *
1954  * @mr: a MemoryRegion within which @addr is a relative address
1955  * @addr: start of the area within @as to be searched
1956  * @size: size of the area to be searched
1957  */
1958 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1959                                        hwaddr addr, uint64_t size);
1960 
1961 /**
1962  * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1963  *
1964  * Synchronizes the dirty page log for all address spaces.
1965  */
1966 void memory_global_dirty_log_sync(void);
1967 
1968 /**
1969  * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1970  *
1971  * Synchronizes the vCPUs with a thread that is reading the dirty bitmap.
1972  * This function must be called after the dirty log bitmap is cleared, and
1973  * before dirty guest memory pages are read.  If you are using
1974  * #DirtyBitmapSnapshot, memory_region_snapshot_and_clear_dirty() takes
1975  * care of doing this.
1976  */
1977 void memory_global_after_dirty_log_sync(void);
1978 
1979 /**
1980  * memory_region_transaction_begin: Start a transaction.
1981  *
1982  * During a transaction, changes will be accumulated and made visible
1983  * only when the transaction ends (is committed).
1984  */
1985 void memory_region_transaction_begin(void);
1986 
1987 /**
1988  * memory_region_transaction_commit: Commit a transaction and make changes
1989  *                                   visible to the guest.
1990  */
1991 void memory_region_transaction_commit(void);
1992 
1993 /**
1994  * memory_listener_register: register callbacks to be called when memory
1995  *                           sections are mapped or unmapped into an address
1996  *                           space
1997  *
1998  * @listener: an object containing the callbacks to be called
1999  * @filter: if non-%NULL, only regions in this address space will be observed
2000  */
2001 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
2002 
2003 /**
2004  * memory_listener_unregister: undo the effect of memory_listener_register()
2005  *
2006  * @listener: an object containing the callbacks to be removed
2007  */
2008 void memory_listener_unregister(MemoryListener *listener);
2009 
2010 /**
2011  * memory_global_dirty_log_start: begin dirty logging for all regions
2012  */
2013 void memory_global_dirty_log_start(void);
2014 
2015 /**
2016  * memory_global_dirty_log_stop: end dirty logging for all regions
2017  */
2018 void memory_global_dirty_log_stop(void);
2019 
2020 void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled);
2021 
2022 /**
2023  * memory_region_dispatch_read: perform a read directly to the specified
2024  * MemoryRegion.
2025  *
2026  * @mr: #MemoryRegion to access
2027  * @addr: address within that region
2028  * @pval: pointer to uint64_t which the data is written to
2029  * @op: size, sign, and endianness of the memory operation
2030  * @attrs: memory transaction attributes to use for the access
2031  */
2032 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
2033                                         hwaddr addr,
2034                                         uint64_t *pval,
2035                                         MemOp op,
2036                                         MemTxAttrs attrs);
2037 /**
2038  * memory_region_dispatch_write: perform a write directly to the specified
2039  * MemoryRegion.
2040  *
2041  * @mr: #MemoryRegion to access
2042  * @addr: address within that region
2043  * @data: data to write
2044  * @op: size, sign, and endianness of the memory operation
2045  * @attrs: memory transaction attributes to use for the access
2046  */
2047 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
2048                                          hwaddr addr,
2049                                          uint64_t data,
2050                                          MemOp op,
2051                                          MemTxAttrs attrs);
2052 
2053 /**
2054  * address_space_init: initializes an address space
2055  *
2056  * @as: an uninitialized #AddressSpace
2057  * @root: a #MemoryRegion that routes addresses for the address space
2058  * @name: an address space name.  The name is only used for debugging
2059  *        output.
2060  */
2061 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
2062 
2063 /**
2064  * address_space_destroy: destroy an address space
2065  *
2066  * Releases all resources associated with an address space.  After an address space
2067  * is destroyed, its root memory region (given by address_space_init()) may be destroyed
2068  * as well.
2069  *
2070  * @as: address space to be destroyed
2071  */
2072 void address_space_destroy(AddressSpace *as);
2073 
2074 /**
2075  * address_space_remove_listeners: unregister all listeners of an address space
2076  *
2077  * Removes all callbacks previously registered with memory_listener_register()
2078  * for @as.
2079  *
2080  * @as: an initialized #AddressSpace
2081  */
2082 void address_space_remove_listeners(AddressSpace *as);
2083 
2084 /**
2085  * address_space_rw: read from or write to an address space.
2086  *
2087  * Return a MemTxResult indicating whether the operation succeeded
2088  * or failed (eg unassigned memory, device rejected the transaction,
2089  * IOMMU fault).
2090  *
2091  * @as: #AddressSpace to be accessed
2092  * @addr: address within that address space
2093  * @attrs: memory transaction attributes
2094  * @buf: buffer with the data transferred
2095  * @len: the number of bytes to read or write
2096  * @is_write: indicates the transfer direction
2097  */
2098 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
2099                              MemTxAttrs attrs, void *buf,
2100                              hwaddr len, bool is_write);
2101 
2102 /**
2103  * address_space_write: write to address space.
2104  *
2105  * Return a MemTxResult indicating whether the operation succeeded
2106  * or failed (eg unassigned memory, device rejected the transaction,
2107  * IOMMU fault).
2108  *
2109  * @as: #AddressSpace to be accessed
2110  * @addr: address within that address space
2111  * @attrs: memory transaction attributes
2112  * @buf: buffer with the data transferred
2113  * @len: the number of bytes to write
2114  */
2115 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
2116                                 MemTxAttrs attrs,
2117                                 const void *buf, hwaddr len);
2118 
2119 /**
2120  * address_space_write_rom: write to address space, including ROM.
2121  *
2122  * This function writes to the specified address space, but will
2123  * write data to both ROM and RAM. This is used for non-guest
2124  * writes like writes from the gdb debug stub or initial loading
2125  * of ROM contents.
2126  *
2127  * Note that portions of the write which attempt to write data to
2128  * a device will be silently ignored -- only real RAM and ROM will
2129  * be written to.
2130  *
2131  * Return a MemTxResult indicating whether the operation succeeded
2132  * or failed (eg unassigned memory, device rejected the transaction,
2133  * IOMMU fault).
2134  *
2135  * @as: #AddressSpace to be accessed
2136  * @addr: address within that address space
2137  * @attrs: memory transaction attributes
2138  * @buf: buffer with the data transferred
2139  * @len: the number of bytes to write
2140  */
2141 MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
2142                                     MemTxAttrs attrs,
2143                                     const void *buf, hwaddr len);
2144 
2145 /* address_space_ld*: load from an address space
2146  * address_space_st*: store to an address space
2147  *
2148  * These functions perform a load or store of the byte, word,
2149  * longword or quad to the specified address within the AddressSpace.
2150  * The _le suffixed functions treat the data as little endian;
2151  * _be indicates big endian; no suffix indicates "same endianness
2152  * as guest CPU".
2153  *
2154  * The "guest CPU endianness" accessors are deprecated for use outside
2155  * target-* code; devices should be CPU-agnostic and use either the LE
2156  * or the BE accessors.
2157  *
2158  * @as #AddressSpace to be accessed
2159  * @addr: address within that address space
2160  * @val: data value, for stores
2161  * @attrs: memory transaction attributes
2162  * @result: location to write the success/failure of the transaction;
2163  *   if NULL, this information is discarded
2164  */
2165 
2166 #define SUFFIX
2167 #define ARG1         as
2168 #define ARG1_DECL    AddressSpace *as
2169 #include "exec/memory_ldst.h.inc"
2170 
2171 #define SUFFIX
2172 #define ARG1         as
2173 #define ARG1_DECL    AddressSpace *as
2174 #include "exec/memory_ldst_phys.h.inc"
2175 
2176 struct MemoryRegionCache {
2177     void *ptr;
2178     hwaddr xlat;
2179     hwaddr len;
2180     FlatView *fv;
2181     MemoryRegionSection mrs;
2182     bool is_write;
2183 };
2184 
2185 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
2186 
2187 
2188 /* address_space_ld*_cached: load from a cached #MemoryRegion
2189  * address_space_st*_cached: store into a cached #MemoryRegion
2190  *
2191  * These functions perform a load or store of the byte, word,
2192  * longword or quad to the specified address.  The address is
2193  * a physical address in the AddressSpace, but it must lie within
2194  * a #MemoryRegion that was mapped with address_space_cache_init.
2195  *
2196  * The _le suffixed functions treat the data as little endian;
2197  * _be indicates big endian; no suffix indicates "same endianness
2198  * as guest CPU".
2199  *
2200  * The "guest CPU endianness" accessors are deprecated for use outside
2201  * target-* code; devices should be CPU-agnostic and use either the LE
2202  * or the BE accessors.
2203  *
2204  * @cache: previously initialized #MemoryRegionCache to be accessed
2205  * @addr: address within the address space
2206  * @val: data value, for stores
2207  * @attrs: memory transaction attributes
2208  * @result: location to write the success/failure of the transaction;
2209  *   if NULL, this information is discarded
2210  */
2211 
2212 #define SUFFIX       _cached_slow
2213 #define ARG1         cache
2214 #define ARG1_DECL    MemoryRegionCache *cache
2215 #include "exec/memory_ldst.h.inc"
2216 
2217 /* Inline fast path for direct RAM access.  */
2218 static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache,
2219     hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
2220 {
2221     assert(addr < cache->len);
2222     if (likely(cache->ptr)) {
2223         return ldub_p(cache->ptr + addr);
2224     } else {
2225         return address_space_ldub_cached_slow(cache, addr, attrs, result);
2226     }
2227 }
2228 
2229 static inline void address_space_stb_cached(MemoryRegionCache *cache,
2230     hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result)
2231 {
2232     assert(addr < cache->len);
2233     if (likely(cache->ptr)) {
2234         stb_p(cache->ptr + addr, val);
2235     } else {
2236         address_space_stb_cached_slow(cache, addr, val, attrs, result);
2237     }
2238 }
2239 
2240 #define ENDIANNESS   _le
2241 #include "exec/memory_ldst_cached.h.inc"
2242 
2243 #define ENDIANNESS   _be
2244 #include "exec/memory_ldst_cached.h.inc"
2245 
2246 #define SUFFIX       _cached
2247 #define ARG1         cache
2248 #define ARG1_DECL    MemoryRegionCache *cache
2249 #include "exec/memory_ldst_phys.h.inc"
2250 
2251 /* address_space_cache_init: prepare for repeated access to a physical
2252  * memory region
2253  *
2254  * @cache: #MemoryRegionCache to be filled
2255  * @as: #AddressSpace to be accessed
2256  * @addr: address within that address space
2257  * @len: length of buffer
2258  * @is_write: indicates the transfer direction
2259  *
2260  * Will only work with RAM, and may map a subset of the requested range by
2261  * returning a value that is less than @len.  On failure, return a negative
2262  * errno value.
2263  *
2264  * Because it only works with RAM, this function can be used for
2265  * read-modify-write operations.  In this case, is_write should be %true.
2266  *
2267  * Note that addresses passed to the address_space_*_cached functions
2268  * are relative to @addr.
2269  */
2270 int64_t address_space_cache_init(MemoryRegionCache *cache,
2271                                  AddressSpace *as,
2272                                  hwaddr addr,
2273                                  hwaddr len,
2274                                  bool is_write);
2275 
2276 /**
2277  * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
2278  *
2279  * @cache: The #MemoryRegionCache to operate on.
2280  * @addr: The first physical address that was written, relative to the
2281  * address that was passed to @address_space_cache_init.
2282  * @access_len: The number of bytes that were written starting at @addr.
2283  */
2284 void address_space_cache_invalidate(MemoryRegionCache *cache,
2285                                     hwaddr addr,
2286                                     hwaddr access_len);
2287 
2288 /**
2289  * address_space_cache_destroy: free a #MemoryRegionCache
2290  *
2291  * @cache: The #MemoryRegionCache whose memory should be released.
2292  */
2293 void address_space_cache_destroy(MemoryRegionCache *cache);
2294 
2295 /* address_space_get_iotlb_entry: translate an address into an IOTLB
2296  * entry. Should be called from an RCU critical section.
2297  */
2298 IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
2299                                             bool is_write, MemTxAttrs attrs);
2300 
2301 /* address_space_translate: translate an address range into an address space
2302  * into a MemoryRegion and an address range into that section.  Should be
2303  * called from an RCU critical section, to avoid that the last reference
2304  * to the returned region disappears after address_space_translate returns.
2305  *
2306  * @fv: #FlatView to be accessed
2307  * @addr: address within that address space
2308  * @xlat: pointer to address within the returned memory region section's
2309  * #MemoryRegion.
2310  * @len: pointer to length
2311  * @is_write: indicates the transfer direction
2312  * @attrs: memory attributes
2313  */
2314 MemoryRegion *flatview_translate(FlatView *fv,
2315                                  hwaddr addr, hwaddr *xlat,
2316                                  hwaddr *len, bool is_write,
2317                                  MemTxAttrs attrs);
2318 
2319 static inline MemoryRegion *address_space_translate(AddressSpace *as,
2320                                                     hwaddr addr, hwaddr *xlat,
2321                                                     hwaddr *len, bool is_write,
2322                                                     MemTxAttrs attrs)
2323 {
2324     return flatview_translate(address_space_to_flatview(as),
2325                               addr, xlat, len, is_write, attrs);
2326 }
2327 
2328 /* address_space_access_valid: check for validity of accessing an address
2329  * space range
2330  *
2331  * Check whether memory is assigned to the given address space range, and
2332  * access is permitted by any IOMMU regions that are active for the address
2333  * space.
2334  *
2335  * For now, addr and len should be aligned to a page size.  This limitation
2336  * will be lifted in the future.
2337  *
2338  * @as: #AddressSpace to be accessed
2339  * @addr: address within that address space
2340  * @len: length of the area to be checked
2341  * @is_write: indicates the transfer direction
2342  * @attrs: memory attributes
2343  */
2344 bool address_space_access_valid(AddressSpace *as, hwaddr addr, hwaddr len,
2345                                 bool is_write, MemTxAttrs attrs);
2346 
2347 /* address_space_map: map a physical memory region into a host virtual address
2348  *
2349  * May map a subset of the requested range, given by and returned in @plen.
2350  * May return %NULL and set *@plen to zero(0), if resources needed to perform
2351  * the mapping are exhausted.
2352  * Use only for reads OR writes - not for read-modify-write operations.
2353  * Use cpu_register_map_client() to know when retrying the map operation is
2354  * likely to succeed.
2355  *
2356  * @as: #AddressSpace to be accessed
2357  * @addr: address within that address space
2358  * @plen: pointer to length of buffer; updated on return
2359  * @is_write: indicates the transfer direction
2360  * @attrs: memory attributes
2361  */
2362 void *address_space_map(AddressSpace *as, hwaddr addr,
2363                         hwaddr *plen, bool is_write, MemTxAttrs attrs);
2364 
2365 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2366  *
2367  * Will also mark the memory as dirty if @is_write == %true.  @access_len gives
2368  * the amount of memory that was actually read or written by the caller.
2369  *
2370  * @as: #AddressSpace used
2371  * @buffer: host pointer as returned by address_space_map()
2372  * @len: buffer length as returned by address_space_map()
2373  * @access_len: amount of data actually transferred
2374  * @is_write: indicates the transfer direction
2375  */
2376 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2377                          bool is_write, hwaddr access_len);
2378 
2379 
2380 /* Internal functions, part of the implementation of address_space_read.  */
2381 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
2382                                     MemTxAttrs attrs, void *buf, hwaddr len);
2383 MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
2384                                    MemTxAttrs attrs, void *buf,
2385                                    hwaddr len, hwaddr addr1, hwaddr l,
2386                                    MemoryRegion *mr);
2387 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
2388 
2389 /* Internal functions, part of the implementation of address_space_read_cached
2390  * and address_space_write_cached.  */
2391 MemTxResult address_space_read_cached_slow(MemoryRegionCache *cache,
2392                                            hwaddr addr, void *buf, hwaddr len);
2393 MemTxResult address_space_write_cached_slow(MemoryRegionCache *cache,
2394                                             hwaddr addr, const void *buf,
2395                                             hwaddr len);
2396 
2397 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
2398 {
2399     if (is_write) {
2400         return memory_region_is_ram(mr) && !mr->readonly &&
2401                !mr->rom_device && !memory_region_is_ram_device(mr);
2402     } else {
2403         return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
2404                memory_region_is_romd(mr);
2405     }
2406 }
2407 
2408 /**
2409  * address_space_read: read from an address space.
2410  *
2411  * Return a MemTxResult indicating whether the operation succeeded
2412  * or failed (eg unassigned memory, device rejected the transaction,
2413  * IOMMU fault).  Called within RCU critical section.
2414  *
2415  * @as: #AddressSpace to be accessed
2416  * @addr: address within that address space
2417  * @attrs: memory transaction attributes
2418  * @buf: buffer with the data transferred
2419  * @len: length of the data transferred
2420  */
2421 static inline __attribute__((__always_inline__))
2422 MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
2423                                MemTxAttrs attrs, void *buf,
2424                                hwaddr len)
2425 {
2426     MemTxResult result = MEMTX_OK;
2427     hwaddr l, addr1;
2428     void *ptr;
2429     MemoryRegion *mr;
2430     FlatView *fv;
2431 
2432     if (__builtin_constant_p(len)) {
2433         if (len) {
2434             RCU_READ_LOCK_GUARD();
2435             fv = address_space_to_flatview(as);
2436             l = len;
2437             mr = flatview_translate(fv, addr, &addr1, &l, false, attrs);
2438             if (len == l && memory_access_is_direct(mr, false)) {
2439                 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
2440                 memcpy(buf, ptr, len);
2441             } else {
2442                 result = flatview_read_continue(fv, addr, attrs, buf, len,
2443                                                 addr1, l, mr);
2444             }
2445         }
2446     } else {
2447         result = address_space_read_full(as, addr, attrs, buf, len);
2448     }
2449     return result;
2450 }
2451 
2452 /**
2453  * address_space_read_cached: read from a cached RAM region
2454  *
2455  * @cache: Cached region to be addressed
2456  * @addr: address relative to the base of the RAM region
2457  * @buf: buffer with the data transferred
2458  * @len: length of the data transferred
2459  */
2460 static inline MemTxResult
2461 address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
2462                           void *buf, hwaddr len)
2463 {
2464     assert(addr < cache->len && len <= cache->len - addr);
2465     fuzz_dma_read_cb(cache->xlat + addr, len, cache->mrs.mr, false);
2466     if (likely(cache->ptr)) {
2467         memcpy(buf, cache->ptr + addr, len);
2468         return MEMTX_OK;
2469     } else {
2470         return address_space_read_cached_slow(cache, addr, buf, len);
2471     }
2472 }
2473 
2474 /**
2475  * address_space_write_cached: write to a cached RAM region
2476  *
2477  * @cache: Cached region to be addressed
2478  * @addr: address relative to the base of the RAM region
2479  * @buf: buffer with the data transferred
2480  * @len: length of the data transferred
2481  */
2482 static inline MemTxResult
2483 address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
2484                            const void *buf, hwaddr len)
2485 {
2486     assert(addr < cache->len && len <= cache->len - addr);
2487     if (likely(cache->ptr)) {
2488         memcpy(cache->ptr + addr, buf, len);
2489         return MEMTX_OK;
2490     } else {
2491         return address_space_write_cached_slow(cache, addr, buf, len);
2492     }
2493 }
2494 
2495 #ifdef NEED_CPU_H
2496 /* enum device_endian to MemOp.  */
2497 static inline MemOp devend_memop(enum device_endian end)
2498 {
2499     QEMU_BUILD_BUG_ON(DEVICE_HOST_ENDIAN != DEVICE_LITTLE_ENDIAN &&
2500                       DEVICE_HOST_ENDIAN != DEVICE_BIG_ENDIAN);
2501 
2502 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
2503     /* Swap if non-host endianness or native (target) endianness */
2504     return (end == DEVICE_HOST_ENDIAN) ? 0 : MO_BSWAP;
2505 #else
2506     const int non_host_endianness =
2507         DEVICE_LITTLE_ENDIAN ^ DEVICE_BIG_ENDIAN ^ DEVICE_HOST_ENDIAN;
2508 
2509     /* In this case, native (target) endianness needs no swap.  */
2510     return (end == non_host_endianness) ? MO_BSWAP : 0;
2511 #endif
2512 }
2513 #endif
2514 
2515 /*
2516  * Inhibit technologies that require discarding of pages in RAM blocks, e.g.,
2517  * to manage the actual amount of memory consumed by the VM (then, the memory
2518  * provided by RAM blocks might be bigger than the desired memory consumption).
2519  * This *must* be set if:
2520  * - Discarding parts of a RAM blocks does not result in the change being
2521  *   reflected in the VM and the pages getting freed.
2522  * - All memory in RAM blocks is pinned or duplicated, invaldiating any previous
2523  *   discards blindly.
2524  * - Discarding parts of a RAM blocks will result in integrity issues (e.g.,
2525  *   encrypted VMs).
2526  * Technologies that only temporarily pin the current working set of a
2527  * driver are fine, because we don't expect such pages to be discarded
2528  * (esp. based on guest action like balloon inflation).
2529  *
2530  * This is *not* to be used to protect from concurrent discards (esp.,
2531  * postcopy).
2532  *
2533  * Returns 0 if successful. Returns -EBUSY if a technology that relies on
2534  * discards to work reliably is active.
2535  */
2536 int ram_block_discard_disable(bool state);
2537 
2538 /*
2539  * Inhibit technologies that disable discarding of pages in RAM blocks.
2540  *
2541  * Returns 0 if successful. Returns -EBUSY if discards are already set to
2542  * broken.
2543  */
2544 int ram_block_discard_require(bool state);
2545 
2546 /*
2547  * Test if discarding of memory in ram blocks is disabled.
2548  */
2549 bool ram_block_discard_is_disabled(void);
2550 
2551 /*
2552  * Test if discarding of memory in ram blocks is required to work reliably.
2553  */
2554 bool ram_block_discard_is_required(void);
2555 
2556 #endif
2557 
2558 #endif
2559