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