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