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