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