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