1 /* 2 * Physical memory management API 3 * 4 * Copyright 2011 Red Hat, Inc. and/or its affiliates 5 * 6 * Authors: 7 * Avi Kivity <avi@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 */ 13 14 #ifndef MEMORY_H 15 #define MEMORY_H 16 17 #ifndef CONFIG_USER_ONLY 18 19 #include "exec/cpu-common.h" 20 #include "exec/hwaddr.h" 21 #include "exec/memattrs.h" 22 #include "exec/ramlist.h" 23 #include "qemu/queue.h" 24 #include "qemu/int128.h" 25 #include "qemu/notify.h" 26 #include "qom/object.h" 27 #include "qemu/rcu.h" 28 #include "hw/qdev-core.h" 29 30 #define RAM_ADDR_INVALID (~(ram_addr_t)0) 31 32 #define MAX_PHYS_ADDR_SPACE_BITS 62 33 #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1) 34 35 #define TYPE_MEMORY_REGION "qemu:memory-region" 36 #define MEMORY_REGION(obj) \ 37 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION) 38 39 #define TYPE_IOMMU_MEMORY_REGION "qemu:iommu-memory-region" 40 #define IOMMU_MEMORY_REGION(obj) \ 41 OBJECT_CHECK(IOMMUMemoryRegion, (obj), TYPE_IOMMU_MEMORY_REGION) 42 #define IOMMU_MEMORY_REGION_CLASS(klass) \ 43 OBJECT_CLASS_CHECK(IOMMUMemoryRegionClass, (klass), \ 44 TYPE_IOMMU_MEMORY_REGION) 45 #define IOMMU_MEMORY_REGION_GET_CLASS(obj) \ 46 OBJECT_GET_CLASS(IOMMUMemoryRegionClass, (obj), \ 47 TYPE_IOMMU_MEMORY_REGION) 48 49 typedef struct MemoryRegionOps MemoryRegionOps; 50 typedef struct MemoryRegionMmio MemoryRegionMmio; 51 52 struct MemoryRegionMmio { 53 CPUReadMemoryFunc *read[3]; 54 CPUWriteMemoryFunc *write[3]; 55 }; 56 57 typedef struct IOMMUTLBEntry IOMMUTLBEntry; 58 59 /* See address_space_translate: bit 0 is read, bit 1 is write. */ 60 typedef enum { 61 IOMMU_NONE = 0, 62 IOMMU_RO = 1, 63 IOMMU_WO = 2, 64 IOMMU_RW = 3, 65 } IOMMUAccessFlags; 66 67 #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0)) 68 69 struct IOMMUTLBEntry { 70 AddressSpace *target_as; 71 hwaddr iova; 72 hwaddr translated_addr; 73 hwaddr addr_mask; /* 0xfff = 4k translation */ 74 IOMMUAccessFlags perm; 75 }; 76 77 /* 78 * Bitmap for different IOMMUNotifier capabilities. Each notifier can 79 * register with one or multiple IOMMU Notifier capability bit(s). 80 */ 81 typedef enum { 82 IOMMU_NOTIFIER_NONE = 0, 83 /* Notify cache invalidations */ 84 IOMMU_NOTIFIER_UNMAP = 0x1, 85 /* Notify entry changes (newly created entries) */ 86 IOMMU_NOTIFIER_MAP = 0x2, 87 } IOMMUNotifierFlag; 88 89 #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP) 90 91 struct IOMMUNotifier; 92 typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier, 93 IOMMUTLBEntry *data); 94 95 struct IOMMUNotifier { 96 IOMMUNotify notify; 97 IOMMUNotifierFlag notifier_flags; 98 /* Notify for address space range start <= addr <= end */ 99 hwaddr start; 100 hwaddr end; 101 QLIST_ENTRY(IOMMUNotifier) node; 102 }; 103 typedef struct IOMMUNotifier IOMMUNotifier; 104 105 static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, 106 IOMMUNotifierFlag flags, 107 hwaddr start, hwaddr end) 108 { 109 n->notify = fn; 110 n->notifier_flags = flags; 111 n->start = start; 112 n->end = end; 113 } 114 115 /* New-style MMIO accessors can indicate that the transaction failed. 116 * A zero (MEMTX_OK) response means success; anything else is a failure 117 * of some kind. The memory subsystem will bitwise-OR together results 118 * if it is synthesizing an operation from multiple smaller accesses. 119 */ 120 #define MEMTX_OK 0 121 #define MEMTX_ERROR (1U << 0) /* device returned an error */ 122 #define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */ 123 typedef uint32_t MemTxResult; 124 125 /* 126 * Memory region callbacks 127 */ 128 struct MemoryRegionOps { 129 /* Read from the memory region. @addr is relative to @mr; @size is 130 * in bytes. */ 131 uint64_t (*read)(void *opaque, 132 hwaddr addr, 133 unsigned size); 134 /* Write to the memory region. @addr is relative to @mr; @size is 135 * in bytes. */ 136 void (*write)(void *opaque, 137 hwaddr addr, 138 uint64_t data, 139 unsigned size); 140 141 MemTxResult (*read_with_attrs)(void *opaque, 142 hwaddr addr, 143 uint64_t *data, 144 unsigned size, 145 MemTxAttrs attrs); 146 MemTxResult (*write_with_attrs)(void *opaque, 147 hwaddr addr, 148 uint64_t data, 149 unsigned size, 150 MemTxAttrs attrs); 151 /* Instruction execution pre-callback: 152 * @addr is the address of the access relative to the @mr. 153 * @size is the size of the area returned by the callback. 154 * @offset is the location of the pointer inside @mr. 155 * 156 * Returns a pointer to a location which contains guest code. 157 */ 158 void *(*request_ptr)(void *opaque, hwaddr addr, unsigned *size, 159 unsigned *offset); 160 161 enum device_endian endianness; 162 /* Guest-visible constraints: */ 163 struct { 164 /* If nonzero, specify bounds on access sizes beyond which a machine 165 * check is thrown. 166 */ 167 unsigned min_access_size; 168 unsigned max_access_size; 169 /* If true, unaligned accesses are supported. Otherwise unaligned 170 * accesses throw machine checks. 171 */ 172 bool unaligned; 173 /* 174 * If present, and returns #false, the transaction is not accepted 175 * by the device (and results in machine dependent behaviour such 176 * as a machine check exception). 177 */ 178 bool (*accepts)(void *opaque, hwaddr addr, 179 unsigned size, bool is_write); 180 } valid; 181 /* Internal implementation constraints: */ 182 struct { 183 /* If nonzero, specifies the minimum size implemented. Smaller sizes 184 * will be rounded upwards and a partial result will be returned. 185 */ 186 unsigned min_access_size; 187 /* If nonzero, specifies the maximum size implemented. Larger sizes 188 * will be done as a series of accesses with smaller sizes. 189 */ 190 unsigned max_access_size; 191 /* If true, unaligned accesses are supported. Otherwise all accesses 192 * are converted to (possibly multiple) naturally aligned accesses. 193 */ 194 bool unaligned; 195 } impl; 196 197 /* If .read and .write are not present, old_mmio may be used for 198 * backwards compatibility with old mmio registration 199 */ 200 const MemoryRegionMmio old_mmio; 201 }; 202 203 typedef struct IOMMUMemoryRegionClass { 204 /* private */ 205 struct DeviceClass parent_class; 206 207 /* 208 * Return a TLB entry that contains a given address. Flag should 209 * be the access permission of this translation operation. We can 210 * set flag to IOMMU_NONE to mean that we don't need any 211 * read/write permission checks, like, when for region replay. 212 */ 213 IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr, 214 IOMMUAccessFlags flag); 215 /* Returns minimum supported page size */ 216 uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu); 217 /* Called when IOMMU Notifier flag changed */ 218 void (*notify_flag_changed)(IOMMUMemoryRegion *iommu, 219 IOMMUNotifierFlag old_flags, 220 IOMMUNotifierFlag new_flags); 221 /* Set this up to provide customized IOMMU replay function */ 222 void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier); 223 } IOMMUMemoryRegionClass; 224 225 typedef struct CoalescedMemoryRange CoalescedMemoryRange; 226 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; 227 228 struct MemoryRegion { 229 Object parent_obj; 230 231 /* All fields are private - violators will be prosecuted */ 232 233 /* The following fields should fit in a cache line */ 234 bool romd_mode; 235 bool ram; 236 bool subpage; 237 bool readonly; /* For RAM regions */ 238 bool rom_device; 239 bool flush_coalesced_mmio; 240 bool global_locking; 241 uint8_t dirty_log_mask; 242 bool is_iommu; 243 RAMBlock *ram_block; 244 Object *owner; 245 246 const MemoryRegionOps *ops; 247 void *opaque; 248 MemoryRegion *container; 249 Int128 size; 250 hwaddr addr; 251 void (*destructor)(MemoryRegion *mr); 252 uint64_t align; 253 bool terminates; 254 bool ram_device; 255 bool enabled; 256 bool warning_printed; /* For reservations */ 257 uint8_t vga_logging_count; 258 MemoryRegion *alias; 259 hwaddr alias_offset; 260 int32_t priority; 261 QTAILQ_HEAD(subregions, MemoryRegion) subregions; 262 QTAILQ_ENTRY(MemoryRegion) subregions_link; 263 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced; 264 const char *name; 265 unsigned ioeventfd_nb; 266 MemoryRegionIoeventfd *ioeventfds; 267 }; 268 269 struct IOMMUMemoryRegion { 270 MemoryRegion parent_obj; 271 272 QLIST_HEAD(, IOMMUNotifier) iommu_notify; 273 IOMMUNotifierFlag iommu_notify_flags; 274 }; 275 276 #define IOMMU_NOTIFIER_FOREACH(n, mr) \ 277 QLIST_FOREACH((n), &(mr)->iommu_notify, node) 278 279 /** 280 * MemoryListener: callbacks structure for updates to the physical memory map 281 * 282 * Allows a component to adjust to changes in the guest-visible memory map. 283 * Use with memory_listener_register() and memory_listener_unregister(). 284 */ 285 struct MemoryListener { 286 void (*begin)(MemoryListener *listener); 287 void (*commit)(MemoryListener *listener); 288 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section); 289 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section); 290 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section); 291 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section, 292 int old, int new); 293 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section, 294 int old, int new); 295 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section); 296 void (*log_global_start)(MemoryListener *listener); 297 void (*log_global_stop)(MemoryListener *listener); 298 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section, 299 bool match_data, uint64_t data, EventNotifier *e); 300 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section, 301 bool match_data, uint64_t data, EventNotifier *e); 302 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section, 303 hwaddr addr, hwaddr len); 304 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section, 305 hwaddr addr, hwaddr len); 306 /* Lower = earlier (during add), later (during del) */ 307 unsigned priority; 308 AddressSpace *address_space; 309 QTAILQ_ENTRY(MemoryListener) link; 310 QTAILQ_ENTRY(MemoryListener) link_as; 311 }; 312 313 /** 314 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects 315 */ 316 struct AddressSpace { 317 /* All fields are private. */ 318 struct rcu_head rcu; 319 char *name; 320 MemoryRegion *root; 321 int ref_count; 322 bool malloced; 323 324 /* Accessed via RCU. */ 325 struct FlatView *current_map; 326 327 int ioeventfd_nb; 328 struct MemoryRegionIoeventfd *ioeventfds; 329 struct AddressSpaceDispatch *dispatch; 330 struct AddressSpaceDispatch *next_dispatch; 331 MemoryListener dispatch_listener; 332 QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners; 333 QTAILQ_ENTRY(AddressSpace) address_spaces_link; 334 }; 335 336 /** 337 * MemoryRegionSection: describes a fragment of a #MemoryRegion 338 * 339 * @mr: the region, or %NULL if empty 340 * @address_space: the address space the region is mapped in 341 * @offset_within_region: the beginning of the section, relative to @mr's start 342 * @size: the size of the section; will not exceed @mr's boundaries 343 * @offset_within_address_space: the address of the first byte of the section 344 * relative to the region's address space 345 * @readonly: writes to this section are ignored 346 */ 347 struct MemoryRegionSection { 348 MemoryRegion *mr; 349 AddressSpace *address_space; 350 hwaddr offset_within_region; 351 Int128 size; 352 hwaddr offset_within_address_space; 353 bool readonly; 354 }; 355 356 /** 357 * memory_region_init: Initialize a memory region 358 * 359 * The region typically acts as a container for other memory regions. Use 360 * memory_region_add_subregion() to add subregions. 361 * 362 * @mr: the #MemoryRegion to be initialized 363 * @owner: the object that tracks the region's reference count 364 * @name: used for debugging; not visible to the user or ABI 365 * @size: size of the region; any subregions beyond this size will be clipped 366 */ 367 void memory_region_init(MemoryRegion *mr, 368 struct Object *owner, 369 const char *name, 370 uint64_t size); 371 372 /** 373 * memory_region_ref: Add 1 to a memory region's reference count 374 * 375 * Whenever memory regions are accessed outside the BQL, they need to be 376 * preserved against hot-unplug. MemoryRegions actually do not have their 377 * own reference count; they piggyback on a QOM object, their "owner". 378 * This function adds a reference to the owner. 379 * 380 * All MemoryRegions must have an owner if they can disappear, even if the 381 * device they belong to operates exclusively under the BQL. This is because 382 * the region could be returned at any time by memory_region_find, and this 383 * is usually under guest control. 384 * 385 * @mr: the #MemoryRegion 386 */ 387 void memory_region_ref(MemoryRegion *mr); 388 389 /** 390 * memory_region_unref: Remove 1 to a memory region's reference count 391 * 392 * Whenever memory regions are accessed outside the BQL, they need to be 393 * preserved against hot-unplug. MemoryRegions actually do not have their 394 * own reference count; they piggyback on a QOM object, their "owner". 395 * This function removes a reference to the owner and possibly destroys it. 396 * 397 * @mr: the #MemoryRegion 398 */ 399 void memory_region_unref(MemoryRegion *mr); 400 401 /** 402 * memory_region_init_io: Initialize an I/O memory region. 403 * 404 * Accesses into the region will cause the callbacks in @ops to be called. 405 * if @size is nonzero, subregions will be clipped to @size. 406 * 407 * @mr: the #MemoryRegion to be initialized. 408 * @owner: the object that tracks the region's reference count 409 * @ops: a structure containing read and write callbacks to be used when 410 * I/O is performed on the region. 411 * @opaque: passed to the read and write callbacks of the @ops structure. 412 * @name: used for debugging; not visible to the user or ABI 413 * @size: size of the region. 414 */ 415 void memory_region_init_io(MemoryRegion *mr, 416 struct Object *owner, 417 const MemoryRegionOps *ops, 418 void *opaque, 419 const char *name, 420 uint64_t size); 421 422 /** 423 * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses 424 * into the region will modify memory 425 * directly. 426 * 427 * @mr: the #MemoryRegion to be initialized. 428 * @owner: the object that tracks the region's reference count 429 * @name: Region name, becomes part of RAMBlock name used in migration stream 430 * must be unique within any device 431 * @size: size of the region. 432 * @errp: pointer to Error*, to store an error if it happens. 433 * 434 * Note that this function does not do anything to cause the data in the 435 * RAM memory region to be migrated; that is the responsibility of the caller. 436 */ 437 void memory_region_init_ram_nomigrate(MemoryRegion *mr, 438 struct Object *owner, 439 const char *name, 440 uint64_t size, 441 Error **errp); 442 443 /** 444 * memory_region_init_resizeable_ram: Initialize memory region with resizeable 445 * RAM. Accesses into the region will 446 * modify memory directly. Only an initial 447 * portion of this RAM is actually used. 448 * The used size can change across reboots. 449 * 450 * @mr: the #MemoryRegion to be initialized. 451 * @owner: the object that tracks the region's reference count 452 * @name: Region name, becomes part of RAMBlock name used in migration stream 453 * must be unique within any device 454 * @size: used size of the region. 455 * @max_size: max size of the region. 456 * @resized: callback to notify owner about used size change. 457 * @errp: pointer to Error*, to store an error if it happens. 458 * 459 * Note that this function does not do anything to cause the data in the 460 * RAM memory region to be migrated; that is the responsibility of the caller. 461 */ 462 void memory_region_init_resizeable_ram(MemoryRegion *mr, 463 struct Object *owner, 464 const char *name, 465 uint64_t size, 466 uint64_t max_size, 467 void (*resized)(const char*, 468 uint64_t length, 469 void *host), 470 Error **errp); 471 #ifdef __linux__ 472 /** 473 * memory_region_init_ram_from_file: Initialize RAM memory region with a 474 * mmap-ed backend. 475 * 476 * @mr: the #MemoryRegion to be initialized. 477 * @owner: the object that tracks the region's reference count 478 * @name: Region name, becomes part of RAMBlock name used in migration stream 479 * must be unique within any device 480 * @size: size of the region. 481 * @share: %true if memory must be mmaped with the MAP_SHARED flag 482 * @path: the path in which to allocate the RAM. 483 * @errp: pointer to Error*, to store an error if it happens. 484 * 485 * Note that this function does not do anything to cause the data in the 486 * RAM memory region to be migrated; that is the responsibility of the caller. 487 */ 488 void memory_region_init_ram_from_file(MemoryRegion *mr, 489 struct Object *owner, 490 const char *name, 491 uint64_t size, 492 bool share, 493 const char *path, 494 Error **errp); 495 496 /** 497 * memory_region_init_ram_from_fd: Initialize RAM memory region with a 498 * mmap-ed backend. 499 * 500 * @mr: the #MemoryRegion to be initialized. 501 * @owner: the object that tracks the region's reference count 502 * @name: the name of the region. 503 * @size: size of the region. 504 * @share: %true if memory must be mmaped with the MAP_SHARED flag 505 * @fd: the fd to mmap. 506 * @errp: pointer to Error*, to store an error if it happens. 507 * 508 * Note that this function does not do anything to cause the data in the 509 * RAM memory region to be migrated; that is the responsibility of the caller. 510 */ 511 void memory_region_init_ram_from_fd(MemoryRegion *mr, 512 struct Object *owner, 513 const char *name, 514 uint64_t size, 515 bool share, 516 int fd, 517 Error **errp); 518 #endif 519 520 /** 521 * memory_region_init_ram_ptr: Initialize RAM memory region from a 522 * user-provided pointer. Accesses into the 523 * region will modify memory directly. 524 * 525 * @mr: the #MemoryRegion to be initialized. 526 * @owner: the object that tracks the region's reference count 527 * @name: Region name, becomes part of RAMBlock name used in migration stream 528 * must be unique within any device 529 * @size: size of the region. 530 * @ptr: memory to be mapped; must contain at least @size bytes. 531 * 532 * Note that this function does not do anything to cause the data in the 533 * RAM memory region to be migrated; that is the responsibility of the caller. 534 */ 535 void memory_region_init_ram_ptr(MemoryRegion *mr, 536 struct Object *owner, 537 const char *name, 538 uint64_t size, 539 void *ptr); 540 541 /** 542 * memory_region_init_ram_device_ptr: Initialize RAM device memory region from 543 * a user-provided pointer. 544 * 545 * A RAM device represents a mapping to a physical device, such as to a PCI 546 * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped 547 * into the VM address space and access to the region will modify memory 548 * directly. However, the memory region should not be included in a memory 549 * dump (device may not be enabled/mapped at the time of the dump), and 550 * operations incompatible with manipulating MMIO should be avoided. Replaces 551 * skip_dump flag. 552 * 553 * @mr: the #MemoryRegion to be initialized. 554 * @owner: the object that tracks the region's reference count 555 * @name: the name of the region. 556 * @size: size of the region. 557 * @ptr: memory to be mapped; must contain at least @size bytes. 558 * 559 * Note that this function does not do anything to cause the data in the 560 * RAM memory region to be migrated; that is the responsibility of the caller. 561 * (For RAM device memory regions, migrating the contents rarely makes sense.) 562 */ 563 void memory_region_init_ram_device_ptr(MemoryRegion *mr, 564 struct Object *owner, 565 const char *name, 566 uint64_t size, 567 void *ptr); 568 569 /** 570 * memory_region_init_alias: Initialize a memory region that aliases all or a 571 * part of another memory region. 572 * 573 * @mr: the #MemoryRegion to be initialized. 574 * @owner: the object that tracks the region's reference count 575 * @name: used for debugging; not visible to the user or ABI 576 * @orig: the region to be referenced; @mr will be equivalent to 577 * @orig between @offset and @offset + @size - 1. 578 * @offset: start of the section in @orig to be referenced. 579 * @size: size of the region. 580 */ 581 void memory_region_init_alias(MemoryRegion *mr, 582 struct Object *owner, 583 const char *name, 584 MemoryRegion *orig, 585 hwaddr offset, 586 uint64_t size); 587 588 /** 589 * memory_region_init_rom_nomigrate: Initialize a ROM memory region. 590 * 591 * This has the same effect as calling memory_region_init_ram_nomigrate() 592 * and then marking the resulting region read-only with 593 * memory_region_set_readonly(). 594 * 595 * Note that this function does not do anything to cause the data in the 596 * RAM side of the memory region to be migrated; that is the responsibility 597 * of the caller. 598 * 599 * @mr: the #MemoryRegion to be initialized. 600 * @owner: the object that tracks the region's reference count 601 * @name: Region name, becomes part of RAMBlock name used in migration stream 602 * must be unique within any device 603 * @size: size of the region. 604 * @errp: pointer to Error*, to store an error if it happens. 605 */ 606 void memory_region_init_rom_nomigrate(MemoryRegion *mr, 607 struct Object *owner, 608 const char *name, 609 uint64_t size, 610 Error **errp); 611 612 /** 613 * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region. 614 * Writes are handled via callbacks. 615 * 616 * Note that this function does not do anything to cause the data in the 617 * RAM side of the memory region to be migrated; that is the responsibility 618 * of the caller. 619 * 620 * @mr: the #MemoryRegion to be initialized. 621 * @owner: the object that tracks the region's reference count 622 * @ops: callbacks for write access handling (must not be NULL). 623 * @name: Region name, becomes part of RAMBlock name used in migration stream 624 * must be unique within any device 625 * @size: size of the region. 626 * @errp: pointer to Error*, to store an error if it happens. 627 */ 628 void memory_region_init_rom_device_nomigrate(MemoryRegion *mr, 629 struct Object *owner, 630 const MemoryRegionOps *ops, 631 void *opaque, 632 const char *name, 633 uint64_t size, 634 Error **errp); 635 636 /** 637 * memory_region_init_reservation: Initialize a memory region that reserves 638 * I/O space. 639 * 640 * A reservation region primariy serves debugging purposes. It claims I/O 641 * space that is not supposed to be handled by QEMU itself. Any access via 642 * the memory API will cause an abort(). 643 * This function is deprecated. Use memory_region_init_io() with NULL 644 * callbacks instead. 645 * 646 * @mr: the #MemoryRegion to be initialized 647 * @owner: the object that tracks the region's reference count 648 * @name: used for debugging; not visible to the user or ABI 649 * @size: size of the region. 650 */ 651 static inline void memory_region_init_reservation(MemoryRegion *mr, 652 Object *owner, 653 const char *name, 654 uint64_t size) 655 { 656 memory_region_init_io(mr, owner, NULL, mr, name, size); 657 } 658 659 /** 660 * memory_region_init_iommu: Initialize a memory region of a custom type 661 * that translates addresses 662 * 663 * An IOMMU region translates addresses and forwards accesses to a target 664 * memory region. 665 * 666 * @typename: QOM class name 667 * @_iommu_mr: the #IOMMUMemoryRegion to be initialized 668 * @instance_size: the IOMMUMemoryRegion subclass instance size 669 * @owner: the object that tracks the region's reference count 670 * @ops: a function that translates addresses into the @target region 671 * @name: used for debugging; not visible to the user or ABI 672 * @size: size of the region. 673 */ 674 void memory_region_init_iommu(void *_iommu_mr, 675 size_t instance_size, 676 const char *mrtypename, 677 Object *owner, 678 const char *name, 679 uint64_t size); 680 681 /** 682 * memory_region_init_ram - Initialize RAM memory region. Accesses into the 683 * region will modify memory directly. 684 * 685 * @mr: the #MemoryRegion to be initialized 686 * @owner: the object that tracks the region's reference count (must be 687 * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL) 688 * @name: name of the memory region 689 * @size: size of the region in bytes 690 * @errp: pointer to Error*, to store an error if it happens. 691 * 692 * This function allocates RAM for a board model or device, and 693 * arranges for it to be migrated (by calling vmstate_register_ram() 694 * if @owner is a DeviceState, or vmstate_register_ram_global() if 695 * @owner is NULL). 696 * 697 * TODO: Currently we restrict @owner to being either NULL (for 698 * global RAM regions with no owner) or devices, so that we can 699 * give the RAM block a unique name for migration purposes. 700 * We should lift this restriction and allow arbitrary Objects. 701 * If you pass a non-NULL non-device @owner then we will assert. 702 */ 703 void memory_region_init_ram(MemoryRegion *mr, 704 struct Object *owner, 705 const char *name, 706 uint64_t size, 707 Error **errp); 708 709 /** 710 * memory_region_init_rom: Initialize a ROM memory region. 711 * 712 * This has the same effect as calling memory_region_init_ram() 713 * and then marking the resulting region read-only with 714 * memory_region_set_readonly(). This includes arranging for the 715 * contents to be migrated. 716 * 717 * TODO: Currently we restrict @owner to being either NULL (for 718 * global RAM regions with no owner) or devices, so that we can 719 * give the RAM block a unique name for migration purposes. 720 * We should lift this restriction and allow arbitrary Objects. 721 * If you pass a non-NULL non-device @owner then we will assert. 722 * 723 * @mr: the #MemoryRegion to be initialized. 724 * @owner: the object that tracks the region's reference count 725 * @name: Region name, becomes part of RAMBlock name used in migration stream 726 * must be unique within any device 727 * @size: size of the region. 728 * @errp: pointer to Error*, to store an error if it happens. 729 */ 730 void memory_region_init_rom(MemoryRegion *mr, 731 struct Object *owner, 732 const char *name, 733 uint64_t size, 734 Error **errp); 735 736 /** 737 * memory_region_init_rom_device: Initialize a ROM memory region. 738 * Writes are handled via callbacks. 739 * 740 * This function initializes a memory region backed by RAM for reads 741 * and callbacks for writes, and arranges for the RAM backing to 742 * be migrated (by calling vmstate_register_ram() 743 * if @owner is a DeviceState, or vmstate_register_ram_global() if 744 * @owner is NULL). 745 * 746 * TODO: Currently we restrict @owner to being either NULL (for 747 * global RAM regions with no owner) or devices, so that we can 748 * give the RAM block a unique name for migration purposes. 749 * We should lift this restriction and allow arbitrary Objects. 750 * If you pass a non-NULL non-device @owner then we will assert. 751 * 752 * @mr: the #MemoryRegion to be initialized. 753 * @owner: the object that tracks the region's reference count 754 * @ops: callbacks for write access handling (must not be NULL). 755 * @name: Region name, becomes part of RAMBlock name used in migration stream 756 * must be unique within any device 757 * @size: size of the region. 758 * @errp: pointer to Error*, to store an error if it happens. 759 */ 760 void memory_region_init_rom_device(MemoryRegion *mr, 761 struct Object *owner, 762 const MemoryRegionOps *ops, 763 void *opaque, 764 const char *name, 765 uint64_t size, 766 Error **errp); 767 768 769 /** 770 * memory_region_owner: get a memory region's owner. 771 * 772 * @mr: the memory region being queried. 773 */ 774 struct Object *memory_region_owner(MemoryRegion *mr); 775 776 /** 777 * memory_region_size: get a memory region's size. 778 * 779 * @mr: the memory region being queried. 780 */ 781 uint64_t memory_region_size(MemoryRegion *mr); 782 783 /** 784 * memory_region_is_ram: check whether a memory region is random access 785 * 786 * Returns %true is a memory region is random access. 787 * 788 * @mr: the memory region being queried 789 */ 790 static inline bool memory_region_is_ram(MemoryRegion *mr) 791 { 792 return mr->ram; 793 } 794 795 /** 796 * memory_region_is_ram_device: check whether a memory region is a ram device 797 * 798 * Returns %true is a memory region is a device backed ram region 799 * 800 * @mr: the memory region being queried 801 */ 802 bool memory_region_is_ram_device(MemoryRegion *mr); 803 804 /** 805 * memory_region_is_romd: check whether a memory region is in ROMD mode 806 * 807 * Returns %true if a memory region is a ROM device and currently set to allow 808 * direct reads. 809 * 810 * @mr: the memory region being queried 811 */ 812 static inline bool memory_region_is_romd(MemoryRegion *mr) 813 { 814 return mr->rom_device && mr->romd_mode; 815 } 816 817 /** 818 * memory_region_get_iommu: check whether a memory region is an iommu 819 * 820 * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu, 821 * otherwise NULL. 822 * 823 * @mr: the memory region being queried 824 */ 825 static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr) 826 { 827 if (mr->alias) { 828 return memory_region_get_iommu(mr->alias); 829 } 830 if (mr->is_iommu) { 831 return (IOMMUMemoryRegion *) mr; 832 } 833 return NULL; 834 } 835 836 /** 837 * memory_region_get_iommu_class_nocheck: returns iommu memory region class 838 * if an iommu or NULL if not 839 * 840 * Returns pointer to IOMMUMemoryRegioniClass if a memory region is an iommu, 841 * otherwise NULL. This is fast path avoinding QOM checking, use with caution. 842 * 843 * @mr: the memory region being queried 844 */ 845 static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck( 846 IOMMUMemoryRegion *iommu_mr) 847 { 848 return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class); 849 } 850 851 #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL) 852 853 /** 854 * memory_region_iommu_get_min_page_size: get minimum supported page size 855 * for an iommu 856 * 857 * Returns minimum supported page size for an iommu. 858 * 859 * @iommu_mr: the memory region being queried 860 */ 861 uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr); 862 863 /** 864 * memory_region_notify_iommu: notify a change in an IOMMU translation entry. 865 * 866 * The notification type will be decided by entry.perm bits: 867 * 868 * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE. 869 * - For MAP (newly added entry) notifies: set entry.perm to the 870 * permission of the page (which is definitely !IOMMU_NONE). 871 * 872 * Note: for any IOMMU implementation, an in-place mapping change 873 * should be notified with an UNMAP followed by a MAP. 874 * 875 * @iommu_mr: the memory region that was changed 876 * @entry: the new entry in the IOMMU translation table. The entry 877 * replaces all old entries for the same virtual I/O address range. 878 * Deleted entries have .@perm == 0. 879 */ 880 void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr, 881 IOMMUTLBEntry entry); 882 883 /** 884 * memory_region_notify_one: notify a change in an IOMMU translation 885 * entry to a single notifier 886 * 887 * This works just like memory_region_notify_iommu(), but it only 888 * notifies a specific notifier, not all of them. 889 * 890 * @notifier: the notifier to be notified 891 * @entry: the new entry in the IOMMU translation table. The entry 892 * replaces all old entries for the same virtual I/O address range. 893 * Deleted entries have .@perm == 0. 894 */ 895 void memory_region_notify_one(IOMMUNotifier *notifier, 896 IOMMUTLBEntry *entry); 897 898 /** 899 * memory_region_register_iommu_notifier: register a notifier for changes to 900 * IOMMU translation entries. 901 * 902 * @mr: the memory region to observe 903 * @n: the IOMMUNotifier to be added; the notify callback receives a 904 * pointer to an #IOMMUTLBEntry as the opaque value; the pointer 905 * ceases to be valid on exit from the notifier. 906 */ 907 void memory_region_register_iommu_notifier(MemoryRegion *mr, 908 IOMMUNotifier *n); 909 910 /** 911 * memory_region_iommu_replay: replay existing IOMMU translations to 912 * a notifier with the minimum page granularity returned by 913 * mr->iommu_ops->get_page_size(). 914 * 915 * @iommu_mr: the memory region to observe 916 * @n: the notifier to which to replay iommu mappings 917 */ 918 void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n); 919 920 /** 921 * memory_region_iommu_replay_all: replay existing IOMMU translations 922 * to all the notifiers registered. 923 * 924 * @iommu_mr: the memory region to observe 925 */ 926 void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr); 927 928 /** 929 * memory_region_unregister_iommu_notifier: unregister a notifier for 930 * changes to IOMMU translation entries. 931 * 932 * @mr: the memory region which was observed and for which notity_stopped() 933 * needs to be called 934 * @n: the notifier to be removed. 935 */ 936 void memory_region_unregister_iommu_notifier(MemoryRegion *mr, 937 IOMMUNotifier *n); 938 939 /** 940 * memory_region_name: get a memory region's name 941 * 942 * Returns the string that was used to initialize the memory region. 943 * 944 * @mr: the memory region being queried 945 */ 946 const char *memory_region_name(const MemoryRegion *mr); 947 948 /** 949 * memory_region_is_logging: return whether a memory region is logging writes 950 * 951 * Returns %true if the memory region is logging writes for the given client 952 * 953 * @mr: the memory region being queried 954 * @client: the client being queried 955 */ 956 bool memory_region_is_logging(MemoryRegion *mr, uint8_t client); 957 958 /** 959 * memory_region_get_dirty_log_mask: return the clients for which a 960 * memory region is logging writes. 961 * 962 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants 963 * are the bit indices. 964 * 965 * @mr: the memory region being queried 966 */ 967 uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr); 968 969 /** 970 * memory_region_is_rom: check whether a memory region is ROM 971 * 972 * Returns %true is a memory region is read-only memory. 973 * 974 * @mr: the memory region being queried 975 */ 976 static inline bool memory_region_is_rom(MemoryRegion *mr) 977 { 978 return mr->ram && mr->readonly; 979 } 980 981 982 /** 983 * memory_region_get_fd: Get a file descriptor backing a RAM memory region. 984 * 985 * Returns a file descriptor backing a file-based RAM memory region, 986 * or -1 if the region is not a file-based RAM memory region. 987 * 988 * @mr: the RAM or alias memory region being queried. 989 */ 990 int memory_region_get_fd(MemoryRegion *mr); 991 992 /** 993 * memory_region_from_host: Convert a pointer into a RAM memory region 994 * and an offset within it. 995 * 996 * Given a host pointer inside a RAM memory region (created with 997 * memory_region_init_ram() or memory_region_init_ram_ptr()), return 998 * the MemoryRegion and the offset within it. 999 * 1000 * Use with care; by the time this function returns, the returned pointer is 1001 * not protected by RCU anymore. If the caller is not within an RCU critical 1002 * section and does not hold the iothread lock, it must have other means of 1003 * protecting the pointer, such as a reference to the region that includes 1004 * the incoming ram_addr_t. 1005 * 1006 * @mr: the memory region being queried. 1007 */ 1008 MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset); 1009 1010 /** 1011 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region. 1012 * 1013 * Returns a host pointer to a RAM memory region (created with 1014 * memory_region_init_ram() or memory_region_init_ram_ptr()). 1015 * 1016 * Use with care; by the time this function returns, the returned pointer is 1017 * not protected by RCU anymore. If the caller is not within an RCU critical 1018 * section and does not hold the iothread lock, it must have other means of 1019 * protecting the pointer, such as a reference to the region that includes 1020 * the incoming ram_addr_t. 1021 * 1022 * @mr: the memory region being queried. 1023 */ 1024 void *memory_region_get_ram_ptr(MemoryRegion *mr); 1025 1026 /* memory_region_ram_resize: Resize a RAM region. 1027 * 1028 * Only legal before guest might have detected the memory size: e.g. on 1029 * incoming migration, or right after reset. 1030 * 1031 * @mr: a memory region created with @memory_region_init_resizeable_ram. 1032 * @newsize: the new size the region 1033 * @errp: pointer to Error*, to store an error if it happens. 1034 */ 1035 void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, 1036 Error **errp); 1037 1038 /** 1039 * memory_region_set_log: Turn dirty logging on or off for a region. 1040 * 1041 * Turns dirty logging on or off for a specified client (display, migration). 1042 * Only meaningful for RAM regions. 1043 * 1044 * @mr: the memory region being updated. 1045 * @log: whether dirty logging is to be enabled or disabled. 1046 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only. 1047 */ 1048 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client); 1049 1050 /** 1051 * memory_region_get_dirty: Check whether a range of bytes is dirty 1052 * for a specified client. 1053 * 1054 * Checks whether a range of bytes has been written to since the last 1055 * call to memory_region_reset_dirty() with the same @client. Dirty logging 1056 * must be enabled. 1057 * 1058 * @mr: the memory region being queried. 1059 * @addr: the address (relative to the start of the region) being queried. 1060 * @size: the size of the range being queried. 1061 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 1062 * %DIRTY_MEMORY_VGA. 1063 */ 1064 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr, 1065 hwaddr size, unsigned client); 1066 1067 /** 1068 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region. 1069 * 1070 * Marks a range of bytes as dirty, after it has been dirtied outside 1071 * guest code. 1072 * 1073 * @mr: the memory region being dirtied. 1074 * @addr: the address (relative to the start of the region) being dirtied. 1075 * @size: size of the range being dirtied. 1076 */ 1077 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, 1078 hwaddr size); 1079 1080 /** 1081 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty 1082 * for a specified client. It clears them. 1083 * 1084 * Checks whether a range of bytes has been written to since the last 1085 * call to memory_region_reset_dirty() with the same @client. Dirty logging 1086 * must be enabled. 1087 * 1088 * @mr: the memory region being queried. 1089 * @addr: the address (relative to the start of the region) being queried. 1090 * @size: the size of the range being queried. 1091 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 1092 * %DIRTY_MEMORY_VGA. 1093 */ 1094 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr, 1095 hwaddr size, unsigned client); 1096 1097 /** 1098 * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty 1099 * bitmap and clear it. 1100 * 1101 * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and 1102 * returns the snapshot. The snapshot can then be used to query dirty 1103 * status, using memory_region_snapshot_get_dirty. Unlike 1104 * memory_region_test_and_clear_dirty this allows to query the same 1105 * page multiple times, which is especially useful for display updates 1106 * where the scanlines often are not page aligned. 1107 * 1108 * The dirty bitmap region which gets copyed into the snapshot (and 1109 * cleared afterwards) can be larger than requested. The boundaries 1110 * are rounded up/down so complete bitmap longs (covering 64 pages on 1111 * 64bit hosts) can be copied over into the bitmap snapshot. Which 1112 * isn't a problem for display updates as the extra pages are outside 1113 * the visible area, and in case the visible area changes a full 1114 * display redraw is due anyway. Should other use cases for this 1115 * function emerge we might have to revisit this implementation 1116 * detail. 1117 * 1118 * Use g_free to release DirtyBitmapSnapshot. 1119 * 1120 * @mr: the memory region being queried. 1121 * @addr: the address (relative to the start of the region) being queried. 1122 * @size: the size of the range being queried. 1123 * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA. 1124 */ 1125 DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr, 1126 hwaddr addr, 1127 hwaddr size, 1128 unsigned client); 1129 1130 /** 1131 * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty 1132 * in the specified dirty bitmap snapshot. 1133 * 1134 * @mr: the memory region being queried. 1135 * @snap: the dirty bitmap snapshot 1136 * @addr: the address (relative to the start of the region) being queried. 1137 * @size: the size of the range being queried. 1138 */ 1139 bool memory_region_snapshot_get_dirty(MemoryRegion *mr, 1140 DirtyBitmapSnapshot *snap, 1141 hwaddr addr, hwaddr size); 1142 1143 /** 1144 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with 1145 * any external TLBs (e.g. kvm) 1146 * 1147 * Flushes dirty information from accelerators such as kvm and vhost-net 1148 * and makes it available to users of the memory API. 1149 * 1150 * @mr: the region being flushed. 1151 */ 1152 void memory_region_sync_dirty_bitmap(MemoryRegion *mr); 1153 1154 /** 1155 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified 1156 * client. 1157 * 1158 * Marks a range of pages as no longer dirty. 1159 * 1160 * @mr: the region being updated. 1161 * @addr: the start of the subrange being cleaned. 1162 * @size: the size of the subrange being cleaned. 1163 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or 1164 * %DIRTY_MEMORY_VGA. 1165 */ 1166 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr, 1167 hwaddr size, unsigned client); 1168 1169 /** 1170 * memory_region_set_readonly: Turn a memory region read-only (or read-write) 1171 * 1172 * Allows a memory region to be marked as read-only (turning it into a ROM). 1173 * only useful on RAM regions. 1174 * 1175 * @mr: the region being updated. 1176 * @readonly: whether rhe region is to be ROM or RAM. 1177 */ 1178 void memory_region_set_readonly(MemoryRegion *mr, bool readonly); 1179 1180 /** 1181 * memory_region_rom_device_set_romd: enable/disable ROMD mode 1182 * 1183 * Allows a ROM device (initialized with memory_region_init_rom_device() to 1184 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the 1185 * device is mapped to guest memory and satisfies read access directly. 1186 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function. 1187 * Writes are always handled by the #MemoryRegion.write function. 1188 * 1189 * @mr: the memory region to be updated 1190 * @romd_mode: %true to put the region into ROMD mode 1191 */ 1192 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode); 1193 1194 /** 1195 * memory_region_set_coalescing: Enable memory coalescing for the region. 1196 * 1197 * Enabled writes to a region to be queued for later processing. MMIO ->write 1198 * callbacks may be delayed until a non-coalesced MMIO is issued. 1199 * Only useful for IO regions. Roughly similar to write-combining hardware. 1200 * 1201 * @mr: the memory region to be write coalesced 1202 */ 1203 void memory_region_set_coalescing(MemoryRegion *mr); 1204 1205 /** 1206 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of 1207 * a region. 1208 * 1209 * Like memory_region_set_coalescing(), but works on a sub-range of a region. 1210 * Multiple calls can be issued coalesced disjoint ranges. 1211 * 1212 * @mr: the memory region to be updated. 1213 * @offset: the start of the range within the region to be coalesced. 1214 * @size: the size of the subrange to be coalesced. 1215 */ 1216 void memory_region_add_coalescing(MemoryRegion *mr, 1217 hwaddr offset, 1218 uint64_t size); 1219 1220 /** 1221 * memory_region_clear_coalescing: Disable MMIO coalescing for the region. 1222 * 1223 * Disables any coalescing caused by memory_region_set_coalescing() or 1224 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory 1225 * hardware. 1226 * 1227 * @mr: the memory region to be updated. 1228 */ 1229 void memory_region_clear_coalescing(MemoryRegion *mr); 1230 1231 /** 1232 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before 1233 * accesses. 1234 * 1235 * Ensure that pending coalesced MMIO request are flushed before the memory 1236 * region is accessed. This property is automatically enabled for all regions 1237 * passed to memory_region_set_coalescing() and memory_region_add_coalescing(). 1238 * 1239 * @mr: the memory region to be updated. 1240 */ 1241 void memory_region_set_flush_coalesced(MemoryRegion *mr); 1242 1243 /** 1244 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before 1245 * accesses. 1246 * 1247 * Clear the automatic coalesced MMIO flushing enabled via 1248 * memory_region_set_flush_coalesced. Note that this service has no effect on 1249 * memory regions that have MMIO coalescing enabled for themselves. For them, 1250 * automatic flushing will stop once coalescing is disabled. 1251 * 1252 * @mr: the memory region to be updated. 1253 */ 1254 void memory_region_clear_flush_coalesced(MemoryRegion *mr); 1255 1256 /** 1257 * memory_region_set_global_locking: Declares the access processing requires 1258 * QEMU's global lock. 1259 * 1260 * When this is invoked, accesses to the memory region will be processed while 1261 * holding the global lock of QEMU. This is the default behavior of memory 1262 * regions. 1263 * 1264 * @mr: the memory region to be updated. 1265 */ 1266 void memory_region_set_global_locking(MemoryRegion *mr); 1267 1268 /** 1269 * memory_region_clear_global_locking: Declares that access processing does 1270 * not depend on the QEMU global lock. 1271 * 1272 * By clearing this property, accesses to the memory region will be processed 1273 * outside of QEMU's global lock (unless the lock is held on when issuing the 1274 * access request). In this case, the device model implementing the access 1275 * handlers is responsible for synchronization of concurrency. 1276 * 1277 * @mr: the memory region to be updated. 1278 */ 1279 void memory_region_clear_global_locking(MemoryRegion *mr); 1280 1281 /** 1282 * memory_region_add_eventfd: Request an eventfd to be triggered when a word 1283 * is written to a location. 1284 * 1285 * Marks a word in an IO region (initialized with memory_region_init_io()) 1286 * as a trigger for an eventfd event. The I/O callback will not be called. 1287 * The caller must be prepared to handle failure (that is, take the required 1288 * action if the callback _is_ called). 1289 * 1290 * @mr: the memory region being updated. 1291 * @addr: the address within @mr that is to be monitored 1292 * @size: the size of the access to trigger the eventfd 1293 * @match_data: whether to match against @data, instead of just @addr 1294 * @data: the data to match against the guest write 1295 * @fd: the eventfd to be triggered when @addr, @size, and @data all match. 1296 **/ 1297 void memory_region_add_eventfd(MemoryRegion *mr, 1298 hwaddr addr, 1299 unsigned size, 1300 bool match_data, 1301 uint64_t data, 1302 EventNotifier *e); 1303 1304 /** 1305 * memory_region_del_eventfd: Cancel an eventfd. 1306 * 1307 * Cancels an eventfd trigger requested by a previous 1308 * memory_region_add_eventfd() call. 1309 * 1310 * @mr: the memory region being updated. 1311 * @addr: the address within @mr that is to be monitored 1312 * @size: the size of the access to trigger the eventfd 1313 * @match_data: whether to match against @data, instead of just @addr 1314 * @data: the data to match against the guest write 1315 * @fd: the eventfd to be triggered when @addr, @size, and @data all match. 1316 */ 1317 void memory_region_del_eventfd(MemoryRegion *mr, 1318 hwaddr addr, 1319 unsigned size, 1320 bool match_data, 1321 uint64_t data, 1322 EventNotifier *e); 1323 1324 /** 1325 * memory_region_add_subregion: Add a subregion to a container. 1326 * 1327 * Adds a subregion at @offset. The subregion may not overlap with other 1328 * subregions (except for those explicitly marked as overlapping). A region 1329 * may only be added once as a subregion (unless removed with 1330 * memory_region_del_subregion()); use memory_region_init_alias() if you 1331 * want a region to be a subregion in multiple locations. 1332 * 1333 * @mr: the region to contain the new subregion; must be a container 1334 * initialized with memory_region_init(). 1335 * @offset: the offset relative to @mr where @subregion is added. 1336 * @subregion: the subregion to be added. 1337 */ 1338 void memory_region_add_subregion(MemoryRegion *mr, 1339 hwaddr offset, 1340 MemoryRegion *subregion); 1341 /** 1342 * memory_region_add_subregion_overlap: Add a subregion to a container 1343 * with overlap. 1344 * 1345 * Adds a subregion at @offset. The subregion may overlap with other 1346 * subregions. Conflicts are resolved by having a higher @priority hide a 1347 * lower @priority. Subregions without priority are taken as @priority 0. 1348 * A region may only be added once as a subregion (unless removed with 1349 * memory_region_del_subregion()); use memory_region_init_alias() if you 1350 * want a region to be a subregion in multiple locations. 1351 * 1352 * @mr: the region to contain the new subregion; must be a container 1353 * initialized with memory_region_init(). 1354 * @offset: the offset relative to @mr where @subregion is added. 1355 * @subregion: the subregion to be added. 1356 * @priority: used for resolving overlaps; highest priority wins. 1357 */ 1358 void memory_region_add_subregion_overlap(MemoryRegion *mr, 1359 hwaddr offset, 1360 MemoryRegion *subregion, 1361 int priority); 1362 1363 /** 1364 * memory_region_get_ram_addr: Get the ram address associated with a memory 1365 * region 1366 */ 1367 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr); 1368 1369 uint64_t memory_region_get_alignment(const MemoryRegion *mr); 1370 /** 1371 * memory_region_del_subregion: Remove a subregion. 1372 * 1373 * Removes a subregion from its container. 1374 * 1375 * @mr: the container to be updated. 1376 * @subregion: the region being removed; must be a current subregion of @mr. 1377 */ 1378 void memory_region_del_subregion(MemoryRegion *mr, 1379 MemoryRegion *subregion); 1380 1381 /* 1382 * memory_region_set_enabled: dynamically enable or disable a region 1383 * 1384 * Enables or disables a memory region. A disabled memory region 1385 * ignores all accesses to itself and its subregions. It does not 1386 * obscure sibling subregions with lower priority - it simply behaves as 1387 * if it was removed from the hierarchy. 1388 * 1389 * Regions default to being enabled. 1390 * 1391 * @mr: the region to be updated 1392 * @enabled: whether to enable or disable the region 1393 */ 1394 void memory_region_set_enabled(MemoryRegion *mr, bool enabled); 1395 1396 /* 1397 * memory_region_set_address: dynamically update the address of a region 1398 * 1399 * Dynamically updates the address of a region, relative to its container. 1400 * May be used on regions are currently part of a memory hierarchy. 1401 * 1402 * @mr: the region to be updated 1403 * @addr: new address, relative to container region 1404 */ 1405 void memory_region_set_address(MemoryRegion *mr, hwaddr addr); 1406 1407 /* 1408 * memory_region_set_size: dynamically update the size of a region. 1409 * 1410 * Dynamically updates the size of a region. 1411 * 1412 * @mr: the region to be updated 1413 * @size: used size of the region. 1414 */ 1415 void memory_region_set_size(MemoryRegion *mr, uint64_t size); 1416 1417 /* 1418 * memory_region_set_alias_offset: dynamically update a memory alias's offset 1419 * 1420 * Dynamically updates the offset into the target region that an alias points 1421 * to, as if the fourth argument to memory_region_init_alias() has changed. 1422 * 1423 * @mr: the #MemoryRegion to be updated; should be an alias. 1424 * @offset: the new offset into the target memory region 1425 */ 1426 void memory_region_set_alias_offset(MemoryRegion *mr, 1427 hwaddr offset); 1428 1429 /** 1430 * memory_region_present: checks if an address relative to a @container 1431 * translates into #MemoryRegion within @container 1432 * 1433 * Answer whether a #MemoryRegion within @container covers the address 1434 * @addr. 1435 * 1436 * @container: a #MemoryRegion within which @addr is a relative address 1437 * @addr: the area within @container to be searched 1438 */ 1439 bool memory_region_present(MemoryRegion *container, hwaddr addr); 1440 1441 /** 1442 * memory_region_is_mapped: returns true if #MemoryRegion is mapped 1443 * into any address space. 1444 * 1445 * @mr: a #MemoryRegion which should be checked if it's mapped 1446 */ 1447 bool memory_region_is_mapped(MemoryRegion *mr); 1448 1449 /** 1450 * memory_region_find: translate an address/size relative to a 1451 * MemoryRegion into a #MemoryRegionSection. 1452 * 1453 * Locates the first #MemoryRegion within @mr that overlaps the range 1454 * given by @addr and @size. 1455 * 1456 * Returns a #MemoryRegionSection that describes a contiguous overlap. 1457 * It will have the following characteristics: 1458 * .@size = 0 iff no overlap was found 1459 * .@mr is non-%NULL iff an overlap was found 1460 * 1461 * Remember that in the return value the @offset_within_region is 1462 * relative to the returned region (in the .@mr field), not to the 1463 * @mr argument. 1464 * 1465 * Similarly, the .@offset_within_address_space is relative to the 1466 * address space that contains both regions, the passed and the 1467 * returned one. However, in the special case where the @mr argument 1468 * has no container (and thus is the root of the address space), the 1469 * following will hold: 1470 * .@offset_within_address_space >= @addr 1471 * .@offset_within_address_space + .@size <= @addr + @size 1472 * 1473 * @mr: a MemoryRegion within which @addr is a relative address 1474 * @addr: start of the area within @as to be searched 1475 * @size: size of the area to be searched 1476 */ 1477 MemoryRegionSection memory_region_find(MemoryRegion *mr, 1478 hwaddr addr, uint64_t size); 1479 1480 /** 1481 * memory_global_dirty_log_sync: synchronize the dirty log for all memory 1482 * 1483 * Synchronizes the dirty page log for all address spaces. 1484 */ 1485 void memory_global_dirty_log_sync(void); 1486 1487 /** 1488 * memory_region_transaction_begin: Start a transaction. 1489 * 1490 * During a transaction, changes will be accumulated and made visible 1491 * only when the transaction ends (is committed). 1492 */ 1493 void memory_region_transaction_begin(void); 1494 1495 /** 1496 * memory_region_transaction_commit: Commit a transaction and make changes 1497 * visible to the guest. 1498 */ 1499 void memory_region_transaction_commit(void); 1500 1501 /** 1502 * memory_listener_register: register callbacks to be called when memory 1503 * sections are mapped or unmapped into an address 1504 * space 1505 * 1506 * @listener: an object containing the callbacks to be called 1507 * @filter: if non-%NULL, only regions in this address space will be observed 1508 */ 1509 void memory_listener_register(MemoryListener *listener, AddressSpace *filter); 1510 1511 /** 1512 * memory_listener_unregister: undo the effect of memory_listener_register() 1513 * 1514 * @listener: an object containing the callbacks to be removed 1515 */ 1516 void memory_listener_unregister(MemoryListener *listener); 1517 1518 /** 1519 * memory_global_dirty_log_start: begin dirty logging for all regions 1520 */ 1521 void memory_global_dirty_log_start(void); 1522 1523 /** 1524 * memory_global_dirty_log_stop: end dirty logging for all regions 1525 */ 1526 void memory_global_dirty_log_stop(void); 1527 1528 void mtree_info(fprintf_function mon_printf, void *f, bool flatview); 1529 1530 /** 1531 * memory_region_request_mmio_ptr: request a pointer to an mmio 1532 * MemoryRegion. If it is possible map a RAM MemoryRegion with this pointer. 1533 * When the device wants to invalidate the pointer it will call 1534 * memory_region_invalidate_mmio_ptr. 1535 * 1536 * @mr: #MemoryRegion to check 1537 * @addr: address within that region 1538 * 1539 * Returns true on success, false otherwise. 1540 */ 1541 bool memory_region_request_mmio_ptr(MemoryRegion *mr, hwaddr addr); 1542 1543 /** 1544 * memory_region_invalidate_mmio_ptr: invalidate the pointer to an mmio 1545 * previously requested. 1546 * In the end that means that if something wants to execute from this area it 1547 * will need to request the pointer again. 1548 * 1549 * @mr: #MemoryRegion associated to the pointer. 1550 * @addr: address within that region 1551 * @size: size of that area. 1552 */ 1553 void memory_region_invalidate_mmio_ptr(MemoryRegion *mr, hwaddr offset, 1554 unsigned size); 1555 1556 /** 1557 * memory_region_dispatch_read: perform a read directly to the specified 1558 * MemoryRegion. 1559 * 1560 * @mr: #MemoryRegion to access 1561 * @addr: address within that region 1562 * @pval: pointer to uint64_t which the data is written to 1563 * @size: size of the access in bytes 1564 * @attrs: memory transaction attributes to use for the access 1565 */ 1566 MemTxResult memory_region_dispatch_read(MemoryRegion *mr, 1567 hwaddr addr, 1568 uint64_t *pval, 1569 unsigned size, 1570 MemTxAttrs attrs); 1571 /** 1572 * memory_region_dispatch_write: perform a write directly to the specified 1573 * MemoryRegion. 1574 * 1575 * @mr: #MemoryRegion to access 1576 * @addr: address within that region 1577 * @data: data to write 1578 * @size: size of the access in bytes 1579 * @attrs: memory transaction attributes to use for the access 1580 */ 1581 MemTxResult memory_region_dispatch_write(MemoryRegion *mr, 1582 hwaddr addr, 1583 uint64_t data, 1584 unsigned size, 1585 MemTxAttrs attrs); 1586 1587 /** 1588 * address_space_init: initializes an address space 1589 * 1590 * @as: an uninitialized #AddressSpace 1591 * @root: a #MemoryRegion that routes addresses for the address space 1592 * @name: an address space name. The name is only used for debugging 1593 * output. 1594 */ 1595 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name); 1596 1597 /** 1598 * address_space_init_shareable: return an address space for a memory region, 1599 * creating it if it does not already exist 1600 * 1601 * @root: a #MemoryRegion that routes addresses for the address space 1602 * @name: an address space name. The name is only used for debugging 1603 * output. 1604 * 1605 * This function will return a pointer to an existing AddressSpace 1606 * which was initialized with the specified MemoryRegion, or it will 1607 * create and initialize one if it does not already exist. The ASes 1608 * are reference-counted, so the memory will be freed automatically 1609 * when the AddressSpace is destroyed via address_space_destroy. 1610 */ 1611 AddressSpace *address_space_init_shareable(MemoryRegion *root, 1612 const char *name); 1613 1614 /** 1615 * address_space_destroy: destroy an address space 1616 * 1617 * Releases all resources associated with an address space. After an address space 1618 * is destroyed, its root memory region (given by address_space_init()) may be destroyed 1619 * as well. 1620 * 1621 * @as: address space to be destroyed 1622 */ 1623 void address_space_destroy(AddressSpace *as); 1624 1625 /** 1626 * address_space_rw: read from or write to an address space. 1627 * 1628 * Return a MemTxResult indicating whether the operation succeeded 1629 * or failed (eg unassigned memory, device rejected the transaction, 1630 * IOMMU fault). 1631 * 1632 * @as: #AddressSpace to be accessed 1633 * @addr: address within that address space 1634 * @attrs: memory transaction attributes 1635 * @buf: buffer with the data transferred 1636 * @is_write: indicates the transfer direction 1637 */ 1638 MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, 1639 MemTxAttrs attrs, uint8_t *buf, 1640 int len, bool is_write); 1641 1642 /** 1643 * address_space_write: write to address space. 1644 * 1645 * Return a MemTxResult indicating whether the operation succeeded 1646 * or failed (eg unassigned memory, device rejected the transaction, 1647 * IOMMU fault). 1648 * 1649 * @as: #AddressSpace to be accessed 1650 * @addr: address within that address space 1651 * @attrs: memory transaction attributes 1652 * @buf: buffer with the data transferred 1653 */ 1654 MemTxResult address_space_write(AddressSpace *as, hwaddr addr, 1655 MemTxAttrs attrs, 1656 const uint8_t *buf, int len); 1657 1658 /* address_space_ld*: load from an address space 1659 * address_space_st*: store to an address space 1660 * 1661 * These functions perform a load or store of the byte, word, 1662 * longword or quad to the specified address within the AddressSpace. 1663 * The _le suffixed functions treat the data as little endian; 1664 * _be indicates big endian; no suffix indicates "same endianness 1665 * as guest CPU". 1666 * 1667 * The "guest CPU endianness" accessors are deprecated for use outside 1668 * target-* code; devices should be CPU-agnostic and use either the LE 1669 * or the BE accessors. 1670 * 1671 * @as #AddressSpace to be accessed 1672 * @addr: address within that address space 1673 * @val: data value, for stores 1674 * @attrs: memory transaction attributes 1675 * @result: location to write the success/failure of the transaction; 1676 * if NULL, this information is discarded 1677 */ 1678 uint32_t address_space_ldub(AddressSpace *as, hwaddr addr, 1679 MemTxAttrs attrs, MemTxResult *result); 1680 uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr, 1681 MemTxAttrs attrs, MemTxResult *result); 1682 uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr, 1683 MemTxAttrs attrs, MemTxResult *result); 1684 uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr, 1685 MemTxAttrs attrs, MemTxResult *result); 1686 uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr, 1687 MemTxAttrs attrs, MemTxResult *result); 1688 uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr, 1689 MemTxAttrs attrs, MemTxResult *result); 1690 uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr, 1691 MemTxAttrs attrs, MemTxResult *result); 1692 void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val, 1693 MemTxAttrs attrs, MemTxResult *result); 1694 void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val, 1695 MemTxAttrs attrs, MemTxResult *result); 1696 void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val, 1697 MemTxAttrs attrs, MemTxResult *result); 1698 void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val, 1699 MemTxAttrs attrs, MemTxResult *result); 1700 void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val, 1701 MemTxAttrs attrs, MemTxResult *result); 1702 void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val, 1703 MemTxAttrs attrs, MemTxResult *result); 1704 void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val, 1705 MemTxAttrs attrs, MemTxResult *result); 1706 1707 uint32_t ldub_phys(AddressSpace *as, hwaddr addr); 1708 uint32_t lduw_le_phys(AddressSpace *as, hwaddr addr); 1709 uint32_t lduw_be_phys(AddressSpace *as, hwaddr addr); 1710 uint32_t ldl_le_phys(AddressSpace *as, hwaddr addr); 1711 uint32_t ldl_be_phys(AddressSpace *as, hwaddr addr); 1712 uint64_t ldq_le_phys(AddressSpace *as, hwaddr addr); 1713 uint64_t ldq_be_phys(AddressSpace *as, hwaddr addr); 1714 void stb_phys(AddressSpace *as, hwaddr addr, uint32_t val); 1715 void stw_le_phys(AddressSpace *as, hwaddr addr, uint32_t val); 1716 void stw_be_phys(AddressSpace *as, hwaddr addr, uint32_t val); 1717 void stl_le_phys(AddressSpace *as, hwaddr addr, uint32_t val); 1718 void stl_be_phys(AddressSpace *as, hwaddr addr, uint32_t val); 1719 void stq_le_phys(AddressSpace *as, hwaddr addr, uint64_t val); 1720 void stq_be_phys(AddressSpace *as, hwaddr addr, uint64_t val); 1721 1722 struct MemoryRegionCache { 1723 hwaddr xlat; 1724 hwaddr len; 1725 AddressSpace *as; 1726 }; 1727 1728 #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .as = NULL }) 1729 1730 /* address_space_cache_init: prepare for repeated access to a physical 1731 * memory region 1732 * 1733 * @cache: #MemoryRegionCache to be filled 1734 * @as: #AddressSpace to be accessed 1735 * @addr: address within that address space 1736 * @len: length of buffer 1737 * @is_write: indicates the transfer direction 1738 * 1739 * Will only work with RAM, and may map a subset of the requested range by 1740 * returning a value that is less than @len. On failure, return a negative 1741 * errno value. 1742 * 1743 * Because it only works with RAM, this function can be used for 1744 * read-modify-write operations. In this case, is_write should be %true. 1745 * 1746 * Note that addresses passed to the address_space_*_cached functions 1747 * are relative to @addr. 1748 */ 1749 int64_t address_space_cache_init(MemoryRegionCache *cache, 1750 AddressSpace *as, 1751 hwaddr addr, 1752 hwaddr len, 1753 bool is_write); 1754 1755 /** 1756 * address_space_cache_invalidate: complete a write to a #MemoryRegionCache 1757 * 1758 * @cache: The #MemoryRegionCache to operate on. 1759 * @addr: The first physical address that was written, relative to the 1760 * address that was passed to @address_space_cache_init. 1761 * @access_len: The number of bytes that were written starting at @addr. 1762 */ 1763 void address_space_cache_invalidate(MemoryRegionCache *cache, 1764 hwaddr addr, 1765 hwaddr access_len); 1766 1767 /** 1768 * address_space_cache_destroy: free a #MemoryRegionCache 1769 * 1770 * @cache: The #MemoryRegionCache whose memory should be released. 1771 */ 1772 void address_space_cache_destroy(MemoryRegionCache *cache); 1773 1774 /* address_space_ld*_cached: load from a cached #MemoryRegion 1775 * address_space_st*_cached: store into a cached #MemoryRegion 1776 * 1777 * These functions perform a load or store of the byte, word, 1778 * longword or quad to the specified address. The address is 1779 * a physical address in the AddressSpace, but it must lie within 1780 * a #MemoryRegion that was mapped with address_space_cache_init. 1781 * 1782 * The _le suffixed functions treat the data as little endian; 1783 * _be indicates big endian; no suffix indicates "same endianness 1784 * as guest CPU". 1785 * 1786 * The "guest CPU endianness" accessors are deprecated for use outside 1787 * target-* code; devices should be CPU-agnostic and use either the LE 1788 * or the BE accessors. 1789 * 1790 * @cache: previously initialized #MemoryRegionCache to be accessed 1791 * @addr: address within the address space 1792 * @val: data value, for stores 1793 * @attrs: memory transaction attributes 1794 * @result: location to write the success/failure of the transaction; 1795 * if NULL, this information is discarded 1796 */ 1797 uint32_t address_space_ldub_cached(MemoryRegionCache *cache, hwaddr addr, 1798 MemTxAttrs attrs, MemTxResult *result); 1799 uint32_t address_space_lduw_le_cached(MemoryRegionCache *cache, hwaddr addr, 1800 MemTxAttrs attrs, MemTxResult *result); 1801 uint32_t address_space_lduw_be_cached(MemoryRegionCache *cache, hwaddr addr, 1802 MemTxAttrs attrs, MemTxResult *result); 1803 uint32_t address_space_ldl_le_cached(MemoryRegionCache *cache, hwaddr addr, 1804 MemTxAttrs attrs, MemTxResult *result); 1805 uint32_t address_space_ldl_be_cached(MemoryRegionCache *cache, hwaddr addr, 1806 MemTxAttrs attrs, MemTxResult *result); 1807 uint64_t address_space_ldq_le_cached(MemoryRegionCache *cache, hwaddr addr, 1808 MemTxAttrs attrs, MemTxResult *result); 1809 uint64_t address_space_ldq_be_cached(MemoryRegionCache *cache, hwaddr addr, 1810 MemTxAttrs attrs, MemTxResult *result); 1811 void address_space_stb_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val, 1812 MemTxAttrs attrs, MemTxResult *result); 1813 void address_space_stw_le_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val, 1814 MemTxAttrs attrs, MemTxResult *result); 1815 void address_space_stw_be_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val, 1816 MemTxAttrs attrs, MemTxResult *result); 1817 void address_space_stl_le_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val, 1818 MemTxAttrs attrs, MemTxResult *result); 1819 void address_space_stl_be_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val, 1820 MemTxAttrs attrs, MemTxResult *result); 1821 void address_space_stq_le_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val, 1822 MemTxAttrs attrs, MemTxResult *result); 1823 void address_space_stq_be_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val, 1824 MemTxAttrs attrs, MemTxResult *result); 1825 1826 uint32_t ldub_phys_cached(MemoryRegionCache *cache, hwaddr addr); 1827 uint32_t lduw_le_phys_cached(MemoryRegionCache *cache, hwaddr addr); 1828 uint32_t lduw_be_phys_cached(MemoryRegionCache *cache, hwaddr addr); 1829 uint32_t ldl_le_phys_cached(MemoryRegionCache *cache, hwaddr addr); 1830 uint32_t ldl_be_phys_cached(MemoryRegionCache *cache, hwaddr addr); 1831 uint64_t ldq_le_phys_cached(MemoryRegionCache *cache, hwaddr addr); 1832 uint64_t ldq_be_phys_cached(MemoryRegionCache *cache, hwaddr addr); 1833 void stb_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val); 1834 void stw_le_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val); 1835 void stw_be_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val); 1836 void stl_le_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val); 1837 void stl_be_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint32_t val); 1838 void stq_le_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val); 1839 void stq_be_phys_cached(MemoryRegionCache *cache, hwaddr addr, uint64_t val); 1840 /* address_space_get_iotlb_entry: translate an address into an IOTLB 1841 * entry. Should be called from an RCU critical section. 1842 */ 1843 IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr, 1844 bool is_write); 1845 1846 /* address_space_translate: translate an address range into an address space 1847 * into a MemoryRegion and an address range into that section. Should be 1848 * called from an RCU critical section, to avoid that the last reference 1849 * to the returned region disappears after address_space_translate returns. 1850 * 1851 * @as: #AddressSpace to be accessed 1852 * @addr: address within that address space 1853 * @xlat: pointer to address within the returned memory region section's 1854 * #MemoryRegion. 1855 * @len: pointer to length 1856 * @is_write: indicates the transfer direction 1857 */ 1858 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr, 1859 hwaddr *xlat, hwaddr *len, 1860 bool is_write); 1861 1862 /* address_space_access_valid: check for validity of accessing an address 1863 * space range 1864 * 1865 * Check whether memory is assigned to the given address space range, and 1866 * access is permitted by any IOMMU regions that are active for the address 1867 * space. 1868 * 1869 * For now, addr and len should be aligned to a page size. This limitation 1870 * will be lifted in the future. 1871 * 1872 * @as: #AddressSpace to be accessed 1873 * @addr: address within that address space 1874 * @len: length of the area to be checked 1875 * @is_write: indicates the transfer direction 1876 */ 1877 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write); 1878 1879 /* address_space_map: map a physical memory region into a host virtual address 1880 * 1881 * May map a subset of the requested range, given by and returned in @plen. 1882 * May return %NULL if resources needed to perform the mapping are exhausted. 1883 * Use only for reads OR writes - not for read-modify-write operations. 1884 * Use cpu_register_map_client() to know when retrying the map operation is 1885 * likely to succeed. 1886 * 1887 * @as: #AddressSpace to be accessed 1888 * @addr: address within that address space 1889 * @plen: pointer to length of buffer; updated on return 1890 * @is_write: indicates the transfer direction 1891 */ 1892 void *address_space_map(AddressSpace *as, hwaddr addr, 1893 hwaddr *plen, bool is_write); 1894 1895 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map() 1896 * 1897 * Will also mark the memory as dirty if @is_write == %true. @access_len gives 1898 * the amount of memory that was actually read or written by the caller. 1899 * 1900 * @as: #AddressSpace used 1901 * @addr: address within that address space 1902 * @len: buffer length as returned by address_space_map() 1903 * @access_len: amount of data actually transferred 1904 * @is_write: indicates the transfer direction 1905 */ 1906 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, 1907 int is_write, hwaddr access_len); 1908 1909 1910 /* Internal functions, part of the implementation of address_space_read. */ 1911 MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr, 1912 MemTxAttrs attrs, uint8_t *buf, 1913 int len, hwaddr addr1, hwaddr l, 1914 MemoryRegion *mr); 1915 MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr, 1916 MemTxAttrs attrs, uint8_t *buf, int len); 1917 void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr); 1918 1919 static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) 1920 { 1921 if (is_write) { 1922 return memory_region_is_ram(mr) && 1923 !mr->readonly && !memory_region_is_ram_device(mr); 1924 } else { 1925 return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) || 1926 memory_region_is_romd(mr); 1927 } 1928 } 1929 1930 /** 1931 * address_space_read: read from an address space. 1932 * 1933 * Return a MemTxResult indicating whether the operation succeeded 1934 * or failed (eg unassigned memory, device rejected the transaction, 1935 * IOMMU fault). 1936 * 1937 * @as: #AddressSpace to be accessed 1938 * @addr: address within that address space 1939 * @attrs: memory transaction attributes 1940 * @buf: buffer with the data transferred 1941 */ 1942 static inline __attribute__((__always_inline__)) 1943 MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs, 1944 uint8_t *buf, int len) 1945 { 1946 MemTxResult result = MEMTX_OK; 1947 hwaddr l, addr1; 1948 void *ptr; 1949 MemoryRegion *mr; 1950 1951 if (__builtin_constant_p(len)) { 1952 if (len) { 1953 rcu_read_lock(); 1954 l = len; 1955 mr = address_space_translate(as, addr, &addr1, &l, false); 1956 if (len == l && memory_access_is_direct(mr, false)) { 1957 ptr = qemu_map_ram_ptr(mr->ram_block, addr1); 1958 memcpy(buf, ptr, len); 1959 } else { 1960 result = address_space_read_continue(as, addr, attrs, buf, len, 1961 addr1, l, mr); 1962 } 1963 rcu_read_unlock(); 1964 } 1965 } else { 1966 result = address_space_read_full(as, addr, attrs, buf, len); 1967 } 1968 return result; 1969 } 1970 1971 /** 1972 * address_space_read_cached: read from a cached RAM region 1973 * 1974 * @cache: Cached region to be addressed 1975 * @addr: address relative to the base of the RAM region 1976 * @buf: buffer with the data transferred 1977 * @len: length of the data transferred 1978 */ 1979 static inline void 1980 address_space_read_cached(MemoryRegionCache *cache, hwaddr addr, 1981 void *buf, int len) 1982 { 1983 assert(addr < cache->len && len <= cache->len - addr); 1984 address_space_read(cache->as, cache->xlat + addr, MEMTXATTRS_UNSPECIFIED, buf, len); 1985 } 1986 1987 /** 1988 * address_space_write_cached: write to a cached RAM region 1989 * 1990 * @cache: Cached region to be addressed 1991 * @addr: address relative to the base of the RAM region 1992 * @buf: buffer with the data transferred 1993 * @len: length of the data transferred 1994 */ 1995 static inline void 1996 address_space_write_cached(MemoryRegionCache *cache, hwaddr addr, 1997 void *buf, int len) 1998 { 1999 assert(addr < cache->len && len <= cache->len - addr); 2000 address_space_write(cache->as, cache->xlat + addr, MEMTXATTRS_UNSPECIFIED, buf, len); 2001 } 2002 2003 #endif 2004 2005 #endif 2006