]> git.proxmox.com Git - mirror_qemu.git/blob - include/exec/memory.h
vhost-vdpa: introduce vhost-vdpa net client
[mirror_qemu.git] / include / exec / memory.h
1 /*
2 * Physical memory management API
3 *
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
5 *
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14 #ifndef MEMORY_H
15 #define MEMORY_H
16
17 #ifndef CONFIG_USER_ONLY
18
19 #include "exec/cpu-common.h"
20 #include "exec/hwaddr.h"
21 #include "exec/memattrs.h"
22 #include "exec/memop.h"
23 #include "exec/ramlist.h"
24 #include "qemu/bswap.h"
25 #include "qemu/queue.h"
26 #include "qemu/int128.h"
27 #include "qemu/notify.h"
28 #include "qom/object.h"
29 #include "qemu/rcu.h"
30
31 #define RAM_ADDR_INVALID (~(ram_addr_t)0)
32
33 #define MAX_PHYS_ADDR_SPACE_BITS 62
34 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
35
36 #define TYPE_MEMORY_REGION "qemu:memory-region"
37 #define MEMORY_REGION(obj) \
38 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
39
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)
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)
49
50 extern bool global_dirty_log;
51
52 typedef struct MemoryRegionOps MemoryRegionOps;
53
54 typedef struct IOMMUTLBEntry IOMMUTLBEntry;
55
56 /* See address_space_translate: bit 0 is read, bit 1 is write. */
57 typedef enum {
58 IOMMU_NONE = 0,
59 IOMMU_RO = 1,
60 IOMMU_WO = 2,
61 IOMMU_RW = 3,
62 } IOMMUAccessFlags;
63
64 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0))
65
66 struct IOMMUTLBEntry {
67 AddressSpace *target_as;
68 hwaddr iova;
69 hwaddr translated_addr;
70 hwaddr addr_mask; /* 0xfff = 4k translation */
71 IOMMUAccessFlags perm;
72 };
73
74 /*
75 * Bitmap for different IOMMUNotifier capabilities. Each notifier can
76 * register with one or multiple IOMMU Notifier capability bit(s).
77 */
78 typedef 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
88 struct IOMMUNotifier;
89 typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier,
90 IOMMUTLBEntry *data);
91
92 struct IOMMUNotifier {
93 IOMMUNotify notify;
94 IOMMUNotifierFlag notifier_flags;
95 /* Notify for address space range start <= addr <= end */
96 hwaddr start;
97 hwaddr end;
98 int iommu_idx;
99 QLIST_ENTRY(IOMMUNotifier) node;
100 };
101 typedef struct IOMMUNotifier IOMMUNotifier;
102
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
123 /* RAM is a persistent kind memory */
124 #define RAM_PMEM (1 << 5)
125
126 static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn,
127 IOMMUNotifierFlag flags,
128 hwaddr start, hwaddr end,
129 int iommu_idx)
130 {
131 n->notify = fn;
132 n->notifier_flags = flags;
133 n->start = start;
134 n->end = end;
135 n->iommu_idx = iommu_idx;
136 }
137
138 /*
139 * Memory region callbacks
140 */
141 struct MemoryRegionOps {
142 /* Read from the memory region. @addr is relative to @mr; @size is
143 * in bytes. */
144 uint64_t (*read)(void *opaque,
145 hwaddr addr,
146 unsigned size);
147 /* Write to the memory region. @addr is relative to @mr; @size is
148 * in bytes. */
149 void (*write)(void *opaque,
150 hwaddr addr,
151 uint64_t data,
152 unsigned size);
153
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
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;
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 */
182 bool (*accepts)(void *opaque, hwaddr addr,
183 unsigned size, bool is_write,
184 MemTxAttrs attrs);
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 */
199 bool unaligned;
200 } impl;
201 };
202
203 typedef struct MemoryRegionClass {
204 /* private */
205 ObjectClass parent_class;
206 } MemoryRegionClass;
207
208
209 enum IOMMUMemoryRegionAttr {
210 IOMMU_ATTR_SPAPR_TCE_FD
211 };
212
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).
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.
238 */
239 typedef struct IOMMUMemoryRegionClass {
240 /* private */
241 MemoryRegionClass parent_class;
242
243 /*
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
266 * @iommu_idx: IOMMU index for the translation
267 */
268 IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr,
269 IOMMUAccessFlags flag, int iommu_idx);
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 */
276 uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu);
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
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
290 */
291 int (*notify_flag_changed)(IOMMUMemoryRegion *iommu,
292 IOMMUNotifierFlag old_flags,
293 IOMMUNotifierFlag new_flags,
294 Error **errp);
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 */
310 void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier);
311
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,
328 void *data);
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);
352 } IOMMUMemoryRegionClass;
353
354 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
355 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
356
357 /** MemoryRegion:
358 *
359 * A struct representing a memory region.
360 */
361 struct MemoryRegion {
362 Object parent_obj;
363
364 /* private: */
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 */
371 bool nonvolatile;
372 bool rom_device;
373 bool flush_coalesced_mmio;
374 bool global_locking;
375 uint8_t dirty_log_mask;
376 bool is_iommu;
377 RAMBlock *ram_block;
378 Object *owner;
379
380 const MemoryRegionOps *ops;
381 void *opaque;
382 MemoryRegion *container;
383 Int128 size;
384 hwaddr addr;
385 void (*destructor)(MemoryRegion *mr);
386 uint64_t align;
387 bool terminates;
388 bool ram_device;
389 bool enabled;
390 bool warning_printed; /* For reservations */
391 uint8_t vga_logging_count;
392 MemoryRegion *alias;
393 hwaddr alias_offset;
394 int32_t priority;
395 QTAILQ_HEAD(, MemoryRegion) subregions;
396 QTAILQ_ENTRY(MemoryRegion) subregions_link;
397 QTAILQ_HEAD(, CoalescedMemoryRange) coalesced;
398 const char *name;
399 unsigned ioeventfd_nb;
400 MemoryRegionIoeventfd *ioeventfds;
401 };
402
403 struct IOMMUMemoryRegion {
404 MemoryRegion parent_obj;
405
406 QLIST_HEAD(, IOMMUNotifier) iommu_notify;
407 IOMMUNotifierFlag iommu_notify_flags;
408 };
409
410 #define IOMMU_NOTIFIER_FOREACH(n, mr) \
411 QLIST_FOREACH((n), &(mr)->iommu_notify, node)
412
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 */
419 struct MemoryListener {
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 */
431 void (*begin)(MemoryListener *listener);
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 */
443 void (*commit)(MemoryListener *listener);
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 */
455 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
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 */
467 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
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 */
479 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
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 */
496 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
497 int old, int new);
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 */
515 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
516 int old, int new);
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 */
528 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
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 */
539 void (*log_clear)(MemoryListener *listener, MemoryRegionSection *section);
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 */
552 void (*log_global_start)(MemoryListener *listener);
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 */
563 void (*log_global_stop)(MemoryListener *listener);
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 */
573 void (*log_global_after_sync)(MemoryListener *listener);
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 */
588 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
589 bool match_data, uint64_t data, EventNotifier *e);
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 */
604 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
605 bool match_data, uint64_t data, EventNotifier *e);
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 */
619 void (*coalesced_io_add)(MemoryListener *listener, MemoryRegionSection *section,
620 hwaddr addr, hwaddr len);
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 */
634 void (*coalesced_io_del)(MemoryListener *listener, MemoryRegionSection *section,
635 hwaddr addr, hwaddr len);
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 */
643 unsigned priority;
644
645 /* private: */
646 AddressSpace *address_space;
647 QTAILQ_ENTRY(MemoryListener) link;
648 QTAILQ_ENTRY(MemoryListener) link_as;
649 };
650
651 /**
652 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
653 */
654 struct AddressSpace {
655 /* private: */
656 struct rcu_head rcu;
657 char *name;
658 MemoryRegion *root;
659
660 /* Accessed via RCU. */
661 struct FlatView *current_map;
662
663 int ioeventfd_nb;
664 struct MemoryRegionIoeventfd *ioeventfds;
665 QTAILQ_HEAD(, MemoryListener) listeners;
666 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
667 };
668
669 typedef struct AddressSpaceDispatch AddressSpaceDispatch;
670 typedef struct FlatRange FlatRange;
671
672 /* Flattened global view of current active memory hierarchy. Kept in sorted
673 * order.
674 */
675 struct 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
685 static inline FlatView *address_space_to_flatview(AddressSpace *as)
686 {
687 return atomic_rcu_read(&as->current_map);
688 }
689
690
691 /**
692 * MemoryRegionSection: describes a fragment of a #MemoryRegion
693 *
694 * @mr: the region, or %NULL if empty
695 * @fv: the flat view of the address space the region is mapped in
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
700 * @readonly: writes to this section are ignored
701 * @nonvolatile: this section is non-volatile
702 */
703 struct MemoryRegionSection {
704 Int128 size;
705 MemoryRegion *mr;
706 FlatView *fv;
707 hwaddr offset_within_region;
708 hwaddr offset_within_address_space;
709 bool readonly;
710 bool nonvolatile;
711 };
712
713 static 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
725 /**
726 * memory_region_init: Initialize a memory region
727 *
728 * The region typically acts as a container for other memory regions. Use
729 * memory_region_add_subregion() to add subregions.
730 *
731 * @mr: the #MemoryRegion to be initialized
732 * @owner: the object that tracks the region's reference count
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 */
736 void memory_region_init(MemoryRegion *mr,
737 struct Object *owner,
738 const char *name,
739 uint64_t size);
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 */
756 void 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 */
768 void memory_region_unref(MemoryRegion *mr);
769
770 /**
771 * memory_region_init_io: Initialize an I/O memory region.
772 *
773 * Accesses into the region will cause the callbacks in @ops to be called.
774 * if @size is nonzero, subregions will be clipped to @size.
775 *
776 * @mr: the #MemoryRegion to be initialized.
777 * @owner: the object that tracks the region's reference count
778 * @ops: a structure containing read and write callbacks to be used when
779 * I/O is performed on the region.
780 * @opaque: passed to the read and write callbacks of the @ops structure.
781 * @name: used for debugging; not visible to the user or ABI
782 * @size: size of the region.
783 */
784 void memory_region_init_io(MemoryRegion *mr,
785 struct Object *owner,
786 const MemoryRegionOps *ops,
787 void *opaque,
788 const char *name,
789 uint64_t size);
790
791 /**
792 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses
793 * into the region will modify memory
794 * directly.
795 *
796 * @mr: the #MemoryRegion to be initialized.
797 * @owner: the object that tracks the region's reference count
798 * @name: Region name, becomes part of RAMBlock name used in migration stream
799 * must be unique within any device
800 * @size: size of the region.
801 * @errp: pointer to Error*, to store an error if it happens.
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.
805 */
806 void memory_region_init_ram_nomigrate(MemoryRegion *mr,
807 struct Object *owner,
808 const char *name,
809 uint64_t size,
810 Error **errp);
811
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 */
828 void 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
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
844 * @name: Region name, becomes part of RAMBlock name used in migration stream
845 * must be unique within any device
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.
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.
853 */
854 void 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);
863 #ifdef CONFIG_POSIX
864
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
871 * @name: Region name, becomes part of RAMBlock name used in migration stream
872 * must be unique within any device
873 * @size: size of the region.
874 * @align: alignment of the region base address; if 0, the default alignment
875 * (getpagesize()) will be used.
876 * @ram_flags: Memory region features:
877 * - RAM_SHARED: memory must be mmaped with the MAP_SHARED flag
878 * - RAM_PMEM: the memory is persistent memory
879 * Other bits are ignored now.
880 * @path: the path in which to allocate the RAM.
881 * @errp: pointer to Error*, to store an error if it happens.
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.
885 */
886 void memory_region_init_ram_from_file(MemoryRegion *mr,
887 struct Object *owner,
888 const char *name,
889 uint64_t size,
890 uint64_t align,
891 uint32_t ram_flags,
892 const char *path,
893 Error **errp);
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.
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.
909 */
910 void 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);
917 #endif
918
919 /**
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.
923 *
924 * @mr: the #MemoryRegion to be initialized.
925 * @owner: the object that tracks the region's reference count
926 * @name: Region name, becomes part of RAMBlock name used in migration stream
927 * must be unique within any device
928 * @size: size of the region.
929 * @ptr: memory to be mapped; must contain at least @size bytes.
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.
933 */
934 void memory_region_init_ram_ptr(MemoryRegion *mr,
935 struct Object *owner,
936 const char *name,
937 uint64_t size,
938 void *ptr);
939
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.
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.)
961 */
962 void memory_region_init_ram_device_ptr(MemoryRegion *mr,
963 struct Object *owner,
964 const char *name,
965 uint64_t size,
966 void *ptr);
967
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.
973 * @owner: the object that tracks the region's reference count
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 */
980 void memory_region_init_alias(MemoryRegion *mr,
981 struct Object *owner,
982 const char *name,
983 MemoryRegion *orig,
984 hwaddr offset,
985 uint64_t size);
986
987 /**
988 * memory_region_init_rom_nomigrate: Initialize a ROM memory region.
989 *
990 * This has the same effect as calling memory_region_init_ram_nomigrate()
991 * and then marking the resulting region read-only with
992 * memory_region_set_readonly().
993 *
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 *
998 * @mr: the #MemoryRegion to be initialized.
999 * @owner: the object that tracks the region's reference count
1000 * @name: Region name, becomes part of RAMBlock name used in migration stream
1001 * must be unique within any device
1002 * @size: size of the region.
1003 * @errp: pointer to Error*, to store an error if it happens.
1004 */
1005 void memory_region_init_rom_nomigrate(MemoryRegion *mr,
1006 struct Object *owner,
1007 const char *name,
1008 uint64_t size,
1009 Error **errp);
1010
1011 /**
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.
1018 *
1019 * @mr: the #MemoryRegion to be initialized.
1020 * @owner: the object that tracks the region's reference count
1021 * @ops: callbacks for write access handling (must not be NULL).
1022 * @opaque: passed to the read and write callbacks of the @ops structure.
1023 * @name: Region name, becomes part of RAMBlock name used in migration stream
1024 * must be unique within any device
1025 * @size: size of the region.
1026 * @errp: pointer to Error*, to store an error if it happens.
1027 */
1028 void 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);
1035
1036 /**
1037 * memory_region_init_iommu: Initialize a memory region of a custom type
1038 * that translates addresses
1039 *
1040 * An IOMMU region translates addresses and forwards accesses to a target
1041 * memory region.
1042 *
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 *
1051 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized
1052 * @instance_size: the IOMMUMemoryRegion subclass instance size
1053 * @mrtypename: the type name of the #IOMMUMemoryRegion
1054 * @owner: the object that tracks the region's reference count
1055 * @name: used for debugging; not visible to the user or ABI
1056 * @size: size of the region.
1057 */
1058 void memory_region_init_iommu(void *_iommu_mr,
1059 size_t instance_size,
1060 const char *mrtypename,
1061 Object *owner,
1062 const char *name,
1063 uint64_t size);
1064
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 */
1087 void 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 */
1114 void 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).
1139 * @opaque: passed to the read and write callbacks of the @ops structure.
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 */
1145 void 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
1154 /**
1155 * memory_region_owner: get a memory region's owner.
1156 *
1157 * @mr: the memory region being queried.
1158 */
1159 struct Object *memory_region_owner(MemoryRegion *mr);
1160
1161 /**
1162 * memory_region_size: get a memory region's size.
1163 *
1164 * @mr: the memory region being queried.
1165 */
1166 uint64_t memory_region_size(MemoryRegion *mr);
1167
1168 /**
1169 * memory_region_is_ram: check whether a memory region is random access
1170 *
1171 * Returns %true if a memory region is random access.
1172 *
1173 * @mr: the memory region being queried
1174 */
1175 static inline bool memory_region_is_ram(MemoryRegion *mr)
1176 {
1177 return mr->ram;
1178 }
1179
1180 /**
1181 * memory_region_is_ram_device: check whether a memory region is a ram device
1182 *
1183 * Returns %true if a memory region is a device backed ram region
1184 *
1185 * @mr: the memory region being queried
1186 */
1187 bool memory_region_is_ram_device(MemoryRegion *mr);
1188
1189 /**
1190 * memory_region_is_romd: check whether a memory region is in ROMD mode
1191 *
1192 * Returns %true if a memory region is a ROM device and currently set to allow
1193 * direct reads.
1194 *
1195 * @mr: the memory region being queried
1196 */
1197 static inline bool memory_region_is_romd(MemoryRegion *mr)
1198 {
1199 return mr->rom_device && mr->romd_mode;
1200 }
1201
1202 /**
1203 * memory_region_get_iommu: check whether a memory region is an iommu
1204 *
1205 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu,
1206 * otherwise NULL.
1207 *
1208 * @mr: the memory region being queried
1209 */
1210 static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr)
1211 {
1212 if (mr->alias) {
1213 return memory_region_get_iommu(mr->alias);
1214 }
1215 if (mr->is_iommu) {
1216 return (IOMMUMemoryRegion *) mr;
1217 }
1218 return NULL;
1219 }
1220
1221 /**
1222 * memory_region_get_iommu_class_nocheck: returns iommu memory region class
1223 * if an iommu or NULL if not
1224 *
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.
1227 *
1228 * @iommu_mr: the memory region being queried
1229 */
1230 static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck(
1231 IOMMUMemoryRegion *iommu_mr)
1232 {
1233 return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class);
1234 }
1235
1236 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL)
1237
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 *
1244 * @iommu_mr: the memory region being queried
1245 */
1246 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr);
1247
1248 /**
1249 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
1250 *
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 *
1260 * @iommu_mr: the memory region that was changed
1261 * @iommu_idx: the IOMMU index for the translation table which has changed
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 */
1266 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr,
1267 int iommu_idx,
1268 IOMMUTLBEntry entry);
1269
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 */
1282 void memory_region_notify_one(IOMMUNotifier *notifier,
1283 IOMMUTLBEntry *entry);
1284
1285 /**
1286 * memory_region_register_iommu_notifier: register a notifier for changes to
1287 * IOMMU translation entries.
1288 *
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 *
1294 * @mr: the memory region to observe
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.
1298 * @errp: pointer to Error*, to store an error if it happens.
1299 */
1300 int memory_region_register_iommu_notifier(MemoryRegion *mr,
1301 IOMMUNotifier *n, Error **errp);
1302
1303 /**
1304 * memory_region_iommu_replay: replay existing IOMMU translations to
1305 * a notifier with the minimum page granularity returned by
1306 * mr->iommu_ops->get_page_size().
1307 *
1308 * Note: this is not related to record-and-replay functionality.
1309 *
1310 * @iommu_mr: the memory region to observe
1311 * @n: the notifier to which to replay iommu mappings
1312 */
1313 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n);
1314
1315 /**
1316 * memory_region_unregister_iommu_notifier: unregister a notifier for
1317 * changes to IOMMU translation entries.
1318 *
1319 * @mr: the memory region which was observed and for which notity_stopped()
1320 * needs to be called
1321 * @n: the notifier to be removed.
1322 */
1323 void memory_region_unregister_iommu_notifier(MemoryRegion *mr,
1324 IOMMUNotifier *n);
1325
1326 /**
1327 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is
1328 * defined on the IOMMU.
1329 *
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.
1333 *
1334 * @iommu_mr: the memory region
1335 * @attr: the requested attribute
1336 * @data: a pointer to the requested attribute data
1337 */
1338 int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr,
1339 enum IOMMUMemoryRegionAttr attr,
1340 void *data);
1341
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 */
1349 int 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 */
1358 int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr);
1359
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 */
1367 const char *memory_region_name(const MemoryRegion *mr);
1368
1369 /**
1370 * memory_region_is_logging: return whether a memory region is logging writes
1371 *
1372 * Returns %true if the memory region is logging writes for the given client
1373 *
1374 * @mr: the memory region being queried
1375 * @client: the client being queried
1376 */
1377 bool 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 *
1383 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
1384 * are the bit indices.
1385 *
1386 * @mr: the memory region being queried
1387 */
1388 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
1389
1390 /**
1391 * memory_region_is_rom: check whether a memory region is ROM
1392 *
1393 * Returns %true if a memory region is read-only memory.
1394 *
1395 * @mr: the memory region being queried
1396 */
1397 static inline bool memory_region_is_rom(MemoryRegion *mr)
1398 {
1399 return mr->ram && mr->readonly;
1400 }
1401
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 */
1409 static inline bool memory_region_is_nonvolatile(MemoryRegion *mr)
1410 {
1411 return mr->nonvolatile;
1412 }
1413
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 */
1422 int memory_region_get_fd(MemoryRegion *mr);
1423
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 *
1438 * @ptr: the host pointer to be converted
1439 * @offset: the offset within memory region
1440 */
1441 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
1442
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
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.
1454 *
1455 * @mr: the memory region being queried.
1456 */
1457 void *memory_region_get_ram_ptr(MemoryRegion *mr);
1458
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 */
1468 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
1469 Error **errp);
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 */
1479 void memory_region_msync(MemoryRegion *mr, hwaddr addr, hwaddr size);
1480
1481 /**
1482 * memory_region_writeback: Trigger cache writeback for
1483 * selected address range
1484 *
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
1488 */
1489 void memory_region_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size);
1490
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.
1499 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
1500 */
1501 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
1502
1503 /**
1504 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
1505 *
1506 * Marks a range of bytes as dirty, after it has been dirtied outside
1507 * guest code.
1508 *
1509 * @mr: the memory region being dirtied.
1510 * @addr: the address (relative to the start of the region) being dirtied.
1511 * @size: size of the range being dirtied.
1512 */
1513 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
1514 hwaddr size);
1515
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 */
1529 void memory_region_clear_dirty_bitmap(MemoryRegion *mr, hwaddr start,
1530 hwaddr len);
1531
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
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.
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 */
1559 DirtyBitmapSnapshot *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 */
1573 bool memory_region_snapshot_get_dirty(MemoryRegion *mr,
1574 DirtyBitmapSnapshot *snap,
1575 hwaddr addr, hwaddr size);
1576
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 */
1589 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
1590 hwaddr size, unsigned client);
1591
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 */
1608 void memory_region_flush_rom_device(MemoryRegion *mr, hwaddr addr, hwaddr size);
1609
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 */
1619 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
1620
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 */
1630 void memory_region_set_nonvolatile(MemoryRegion *mr, bool nonvolatile);
1631
1632 /**
1633 * memory_region_rom_device_set_romd: enable/disable ROMD mode
1634 *
1635 * Allows a ROM device (initialized with memory_region_init_rom_device() to
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.
1640 *
1641 * @mr: the memory region to be updated
1642 * @romd_mode: %true to put the region into ROMD mode
1643 */
1644 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
1645
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 */
1655 void 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 */
1668 void memory_region_add_coalescing(MemoryRegion *mr,
1669 hwaddr offset,
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 */
1681 void memory_region_clear_coalescing(MemoryRegion *mr);
1682
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 */
1693 void 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 */
1706 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
1707
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 */
1719 void memory_region_clear_global_locking(MemoryRegion *mr);
1720
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.
1727 * The caller must be prepared to handle failure (that is, take the required
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
1735 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1736 **/
1737 void memory_region_add_eventfd(MemoryRegion *mr,
1738 hwaddr addr,
1739 unsigned size,
1740 bool match_data,
1741 uint64_t data,
1742 EventNotifier *e);
1743
1744 /**
1745 * memory_region_del_eventfd: Cancel an eventfd.
1746 *
1747 * Cancels an eventfd trigger requested by a previous
1748 * memory_region_add_eventfd() call.
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
1755 * @e: event notifier to be triggered when @addr, @size, and @data all match.
1756 */
1757 void memory_region_del_eventfd(MemoryRegion *mr,
1758 hwaddr addr,
1759 unsigned size,
1760 bool match_data,
1761 uint64_t data,
1762 EventNotifier *e);
1763
1764 /**
1765 * memory_region_add_subregion: Add a subregion to a container.
1766 *
1767 * Adds a subregion at @offset. The subregion may not overlap with other
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 */
1778 void memory_region_add_subregion(MemoryRegion *mr,
1779 hwaddr offset,
1780 MemoryRegion *subregion);
1781 /**
1782 * memory_region_add_subregion_overlap: Add a subregion to a container
1783 * with overlap.
1784 *
1785 * Adds a subregion at @offset. The subregion may overlap with other
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 */
1798 void memory_region_add_subregion_overlap(MemoryRegion *mr,
1799 hwaddr offset,
1800 MemoryRegion *subregion,
1801 int priority);
1802
1803 /**
1804 * memory_region_get_ram_addr: Get the ram address associated with a memory
1805 * region
1806 *
1807 * @mr: the region to be queried
1808 */
1809 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
1810
1811 uint64_t memory_region_get_alignment(const MemoryRegion *mr);
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 */
1820 void memory_region_del_subregion(MemoryRegion *mr,
1821 MemoryRegion *subregion);
1822
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 */
1836 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1837
1838 /*
1839 * memory_region_set_address: dynamically update the address of a region
1840 *
1841 * Dynamically updates the address of a region, relative to its container.
1842 * May be used on regions are currently part of a memory hierarchy.
1843 *
1844 * @mr: the region to be updated
1845 * @addr: new address, relative to container region
1846 */
1847 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
1848
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 */
1857 void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1858
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 */
1868 void memory_region_set_alias_offset(MemoryRegion *mr,
1869 hwaddr offset);
1870
1871 /**
1872 * memory_region_present: checks if an address relative to a @container
1873 * translates into #MemoryRegion within @container
1874 *
1875 * Answer whether a #MemoryRegion within @container covers the address
1876 * @addr.
1877 *
1878 * @container: a #MemoryRegion within which @addr is a relative address
1879 * @addr: the area within @container to be searched
1880 */
1881 bool memory_region_present(MemoryRegion *container, hwaddr addr);
1882
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 */
1889 bool memory_region_is_mapped(MemoryRegion *mr);
1890
1891 /**
1892 * memory_region_find: translate an address/size relative to a
1893 * MemoryRegion into a #MemoryRegionSection.
1894 *
1895 * Locates the first #MemoryRegion within @mr that overlaps the range
1896 * given by @addr and @size.
1897 *
1898 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1899 * It will have the following characteristics:
1900 * - @size = 0 iff no overlap was found
1901 * - @mr is non-%NULL iff an overlap was found
1902 *
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
1910 * has no container (and thus is the root of the address space), the
1911 * following will hold:
1912 * - @offset_within_address_space >= @addr
1913 * - @offset_within_address_space + .@size <= @addr + @size
1914 *
1915 * @mr: a MemoryRegion within which @addr is a relative address
1916 * @addr: start of the area within @as to be searched
1917 * @size: size of the area to be searched
1918 */
1919 MemoryRegionSection memory_region_find(MemoryRegion *mr,
1920 hwaddr addr, uint64_t size);
1921
1922 /**
1923 * memory_global_dirty_log_sync: synchronize the dirty log for all memory
1924 *
1925 * Synchronizes the dirty page log for all address spaces.
1926 */
1927 void memory_global_dirty_log_sync(void);
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 */
1938 void memory_global_after_dirty_log_sync(void);
1939
1940 /**
1941 * memory_region_transaction_begin: Start a transaction.
1942 *
1943 * During a transaction, changes will be accumulated and made visible
1944 * only when the transaction ends (is committed).
1945 */
1946 void memory_region_transaction_begin(void);
1947
1948 /**
1949 * memory_region_transaction_commit: Commit a transaction and make changes
1950 * visible to the guest.
1951 */
1952 void memory_region_transaction_commit(void);
1953
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
1960 * @filter: if non-%NULL, only regions in this address space will be observed
1961 */
1962 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
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 */
1969 void memory_listener_unregister(MemoryListener *listener);
1970
1971 /**
1972 * memory_global_dirty_log_start: begin dirty logging for all regions
1973 */
1974 void memory_global_dirty_log_start(void);
1975
1976 /**
1977 * memory_global_dirty_log_stop: end dirty logging for all regions
1978 */
1979 void memory_global_dirty_log_stop(void);
1980
1981 void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled);
1982
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
1990 * @op: size, sign, and endianness of the memory operation
1991 * @attrs: memory transaction attributes to use for the access
1992 */
1993 MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1994 hwaddr addr,
1995 uint64_t *pval,
1996 MemOp op,
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
2005 * @op: size, sign, and endianness of the memory operation
2006 * @attrs: memory transaction attributes to use for the access
2007 */
2008 MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
2009 hwaddr addr,
2010 uint64_t data,
2011 MemOp op,
2012 MemTxAttrs attrs);
2013
2014 /**
2015 * address_space_init: initializes an address space
2016 *
2017 * @as: an uninitialized #AddressSpace
2018 * @root: a #MemoryRegion that routes addresses for the address space
2019 * @name: an address space name. The name is only used for debugging
2020 * output.
2021 */
2022 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
2023
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 */
2033 void address_space_destroy(AddressSpace *as);
2034
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 */
2043 void address_space_remove_listeners(AddressSpace *as);
2044
2045 /**
2046 * address_space_rw: read from or write to an address space.
2047 *
2048 * Return a MemTxResult indicating whether the operation succeeded
2049 * or failed (eg unassigned memory, device rejected the transaction,
2050 * IOMMU fault).
2051 *
2052 * @as: #AddressSpace to be accessed
2053 * @addr: address within that address space
2054 * @attrs: memory transaction attributes
2055 * @buf: buffer with the data transferred
2056 * @len: the number of bytes to read or write
2057 * @is_write: indicates the transfer direction
2058 */
2059 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
2060 MemTxAttrs attrs, void *buf,
2061 hwaddr len, bool is_write);
2062
2063 /**
2064 * address_space_write: write to address space.
2065 *
2066 * Return a MemTxResult indicating whether the operation succeeded
2067 * or failed (eg unassigned memory, device rejected the transaction,
2068 * IOMMU fault).
2069 *
2070 * @as: #AddressSpace to be accessed
2071 * @addr: address within that address space
2072 * @attrs: memory transaction attributes
2073 * @buf: buffer with the data transferred
2074 * @len: the number of bytes to write
2075 */
2076 MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
2077 MemTxAttrs attrs,
2078 const void *buf, hwaddr len);
2079
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 */
2102 MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
2103 MemTxAttrs attrs,
2104 const void *buf, hwaddr len);
2105
2106 /* address_space_ld*: load from an address space
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 */
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"
2136
2137 struct MemoryRegionCache {
2138 void *ptr;
2139 hwaddr xlat;
2140 hwaddr len;
2141 FlatView *fv;
2142 MemoryRegionSection mrs;
2143 bool is_write;
2144 };
2145
2146 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL })
2147
2148
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
2173 #define SUFFIX _cached_slow
2174 #define ARG1 cache
2175 #define ARG1_DECL MemoryRegionCache *cache
2176 #include "exec/memory_ldst.inc.h"
2177
2178 /* Inline fast path for direct RAM access. */
2179 static 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
2190 static 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
2207 #define SUFFIX _cached
2208 #define ARG1 cache
2209 #define ARG1_DECL MemoryRegionCache *cache
2210 #include "exec/memory_ldst_phys.inc.h"
2211
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 */
2231 int64_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 */
2245 void 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 */
2254 void address_space_cache_destroy(MemoryRegionCache *cache);
2255
2256 /* address_space_get_iotlb_entry: translate an address into an IOTLB
2257 * entry. Should be called from an RCU critical section.
2258 */
2259 IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr,
2260 bool is_write, MemTxAttrs attrs);
2261
2262 /* address_space_translate: translate an address range into an address space
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.
2266 *
2267 * @fv: #FlatView to be accessed
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
2273 * @attrs: memory attributes
2274 */
2275 MemoryRegion *flatview_translate(FlatView *fv,
2276 hwaddr addr, hwaddr *xlat,
2277 hwaddr *len, bool is_write,
2278 MemTxAttrs attrs);
2279
2280 static inline MemoryRegion *address_space_translate(AddressSpace *as,
2281 hwaddr addr, hwaddr *xlat,
2282 hwaddr *len, bool is_write,
2283 MemTxAttrs attrs)
2284 {
2285 return flatview_translate(address_space_to_flatview(as),
2286 addr, xlat, len, is_write, attrs);
2287 }
2288
2289 /* address_space_access_valid: check for validity of accessing an address
2290 * space range
2291 *
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.
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
2303 * @attrs: memory attributes
2304 */
2305 bool address_space_access_valid(AddressSpace *as, hwaddr addr, hwaddr len,
2306 bool is_write, MemTxAttrs attrs);
2307
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.
2311 * May return %NULL and set *@plen to zero(0), if resources needed to perform
2312 * the mapping are exhausted.
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
2321 * @attrs: memory attributes
2322 */
2323 void *address_space_map(AddressSpace *as, hwaddr addr,
2324 hwaddr *plen, bool is_write, MemTxAttrs attrs);
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
2332 * @buffer: host pointer as returned by address_space_map()
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 */
2337 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
2338 bool is_write, hwaddr access_len);
2339
2340
2341 /* Internal functions, part of the implementation of address_space_read. */
2342 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
2343 MemTxAttrs attrs, void *buf, hwaddr len);
2344 MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr,
2345 MemTxAttrs attrs, void *buf,
2346 hwaddr len, hwaddr addr1, hwaddr l,
2347 MemoryRegion *mr);
2348 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
2349
2350 /* Internal functions, part of the implementation of address_space_read_cached
2351 * and address_space_write_cached. */
2352 MemTxResult address_space_read_cached_slow(MemoryRegionCache *cache,
2353 hwaddr addr, void *buf, hwaddr len);
2354 MemTxResult address_space_write_cached_slow(MemoryRegionCache *cache,
2355 hwaddr addr, const void *buf,
2356 hwaddr len);
2357
2358 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
2359 {
2360 if (is_write) {
2361 return memory_region_is_ram(mr) && !mr->readonly &&
2362 !mr->rom_device && !memory_region_is_ram_device(mr);
2363 } else {
2364 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) ||
2365 memory_region_is_romd(mr);
2366 }
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,
2374 * IOMMU fault). Called within RCU critical section.
2375 *
2376 * @as: #AddressSpace to be accessed
2377 * @addr: address within that address space
2378 * @attrs: memory transaction attributes
2379 * @buf: buffer with the data transferred
2380 * @len: length of the data transferred
2381 */
2382 static inline __attribute__((__always_inline__))
2383 MemTxResult address_space_read(AddressSpace *as, hwaddr addr,
2384 MemTxAttrs attrs, void *buf,
2385 hwaddr len)
2386 {
2387 MemTxResult result = MEMTX_OK;
2388 hwaddr l, addr1;
2389 void *ptr;
2390 MemoryRegion *mr;
2391 FlatView *fv;
2392
2393 if (__builtin_constant_p(len)) {
2394 if (len) {
2395 RCU_READ_LOCK_GUARD();
2396 fv = address_space_to_flatview(as);
2397 l = len;
2398 mr = flatview_translate(fv, addr, &addr1, &l, false, attrs);
2399 if (len == l && memory_access_is_direct(mr, false)) {
2400 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
2401 memcpy(buf, ptr, len);
2402 } else {
2403 result = flatview_read_continue(fv, addr, attrs, buf, len,
2404 addr1, l, mr);
2405 }
2406 }
2407 } else {
2408 result = address_space_read_full(as, addr, attrs, buf, len);
2409 }
2410 return result;
2411 }
2412
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 */
2421 static inline MemTxResult
2422 address_space_read_cached(MemoryRegionCache *cache, hwaddr addr,
2423 void *buf, hwaddr len)
2424 {
2425 assert(addr < cache->len && len <= cache->len - addr);
2426 if (likely(cache->ptr)) {
2427 memcpy(buf, cache->ptr + addr, len);
2428 return MEMTX_OK;
2429 } else {
2430 return address_space_read_cached_slow(cache, addr, buf, len);
2431 }
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 */
2442 static inline MemTxResult
2443 address_space_write_cached(MemoryRegionCache *cache, hwaddr addr,
2444 const void *buf, hwaddr len)
2445 {
2446 assert(addr < cache->len && len <= cache->len - addr);
2447 if (likely(cache->ptr)) {
2448 memcpy(cache->ptr + addr, buf, len);
2449 return MEMTX_OK;
2450 } else {
2451 return address_space_write_cached_slow(cache, addr, buf, len);
2452 }
2453 }
2454
2455 #ifdef NEED_CPU_H
2456 /* enum device_endian to MemOp. */
2457 static 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
2474
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 */
2496 int 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 */
2504 int ram_block_discard_require(bool state);
2505
2506 /*
2507 * Test if discarding of memory in ram blocks is disabled.
2508 */
2509 bool ram_block_discard_is_disabled(void);
2510
2511 /*
2512 * Test if discarding of memory in ram blocks is required to work reliably.
2513 */
2514 bool ram_block_discard_is_required(void);
2515
2516 #endif
2517
2518 #endif