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