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