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