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