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 bool memory_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, 778 ram_addr_t *ram_addr, bool *read_only, 779 bool *mr_has_discard_manager); 780 781 typedef struct CoalescedMemoryRange CoalescedMemoryRange; 782 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; 783 784 /** MemoryRegion: 785 * 786 * A struct representing a memory region. 787 */ 788 struct MemoryRegion { 789 Object parent_obj; 790 791 /* private: */ 792 793 /* The following fields should fit in a cache line */ 794 bool romd_mode; 795 bool ram; 796 bool subpage; 797 bool readonly; /* For RAM regions */ 798 bool nonvolatile; 799 bool rom_device; 800 bool flush_coalesced_mmio; 801 bool unmergeable; 802 uint8_t dirty_log_mask; 803 bool is_iommu; 804 RAMBlock *ram_block; 805 Object *owner; 806 /* owner as TYPE_DEVICE. Used for re-entrancy checks in MR access hotpath */ 807 DeviceState *dev; 808 809 const MemoryRegionOps *ops; 810 void *opaque; 811 MemoryRegion *container; 812 int mapped_via_alias; /* Mapped via an alias, container might be NULL */ 813 Int128 size; 814 hwaddr addr; 815 void (*destructor)(MemoryRegion *mr); 816 uint64_t align; 817 bool terminates; 818 bool ram_device; 819 bool enabled; 820 bool warning_printed; /* For reservations */ 821 uint8_t vga_logging_count; 822 MemoryRegion *alias; 823 hwaddr alias_offset; 824 int32_t priority; 825 QTAILQ_HEAD(, MemoryRegion) subregions; 826 QTAILQ_ENTRY(MemoryRegion) subregions_link; 827 QTAILQ_HEAD(, CoalescedMemoryRange) coalesced; 828 const char *name; 829 unsigned ioeventfd_nb; 830 MemoryRegionIoeventfd *ioeventfds; 831 RamDiscardManager *rdm; /* Only for RAM */ 832 833 /* For devices designed to perform re-entrant IO into their own IO MRs */ 834 bool disable_reentrancy_guard; 835 }; 836 837 struct IOMMUMemoryRegion { 838 MemoryRegion parent_obj; 839 840 QLIST_HEAD(, IOMMUNotifier) iommu_notify; 841 IOMMUNotifierFlag iommu_notify_flags; 842 }; 843 844 #define IOMMU_NOTIFIER_FOREACH(n, mr) \ 845 QLIST_FOREACH((n), &(mr)->iommu_notify, node) 846 847 #define MEMORY_LISTENER_PRIORITY_MIN 0 848 #define MEMORY_LISTENER_PRIORITY_ACCEL 10 849 #define MEMORY_LISTENER_PRIORITY_DEV_BACKEND 10 850 851 /** 852 * struct MemoryListener: callbacks structure for updates to the physical memory map 853 * 854 * Allows a component to adjust to changes in the guest-visible memory map. 855 * Use with memory_listener_register() and memory_listener_unregister(). 856 */ 857 struct MemoryListener { 858 /** 859 * @begin: 860 * 861 * Called at the beginning of an address space update transaction. 862 * Followed by calls to #MemoryListener.region_add(), 863 * #MemoryListener.region_del(), #MemoryListener.region_nop(), 864 * #MemoryListener.log_start() and #MemoryListener.log_stop() in 865 * increasing address order. 866 * 867 * @listener: The #MemoryListener. 868 */ 869 void (*begin)(MemoryListener *listener); 870 871 /** 872 * @commit: 873 * 874 * Called at the end of an address space update transaction, 875 * after the last call to #MemoryListener.region_add(), 876 * #MemoryListener.region_del() or #MemoryListener.region_nop(), 877 * #MemoryListener.log_start() and #MemoryListener.log_stop(). 878 * 879 * @listener: The #MemoryListener. 880 */ 881 void (*commit)(MemoryListener *listener); 882 883 /** 884 * @region_add: 885 * 886 * Called during an address space update transaction, 887 * for a section of the address space that is new in this address space 888 * space since the last transaction. 889 * 890 * @listener: The #MemoryListener. 891 * @section: The new #MemoryRegionSection. 892 */ 893 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section); 894 895 /** 896 * @region_del: 897 * 898 * Called during an address space update transaction, 899 * for a section of the address space that has disappeared in the address 900 * space since the last transaction. 901 * 902 * @listener: The #MemoryListener. 903 * @section: The old #MemoryRegionSection. 904 */ 905 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section); 906 907 /** 908 * @region_nop: 909 * 910 * Called during an address space update transaction, 911 * for a section of the address space that is in the same place in the address 912 * space as in the last transaction. 913 * 914 * @listener: The #MemoryListener. 915 * @section: The #MemoryRegionSection. 916 */ 917 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section); 918 919 /** 920 * @log_start: 921 * 922 * Called during an address space update transaction, after 923 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or 924 * #MemoryListener.region_nop(), if dirty memory logging clients have 925 * become active since the last transaction. 926 * 927 * @listener: The #MemoryListener. 928 * @section: The #MemoryRegionSection. 929 * @old: A bitmap of dirty memory logging clients that were active in 930 * the previous transaction. 931 * @new: A bitmap of dirty memory logging clients that are active in 932 * the current transaction. 933 */ 934 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section, 935 int old, int new); 936 937 /** 938 * @log_stop: 939 * 940 * Called during an address space update transaction, after 941 * one of #MemoryListener.region_add(), #MemoryListener.region_del() or 942 * #MemoryListener.region_nop() and possibly after 943 * #MemoryListener.log_start(), if dirty memory logging clients have 944 * become inactive since the last transaction. 945 * 946 * @listener: The #MemoryListener. 947 * @section: The #MemoryRegionSection. 948 * @old: A bitmap of dirty memory logging clients that were active in 949 * the previous transaction. 950 * @new: A bitmap of dirty memory logging clients that are active in 951 * the current transaction. 952 */ 953 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section, 954 int old, int new); 955 956 /** 957 * @log_sync: 958 * 959 * Called by memory_region_snapshot_and_clear_dirty() and 960 * memory_global_dirty_log_sync(), before accessing QEMU's "official" 961 * copy of the dirty memory bitmap for a #MemoryRegionSection. 962 * 963 * @listener: The #MemoryListener. 964 * @section: The #MemoryRegionSection. 965 */ 966 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section); 967 968 /** 969 * @log_sync_global: 970 * 971 * This is the global version of @log_sync when the listener does 972 * not have a way to synchronize the log with finer granularity. 973 * When the listener registers with @log_sync_global defined, then 974 * its @log_sync must be NULL. Vice versa. 975 * 976 * @listener: The #MemoryListener. 977 * @last_stage: The last stage to synchronize the log during migration. 978 * The caller should guarantee that the synchronization with true for 979 * @last_stage is triggered for once after all VCPUs have been stopped. 980 */ 981 void (*log_sync_global)(MemoryListener *listener, bool last_stage); 982 983 /** 984 * @log_clear: 985 * 986 * Called before reading the dirty memory bitmap for a 987 * #MemoryRegionSection. 988 * 989 * @listener: The #MemoryListener. 990 * @section: The #MemoryRegionSection. 991 */ 992 void (*log_clear)(MemoryListener *listener, MemoryRegionSection *section); 993 994 /** 995 * @log_global_start: 996 * 997 * Called by memory_global_dirty_log_start(), which 998 * enables the %DIRTY_LOG_MIGRATION client on all memory regions in 999 * the address space. #MemoryListener.log_global_start() is also 1000 * called when a #MemoryListener is added, if global dirty logging is 1001 * active at that time. 1002 * 1003 * @listener: The #MemoryListener. 1004 */ 1005 void (*log_global_start)(MemoryListener *listener); 1006 1007 /** 1008 * @log_global_stop: 1009 * 1010 * Called by memory_global_dirty_log_stop(), which 1011 * disables the %DIRTY_LOG_MIGRATION client on all memory regions in 1012 * the address space. 1013 * 1014 * @listener: The #MemoryListener. 1015 */ 1016 void (*log_global_stop)(MemoryListener *listener); 1017 1018 /** 1019 * @log_global_after_sync: 1020 * 1021 * Called after reading the dirty memory bitmap 1022 * for any #MemoryRegionSection. 1023 * 1024 * @listener: The #MemoryListener. 1025 */ 1026 void (*log_global_after_sync)(MemoryListener *listener); 1027 1028 /** 1029 * @eventfd_add: 1030 * 1031 * Called during an address space update transaction, 1032 * for a section of the address space that has had a new ioeventfd 1033 * registration since the last transaction. 1034 * 1035 * @listener: The #MemoryListener. 1036 * @section: The new #MemoryRegionSection. 1037 * @match_data: The @match_data parameter for the new ioeventfd. 1038 * @data: The @data parameter for the new ioeventfd. 1039 * @e: The #EventNotifier parameter for the new ioeventfd. 1040 */ 1041 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section, 1042 bool match_data, uint64_t data, EventNotifier *e); 1043 1044 /** 1045 * @eventfd_del: 1046 * 1047 * Called during an address space update transaction, 1048 * for a section of the address space that has dropped an 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 dropped ioeventfd. 1054 * @data: The @data parameter for the dropped ioeventfd. 1055 * @e: The #EventNotifier parameter for the dropped ioeventfd. 1056 */ 1057 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section, 1058 bool match_data, uint64_t data, EventNotifier *e); 1059 1060 /** 1061 * @coalesced_io_add: 1062 * 1063 * Called during an address space update transaction, 1064 * for a section of the address space that has had a new coalesced 1065 * MMIO range registration since the last transaction. 1066 * 1067 * @listener: The #MemoryListener. 1068 * @section: The new #MemoryRegionSection. 1069 * @addr: The starting address for the coalesced MMIO range. 1070 * @len: The length of the coalesced MMIO range. 1071 */ 1072 void (*coalesced_io_add)(MemoryListener *listener, MemoryRegionSection *section, 1073 hwaddr addr, hwaddr len); 1074 1075 /** 1076 * @coalesced_io_del: 1077 * 1078 * Called during an address space update transaction, 1079 * for a section of the address space that has dropped a coalesced 1080 * MMIO range since the last transaction. 1081 * 1082 * @listener: The #MemoryListener. 1083 * @section: The new #MemoryRegionSection. 1084 * @addr: The starting address for the coalesced MMIO range. 1085 * @len: The length of the coalesced MMIO range. 1086 */ 1087 void (*coalesced_io_del)(MemoryListener *listener, MemoryRegionSection *section, 1088 hwaddr addr, hwaddr len); 1089 /** 1090 * @priority: 1091 * 1092 * Govern the order in which memory listeners are invoked. Lower priorities 1093 * are invoked earlier for "add" or "start" callbacks, and later for "delete" 1094 * or "stop" callbacks. 1095 */ 1096 unsigned priority; 1097 1098 /** 1099 * @name: 1100 * 1101 * Name of the listener. It can be used in contexts where we'd like to 1102 * identify one memory listener with the rest. 1103 */ 1104 const char *name; 1105 1106 /* private: */ 1107 AddressSpace *address_space; 1108 QTAILQ_ENTRY(MemoryListener) link; 1109 QTAILQ_ENTRY(MemoryListener) link_as; 1110 }; 1111 1112 /** 1113 * struct AddressSpace: describes a mapping of addresses to #MemoryRegion objects 1114 */ 1115 struct AddressSpace { 1116 /* private: */ 1117 struct rcu_head rcu; 1118 char *name; 1119 MemoryRegion *root; 1120 1121 /* Accessed via RCU. */ 1122 struct FlatView *current_map; 1123 1124 int ioeventfd_nb; 1125 int ioeventfd_notifiers; 1126 struct MemoryRegionIoeventfd *ioeventfds; 1127 QTAILQ_HEAD(, MemoryListener) listeners; 1128 QTAILQ_ENTRY(AddressSpace) address_spaces_link; 1129 }; 1130 1131 typedef struct AddressSpaceDispatch AddressSpaceDispatch; 1132 typedef struct FlatRange FlatRange; 1133 1134 /* Flattened global view of current active memory hierarchy. Kept in sorted 1135 * order. 1136 */ 1137 struct FlatView { 1138 struct rcu_head rcu; 1139 unsigned ref; 1140 FlatRange *ranges; 1141 unsigned nr; 1142 unsigned nr_allocated; 1143 struct AddressSpaceDispatch *dispatch; 1144 MemoryRegion *root; 1145 }; 1146 1147 static inline FlatView *address_space_to_flatview(AddressSpace *as) 1148 { 1149 return qatomic_rcu_read(&as->current_map); 1150 } 1151 1152 /** 1153 * typedef flatview_cb: callback for flatview_for_each_range() 1154 * 1155 * @start: start address of the range within the FlatView 1156 * @len: length of the range in bytes 1157 * @mr: MemoryRegion covering this range 1158 * @offset_in_region: offset of the first byte of the range within @mr 1159 * @opaque: data pointer passed to flatview_for_each_range() 1160 * 1161 * Returns: true to stop the iteration, false to keep going. 1162 */ 1163 typedef bool (*flatview_cb)(Int128 start, 1164 Int128 len, 1165 const MemoryRegion *mr, 1166 hwaddr offset_in_region, 1167 void *opaque); 1168 1169 /** 1170 * flatview_for_each_range: Iterate through a FlatView 1171 * @fv: the FlatView to iterate through 1172 * @cb: function to call for each range 1173 * @opaque: opaque data pointer to pass to @cb 1174 * 1175 * A FlatView is made up of a list of non-overlapping ranges, each of 1176 * which is a slice of a MemoryRegion. This function iterates through 1177 * each range in @fv, calling @cb. The callback function can terminate 1178 * iteration early by returning 'true'. 1179 */ 1180 void flatview_for_each_range(FlatView *fv, flatview_cb cb, void *opaque); 1181 1182 static inline bool MemoryRegionSection_eq(MemoryRegionSection *a, 1183 MemoryRegionSection *b) 1184 { 1185 return a->mr == b->mr && 1186 a->fv == b->fv && 1187 a->offset_within_region == b->offset_within_region && 1188 a->offset_within_address_space == b->offset_within_address_space && 1189 int128_eq(a->size, b->size) && 1190 a->readonly == b->readonly && 1191 a->nonvolatile == b->nonvolatile; 1192 } 1193 1194 /** 1195 * memory_region_section_new_copy: Copy a memory region section 1196 * 1197 * Allocate memory for a new copy, copy the memory region section, and 1198 * properly take a reference on all relevant members. 1199 * 1200 * @s: the #MemoryRegionSection to copy 1201 */ 1202 MemoryRegionSection *memory_region_section_new_copy(MemoryRegionSection *s); 1203 1204 /** 1205 * memory_region_section_new_copy: Free a copied memory region section 1206 * 1207 * Free a copy of a memory section created via memory_region_section_new_copy(). 1208 * properly dropping references on all relevant members. 1209 * 1210 * @s: the #MemoryRegionSection to copy 1211 */ 1212 void memory_region_section_free_copy(MemoryRegionSection *s); 1213 1214 /** 1215 * memory_region_init: Initialize a memory region 1216 * 1217 * The region typically acts as a container for other memory regions. Use 1218 * memory_region_add_subregion() to add subregions. 1219 * 1220 * @mr: the #MemoryRegion to be initialized 1221 * @owner: the object that tracks the region's reference count 1222 * @name: used for debugging; not visible to the user or ABI 1223 * @size: size of the region; any subregions beyond this size will be clipped 1224 */ 1225 void memory_region_init(MemoryRegion *mr, 1226 Object *owner, 1227 const char *name, 1228 uint64_t size); 1229 1230 /** 1231 * memory_region_ref: Add 1 to a memory region's reference count 1232 * 1233 * Whenever memory regions are accessed outside the BQL, they need to be 1234 * preserved against hot-unplug. MemoryRegions actually do not have their 1235 * own reference count; they piggyback on a QOM object, their "owner". 1236 * This function adds a reference to the owner. 1237 * 1238 * All MemoryRegions must have an owner if they can disappear, even if the 1239 * device they belong to operates exclusively under the BQL. This is because 1240 * the region could be returned at any time by memory_region_find, and this 1241 * is usually under guest control. 1242 * 1243 * @mr: the #MemoryRegion 1244 */ 1245 void memory_region_ref(MemoryRegion *mr); 1246 1247 /** 1248 * memory_region_unref: Remove 1 to a memory region's reference count 1249 * 1250 * Whenever memory regions are accessed outside the BQL, they need to be 1251 * preserved against hot-unplug. MemoryRegions actually do not have their 1252 * own reference count; they piggyback on a QOM object, their "owner". 1253 * This function removes a reference to the owner and possibly destroys it. 1254 * 1255 * @mr: the #MemoryRegion 1256 */ 1257 void memory_region_unref(MemoryRegion *mr); 1258 1259 /** 1260 * memory_region_init_io: Initialize an I/O memory region. 1261 * 1262 * Accesses into the region will cause the callbacks in @ops to be called. 1263 * if @size is nonzero, subregions will be clipped to @size. 1264 * 1265 * @mr: the #MemoryRegion to be initialized. 1266 * @owner: the object that tracks the region's reference count 1267 * @ops: a structure containing read and write callbacks to be used when 1268 * I/O is performed on the region. 1269 * @opaque: passed to the read and write callbacks of the @ops structure. 1270 * @name: used for debugging; not visible to the user or ABI 1271 * @size: size of the region. 1272 */ 1273 void memory_region_init_io(MemoryRegion *mr, 1274 Object *owner, 1275 const MemoryRegionOps *ops, 1276 void *opaque, 1277 const char *name, 1278 uint64_t size); 1279 1280 /** 1281 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses 1282 * into the region will modify memory 1283 * directly. 1284 * 1285 * @mr: the #MemoryRegion to be initialized. 1286 * @owner: the object that tracks the region's reference count 1287 * @name: Region name, becomes part of RAMBlock name used in migration stream 1288 * must be unique within any device 1289 * @size: size of the region. 1290 * @errp: pointer to Error*, to store an error if it happens. 1291 * 1292 * Note that this function does not do anything to cause the data in the 1293 * RAM memory region to be migrated; that is the responsibility of the caller. 1294 * 1295 * Return: true on success, else false setting @errp with error. 1296 */ 1297 bool memory_region_init_ram_nomigrate(MemoryRegion *mr, 1298 Object *owner, 1299 const char *name, 1300 uint64_t size, 1301 Error **errp); 1302 1303 /** 1304 * memory_region_init_ram_flags_nomigrate: Initialize RAM memory region. 1305 * Accesses into the region will 1306 * modify memory directly. 1307 * 1308 * @mr: the #MemoryRegion to be initialized. 1309 * @owner: the object that tracks the region's reference count 1310 * @name: Region name, becomes part of RAMBlock name used in migration stream 1311 * must be unique within any device 1312 * @size: size of the region. 1313 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_NORESERVE, 1314 * RAM_GUEST_MEMFD. 1315 * @errp: pointer to Error*, to store an error if it happens. 1316 * 1317 * Note that this function does not do anything to cause the data in the 1318 * RAM memory region to be migrated; that is the responsibility of the caller. 1319 * 1320 * Return: true on success, else false setting @errp with error. 1321 */ 1322 bool memory_region_init_ram_flags_nomigrate(MemoryRegion *mr, 1323 Object *owner, 1324 const char *name, 1325 uint64_t size, 1326 uint32_t ram_flags, 1327 Error **errp); 1328 1329 /** 1330 * memory_region_init_resizeable_ram: Initialize memory region with resizable 1331 * RAM. Accesses into the region will 1332 * modify memory directly. Only an initial 1333 * portion of this RAM is actually used. 1334 * Changing the size while migrating 1335 * can result in the migration being 1336 * canceled. 1337 * 1338 * @mr: the #MemoryRegion to be initialized. 1339 * @owner: the object that tracks the region's reference count 1340 * @name: Region name, becomes part of RAMBlock name used in migration stream 1341 * must be unique within any device 1342 * @size: used size of the region. 1343 * @max_size: max size of the region. 1344 * @resized: callback to notify owner about used size change. 1345 * @errp: pointer to Error*, to store an error if it happens. 1346 * 1347 * Note that this function does not do anything to cause the data in the 1348 * RAM memory region to be migrated; that is the responsibility of the caller. 1349 * 1350 * Return: true on success, else false setting @errp with error. 1351 */ 1352 bool memory_region_init_resizeable_ram(MemoryRegion *mr, 1353 Object *owner, 1354 const char *name, 1355 uint64_t size, 1356 uint64_t max_size, 1357 void (*resized)(const char*, 1358 uint64_t length, 1359 void *host), 1360 Error **errp); 1361 #ifdef CONFIG_POSIX 1362 1363 /** 1364 * memory_region_init_ram_from_file: Initialize RAM memory region with a 1365 * mmap-ed backend. 1366 * 1367 * @mr: the #MemoryRegion to be initialized. 1368 * @owner: the object that tracks the region's reference count 1369 * @name: Region name, becomes part of RAMBlock name used in migration stream 1370 * must be unique within any device 1371 * @size: size of the region. 1372 * @align: alignment of the region base address; if 0, the default alignment 1373 * (getpagesize()) will be used. 1374 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM, 1375 * RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY, 1376 * RAM_READONLY_FD, RAM_GUEST_MEMFD 1377 * @path: the path in which to allocate the RAM. 1378 * @offset: offset within the file referenced by path 1379 * @errp: pointer to Error*, to store an error if it happens. 1380 * 1381 * Note that this function does not do anything to cause the data in the 1382 * RAM memory region to be migrated; that is the responsibility of the caller. 1383 * 1384 * Return: true on success, else false setting @errp with error. 1385 */ 1386 bool memory_region_init_ram_from_file(MemoryRegion *mr, 1387 Object *owner, 1388 const char *name, 1389 uint64_t size, 1390 uint64_t align, 1391 uint32_t ram_flags, 1392 const char *path, 1393 ram_addr_t offset, 1394 Error **errp); 1395 1396 /** 1397 * memory_region_init_ram_from_fd: Initialize RAM memory region with a 1398 * mmap-ed backend. 1399 * 1400 * @mr: the #MemoryRegion to be initialized. 1401 * @owner: the object that tracks the region's reference count 1402 * @name: the name of the region. 1403 * @size: size of the region. 1404 * @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM, 1405 * RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY, 1406 * RAM_READONLY_FD, RAM_GUEST_MEMFD 1407 * @fd: the fd to mmap. 1408 * @offset: offset within the file referenced by fd 1409 * @errp: pointer to Error*, to store an error if it happens. 1410 * 1411 * Note that this function does not do anything to cause the data in the 1412 * RAM memory region to be migrated; that is the responsibility of the caller. 1413 * 1414 * Return: true on success, else false setting @errp with error. 1415 */ 1416 bool memory_region_init_ram_from_fd(MemoryRegion *mr, 1417 Object *owner, 1418 const char *name, 1419 uint64_t size, 1420 uint32_t ram_flags, 1421 int fd, 1422 ram_addr_t offset, 1423 Error **errp); 1424 #endif 1425 1426 /** 1427 * memory_region_init_ram_ptr: Initialize RAM memory region from a 1428 * user-provided pointer. Accesses into the 1429 * region will modify memory directly. 1430 * 1431 * @mr: the #MemoryRegion to be initialized. 1432 * @owner: the object that tracks the region's reference count 1433 * @name: Region name, becomes part of RAMBlock name used in migration stream 1434 * must be unique within any device 1435 * @size: size of the region. 1436 * @ptr: memory to be mapped; must contain at least @size bytes. 1437 * 1438 * Note that this function does not do anything to cause the data in the 1439 * RAM memory region to be migrated; that is the responsibility of the caller. 1440 */ 1441 void memory_region_init_ram_ptr(MemoryRegion *mr, 1442 Object *owner, 1443 const char *name, 1444 uint64_t size, 1445 void *ptr); 1446 1447 /** 1448 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from 1449 * a user-provided pointer. 1450 * 1451 * A RAM device represents a mapping to a physical device, such as to a PCI 1452 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped 1453 * into the VM address space and access to the region will modify memory 1454 * directly. However, the memory region should not be included in a memory 1455 * dump (device may not be enabled/mapped at the time of the dump), and 1456 * operations incompatible with manipulating MMIO should be avoided. Replaces 1457 * skip_dump flag. 1458 * 1459 * @mr: the #MemoryRegion to be initialized. 1460 * @owner: the object that tracks the region's reference count 1461 * @name: the name of the region. 1462 * @size: size of the region. 1463 * @ptr: memory to be mapped; must contain at least @size bytes. 1464 * 1465 * Note that this function does not do anything to cause the data in the 1466 * RAM memory region to be migrated; that is the responsibility of the caller. 1467 * (For RAM device memory regions, migrating the contents rarely makes sense.) 1468 */ 1469 void memory_region_init_ram_device_ptr(MemoryRegion *mr, 1470 Object *owner, 1471 const char *name, 1472 uint64_t size, 1473 void *ptr); 1474 1475 /** 1476 * memory_region_init_alias: Initialize a memory region that aliases all or a 1477 * part of another memory region. 1478 * 1479 * @mr: the #MemoryRegion to be initialized. 1480 * @owner: the object that tracks the region's reference count 1481 * @name: used for debugging; not visible to the user or ABI 1482 * @orig: the region to be referenced; @mr will be equivalent to 1483 * @orig between @offset and @offset + @size - 1. 1484 * @offset: start of the section in @orig to be referenced. 1485 * @size: size of the region. 1486 */ 1487 void memory_region_init_alias(MemoryRegion *mr, 1488 Object *owner, 1489 const char *name, 1490 MemoryRegion *orig, 1491 hwaddr offset, 1492 uint64_t size); 1493 1494 /** 1495 * memory_region_init_rom_nomigrate: Initialize a ROM memory region. 1496 * 1497 * This has the same effect as calling memory_region_init_ram_nomigrate() 1498 * and then marking the resulting region read-only with 1499 * memory_region_set_readonly(). 1500 * 1501 * Note that this function does not do anything to cause the data in the 1502 * RAM side of the memory region to be migrated; that is the responsibility 1503 * of the caller. 1504 * 1505 * @mr: the #MemoryRegion to be initialized. 1506 * @owner: the object that tracks the region's reference count 1507 * @name: Region name, becomes part of RAMBlock name used in migration stream 1508 * must be unique within any device 1509 * @size: size of the region. 1510 * @errp: pointer to Error*, to store an error if it happens. 1511 * 1512 * Return: true on success, else false setting @errp with error. 1513 */ 1514 bool memory_region_init_rom_nomigrate(MemoryRegion *mr, 1515 Object *owner, 1516 const char *name, 1517 uint64_t size, 1518 Error **errp); 1519 1520 /** 1521 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region. 1522 * Writes are handled via callbacks. 1523 * 1524 * Note that this function does not do anything to cause the data in the 1525 * RAM side of the memory region to be migrated; that is the responsibility 1526 * of the caller. 1527 * 1528 * @mr: the #MemoryRegion to be initialized. 1529 * @owner: the object that tracks the region's reference count 1530 * @ops: callbacks for write access handling (must not be NULL). 1531 * @opaque: passed to the read and write callbacks of the @ops structure. 1532 * @name: Region name, becomes part of RAMBlock name used in migration stream 1533 * must be unique within any device 1534 * @size: size of the region. 1535 * @errp: pointer to Error*, to store an error if it happens. 1536 * 1537 * Return: true on success, else false setting @errp with error. 1538 */ 1539 bool memory_region_init_rom_device_nomigrate(MemoryRegion *mr, 1540 Object *owner, 1541 const MemoryRegionOps *ops, 1542 void *opaque, 1543 const char *name, 1544 uint64_t size, 1545 Error **errp); 1546 1547 /** 1548 * memory_region_init_iommu: Initialize a memory region of a custom type 1549 * that translates addresses 1550 * 1551 * An IOMMU region translates addresses and forwards accesses to a target 1552 * memory region. 1553 * 1554 * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION. 1555 * @_iommu_mr should be a pointer to enough memory for an instance of 1556 * that subclass, @instance_size is the size of that subclass, and 1557 * @mrtypename is its name. This function will initialize @_iommu_mr as an 1558 * instance of the subclass, and its methods will then be called to handle 1559 * accesses to the memory region. See the documentation of 1560 * #IOMMUMemoryRegionClass for further details. 1561 * 1562 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized 1563 * @instance_size: the IOMMUMemoryRegion subclass instance size 1564 * @mrtypename: the type name of the #IOMMUMemoryRegion 1565 * @owner: the object that tracks the region's reference count 1566 * @name: used for debugging; not visible to the user or ABI 1567 * @size: size of the region. 1568 */ 1569 void memory_region_init_iommu(void *_iommu_mr, 1570 size_t instance_size, 1571 const char *mrtypename, 1572 Object *owner, 1573 const char *name, 1574 uint64_t size); 1575 1576 /** 1577 * memory_region_init_ram - Initialize RAM memory region. Accesses into the 1578 * region will modify memory directly. 1579 * 1580 * @mr: the #MemoryRegion to be initialized 1581 * @owner: the object that tracks the region's reference count (must be 1582 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL) 1583 * @name: name of the memory region 1584 * @size: size of the region in bytes 1585 * @errp: pointer to Error*, to store an error if it happens. 1586 * 1587 * This function allocates RAM for a board model or device, and 1588 * arranges for it to be migrated (by calling vmstate_register_ram() 1589 * if @owner is a DeviceState, or vmstate_register_ram_global() if 1590 * @owner is NULL). 1591 * 1592 * TODO: Currently we restrict @owner to being either NULL (for 1593 * global RAM regions with no owner) or devices, so that we can 1594 * give the RAM block a unique name for migration purposes. 1595 * We should lift this restriction and allow arbitrary Objects. 1596 * If you pass a non-NULL non-device @owner then we will assert. 1597 * 1598 * Return: true on success, else false setting @errp with error. 1599 */ 1600 bool memory_region_init_ram(MemoryRegion *mr, 1601 Object *owner, 1602 const char *name, 1603 uint64_t size, 1604 Error **errp); 1605 1606 /** 1607 * memory_region_init_rom: Initialize a ROM memory region. 1608 * 1609 * This has the same effect as calling memory_region_init_ram() 1610 * and then marking the resulting region read-only with 1611 * memory_region_set_readonly(). This includes arranging for the 1612 * contents to be migrated. 1613 * 1614 * TODO: Currently we restrict @owner to being either NULL (for 1615 * global RAM regions with no owner) or devices, so that we can 1616 * give the RAM block a unique name for migration purposes. 1617 * We should lift this restriction and allow arbitrary Objects. 1618 * If you pass a non-NULL non-device @owner then we will assert. 1619 * 1620 * @mr: the #MemoryRegion to be initialized. 1621 * @owner: the object that tracks the region's reference count 1622 * @name: Region name, becomes part of RAMBlock name used in migration stream 1623 * must be unique within any device 1624 * @size: size of the region. 1625 * @errp: pointer to Error*, to store an error if it happens. 1626 * 1627 * Return: true on success, else false setting @errp with error. 1628 */ 1629 bool memory_region_init_rom(MemoryRegion *mr, 1630 Object *owner, 1631 const char *name, 1632 uint64_t size, 1633 Error **errp); 1634 1635 /** 1636 * memory_region_init_rom_device: Initialize a ROM memory region. 1637 * Writes are handled via callbacks. 1638 * 1639 * This function initializes a memory region backed by RAM for reads 1640 * and callbacks for writes, and arranges for the RAM backing to 1641 * be migrated (by calling vmstate_register_ram() 1642 * if @owner is a DeviceState, or vmstate_register_ram_global() if 1643 * @owner is NULL). 1644 * 1645 * TODO: Currently we restrict @owner to being either NULL (for 1646 * global RAM regions with no owner) or devices, so that we can 1647 * give the RAM block a unique name for migration purposes. 1648 * We should lift this restriction and allow arbitrary Objects. 1649 * If you pass a non-NULL non-device @owner then we will assert. 1650 * 1651 * @mr: the #MemoryRegion to be initialized. 1652 * @owner: the object that tracks the region's reference count 1653 * @ops: callbacks for write access handling (must not be NULL). 1654 * @opaque: passed to the read and write callbacks of the @ops structure. 1655 * @name: Region name, becomes part of RAMBlock name used in migration stream 1656 * must be unique within any device 1657 * @size: size of the region. 1658 * @errp: pointer to Error*, to store an error if it happens. 1659 * 1660 * Return: true on success, else false setting @errp with error. 1661 */ 1662 bool memory_region_init_rom_device(MemoryRegion *mr, 1663 Object *owner, 1664 const MemoryRegionOps *ops, 1665 void *opaque, 1666 const char *name, 1667 uint64_t size, 1668 Error **errp); 1669 1670 1671 /** 1672 * memory_region_owner: get a memory region's owner. 1673 * 1674 * @mr: the memory region being queried. 1675 */ 1676 Object *memory_region_owner(MemoryRegion *mr); 1677 1678 /** 1679 * memory_region_size: get a memory region's size. 1680 * 1681 * @mr: the memory region being queried. 1682 */ 1683 uint64_t memory_region_size(MemoryRegion *mr); 1684 1685 /** 1686 * memory_region_is_ram: check whether a memory region is random access 1687 * 1688 * Returns %true if a memory region is random access. 1689 * 1690 * @mr: the memory region being queried 1691 */ 1692 static inline bool memory_region_is_ram(MemoryRegion *mr) 1693 { 1694 return mr->ram; 1695 } 1696 1697 /** 1698 * memory_region_is_ram_device: check whether a memory region is a ram device 1699 * 1700 * Returns %true if a memory region is a device backed ram region 1701 * 1702 * @mr: the memory region being queried 1703 */ 1704 bool memory_region_is_ram_device(MemoryRegion *mr); 1705 1706 /** 1707 * memory_region_is_romd: check whether a memory region is in ROMD mode 1708 * 1709 * Returns %true if a memory region is a ROM device and currently set to allow 1710 * direct reads. 1711 * 1712 * @mr: the memory region being queried 1713 */ 1714 static inline bool memory_region_is_romd(MemoryRegion *mr) 1715 { 1716 return mr->rom_device && mr->romd_mode; 1717 } 1718 1719 /** 1720 * memory_region_is_protected: check whether a memory region is protected 1721 * 1722 * Returns %true if a memory region is protected RAM and cannot be accessed 1723 * via standard mechanisms, e.g. DMA. 1724 * 1725 * @mr: the memory region being queried 1726 */ 1727 bool memory_region_is_protected(MemoryRegion *mr); 1728 1729 /** 1730 * memory_region_has_guest_memfd: check whether a memory region has guest_memfd 1731 * associated 1732 * 1733 * Returns %true if a memory region's ram_block has valid guest_memfd assigned. 1734 * 1735 * @mr: the memory region being queried 1736 */ 1737 bool memory_region_has_guest_memfd(MemoryRegion *mr); 1738 1739 /** 1740 * memory_region_get_iommu: check whether a memory region is an iommu 1741 * 1742 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu, 1743 * otherwise NULL. 1744 * 1745 * @mr: the memory region being queried 1746 */ 1747 static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr) 1748 { 1749 if (mr->alias) { 1750 return memory_region_get_iommu(mr->alias); 1751 } 1752 if (mr->is_iommu) { 1753 return (IOMMUMemoryRegion *) mr; 1754 } 1755 return NULL; 1756 } 1757 1758 /** 1759 * memory_region_get_iommu_class_nocheck: returns iommu memory region class 1760 * if an iommu or NULL if not 1761 * 1762 * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu, 1763 * otherwise NULL. This is fast path avoiding QOM checking, use with caution. 1764 * 1765 * @iommu_mr: the memory region being queried 1766 */ 1767 static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck( 1768 IOMMUMemoryRegion *iommu_mr) 1769 { 1770 return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class); 1771 } 1772 1773 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL) 1774 1775 /** 1776 * memory_region_iommu_get_min_page_size: get minimum supported page size 1777 * for an iommu 1778 * 1779 * Returns minimum supported page size for an iommu. 1780 * 1781 * @iommu_mr: the memory region being queried 1782 */ 1783 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr); 1784 1785 /** 1786 * memory_region_notify_iommu: notify a change in an IOMMU translation entry. 1787 * 1788 * Note: for any IOMMU implementation, an in-place mapping change 1789 * should be notified with an UNMAP followed by a MAP. 1790 * 1791 * @iommu_mr: the memory region that was changed 1792 * @iommu_idx: the IOMMU index for the translation table which has changed 1793 * @event: TLB event with the new entry in the IOMMU translation table. 1794 * The entry replaces all old entries for the same virtual I/O address 1795 * range. 1796 */ 1797 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr, 1798 int iommu_idx, 1799 IOMMUTLBEvent event); 1800 1801 /** 1802 * memory_region_notify_iommu_one: notify a change in an IOMMU translation 1803 * entry to a single notifier 1804 * 1805 * This works just like memory_region_notify_iommu(), but it only 1806 * notifies a specific notifier, not all of them. 1807 * 1808 * @notifier: the notifier to be notified 1809 * @event: TLB event with the new entry in the IOMMU translation table. 1810 * The entry replaces all old entries for the same virtual I/O address 1811 * range. 1812 */ 1813 void memory_region_notify_iommu_one(IOMMUNotifier *notifier, 1814 IOMMUTLBEvent *event); 1815 1816 /** 1817 * memory_region_unmap_iommu_notifier_range: notify a unmap for an IOMMU 1818 * translation that covers the 1819 * range of a notifier 1820 * 1821 * @notifier: the notifier to be notified 1822 */ 1823 void memory_region_unmap_iommu_notifier_range(IOMMUNotifier *notifier); 1824 1825 1826 /** 1827 * memory_region_register_iommu_notifier: register a notifier for changes to 1828 * IOMMU translation entries. 1829 * 1830 * Returns 0 on success, or a negative errno otherwise. In particular, 1831 * -EINVAL indicates that at least one of the attributes of the notifier 1832 * is not supported (flag/range) by the IOMMU memory region. In case of error 1833 * the error object must be created. 1834 * 1835 * @mr: the memory region to observe 1836 * @n: the IOMMUNotifier to be added; the notify callback receives a 1837 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer 1838 * ceases to be valid on exit from the notifier. 1839 * @errp: pointer to Error*, to store an error if it happens. 1840 */ 1841 int memory_region_register_iommu_notifier(MemoryRegion *mr, 1842 IOMMUNotifier *n, Error **errp); 1843 1844 /** 1845 * memory_region_iommu_replay: replay existing IOMMU translations to 1846 * a notifier with the minimum page granularity returned by 1847 * mr->iommu_ops->get_page_size(). 1848 * 1849 * Note: this is not related to record-and-replay functionality. 1850 * 1851 * @iommu_mr: the memory region to observe 1852 * @n: the notifier to which to replay iommu mappings 1853 */ 1854 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n); 1855 1856 /** 1857 * memory_region_unregister_iommu_notifier: unregister a notifier for 1858 * changes to IOMMU translation entries. 1859 * 1860 * @mr: the memory region which was observed and for which notity_stopped() 1861 * needs to be called 1862 * @n: the notifier to be removed. 1863 */ 1864 void memory_region_unregister_iommu_notifier(MemoryRegion *mr, 1865 IOMMUNotifier *n); 1866 1867 /** 1868 * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is 1869 * defined on the IOMMU. 1870 * 1871 * Returns 0 on success, or a negative errno otherwise. In particular, 1872 * -EINVAL indicates that the IOMMU does not support the requested 1873 * attribute. 1874 * 1875 * @iommu_mr: the memory region 1876 * @attr: the requested attribute 1877 * @data: a pointer to the requested attribute data 1878 */ 1879 int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr, 1880 enum IOMMUMemoryRegionAttr attr, 1881 void *data); 1882 1883 /** 1884 * memory_region_iommu_attrs_to_index: return the IOMMU index to 1885 * use for translations with the given memory transaction attributes. 1886 * 1887 * @iommu_mr: the memory region 1888 * @attrs: the memory transaction attributes 1889 */ 1890 int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr, 1891 MemTxAttrs attrs); 1892 1893 /** 1894 * memory_region_iommu_num_indexes: return the total number of IOMMU 1895 * indexes that this IOMMU supports. 1896 * 1897 * @iommu_mr: the memory region 1898 */ 1899 int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr); 1900 1901 /** 1902 * memory_region_iommu_set_page_size_mask: set the supported page 1903 * sizes for a given IOMMU memory region 1904 * 1905 * @iommu_mr: IOMMU memory region 1906 * @page_size_mask: supported page size mask 1907 * @errp: pointer to Error*, to store an error if it happens. 1908 */ 1909 int memory_region_iommu_set_page_size_mask(IOMMUMemoryRegion *iommu_mr, 1910 uint64_t page_size_mask, 1911 Error **errp); 1912 1913 /** 1914 * memory_region_iommu_set_iova_ranges - Set the usable IOVA ranges 1915 * for a given IOMMU MR region 1916 * 1917 * @iommu: IOMMU memory region 1918 * @iova_ranges: list of ordered IOVA ranges (at least one range) 1919 * @errp: pointer to Error*, to store an error if it happens. 1920 */ 1921 int memory_region_iommu_set_iova_ranges(IOMMUMemoryRegion *iommu, 1922 GList *iova_ranges, 1923 Error **errp); 1924 1925 /** 1926 * memory_region_name: get a memory region's name 1927 * 1928 * Returns the string that was used to initialize the memory region. 1929 * 1930 * @mr: the memory region being queried 1931 */ 1932 const char *memory_region_name(const MemoryRegion *mr); 1933 1934 /** 1935 * memory_region_is_logging: return whether a memory region is logging writes 1936 * 1937 * Returns %true if the memory region is logging writes for the given client 1938 * 1939 * @mr: the memory region being queried 1940 * @client: the client being queried 1941 */ 1942 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client); 1943 1944 /** 1945 * memory_region_get_dirty_log_mask: return the clients for which a 1946 * memory region is logging writes. 1947 * 1948 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants 1949 * are the bit indices. 1950 * 1951 * @mr: the memory region being queried 1952 */ 1953 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr); 1954 1955 /** 1956 * memory_region_is_rom: check whether a memory region is ROM 1957 * 1958 * Returns %true if a memory region is read-only memory. 1959 * 1960 * @mr: the memory region being queried 1961 */ 1962 static inline bool memory_region_is_rom(MemoryRegion *mr) 1963 { 1964 return mr->ram && mr->readonly; 1965 } 1966 1967 /** 1968 * memory_region_is_nonvolatile: check whether a memory region is non-volatile 1969 * 1970 * Returns %true is a memory region is non-volatile memory. 1971 * 1972 * @mr: the memory region being queried 1973 */ 1974 static inline bool memory_region_is_nonvolatile(MemoryRegion *mr) 1975 { 1976 return mr->nonvolatile; 1977 } 1978 1979 /** 1980 * memory_region_get_fd: Get a file descriptor backing a RAM memory region. 1981 * 1982 * Returns a file descriptor backing a file-based RAM memory region, 1983 * or -1 if the region is not a file-based RAM memory region. 1984 * 1985 * @mr: the RAM or alias memory region being queried. 1986 */ 1987 int memory_region_get_fd(MemoryRegion *mr); 1988 1989 /** 1990 * memory_region_from_host: Convert a pointer into a RAM memory region 1991 * and an offset within it. 1992 * 1993 * Given a host pointer inside a RAM memory region (created with 1994 * memory_region_init_ram() or memory_region_init_ram_ptr()), return 1995 * the MemoryRegion and the offset within it. 1996 * 1997 * Use with care; by the time this function returns, the returned pointer is 1998 * not protected by RCU anymore. If the caller is not within an RCU critical 1999 * section and does not hold the BQL, it must have other means of 2000 * protecting the pointer, such as a reference to the region that includes 2001 * the incoming ram_addr_t. 2002 * 2003 * @ptr: the host pointer to be converted 2004 * @offset: the offset within memory region 2005 */ 2006 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset); 2007 2008 /** 2009 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region. 2010 * 2011 * Returns a host pointer to a RAM memory region (created with 2012 * memory_region_init_ram() or memory_region_init_ram_ptr()). 2013 * 2014 * Use with care; by the time this function returns, the returned pointer is 2015 * not protected by RCU anymore. If the caller is not within an RCU critical 2016 * section and does not hold the BQL, it must have other means of 2017 * protecting the pointer, such as a reference to the region that includes 2018 * the incoming ram_addr_t. 2019 * 2020 * @mr: the memory region being queried. 2021 */ 2022 void *memory_region_get_ram_ptr(MemoryRegion *mr); 2023 2024 /* memory_region_ram_resize: Resize a RAM region. 2025 * 2026 * Resizing RAM while migrating can result in the migration being canceled. 2027 * Care has to be taken if the guest might have already detected the memory. 2028 * 2029 * @mr: a memory region created with @memory_region_init_resizeable_ram. 2030 * @newsize: the new size the region 2031 * @errp: pointer to Error*, to store an error if it happens. 2032 */ 2033 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, 2034 Error **errp); 2035 2036 /** 2037 * memory_region_msync: Synchronize selected address range of 2038 * a memory mapped region 2039 * 2040 * @mr: the memory region to be msync 2041 * @addr: the initial address of the range to be sync 2042 * @size: the size of the range to be sync 2043 */ 2044 void memory_region_msync(MemoryRegion *mr, hwaddr addr, hwaddr size); 2045 2046 /** 2047 * memory_region_writeback: Trigger cache writeback for 2048 * selected address range 2049 * 2050 * @mr: the memory region to be updated 2051 * @addr: the initial address of the range to be written back 2052 * @size: the size of the range to be written back 2053 */ 2054 void memory_region_writeback(MemoryRegion *mr, hwaddr addr, hwaddr size); 2055 2056 /** 2057 * memory_region_set_log: Turn dirty logging on or off for a region. 2058 * 2059 * Turns dirty logging on or off for a specified client (display, migration). 2060 * Only meaningful for RAM regions. 2061 * 2062 * @mr: the memory region being updated. 2063 * @log: whether dirty logging is to be enabled or disabled. 2064 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only. 2065 */ 2066 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client); 2067 2068 /** 2069 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region. 2070 * 2071 * Marks a range of bytes as dirty, after it has been dirtied outside 2072 * guest code. 2073 * 2074 * @mr: the memory region being dirtied. 2075 * @addr: the address (relative to the start of the region) being dirtied. 2076 * @size: size of the range being dirtied. 2077 */ 2078 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, 2079 hwaddr size); 2080 2081 /** 2082 * memory_region_clear_dirty_bitmap - clear dirty bitmap for memory range 2083 * 2084 * This function is called when the caller wants to clear the remote 2085 * dirty bitmap of a memory range within the memory region. This can 2086 * be used by e.g. KVM to manually clear dirty log when 2087 * KVM_CAP_MANUAL_DIRTY_LOG_PROTECT is declared support by the host 2088 * kernel. 2089 * 2090 * @mr: the memory region to clear the dirty log upon 2091 * @start: start address offset within the memory region 2092 * @len: length of the memory region to clear dirty bitmap 2093 */ 2094 void memory_region_clear_dirty_bitmap(MemoryRegion *mr, hwaddr start, 2095 hwaddr len); 2096 2097 /** 2098 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty 2099 * bitmap and clear it. 2100 * 2101 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and 2102 * returns the snapshot. The snapshot can then be used to query dirty 2103 * status, using memory_region_snapshot_get_dirty. Snapshotting allows 2104 * querying the same page multiple times, which is especially useful for 2105 * display updates where the scanlines often are not page aligned. 2106 * 2107 * The dirty bitmap region which gets copied into the snapshot (and 2108 * cleared afterwards) can be larger than requested. The boundaries 2109 * are rounded up/down so complete bitmap longs (covering 64 pages on 2110 * 64bit hosts) can be copied over into the bitmap snapshot. Which 2111 * isn't a problem for display updates as the extra pages are outside 2112 * the visible area, and in case the visible area changes a full 2113 * display redraw is due anyway. Should other use cases for this 2114 * function emerge we might have to revisit this implementation 2115 * detail. 2116 * 2117 * Use g_free to release DirtyBitmapSnapshot. 2118 * 2119 * @mr: the memory region being queried. 2120 * @addr: the address (relative to the start of the region) being queried. 2121 * @size: the size of the range being queried. 2122 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA. 2123 */ 2124 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr, 2125 hwaddr addr, 2126 hwaddr size, 2127 unsigned client); 2128 2129 /** 2130 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty 2131 * in the specified dirty bitmap snapshot. 2132 * 2133 * @mr: the memory region being queried. 2134 * @snap: the dirty bitmap snapshot 2135 * @addr: the address (relative to the start of the region) being queried. 2136 * @size: the size of the range being queried. 2137 */ 2138 bool memory_region_snapshot_get_dirty(MemoryRegion *mr, 2139 DirtyBitmapSnapshot *snap, 2140 hwaddr addr, hwaddr size); 2141 2142 /** 2143 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified 2144 * client. 2145 * 2146 * Marks a range of pages as no longer dirty. 2147 * 2148 * @mr: the region being updated. 2149 * @addr: the start of the subrange being cleaned. 2150 * @size: the size of the subrange being cleaned. 2151 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 2152 * %DIRTY_MEMORY_VGA. 2153 */ 2154 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr, 2155 hwaddr size, unsigned client); 2156 2157 /** 2158 * memory_region_flush_rom_device: Mark a range of pages dirty and invalidate 2159 * TBs (for self-modifying code). 2160 * 2161 * The MemoryRegionOps->write() callback of a ROM device must use this function 2162 * to mark byte ranges that have been modified internally, such as by directly 2163 * accessing the memory returned by memory_region_get_ram_ptr(). 2164 * 2165 * This function marks the range dirty and invalidates TBs so that TCG can 2166 * detect self-modifying code. 2167 * 2168 * @mr: the region being flushed. 2169 * @addr: the start, relative to the start of the region, of the range being 2170 * flushed. 2171 * @size: the size, in bytes, of the range being flushed. 2172 */ 2173 void memory_region_flush_rom_device(MemoryRegion *mr, hwaddr addr, hwaddr size); 2174 2175 /** 2176 * memory_region_set_readonly: Turn a memory region read-only (or read-write) 2177 * 2178 * Allows a memory region to be marked as read-only (turning it into a ROM). 2179 * only useful on RAM regions. 2180 * 2181 * @mr: the region being updated. 2182 * @readonly: whether rhe region is to be ROM or RAM. 2183 */ 2184 void memory_region_set_readonly(MemoryRegion *mr, bool readonly); 2185 2186 /** 2187 * memory_region_set_nonvolatile: Turn a memory region non-volatile 2188 * 2189 * Allows a memory region to be marked as non-volatile. 2190 * only useful on RAM regions. 2191 * 2192 * @mr: the region being updated. 2193 * @nonvolatile: whether rhe region is to be non-volatile. 2194 */ 2195 void memory_region_set_nonvolatile(MemoryRegion *mr, bool nonvolatile); 2196 2197 /** 2198 * memory_region_rom_device_set_romd: enable/disable ROMD mode 2199 * 2200 * Allows a ROM device (initialized with memory_region_init_rom_device() to 2201 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the 2202 * device is mapped to guest memory and satisfies read access directly. 2203 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function. 2204 * Writes are always handled by the #MemoryRegion.write function. 2205 * 2206 * @mr: the memory region to be updated 2207 * @romd_mode: %true to put the region into ROMD mode 2208 */ 2209 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode); 2210 2211 /** 2212 * memory_region_set_coalescing: Enable memory coalescing for the region. 2213 * 2214 * Enabled writes to a region to be queued for later processing. MMIO ->write 2215 * callbacks may be delayed until a non-coalesced MMIO is issued. 2216 * Only useful for IO regions. Roughly similar to write-combining hardware. 2217 * 2218 * @mr: the memory region to be write coalesced 2219 */ 2220 void memory_region_set_coalescing(MemoryRegion *mr); 2221 2222 /** 2223 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of 2224 * a region. 2225 * 2226 * Like memory_region_set_coalescing(), but works on a sub-range of a region. 2227 * Multiple calls can be issued coalesced disjoint ranges. 2228 * 2229 * @mr: the memory region to be updated. 2230 * @offset: the start of the range within the region to be coalesced. 2231 * @size: the size of the subrange to be coalesced. 2232 */ 2233 void memory_region_add_coalescing(MemoryRegion *mr, 2234 hwaddr offset, 2235 uint64_t size); 2236 2237 /** 2238 * memory_region_clear_coalescing: Disable MMIO coalescing for the region. 2239 * 2240 * Disables any coalescing caused by memory_region_set_coalescing() or 2241 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory 2242 * hardware. 2243 * 2244 * @mr: the memory region to be updated. 2245 */ 2246 void memory_region_clear_coalescing(MemoryRegion *mr); 2247 2248 /** 2249 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before 2250 * accesses. 2251 * 2252 * Ensure that pending coalesced MMIO request are flushed before the memory 2253 * region is accessed. This property is automatically enabled for all regions 2254 * passed to memory_region_set_coalescing() and memory_region_add_coalescing(). 2255 * 2256 * @mr: the memory region to be updated. 2257 */ 2258 void memory_region_set_flush_coalesced(MemoryRegion *mr); 2259 2260 /** 2261 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before 2262 * accesses. 2263 * 2264 * Clear the automatic coalesced MMIO flushing enabled via 2265 * memory_region_set_flush_coalesced. Note that this service has no effect on 2266 * memory regions that have MMIO coalescing enabled for themselves. For them, 2267 * automatic flushing will stop once coalescing is disabled. 2268 * 2269 * @mr: the memory region to be updated. 2270 */ 2271 void memory_region_clear_flush_coalesced(MemoryRegion *mr); 2272 2273 /** 2274 * memory_region_add_eventfd: Request an eventfd to be triggered when a word 2275 * is written to a location. 2276 * 2277 * Marks a word in an IO region (initialized with memory_region_init_io()) 2278 * as a trigger for an eventfd event. The I/O callback will not be called. 2279 * The caller must be prepared to handle failure (that is, take the required 2280 * action if the callback _is_ called). 2281 * 2282 * @mr: the memory region being updated. 2283 * @addr: the address within @mr that is to be monitored 2284 * @size: the size of the access to trigger the eventfd 2285 * @match_data: whether to match against @data, instead of just @addr 2286 * @data: the data to match against the guest write 2287 * @e: event notifier to be triggered when @addr, @size, and @data all match. 2288 **/ 2289 void memory_region_add_eventfd(MemoryRegion *mr, 2290 hwaddr addr, 2291 unsigned size, 2292 bool match_data, 2293 uint64_t data, 2294 EventNotifier *e); 2295 2296 /** 2297 * memory_region_del_eventfd: Cancel an eventfd. 2298 * 2299 * Cancels an eventfd trigger requested by a previous 2300 * memory_region_add_eventfd() call. 2301 * 2302 * @mr: the memory region being updated. 2303 * @addr: the address within @mr that is to be monitored 2304 * @size: the size of the access to trigger the eventfd 2305 * @match_data: whether to match against @data, instead of just @addr 2306 * @data: the data to match against the guest write 2307 * @e: event notifier to be triggered when @addr, @size, and @data all match. 2308 */ 2309 void memory_region_del_eventfd(MemoryRegion *mr, 2310 hwaddr addr, 2311 unsigned size, 2312 bool match_data, 2313 uint64_t data, 2314 EventNotifier *e); 2315 2316 /** 2317 * memory_region_add_subregion: Add a subregion to a container. 2318 * 2319 * Adds a subregion at @offset. The subregion may not overlap with other 2320 * subregions (except for those explicitly marked as overlapping). A region 2321 * may only be added once as a subregion (unless removed with 2322 * memory_region_del_subregion()); use memory_region_init_alias() if you 2323 * want a region to be a subregion in multiple locations. 2324 * 2325 * @mr: the region to contain the new subregion; must be a container 2326 * initialized with memory_region_init(). 2327 * @offset: the offset relative to @mr where @subregion is added. 2328 * @subregion: the subregion to be added. 2329 */ 2330 void memory_region_add_subregion(MemoryRegion *mr, 2331 hwaddr offset, 2332 MemoryRegion *subregion); 2333 /** 2334 * memory_region_add_subregion_overlap: Add a subregion to a container 2335 * with overlap. 2336 * 2337 * Adds a subregion at @offset. The subregion may overlap with other 2338 * subregions. Conflicts are resolved by having a higher @priority hide a 2339 * lower @priority. Subregions without priority are taken as @priority 0. 2340 * A region may only be added once as a subregion (unless removed with 2341 * memory_region_del_subregion()); use memory_region_init_alias() if you 2342 * want a region to be a subregion in multiple locations. 2343 * 2344 * @mr: the region to contain the new subregion; must be a container 2345 * initialized with memory_region_init(). 2346 * @offset: the offset relative to @mr where @subregion is added. 2347 * @subregion: the subregion to be added. 2348 * @priority: used for resolving overlaps; highest priority wins. 2349 */ 2350 void memory_region_add_subregion_overlap(MemoryRegion *mr, 2351 hwaddr offset, 2352 MemoryRegion *subregion, 2353 int priority); 2354 2355 /** 2356 * memory_region_get_ram_addr: Get the ram address associated with a memory 2357 * region 2358 * 2359 * @mr: the region to be queried 2360 */ 2361 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr); 2362 2363 uint64_t memory_region_get_alignment(const MemoryRegion *mr); 2364 /** 2365 * memory_region_del_subregion: Remove a subregion. 2366 * 2367 * Removes a subregion from its container. 2368 * 2369 * @mr: the container to be updated. 2370 * @subregion: the region being removed; must be a current subregion of @mr. 2371 */ 2372 void memory_region_del_subregion(MemoryRegion *mr, 2373 MemoryRegion *subregion); 2374 2375 /* 2376 * memory_region_set_enabled: dynamically enable or disable a region 2377 * 2378 * Enables or disables a memory region. A disabled memory region 2379 * ignores all accesses to itself and its subregions. It does not 2380 * obscure sibling subregions with lower priority - it simply behaves as 2381 * if it was removed from the hierarchy. 2382 * 2383 * Regions default to being enabled. 2384 * 2385 * @mr: the region to be updated 2386 * @enabled: whether to enable or disable the region 2387 */ 2388 void memory_region_set_enabled(MemoryRegion *mr, bool enabled); 2389 2390 /* 2391 * memory_region_set_address: dynamically update the address of a region 2392 * 2393 * Dynamically updates the address of a region, relative to its container. 2394 * May be used on regions are currently part of a memory hierarchy. 2395 * 2396 * @mr: the region to be updated 2397 * @addr: new address, relative to container region 2398 */ 2399 void memory_region_set_address(MemoryRegion *mr, hwaddr addr); 2400 2401 /* 2402 * memory_region_set_size: dynamically update the size of a region. 2403 * 2404 * Dynamically updates the size of a region. 2405 * 2406 * @mr: the region to be updated 2407 * @size: used size of the region. 2408 */ 2409 void memory_region_set_size(MemoryRegion *mr, uint64_t size); 2410 2411 /* 2412 * memory_region_set_alias_offset: dynamically update a memory alias's offset 2413 * 2414 * Dynamically updates the offset into the target region that an alias points 2415 * to, as if the fourth argument to memory_region_init_alias() has changed. 2416 * 2417 * @mr: the #MemoryRegion to be updated; should be an alias. 2418 * @offset: the new offset into the target memory region 2419 */ 2420 void memory_region_set_alias_offset(MemoryRegion *mr, 2421 hwaddr offset); 2422 2423 /* 2424 * memory_region_set_unmergeable: Set a memory region unmergeable 2425 * 2426 * Mark a memory region unmergeable, resulting in the memory region (or 2427 * everything contained in a memory region container) not getting merged when 2428 * simplifying the address space and notifying memory listeners. Consequently, 2429 * memory listeners will never get notified about ranges that are larger than 2430 * the original memory regions. 2431 * 2432 * This is primarily useful when multiple aliases to a RAM memory region are 2433 * mapped into a memory region container, and updates (e.g., enable/disable or 2434 * map/unmap) of individual memory region aliases are not supposed to affect 2435 * other memory regions in the same container. 2436 * 2437 * @mr: the #MemoryRegion to be updated 2438 * @unmergeable: whether to mark the #MemoryRegion unmergeable 2439 */ 2440 void memory_region_set_unmergeable(MemoryRegion *mr, bool unmergeable); 2441 2442 /** 2443 * memory_region_present: checks if an address relative to a @container 2444 * translates into #MemoryRegion within @container 2445 * 2446 * Answer whether a #MemoryRegion within @container covers the address 2447 * @addr. 2448 * 2449 * @container: a #MemoryRegion within which @addr is a relative address 2450 * @addr: the area within @container to be searched 2451 */ 2452 bool memory_region_present(MemoryRegion *container, hwaddr addr); 2453 2454 /** 2455 * memory_region_is_mapped: returns true if #MemoryRegion is mapped 2456 * into another memory region, which does not necessarily imply that it is 2457 * mapped into an address space. 2458 * 2459 * @mr: a #MemoryRegion which should be checked if it's mapped 2460 */ 2461 bool memory_region_is_mapped(MemoryRegion *mr); 2462 2463 /** 2464 * memory_region_get_ram_discard_manager: get the #RamDiscardManager for a 2465 * #MemoryRegion 2466 * 2467 * The #RamDiscardManager cannot change while a memory region is mapped. 2468 * 2469 * @mr: the #MemoryRegion 2470 */ 2471 RamDiscardManager *memory_region_get_ram_discard_manager(MemoryRegion *mr); 2472 2473 /** 2474 * memory_region_has_ram_discard_manager: check whether a #MemoryRegion has a 2475 * #RamDiscardManager assigned 2476 * 2477 * @mr: the #MemoryRegion 2478 */ 2479 static inline bool memory_region_has_ram_discard_manager(MemoryRegion *mr) 2480 { 2481 return !!memory_region_get_ram_discard_manager(mr); 2482 } 2483 2484 /** 2485 * memory_region_set_ram_discard_manager: set the #RamDiscardManager for a 2486 * #MemoryRegion 2487 * 2488 * This function must not be called for a mapped #MemoryRegion, a #MemoryRegion 2489 * that does not cover RAM, or a #MemoryRegion that already has a 2490 * #RamDiscardManager assigned. 2491 * 2492 * @mr: the #MemoryRegion 2493 * @rdm: #RamDiscardManager to set 2494 */ 2495 void memory_region_set_ram_discard_manager(MemoryRegion *mr, 2496 RamDiscardManager *rdm); 2497 2498 /** 2499 * memory_region_find: translate an address/size relative to a 2500 * MemoryRegion into a #MemoryRegionSection. 2501 * 2502 * Locates the first #MemoryRegion within @mr that overlaps the range 2503 * given by @addr and @size. 2504 * 2505 * Returns a #MemoryRegionSection that describes a contiguous overlap. 2506 * It will have the following characteristics: 2507 * - @size = 0 iff no overlap was found 2508 * - @mr is non-%NULL iff an overlap was found 2509 * 2510 * Remember that in the return value the @offset_within_region is 2511 * relative to the returned region (in the .@mr field), not to the 2512 * @mr argument. 2513 * 2514 * Similarly, the .@offset_within_address_space is relative to the 2515 * address space that contains both regions, the passed and the 2516 * returned one. However, in the special case where the @mr argument 2517 * has no container (and thus is the root of the address space), the 2518 * following will hold: 2519 * - @offset_within_address_space >= @addr 2520 * - @offset_within_address_space + .@size <= @addr + @size 2521 * 2522 * @mr: a MemoryRegion within which @addr is a relative address 2523 * @addr: start of the area within @as to be searched 2524 * @size: size of the area to be searched 2525 */ 2526 MemoryRegionSection memory_region_find(MemoryRegion *mr, 2527 hwaddr addr, uint64_t size); 2528 2529 /** 2530 * memory_global_dirty_log_sync: synchronize the dirty log for all memory 2531 * 2532 * Synchronizes the dirty page log for all address spaces. 2533 * 2534 * @last_stage: whether this is the last stage of live migration 2535 */ 2536 void memory_global_dirty_log_sync(bool last_stage); 2537 2538 /** 2539 * memory_global_dirty_log_sync: synchronize the dirty log for all memory 2540 * 2541 * Synchronizes the vCPUs with a thread that is reading the dirty bitmap. 2542 * This function must be called after the dirty log bitmap is cleared, and 2543 * before dirty guest memory pages are read. If you are using 2544 * #DirtyBitmapSnapshot, memory_region_snapshot_and_clear_dirty() takes 2545 * care of doing this. 2546 */ 2547 void memory_global_after_dirty_log_sync(void); 2548 2549 /** 2550 * memory_region_transaction_begin: Start a transaction. 2551 * 2552 * During a transaction, changes will be accumulated and made visible 2553 * only when the transaction ends (is committed). 2554 */ 2555 void memory_region_transaction_begin(void); 2556 2557 /** 2558 * memory_region_transaction_commit: Commit a transaction and make changes 2559 * visible to the guest. 2560 */ 2561 void memory_region_transaction_commit(void); 2562 2563 /** 2564 * memory_listener_register: register callbacks to be called when memory 2565 * sections are mapped or unmapped into an address 2566 * space 2567 * 2568 * @listener: an object containing the callbacks to be called 2569 * @filter: if non-%NULL, only regions in this address space will be observed 2570 */ 2571 void memory_listener_register(MemoryListener *listener, AddressSpace *filter); 2572 2573 /** 2574 * memory_listener_unregister: undo the effect of memory_listener_register() 2575 * 2576 * @listener: an object containing the callbacks to be removed 2577 */ 2578 void memory_listener_unregister(MemoryListener *listener); 2579 2580 /** 2581 * memory_global_dirty_log_start: begin dirty logging for all regions 2582 * 2583 * @flags: purpose of starting dirty log, migration or dirty rate 2584 */ 2585 void memory_global_dirty_log_start(unsigned int flags); 2586 2587 /** 2588 * memory_global_dirty_log_stop: end dirty logging for all regions 2589 * 2590 * @flags: purpose of stopping dirty log, migration or dirty rate 2591 */ 2592 void memory_global_dirty_log_stop(unsigned int flags); 2593 2594 void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled); 2595 2596 bool memory_region_access_valid(MemoryRegion *mr, hwaddr addr, 2597 unsigned size, bool is_write, 2598 MemTxAttrs attrs); 2599 2600 /** 2601 * memory_region_dispatch_read: perform a read directly to the specified 2602 * MemoryRegion. 2603 * 2604 * @mr: #MemoryRegion to access 2605 * @addr: address within that region 2606 * @pval: pointer to uint64_t which the data is written to 2607 * @op: size, sign, and endianness of the memory operation 2608 * @attrs: memory transaction attributes to use for the access 2609 */ 2610 MemTxResult memory_region_dispatch_read(MemoryRegion *mr, 2611 hwaddr addr, 2612 uint64_t *pval, 2613 MemOp op, 2614 MemTxAttrs attrs); 2615 /** 2616 * memory_region_dispatch_write: perform a write directly to the specified 2617 * MemoryRegion. 2618 * 2619 * @mr: #MemoryRegion to access 2620 * @addr: address within that region 2621 * @data: data to write 2622 * @op: size, sign, and endianness of the memory operation 2623 * @attrs: memory transaction attributes to use for the access 2624 */ 2625 MemTxResult memory_region_dispatch_write(MemoryRegion *mr, 2626 hwaddr addr, 2627 uint64_t data, 2628 MemOp op, 2629 MemTxAttrs attrs); 2630 2631 /** 2632 * address_space_init: initializes an address space 2633 * 2634 * @as: an uninitialized #AddressSpace 2635 * @root: a #MemoryRegion that routes addresses for the address space 2636 * @name: an address space name. The name is only used for debugging 2637 * output. 2638 */ 2639 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name); 2640 2641 /** 2642 * address_space_destroy: destroy an address space 2643 * 2644 * Releases all resources associated with an address space. After an address space 2645 * is destroyed, its root memory region (given by address_space_init()) may be destroyed 2646 * as well. 2647 * 2648 * @as: address space to be destroyed 2649 */ 2650 void address_space_destroy(AddressSpace *as); 2651 2652 /** 2653 * address_space_remove_listeners: unregister all listeners of an address space 2654 * 2655 * Removes all callbacks previously registered with memory_listener_register() 2656 * for @as. 2657 * 2658 * @as: an initialized #AddressSpace 2659 */ 2660 void address_space_remove_listeners(AddressSpace *as); 2661 2662 /** 2663 * address_space_rw: read from or write to an address space. 2664 * 2665 * Return a MemTxResult indicating whether the operation succeeded 2666 * or failed (eg unassigned memory, device rejected the transaction, 2667 * IOMMU fault). 2668 * 2669 * @as: #AddressSpace to be accessed 2670 * @addr: address within that address space 2671 * @attrs: memory transaction attributes 2672 * @buf: buffer with the data transferred 2673 * @len: the number of bytes to read or write 2674 * @is_write: indicates the transfer direction 2675 */ 2676 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, 2677 MemTxAttrs attrs, void *buf, 2678 hwaddr len, bool is_write); 2679 2680 /** 2681 * address_space_write: write to address space. 2682 * 2683 * Return a MemTxResult indicating whether the operation succeeded 2684 * or failed (eg unassigned memory, device rejected the transaction, 2685 * IOMMU fault). 2686 * 2687 * @as: #AddressSpace to be accessed 2688 * @addr: address within that address space 2689 * @attrs: memory transaction attributes 2690 * @buf: buffer with the data transferred 2691 * @len: the number of bytes to write 2692 */ 2693 MemTxResult address_space_write(AddressSpace *as, hwaddr addr, 2694 MemTxAttrs attrs, 2695 const void *buf, hwaddr len); 2696 2697 /** 2698 * address_space_write_rom: write to address space, including ROM. 2699 * 2700 * This function writes to the specified address space, but will 2701 * write data to both ROM and RAM. This is used for non-guest 2702 * writes like writes from the gdb debug stub or initial loading 2703 * of ROM contents. 2704 * 2705 * Note that portions of the write which attempt to write data to 2706 * a device will be silently ignored -- only real RAM and ROM will 2707 * be written to. 2708 * 2709 * Return a MemTxResult indicating whether the operation succeeded 2710 * or failed (eg unassigned memory, device rejected the transaction, 2711 * IOMMU fault). 2712 * 2713 * @as: #AddressSpace to be accessed 2714 * @addr: address within that address space 2715 * @attrs: memory transaction attributes 2716 * @buf: buffer with the data transferred 2717 * @len: the number of bytes to write 2718 */ 2719 MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr, 2720 MemTxAttrs attrs, 2721 const void *buf, hwaddr len); 2722 2723 /* address_space_ld*: load from an address space 2724 * address_space_st*: store to an address space 2725 * 2726 * These functions perform a load or store of the byte, word, 2727 * longword or quad to the specified address within the AddressSpace. 2728 * The _le suffixed functions treat the data as little endian; 2729 * _be indicates big endian; no suffix indicates "same endianness 2730 * as guest CPU". 2731 * 2732 * The "guest CPU endianness" accessors are deprecated for use outside 2733 * target-* code; devices should be CPU-agnostic and use either the LE 2734 * or the BE accessors. 2735 * 2736 * @as #AddressSpace to be accessed 2737 * @addr: address within that address space 2738 * @val: data value, for stores 2739 * @attrs: memory transaction attributes 2740 * @result: location to write the success/failure of the transaction; 2741 * if NULL, this information is discarded 2742 */ 2743 2744 #define SUFFIX 2745 #define ARG1 as 2746 #define ARG1_DECL AddressSpace *as 2747 #include "exec/memory_ldst.h.inc" 2748 2749 #define SUFFIX 2750 #define ARG1 as 2751 #define ARG1_DECL AddressSpace *as 2752 #include "exec/memory_ldst_phys.h.inc" 2753 2754 struct MemoryRegionCache { 2755 void *ptr; 2756 hwaddr xlat; 2757 hwaddr len; 2758 FlatView *fv; 2759 MemoryRegionSection mrs; 2760 bool is_write; 2761 }; 2762 2763 /* address_space_ld*_cached: load from a cached #MemoryRegion 2764 * address_space_st*_cached: store into a cached #MemoryRegion 2765 * 2766 * These functions perform a load or store of the byte, word, 2767 * longword or quad to the specified address. The address is 2768 * a physical address in the AddressSpace, but it must lie within 2769 * a #MemoryRegion that was mapped with address_space_cache_init. 2770 * 2771 * The _le suffixed functions treat the data as little endian; 2772 * _be indicates big endian; no suffix indicates "same endianness 2773 * as guest CPU". 2774 * 2775 * The "guest CPU endianness" accessors are deprecated for use outside 2776 * target-* code; devices should be CPU-agnostic and use either the LE 2777 * or the BE accessors. 2778 * 2779 * @cache: previously initialized #MemoryRegionCache to be accessed 2780 * @addr: address within the address space 2781 * @val: data value, for stores 2782 * @attrs: memory transaction attributes 2783 * @result: location to write the success/failure of the transaction; 2784 * if NULL, this information is discarded 2785 */ 2786 2787 #define SUFFIX _cached_slow 2788 #define ARG1 cache 2789 #define ARG1_DECL MemoryRegionCache *cache 2790 #include "exec/memory_ldst.h.inc" 2791 2792 /* Inline fast path for direct RAM access. */ 2793 static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache, 2794 hwaddr addr, MemTxAttrs attrs, MemTxResult *result) 2795 { 2796 assert(addr < cache->len); 2797 if (likely(cache->ptr)) { 2798 return ldub_p(cache->ptr + addr); 2799 } else { 2800 return address_space_ldub_cached_slow(cache, addr, attrs, result); 2801 } 2802 } 2803 2804 static inline void address_space_stb_cached(MemoryRegionCache *cache, 2805 hwaddr addr, uint8_t val, MemTxAttrs attrs, MemTxResult *result) 2806 { 2807 assert(addr < cache->len); 2808 if (likely(cache->ptr)) { 2809 stb_p(cache->ptr + addr, val); 2810 } else { 2811 address_space_stb_cached_slow(cache, addr, val, attrs, result); 2812 } 2813 } 2814 2815 #define ENDIANNESS _le 2816 #include "exec/memory_ldst_cached.h.inc" 2817 2818 #define ENDIANNESS _be 2819 #include "exec/memory_ldst_cached.h.inc" 2820 2821 #define SUFFIX _cached 2822 #define ARG1 cache 2823 #define ARG1_DECL MemoryRegionCache *cache 2824 #include "exec/memory_ldst_phys.h.inc" 2825 2826 /* address_space_cache_init: prepare for repeated access to a physical 2827 * memory region 2828 * 2829 * @cache: #MemoryRegionCache to be filled 2830 * @as: #AddressSpace to be accessed 2831 * @addr: address within that address space 2832 * @len: length of buffer 2833 * @is_write: indicates the transfer direction 2834 * 2835 * Will only work with RAM, and may map a subset of the requested range by 2836 * returning a value that is less than @len. On failure, return a negative 2837 * errno value. 2838 * 2839 * Because it only works with RAM, this function can be used for 2840 * read-modify-write operations. In this case, is_write should be %true. 2841 * 2842 * Note that addresses passed to the address_space_*_cached functions 2843 * are relative to @addr. 2844 */ 2845 int64_t address_space_cache_init(MemoryRegionCache *cache, 2846 AddressSpace *as, 2847 hwaddr addr, 2848 hwaddr len, 2849 bool is_write); 2850 2851 /** 2852 * address_space_cache_init_empty: Initialize empty #MemoryRegionCache 2853 * 2854 * @cache: The #MemoryRegionCache to operate on. 2855 * 2856 * Initializes #MemoryRegionCache structure without memory region attached. 2857 * Cache initialized this way can only be safely destroyed, but not used. 2858 */ 2859 static inline void address_space_cache_init_empty(MemoryRegionCache *cache) 2860 { 2861 cache->mrs.mr = NULL; 2862 /* There is no real need to initialize fv, but it makes Coverity happy. */ 2863 cache->fv = NULL; 2864 } 2865 2866 /** 2867 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache 2868 * 2869 * @cache: The #MemoryRegionCache to operate on. 2870 * @addr: The first physical address that was written, relative to the 2871 * address that was passed to @address_space_cache_init. 2872 * @access_len: The number of bytes that were written starting at @addr. 2873 */ 2874 void address_space_cache_invalidate(MemoryRegionCache *cache, 2875 hwaddr addr, 2876 hwaddr access_len); 2877 2878 /** 2879 * address_space_cache_destroy: free a #MemoryRegionCache 2880 * 2881 * @cache: The #MemoryRegionCache whose memory should be released. 2882 */ 2883 void address_space_cache_destroy(MemoryRegionCache *cache); 2884 2885 /* address_space_get_iotlb_entry: translate an address into an IOTLB 2886 * entry. Should be called from an RCU critical section. 2887 */ 2888 IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr, 2889 bool is_write, MemTxAttrs attrs); 2890 2891 /* address_space_translate: translate an address range into an address space 2892 * into a MemoryRegion and an address range into that section. Should be 2893 * called from an RCU critical section, to avoid that the last reference 2894 * to the returned region disappears after address_space_translate returns. 2895 * 2896 * @fv: #FlatView to be accessed 2897 * @addr: address within that address space 2898 * @xlat: pointer to address within the returned memory region section's 2899 * #MemoryRegion. 2900 * @len: pointer to length 2901 * @is_write: indicates the transfer direction 2902 * @attrs: memory attributes 2903 */ 2904 MemoryRegion *flatview_translate(FlatView *fv, 2905 hwaddr addr, hwaddr *xlat, 2906 hwaddr *len, bool is_write, 2907 MemTxAttrs attrs); 2908 2909 static inline MemoryRegion *address_space_translate(AddressSpace *as, 2910 hwaddr addr, hwaddr *xlat, 2911 hwaddr *len, bool is_write, 2912 MemTxAttrs attrs) 2913 { 2914 return flatview_translate(address_space_to_flatview(as), 2915 addr, xlat, len, is_write, attrs); 2916 } 2917 2918 /* address_space_access_valid: check for validity of accessing an address 2919 * space range 2920 * 2921 * Check whether memory is assigned to the given address space range, and 2922 * access is permitted by any IOMMU regions that are active for the address 2923 * space. 2924 * 2925 * For now, addr and len should be aligned to a page size. This limitation 2926 * will be lifted in the future. 2927 * 2928 * @as: #AddressSpace to be accessed 2929 * @addr: address within that address space 2930 * @len: length of the area to be checked 2931 * @is_write: indicates the transfer direction 2932 * @attrs: memory attributes 2933 */ 2934 bool address_space_access_valid(AddressSpace *as, hwaddr addr, hwaddr len, 2935 bool is_write, MemTxAttrs attrs); 2936 2937 /* address_space_map: map a physical memory region into a host virtual address 2938 * 2939 * May map a subset of the requested range, given by and returned in @plen. 2940 * May return %NULL and set *@plen to zero(0), if resources needed to perform 2941 * the mapping are exhausted. 2942 * Use only for reads OR writes - not for read-modify-write operations. 2943 * Use cpu_register_map_client() to know when retrying the map operation is 2944 * likely to succeed. 2945 * 2946 * @as: #AddressSpace to be accessed 2947 * @addr: address within that address space 2948 * @plen: pointer to length of buffer; updated on return 2949 * @is_write: indicates the transfer direction 2950 * @attrs: memory attributes 2951 */ 2952 void *address_space_map(AddressSpace *as, hwaddr addr, 2953 hwaddr *plen, bool is_write, MemTxAttrs attrs); 2954 2955 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map() 2956 * 2957 * Will also mark the memory as dirty if @is_write == %true. @access_len gives 2958 * the amount of memory that was actually read or written by the caller. 2959 * 2960 * @as: #AddressSpace used 2961 * @buffer: host pointer as returned by address_space_map() 2962 * @len: buffer length as returned by address_space_map() 2963 * @access_len: amount of data actually transferred 2964 * @is_write: indicates the transfer direction 2965 */ 2966 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, 2967 bool is_write, hwaddr access_len); 2968 2969 2970 /* Internal functions, part of the implementation of address_space_read. */ 2971 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr, 2972 MemTxAttrs attrs, void *buf, hwaddr len); 2973 MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr, 2974 MemTxAttrs attrs, void *buf, 2975 hwaddr len, hwaddr addr1, hwaddr l, 2976 MemoryRegion *mr); 2977 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr); 2978 2979 /* Internal functions, part of the implementation of address_space_read_cached 2980 * and address_space_write_cached. */ 2981 MemTxResult address_space_read_cached_slow(MemoryRegionCache *cache, 2982 hwaddr addr, void *buf, hwaddr len); 2983 MemTxResult address_space_write_cached_slow(MemoryRegionCache *cache, 2984 hwaddr addr, const void *buf, 2985 hwaddr len); 2986 2987 int memory_access_size(MemoryRegion *mr, unsigned l, hwaddr addr); 2988 bool prepare_mmio_access(MemoryRegion *mr); 2989 2990 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) 2991 { 2992 if (is_write) { 2993 return memory_region_is_ram(mr) && !mr->readonly && 2994 !mr->rom_device && !memory_region_is_ram_device(mr); 2995 } else { 2996 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) || 2997 memory_region_is_romd(mr); 2998 } 2999 } 3000 3001 /** 3002 * address_space_read: read from an address space. 3003 * 3004 * Return a MemTxResult indicating whether the operation succeeded 3005 * or failed (eg unassigned memory, device rejected the transaction, 3006 * IOMMU fault). Called within RCU critical section. 3007 * 3008 * @as: #AddressSpace to be accessed 3009 * @addr: address within that address space 3010 * @attrs: memory transaction attributes 3011 * @buf: buffer with the data transferred 3012 * @len: length of the data transferred 3013 */ 3014 static inline __attribute__((__always_inline__)) 3015 MemTxResult address_space_read(AddressSpace *as, hwaddr addr, 3016 MemTxAttrs attrs, void *buf, 3017 hwaddr len) 3018 { 3019 MemTxResult result = MEMTX_OK; 3020 hwaddr l, addr1; 3021 void *ptr; 3022 MemoryRegion *mr; 3023 FlatView *fv; 3024 3025 if (__builtin_constant_p(len)) { 3026 if (len) { 3027 RCU_READ_LOCK_GUARD(); 3028 fv = address_space_to_flatview(as); 3029 l = len; 3030 mr = flatview_translate(fv, addr, &addr1, &l, false, attrs); 3031 if (len == l && memory_access_is_direct(mr, false)) { 3032 ptr = qemu_map_ram_ptr(mr->ram_block, addr1); 3033 memcpy(buf, ptr, len); 3034 } else { 3035 result = flatview_read_continue(fv, addr, attrs, buf, len, 3036 addr1, l, mr); 3037 } 3038 } 3039 } else { 3040 result = address_space_read_full(as, addr, attrs, buf, len); 3041 } 3042 return result; 3043 } 3044 3045 /** 3046 * address_space_read_cached: read from a cached RAM region 3047 * 3048 * @cache: Cached region to be addressed 3049 * @addr: address relative to the base of the RAM region 3050 * @buf: buffer with the data transferred 3051 * @len: length of the data transferred 3052 */ 3053 static inline MemTxResult 3054 address_space_read_cached(MemoryRegionCache *cache, hwaddr addr, 3055 void *buf, hwaddr len) 3056 { 3057 assert(addr < cache->len && len <= cache->len - addr); 3058 fuzz_dma_read_cb(cache->xlat + addr, len, cache->mrs.mr); 3059 if (likely(cache->ptr)) { 3060 memcpy(buf, cache->ptr + addr, len); 3061 return MEMTX_OK; 3062 } else { 3063 return address_space_read_cached_slow(cache, addr, buf, len); 3064 } 3065 } 3066 3067 /** 3068 * address_space_write_cached: write to a cached RAM region 3069 * 3070 * @cache: Cached region to be addressed 3071 * @addr: address relative to the base of the RAM region 3072 * @buf: buffer with the data transferred 3073 * @len: length of the data transferred 3074 */ 3075 static inline MemTxResult 3076 address_space_write_cached(MemoryRegionCache *cache, hwaddr addr, 3077 const void *buf, hwaddr len) 3078 { 3079 assert(addr < cache->len && len <= cache->len - addr); 3080 if (likely(cache->ptr)) { 3081 memcpy(cache->ptr + addr, buf, len); 3082 return MEMTX_OK; 3083 } else { 3084 return address_space_write_cached_slow(cache, addr, buf, len); 3085 } 3086 } 3087 3088 /** 3089 * address_space_set: Fill address space with a constant byte. 3090 * 3091 * Return a MemTxResult indicating whether the operation succeeded 3092 * or failed (eg unassigned memory, device rejected the transaction, 3093 * IOMMU fault). 3094 * 3095 * @as: #AddressSpace to be accessed 3096 * @addr: address within that address space 3097 * @c: constant byte to fill the memory 3098 * @len: the number of bytes to fill with the constant byte 3099 * @attrs: memory transaction attributes 3100 */ 3101 MemTxResult address_space_set(AddressSpace *as, hwaddr addr, 3102 uint8_t c, hwaddr len, MemTxAttrs attrs); 3103 3104 #ifdef NEED_CPU_H 3105 /* enum device_endian to MemOp. */ 3106 static inline MemOp devend_memop(enum device_endian end) 3107 { 3108 QEMU_BUILD_BUG_ON(DEVICE_HOST_ENDIAN != DEVICE_LITTLE_ENDIAN && 3109 DEVICE_HOST_ENDIAN != DEVICE_BIG_ENDIAN); 3110 3111 #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN 3112 /* Swap if non-host endianness or native (target) endianness */ 3113 return (end == DEVICE_HOST_ENDIAN) ? 0 : MO_BSWAP; 3114 #else 3115 const int non_host_endianness = 3116 DEVICE_LITTLE_ENDIAN ^ DEVICE_BIG_ENDIAN ^ DEVICE_HOST_ENDIAN; 3117 3118 /* In this case, native (target) endianness needs no swap. */ 3119 return (end == non_host_endianness) ? MO_BSWAP : 0; 3120 #endif 3121 } 3122 #endif 3123 3124 /* 3125 * Inhibit technologies that require discarding of pages in RAM blocks, e.g., 3126 * to manage the actual amount of memory consumed by the VM (then, the memory 3127 * provided by RAM blocks might be bigger than the desired memory consumption). 3128 * This *must* be set if: 3129 * - Discarding parts of a RAM blocks does not result in the change being 3130 * reflected in the VM and the pages getting freed. 3131 * - All memory in RAM blocks is pinned or duplicated, invaldiating any previous 3132 * discards blindly. 3133 * - Discarding parts of a RAM blocks will result in integrity issues (e.g., 3134 * encrypted VMs). 3135 * Technologies that only temporarily pin the current working set of a 3136 * driver are fine, because we don't expect such pages to be discarded 3137 * (esp. based on guest action like balloon inflation). 3138 * 3139 * This is *not* to be used to protect from concurrent discards (esp., 3140 * postcopy). 3141 * 3142 * Returns 0 if successful. Returns -EBUSY if a technology that relies on 3143 * discards to work reliably is active. 3144 */ 3145 int ram_block_discard_disable(bool state); 3146 3147 /* 3148 * See ram_block_discard_disable(): only disable uncoordinated discards, 3149 * keeping coordinated discards (via the RamDiscardManager) enabled. 3150 */ 3151 int ram_block_uncoordinated_discard_disable(bool state); 3152 3153 /* 3154 * Inhibit technologies that disable discarding of pages in RAM blocks. 3155 * 3156 * Returns 0 if successful. Returns -EBUSY if discards are already set to 3157 * broken. 3158 */ 3159 int ram_block_discard_require(bool state); 3160 3161 /* 3162 * See ram_block_discard_require(): only inhibit technologies that disable 3163 * uncoordinated discarding of pages in RAM blocks, allowing co-existence with 3164 * technologies that only inhibit uncoordinated discards (via the 3165 * RamDiscardManager). 3166 */ 3167 int ram_block_coordinated_discard_require(bool state); 3168 3169 /* 3170 * Test if any discarding of memory in ram blocks is disabled. 3171 */ 3172 bool ram_block_discard_is_disabled(void); 3173 3174 /* 3175 * Test if any discarding of memory in ram blocks is required to work reliably. 3176 */ 3177 bool ram_block_discard_is_required(void); 3178 3179 #endif 3180 3181 #endif 3182