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