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