]> git.proxmox.com Git - mirror_qemu.git/blame - include/exec/memory.h
migration: Add last stage indicator to global dirty log
[mirror_qemu.git] / include / exec / memory.h
CommitLineData
093bc2cd
AK
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
022c62cb
PB
19#include "exec/cpu-common.h"
20#include "exec/hwaddr.h"
cc05c43a 21#include "exec/memattrs.h"
e67c9046 22#include "exec/memop.h"
0987d735 23#include "exec/ramlist.h"
1b53ecd9 24#include "qemu/bswap.h"
1de7afc9 25#include "qemu/queue.h"
1de7afc9 26#include "qemu/int128.h"
06866575 27#include "qemu/notify.h"
b4fefef9 28#include "qom/object.h"
374f2981 29#include "qemu/rcu.h"
093bc2cd 30
07bdaa41
PB
31#define RAM_ADDR_INVALID (~(ram_addr_t)0)
32
052e87b0
PB
33#define MAX_PHYS_ADDR_SPACE_BITS 62
34#define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
35
bb3c92ed 36#define TYPE_MEMORY_REGION "memory-region"
8110fa1d
EH
37DECLARE_INSTANCE_CHECKER(MemoryRegion, MEMORY_REGION,
38 TYPE_MEMORY_REGION)
b4fefef9 39
bb3c92ed 40#define TYPE_IOMMU_MEMORY_REGION "iommu-memory-region"
db1015e9 41typedef struct IOMMUMemoryRegionClass IOMMUMemoryRegionClass;
8110fa1d
EH
42DECLARE_OBJ_CHECKERS(IOMMUMemoryRegion, IOMMUMemoryRegionClass,
43 IOMMU_MEMORY_REGION, TYPE_IOMMU_MEMORY_REGION)
3df9d748 44
8947d7fc
DH
45#define TYPE_RAM_DISCARD_MANAGER "qemu:ram-discard-manager"
46typedef struct RamDiscardManagerClass RamDiscardManagerClass;
47typedef struct RamDiscardManager RamDiscardManager;
48DECLARE_OBJ_CHECKERS(RamDiscardManager, RamDiscardManagerClass,
49 RAM_DISCARD_MANAGER, TYPE_RAM_DISCARD_MANAGER);
50
20f5a302
AB
51#ifdef CONFIG_FUZZ
52void fuzz_dma_read_cb(size_t addr,
53 size_t len,
fc1c8344 54 MemoryRegion *mr);
e7d3222e
AB
55#else
56static inline void fuzz_dma_read_cb(size_t addr,
57 size_t len,
fc1c8344 58 MemoryRegion *mr)
e7d3222e
AB
59{
60 /* Do Nothing */
61}
20f5a302
AB
62#endif
63
63b41db4
HH
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
cc2b33ea
HH
72/* Dirty tracking enabled because dirty limit */
73#define GLOBAL_DIRTY_LIMIT (1U << 2)
74
75#define GLOBAL_DIRTY_MASK (0x7)
63b41db4
HH
76
77extern unsigned int global_dirty_tracking;
ae7a2bca 78
093bc2cd 79typedef struct MemoryRegionOps MemoryRegionOps;
74901c3b 80
f7806925
EA
81struct ReservedRegion {
82 hwaddr low;
83 hwaddr high;
84 unsigned type;
85};
86
8947d7fc
DH
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 */
99struct 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
30951157
AK
109typedef struct IOMMUTLBEntry IOMMUTLBEntry;
110
111/* See address_space_translate: bit 0 is read, bit 1 is write. */
112typedef enum {
113 IOMMU_NONE = 0,
114 IOMMU_RO = 1,
115 IOMMU_WO = 2,
116 IOMMU_RW = 3,
117} IOMMUAccessFlags;
118
f06a696d
PX
119#define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
120
30951157
AK
121struct IOMMUTLBEntry {
122 AddressSpace *target_as;
123 hwaddr iova;
124 hwaddr translated_addr;
125 hwaddr addr_mask; /* 0xfff = 4k translation */
126 IOMMUAccessFlags perm;
127};
128
cdb30812
PX
129/*
130 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
131 * register with one or multiple IOMMU Notifier capability bit(s).
8a7c6060
PX
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.
cdb30812
PX
158 */
159typedef 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,
b68ba1ca
EP
165 /* Notify changes on device IOTLB entries */
166 IOMMU_NOTIFIER_DEVIOTLB_UNMAP = 0x04,
cdb30812
PX
167} IOMMUNotifierFlag;
168
b68ba1ca
EP
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)
cdb30812 173
698feb5e
PX
174struct IOMMUNotifier;
175typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier,
176 IOMMUTLBEntry *data);
177
cdb30812 178struct IOMMUNotifier {
698feb5e 179 IOMMUNotify notify;
cdb30812 180 IOMMUNotifierFlag notifier_flags;
698feb5e
PX
181 /* Notify for address space range start <= addr <= end */
182 hwaddr start;
183 hwaddr end;
cb1efcf4 184 int iommu_idx;
cdb30812
PX
185 QLIST_ENTRY(IOMMUNotifier) node;
186};
187typedef struct IOMMUNotifier IOMMUNotifier;
188
5039caf3
EP
189typedef struct IOMMUTLBEvent {
190 IOMMUNotifierFlag type;
191 IOMMUTLBEntry entry;
192} IOMMUTLBEvent;
193
b0e5de93
JH
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.
c7c0e724 201 * Resizing RAM while migrating can result in the migration being canceled.
b0e5de93
JH
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
a4de8552
JH
214/* RAM is a persistent kind memory */
215#define RAM_PMEM (1 << 5)
216
0e9b5cd6 217
278e2f55
AG
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
8dbe22c6
DH
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
56918a12
SC
232/* RAM that isn't accessible through normal means. */
233#define RAM_PROTECTED (1 << 8)
234
698feb5e
PX
235static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
236 IOMMUNotifierFlag flags,
cb1efcf4
PM
237 hwaddr start, hwaddr end,
238 int iommu_idx)
698feb5e
PX
239{
240 n->notify = fn;
241 n->notifier_flags = flags;
242 n->start = start;
243 n->end = end;
cb1efcf4 244 n->iommu_idx = iommu_idx;
698feb5e
PX
245}
246
093bc2cd
AK
247/*
248 * Memory region callbacks
249 */
250struct MemoryRegionOps {
251 /* Read from the memory region. @addr is relative to @mr; @size is
252 * in bytes. */
253 uint64_t (*read)(void *opaque,
a8170e5e 254 hwaddr addr,
093bc2cd
AK
255 unsigned size);
256 /* Write to the memory region. @addr is relative to @mr; @size is
257 * in bytes. */
258 void (*write)(void *opaque,
a8170e5e 259 hwaddr addr,
093bc2cd
AK
260 uint64_t data,
261 unsigned size);
262
cc05c43a
PM
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
093bc2cd
AK
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;
897fa7cf
AK
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 */
a8170e5e 291 bool (*accepts)(void *opaque, hwaddr addr,
8372d383
PM
292 unsigned size, bool is_write,
293 MemTxAttrs attrs);
093bc2cd
AK
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 */
edc1ba7a 308 bool unaligned;
093bc2cd
AK
309 } impl;
310};
311
1b53ecd9
MA
312typedef struct MemoryRegionClass {
313 /* private */
314 ObjectClass parent_class;
315} MemoryRegionClass;
316
317
f1334de6
AK
318enum IOMMUMemoryRegionAttr {
319 IOMMU_ATTR_SPAPR_TCE_FD
320};
321
acbef3cc 322/*
2ce931d0
PM
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
3b5ebf85 332 * memory_region_notify_iommu_one() for each registered notifier).
21f40209
PM
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:
ffb716f0 339 *
21f40209 340 * @attrs_to_index returns the IOMMU index for a set of transaction attributes
ffb716f0 341 *
21f40209 342 * @translate takes an input address and an IOMMU index
ffb716f0 343 *
21f40209
PM
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.
2ce931d0 350 */
db1015e9 351struct IOMMUMemoryRegionClass {
ffb716f0 352 /* private: */
1b53ecd9 353 MemoryRegionClass parent_class;
30951157 354
ffb716f0
EH
355 /* public: */
356 /**
357 * @translate:
358 *
2ce931d0
PM
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
ffb716f0 379 *
2ce931d0 380 * @hwaddr: address to be translated within the memory region
ffb716f0
EH
381 *
382 * @flag: requested access permission
383 *
2c91bcf2 384 * @iommu_idx: IOMMU index for the translation
bf55b7af 385 */
3df9d748 386 IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr,
2c91bcf2 387 IOMMUAccessFlags flag, int iommu_idx);
ffb716f0
EH
388 /**
389 * @get_min_page_size:
390 *
391 * Returns minimum supported page size in bytes.
392 *
2ce931d0
PM
393 * If this method is not provided then the minimum is assumed to
394 * be TARGET_PAGE_SIZE.
395 *
396 * @iommu: the IOMMUMemoryRegion
397 */
3df9d748 398 uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu);
ffb716f0
EH
399 /**
400 * @notify_flag_changed:
401 *
402 * Called when IOMMU Notifier flag changes (ie when the set of
2ce931d0
PM
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
ffb716f0 408 *
2ce931d0 409 * @old_flags: events which previously needed to be notified
ffb716f0 410 *
2ce931d0 411 * @new_flags: events which now need to be notified
549d4005
EA
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
2ce931d0 417 */
549d4005
EA
418 int (*notify_flag_changed)(IOMMUMemoryRegion *iommu,
419 IOMMUNotifierFlag old_flags,
420 IOMMUNotifierFlag new_flags,
421 Error **errp);
ffb716f0
EH
422 /**
423 * @replay:
424 *
425 * Called to handle memory_region_iommu_replay().
2ce931d0
PM
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 */
3df9d748 440 void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier);
f1334de6 441
ffb716f0
EH
442 /**
443 * @get_attr:
444 *
445 * Get IOMMU misc attributes. This is an optional method that
2ce931d0
PM
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
ffb716f0 454 *
2ce931d0 455 * @attr: attribute being queried
ffb716f0 456 *
2ce931d0
PM
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,
f1334de6 463 void *data);
21f40209 464
ffb716f0
EH
465 /**
466 * @attrs_to_index:
467 *
468 * Return the IOMMU index to use for a given set of transaction attributes.
21f40209
PM
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
ffb716f0
EH
481 /**
482 * @num_indexes:
483 *
484 * Return the number of IOMMU indexes this IOMMU supports.
21f40209
PM
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);
457f8cbb
BB
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);
db1015e9 519};
30951157 520
8947d7fc
DH
521typedef struct RamDiscardListener RamDiscardListener;
522typedef int (*NotifyRamPopulate)(RamDiscardListener *rdl,
523 MemoryRegionSection *section);
524typedef void (*NotifyRamDiscard)(RamDiscardListener *rdl,
525 MemoryRegionSection *section);
526
527struct 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
571static 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
581typedef int (*ReplayRamPopulate)(MemoryRegionSection *section, void *opaque);
adaf9d92 582typedef void (*ReplayRamDiscard)(MemoryRegionSection *section, void *opaque);
8947d7fc
DH
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
2cb40d44 590 * populated (consuming memory), to be used/accessed by the VM.
8947d7fc
DH
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
2cb40d44 614 * becoming discarded in a different granularity than it was populated and the
8947d7fc
DH
615 * other way around.
616 */
617struct 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
adaf9d92
DH
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
8947d7fc
DH
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
719uint64_t ram_discard_manager_get_min_granularity(const RamDiscardManager *rdm,
720 const MemoryRegion *mr);
721
722bool ram_discard_manager_is_populated(const RamDiscardManager *rdm,
723 const MemoryRegionSection *section);
724
725int ram_discard_manager_replay_populated(const RamDiscardManager *rdm,
726 MemoryRegionSection *section,
727 ReplayRamPopulate replay_fn,
728 void *opaque);
729
adaf9d92
DH
730void ram_discard_manager_replay_discarded(const RamDiscardManager *rdm,
731 MemoryRegionSection *section,
732 ReplayRamDiscard replay_fn,
733 void *opaque);
734
8947d7fc
DH
735void ram_discard_manager_register_listener(RamDiscardManager *rdm,
736 RamDiscardListener *rdl,
737 MemoryRegionSection *section);
738
739void ram_discard_manager_unregister_listener(RamDiscardManager *rdm,
740 RamDiscardListener *rdl);
741
baa44bce
CL
742bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
743 ram_addr_t *ram_addr, bool *read_only,
744 bool *mr_has_discard_manager);
745
093bc2cd 746typedef struct CoalescedMemoryRange CoalescedMemoryRange;
3e9d69e7 747typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
093bc2cd 748
08226b44
PB
749/** MemoryRegion:
750 *
751 * A struct representing a memory region.
752 */
093bc2cd 753struct MemoryRegion {
b4fefef9 754 Object parent_obj;
a676854f 755
08226b44 756 /* private: */
a676854f
PB
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 */
c26763f8 763 bool nonvolatile;
a676854f
PB
764 bool rom_device;
765 bool flush_coalesced_mmio;
a676854f 766 uint8_t dirty_log_mask;
3df9d748 767 bool is_iommu;
58eaa217 768 RAMBlock *ram_block;
612263cf 769 Object *owner;
a2e1753b
AB
770 /* owner as TYPE_DEVICE. Used for re-entrancy checks in MR access hotpath */
771 DeviceState *dev;
a676854f
PB
772
773 const MemoryRegionOps *ops;
093bc2cd 774 void *opaque;
feca4ac1 775 MemoryRegion *container;
5ead6218 776 int mapped_via_alias; /* Mapped via an alias, container might be NULL */
08dafab4 777 Int128 size;
a8170e5e 778 hwaddr addr;
545e92e0 779 void (*destructor)(MemoryRegion *mr);
a2b257d6 780 uint64_t align;
14a3c10a 781 bool terminates;
21e00fa5 782 bool ram_device;
6bba19ba 783 bool enabled;
1660e72d 784 bool warning_printed; /* For reservations */
deb809ed 785 uint8_t vga_logging_count;
093bc2cd 786 MemoryRegion *alias;
a8170e5e 787 hwaddr alias_offset;
d33382da 788 int32_t priority;
b58deb34 789 QTAILQ_HEAD(, MemoryRegion) subregions;
093bc2cd 790 QTAILQ_ENTRY(MemoryRegion) subregions_link;
b58deb34 791 QTAILQ_HEAD(, CoalescedMemoryRange) coalesced;
302fa283 792 const char *name;
3e9d69e7
AK
793 unsigned ioeventfd_nb;
794 MemoryRegionIoeventfd *ioeventfds;
8947d7fc 795 RamDiscardManager *rdm; /* Only for RAM */
a2e1753b
AB
796
797 /* For devices designed to perform re-entrant IO into their own IO MRs */
798 bool disable_reentrancy_guard;
3df9d748
AK
799};
800
801struct IOMMUMemoryRegion {
802 MemoryRegion parent_obj;
803
cdb30812 804 QLIST_HEAD(, IOMMUNotifier) iommu_notify;
5bf3d319 805 IOMMUNotifierFlag iommu_notify_flags;
093bc2cd
AK
806};
807
512fa408
PX
808#define IOMMU_NOTIFIER_FOREACH(n, mr) \
809 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
810
c2fc83e8 811/**
301302f0 812 * struct MemoryListener: callbacks structure for updates to the physical memory map
c2fc83e8
PB
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 */
817struct MemoryListener {
5d248213
PB
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 */
c2fc83e8 829 void (*begin)(MemoryListener *listener);
5d248213
PB
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 */
c2fc83e8 841 void (*commit)(MemoryListener *listener);
5d248213
PB
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 */
c2fc83e8 853 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
5d248213
PB
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 */
c2fc83e8 865 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
5d248213
PB
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 */
c2fc83e8 877 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
5d248213
PB
878
879 /**
880 * @log_start:
881 *
882 * Called during an address space update transaction, after
d7878875 883 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or
5d248213
PB
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 */
b2dfd71c
PB
894 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
895 int old, int new);
5d248213
PB
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 */
b2dfd71c
PB
913 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
914 int old, int new);
5d248213
PB
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 */
c2fc83e8 926 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
5d248213 927
b87eaa9b
PX
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.
1e493be5
GS
937 * @last_stage: The last stage to synchronize the log during migration.
938 * The caller should gurantee that the synchronization with true for
939 * @last_stage is triggered for once after all VCPUs have been stopped.
b87eaa9b 940 */
1e493be5 941 void (*log_sync_global)(MemoryListener *listener, bool last_stage);
b87eaa9b 942
5d248213
PB
943 /**
944 * @log_clear:
945 *
946 * Called before reading the dirty memory bitmap for a
947 * #MemoryRegionSection.
948 *
949 * @listener: The #MemoryListener.
950 * @section: The #MemoryRegionSection.
951 */
077874e0 952 void (*log_clear)(MemoryListener *listener, MemoryRegionSection *section);
5d248213
PB
953
954 /**
955 * @log_global_start:
956 *
957 * Called by memory_global_dirty_log_start(), which
958 * enables the %DIRTY_LOG_MIGRATION client on all memory regions in
959 * the address space. #MemoryListener.log_global_start() is also
960 * called when a #MemoryListener is added, if global dirty logging is
961 * active at that time.
962 *
963 * @listener: The #MemoryListener.
964 */
c2fc83e8 965 void (*log_global_start)(MemoryListener *listener);
5d248213
PB
966
967 /**
968 * @log_global_stop:
969 *
970 * Called by memory_global_dirty_log_stop(), which
971 * disables the %DIRTY_LOG_MIGRATION client on all memory regions in
972 * the address space.
973 *
974 * @listener: The #MemoryListener.
975 */
c2fc83e8 976 void (*log_global_stop)(MemoryListener *listener);
5d248213
PB
977
978 /**
979 * @log_global_after_sync:
980 *
981 * Called after reading the dirty memory bitmap
982 * for any #MemoryRegionSection.
983 *
984 * @listener: The #MemoryListener.
985 */
9458a9a1 986 void (*log_global_after_sync)(MemoryListener *listener);
5d248213
PB
987
988 /**
989 * @eventfd_add:
990 *
991 * Called during an address space update transaction,
992 * for a section of the address space that has had a new ioeventfd
993 * registration since the last transaction.
994 *
995 * @listener: The #MemoryListener.
996 * @section: The new #MemoryRegionSection.
997 * @match_data: The @match_data parameter for the new ioeventfd.
998 * @data: The @data parameter for the new ioeventfd.
999 * @e: The #EventNotifier parameter for the new ioeventfd.
1000 */
c2fc83e8
PB
1001 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
1002 bool match_data, uint64_t data, EventNotifier *e);
5d248213
PB
1003
1004 /**
1005 * @eventfd_del:
1006 *
1007 * Called during an address space update transaction,
1008 * for a section of the address space that has dropped an ioeventfd
1009 * registration since the last transaction.
1010 *
1011 * @listener: The #MemoryListener.
1012 * @section: The new #MemoryRegionSection.
1013 * @match_data: The @match_data parameter for the dropped ioeventfd.
1014 * @data: The @data parameter for the dropped ioeventfd.
1015 * @e: The #EventNotifier parameter for the dropped ioeventfd.
1016 */
c2fc83e8
PB
1017 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
1018 bool match_data, uint64_t data, EventNotifier *e);
5d248213
PB
1019
1020 /**
1021 * @coalesced_io_add:
1022 *
1023 * Called during an address space update transaction,
1024 * for a section of the address space that has had a new coalesced
1025 * MMIO range registration since the last transaction.
1026 *
1027 * @listener: The #MemoryListener.
1028 * @section: The new #MemoryRegionSection.
1029 * @addr: The starting address for the coalesced MMIO range.
1030 * @len: The length of the coalesced MMIO range.
1031 */
e6d34aee 1032 void (*coalesced_io_add)(MemoryListener *listener, MemoryRegionSection *section,
c2fc83e8 1033 hwaddr addr, hwaddr len);
5d248213
PB
1034
1035 /**
1036 * @coalesced_io_del:
1037 *
1038 * Called during an address space update transaction,
1039 * for a section of the address space that has dropped a coalesced
1040 * MMIO range since the last transaction.
1041 *
1042 * @listener: The #MemoryListener.
1043 * @section: The new #MemoryRegionSection.
1044 * @addr: The starting address for the coalesced MMIO range.
1045 * @len: The length of the coalesced MMIO range.
1046 */
e6d34aee 1047 void (*coalesced_io_del)(MemoryListener *listener, MemoryRegionSection *section,
c2fc83e8 1048 hwaddr addr, hwaddr len);
5d248213
PB
1049 /**
1050 * @priority:
1051 *
1052 * Govern the order in which memory listeners are invoked. Lower priorities
1053 * are invoked earlier for "add" or "start" callbacks, and later for "delete"
1054 * or "stop" callbacks.
1055 */
c2fc83e8 1056 unsigned priority;
5d248213 1057
142518bd
PX
1058 /**
1059 * @name:
1060 *
1061 * Name of the listener. It can be used in contexts where we'd like to
1062 * identify one memory listener with the rest.
1063 */
1064 const char *name;
1065
5d248213 1066 /* private: */
d45fa784 1067 AddressSpace *address_space;
c2fc83e8 1068 QTAILQ_ENTRY(MemoryListener) link;
9a54635d 1069 QTAILQ_ENTRY(MemoryListener) link_as;
c2fc83e8
PB
1070};
1071
9ad2bbc1 1072/**
301302f0 1073 * struct AddressSpace: describes a mapping of addresses to #MemoryRegion objects
9ad2bbc1
AK
1074 */
1075struct AddressSpace {
08226b44 1076 /* private: */
374f2981 1077 struct rcu_head rcu;
7dca8043 1078 char *name;
9ad2bbc1 1079 MemoryRegion *root;
374f2981
PB
1080
1081 /* Accessed via RCU. */
9ad2bbc1 1082 struct FlatView *current_map;
374f2981 1083
9ad2bbc1
AK
1084 int ioeventfd_nb;
1085 struct MemoryRegionIoeventfd *ioeventfds;
eae3eb3e 1086 QTAILQ_HEAD(, MemoryListener) listeners;
0d673e36 1087 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
9ad2bbc1
AK
1088};
1089
785a507e
PB
1090typedef struct AddressSpaceDispatch AddressSpaceDispatch;
1091typedef struct FlatRange FlatRange;
1092
1093/* Flattened global view of current active memory hierarchy. Kept in sorted
1094 * order.
1095 */
1096struct FlatView {
1097 struct rcu_head rcu;
1098 unsigned ref;
1099 FlatRange *ranges;
1100 unsigned nr;
1101 unsigned nr_allocated;
1102 struct AddressSpaceDispatch *dispatch;
1103 MemoryRegion *root;
1104};
1105
1106static inline FlatView *address_space_to_flatview(AddressSpace *as)
1107{
d73415a3 1108 return qatomic_rcu_read(&as->current_map);
785a507e
PB
1109}
1110
a5e32ec1
PM
1111/**
1112 * typedef flatview_cb: callback for flatview_for_each_range()
1113 *
1114 * @start: start address of the range within the FlatView
1115 * @len: length of the range in bytes
1116 * @mr: MemoryRegion covering this range
b3566001 1117 * @offset_in_region: offset of the first byte of the range within @mr
a5e32ec1
PM
1118 * @opaque: data pointer passed to flatview_for_each_range()
1119 *
1120 * Returns: true to stop the iteration, false to keep going.
1121 */
d1e8cf77
PM
1122typedef bool (*flatview_cb)(Int128 start,
1123 Int128 len,
a5e32ec1 1124 const MemoryRegion *mr,
b3566001 1125 hwaddr offset_in_region,
a5e32ec1 1126 void *opaque);
fb5ef4ee 1127
a5e32ec1
PM
1128/**
1129 * flatview_for_each_range: Iterate through a FlatView
1130 * @fv: the FlatView to iterate through
1131 * @cb: function to call for each range
1132 * @opaque: opaque data pointer to pass to @cb
1133 *
1134 * A FlatView is made up of a list of non-overlapping ranges, each of
1135 * which is a slice of a MemoryRegion. This function iterates through
1136 * each range in @fv, calling @cb. The callback function can terminate
1137 * iteration early by returning 'true'.
1138 */
1139void flatview_for_each_range(FlatView *fv, flatview_cb cb, void *opaque);
16620684 1140
9366cf02
DDAG
1141static inline bool MemoryRegionSection_eq(MemoryRegionSection *a,
1142 MemoryRegionSection *b)
1143{
1144 return a->mr == b->mr &&
1145 a->fv == b->fv &&
1146 a->offset_within_region == b->offset_within_region &&
1147 a->offset_within_address_space == b->offset_within_address_space &&
1148 int128_eq(a->size, b->size) &&
1149 a->readonly == b->readonly &&
1150 a->nonvolatile == b->nonvolatile;
1151}
1152
22843838
DH
1153/**
1154 * memory_region_section_new_copy: Copy a memory region section
1155 *
1156 * Allocate memory for a new copy, copy the memory region section, and
1157 * properly take a reference on all relevant members.
1158 *
1159 * @s: the #MemoryRegionSection to copy
1160 */
1161MemoryRegionSection *memory_region_section_new_copy(MemoryRegionSection *s);
1162
1163/**
1164 * memory_region_section_new_copy: Free a copied memory region section
1165 *
1166 * Free a copy of a memory section created via memory_region_section_new_copy().
1167 * properly dropping references on all relevant members.
1168 *
1169 * @s: the #MemoryRegionSection to copy
1170 */
1171void memory_region_section_free_copy(MemoryRegionSection *s);
1172
093bc2cd
AK
1173/**
1174 * memory_region_init: Initialize a memory region
1175 *
69ddaf66 1176 * The region typically acts as a container for other memory regions. Use
093bc2cd
AK
1177 * memory_region_add_subregion() to add subregions.
1178 *
1179 * @mr: the #MemoryRegion to be initialized
2c9b15ca 1180 * @owner: the object that tracks the region's reference count
093bc2cd
AK
1181 * @name: used for debugging; not visible to the user or ABI
1182 * @size: size of the region; any subregions beyond this size will be clipped
1183 */
1184void memory_region_init(MemoryRegion *mr,
d32335e8 1185 Object *owner,
093bc2cd
AK
1186 const char *name,
1187 uint64_t size);
46637be2
PB
1188
1189/**
1190 * memory_region_ref: Add 1 to a memory region's reference count
1191 *
1192 * Whenever memory regions are accessed outside the BQL, they need to be
1193 * preserved against hot-unplug. MemoryRegions actually do not have their
1194 * own reference count; they piggyback on a QOM object, their "owner".
1195 * This function adds a reference to the owner.
1196 *
1197 * All MemoryRegions must have an owner if they can disappear, even if the
1198 * device they belong to operates exclusively under the BQL. This is because
1199 * the region could be returned at any time by memory_region_find, and this
1200 * is usually under guest control.
1201 *
1202 * @mr: the #MemoryRegion
1203 */
1204void memory_region_ref(MemoryRegion *mr);
1205
1206/**
1207 * memory_region_unref: Remove 1 to a memory region's reference count
1208 *
1209 * Whenever memory regions are accessed outside the BQL, they need to be
1210 * preserved against hot-unplug. MemoryRegions actually do not have their
1211 * own reference count; they piggyback on a QOM object, their "owner".
1212 * This function removes a reference to the owner and possibly destroys it.
1213 *
1214 * @mr: the #MemoryRegion
1215 */
1216void memory_region_unref(MemoryRegion *mr);
1217
093bc2cd
AK
1218/**
1219 * memory_region_init_io: Initialize an I/O memory region.
1220 *
69ddaf66 1221 * Accesses into the region will cause the callbacks in @ops to be called.
093bc2cd
AK
1222 * if @size is nonzero, subregions will be clipped to @size.
1223 *
1224 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 1225 * @owner: the object that tracks the region's reference count
093bc2cd
AK
1226 * @ops: a structure containing read and write callbacks to be used when
1227 * I/O is performed on the region.
b6af0975 1228 * @opaque: passed to the read and write callbacks of the @ops structure.
093bc2cd
AK
1229 * @name: used for debugging; not visible to the user or ABI
1230 * @size: size of the region.
1231 */
1232void memory_region_init_io(MemoryRegion *mr,
d32335e8 1233 Object *owner,
093bc2cd
AK
1234 const MemoryRegionOps *ops,
1235 void *opaque,
1236 const char *name,
1237 uint64_t size);
1238
1239/**
1cfe48c1
PM
1240 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
1241 * into the region will modify memory
1242 * directly.
093bc2cd
AK
1243 *
1244 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 1245 * @owner: the object that tracks the region's reference count
e8f5fe2d
DDAG
1246 * @name: Region name, becomes part of RAMBlock name used in migration stream
1247 * must be unique within any device
093bc2cd 1248 * @size: size of the region.
49946538 1249 * @errp: pointer to Error*, to store an error if it happens.
a5c0234b
PM
1250 *
1251 * Note that this function does not do anything to cause the data in the
1252 * RAM memory region to be migrated; that is the responsibility of the caller.
093bc2cd 1253 */
1cfe48c1 1254void memory_region_init_ram_nomigrate(MemoryRegion *mr,
d32335e8 1255 Object *owner,
1cfe48c1
PM
1256 const char *name,
1257 uint64_t size,
1258 Error **errp);
093bc2cd 1259
06329cce 1260/**
7f863cba
DH
1261 * memory_region_init_ram_flags_nomigrate: Initialize RAM memory region.
1262 * Accesses into the region will
1263 * modify memory directly.
06329cce
MA
1264 *
1265 * @mr: the #MemoryRegion to be initialized.
1266 * @owner: the object that tracks the region's reference count
1267 * @name: Region name, becomes part of RAMBlock name used in migration stream
1268 * must be unique within any device
1269 * @size: size of the region.
8dbe22c6 1270 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_NORESERVE.
06329cce
MA
1271 * @errp: pointer to Error*, to store an error if it happens.
1272 *
7f863cba
DH
1273 * Note that this function does not do anything to cause the data in the
1274 * RAM memory region to be migrated; that is the responsibility of the caller.
06329cce 1275 */
7f863cba
DH
1276void memory_region_init_ram_flags_nomigrate(MemoryRegion *mr,
1277 Object *owner,
1278 const char *name,
1279 uint64_t size,
1280 uint32_t ram_flags,
1281 Error **errp);
06329cce 1282
60786ef3 1283/**
2cb40d44 1284 * memory_region_init_resizeable_ram: Initialize memory region with resizable
60786ef3
MT
1285 * RAM. Accesses into the region will
1286 * modify memory directly. Only an initial
1287 * portion of this RAM is actually used.
c7c0e724
DH
1288 * Changing the size while migrating
1289 * can result in the migration being
1290 * canceled.
60786ef3
MT
1291 *
1292 * @mr: the #MemoryRegion to be initialized.
1293 * @owner: the object that tracks the region's reference count
e8f5fe2d
DDAG
1294 * @name: Region name, becomes part of RAMBlock name used in migration stream
1295 * must be unique within any device
60786ef3
MT
1296 * @size: used size of the region.
1297 * @max_size: max size of the region.
1298 * @resized: callback to notify owner about used size change.
1299 * @errp: pointer to Error*, to store an error if it happens.
a5c0234b
PM
1300 *
1301 * Note that this function does not do anything to cause the data in the
1302 * RAM memory region to be migrated; that is the responsibility of the caller.
60786ef3
MT
1303 */
1304void memory_region_init_resizeable_ram(MemoryRegion *mr,
d32335e8 1305 Object *owner,
60786ef3
MT
1306 const char *name,
1307 uint64_t size,
1308 uint64_t max_size,
1309 void (*resized)(const char*,
1310 uint64_t length,
1311 void *host),
1312 Error **errp);
d5dbde46 1313#ifdef CONFIG_POSIX
cbfc0171 1314
0b183fc8
PB
1315/**
1316 * memory_region_init_ram_from_file: Initialize RAM memory region with a
1317 * mmap-ed backend.
1318 *
1319 * @mr: the #MemoryRegion to be initialized.
1320 * @owner: the object that tracks the region's reference count
e8f5fe2d
DDAG
1321 * @name: Region name, becomes part of RAMBlock name used in migration stream
1322 * must be unique within any device
0b183fc8 1323 * @size: size of the region.
98376843
HZ
1324 * @align: alignment of the region base address; if 0, the default alignment
1325 * (getpagesize()) will be used.
8dbe22c6
DH
1326 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
1327 * RAM_NORESERVE,
0b183fc8 1328 * @path: the path in which to allocate the RAM.
369d6dc4 1329 * @readonly: true to open @path for reading, false for read/write.
7f56e740 1330 * @errp: pointer to Error*, to store an error if it happens.
a5c0234b
PM
1331 *
1332 * Note that this function does not do anything to cause the data in the
1333 * RAM memory region to be migrated; that is the responsibility of the caller.
0b183fc8
PB
1334 */
1335void memory_region_init_ram_from_file(MemoryRegion *mr,
d32335e8 1336 Object *owner,
0b183fc8
PB
1337 const char *name,
1338 uint64_t size,
98376843 1339 uint64_t align,
cbfc0171 1340 uint32_t ram_flags,
7f56e740 1341 const char *path,
369d6dc4 1342 bool readonly,
7f56e740 1343 Error **errp);
fea617c5
MAL
1344
1345/**
1346 * memory_region_init_ram_from_fd: Initialize RAM memory region with a
1347 * mmap-ed backend.
1348 *
1349 * @mr: the #MemoryRegion to be initialized.
1350 * @owner: the object that tracks the region's reference count
1351 * @name: the name of the region.
1352 * @size: size of the region.
8dbe22c6 1353 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
56918a12 1354 * RAM_NORESERVE, RAM_PROTECTED.
fea617c5 1355 * @fd: the fd to mmap.
44a4ff31 1356 * @offset: offset within the file referenced by fd
fea617c5 1357 * @errp: pointer to Error*, to store an error if it happens.
a5c0234b
PM
1358 *
1359 * Note that this function does not do anything to cause the data in the
1360 * RAM memory region to be migrated; that is the responsibility of the caller.
fea617c5
MAL
1361 */
1362void memory_region_init_ram_from_fd(MemoryRegion *mr,
d32335e8 1363 Object *owner,
fea617c5
MAL
1364 const char *name,
1365 uint64_t size,
d5015b80 1366 uint32_t ram_flags,
fea617c5 1367 int fd,
44a4ff31 1368 ram_addr_t offset,
fea617c5 1369 Error **errp);
0b183fc8
PB
1370#endif
1371
093bc2cd 1372/**
1a7e8cae
BZ
1373 * memory_region_init_ram_ptr: Initialize RAM memory region from a
1374 * user-provided pointer. Accesses into the
1375 * region will modify memory directly.
093bc2cd
AK
1376 *
1377 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 1378 * @owner: the object that tracks the region's reference count
e8f5fe2d
DDAG
1379 * @name: Region name, becomes part of RAMBlock name used in migration stream
1380 * must be unique within any device
093bc2cd
AK
1381 * @size: size of the region.
1382 * @ptr: memory to be mapped; must contain at least @size bytes.
a5c0234b
PM
1383 *
1384 * Note that this function does not do anything to cause the data in the
1385 * RAM memory region to be migrated; that is the responsibility of the caller.
093bc2cd
AK
1386 */
1387void memory_region_init_ram_ptr(MemoryRegion *mr,
d32335e8 1388 Object *owner,
093bc2cd
AK
1389 const char *name,
1390 uint64_t size,
1391 void *ptr);
1392
21e00fa5
AW
1393/**
1394 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from
1395 * a user-provided pointer.
1396 *
1397 * A RAM device represents a mapping to a physical device, such as to a PCI
1398 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped
1399 * into the VM address space and access to the region will modify memory
1400 * directly. However, the memory region should not be included in a memory
1401 * dump (device may not be enabled/mapped at the time of the dump), and
1402 * operations incompatible with manipulating MMIO should be avoided. Replaces
1403 * skip_dump flag.
1404 *
1405 * @mr: the #MemoryRegion to be initialized.
1406 * @owner: the object that tracks the region's reference count
1407 * @name: the name of the region.
1408 * @size: size of the region.
1409 * @ptr: memory to be mapped; must contain at least @size bytes.
a5c0234b
PM
1410 *
1411 * Note that this function does not do anything to cause the data in the
1412 * RAM memory region to be migrated; that is the responsibility of the caller.
1413 * (For RAM device memory regions, migrating the contents rarely makes sense.)
21e00fa5
AW
1414 */
1415void memory_region_init_ram_device_ptr(MemoryRegion *mr,
d32335e8 1416 Object *owner,
21e00fa5
AW
1417 const char *name,
1418 uint64_t size,
1419 void *ptr);
1420
093bc2cd
AK
1421/**
1422 * memory_region_init_alias: Initialize a memory region that aliases all or a
1423 * part of another memory region.
1424 *
1425 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 1426 * @owner: the object that tracks the region's reference count
093bc2cd
AK
1427 * @name: used for debugging; not visible to the user or ABI
1428 * @orig: the region to be referenced; @mr will be equivalent to
1429 * @orig between @offset and @offset + @size - 1.
1430 * @offset: start of the section in @orig to be referenced.
1431 * @size: size of the region.
1432 */
1433void memory_region_init_alias(MemoryRegion *mr,
d32335e8 1434 Object *owner,
093bc2cd
AK
1435 const char *name,
1436 MemoryRegion *orig,
a8170e5e 1437 hwaddr offset,
093bc2cd 1438 uint64_t size);
d0a9b5bc 1439
a1777f7f 1440/**
b59821a9 1441 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
a1777f7f 1442 *
b59821a9 1443 * This has the same effect as calling memory_region_init_ram_nomigrate()
a1777f7f
PM
1444 * and then marking the resulting region read-only with
1445 * memory_region_set_readonly().
1446 *
b59821a9
PM
1447 * Note that this function does not do anything to cause the data in the
1448 * RAM side of the memory region to be migrated; that is the responsibility
1449 * of the caller.
1450 *
a1777f7f
PM
1451 * @mr: the #MemoryRegion to be initialized.
1452 * @owner: the object that tracks the region's reference count
e8f5fe2d
DDAG
1453 * @name: Region name, becomes part of RAMBlock name used in migration stream
1454 * must be unique within any device
a1777f7f
PM
1455 * @size: size of the region.
1456 * @errp: pointer to Error*, to store an error if it happens.
1457 */
b59821a9 1458void memory_region_init_rom_nomigrate(MemoryRegion *mr,
d32335e8 1459 Object *owner,
b59821a9
PM
1460 const char *name,
1461 uint64_t size,
1462 Error **errp);
a1777f7f 1463
d0a9b5bc 1464/**
b59821a9
PM
1465 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region.
1466 * Writes are handled via callbacks.
1467 *
1468 * Note that this function does not do anything to cause the data in the
1469 * RAM side of the memory region to be migrated; that is the responsibility
1470 * of the caller.
d0a9b5bc
AK
1471 *
1472 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 1473 * @owner: the object that tracks the region's reference count
39e0b03d 1474 * @ops: callbacks for write access handling (must not be NULL).
57914ecb 1475 * @opaque: passed to the read and write callbacks of the @ops structure.
e8f5fe2d
DDAG
1476 * @name: Region name, becomes part of RAMBlock name used in migration stream
1477 * must be unique within any device
d0a9b5bc 1478 * @size: size of the region.
33e0eb52 1479 * @errp: pointer to Error*, to store an error if it happens.
d0a9b5bc 1480 */
b59821a9 1481void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,
d32335e8 1482 Object *owner,
b59821a9
PM
1483 const MemoryRegionOps *ops,
1484 void *opaque,
1485 const char *name,
1486 uint64_t size,
1487 Error **errp);
d0a9b5bc 1488
30951157 1489/**
1221a474
AK
1490 * memory_region_init_iommu: Initialize a memory region of a custom type
1491 * that translates addresses
30951157
AK
1492 *
1493 * An IOMMU region translates addresses and forwards accesses to a target
1494 * memory region.
1495 *
2ce931d0
PM
1496 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION.
1497 * @_iommu_mr should be a pointer to enough memory for an instance of
1498 * that subclass, @instance_size is the size of that subclass, and
1499 * @mrtypename is its name. This function will initialize @_iommu_mr as an
1500 * instance of the subclass, and its methods will then be called to handle
1501 * accesses to the memory region. See the documentation of
1502 * #IOMMUMemoryRegionClass for further details.
1503 *
1221a474
AK
1504 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
1505 * @instance_size: the IOMMUMemoryRegion subclass instance size
57914ecb 1506 * @mrtypename: the type name of the #IOMMUMemoryRegion
2c9b15ca 1507 * @owner: the object that tracks the region's reference count
30951157
AK
1508 * @name: used for debugging; not visible to the user or ABI
1509 * @size: size of the region.
1510 */
1221a474
AK
1511void memory_region_init_iommu(void *_iommu_mr,
1512 size_t instance_size,
1513 const char *mrtypename,
1514 Object *owner,
30951157
AK
1515 const char *name,
1516 uint64_t size);
1517
b08199c6
PM
1518/**
1519 * memory_region_init_ram - Initialize RAM memory region. Accesses into the
1520 * region will modify memory directly.
1521 *
1522 * @mr: the #MemoryRegion to be initialized
1523 * @owner: the object that tracks the region's reference count (must be
1524 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL)
1525 * @name: name of the memory region
1526 * @size: size of the region in bytes
1527 * @errp: pointer to Error*, to store an error if it happens.
1528 *
1529 * This function allocates RAM for a board model or device, and
1530 * arranges for it to be migrated (by calling vmstate_register_ram()
1531 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1532 * @owner is NULL).
1533 *
1534 * TODO: Currently we restrict @owner to being either NULL (for
1535 * global RAM regions with no owner) or devices, so that we can
1536 * give the RAM block a unique name for migration purposes.
1537 * We should lift this restriction and allow arbitrary Objects.
1538 * If you pass a non-NULL non-device @owner then we will assert.
1539 */
1540void memory_region_init_ram(MemoryRegion *mr,
d32335e8 1541 Object *owner,
b08199c6
PM
1542 const char *name,
1543 uint64_t size,
1544 Error **errp);
1545
1546/**
1547 * memory_region_init_rom: Initialize a ROM memory region.
1548 *
1549 * This has the same effect as calling memory_region_init_ram()
1550 * and then marking the resulting region read-only with
1551 * memory_region_set_readonly(). This includes arranging for the
1552 * contents to be migrated.
1553 *
1554 * TODO: Currently we restrict @owner to being either NULL (for
1555 * global RAM regions with no owner) or devices, so that we can
1556 * give the RAM block a unique name for migration purposes.
1557 * We should lift this restriction and allow arbitrary Objects.
1558 * If you pass a non-NULL non-device @owner then we will assert.
1559 *
1560 * @mr: the #MemoryRegion to be initialized.
1561 * @owner: the object that tracks the region's reference count
1562 * @name: Region name, becomes part of RAMBlock name used in migration stream
1563 * must be unique within any device
1564 * @size: size of the region.
1565 * @errp: pointer to Error*, to store an error if it happens.
1566 */
1567void memory_region_init_rom(MemoryRegion *mr,
d32335e8 1568 Object *owner,
b08199c6
PM
1569 const char *name,
1570 uint64_t size,
1571 Error **errp);
1572
1573/**
1574 * memory_region_init_rom_device: Initialize a ROM memory region.
1575 * Writes are handled via callbacks.
1576 *
1577 * This function initializes a memory region backed by RAM for reads
1578 * and callbacks for writes, and arranges for the RAM backing to
1579 * be migrated (by calling vmstate_register_ram()
1580 * if @owner is a DeviceState, or vmstate_register_ram_global() if
1581 * @owner is NULL).
1582 *
1583 * TODO: Currently we restrict @owner to being either NULL (for
1584 * global RAM regions with no owner) or devices, so that we can
1585 * give the RAM block a unique name for migration purposes.
1586 * We should lift this restriction and allow arbitrary Objects.
1587 * If you pass a non-NULL non-device @owner then we will assert.
1588 *
1589 * @mr: the #MemoryRegion to be initialized.
1590 * @owner: the object that tracks the region's reference count
1591 * @ops: callbacks for write access handling (must not be NULL).
5d248213 1592 * @opaque: passed to the read and write callbacks of the @ops structure.
b08199c6
PM
1593 * @name: Region name, becomes part of RAMBlock name used in migration stream
1594 * must be unique within any device
1595 * @size: size of the region.
1596 * @errp: pointer to Error*, to store an error if it happens.
1597 */
1598void memory_region_init_rom_device(MemoryRegion *mr,
d32335e8 1599 Object *owner,
b08199c6
PM
1600 const MemoryRegionOps *ops,
1601 void *opaque,
1602 const char *name,
1603 uint64_t size,
1604 Error **errp);
1605
1606
803c0816
PB
1607/**
1608 * memory_region_owner: get a memory region's owner.
1609 *
1610 * @mr: the memory region being queried.
1611 */
d32335e8 1612Object *memory_region_owner(MemoryRegion *mr);
803c0816 1613
093bc2cd
AK
1614/**
1615 * memory_region_size: get a memory region's size.
1616 *
1617 * @mr: the memory region being queried.
1618 */
1619uint64_t memory_region_size(MemoryRegion *mr);
1620
8ea9252a
AK
1621/**
1622 * memory_region_is_ram: check whether a memory region is random access
1623 *
847b31f0 1624 * Returns %true if a memory region is random access.
8ea9252a
AK
1625 *
1626 * @mr: the memory region being queried
1627 */
1619d1fe
PB
1628static inline bool memory_region_is_ram(MemoryRegion *mr)
1629{
1630 return mr->ram;
1631}
8ea9252a 1632
e4dc3f59 1633/**
21e00fa5 1634 * memory_region_is_ram_device: check whether a memory region is a ram device
e4dc3f59 1635 *
847b31f0 1636 * Returns %true if a memory region is a device backed ram region
e4dc3f59
ND
1637 *
1638 * @mr: the memory region being queried
1639 */
21e00fa5 1640bool memory_region_is_ram_device(MemoryRegion *mr);
e4dc3f59 1641
fd062573 1642/**
5f9a5ea1 1643 * memory_region_is_romd: check whether a memory region is in ROMD mode
fd062573 1644 *
5f9a5ea1 1645 * Returns %true if a memory region is a ROM device and currently set to allow
fd062573
BS
1646 * direct reads.
1647 *
1648 * @mr: the memory region being queried
1649 */
1650static inline bool memory_region_is_romd(MemoryRegion *mr)
1651{
5f9a5ea1 1652 return mr->rom_device && mr->romd_mode;
fd062573
BS
1653}
1654
56918a12
SC
1655/**
1656 * memory_region_is_protected: check whether a memory region is protected
1657 *
1658 * Returns %true if a memory region is protected RAM and cannot be accessed
1659 * via standard mechanisms, e.g. DMA.
1660 *
1661 * @mr: the memory region being queried
1662 */
1663bool memory_region_is_protected(MemoryRegion *mr);
1664
30951157 1665/**
3df9d748 1666 * memory_region_get_iommu: check whether a memory region is an iommu
30951157 1667 *
3df9d748
AK
1668 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
1669 * otherwise NULL.
30951157
AK
1670 *
1671 * @mr: the memory region being queried
1672 */
3df9d748 1673static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
1619d1fe 1674{
12d37882 1675 if (mr->alias) {
3df9d748
AK
1676 return memory_region_get_iommu(mr->alias);
1677 }
1678 if (mr->is_iommu) {
1679 return (IOMMUMemoryRegion *) mr;
12d37882 1680 }
3df9d748 1681 return NULL;
1619d1fe
PB
1682}
1683
1221a474
AK
1684/**
1685 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
1686 * if an iommu or NULL if not
1687 *
57914ecb
JZ
1688 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu,
1689 * otherwise NULL. This is fast path avoiding QOM checking, use with caution.
1221a474 1690 *
5d248213 1691 * @iommu_mr: the memory region being queried
1221a474
AK
1692 */
1693static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck(
1694 IOMMUMemoryRegion *iommu_mr)
1695{
1696 return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class);
1697}
1698
3df9d748 1699#define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
30951157 1700
f682e9c2
AK
1701/**
1702 * memory_region_iommu_get_min_page_size: get minimum supported page size
1703 * for an iommu
1704 *
1705 * Returns minimum supported page size for an iommu.
1706 *
3df9d748 1707 * @iommu_mr: the memory region being queried
f682e9c2 1708 */
3df9d748 1709uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
f682e9c2 1710
06866575
DG
1711/**
1712 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1713 *
cdb30812
PX
1714 * Note: for any IOMMU implementation, an in-place mapping change
1715 * should be notified with an UNMAP followed by a MAP.
1716 *
3df9d748 1717 * @iommu_mr: the memory region that was changed
cb1efcf4 1718 * @iommu_idx: the IOMMU index for the translation table which has changed
5039caf3
EP
1719 * @event: TLB event with the new entry in the IOMMU translation table.
1720 * The entry replaces all old entries for the same virtual I/O address
1721 * range.
06866575 1722 */
3df9d748 1723void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
cb1efcf4 1724 int iommu_idx,
5039caf3 1725 IOMMUTLBEvent event);
06866575 1726
bd2bfa4c 1727/**
3b5ebf85 1728 * memory_region_notify_iommu_one: notify a change in an IOMMU translation
bd2bfa4c
PX
1729 * entry to a single notifier
1730 *
1731 * This works just like memory_region_notify_iommu(), but it only
1732 * notifies a specific notifier, not all of them.
1733 *
1734 * @notifier: the notifier to be notified
5039caf3
EP
1735 * @event: TLB event with the new entry in the IOMMU translation table.
1736 * The entry replaces all old entries for the same virtual I/O address
1737 * range.
bd2bfa4c 1738 */
3b5ebf85 1739void memory_region_notify_iommu_one(IOMMUNotifier *notifier,
5039caf3 1740 IOMMUTLBEvent *event);
bd2bfa4c 1741
7caebbf9
JW
1742/**
1743 * memory_region_unmap_iommu_notifier_range: notify a unmap for an IOMMU
1744 * translation that covers the
1745 * range of a notifier
1746 *
1747 * @notifier: the notifier to be notified
1748 */
afa55c6e 1749void memory_region_unmap_iommu_notifier_range(IOMMUNotifier *notifier);
7caebbf9
JW
1750
1751
06866575
DG
1752/**
1753 * memory_region_register_iommu_notifier: register a notifier for changes to
1754 * IOMMU translation entries.
1755 *
549d4005
EA
1756 * Returns 0 on success, or a negative errno otherwise. In particular,
1757 * -EINVAL indicates that at least one of the attributes of the notifier
1758 * is not supported (flag/range) by the IOMMU memory region. In case of error
1759 * the error object must be created.
1760 *
06866575 1761 * @mr: the memory region to observe
cdb30812
PX
1762 * @n: the IOMMUNotifier to be added; the notify callback receives a
1763 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer
1764 * ceases to be valid on exit from the notifier.
5d248213 1765 * @errp: pointer to Error*, to store an error if it happens.
06866575 1766 */
549d4005
EA
1767int memory_region_register_iommu_notifier(MemoryRegion *mr,
1768 IOMMUNotifier *n, Error **errp);
06866575 1769
a788f227
DG
1770/**
1771 * memory_region_iommu_replay: replay existing IOMMU translations to
f682e9c2
AK
1772 * a notifier with the minimum page granularity returned by
1773 * mr->iommu_ops->get_page_size().
a788f227 1774 *
2ce931d0
PM
1775 * Note: this is not related to record-and-replay functionality.
1776 *
3df9d748 1777 * @iommu_mr: the memory region to observe
a788f227 1778 * @n: the notifier to which to replay iommu mappings
a788f227 1779 */
3df9d748 1780void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
a788f227 1781
06866575
DG
1782/**
1783 * memory_region_unregister_iommu_notifier: unregister a notifier for
1784 * changes to IOMMU translation entries.
1785 *
d22d8956
AK
1786 * @mr: the memory region which was observed and for which notity_stopped()
1787 * needs to be called
06866575
DG
1788 * @n: the notifier to be removed.
1789 */
cdb30812
PX
1790void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1791 IOMMUNotifier *n);
06866575 1792
f1334de6
AK
1793/**
1794 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1795 * defined on the IOMMU.
1796 *
2ce931d0
PM
1797 * Returns 0 on success, or a negative errno otherwise. In particular,
1798 * -EINVAL indicates that the IOMMU does not support the requested
1799 * attribute.
f1334de6
AK
1800 *
1801 * @iommu_mr: the memory region
1802 * @attr: the requested attribute
1803 * @data: a pointer to the requested attribute data
1804 */
1805int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1806 enum IOMMUMemoryRegionAttr attr,
1807 void *data);
1808
21f40209
PM
1809/**
1810 * memory_region_iommu_attrs_to_index: return the IOMMU index to
1811 * use for translations with the given memory transaction attributes.
1812 *
1813 * @iommu_mr: the memory region
1814 * @attrs: the memory transaction attributes
1815 */
1816int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr,
1817 MemTxAttrs attrs);
1818
1819/**
1820 * memory_region_iommu_num_indexes: return the total number of IOMMU
1821 * indexes that this IOMMU supports.
1822 *
1823 * @iommu_mr: the memory region
1824 */
1825int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr);
1826
457f8cbb
BB
1827/**
1828 * memory_region_iommu_set_page_size_mask: set the supported page
1829 * sizes for a given IOMMU memory region
1830 *
1831 * @iommu_mr: IOMMU memory region
1832 * @page_size_mask: supported page size mask
1833 * @errp: pointer to Error*, to store an error if it happens.
1834 */
1835int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion *iommu_mr,
1836 uint64_t page_size_mask,
1837 Error **errp);
1838
8991c79b
AK
1839/**
1840 * memory_region_name: get a memory region's name
1841 *
1842 * Returns the string that was used to initialize the memory region.
1843 *
1844 * @mr: the memory region being queried
1845 */
5d546d4b 1846const char *memory_region_name(const MemoryRegion *mr);
8991c79b 1847
55043ba3
AK
1848/**
1849 * memory_region_is_logging: return whether a memory region is logging writes
1850 *
2d1a35be 1851 * Returns %true if the memory region is logging writes for the given client
55043ba3
AK
1852 *
1853 * @mr: the memory region being queried
2d1a35be 1854 * @client: the client being queried
55043ba3 1855 */
2d1a35be
PB
1856bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
1857
1858/**
1859 * memory_region_get_dirty_log_mask: return the clients for which a
1860 * memory region is logging writes.
1861 *
677e7805
PB
1862 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1863 * are the bit indices.
2d1a35be
PB
1864 *
1865 * @mr: the memory region being queried
1866 */
1867uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
55043ba3 1868
ce7923da
AK
1869/**
1870 * memory_region_is_rom: check whether a memory region is ROM
1871 *
847b31f0 1872 * Returns %true if a memory region is read-only memory.
ce7923da
AK
1873 *
1874 * @mr: the memory region being queried
1875 */
1619d1fe
PB
1876static inline bool memory_region_is_rom(MemoryRegion *mr)
1877{
1878 return mr->ram && mr->readonly;
1879}
1880
c26763f8
MAL
1881/**
1882 * memory_region_is_nonvolatile: check whether a memory region is non-volatile
1883 *
1884 * Returns %true is a memory region is non-volatile memory.
1885 *
1886 * @mr: the memory region being queried
1887 */
1888static inline bool memory_region_is_nonvolatile(MemoryRegion *mr)
1889{
1890 return mr->nonvolatile;
1891}
ce7923da 1892
a35ba7be
PB
1893/**
1894 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
1895 *
1896 * Returns a file descriptor backing a file-based RAM memory region,
1897 * or -1 if the region is not a file-based RAM memory region.
1898 *
1899 * @mr: the RAM or alias memory region being queried.
1900 */
1901int memory_region_get_fd(MemoryRegion *mr);
1902
07bdaa41
PB
1903/**
1904 * memory_region_from_host: Convert a pointer into a RAM memory region
1905 * and an offset within it.
1906 *
1907 * Given a host pointer inside a RAM memory region (created with
1908 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
1909 * the MemoryRegion and the offset within it.
1910 *
1911 * Use with care; by the time this function returns, the returned pointer is
1912 * not protected by RCU anymore. If the caller is not within an RCU critical
1913 * section and does not hold the iothread lock, it must have other means of
1914 * protecting the pointer, such as a reference to the region that includes
1915 * the incoming ram_addr_t.
1916 *
57914ecb
JZ
1917 * @ptr: the host pointer to be converted
1918 * @offset: the offset within memory region
07bdaa41
PB
1919 */
1920MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
1921
093bc2cd
AK
1922/**
1923 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
1924 *
1925 * Returns a host pointer to a RAM memory region (created with
49b24afc
PB
1926 * memory_region_init_ram() or memory_region_init_ram_ptr()).
1927 *
1928 * Use with care; by the time this function returns, the returned pointer is
1929 * not protected by RCU anymore. If the caller is not within an RCU critical
1930 * section and does not hold the iothread lock, it must have other means of
1931 * protecting the pointer, such as a reference to the region that includes
1932 * the incoming ram_addr_t.
093bc2cd
AK
1933 *
1934 * @mr: the memory region being queried.
1935 */
1936void *memory_region_get_ram_ptr(MemoryRegion *mr);
1937
37d7c084
PB
1938/* memory_region_ram_resize: Resize a RAM region.
1939 *
c7c0e724
DH
1940 * Resizing RAM while migrating can result in the migration being canceled.
1941 * Care has to be taken if the guest might have already detected the memory.
37d7c084
PB
1942 *
1943 * @mr: a memory region created with @memory_region_init_resizeable_ram.
1944 * @newsize: the new size the region
1945 * @errp: pointer to Error*, to store an error if it happens.
1946 */
1947void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
1948 Error **errp);
9ecc996a
PMD
1949
1950/**
1951 * memory_region_msync: Synchronize selected address range of
1952 * a memory mapped region
1953 *
1954 * @mr: the memory region to be msync
1955 * @addr: the initial address of the range to be sync
1956 * @size: the size of the range to be sync
1957 */
1958void memory_region_msync(MemoryRegion *mr, hwaddr addr, hwaddr size);
1959
61c490e2 1960/**
9ecc996a 1961 * memory_region_writeback: Trigger cache writeback for
5d248213 1962 * selected address range
61c490e2 1963 *
5d248213
PB
1964 * @mr: the memory region to be updated
1965 * @addr: the initial address of the range to be written back
1966 * @size: the size of the range to be written back
61c490e2 1967 */
4dfe59d1 1968void memory_region_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size);
37d7c084 1969
093bc2cd
AK
1970/**
1971 * memory_region_set_log: Turn dirty logging on or off for a region.
1972 *
1973 * Turns dirty logging on or off for a specified client (display, migration).
1974 * Only meaningful for RAM regions.
1975 *
1976 * @mr: the memory region being updated.
1977 * @log: whether dirty logging is to be enabled or disabled.
dbddac6d 1978 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
093bc2cd
AK
1979 */
1980void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
1981
093bc2cd 1982/**
fd4aa979 1983 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
093bc2cd 1984 *
fd4aa979
BS
1985 * Marks a range of bytes as dirty, after it has been dirtied outside
1986 * guest code.
093bc2cd 1987 *
fd4aa979 1988 * @mr: the memory region being dirtied.
093bc2cd 1989 * @addr: the address (relative to the start of the region) being dirtied.
fd4aa979 1990 * @size: size of the range being dirtied.
093bc2cd 1991 */
a8170e5e
AK
1992void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1993 hwaddr size);
093bc2cd 1994
077874e0
PX
1995/**
1996 * memory_region_clear_dirty_bitmap - clear dirty bitmap for memory range
1997 *
1998 * This function is called when the caller wants to clear the remote
1999 * dirty bitmap of a memory range within the memory region. This can
2000 * be used by e.g. KVM to manually clear dirty log when
2001 * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT is declared support by the host
2002 * kernel.
2003 *
2004 * @mr: the memory region to clear the dirty log upon
2005 * @start: start address offset within the memory region
2006 * @len: length of the memory region to clear dirty bitmap
2007 */
2008void memory_region_clear_dirty_bitmap(MemoryRegion *mr, hwaddr start,
2009 hwaddr len);
2010
8deaf12c
GH
2011/**
2012 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty
2013 * bitmap and clear it.
2014 *
2015 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and
2016 * returns the snapshot. The snapshot can then be used to query dirty
77302fb5
PB
2017 * status, using memory_region_snapshot_get_dirty. Snapshotting allows
2018 * querying the same page multiple times, which is especially useful for
2019 * display updates where the scanlines often are not page aligned.
8deaf12c 2020 *
1e458f11 2021 * The dirty bitmap region which gets copied into the snapshot (and
8deaf12c
GH
2022 * cleared afterwards) can be larger than requested. The boundaries
2023 * are rounded up/down so complete bitmap longs (covering 64 pages on
2024 * 64bit hosts) can be copied over into the bitmap snapshot. Which
2025 * isn't a problem for display updates as the extra pages are outside
2026 * the visible area, and in case the visible area changes a full
2027 * display redraw is due anyway. Should other use cases for this
2028 * function emerge we might have to revisit this implementation
2029 * detail.
2030 *
2031 * Use g_free to release DirtyBitmapSnapshot.
2032 *
2033 * @mr: the memory region being queried.
2034 * @addr: the address (relative to the start of the region) being queried.
2035 * @size: the size of the range being queried.
2036 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA.
2037 */
2038DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr,
2039 hwaddr addr,
2040 hwaddr size,
2041 unsigned client);
2042
2043/**
2044 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty
2045 * in the specified dirty bitmap snapshot.
2046 *
2047 * @mr: the memory region being queried.
2048 * @snap: the dirty bitmap snapshot
2049 * @addr: the address (relative to the start of the region) being queried.
2050 * @size: the size of the range being queried.
2051 */
2052bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
2053 DirtyBitmapSnapshot *snap,
2054 hwaddr addr, hwaddr size);
2055
093bc2cd
AK
2056/**
2057 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
2058 * client.
2059 *
2060 * Marks a range of pages as no longer dirty.
2061 *
2062 * @mr: the region being updated.
2063 * @addr: the start of the subrange being cleaned.
2064 * @size: the size of the subrange being cleaned.
2065 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
2066 * %DIRTY_MEMORY_VGA.
2067 */
a8170e5e
AK
2068void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
2069 hwaddr size, unsigned client);
093bc2cd 2070
047be4ed
SH
2071/**
2072 * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate
2073 * TBs (for self-modifying code).
2074 *
2075 * The MemoryRegionOps->write() callback of a ROM device must use this function
2076 * to mark byte ranges that have been modified internally, such as by directly
2077 * accessing the memory returned by memory_region_get_ram_ptr().
2078 *
2079 * This function marks the range dirty and invalidates TBs so that TCG can
2080 * detect self-modifying code.
2081 *
2082 * @mr: the region being flushed.
2083 * @addr: the start, relative to the start of the region, of the range being
2084 * flushed.
2085 * @size: the size, in bytes, of the range being flushed.
2086 */
2087void memory_region_flush_rom_device(MemoryRegion *mr, hwaddr addr, hwaddr size);
2088
093bc2cd
AK
2089/**
2090 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
2091 *
2092 * Allows a memory region to be marked as read-only (turning it into a ROM).
2093 * only useful on RAM regions.
2094 *
2095 * @mr: the region being updated.
2096 * @readonly: whether rhe region is to be ROM or RAM.
2097 */
2098void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
2099
c26763f8
MAL
2100/**
2101 * memory_region_set_nonvolatile: Turn a memory region non-volatile
2102 *
2103 * Allows a memory region to be marked as non-volatile.
2104 * only useful on RAM regions.
2105 *
2106 * @mr: the region being updated.
2107 * @nonvolatile: whether rhe region is to be non-volatile.
2108 */
2109void memory_region_set_nonvolatile(MemoryRegion *mr, bool nonvolatile);
2110
d0a9b5bc 2111/**
5f9a5ea1 2112 * memory_region_rom_device_set_romd: enable/disable ROMD mode
d0a9b5bc
AK
2113 *
2114 * Allows a ROM device (initialized with memory_region_init_rom_device() to
5f9a5ea1
JK
2115 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
2116 * device is mapped to guest memory and satisfies read access directly.
2117 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
2118 * Writes are always handled by the #MemoryRegion.write function.
d0a9b5bc
AK
2119 *
2120 * @mr: the memory region to be updated
5f9a5ea1 2121 * @romd_mode: %true to put the region into ROMD mode
d0a9b5bc 2122 */
5f9a5ea1 2123void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
d0a9b5bc 2124
093bc2cd
AK
2125/**
2126 * memory_region_set_coalescing: Enable memory coalescing for the region.
2127 *
2128 * Enabled writes to a region to be queued for later processing. MMIO ->write
2129 * callbacks may be delayed until a non-coalesced MMIO is issued.
2130 * Only useful for IO regions. Roughly similar to write-combining hardware.
2131 *
2132 * @mr: the memory region to be write coalesced
2133 */
2134void memory_region_set_coalescing(MemoryRegion *mr);
2135
2136/**
2137 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
2138 * a region.
2139 *
2140 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
2141 * Multiple calls can be issued coalesced disjoint ranges.
2142 *
2143 * @mr: the memory region to be updated.
2144 * @offset: the start of the range within the region to be coalesced.
2145 * @size: the size of the subrange to be coalesced.
2146 */
2147void memory_region_add_coalescing(MemoryRegion *mr,
a8170e5e 2148 hwaddr offset,
093bc2cd
AK
2149 uint64_t size);
2150
2151/**
2152 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
2153 *
2154 * Disables any coalescing caused by memory_region_set_coalescing() or
2155 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
2156 * hardware.
2157 *
2158 * @mr: the memory region to be updated.
2159 */
2160void memory_region_clear_coalescing(MemoryRegion *mr);
2161
d410515e
JK
2162/**
2163 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
2164 * accesses.
2165 *
2166 * Ensure that pending coalesced MMIO request are flushed before the memory
2167 * region is accessed. This property is automatically enabled for all regions
2168 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
2169 *
2170 * @mr: the memory region to be updated.
2171 */
2172void memory_region_set_flush_coalesced(MemoryRegion *mr);
2173
2174/**
2175 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
2176 * accesses.
2177 *
2178 * Clear the automatic coalesced MMIO flushing enabled via
2179 * memory_region_set_flush_coalesced. Note that this service has no effect on
2180 * memory regions that have MMIO coalescing enabled for themselves. For them,
2181 * automatic flushing will stop once coalescing is disabled.
2182 *
2183 * @mr: the memory region to be updated.
2184 */
2185void memory_region_clear_flush_coalesced(MemoryRegion *mr);
2186
3e9d69e7
AK
2187/**
2188 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
2189 * is written to a location.
2190 *
2191 * Marks a word in an IO region (initialized with memory_region_init_io())
2192 * as a trigger for an eventfd event. The I/O callback will not be called.
69ddaf66 2193 * The caller must be prepared to handle failure (that is, take the required
3e9d69e7
AK
2194 * action if the callback _is_ called).
2195 *
2196 * @mr: the memory region being updated.
2197 * @addr: the address within @mr that is to be monitored
2198 * @size: the size of the access to trigger the eventfd
2199 * @match_data: whether to match against @data, instead of just @addr
2200 * @data: the data to match against the guest write
57914ecb 2201 * @e: event notifier to be triggered when @addr, @size, and @data all match.
3e9d69e7
AK
2202 **/
2203void memory_region_add_eventfd(MemoryRegion *mr,
a8170e5e 2204 hwaddr addr,
3e9d69e7
AK
2205 unsigned size,
2206 bool match_data,
2207 uint64_t data,
753d5e14 2208 EventNotifier *e);
3e9d69e7
AK
2209
2210/**
69ddaf66 2211 * memory_region_del_eventfd: Cancel an eventfd.
3e9d69e7 2212 *
69ddaf66
ASRJ
2213 * Cancels an eventfd trigger requested by a previous
2214 * memory_region_add_eventfd() call.
3e9d69e7
AK
2215 *
2216 * @mr: the memory region being updated.
2217 * @addr: the address within @mr that is to be monitored
2218 * @size: the size of the access to trigger the eventfd
2219 * @match_data: whether to match against @data, instead of just @addr
2220 * @data: the data to match against the guest write
57914ecb 2221 * @e: event notifier to be triggered when @addr, @size, and @data all match.
3e9d69e7
AK
2222 */
2223void memory_region_del_eventfd(MemoryRegion *mr,
a8170e5e 2224 hwaddr addr,
3e9d69e7
AK
2225 unsigned size,
2226 bool match_data,
2227 uint64_t data,
753d5e14
PB
2228 EventNotifier *e);
2229
093bc2cd 2230/**
69ddaf66 2231 * memory_region_add_subregion: Add a subregion to a container.
093bc2cd 2232 *
69ddaf66 2233 * Adds a subregion at @offset. The subregion may not overlap with other
093bc2cd
AK
2234 * subregions (except for those explicitly marked as overlapping). A region
2235 * may only be added once as a subregion (unless removed with
2236 * memory_region_del_subregion()); use memory_region_init_alias() if you
2237 * want a region to be a subregion in multiple locations.
2238 *
2239 * @mr: the region to contain the new subregion; must be a container
2240 * initialized with memory_region_init().
2241 * @offset: the offset relative to @mr where @subregion is added.
2242 * @subregion: the subregion to be added.
2243 */
2244void memory_region_add_subregion(MemoryRegion *mr,
a8170e5e 2245 hwaddr offset,
093bc2cd
AK
2246 MemoryRegion *subregion);
2247/**
1a7e8cae
BZ
2248 * memory_region_add_subregion_overlap: Add a subregion to a container
2249 * with overlap.
093bc2cd 2250 *
69ddaf66 2251 * Adds a subregion at @offset. The subregion may overlap with other
093bc2cd
AK
2252 * subregions. Conflicts are resolved by having a higher @priority hide a
2253 * lower @priority. Subregions without priority are taken as @priority 0.
2254 * A region may only be added once as a subregion (unless removed with
2255 * memory_region_del_subregion()); use memory_region_init_alias() if you
2256 * want a region to be a subregion in multiple locations.
2257 *
2258 * @mr: the region to contain the new subregion; must be a container
2259 * initialized with memory_region_init().
2260 * @offset: the offset relative to @mr where @subregion is added.
2261 * @subregion: the subregion to be added.
2262 * @priority: used for resolving overlaps; highest priority wins.
2263 */
2264void memory_region_add_subregion_overlap(MemoryRegion *mr,
a8170e5e 2265 hwaddr offset,
093bc2cd 2266 MemoryRegion *subregion,
a1ff8ae0 2267 int priority);
e34911c4
AK
2268
2269/**
2270 * memory_region_get_ram_addr: Get the ram address associated with a memory
2271 * region
5d248213
PB
2272 *
2273 * @mr: the region to be queried
e34911c4 2274 */
7ebb2745 2275ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
e34911c4 2276
a2b257d6 2277uint64_t memory_region_get_alignment(const MemoryRegion *mr);
093bc2cd
AK
2278/**
2279 * memory_region_del_subregion: Remove a subregion.
2280 *
2281 * Removes a subregion from its container.
2282 *
2283 * @mr: the container to be updated.
2284 * @subregion: the region being removed; must be a current subregion of @mr.
2285 */
2286void memory_region_del_subregion(MemoryRegion *mr,
2287 MemoryRegion *subregion);
2288
6bba19ba
AK
2289/*
2290 * memory_region_set_enabled: dynamically enable or disable a region
2291 *
2292 * Enables or disables a memory region. A disabled memory region
2293 * ignores all accesses to itself and its subregions. It does not
2294 * obscure sibling subregions with lower priority - it simply behaves as
2295 * if it was removed from the hierarchy.
2296 *
2297 * Regions default to being enabled.
2298 *
2299 * @mr: the region to be updated
2300 * @enabled: whether to enable or disable the region
2301 */
2302void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
2303
2282e1af
AK
2304/*
2305 * memory_region_set_address: dynamically update the address of a region
2306 *
feca4ac1 2307 * Dynamically updates the address of a region, relative to its container.
2282e1af
AK
2308 * May be used on regions are currently part of a memory hierarchy.
2309 *
2310 * @mr: the region to be updated
feca4ac1 2311 * @addr: new address, relative to container region
2282e1af 2312 */
a8170e5e 2313void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
2282e1af 2314
e7af4c67
MT
2315/*
2316 * memory_region_set_size: dynamically update the size of a region.
2317 *
2318 * Dynamically updates the size of a region.
2319 *
2320 * @mr: the region to be updated
2321 * @size: used size of the region.
2322 */
2323void memory_region_set_size(MemoryRegion *mr, uint64_t size);
2324
4703359e
AK
2325/*
2326 * memory_region_set_alias_offset: dynamically update a memory alias's offset
2327 *
2328 * Dynamically updates the offset into the target region that an alias points
2329 * to, as if the fourth argument to memory_region_init_alias() has changed.
2330 *
2331 * @mr: the #MemoryRegion to be updated; should be an alias.
2332 * @offset: the new offset into the target memory region
2333 */
2334void memory_region_set_alias_offset(MemoryRegion *mr,
a8170e5e 2335 hwaddr offset);
4703359e 2336
3ce10901 2337/**
feca4ac1
PB
2338 * memory_region_present: checks if an address relative to a @container
2339 * translates into #MemoryRegion within @container
3ce10901 2340 *
feca4ac1 2341 * Answer whether a #MemoryRegion within @container covers the address
3ce10901
PB
2342 * @addr.
2343 *
feca4ac1
PB
2344 * @container: a #MemoryRegion within which @addr is a relative address
2345 * @addr: the area within @container to be searched
3ce10901 2346 */
feca4ac1 2347bool memory_region_present(MemoryRegion *container, hwaddr addr);
3ce10901 2348
eed2bacf
IM
2349/**
2350 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
455faf03
DH
2351 * into another memory region, which does not necessarily imply that it is
2352 * mapped into an address space.
eed2bacf
IM
2353 *
2354 * @mr: a #MemoryRegion which should be checked if it's mapped
2355 */
2356bool memory_region_is_mapped(MemoryRegion *mr);
2357
8947d7fc
DH
2358/**
2359 * memory_region_get_ram_discard_manager: get the #RamDiscardManager for a
2360 * #MemoryRegion
2361 *
2362 * The #RamDiscardManager cannot change while a memory region is mapped.
2363 *
2364 * @mr: the #MemoryRegion
2365 */
2366RamDiscardManager *memory_region_get_ram_discard_manager(MemoryRegion *mr);
2367
2368/**
2369 * memory_region_has_ram_discard_manager: check whether a #MemoryRegion has a
2370 * #RamDiscardManager assigned
2371 *
2372 * @mr: the #MemoryRegion
2373 */
2374static inline bool memory_region_has_ram_discard_manager(MemoryRegion *mr)
2375{
2376 return !!memory_region_get_ram_discard_manager(mr);
2377}
2378
2379/**
2380 * memory_region_set_ram_discard_manager: set the #RamDiscardManager for a
2381 * #MemoryRegion
2382 *
2383 * This function must not be called for a mapped #MemoryRegion, a #MemoryRegion
2384 * that does not cover RAM, or a #MemoryRegion that already has a
2385 * #RamDiscardManager assigned.
2386 *
2387 * @mr: the #MemoryRegion
2388 * @rdm: #RamDiscardManager to set
2389 */
2390void memory_region_set_ram_discard_manager(MemoryRegion *mr,
2391 RamDiscardManager *rdm);
2392
e2177955 2393/**
73034e9e
PB
2394 * memory_region_find: translate an address/size relative to a
2395 * MemoryRegion into a #MemoryRegionSection.
e2177955 2396 *
73034e9e
PB
2397 * Locates the first #MemoryRegion within @mr that overlaps the range
2398 * given by @addr and @size.
e2177955
AK
2399 *
2400 * Returns a #MemoryRegionSection that describes a contiguous overlap.
2401 * It will have the following characteristics:
08226b44
PB
2402 * - @size = 0 iff no overlap was found
2403 * - @mr is non-%NULL iff an overlap was found
e2177955 2404 *
73034e9e
PB
2405 * Remember that in the return value the @offset_within_region is
2406 * relative to the returned region (in the .@mr field), not to the
2407 * @mr argument.
2408 *
2409 * Similarly, the .@offset_within_address_space is relative to the
2410 * address space that contains both regions, the passed and the
2411 * returned one. However, in the special case where the @mr argument
feca4ac1 2412 * has no container (and thus is the root of the address space), the
73034e9e 2413 * following will hold:
08226b44
PB
2414 * - @offset_within_address_space >= @addr
2415 * - @offset_within_address_space + .@size <= @addr + @size
73034e9e
PB
2416 *
2417 * @mr: a MemoryRegion within which @addr is a relative address
2418 * @addr: start of the area within @as to be searched
e2177955
AK
2419 * @size: size of the area to be searched
2420 */
73034e9e 2421MemoryRegionSection memory_region_find(MemoryRegion *mr,
a8170e5e 2422 hwaddr addr, uint64_t size);
e2177955 2423
86e775c6 2424/**
9c1f8f44 2425 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
86e775c6 2426 *
9c1f8f44 2427 * Synchronizes the dirty page log for all address spaces.
1e493be5
GS
2428 *
2429 * @last_stage: whether this is the last stage of live migration
86e775c6 2430 */
1e493be5 2431void memory_global_dirty_log_sync(bool last_stage);
9458a9a1
PB
2432
2433/**
2434 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
2435 *
2436 * Synchronizes the vCPUs with a thread that is reading the dirty bitmap.
2437 * This function must be called after the dirty log bitmap is cleared, and
2438 * before dirty guest memory pages are read. If you are using
2439 * #DirtyBitmapSnapshot, memory_region_snapshot_and_clear_dirty() takes
2440 * care of doing this.
2441 */
2442void memory_global_after_dirty_log_sync(void);
86e775c6 2443
69ddaf66
ASRJ
2444/**
2445 * memory_region_transaction_begin: Start a transaction.
2446 *
2447 * During a transaction, changes will be accumulated and made visible
dabdf394 2448 * only when the transaction ends (is committed).
4ef4db86
AK
2449 */
2450void memory_region_transaction_begin(void);
69ddaf66
ASRJ
2451
2452/**
2453 * memory_region_transaction_commit: Commit a transaction and make changes
2454 * visible to the guest.
4ef4db86
AK
2455 */
2456void memory_region_transaction_commit(void);
2457
7664e80c
AK
2458/**
2459 * memory_listener_register: register callbacks to be called when memory
2460 * sections are mapped or unmapped into an address
2461 * space
2462 *
2463 * @listener: an object containing the callbacks to be called
7376e582 2464 * @filter: if non-%NULL, only regions in this address space will be observed
7664e80c 2465 */
f6790af6 2466void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
7664e80c
AK
2467
2468/**
2469 * memory_listener_unregister: undo the effect of memory_listener_register()
2470 *
2471 * @listener: an object containing the callbacks to be removed
2472 */
2473void memory_listener_unregister(MemoryListener *listener);
2474
2475/**
2476 * memory_global_dirty_log_start: begin dirty logging for all regions
63b41db4
HH
2477 *
2478 * @flags: purpose of starting dirty log, migration or dirty rate
7664e80c 2479 */
63b41db4 2480void memory_global_dirty_log_start(unsigned int flags);
7664e80c
AK
2481
2482/**
1a7e8cae 2483 * memory_global_dirty_log_stop: end dirty logging for all regions
63b41db4
HH
2484 *
2485 * @flags: purpose of stopping dirty log, migration or dirty rate
7664e80c 2486 */
63b41db4 2487void memory_global_dirty_log_stop(unsigned int flags);
7664e80c 2488
2261d393 2489void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled);
314e2987 2490
94e273db
PMD
2491bool memory_region_access_valid(MemoryRegion *mr, hwaddr addr,
2492 unsigned size, bool is_write,
2493 MemTxAttrs attrs);
2494
3b643495
PM
2495/**
2496 * memory_region_dispatch_read: perform a read directly to the specified
2497 * MemoryRegion.
2498 *
2499 * @mr: #MemoryRegion to access
2500 * @addr: address within that region
2501 * @pval: pointer to uint64_t which the data is written to
e67c9046 2502 * @op: size, sign, and endianness of the memory operation
3b643495
PM
2503 * @attrs: memory transaction attributes to use for the access
2504 */
2505MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
2506 hwaddr addr,
2507 uint64_t *pval,
e67c9046 2508 MemOp op,
3b643495
PM
2509 MemTxAttrs attrs);
2510/**
2511 * memory_region_dispatch_write: perform a write directly to the specified
2512 * MemoryRegion.
2513 *
2514 * @mr: #MemoryRegion to access
2515 * @addr: address within that region
2516 * @data: data to write
e67c9046 2517 * @op: size, sign, and endianness of the memory operation
3b643495
PM
2518 * @attrs: memory transaction attributes to use for the access
2519 */
2520MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
2521 hwaddr addr,
2522 uint64_t data,
e67c9046 2523 MemOp op,
3b643495
PM
2524 MemTxAttrs attrs);
2525
9ad2bbc1
AK
2526/**
2527 * address_space_init: initializes an address space
2528 *
2529 * @as: an uninitialized #AddressSpace
67cc32eb 2530 * @root: a #MemoryRegion that routes addresses for the address space
7dca8043
AK
2531 * @name: an address space name. The name is only used for debugging
2532 * output.
9ad2bbc1 2533 */
7dca8043 2534void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
9ad2bbc1 2535
83f3c251
AK
2536/**
2537 * address_space_destroy: destroy an address space
2538 *
2539 * Releases all resources associated with an address space. After an address space
2540 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
2541 * as well.
2542 *
2543 * @as: address space to be destroyed
2544 */
2545void address_space_destroy(AddressSpace *as);
2546
a2166410
GK
2547/**
2548 * address_space_remove_listeners: unregister all listeners of an address space
2549 *
2550 * Removes all callbacks previously registered with memory_listener_register()
2551 * for @as.
2552 *
2553 * @as: an initialized #AddressSpace
2554 */
2555void address_space_remove_listeners(AddressSpace *as);
2556
ac1970fb
AK
2557/**
2558 * address_space_rw: read from or write to an address space.
2559 *
5c9eb028
PM
2560 * Return a MemTxResult indicating whether the operation succeeded
2561 * or failed (eg unassigned memory, device rejected the transaction,
2562 * IOMMU fault).
fd8aaa76 2563 *
ac1970fb
AK
2564 * @as: #AddressSpace to be accessed
2565 * @addr: address within that address space
5c9eb028 2566 * @attrs: memory transaction attributes
ac1970fb 2567 * @buf: buffer with the data transferred
57914ecb 2568 * @len: the number of bytes to read or write
ac1970fb
AK
2569 * @is_write: indicates the transfer direction
2570 */
5c9eb028 2571MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
daa3dda4 2572 MemTxAttrs attrs, void *buf,
0c249ff7 2573 hwaddr len, bool is_write);
ac1970fb
AK
2574
2575/**
2576 * address_space_write: write to address space.
2577 *
5c9eb028
PM
2578 * Return a MemTxResult indicating whether the operation succeeded
2579 * or failed (eg unassigned memory, device rejected the transaction,
2580 * IOMMU fault).
fd8aaa76 2581 *
ac1970fb
AK
2582 * @as: #AddressSpace to be accessed
2583 * @addr: address within that address space
5c9eb028 2584 * @attrs: memory transaction attributes
ac1970fb 2585 * @buf: buffer with the data transferred
57914ecb 2586 * @len: the number of bytes to write
ac1970fb 2587 */
5c9eb028
PM
2588MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
2589 MemTxAttrs attrs,
daa3dda4 2590 const void *buf, hwaddr len);
ac1970fb 2591
3c8133f9
PM
2592/**
2593 * address_space_write_rom: write to address space, including ROM.
2594 *
2595 * This function writes to the specified address space, but will
2596 * write data to both ROM and RAM. This is used for non-guest
2597 * writes like writes from the gdb debug stub or initial loading
2598 * of ROM contents.
2599 *
2600 * Note that portions of the write which attempt to write data to
2601 * a device will be silently ignored -- only real RAM and ROM will
2602 * be written to.
2603 *
2604 * Return a MemTxResult indicating whether the operation succeeded
2605 * or failed (eg unassigned memory, device rejected the transaction,
2606 * IOMMU fault).
2607 *
2608 * @as: #AddressSpace to be accessed
2609 * @addr: address within that address space
2610 * @attrs: memory transaction attributes
2611 * @buf: buffer with the data transferred
2612 * @len: the number of bytes to write
2613 */
2614MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
2615 MemTxAttrs attrs,
daa3dda4 2616 const void *buf, hwaddr len);
3c8133f9 2617
3cc8f884 2618/* address_space_ld*: load from an address space
50013115
PM
2619 * address_space_st*: store to an address space
2620 *
2621 * These functions perform a load or store of the byte, word,
2622 * longword or quad to the specified address within the AddressSpace.
2623 * The _le suffixed functions treat the data as little endian;
2624 * _be indicates big endian; no suffix indicates "same endianness
2625 * as guest CPU".
2626 *
2627 * The "guest CPU endianness" accessors are deprecated for use outside
2628 * target-* code; devices should be CPU-agnostic and use either the LE
2629 * or the BE accessors.
2630 *
2631 * @as #AddressSpace to be accessed
2632 * @addr: address within that address space
2633 * @val: data value, for stores
2634 * @attrs: memory transaction attributes
2635 * @result: location to write the success/failure of the transaction;
2636 * if NULL, this information is discarded
2637 */
4269c82b
PB
2638
2639#define SUFFIX
2640#define ARG1 as
2641#define ARG1_DECL AddressSpace *as
0979ed01 2642#include "exec/memory_ldst.h.inc"
4269c82b
PB
2643
2644#define SUFFIX
2645#define ARG1 as
2646#define ARG1_DECL AddressSpace *as
0979ed01 2647#include "exec/memory_ldst_phys.h.inc"
0ce265ff 2648
1f4e496e 2649struct MemoryRegionCache {
48564041 2650 void *ptr;
1f4e496e 2651 hwaddr xlat;
1f4e496e 2652 hwaddr len;
48564041
PB
2653 FlatView *fv;
2654 MemoryRegionSection mrs;
2655 bool is_write;
1f4e496e
PB
2656};
2657
48564041
PB
2658#define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
2659
5eba0404 2660
4269c82b
PB
2661/* address_space_ld*_cached: load from a cached #MemoryRegion
2662 * address_space_st*_cached: store into a cached #MemoryRegion
2663 *
2664 * These functions perform a load or store of the byte, word,
2665 * longword or quad to the specified address. The address is
2666 * a physical address in the AddressSpace, but it must lie within
2667 * a #MemoryRegion that was mapped with address_space_cache_init.
2668 *
2669 * The _le suffixed functions treat the data as little endian;
2670 * _be indicates big endian; no suffix indicates "same endianness
2671 * as guest CPU".
2672 *
2673 * The "guest CPU endianness" accessors are deprecated for use outside
2674 * target-* code; devices should be CPU-agnostic and use either the LE
2675 * or the BE accessors.
2676 *
2677 * @cache: previously initialized #MemoryRegionCache to be accessed
2678 * @addr: address within the address space
2679 * @val: data value, for stores
2680 * @attrs: memory transaction attributes
2681 * @result: location to write the success/failure of the transaction;
2682 * if NULL, this information is discarded
2683 */
2684
48564041 2685#define SUFFIX _cached_slow
4269c82b
PB
2686#define ARG1 cache
2687#define ARG1_DECL MemoryRegionCache *cache
0979ed01 2688#include "exec/memory_ldst.h.inc"
4269c82b 2689
48564041
PB
2690/* Inline fast path for direct RAM access. */
2691static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache,
2692 hwaddr addr, MemTxAttrs attrs, MemTxResult *result)
2693{
2694 assert(addr < cache->len);
2695 if (likely(cache->ptr)) {
2696 return ldub_p(cache->ptr + addr);
2697 } else {
2698 return address_space_ldub_cached_slow(cache, addr, attrs, result);
2699 }
2700}
2701
2702static inline void address_space_stb_cached(MemoryRegionCache *cache,
4121f4b3 2703 hwaddr addr, uint8_t val, MemTxAttrs attrs, MemTxResult *result)
48564041
PB
2704{
2705 assert(addr < cache->len);
2706 if (likely(cache->ptr)) {
2707 stb_p(cache->ptr + addr, val);
2708 } else {
2709 address_space_stb_cached_slow(cache, addr, val, attrs, result);
2710 }
2711}
2712
2713#define ENDIANNESS _le
0979ed01 2714#include "exec/memory_ldst_cached.h.inc"
48564041
PB
2715
2716#define ENDIANNESS _be
0979ed01 2717#include "exec/memory_ldst_cached.h.inc"
48564041 2718
4269c82b
PB
2719#define SUFFIX _cached
2720#define ARG1 cache
2721#define ARG1_DECL MemoryRegionCache *cache
0979ed01 2722#include "exec/memory_ldst_phys.h.inc"
4269c82b 2723
1f4e496e
PB
2724/* address_space_cache_init: prepare for repeated access to a physical
2725 * memory region
2726 *
2727 * @cache: #MemoryRegionCache to be filled
2728 * @as: #AddressSpace to be accessed
2729 * @addr: address within that address space
2730 * @len: length of buffer
2731 * @is_write: indicates the transfer direction
2732 *
2733 * Will only work with RAM, and may map a subset of the requested range by
2734 * returning a value that is less than @len. On failure, return a negative
2735 * errno value.
2736 *
2737 * Because it only works with RAM, this function can be used for
2738 * read-modify-write operations. In this case, is_write should be %true.
2739 *
2740 * Note that addresses passed to the address_space_*_cached functions
2741 * are relative to @addr.
2742 */
2743int64_t address_space_cache_init(MemoryRegionCache *cache,
2744 AddressSpace *as,
2745 hwaddr addr,
2746 hwaddr len,
2747 bool is_write);
2748
2749/**
2750 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache
2751 *
2752 * @cache: The #MemoryRegionCache to operate on.
2753 * @addr: The first physical address that was written, relative to the
2754 * address that was passed to @address_space_cache_init.
2755 * @access_len: The number of bytes that were written starting at @addr.
2756 */
2757void address_space_cache_invalidate(MemoryRegionCache *cache,
2758 hwaddr addr,
2759 hwaddr access_len);
2760
2761/**
2762 * address_space_cache_destroy: free a #MemoryRegionCache
2763 *
2764 * @cache: The #MemoryRegionCache whose memory should be released.
2765 */
2766void address_space_cache_destroy(MemoryRegionCache *cache);
2767
052c8fa9
JW
2768/* address_space_get_iotlb_entry: translate an address into an IOTLB
2769 * entry. Should be called from an RCU critical section.
2770 */
2771IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
7446eb07 2772 bool is_write, MemTxAttrs attrs);
1f4e496e 2773
149f54b5 2774/* address_space_translate: translate an address range into an address space
41063e1e
PB
2775 * into a MemoryRegion and an address range into that section. Should be
2776 * called from an RCU critical section, to avoid that the last reference
2777 * to the returned region disappears after address_space_translate returns.
149f54b5 2778 *
57914ecb 2779 * @fv: #FlatView to be accessed
149f54b5
PB
2780 * @addr: address within that address space
2781 * @xlat: pointer to address within the returned memory region section's
2782 * #MemoryRegion.
2783 * @len: pointer to length
2784 * @is_write: indicates the transfer direction
bc6b1cec 2785 * @attrs: memory attributes
149f54b5 2786 */
16620684
AK
2787MemoryRegion *flatview_translate(FlatView *fv,
2788 hwaddr addr, hwaddr *xlat,
efa99a2f
PM
2789 hwaddr *len, bool is_write,
2790 MemTxAttrs attrs);
16620684
AK
2791
2792static inline MemoryRegion *address_space_translate(AddressSpace *as,
2793 hwaddr addr, hwaddr *xlat,
bc6b1cec
PM
2794 hwaddr *len, bool is_write,
2795 MemTxAttrs attrs)
16620684
AK
2796{
2797 return flatview_translate(address_space_to_flatview(as),
efa99a2f 2798 addr, xlat, len, is_write, attrs);
16620684 2799}
149f54b5 2800
51644ab7
PB
2801/* address_space_access_valid: check for validity of accessing an address
2802 * space range
2803 *
30951157
AK
2804 * Check whether memory is assigned to the given address space range, and
2805 * access is permitted by any IOMMU regions that are active for the address
2806 * space.
51644ab7
PB
2807 *
2808 * For now, addr and len should be aligned to a page size. This limitation
2809 * will be lifted in the future.
2810 *
2811 * @as: #AddressSpace to be accessed
2812 * @addr: address within that address space
2813 * @len: length of the area to be checked
2814 * @is_write: indicates the transfer direction
fddffa42 2815 * @attrs: memory attributes
51644ab7 2816 */
0c249ff7 2817bool address_space_access_valid(AddressSpace *as, hwaddr addr, hwaddr len,
fddffa42 2818 bool is_write, MemTxAttrs attrs);
51644ab7 2819
ac1970fb
AK
2820/* address_space_map: map a physical memory region into a host virtual address
2821 *
2822 * May map a subset of the requested range, given by and returned in @plen.
77f55eac
PP
2823 * May return %NULL and set *@plen to zero(0), if resources needed to perform
2824 * the mapping are exhausted.
ac1970fb
AK
2825 * Use only for reads OR writes - not for read-modify-write operations.
2826 * Use cpu_register_map_client() to know when retrying the map operation is
2827 * likely to succeed.
2828 *
2829 * @as: #AddressSpace to be accessed
2830 * @addr: address within that address space
2831 * @plen: pointer to length of buffer; updated on return
2832 * @is_write: indicates the transfer direction
f26404fb 2833 * @attrs: memory attributes
ac1970fb 2834 */
a8170e5e 2835void *address_space_map(AddressSpace *as, hwaddr addr,
f26404fb 2836 hwaddr *plen, bool is_write, MemTxAttrs attrs);
ac1970fb
AK
2837
2838/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
2839 *
2840 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
2841 * the amount of memory that was actually read or written by the caller.
2842 *
2843 * @as: #AddressSpace used
57914ecb 2844 * @buffer: host pointer as returned by address_space_map()
ac1970fb
AK
2845 * @len: buffer length as returned by address_space_map()
2846 * @access_len: amount of data actually transferred
2847 * @is_write: indicates the transfer direction
2848 */
a8170e5e 2849void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
ae5883ab 2850 bool is_write, hwaddr access_len);
ac1970fb
AK
2851
2852
a203ac70 2853/* Internal functions, part of the implementation of address_space_read. */
b2a44fca 2854MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
daa3dda4 2855 MemTxAttrs attrs, void *buf, hwaddr len);
16620684 2856MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
a152be43 2857 MemTxAttrs attrs, void *buf,
0c249ff7 2858 hwaddr len, hwaddr addr1, hwaddr l,
16620684 2859 MemoryRegion *mr);
0878d0e1 2860void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
3cc8f884 2861
48564041
PB
2862/* Internal functions, part of the implementation of address_space_read_cached
2863 * and address_space_write_cached. */
38df19fa
PMD
2864MemTxResult address_space_read_cached_slow(MemoryRegionCache *cache,
2865 hwaddr addr, void *buf, hwaddr len);
2866MemTxResult address_space_write_cached_slow(MemoryRegionCache *cache,
2867 hwaddr addr, const void *buf,
2868 hwaddr len);
48564041 2869
3123f93d
JR
2870int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr);
2871bool prepare_mmio_access(MemoryRegion *mr);
2872
3cc8f884
PB
2873static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
2874{
2875 if (is_write) {
d489ae4a
AD
2876 return memory_region_is_ram(mr) && !mr->readonly &&
2877 !mr->rom_device && !memory_region_is_ram_device(mr);
3cc8f884 2878 } else {
4a2e242b
AW
2879 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
2880 memory_region_is_romd(mr);
3cc8f884 2881 }
3cc8f884
PB
2882}
2883
2884/**
2885 * address_space_read: read from an address space.
2886 *
2887 * Return a MemTxResult indicating whether the operation succeeded
2888 * or failed (eg unassigned memory, device rejected the transaction,
b2a44fca 2889 * IOMMU fault). Called within RCU critical section.
3cc8f884 2890 *
b2a44fca 2891 * @as: #AddressSpace to be accessed
3cc8f884
PB
2892 * @addr: address within that address space
2893 * @attrs: memory transaction attributes
2894 * @buf: buffer with the data transferred
5d248213 2895 * @len: length of the data transferred
3cc8f884
PB
2896 */
2897static inline __attribute__((__always_inline__))
b2a44fca 2898MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
daa3dda4 2899 MemTxAttrs attrs, void *buf,
0c249ff7 2900 hwaddr len)
3cc8f884
PB
2901{
2902 MemTxResult result = MEMTX_OK;
2903 hwaddr l, addr1;
2904 void *ptr;
2905 MemoryRegion *mr;
b2a44fca 2906 FlatView *fv;
3cc8f884
PB
2907
2908 if (__builtin_constant_p(len)) {
2909 if (len) {
293a733d 2910 RCU_READ_LOCK_GUARD();
b2a44fca 2911 fv = address_space_to_flatview(as);
3cc8f884 2912 l = len;
efa99a2f 2913 mr = flatview_translate(fv, addr, &addr1, &l, false, attrs);
3cc8f884 2914 if (len == l && memory_access_is_direct(mr, false)) {
0878d0e1 2915 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
3cc8f884
PB
2916 memcpy(buf, ptr, len);
2917 } else {
16620684
AK
2918 result = flatview_read_continue(fv, addr, attrs, buf, len,
2919 addr1, l, mr);
3cc8f884 2920 }
3cc8f884
PB
2921 }
2922 } else {
b2a44fca 2923 result = address_space_read_full(as, addr, attrs, buf, len);
3cc8f884
PB
2924 }
2925 return result;
2926}
a203ac70 2927
1f4e496e
PB
2928/**
2929 * address_space_read_cached: read from a cached RAM region
2930 *
2931 * @cache: Cached region to be addressed
2932 * @addr: address relative to the base of the RAM region
2933 * @buf: buffer with the data transferred
2934 * @len: length of the data transferred
2935 */
38df19fa 2936static inline MemTxResult
1f4e496e 2937address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
0c249ff7 2938 void *buf, hwaddr len)
1f4e496e
PB
2939{
2940 assert(addr < cache->len && len <= cache->len - addr);
fc1c8344 2941 fuzz_dma_read_cb(cache->xlat + addr, len, cache->mrs.mr);
48564041
PB
2942 if (likely(cache->ptr)) {
2943 memcpy(buf, cache->ptr + addr, len);
38df19fa 2944 return MEMTX_OK;
48564041 2945 } else {
38df19fa 2946 return address_space_read_cached_slow(cache, addr, buf, len);
48564041 2947 }
1f4e496e
PB
2948}
2949
2950/**
2951 * address_space_write_cached: write to a cached RAM region
2952 *
2953 * @cache: Cached region to be addressed
2954 * @addr: address relative to the base of the RAM region
2955 * @buf: buffer with the data transferred
2956 * @len: length of the data transferred
2957 */
38df19fa 2958static inline MemTxResult
1f4e496e 2959address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
daa3dda4 2960 const void *buf, hwaddr len)
1f4e496e
PB
2961{
2962 assert(addr < cache->len && len <= cache->len - addr);
48564041
PB
2963 if (likely(cache->ptr)) {
2964 memcpy(cache->ptr + addr, buf, len);
38df19fa 2965 return MEMTX_OK;
48564041 2966 } else {
38df19fa 2967 return address_space_write_cached_slow(cache, addr, buf, len);
48564041 2968 }
1f4e496e
PB
2969}
2970
75f01c68
PMD
2971/**
2972 * address_space_set: Fill address space with a constant byte.
2973 *
2974 * Return a MemTxResult indicating whether the operation succeeded
2975 * or failed (eg unassigned memory, device rejected the transaction,
2976 * IOMMU fault).
2977 *
2978 * @as: #AddressSpace to be accessed
2979 * @addr: address within that address space
2980 * @c: constant byte to fill the memory
2981 * @len: the number of bytes to fill with the constant byte
2982 * @attrs: memory transaction attributes
2983 */
2984MemTxResult address_space_set(AddressSpace *as, hwaddr addr,
2985 uint8_t c, hwaddr len, MemTxAttrs attrs);
2986
7a3df11c 2987#ifdef NEED_CPU_H
d5d680ca 2988/* enum device_endian to MemOp. */
7a3df11c
PB
2989static inline MemOp devend_memop(enum device_endian end)
2990{
2991 QEMU_BUILD_BUG_ON(DEVICE_HOST_ENDIAN != DEVICE_LITTLE_ENDIAN &&
2992 DEVICE_HOST_ENDIAN != DEVICE_BIG_ENDIAN);
2993
ee3eb3a7 2994#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
7a3df11c
PB
2995 /* Swap if non-host endianness or native (target) endianness */
2996 return (end == DEVICE_HOST_ENDIAN) ? 0 : MO_BSWAP;
2997#else
2998 const int non_host_endianness =
2999 DEVICE_LITTLE_ENDIAN ^ DEVICE_BIG_ENDIAN ^ DEVICE_HOST_ENDIAN;
3000
3001 /* In this case, native (target) endianness needs no swap. */
3002 return (end == non_host_endianness) ? MO_BSWAP : 0;
3003#endif
3004}
3005#endif
d5d680ca 3006
d24f31db
DH
3007/*
3008 * Inhibit technologies that require discarding of pages in RAM blocks, e.g.,
3009 * to manage the actual amount of memory consumed by the VM (then, the memory
3010 * provided by RAM blocks might be bigger than the desired memory consumption).
3011 * This *must* be set if:
3012 * - Discarding parts of a RAM blocks does not result in the change being
3013 * reflected in the VM and the pages getting freed.
3014 * - All memory in RAM blocks is pinned or duplicated, invaldiating any previous
3015 * discards blindly.
3016 * - Discarding parts of a RAM blocks will result in integrity issues (e.g.,
3017 * encrypted VMs).
3018 * Technologies that only temporarily pin the current working set of a
3019 * driver are fine, because we don't expect such pages to be discarded
3020 * (esp. based on guest action like balloon inflation).
3021 *
3022 * This is *not* to be used to protect from concurrent discards (esp.,
3023 * postcopy).
3024 *
3025 * Returns 0 if successful. Returns -EBUSY if a technology that relies on
3026 * discards to work reliably is active.
3027 */
3028int ram_block_discard_disable(bool state);
3029
7e6d32eb
DH
3030/*
3031 * See ram_block_discard_disable(): only disable uncoordinated discards,
3032 * keeping coordinated discards (via the RamDiscardManager) enabled.
3033 */
3034int ram_block_uncoordinated_discard_disable(bool state);
3035
d24f31db
DH
3036/*
3037 * Inhibit technologies that disable discarding of pages in RAM blocks.
3038 *
3039 * Returns 0 if successful. Returns -EBUSY if discards are already set to
3040 * broken.
3041 */
3042int ram_block_discard_require(bool state);
3043
3044/*
7e6d32eb
DH
3045 * See ram_block_discard_require(): only inhibit technologies that disable
3046 * uncoordinated discarding of pages in RAM blocks, allowing co-existance with
3047 * technologies that only inhibit uncoordinated discards (via the
3048 * RamDiscardManager).
3049 */
3050int ram_block_coordinated_discard_require(bool state);
3051
3052/*
3053 * Test if any discarding of memory in ram blocks is disabled.
d24f31db
DH
3054 */
3055bool ram_block_discard_is_disabled(void);
3056
3057/*
7e6d32eb 3058 * Test if any discarding of memory in ram blocks is required to work reliably.
d24f31db
DH
3059 */
3060bool ram_block_discard_is_required(void);
3061
093bc2cd
AK
3062#endif
3063
3064#endif