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