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