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