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 #define DIRTY_MEMORY_VGA 0 20 #define DIRTY_MEMORY_CODE 1 21 #define DIRTY_MEMORY_MIGRATION 2 22 #define DIRTY_MEMORY_NUM 3 /* num of dirty bits */ 23 24 #include "exec/cpu-common.h" 25 #ifndef CONFIG_USER_ONLY 26 #include "exec/hwaddr.h" 27 #endif 28 #include "exec/memattrs.h" 29 #include "qemu/queue.h" 30 #include "qemu/int128.h" 31 #include "qemu/notify.h" 32 #include "qom/object.h" 33 #include "qemu/rcu.h" 34 35 #define RAM_ADDR_INVALID (~(ram_addr_t)0) 36 37 #define MAX_PHYS_ADDR_SPACE_BITS 62 38 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1) 39 40 #define TYPE_MEMORY_REGION "qemu:memory-region" 41 #define MEMORY_REGION(obj) \ 42 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION) 43 44 typedef struct MemoryRegionOps MemoryRegionOps; 45 typedef struct MemoryRegionMmio MemoryRegionMmio; 46 47 struct MemoryRegionMmio { 48 CPUReadMemoryFunc *read[3]; 49 CPUWriteMemoryFunc *write[3]; 50 }; 51 52 typedef struct IOMMUTLBEntry IOMMUTLBEntry; 53 54 /* See address_space_translate: bit 0 is read, bit 1 is write. */ 55 typedef enum { 56 IOMMU_NONE = 0, 57 IOMMU_RO = 1, 58 IOMMU_WO = 2, 59 IOMMU_RW = 3, 60 } IOMMUAccessFlags; 61 62 struct IOMMUTLBEntry { 63 AddressSpace *target_as; 64 hwaddr iova; 65 hwaddr translated_addr; 66 hwaddr addr_mask; /* 0xfff = 4k translation */ 67 IOMMUAccessFlags perm; 68 }; 69 70 /* 71 * Bitmap for different IOMMUNotifier capabilities. Each notifier can 72 * register with one or multiple IOMMU Notifier capability bit(s). 73 */ 74 typedef enum { 75 IOMMU_NOTIFIER_NONE = 0, 76 /* Notify cache invalidations */ 77 IOMMU_NOTIFIER_UNMAP = 0x1, 78 /* Notify entry changes (newly created entries) */ 79 IOMMU_NOTIFIER_MAP = 0x2, 80 } IOMMUNotifierFlag; 81 82 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP) 83 84 struct IOMMUNotifier { 85 void (*notify)(struct IOMMUNotifier *notifier, IOMMUTLBEntry *data); 86 IOMMUNotifierFlag notifier_flags; 87 QLIST_ENTRY(IOMMUNotifier) node; 88 }; 89 typedef struct IOMMUNotifier IOMMUNotifier; 90 91 /* New-style MMIO accessors can indicate that the transaction failed. 92 * A zero (MEMTX_OK) response means success; anything else is a failure 93 * of some kind. The memory subsystem will bitwise-OR together results 94 * if it is synthesizing an operation from multiple smaller accesses. 95 */ 96 #define MEMTX_OK 0 97 #define MEMTX_ERROR (1U << 0) /* device returned an error */ 98 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */ 99 typedef uint32_t MemTxResult; 100 101 /* 102 * Memory region callbacks 103 */ 104 struct MemoryRegionOps { 105 /* Read from the memory region. @addr is relative to @mr; @size is 106 * in bytes. */ 107 uint64_t (*read)(void *opaque, 108 hwaddr addr, 109 unsigned size); 110 /* Write to the memory region. @addr is relative to @mr; @size is 111 * in bytes. */ 112 void (*write)(void *opaque, 113 hwaddr addr, 114 uint64_t data, 115 unsigned size); 116 117 MemTxResult (*read_with_attrs)(void *opaque, 118 hwaddr addr, 119 uint64_t *data, 120 unsigned size, 121 MemTxAttrs attrs); 122 MemTxResult (*write_with_attrs)(void *opaque, 123 hwaddr addr, 124 uint64_t data, 125 unsigned size, 126 MemTxAttrs attrs); 127 128 enum device_endian endianness; 129 /* Guest-visible constraints: */ 130 struct { 131 /* If nonzero, specify bounds on access sizes beyond which a machine 132 * check is thrown. 133 */ 134 unsigned min_access_size; 135 unsigned max_access_size; 136 /* If true, unaligned accesses are supported. Otherwise unaligned 137 * accesses throw machine checks. 138 */ 139 bool unaligned; 140 /* 141 * If present, and returns #false, the transaction is not accepted 142 * by the device (and results in machine dependent behaviour such 143 * as a machine check exception). 144 */ 145 bool (*accepts)(void *opaque, hwaddr addr, 146 unsigned size, bool is_write); 147 } valid; 148 /* Internal implementation constraints: */ 149 struct { 150 /* If nonzero, specifies the minimum size implemented. Smaller sizes 151 * will be rounded upwards and a partial result will be returned. 152 */ 153 unsigned min_access_size; 154 /* If nonzero, specifies the maximum size implemented. Larger sizes 155 * will be done as a series of accesses with smaller sizes. 156 */ 157 unsigned max_access_size; 158 /* If true, unaligned accesses are supported. Otherwise all accesses 159 * are converted to (possibly multiple) naturally aligned accesses. 160 */ 161 bool unaligned; 162 } impl; 163 164 /* If .read and .write are not present, old_mmio may be used for 165 * backwards compatibility with old mmio registration 166 */ 167 const MemoryRegionMmio old_mmio; 168 }; 169 170 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps; 171 172 struct MemoryRegionIOMMUOps { 173 /* Return a TLB entry that contains a given address. */ 174 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write); 175 /* Returns minimum supported page size */ 176 uint64_t (*get_min_page_size)(MemoryRegion *iommu); 177 /* Called when IOMMU Notifier flag changed */ 178 void (*notify_flag_changed)(MemoryRegion *iommu, 179 IOMMUNotifierFlag old_flags, 180 IOMMUNotifierFlag new_flags); 181 }; 182 183 typedef struct CoalescedMemoryRange CoalescedMemoryRange; 184 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; 185 186 struct MemoryRegion { 187 Object parent_obj; 188 189 /* All fields are private - violators will be prosecuted */ 190 191 /* The following fields should fit in a cache line */ 192 bool romd_mode; 193 bool ram; 194 bool subpage; 195 bool readonly; /* For RAM regions */ 196 bool rom_device; 197 bool flush_coalesced_mmio; 198 bool global_locking; 199 uint8_t dirty_log_mask; 200 RAMBlock *ram_block; 201 Object *owner; 202 const MemoryRegionIOMMUOps *iommu_ops; 203 204 const MemoryRegionOps *ops; 205 void *opaque; 206 MemoryRegion *container; 207 Int128 size; 208 hwaddr addr; 209 void (*destructor)(MemoryRegion *mr); 210 uint64_t align; 211 bool terminates; 212 bool skip_dump; 213 bool enabled; 214 bool warning_printed; /* For reservations */ 215 uint8_t vga_logging_count; 216 MemoryRegion *alias; 217 hwaddr alias_offset; 218 int32_t priority; 219 QTAILQ_HEAD(subregions, MemoryRegion) subregions; 220 QTAILQ_ENTRY(MemoryRegion) subregions_link; 221 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced; 222 const char *name; 223 unsigned ioeventfd_nb; 224 MemoryRegionIoeventfd *ioeventfds; 225 QLIST_HEAD(, IOMMUNotifier) iommu_notify; 226 IOMMUNotifierFlag iommu_notify_flags; 227 }; 228 229 /** 230 * MemoryListener: callbacks structure for updates to the physical memory map 231 * 232 * Allows a component to adjust to changes in the guest-visible memory map. 233 * Use with memory_listener_register() and memory_listener_unregister(). 234 */ 235 struct MemoryListener { 236 void (*begin)(MemoryListener *listener); 237 void (*commit)(MemoryListener *listener); 238 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section); 239 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section); 240 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section); 241 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section, 242 int old, int new); 243 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section, 244 int old, int new); 245 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section); 246 void (*log_global_start)(MemoryListener *listener); 247 void (*log_global_stop)(MemoryListener *listener); 248 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section, 249 bool match_data, uint64_t data, EventNotifier *e); 250 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section, 251 bool match_data, uint64_t data, EventNotifier *e); 252 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section, 253 hwaddr addr, hwaddr len); 254 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section, 255 hwaddr addr, hwaddr len); 256 /* Lower = earlier (during add), later (during del) */ 257 unsigned priority; 258 AddressSpace *address_space_filter; 259 QTAILQ_ENTRY(MemoryListener) link; 260 }; 261 262 /** 263 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects 264 */ 265 struct AddressSpace { 266 /* All fields are private. */ 267 struct rcu_head rcu; 268 char *name; 269 MemoryRegion *root; 270 int ref_count; 271 bool malloced; 272 273 /* Accessed via RCU. */ 274 struct FlatView *current_map; 275 276 int ioeventfd_nb; 277 struct MemoryRegionIoeventfd *ioeventfds; 278 struct AddressSpaceDispatch *dispatch; 279 struct AddressSpaceDispatch *next_dispatch; 280 MemoryListener dispatch_listener; 281 282 QTAILQ_ENTRY(AddressSpace) address_spaces_link; 283 }; 284 285 /** 286 * MemoryRegionSection: describes a fragment of a #MemoryRegion 287 * 288 * @mr: the region, or %NULL if empty 289 * @address_space: the address space the region is mapped in 290 * @offset_within_region: the beginning of the section, relative to @mr's start 291 * @size: the size of the section; will not exceed @mr's boundaries 292 * @offset_within_address_space: the address of the first byte of the section 293 * relative to the region's address space 294 * @readonly: writes to this section are ignored 295 */ 296 struct MemoryRegionSection { 297 MemoryRegion *mr; 298 AddressSpace *address_space; 299 hwaddr offset_within_region; 300 Int128 size; 301 hwaddr offset_within_address_space; 302 bool readonly; 303 }; 304 305 /** 306 * memory_region_init: Initialize a memory region 307 * 308 * The region typically acts as a container for other memory regions. Use 309 * memory_region_add_subregion() to add subregions. 310 * 311 * @mr: the #MemoryRegion to be initialized 312 * @owner: the object that tracks the region's reference count 313 * @name: used for debugging; not visible to the user or ABI 314 * @size: size of the region; any subregions beyond this size will be clipped 315 */ 316 void memory_region_init(MemoryRegion *mr, 317 struct Object *owner, 318 const char *name, 319 uint64_t size); 320 321 /** 322 * memory_region_ref: Add 1 to a memory region's reference count 323 * 324 * Whenever memory regions are accessed outside the BQL, they need to be 325 * preserved against hot-unplug. MemoryRegions actually do not have their 326 * own reference count; they piggyback on a QOM object, their "owner". 327 * This function adds a reference to the owner. 328 * 329 * All MemoryRegions must have an owner if they can disappear, even if the 330 * device they belong to operates exclusively under the BQL. This is because 331 * the region could be returned at any time by memory_region_find, and this 332 * is usually under guest control. 333 * 334 * @mr: the #MemoryRegion 335 */ 336 void memory_region_ref(MemoryRegion *mr); 337 338 /** 339 * memory_region_unref: Remove 1 to a memory region's reference count 340 * 341 * Whenever memory regions are accessed outside the BQL, they need to be 342 * preserved against hot-unplug. MemoryRegions actually do not have their 343 * own reference count; they piggyback on a QOM object, their "owner". 344 * This function removes a reference to the owner and possibly destroys it. 345 * 346 * @mr: the #MemoryRegion 347 */ 348 void memory_region_unref(MemoryRegion *mr); 349 350 /** 351 * memory_region_init_io: Initialize an I/O memory region. 352 * 353 * Accesses into the region will cause the callbacks in @ops to be called. 354 * if @size is nonzero, subregions will be clipped to @size. 355 * 356 * @mr: the #MemoryRegion to be initialized. 357 * @owner: the object that tracks the region's reference count 358 * @ops: a structure containing read and write callbacks to be used when 359 * I/O is performed on the region. 360 * @opaque: passed to the read and write callbacks of the @ops structure. 361 * @name: used for debugging; not visible to the user or ABI 362 * @size: size of the region. 363 */ 364 void memory_region_init_io(MemoryRegion *mr, 365 struct Object *owner, 366 const MemoryRegionOps *ops, 367 void *opaque, 368 const char *name, 369 uint64_t size); 370 371 /** 372 * memory_region_init_ram: Initialize RAM memory region. Accesses into the 373 * region will modify memory directly. 374 * 375 * @mr: the #MemoryRegion to be initialized. 376 * @owner: the object that tracks the region's reference count 377 * @name: the name of the region. 378 * @size: size of the region. 379 * @errp: pointer to Error*, to store an error if it happens. 380 */ 381 void memory_region_init_ram(MemoryRegion *mr, 382 struct Object *owner, 383 const char *name, 384 uint64_t size, 385 Error **errp); 386 387 /** 388 * memory_region_init_resizeable_ram: Initialize memory region with resizeable 389 * RAM. Accesses into the region will 390 * modify memory directly. Only an initial 391 * portion of this RAM is actually used. 392 * The used size can change across reboots. 393 * 394 * @mr: the #MemoryRegion to be initialized. 395 * @owner: the object that tracks the region's reference count 396 * @name: the name of the region. 397 * @size: used size of the region. 398 * @max_size: max size of the region. 399 * @resized: callback to notify owner about used size change. 400 * @errp: pointer to Error*, to store an error if it happens. 401 */ 402 void memory_region_init_resizeable_ram(MemoryRegion *mr, 403 struct Object *owner, 404 const char *name, 405 uint64_t size, 406 uint64_t max_size, 407 void (*resized)(const char*, 408 uint64_t length, 409 void *host), 410 Error **errp); 411 #ifdef __linux__ 412 /** 413 * memory_region_init_ram_from_file: Initialize RAM memory region with a 414 * mmap-ed backend. 415 * 416 * @mr: the #MemoryRegion to be initialized. 417 * @owner: the object that tracks the region's reference count 418 * @name: the name of the region. 419 * @size: size of the region. 420 * @share: %true if memory must be mmaped with the MAP_SHARED flag 421 * @path: the path in which to allocate the RAM. 422 * @errp: pointer to Error*, to store an error if it happens. 423 */ 424 void memory_region_init_ram_from_file(MemoryRegion *mr, 425 struct Object *owner, 426 const char *name, 427 uint64_t size, 428 bool share, 429 const char *path, 430 Error **errp); 431 #endif 432 433 /** 434 * memory_region_init_ram_ptr: Initialize RAM memory region from a 435 * user-provided pointer. Accesses into the 436 * region will modify memory directly. 437 * 438 * @mr: the #MemoryRegion to be initialized. 439 * @owner: the object that tracks the region's reference count 440 * @name: the name of the region. 441 * @size: size of the region. 442 * @ptr: memory to be mapped; must contain at least @size bytes. 443 */ 444 void memory_region_init_ram_ptr(MemoryRegion *mr, 445 struct Object *owner, 446 const char *name, 447 uint64_t size, 448 void *ptr); 449 450 /** 451 * memory_region_init_alias: Initialize a memory region that aliases all or a 452 * part of another memory region. 453 * 454 * @mr: the #MemoryRegion to be initialized. 455 * @owner: the object that tracks the region's reference count 456 * @name: used for debugging; not visible to the user or ABI 457 * @orig: the region to be referenced; @mr will be equivalent to 458 * @orig between @offset and @offset + @size - 1. 459 * @offset: start of the section in @orig to be referenced. 460 * @size: size of the region. 461 */ 462 void memory_region_init_alias(MemoryRegion *mr, 463 struct Object *owner, 464 const char *name, 465 MemoryRegion *orig, 466 hwaddr offset, 467 uint64_t size); 468 469 /** 470 * memory_region_init_rom: Initialize a ROM memory region. 471 * 472 * This has the same effect as calling memory_region_init_ram() 473 * and then marking the resulting region read-only with 474 * memory_region_set_readonly(). 475 * 476 * @mr: the #MemoryRegion to be initialized. 477 * @owner: the object that tracks the region's reference count 478 * @name: the name of the region. 479 * @size: size of the region. 480 * @errp: pointer to Error*, to store an error if it happens. 481 */ 482 void memory_region_init_rom(MemoryRegion *mr, 483 struct Object *owner, 484 const char *name, 485 uint64_t size, 486 Error **errp); 487 488 /** 489 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are 490 * handled via callbacks. 491 * 492 * @mr: the #MemoryRegion to be initialized. 493 * @owner: the object that tracks the region's reference count 494 * @ops: callbacks for write access handling (must not be NULL). 495 * @name: the name of the region. 496 * @size: size of the region. 497 * @errp: pointer to Error*, to store an error if it happens. 498 */ 499 void memory_region_init_rom_device(MemoryRegion *mr, 500 struct Object *owner, 501 const MemoryRegionOps *ops, 502 void *opaque, 503 const char *name, 504 uint64_t size, 505 Error **errp); 506 507 /** 508 * memory_region_init_reservation: Initialize a memory region that reserves 509 * I/O space. 510 * 511 * A reservation region primariy serves debugging purposes. It claims I/O 512 * space that is not supposed to be handled by QEMU itself. Any access via 513 * the memory API will cause an abort(). 514 * This function is deprecated. Use memory_region_init_io() with NULL 515 * callbacks instead. 516 * 517 * @mr: the #MemoryRegion to be initialized 518 * @owner: the object that tracks the region's reference count 519 * @name: used for debugging; not visible to the user or ABI 520 * @size: size of the region. 521 */ 522 static inline void memory_region_init_reservation(MemoryRegion *mr, 523 Object *owner, 524 const char *name, 525 uint64_t size) 526 { 527 memory_region_init_io(mr, owner, NULL, mr, name, size); 528 } 529 530 /** 531 * memory_region_init_iommu: Initialize a memory region that translates 532 * addresses 533 * 534 * An IOMMU region translates addresses and forwards accesses to a target 535 * memory region. 536 * 537 * @mr: the #MemoryRegion to be initialized 538 * @owner: the object that tracks the region's reference count 539 * @ops: a function that translates addresses into the @target region 540 * @name: used for debugging; not visible to the user or ABI 541 * @size: size of the region. 542 */ 543 void memory_region_init_iommu(MemoryRegion *mr, 544 struct Object *owner, 545 const MemoryRegionIOMMUOps *ops, 546 const char *name, 547 uint64_t size); 548 549 /** 550 * memory_region_owner: get a memory region's owner. 551 * 552 * @mr: the memory region being queried. 553 */ 554 struct Object *memory_region_owner(MemoryRegion *mr); 555 556 /** 557 * memory_region_size: get a memory region's size. 558 * 559 * @mr: the memory region being queried. 560 */ 561 uint64_t memory_region_size(MemoryRegion *mr); 562 563 /** 564 * memory_region_is_ram: check whether a memory region is random access 565 * 566 * Returns %true is a memory region is random access. 567 * 568 * @mr: the memory region being queried 569 */ 570 static inline bool memory_region_is_ram(MemoryRegion *mr) 571 { 572 return mr->ram; 573 } 574 575 /** 576 * memory_region_is_skip_dump: check whether a memory region should not be 577 * dumped 578 * 579 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP). 580 * 581 * @mr: the memory region being queried 582 */ 583 bool memory_region_is_skip_dump(MemoryRegion *mr); 584 585 /** 586 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory 587 * region 588 * 589 * @mr: the memory region being queried 590 */ 591 void memory_region_set_skip_dump(MemoryRegion *mr); 592 593 /** 594 * memory_region_is_romd: check whether a memory region is in ROMD mode 595 * 596 * Returns %true if a memory region is a ROM device and currently set to allow 597 * direct reads. 598 * 599 * @mr: the memory region being queried 600 */ 601 static inline bool memory_region_is_romd(MemoryRegion *mr) 602 { 603 return mr->rom_device && mr->romd_mode; 604 } 605 606 /** 607 * memory_region_is_iommu: check whether a memory region is an iommu 608 * 609 * Returns %true is a memory region is an iommu. 610 * 611 * @mr: the memory region being queried 612 */ 613 static inline bool memory_region_is_iommu(MemoryRegion *mr) 614 { 615 return mr->iommu_ops; 616 } 617 618 619 /** 620 * memory_region_iommu_get_min_page_size: get minimum supported page size 621 * for an iommu 622 * 623 * Returns minimum supported page size for an iommu. 624 * 625 * @mr: the memory region being queried 626 */ 627 uint64_t memory_region_iommu_get_min_page_size(MemoryRegion *mr); 628 629 /** 630 * memory_region_notify_iommu: notify a change in an IOMMU translation entry. 631 * 632 * The notification type will be decided by entry.perm bits: 633 * 634 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE. 635 * - For MAP (newly added entry) notifies: set entry.perm to the 636 * permission of the page (which is definitely !IOMMU_NONE). 637 * 638 * Note: for any IOMMU implementation, an in-place mapping change 639 * should be notified with an UNMAP followed by a MAP. 640 * 641 * @mr: the memory region that was changed 642 * @entry: the new entry in the IOMMU translation table. The entry 643 * replaces all old entries for the same virtual I/O address range. 644 * Deleted entries have .@perm == 0. 645 */ 646 void memory_region_notify_iommu(MemoryRegion *mr, 647 IOMMUTLBEntry entry); 648 649 /** 650 * memory_region_register_iommu_notifier: register a notifier for changes to 651 * IOMMU translation entries. 652 * 653 * @mr: the memory region to observe 654 * @n: the IOMMUNotifier to be added; the notify callback receives a 655 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer 656 * ceases to be valid on exit from the notifier. 657 */ 658 void memory_region_register_iommu_notifier(MemoryRegion *mr, 659 IOMMUNotifier *n); 660 661 /** 662 * memory_region_iommu_replay: replay existing IOMMU translations to 663 * a notifier with the minimum page granularity returned by 664 * mr->iommu_ops->get_page_size(). 665 * 666 * @mr: the memory region to observe 667 * @n: the notifier to which to replay iommu mappings 668 * @is_write: Whether to treat the replay as a translate "write" 669 * through the iommu 670 */ 671 void memory_region_iommu_replay(MemoryRegion *mr, IOMMUNotifier *n, 672 bool is_write); 673 674 /** 675 * memory_region_unregister_iommu_notifier: unregister a notifier for 676 * changes to IOMMU translation entries. 677 * 678 * @mr: the memory region which was observed and for which notity_stopped() 679 * needs to be called 680 * @n: the notifier to be removed. 681 */ 682 void memory_region_unregister_iommu_notifier(MemoryRegion *mr, 683 IOMMUNotifier *n); 684 685 /** 686 * memory_region_name: get a memory region's name 687 * 688 * Returns the string that was used to initialize the memory region. 689 * 690 * @mr: the memory region being queried 691 */ 692 const char *memory_region_name(const MemoryRegion *mr); 693 694 /** 695 * memory_region_is_logging: return whether a memory region is logging writes 696 * 697 * Returns %true if the memory region is logging writes for the given client 698 * 699 * @mr: the memory region being queried 700 * @client: the client being queried 701 */ 702 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client); 703 704 /** 705 * memory_region_get_dirty_log_mask: return the clients for which a 706 * memory region is logging writes. 707 * 708 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants 709 * are the bit indices. 710 * 711 * @mr: the memory region being queried 712 */ 713 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr); 714 715 /** 716 * memory_region_is_rom: check whether a memory region is ROM 717 * 718 * Returns %true is a memory region is read-only memory. 719 * 720 * @mr: the memory region being queried 721 */ 722 static inline bool memory_region_is_rom(MemoryRegion *mr) 723 { 724 return mr->ram && mr->readonly; 725 } 726 727 728 /** 729 * memory_region_get_fd: Get a file descriptor backing a RAM memory region. 730 * 731 * Returns a file descriptor backing a file-based RAM memory region, 732 * or -1 if the region is not a file-based RAM memory region. 733 * 734 * @mr: the RAM or alias memory region being queried. 735 */ 736 int memory_region_get_fd(MemoryRegion *mr); 737 738 /** 739 * memory_region_set_fd: Mark a RAM memory region as backed by a 740 * file descriptor. 741 * 742 * This function is typically used after memory_region_init_ram_ptr(). 743 * 744 * @mr: the memory region being queried. 745 * @fd: the file descriptor that backs @mr. 746 */ 747 void memory_region_set_fd(MemoryRegion *mr, int fd); 748 749 /** 750 * memory_region_from_host: Convert a pointer into a RAM memory region 751 * and an offset within it. 752 * 753 * Given a host pointer inside a RAM memory region (created with 754 * memory_region_init_ram() or memory_region_init_ram_ptr()), return 755 * the MemoryRegion and the offset within it. 756 * 757 * Use with care; by the time this function returns, the returned pointer is 758 * not protected by RCU anymore. If the caller is not within an RCU critical 759 * section and does not hold the iothread lock, it must have other means of 760 * protecting the pointer, such as a reference to the region that includes 761 * the incoming ram_addr_t. 762 * 763 * @mr: the memory region being queried. 764 */ 765 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset); 766 767 /** 768 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region. 769 * 770 * Returns a host pointer to a RAM memory region (created with 771 * memory_region_init_ram() or memory_region_init_ram_ptr()). 772 * 773 * Use with care; by the time this function returns, the returned pointer is 774 * not protected by RCU anymore. If the caller is not within an RCU critical 775 * section and does not hold the iothread lock, it must have other means of 776 * protecting the pointer, such as a reference to the region that includes 777 * the incoming ram_addr_t. 778 * 779 * @mr: the memory region being queried. 780 */ 781 void *memory_region_get_ram_ptr(MemoryRegion *mr); 782 783 /* memory_region_ram_resize: Resize a RAM region. 784 * 785 * Only legal before guest might have detected the memory size: e.g. on 786 * incoming migration, or right after reset. 787 * 788 * @mr: a memory region created with @memory_region_init_resizeable_ram. 789 * @newsize: the new size the region 790 * @errp: pointer to Error*, to store an error if it happens. 791 */ 792 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, 793 Error **errp); 794 795 /** 796 * memory_region_set_log: Turn dirty logging on or off for a region. 797 * 798 * Turns dirty logging on or off for a specified client (display, migration). 799 * Only meaningful for RAM regions. 800 * 801 * @mr: the memory region being updated. 802 * @log: whether dirty logging is to be enabled or disabled. 803 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only. 804 */ 805 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client); 806 807 /** 808 * memory_region_get_dirty: Check whether a range of bytes is dirty 809 * for a specified client. 810 * 811 * Checks whether a range of bytes has been written to since the last 812 * call to memory_region_reset_dirty() with the same @client. Dirty logging 813 * must be enabled. 814 * 815 * @mr: the memory region being queried. 816 * @addr: the address (relative to the start of the region) being queried. 817 * @size: the size of the range being queried. 818 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 819 * %DIRTY_MEMORY_VGA. 820 */ 821 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr, 822 hwaddr size, unsigned client); 823 824 /** 825 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region. 826 * 827 * Marks a range of bytes as dirty, after it has been dirtied outside 828 * guest code. 829 * 830 * @mr: the memory region being dirtied. 831 * @addr: the address (relative to the start of the region) being dirtied. 832 * @size: size of the range being dirtied. 833 */ 834 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, 835 hwaddr size); 836 837 /** 838 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty 839 * for a specified client. It clears them. 840 * 841 * Checks whether a range of bytes has been written to since the last 842 * call to memory_region_reset_dirty() with the same @client. Dirty logging 843 * must be enabled. 844 * 845 * @mr: the memory region being queried. 846 * @addr: the address (relative to the start of the region) being queried. 847 * @size: the size of the range being queried. 848 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 849 * %DIRTY_MEMORY_VGA. 850 */ 851 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr, 852 hwaddr size, unsigned client); 853 /** 854 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with 855 * any external TLBs (e.g. kvm) 856 * 857 * Flushes dirty information from accelerators such as kvm and vhost-net 858 * and makes it available to users of the memory API. 859 * 860 * @mr: the region being flushed. 861 */ 862 void memory_region_sync_dirty_bitmap(MemoryRegion *mr); 863 864 /** 865 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified 866 * client. 867 * 868 * Marks a range of pages as no longer dirty. 869 * 870 * @mr: the region being updated. 871 * @addr: the start of the subrange being cleaned. 872 * @size: the size of the subrange being cleaned. 873 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 874 * %DIRTY_MEMORY_VGA. 875 */ 876 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr, 877 hwaddr size, unsigned client); 878 879 /** 880 * memory_region_set_readonly: Turn a memory region read-only (or read-write) 881 * 882 * Allows a memory region to be marked as read-only (turning it into a ROM). 883 * only useful on RAM regions. 884 * 885 * @mr: the region being updated. 886 * @readonly: whether rhe region is to be ROM or RAM. 887 */ 888 void memory_region_set_readonly(MemoryRegion *mr, bool readonly); 889 890 /** 891 * memory_region_rom_device_set_romd: enable/disable ROMD mode 892 * 893 * Allows a ROM device (initialized with memory_region_init_rom_device() to 894 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the 895 * device is mapped to guest memory and satisfies read access directly. 896 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function. 897 * Writes are always handled by the #MemoryRegion.write function. 898 * 899 * @mr: the memory region to be updated 900 * @romd_mode: %true to put the region into ROMD mode 901 */ 902 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode); 903 904 /** 905 * memory_region_set_coalescing: Enable memory coalescing for the region. 906 * 907 * Enabled writes to a region to be queued for later processing. MMIO ->write 908 * callbacks may be delayed until a non-coalesced MMIO is issued. 909 * Only useful for IO regions. Roughly similar to write-combining hardware. 910 * 911 * @mr: the memory region to be write coalesced 912 */ 913 void memory_region_set_coalescing(MemoryRegion *mr); 914 915 /** 916 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of 917 * a region. 918 * 919 * Like memory_region_set_coalescing(), but works on a sub-range of a region. 920 * Multiple calls can be issued coalesced disjoint ranges. 921 * 922 * @mr: the memory region to be updated. 923 * @offset: the start of the range within the region to be coalesced. 924 * @size: the size of the subrange to be coalesced. 925 */ 926 void memory_region_add_coalescing(MemoryRegion *mr, 927 hwaddr offset, 928 uint64_t size); 929 930 /** 931 * memory_region_clear_coalescing: Disable MMIO coalescing for the region. 932 * 933 * Disables any coalescing caused by memory_region_set_coalescing() or 934 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory 935 * hardware. 936 * 937 * @mr: the memory region to be updated. 938 */ 939 void memory_region_clear_coalescing(MemoryRegion *mr); 940 941 /** 942 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before 943 * accesses. 944 * 945 * Ensure that pending coalesced MMIO request are flushed before the memory 946 * region is accessed. This property is automatically enabled for all regions 947 * passed to memory_region_set_coalescing() and memory_region_add_coalescing(). 948 * 949 * @mr: the memory region to be updated. 950 */ 951 void memory_region_set_flush_coalesced(MemoryRegion *mr); 952 953 /** 954 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before 955 * accesses. 956 * 957 * Clear the automatic coalesced MMIO flushing enabled via 958 * memory_region_set_flush_coalesced. Note that this service has no effect on 959 * memory regions that have MMIO coalescing enabled for themselves. For them, 960 * automatic flushing will stop once coalescing is disabled. 961 * 962 * @mr: the memory region to be updated. 963 */ 964 void memory_region_clear_flush_coalesced(MemoryRegion *mr); 965 966 /** 967 * memory_region_set_global_locking: Declares the access processing requires 968 * QEMU's global lock. 969 * 970 * When this is invoked, accesses to the memory region will be processed while 971 * holding the global lock of QEMU. This is the default behavior of memory 972 * regions. 973 * 974 * @mr: the memory region to be updated. 975 */ 976 void memory_region_set_global_locking(MemoryRegion *mr); 977 978 /** 979 * memory_region_clear_global_locking: Declares that access processing does 980 * not depend on the QEMU global lock. 981 * 982 * By clearing this property, accesses to the memory region will be processed 983 * outside of QEMU's global lock (unless the lock is held on when issuing the 984 * access request). In this case, the device model implementing the access 985 * handlers is responsible for synchronization of concurrency. 986 * 987 * @mr: the memory region to be updated. 988 */ 989 void memory_region_clear_global_locking(MemoryRegion *mr); 990 991 /** 992 * memory_region_add_eventfd: Request an eventfd to be triggered when a word 993 * is written to a location. 994 * 995 * Marks a word in an IO region (initialized with memory_region_init_io()) 996 * as a trigger for an eventfd event. The I/O callback will not be called. 997 * The caller must be prepared to handle failure (that is, take the required 998 * action if the callback _is_ called). 999 * 1000 * @mr: the memory region being updated. 1001 * @addr: the address within @mr that is to be monitored 1002 * @size: the size of the access to trigger the eventfd 1003 * @match_data: whether to match against @data, instead of just @addr 1004 * @data: the data to match against the guest write 1005 * @fd: the eventfd to be triggered when @addr, @size, and @data all match. 1006 **/ 1007 void memory_region_add_eventfd(MemoryRegion *mr, 1008 hwaddr addr, 1009 unsigned size, 1010 bool match_data, 1011 uint64_t data, 1012 EventNotifier *e); 1013 1014 /** 1015 * memory_region_del_eventfd: Cancel an eventfd. 1016 * 1017 * Cancels an eventfd trigger requested by a previous 1018 * memory_region_add_eventfd() call. 1019 * 1020 * @mr: the memory region being updated. 1021 * @addr: the address within @mr that is to be monitored 1022 * @size: the size of the access to trigger the eventfd 1023 * @match_data: whether to match against @data, instead of just @addr 1024 * @data: the data to match against the guest write 1025 * @fd: the eventfd to be triggered when @addr, @size, and @data all match. 1026 */ 1027 void memory_region_del_eventfd(MemoryRegion *mr, 1028 hwaddr addr, 1029 unsigned size, 1030 bool match_data, 1031 uint64_t data, 1032 EventNotifier *e); 1033 1034 /** 1035 * memory_region_add_subregion: Add a subregion to a container. 1036 * 1037 * Adds a subregion at @offset. The subregion may not overlap with other 1038 * subregions (except for those explicitly marked as overlapping). A region 1039 * may only be added once as a subregion (unless removed with 1040 * memory_region_del_subregion()); use memory_region_init_alias() if you 1041 * want a region to be a subregion in multiple locations. 1042 * 1043 * @mr: the region to contain the new subregion; must be a container 1044 * initialized with memory_region_init(). 1045 * @offset: the offset relative to @mr where @subregion is added. 1046 * @subregion: the subregion to be added. 1047 */ 1048 void memory_region_add_subregion(MemoryRegion *mr, 1049 hwaddr offset, 1050 MemoryRegion *subregion); 1051 /** 1052 * memory_region_add_subregion_overlap: Add a subregion to a container 1053 * with overlap. 1054 * 1055 * Adds a subregion at @offset. The subregion may overlap with other 1056 * subregions. Conflicts are resolved by having a higher @priority hide a 1057 * lower @priority. Subregions without priority are taken as @priority 0. 1058 * A region may only be added once as a subregion (unless removed with 1059 * memory_region_del_subregion()); use memory_region_init_alias() if you 1060 * want a region to be a subregion in multiple locations. 1061 * 1062 * @mr: the region to contain the new subregion; must be a container 1063 * initialized with memory_region_init(). 1064 * @offset: the offset relative to @mr where @subregion is added. 1065 * @subregion: the subregion to be added. 1066 * @priority: used for resolving overlaps; highest priority wins. 1067 */ 1068 void memory_region_add_subregion_overlap(MemoryRegion *mr, 1069 hwaddr offset, 1070 MemoryRegion *subregion, 1071 int priority); 1072 1073 /** 1074 * memory_region_get_ram_addr: Get the ram address associated with a memory 1075 * region 1076 */ 1077 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr); 1078 1079 uint64_t memory_region_get_alignment(const MemoryRegion *mr); 1080 /** 1081 * memory_region_del_subregion: Remove a subregion. 1082 * 1083 * Removes a subregion from its container. 1084 * 1085 * @mr: the container to be updated. 1086 * @subregion: the region being removed; must be a current subregion of @mr. 1087 */ 1088 void memory_region_del_subregion(MemoryRegion *mr, 1089 MemoryRegion *subregion); 1090 1091 /* 1092 * memory_region_set_enabled: dynamically enable or disable a region 1093 * 1094 * Enables or disables a memory region. A disabled memory region 1095 * ignores all accesses to itself and its subregions. It does not 1096 * obscure sibling subregions with lower priority - it simply behaves as 1097 * if it was removed from the hierarchy. 1098 * 1099 * Regions default to being enabled. 1100 * 1101 * @mr: the region to be updated 1102 * @enabled: whether to enable or disable the region 1103 */ 1104 void memory_region_set_enabled(MemoryRegion *mr, bool enabled); 1105 1106 /* 1107 * memory_region_set_address: dynamically update the address of a region 1108 * 1109 * Dynamically updates the address of a region, relative to its container. 1110 * May be used on regions are currently part of a memory hierarchy. 1111 * 1112 * @mr: the region to be updated 1113 * @addr: new address, relative to container region 1114 */ 1115 void memory_region_set_address(MemoryRegion *mr, hwaddr addr); 1116 1117 /* 1118 * memory_region_set_size: dynamically update the size of a region. 1119 * 1120 * Dynamically updates the size of a region. 1121 * 1122 * @mr: the region to be updated 1123 * @size: used size of the region. 1124 */ 1125 void memory_region_set_size(MemoryRegion *mr, uint64_t size); 1126 1127 /* 1128 * memory_region_set_alias_offset: dynamically update a memory alias's offset 1129 * 1130 * Dynamically updates the offset into the target region that an alias points 1131 * to, as if the fourth argument to memory_region_init_alias() has changed. 1132 * 1133 * @mr: the #MemoryRegion to be updated; should be an alias. 1134 * @offset: the new offset into the target memory region 1135 */ 1136 void memory_region_set_alias_offset(MemoryRegion *mr, 1137 hwaddr offset); 1138 1139 /** 1140 * memory_region_present: checks if an address relative to a @container 1141 * translates into #MemoryRegion within @container 1142 * 1143 * Answer whether a #MemoryRegion within @container covers the address 1144 * @addr. 1145 * 1146 * @container: a #MemoryRegion within which @addr is a relative address 1147 * @addr: the area within @container to be searched 1148 */ 1149 bool memory_region_present(MemoryRegion *container, hwaddr addr); 1150 1151 /** 1152 * memory_region_is_mapped: returns true if #MemoryRegion is mapped 1153 * into any address space. 1154 * 1155 * @mr: a #MemoryRegion which should be checked if it's mapped 1156 */ 1157 bool memory_region_is_mapped(MemoryRegion *mr); 1158 1159 /** 1160 * memory_region_find: translate an address/size relative to a 1161 * MemoryRegion into a #MemoryRegionSection. 1162 * 1163 * Locates the first #MemoryRegion within @mr that overlaps the range 1164 * given by @addr and @size. 1165 * 1166 * Returns a #MemoryRegionSection that describes a contiguous overlap. 1167 * It will have the following characteristics: 1168 * .@size = 0 iff no overlap was found 1169 * .@mr is non-%NULL iff an overlap was found 1170 * 1171 * Remember that in the return value the @offset_within_region is 1172 * relative to the returned region (in the .@mr field), not to the 1173 * @mr argument. 1174 * 1175 * Similarly, the .@offset_within_address_space is relative to the 1176 * address space that contains both regions, the passed and the 1177 * returned one. However, in the special case where the @mr argument 1178 * has no container (and thus is the root of the address space), the 1179 * following will hold: 1180 * .@offset_within_address_space >= @addr 1181 * .@offset_within_address_space + .@size <= @addr + @size 1182 * 1183 * @mr: a MemoryRegion within which @addr is a relative address 1184 * @addr: start of the area within @as to be searched 1185 * @size: size of the area to be searched 1186 */ 1187 MemoryRegionSection memory_region_find(MemoryRegion *mr, 1188 hwaddr addr, uint64_t size); 1189 1190 /** 1191 * memory_global_dirty_log_sync: synchronize the dirty log for all memory 1192 * 1193 * Synchronizes the dirty page log for all address spaces. 1194 */ 1195 void memory_global_dirty_log_sync(void); 1196 1197 /** 1198 * memory_region_transaction_begin: Start a transaction. 1199 * 1200 * During a transaction, changes will be accumulated and made visible 1201 * only when the transaction ends (is committed). 1202 */ 1203 void memory_region_transaction_begin(void); 1204 1205 /** 1206 * memory_region_transaction_commit: Commit a transaction and make changes 1207 * visible to the guest. 1208 */ 1209 void memory_region_transaction_commit(void); 1210 1211 /** 1212 * memory_listener_register: register callbacks to be called when memory 1213 * sections are mapped or unmapped into an address 1214 * space 1215 * 1216 * @listener: an object containing the callbacks to be called 1217 * @filter: if non-%NULL, only regions in this address space will be observed 1218 */ 1219 void memory_listener_register(MemoryListener *listener, AddressSpace *filter); 1220 1221 /** 1222 * memory_listener_unregister: undo the effect of memory_listener_register() 1223 * 1224 * @listener: an object containing the callbacks to be removed 1225 */ 1226 void memory_listener_unregister(MemoryListener *listener); 1227 1228 /** 1229 * memory_global_dirty_log_start: begin dirty logging for all regions 1230 */ 1231 void memory_global_dirty_log_start(void); 1232 1233 /** 1234 * memory_global_dirty_log_stop: end dirty logging for all regions 1235 */ 1236 void memory_global_dirty_log_stop(void); 1237 1238 void mtree_info(fprintf_function mon_printf, void *f); 1239 1240 /** 1241 * memory_region_dispatch_read: perform a read directly to the specified 1242 * MemoryRegion. 1243 * 1244 * @mr: #MemoryRegion to access 1245 * @addr: address within that region 1246 * @pval: pointer to uint64_t which the data is written to 1247 * @size: size of the access in bytes 1248 * @attrs: memory transaction attributes to use for the access 1249 */ 1250 MemTxResult memory_region_dispatch_read(MemoryRegion *mr, 1251 hwaddr addr, 1252 uint64_t *pval, 1253 unsigned size, 1254 MemTxAttrs attrs); 1255 /** 1256 * memory_region_dispatch_write: perform a write directly to the specified 1257 * MemoryRegion. 1258 * 1259 * @mr: #MemoryRegion to access 1260 * @addr: address within that region 1261 * @data: data to write 1262 * @size: size of the access in bytes 1263 * @attrs: memory transaction attributes to use for the access 1264 */ 1265 MemTxResult memory_region_dispatch_write(MemoryRegion *mr, 1266 hwaddr addr, 1267 uint64_t data, 1268 unsigned size, 1269 MemTxAttrs attrs); 1270 1271 /** 1272 * address_space_init: initializes an address space 1273 * 1274 * @as: an uninitialized #AddressSpace 1275 * @root: a #MemoryRegion that routes addresses for the address space 1276 * @name: an address space name. The name is only used for debugging 1277 * output. 1278 */ 1279 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name); 1280 1281 /** 1282 * address_space_init_shareable: return an address space for a memory region, 1283 * creating it if it does not already exist 1284 * 1285 * @root: a #MemoryRegion that routes addresses for the address space 1286 * @name: an address space name. The name is only used for debugging 1287 * output. 1288 * 1289 * This function will return a pointer to an existing AddressSpace 1290 * which was initialized with the specified MemoryRegion, or it will 1291 * create and initialize one if it does not already exist. The ASes 1292 * are reference-counted, so the memory will be freed automatically 1293 * when the AddressSpace is destroyed via address_space_destroy. 1294 */ 1295 AddressSpace *address_space_init_shareable(MemoryRegion *root, 1296 const char *name); 1297 1298 /** 1299 * address_space_destroy: destroy an address space 1300 * 1301 * Releases all resources associated with an address space. After an address space 1302 * is destroyed, its root memory region (given by address_space_init()) may be destroyed 1303 * as well. 1304 * 1305 * @as: address space to be destroyed 1306 */ 1307 void address_space_destroy(AddressSpace *as); 1308 1309 /** 1310 * address_space_rw: read from or write to an address space. 1311 * 1312 * Return a MemTxResult indicating whether the operation succeeded 1313 * or failed (eg unassigned memory, device rejected the transaction, 1314 * IOMMU fault). 1315 * 1316 * @as: #AddressSpace to be accessed 1317 * @addr: address within that address space 1318 * @attrs: memory transaction attributes 1319 * @buf: buffer with the data transferred 1320 * @is_write: indicates the transfer direction 1321 */ 1322 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, 1323 MemTxAttrs attrs, uint8_t *buf, 1324 int len, bool is_write); 1325 1326 /** 1327 * address_space_write: write to address space. 1328 * 1329 * Return a MemTxResult indicating whether the operation succeeded 1330 * or failed (eg unassigned memory, device rejected the transaction, 1331 * IOMMU fault). 1332 * 1333 * @as: #AddressSpace to be accessed 1334 * @addr: address within that address space 1335 * @attrs: memory transaction attributes 1336 * @buf: buffer with the data transferred 1337 */ 1338 MemTxResult address_space_write(AddressSpace *as, hwaddr addr, 1339 MemTxAttrs attrs, 1340 const uint8_t *buf, int len); 1341 1342 /* address_space_ld*: load from an address space 1343 * address_space_st*: store to an address space 1344 * 1345 * These functions perform a load or store of the byte, word, 1346 * longword or quad to the specified address within the AddressSpace. 1347 * The _le suffixed functions treat the data as little endian; 1348 * _be indicates big endian; no suffix indicates "same endianness 1349 * as guest CPU". 1350 * 1351 * The "guest CPU endianness" accessors are deprecated for use outside 1352 * target-* code; devices should be CPU-agnostic and use either the LE 1353 * or the BE accessors. 1354 * 1355 * @as #AddressSpace to be accessed 1356 * @addr: address within that address space 1357 * @val: data value, for stores 1358 * @attrs: memory transaction attributes 1359 * @result: location to write the success/failure of the transaction; 1360 * if NULL, this information is discarded 1361 */ 1362 uint32_t address_space_ldub(AddressSpace *as, hwaddr addr, 1363 MemTxAttrs attrs, MemTxResult *result); 1364 uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr, 1365 MemTxAttrs attrs, MemTxResult *result); 1366 uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr, 1367 MemTxAttrs attrs, MemTxResult *result); 1368 uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr, 1369 MemTxAttrs attrs, MemTxResult *result); 1370 uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr, 1371 MemTxAttrs attrs, MemTxResult *result); 1372 uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr, 1373 MemTxAttrs attrs, MemTxResult *result); 1374 uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr, 1375 MemTxAttrs attrs, MemTxResult *result); 1376 void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val, 1377 MemTxAttrs attrs, MemTxResult *result); 1378 void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val, 1379 MemTxAttrs attrs, MemTxResult *result); 1380 void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val, 1381 MemTxAttrs attrs, MemTxResult *result); 1382 void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val, 1383 MemTxAttrs attrs, MemTxResult *result); 1384 void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val, 1385 MemTxAttrs attrs, MemTxResult *result); 1386 void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val, 1387 MemTxAttrs attrs, MemTxResult *result); 1388 void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val, 1389 MemTxAttrs attrs, MemTxResult *result); 1390 1391 /* address_space_translate: translate an address range into an address space 1392 * into a MemoryRegion and an address range into that section. Should be 1393 * called from an RCU critical section, to avoid that the last reference 1394 * to the returned region disappears after address_space_translate returns. 1395 * 1396 * @as: #AddressSpace to be accessed 1397 * @addr: address within that address space 1398 * @xlat: pointer to address within the returned memory region section's 1399 * #MemoryRegion. 1400 * @len: pointer to length 1401 * @is_write: indicates the transfer direction 1402 */ 1403 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr, 1404 hwaddr *xlat, hwaddr *len, 1405 bool is_write); 1406 1407 /* address_space_access_valid: check for validity of accessing an address 1408 * space range 1409 * 1410 * Check whether memory is assigned to the given address space range, and 1411 * access is permitted by any IOMMU regions that are active for the address 1412 * space. 1413 * 1414 * For now, addr and len should be aligned to a page size. This limitation 1415 * will be lifted in the future. 1416 * 1417 * @as: #AddressSpace to be accessed 1418 * @addr: address within that address space 1419 * @len: length of the area to be checked 1420 * @is_write: indicates the transfer direction 1421 */ 1422 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write); 1423 1424 /* address_space_map: map a physical memory region into a host virtual address 1425 * 1426 * May map a subset of the requested range, given by and returned in @plen. 1427 * May return %NULL if resources needed to perform the mapping are exhausted. 1428 * Use only for reads OR writes - not for read-modify-write operations. 1429 * Use cpu_register_map_client() to know when retrying the map operation is 1430 * likely to succeed. 1431 * 1432 * @as: #AddressSpace to be accessed 1433 * @addr: address within that address space 1434 * @plen: pointer to length of buffer; updated on return 1435 * @is_write: indicates the transfer direction 1436 */ 1437 void *address_space_map(AddressSpace *as, hwaddr addr, 1438 hwaddr *plen, bool is_write); 1439 1440 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map() 1441 * 1442 * Will also mark the memory as dirty if @is_write == %true. @access_len gives 1443 * the amount of memory that was actually read or written by the caller. 1444 * 1445 * @as: #AddressSpace used 1446 * @addr: address within that address space 1447 * @len: buffer length as returned by address_space_map() 1448 * @access_len: amount of data actually transferred 1449 * @is_write: indicates the transfer direction 1450 */ 1451 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, 1452 int is_write, hwaddr access_len); 1453 1454 1455 /* Internal functions, part of the implementation of address_space_read. */ 1456 MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr, 1457 MemTxAttrs attrs, uint8_t *buf, 1458 int len, hwaddr addr1, hwaddr l, 1459 MemoryRegion *mr); 1460 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr, 1461 MemTxAttrs attrs, uint8_t *buf, int len); 1462 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr); 1463 1464 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) 1465 { 1466 if (is_write) { 1467 return memory_region_is_ram(mr) && !mr->readonly; 1468 } else { 1469 return memory_region_is_ram(mr) || memory_region_is_romd(mr); 1470 } 1471 } 1472 1473 /** 1474 * address_space_read: read from an address space. 1475 * 1476 * Return a MemTxResult indicating whether the operation succeeded 1477 * or failed (eg unassigned memory, device rejected the transaction, 1478 * IOMMU fault). 1479 * 1480 * @as: #AddressSpace to be accessed 1481 * @addr: address within that address space 1482 * @attrs: memory transaction attributes 1483 * @buf: buffer with the data transferred 1484 */ 1485 static inline __attribute__((__always_inline__)) 1486 MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs, 1487 uint8_t *buf, int len) 1488 { 1489 MemTxResult result = MEMTX_OK; 1490 hwaddr l, addr1; 1491 void *ptr; 1492 MemoryRegion *mr; 1493 1494 if (__builtin_constant_p(len)) { 1495 if (len) { 1496 rcu_read_lock(); 1497 l = len; 1498 mr = address_space_translate(as, addr, &addr1, &l, false); 1499 if (len == l && memory_access_is_direct(mr, false)) { 1500 ptr = qemu_map_ram_ptr(mr->ram_block, addr1); 1501 memcpy(buf, ptr, len); 1502 } else { 1503 result = address_space_read_continue(as, addr, attrs, buf, len, 1504 addr1, l, mr); 1505 } 1506 rcu_read_unlock(); 1507 } 1508 } else { 1509 result = address_space_read_full(as, addr, attrs, buf, len); 1510 } 1511 return result; 1512 } 1513 1514 #endif 1515 1516 #endif 1517