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 <stdint.h> 20 #include <stdbool.h> 21 #include "qemu-common.h" 22 #include "exec/cpu-common.h" 23 #ifndef CONFIG_USER_ONLY 24 #include "exec/hwaddr.h" 25 #endif 26 #include "qemu/queue.h" 27 #include "qemu/int128.h" 28 #include "qemu/notify.h" 29 30 #define MAX_PHYS_ADDR_SPACE_BITS 62 31 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1) 32 33 typedef struct MemoryRegionOps MemoryRegionOps; 34 typedef struct MemoryRegionMmio MemoryRegionMmio; 35 36 /* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic 37 * registration. 38 */ 39 #define DIRTY_MEMORY_VGA 0 40 #define DIRTY_MEMORY_CODE 1 41 #define DIRTY_MEMORY_MIGRATION 3 42 43 struct MemoryRegionMmio { 44 CPUReadMemoryFunc *read[3]; 45 CPUWriteMemoryFunc *write[3]; 46 }; 47 48 typedef struct IOMMUTLBEntry IOMMUTLBEntry; 49 50 /* See address_space_translate: bit 0 is read, bit 1 is write. */ 51 typedef enum { 52 IOMMU_NONE = 0, 53 IOMMU_RO = 1, 54 IOMMU_WO = 2, 55 IOMMU_RW = 3, 56 } IOMMUAccessFlags; 57 58 struct IOMMUTLBEntry { 59 AddressSpace *target_as; 60 hwaddr iova; 61 hwaddr translated_addr; 62 hwaddr addr_mask; /* 0xfff = 4k translation */ 63 IOMMUAccessFlags perm; 64 }; 65 66 /* 67 * Memory region callbacks 68 */ 69 struct MemoryRegionOps { 70 /* Read from the memory region. @addr is relative to @mr; @size is 71 * in bytes. */ 72 uint64_t (*read)(void *opaque, 73 hwaddr addr, 74 unsigned size); 75 /* Write to the memory region. @addr is relative to @mr; @size is 76 * in bytes. */ 77 void (*write)(void *opaque, 78 hwaddr addr, 79 uint64_t data, 80 unsigned size); 81 82 enum device_endian endianness; 83 /* Guest-visible constraints: */ 84 struct { 85 /* If nonzero, specify bounds on access sizes beyond which a machine 86 * check is thrown. 87 */ 88 unsigned min_access_size; 89 unsigned max_access_size; 90 /* If true, unaligned accesses are supported. Otherwise unaligned 91 * accesses throw machine checks. 92 */ 93 bool unaligned; 94 /* 95 * If present, and returns #false, the transaction is not accepted 96 * by the device (and results in machine dependent behaviour such 97 * as a machine check exception). 98 */ 99 bool (*accepts)(void *opaque, hwaddr addr, 100 unsigned size, bool is_write); 101 } valid; 102 /* Internal implementation constraints: */ 103 struct { 104 /* If nonzero, specifies the minimum size implemented. Smaller sizes 105 * will be rounded upwards and a partial result will be returned. 106 */ 107 unsigned min_access_size; 108 /* If nonzero, specifies the maximum size implemented. Larger sizes 109 * will be done as a series of accesses with smaller sizes. 110 */ 111 unsigned max_access_size; 112 /* If true, unaligned accesses are supported. Otherwise all accesses 113 * are converted to (possibly multiple) naturally aligned accesses. 114 */ 115 bool unaligned; 116 } impl; 117 118 /* If .read and .write are not present, old_mmio may be used for 119 * backwards compatibility with old mmio registration 120 */ 121 const MemoryRegionMmio old_mmio; 122 }; 123 124 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps; 125 126 struct MemoryRegionIOMMUOps { 127 /* Return a TLB entry that contains a given address. */ 128 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr); 129 }; 130 131 typedef struct CoalescedMemoryRange CoalescedMemoryRange; 132 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; 133 134 struct MemoryRegion { 135 /* All fields are private - violators will be prosecuted */ 136 const MemoryRegionOps *ops; 137 const MemoryRegionIOMMUOps *iommu_ops; 138 void *opaque; 139 struct Object *owner; 140 MemoryRegion *parent; 141 Int128 size; 142 hwaddr addr; 143 void (*destructor)(MemoryRegion *mr); 144 ram_addr_t ram_addr; 145 bool subpage; 146 bool terminates; 147 bool romd_mode; 148 bool ram; 149 bool readonly; /* For RAM regions */ 150 bool enabled; 151 bool rom_device; 152 bool warning_printed; /* For reservations */ 153 bool flush_coalesced_mmio; 154 MemoryRegion *alias; 155 hwaddr alias_offset; 156 unsigned priority; 157 bool may_overlap; 158 QTAILQ_HEAD(subregions, MemoryRegion) subregions; 159 QTAILQ_ENTRY(MemoryRegion) subregions_link; 160 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced; 161 const char *name; 162 uint8_t dirty_log_mask; 163 unsigned ioeventfd_nb; 164 MemoryRegionIoeventfd *ioeventfds; 165 NotifierList iommu_notify; 166 }; 167 168 /** 169 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects 170 */ 171 struct AddressSpace { 172 /* All fields are private. */ 173 char *name; 174 MemoryRegion *root; 175 struct FlatView *current_map; 176 int ioeventfd_nb; 177 struct MemoryRegionIoeventfd *ioeventfds; 178 struct AddressSpaceDispatch *dispatch; 179 QTAILQ_ENTRY(AddressSpace) address_spaces_link; 180 }; 181 182 /** 183 * MemoryRegionSection: describes a fragment of a #MemoryRegion 184 * 185 * @mr: the region, or %NULL if empty 186 * @address_space: the address space the region is mapped in 187 * @offset_within_region: the beginning of the section, relative to @mr's start 188 * @size: the size of the section; will not exceed @mr's boundaries 189 * @offset_within_address_space: the address of the first byte of the section 190 * relative to the region's address space 191 * @readonly: writes to this section are ignored 192 */ 193 struct MemoryRegionSection { 194 MemoryRegion *mr; 195 AddressSpace *address_space; 196 hwaddr offset_within_region; 197 Int128 size; 198 hwaddr offset_within_address_space; 199 bool readonly; 200 }; 201 202 typedef struct MemoryListener MemoryListener; 203 204 /** 205 * MemoryListener: callbacks structure for updates to the physical memory map 206 * 207 * Allows a component to adjust to changes in the guest-visible memory map. 208 * Use with memory_listener_register() and memory_listener_unregister(). 209 */ 210 struct MemoryListener { 211 void (*begin)(MemoryListener *listener); 212 void (*commit)(MemoryListener *listener); 213 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section); 214 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section); 215 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section); 216 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section); 217 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section); 218 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section); 219 void (*log_global_start)(MemoryListener *listener); 220 void (*log_global_stop)(MemoryListener *listener); 221 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section, 222 bool match_data, uint64_t data, EventNotifier *e); 223 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section, 224 bool match_data, uint64_t data, EventNotifier *e); 225 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section, 226 hwaddr addr, hwaddr len); 227 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section, 228 hwaddr addr, hwaddr len); 229 /* Lower = earlier (during add), later (during del) */ 230 unsigned priority; 231 AddressSpace *address_space_filter; 232 QTAILQ_ENTRY(MemoryListener) link; 233 }; 234 235 /** 236 * memory_region_init: Initialize a memory region 237 * 238 * The region typically acts as a container for other memory regions. Use 239 * memory_region_add_subregion() to add subregions. 240 * 241 * @mr: the #MemoryRegion to be initialized 242 * @owner: the object that tracks the region's reference count 243 * @name: used for debugging; not visible to the user or ABI 244 * @size: size of the region; any subregions beyond this size will be clipped 245 */ 246 void memory_region_init(MemoryRegion *mr, 247 struct Object *owner, 248 const char *name, 249 uint64_t size); 250 251 /** 252 * memory_region_ref: Add 1 to a memory region's reference count 253 * 254 * Whenever memory regions are accessed outside the BQL, they need to be 255 * preserved against hot-unplug. MemoryRegions actually do not have their 256 * own reference count; they piggyback on a QOM object, their "owner". 257 * This function adds a reference to the owner. 258 * 259 * All MemoryRegions must have an owner if they can disappear, even if the 260 * device they belong to operates exclusively under the BQL. This is because 261 * the region could be returned at any time by memory_region_find, and this 262 * is usually under guest control. 263 * 264 * @mr: the #MemoryRegion 265 */ 266 void memory_region_ref(MemoryRegion *mr); 267 268 /** 269 * memory_region_unref: Remove 1 to a memory region's reference count 270 * 271 * Whenever memory regions are accessed outside the BQL, they need to be 272 * preserved against hot-unplug. MemoryRegions actually do not have their 273 * own reference count; they piggyback on a QOM object, their "owner". 274 * This function removes a reference to the owner and possibly destroys it. 275 * 276 * @mr: the #MemoryRegion 277 */ 278 void memory_region_unref(MemoryRegion *mr); 279 280 /** 281 * memory_region_init_io: Initialize an I/O memory region. 282 * 283 * Accesses into the region will cause the callbacks in @ops to be called. 284 * if @size is nonzero, subregions will be clipped to @size. 285 * 286 * @mr: the #MemoryRegion to be initialized. 287 * @owner: the object that tracks the region's reference count 288 * @ops: a structure containing read and write callbacks to be used when 289 * I/O is performed on the region. 290 * @opaque: passed to to the read and write callbacks of the @ops structure. 291 * @name: used for debugging; not visible to the user or ABI 292 * @size: size of the region. 293 */ 294 void memory_region_init_io(MemoryRegion *mr, 295 struct Object *owner, 296 const MemoryRegionOps *ops, 297 void *opaque, 298 const char *name, 299 uint64_t size); 300 301 /** 302 * memory_region_init_ram: Initialize RAM memory region. Accesses into the 303 * region will modify memory directly. 304 * 305 * @mr: the #MemoryRegion to be initialized. 306 * @owner: the object that tracks the region's reference count 307 * @name: the name of the region. 308 * @size: size of the region. 309 */ 310 void memory_region_init_ram(MemoryRegion *mr, 311 struct Object *owner, 312 const char *name, 313 uint64_t size); 314 315 /** 316 * memory_region_init_ram_ptr: Initialize RAM memory region from a 317 * user-provided pointer. Accesses into the 318 * region will modify memory directly. 319 * 320 * @mr: the #MemoryRegion to be initialized. 321 * @owner: the object that tracks the region's reference count 322 * @name: the name of the region. 323 * @size: size of the region. 324 * @ptr: memory to be mapped; must contain at least @size bytes. 325 */ 326 void memory_region_init_ram_ptr(MemoryRegion *mr, 327 struct Object *owner, 328 const char *name, 329 uint64_t size, 330 void *ptr); 331 332 /** 333 * memory_region_init_alias: Initialize a memory region that aliases all or a 334 * part of another memory region. 335 * 336 * @mr: the #MemoryRegion to be initialized. 337 * @owner: the object that tracks the region's reference count 338 * @name: used for debugging; not visible to the user or ABI 339 * @orig: the region to be referenced; @mr will be equivalent to 340 * @orig between @offset and @offset + @size - 1. 341 * @offset: start of the section in @orig to be referenced. 342 * @size: size of the region. 343 */ 344 void memory_region_init_alias(MemoryRegion *mr, 345 struct Object *owner, 346 const char *name, 347 MemoryRegion *orig, 348 hwaddr offset, 349 uint64_t size); 350 351 /** 352 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are 353 * handled via callbacks. 354 * 355 * @mr: the #MemoryRegion to be initialized. 356 * @owner: the object that tracks the region's reference count 357 * @ops: callbacks for write access handling. 358 * @name: the name of the region. 359 * @size: size of the region. 360 */ 361 void memory_region_init_rom_device(MemoryRegion *mr, 362 struct Object *owner, 363 const MemoryRegionOps *ops, 364 void *opaque, 365 const char *name, 366 uint64_t size); 367 368 /** 369 * memory_region_init_reservation: Initialize a memory region that reserves 370 * I/O space. 371 * 372 * A reservation region primariy serves debugging purposes. It claims I/O 373 * space that is not supposed to be handled by QEMU itself. Any access via 374 * the memory API will cause an abort(). 375 * 376 * @mr: the #MemoryRegion to be initialized 377 * @owner: the object that tracks the region's reference count 378 * @name: used for debugging; not visible to the user or ABI 379 * @size: size of the region. 380 */ 381 void memory_region_init_reservation(MemoryRegion *mr, 382 struct Object *owner, 383 const char *name, 384 uint64_t size); 385 386 /** 387 * memory_region_init_iommu: Initialize a memory region that translates 388 * addresses 389 * 390 * An IOMMU region translates addresses and forwards accesses to a target 391 * memory region. 392 * 393 * @mr: the #MemoryRegion to be initialized 394 * @owner: the object that tracks the region's reference count 395 * @ops: a function that translates addresses into the @target region 396 * @name: used for debugging; not visible to the user or ABI 397 * @size: size of the region. 398 */ 399 void memory_region_init_iommu(MemoryRegion *mr, 400 struct Object *owner, 401 const MemoryRegionIOMMUOps *ops, 402 const char *name, 403 uint64_t size); 404 405 /** 406 * memory_region_destroy: Destroy a memory region and reclaim all resources. 407 * 408 * @mr: the region to be destroyed. May not currently be a subregion 409 * (see memory_region_add_subregion()) or referenced in an alias 410 * (see memory_region_init_alias()). 411 */ 412 void memory_region_destroy(MemoryRegion *mr); 413 414 /** 415 * memory_region_owner: get a memory region's owner. 416 * 417 * @mr: the memory region being queried. 418 */ 419 struct Object *memory_region_owner(MemoryRegion *mr); 420 421 /** 422 * memory_region_size: get a memory region's size. 423 * 424 * @mr: the memory region being queried. 425 */ 426 uint64_t memory_region_size(MemoryRegion *mr); 427 428 /** 429 * memory_region_is_ram: check whether a memory region is random access 430 * 431 * Returns %true is a memory region is random access. 432 * 433 * @mr: the memory region being queried 434 */ 435 bool memory_region_is_ram(MemoryRegion *mr); 436 437 /** 438 * memory_region_is_romd: check whether a memory region is in ROMD mode 439 * 440 * Returns %true if a memory region is a ROM device and currently set to allow 441 * direct reads. 442 * 443 * @mr: the memory region being queried 444 */ 445 static inline bool memory_region_is_romd(MemoryRegion *mr) 446 { 447 return mr->rom_device && mr->romd_mode; 448 } 449 450 /** 451 * memory_region_is_iommu: check whether a memory region is an iommu 452 * 453 * Returns %true is a memory region is an iommu. 454 * 455 * @mr: the memory region being queried 456 */ 457 bool memory_region_is_iommu(MemoryRegion *mr); 458 459 /** 460 * memory_region_notify_iommu: notify a change in an IOMMU translation entry. 461 * 462 * @mr: the memory region that was changed 463 * @entry: the new entry in the IOMMU translation table. The entry 464 * replaces all old entries for the same virtual I/O address range. 465 * Deleted entries have .@perm == 0. 466 */ 467 void memory_region_notify_iommu(MemoryRegion *mr, 468 IOMMUTLBEntry entry); 469 470 /** 471 * memory_region_register_iommu_notifier: register a notifier for changes to 472 * IOMMU translation entries. 473 * 474 * @mr: the memory region to observe 475 * @n: the notifier to be added; the notifier receives a pointer to an 476 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be 477 * valid on exit from the notifier. 478 */ 479 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n); 480 481 /** 482 * memory_region_unregister_iommu_notifier: unregister a notifier for 483 * changes to IOMMU translation entries. 484 * 485 * @n: the notifier to be removed. 486 */ 487 void memory_region_unregister_iommu_notifier(Notifier *n); 488 489 /** 490 * memory_region_name: get a memory region's name 491 * 492 * Returns the string that was used to initialize the memory region. 493 * 494 * @mr: the memory region being queried 495 */ 496 const char *memory_region_name(MemoryRegion *mr); 497 498 /** 499 * memory_region_is_logging: return whether a memory region is logging writes 500 * 501 * Returns %true if the memory region is logging writes 502 * 503 * @mr: the memory region being queried 504 */ 505 bool memory_region_is_logging(MemoryRegion *mr); 506 507 /** 508 * memory_region_is_rom: check whether a memory region is ROM 509 * 510 * Returns %true is a memory region is read-only memory. 511 * 512 * @mr: the memory region being queried 513 */ 514 bool memory_region_is_rom(MemoryRegion *mr); 515 516 /** 517 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region. 518 * 519 * Returns a host pointer to a RAM memory region (created with 520 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with 521 * care. 522 * 523 * @mr: the memory region being queried. 524 */ 525 void *memory_region_get_ram_ptr(MemoryRegion *mr); 526 527 /** 528 * memory_region_set_log: Turn dirty logging on or off for a region. 529 * 530 * Turns dirty logging on or off for a specified client (display, migration). 531 * Only meaningful for RAM regions. 532 * 533 * @mr: the memory region being updated. 534 * @log: whether dirty logging is to be enabled or disabled. 535 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 536 * %DIRTY_MEMORY_VGA. 537 */ 538 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client); 539 540 /** 541 * memory_region_get_dirty: Check whether a range of bytes is dirty 542 * for a specified client. 543 * 544 * Checks whether a range of bytes has been written to since the last 545 * call to memory_region_reset_dirty() with the same @client. Dirty logging 546 * must be enabled. 547 * 548 * @mr: the memory region being queried. 549 * @addr: the address (relative to the start of the region) being queried. 550 * @size: the size of the range being queried. 551 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 552 * %DIRTY_MEMORY_VGA. 553 */ 554 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr, 555 hwaddr size, unsigned client); 556 557 /** 558 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region. 559 * 560 * Marks a range of bytes as dirty, after it has been dirtied outside 561 * guest code. 562 * 563 * @mr: the memory region being dirtied. 564 * @addr: the address (relative to the start of the region) being dirtied. 565 * @size: size of the range being dirtied. 566 */ 567 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, 568 hwaddr size); 569 570 /** 571 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty 572 * for a specified client. It clears them. 573 * 574 * Checks whether a range of bytes has been written to since the last 575 * call to memory_region_reset_dirty() with the same @client. Dirty logging 576 * must be enabled. 577 * 578 * @mr: the memory region being queried. 579 * @addr: the address (relative to the start of the region) being queried. 580 * @size: the size of the range being queried. 581 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 582 * %DIRTY_MEMORY_VGA. 583 */ 584 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr, 585 hwaddr size, unsigned client); 586 /** 587 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with 588 * any external TLBs (e.g. kvm) 589 * 590 * Flushes dirty information from accelerators such as kvm and vhost-net 591 * and makes it available to users of the memory API. 592 * 593 * @mr: the region being flushed. 594 */ 595 void memory_region_sync_dirty_bitmap(MemoryRegion *mr); 596 597 /** 598 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified 599 * client. 600 * 601 * Marks a range of pages as no longer dirty. 602 * 603 * @mr: the region being updated. 604 * @addr: the start of the subrange being cleaned. 605 * @size: the size of the subrange being cleaned. 606 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 607 * %DIRTY_MEMORY_VGA. 608 */ 609 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr, 610 hwaddr size, unsigned client); 611 612 /** 613 * memory_region_set_readonly: Turn a memory region read-only (or read-write) 614 * 615 * Allows a memory region to be marked as read-only (turning it into a ROM). 616 * only useful on RAM regions. 617 * 618 * @mr: the region being updated. 619 * @readonly: whether rhe region is to be ROM or RAM. 620 */ 621 void memory_region_set_readonly(MemoryRegion *mr, bool readonly); 622 623 /** 624 * memory_region_rom_device_set_romd: enable/disable ROMD mode 625 * 626 * Allows a ROM device (initialized with memory_region_init_rom_device() to 627 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the 628 * device is mapped to guest memory and satisfies read access directly. 629 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function. 630 * Writes are always handled by the #MemoryRegion.write function. 631 * 632 * @mr: the memory region to be updated 633 * @romd_mode: %true to put the region into ROMD mode 634 */ 635 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode); 636 637 /** 638 * memory_region_set_coalescing: Enable memory coalescing for the region. 639 * 640 * Enabled writes to a region to be queued for later processing. MMIO ->write 641 * callbacks may be delayed until a non-coalesced MMIO is issued. 642 * Only useful for IO regions. Roughly similar to write-combining hardware. 643 * 644 * @mr: the memory region to be write coalesced 645 */ 646 void memory_region_set_coalescing(MemoryRegion *mr); 647 648 /** 649 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of 650 * a region. 651 * 652 * Like memory_region_set_coalescing(), but works on a sub-range of a region. 653 * Multiple calls can be issued coalesced disjoint ranges. 654 * 655 * @mr: the memory region to be updated. 656 * @offset: the start of the range within the region to be coalesced. 657 * @size: the size of the subrange to be coalesced. 658 */ 659 void memory_region_add_coalescing(MemoryRegion *mr, 660 hwaddr offset, 661 uint64_t size); 662 663 /** 664 * memory_region_clear_coalescing: Disable MMIO coalescing for the region. 665 * 666 * Disables any coalescing caused by memory_region_set_coalescing() or 667 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory 668 * hardware. 669 * 670 * @mr: the memory region to be updated. 671 */ 672 void memory_region_clear_coalescing(MemoryRegion *mr); 673 674 /** 675 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before 676 * accesses. 677 * 678 * Ensure that pending coalesced MMIO request are flushed before the memory 679 * region is accessed. This property is automatically enabled for all regions 680 * passed to memory_region_set_coalescing() and memory_region_add_coalescing(). 681 * 682 * @mr: the memory region to be updated. 683 */ 684 void memory_region_set_flush_coalesced(MemoryRegion *mr); 685 686 /** 687 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before 688 * accesses. 689 * 690 * Clear the automatic coalesced MMIO flushing enabled via 691 * memory_region_set_flush_coalesced. Note that this service has no effect on 692 * memory regions that have MMIO coalescing enabled for themselves. For them, 693 * automatic flushing will stop once coalescing is disabled. 694 * 695 * @mr: the memory region to be updated. 696 */ 697 void memory_region_clear_flush_coalesced(MemoryRegion *mr); 698 699 /** 700 * memory_region_add_eventfd: Request an eventfd to be triggered when a word 701 * is written to a location. 702 * 703 * Marks a word in an IO region (initialized with memory_region_init_io()) 704 * as a trigger for an eventfd event. The I/O callback will not be called. 705 * The caller must be prepared to handle failure (that is, take the required 706 * action if the callback _is_ called). 707 * 708 * @mr: the memory region being updated. 709 * @addr: the address within @mr that is to be monitored 710 * @size: the size of the access to trigger the eventfd 711 * @match_data: whether to match against @data, instead of just @addr 712 * @data: the data to match against the guest write 713 * @fd: the eventfd to be triggered when @addr, @size, and @data all match. 714 **/ 715 void memory_region_add_eventfd(MemoryRegion *mr, 716 hwaddr addr, 717 unsigned size, 718 bool match_data, 719 uint64_t data, 720 EventNotifier *e); 721 722 /** 723 * memory_region_del_eventfd: Cancel an eventfd. 724 * 725 * Cancels an eventfd trigger requested by a previous 726 * memory_region_add_eventfd() call. 727 * 728 * @mr: the memory region being updated. 729 * @addr: the address within @mr that is to be monitored 730 * @size: the size of the access to trigger the eventfd 731 * @match_data: whether to match against @data, instead of just @addr 732 * @data: the data to match against the guest write 733 * @fd: the eventfd to be triggered when @addr, @size, and @data all match. 734 */ 735 void memory_region_del_eventfd(MemoryRegion *mr, 736 hwaddr addr, 737 unsigned size, 738 bool match_data, 739 uint64_t data, 740 EventNotifier *e); 741 742 /** 743 * memory_region_add_subregion: Add a subregion to a container. 744 * 745 * Adds a subregion at @offset. The subregion may not overlap with other 746 * subregions (except for those explicitly marked as overlapping). A region 747 * may only be added once as a subregion (unless removed with 748 * memory_region_del_subregion()); use memory_region_init_alias() if you 749 * want a region to be a subregion in multiple locations. 750 * 751 * @mr: the region to contain the new subregion; must be a container 752 * initialized with memory_region_init(). 753 * @offset: the offset relative to @mr where @subregion is added. 754 * @subregion: the subregion to be added. 755 */ 756 void memory_region_add_subregion(MemoryRegion *mr, 757 hwaddr offset, 758 MemoryRegion *subregion); 759 /** 760 * memory_region_add_subregion_overlap: Add a subregion to a container 761 * with overlap. 762 * 763 * Adds a subregion at @offset. The subregion may overlap with other 764 * subregions. Conflicts are resolved by having a higher @priority hide a 765 * lower @priority. Subregions without priority are taken as @priority 0. 766 * A region may only be added once as a subregion (unless removed with 767 * memory_region_del_subregion()); use memory_region_init_alias() if you 768 * want a region to be a subregion in multiple locations. 769 * 770 * @mr: the region to contain the new subregion; must be a container 771 * initialized with memory_region_init(). 772 * @offset: the offset relative to @mr where @subregion is added. 773 * @subregion: the subregion to be added. 774 * @priority: used for resolving overlaps; highest priority wins. 775 */ 776 void memory_region_add_subregion_overlap(MemoryRegion *mr, 777 hwaddr offset, 778 MemoryRegion *subregion, 779 unsigned priority); 780 781 /** 782 * memory_region_get_ram_addr: Get the ram address associated with a memory 783 * region 784 * 785 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen 786 * code is being reworked. 787 */ 788 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr); 789 790 /** 791 * memory_region_del_subregion: Remove a subregion. 792 * 793 * Removes a subregion from its container. 794 * 795 * @mr: the container to be updated. 796 * @subregion: the region being removed; must be a current subregion of @mr. 797 */ 798 void memory_region_del_subregion(MemoryRegion *mr, 799 MemoryRegion *subregion); 800 801 /* 802 * memory_region_set_enabled: dynamically enable or disable a region 803 * 804 * Enables or disables a memory region. A disabled memory region 805 * ignores all accesses to itself and its subregions. It does not 806 * obscure sibling subregions with lower priority - it simply behaves as 807 * if it was removed from the hierarchy. 808 * 809 * Regions default to being enabled. 810 * 811 * @mr: the region to be updated 812 * @enabled: whether to enable or disable the region 813 */ 814 void memory_region_set_enabled(MemoryRegion *mr, bool enabled); 815 816 /* 817 * memory_region_set_address: dynamically update the address of a region 818 * 819 * Dynamically updates the address of a region, relative to its parent. 820 * May be used on regions are currently part of a memory hierarchy. 821 * 822 * @mr: the region to be updated 823 * @addr: new address, relative to parent region 824 */ 825 void memory_region_set_address(MemoryRegion *mr, hwaddr addr); 826 827 /* 828 * memory_region_set_alias_offset: dynamically update a memory alias's offset 829 * 830 * Dynamically updates the offset into the target region that an alias points 831 * to, as if the fourth argument to memory_region_init_alias() has changed. 832 * 833 * @mr: the #MemoryRegion to be updated; should be an alias. 834 * @offset: the new offset into the target memory region 835 */ 836 void memory_region_set_alias_offset(MemoryRegion *mr, 837 hwaddr offset); 838 839 /** 840 * memory_region_present: translate an address/size relative to a 841 * MemoryRegion into a #MemoryRegionSection. 842 * 843 * Answer whether a #MemoryRegion within @parent covers the address 844 * @addr. 845 * 846 * @parent: a MemoryRegion within which @addr is a relative address 847 * @addr: the area within @parent to be searched 848 */ 849 bool memory_region_present(MemoryRegion *parent, hwaddr addr); 850 851 /** 852 * memory_region_find: translate an address/size relative to a 853 * MemoryRegion into a #MemoryRegionSection. 854 * 855 * Locates the first #MemoryRegion within @mr that overlaps the range 856 * given by @addr and @size. 857 * 858 * Returns a #MemoryRegionSection that describes a contiguous overlap. 859 * It will have the following characteristics: 860 * .@size = 0 iff no overlap was found 861 * .@mr is non-%NULL iff an overlap was found 862 * 863 * Remember that in the return value the @offset_within_region is 864 * relative to the returned region (in the .@mr field), not to the 865 * @mr argument. 866 * 867 * Similarly, the .@offset_within_address_space is relative to the 868 * address space that contains both regions, the passed and the 869 * returned one. However, in the special case where the @mr argument 870 * has no parent (and thus is the root of the address space), the 871 * following will hold: 872 * .@offset_within_address_space >= @addr 873 * .@offset_within_address_space + .@size <= @addr + @size 874 * 875 * @mr: a MemoryRegion within which @addr is a relative address 876 * @addr: start of the area within @as to be searched 877 * @size: size of the area to be searched 878 */ 879 MemoryRegionSection memory_region_find(MemoryRegion *mr, 880 hwaddr addr, uint64_t size); 881 882 /** 883 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory 884 * 885 * Synchronizes the dirty page log for an entire address space. 886 * @as: the address space that contains the memory being synchronized 887 */ 888 void address_space_sync_dirty_bitmap(AddressSpace *as); 889 890 /** 891 * memory_region_transaction_begin: Start a transaction. 892 * 893 * During a transaction, changes will be accumulated and made visible 894 * only when the transaction ends (is committed). 895 */ 896 void memory_region_transaction_begin(void); 897 898 /** 899 * memory_region_transaction_commit: Commit a transaction and make changes 900 * visible to the guest. 901 */ 902 void memory_region_transaction_commit(void); 903 904 /** 905 * memory_listener_register: register callbacks to be called when memory 906 * sections are mapped or unmapped into an address 907 * space 908 * 909 * @listener: an object containing the callbacks to be called 910 * @filter: if non-%NULL, only regions in this address space will be observed 911 */ 912 void memory_listener_register(MemoryListener *listener, AddressSpace *filter); 913 914 /** 915 * memory_listener_unregister: undo the effect of memory_listener_register() 916 * 917 * @listener: an object containing the callbacks to be removed 918 */ 919 void memory_listener_unregister(MemoryListener *listener); 920 921 /** 922 * memory_global_dirty_log_start: begin dirty logging for all regions 923 */ 924 void memory_global_dirty_log_start(void); 925 926 /** 927 * memory_global_dirty_log_stop: end dirty logging for all regions 928 */ 929 void memory_global_dirty_log_stop(void); 930 931 void mtree_info(fprintf_function mon_printf, void *f); 932 933 /** 934 * address_space_init: initializes an address space 935 * 936 * @as: an uninitialized #AddressSpace 937 * @root: a #MemoryRegion that routes addesses for the address space 938 * @name: an address space name. The name is only used for debugging 939 * output. 940 */ 941 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name); 942 943 944 /** 945 * address_space_destroy: destroy an address space 946 * 947 * Releases all resources associated with an address space. After an address space 948 * is destroyed, its root memory region (given by address_space_init()) may be destroyed 949 * as well. 950 * 951 * @as: address space to be destroyed 952 */ 953 void address_space_destroy(AddressSpace *as); 954 955 /** 956 * address_space_rw: read from or write to an address space. 957 * 958 * Return true if the operation hit any unassigned memory or encountered an 959 * IOMMU fault. 960 * 961 * @as: #AddressSpace to be accessed 962 * @addr: address within that address space 963 * @buf: buffer with the data transferred 964 * @is_write: indicates the transfer direction 965 */ 966 bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf, 967 int len, bool is_write); 968 969 /** 970 * address_space_write: write to address space. 971 * 972 * Return true if the operation hit any unassigned memory or encountered an 973 * IOMMU fault. 974 * 975 * @as: #AddressSpace to be accessed 976 * @addr: address within that address space 977 * @buf: buffer with the data transferred 978 */ 979 bool address_space_write(AddressSpace *as, hwaddr addr, 980 const uint8_t *buf, int len); 981 982 /** 983 * address_space_read: read from an address space. 984 * 985 * Return true if the operation hit any unassigned memory or encountered an 986 * IOMMU fault. 987 * 988 * @as: #AddressSpace to be accessed 989 * @addr: address within that address space 990 * @buf: buffer with the data transferred 991 */ 992 bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len); 993 994 /* address_space_translate: translate an address range into an address space 995 * into a MemoryRegion and an address range into that section 996 * 997 * @as: #AddressSpace to be accessed 998 * @addr: address within that address space 999 * @xlat: pointer to address within the returned memory region section's 1000 * #MemoryRegion. 1001 * @len: pointer to length 1002 * @is_write: indicates the transfer direction 1003 */ 1004 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr, 1005 hwaddr *xlat, hwaddr *len, 1006 bool is_write); 1007 1008 /* address_space_access_valid: check for validity of accessing an address 1009 * space range 1010 * 1011 * Check whether memory is assigned to the given address space range, and 1012 * access is permitted by any IOMMU regions that are active for the address 1013 * space. 1014 * 1015 * For now, addr and len should be aligned to a page size. This limitation 1016 * will be lifted in the future. 1017 * 1018 * @as: #AddressSpace to be accessed 1019 * @addr: address within that address space 1020 * @len: length of the area to be checked 1021 * @is_write: indicates the transfer direction 1022 */ 1023 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write); 1024 1025 /* address_space_map: map a physical memory region into a host virtual address 1026 * 1027 * May map a subset of the requested range, given by and returned in @plen. 1028 * May return %NULL if resources needed to perform the mapping are exhausted. 1029 * Use only for reads OR writes - not for read-modify-write operations. 1030 * Use cpu_register_map_client() to know when retrying the map operation is 1031 * likely to succeed. 1032 * 1033 * @as: #AddressSpace to be accessed 1034 * @addr: address within that address space 1035 * @plen: pointer to length of buffer; updated on return 1036 * @is_write: indicates the transfer direction 1037 */ 1038 void *address_space_map(AddressSpace *as, hwaddr addr, 1039 hwaddr *plen, bool is_write); 1040 1041 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map() 1042 * 1043 * Will also mark the memory as dirty if @is_write == %true. @access_len gives 1044 * the amount of memory that was actually read or written by the caller. 1045 * 1046 * @as: #AddressSpace used 1047 * @addr: address within that address space 1048 * @len: buffer length as returned by address_space_map() 1049 * @access_len: amount of data actually transferred 1050 * @is_write: indicates the transfer direction 1051 */ 1052 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, 1053 int is_write, hwaddr access_len); 1054 1055 1056 #endif 1057 1058 #endif 1059