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