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#define DIRTY_MEMORY_VGA       0
  20#define DIRTY_MEMORY_CODE      1
  21#define DIRTY_MEMORY_MIGRATION 2
  22#define DIRTY_MEMORY_NUM       3        /* num of dirty bits */
  23
  24#include "exec/cpu-common.h"
  25#ifndef CONFIG_USER_ONLY
  26#include "exec/hwaddr.h"
  27#endif
  28#include "exec/memattrs.h"
  29#include "qemu/queue.h"
  30#include "qemu/int128.h"
  31#include "qemu/notify.h"
  32#include "qom/object.h"
  33#include "qemu/rcu.h"
  34
  35extern const char *machine_path;
  36
  37#define MAX_PHYS_ADDR_SPACE_BITS 62
  38#define MAX_PHYS_ADDR            (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
  39
  40#define TYPE_MEMORY_REGION "qemu:memory-region"
  41#define MEMORY_REGION(obj) \
  42        OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
  43
  44#define TYPE_MEMORY_TRANSACTION_ATTR "qemu:memory-transaction-attr"
  45#define MEMORY_TRANSACTION_ATTR(obj) \
  46        OBJECT_CHECK(MemTxAttrs, (obj), TYPE_MEMORY_TRANSACTION_ATTR)
  47
  48typedef struct MemoryRegionOps MemoryRegionOps;
  49typedef struct MemoryRegionMmio MemoryRegionMmio;
  50
  51typedef struct MemoryTransaction
  52{
  53    union {
  54        uint64_t *p64;
  55        uint64_t u64;
  56        uint32_t u32;
  57        uint16_t u16;
  58        uint8_t  u8;
  59    } data;
  60    bool rw;
  61    hwaddr addr;
  62    unsigned int size;
  63    MemTxAttrs attr;
  64    void *opaque;
  65} MemoryTransaction;
  66
  67struct MemoryRegionMmio {
  68    CPUReadMemoryFunc *read[3];
  69    CPUWriteMemoryFunc *write[3];
  70};
  71
  72typedef struct IOMMUTLBEntry IOMMUTLBEntry;
  73
  74/* See address_space_translate: bit 0 is read, bit 1 is write.  */
  75typedef enum {
  76    IOMMU_NONE = 0,
  77    IOMMU_RO   = 1,
  78    IOMMU_WO   = 2,
  79    IOMMU_RW   = 3,
  80} IOMMUAccessFlags;
  81
  82struct IOMMUTLBEntry {
  83    AddressSpace    *target_as;
  84    hwaddr           iova;
  85    hwaddr           translated_addr;
  86    hwaddr           addr_mask;  /* 0xfff = 4k translation */
  87    IOMMUAccessFlags perm;
  88};
  89
  90/* New-style MMIO accessors can indicate that the transaction failed.
  91 * A zero (MEMTX_OK) response means success; anything else is a failure
  92 * of some kind. The memory subsystem will bitwise-OR together results
  93 * if it is synthesizing an operation from multiple smaller accesses.
  94 */
  95#define MEMTX_OK 0
  96#define MEMTX_ERROR             (1U << 0) /* device returned an error */
  97#define MEMTX_DECODE_ERROR      (1U << 1) /* nothing at that address */
  98typedef uint32_t MemTxResult;
  99
 100/*
 101 * Memory region callbacks
 102 */
 103struct MemoryRegionOps {
 104    /* FIXME: Remove */
 105    void (*access)(MemoryTransaction *tr);
 106
 107    /* Read from the memory region. @addr is relative to @mr; @size is
 108     * in bytes. */
 109    uint64_t (*read)(void *opaque,
 110                     hwaddr addr,
 111                     unsigned size);
 112    /* Write to the memory region. @addr is relative to @mr; @size is
 113     * in bytes. */
 114    void (*write)(void *opaque,
 115                  hwaddr addr,
 116                  uint64_t data,
 117                  unsigned size);
 118
 119    MemTxResult (*read_with_attrs)(void *opaque,
 120                                   hwaddr addr,
 121                                   uint64_t *data,
 122                                   unsigned size,
 123                                   MemTxAttrs attrs);
 124    MemTxResult (*write_with_attrs)(void *opaque,
 125                                    hwaddr addr,
 126                                    uint64_t data,
 127                                    unsigned size,
 128                                    MemTxAttrs attrs);
 129
 130    enum device_endian endianness;
 131    /* Guest-visible constraints: */
 132    struct {
 133        /* If nonzero, specify bounds on access sizes beyond which a machine
 134         * check is thrown.
 135         */
 136        unsigned min_access_size;
 137        unsigned max_access_size;
 138        /* If true, unaligned accesses are supported.  Otherwise unaligned
 139         * accesses throw machine checks.
 140         */
 141         bool unaligned;
 142        /*
 143         * If present, and returns #false, the transaction is not accepted
 144         * by the device (and results in machine dependent behaviour such
 145         * as a machine check exception).
 146         */
 147        bool (*accepts)(void *opaque, hwaddr addr,
 148                        unsigned size, bool is_write);
 149
 150        /* The same function as accepts except is passed a MemoryTransaction */
 151        bool (*accepts_tr)(MemoryTransaction *tr);
 152    } valid;
 153    /* Internal implementation constraints: */
 154    struct {
 155        /* If nonzero, specifies the minimum size implemented.  Smaller sizes
 156         * will be rounded upwards and a partial result will be returned.
 157         */
 158        unsigned min_access_size;
 159        /* If nonzero, specifies the maximum size implemented.  Larger sizes
 160         * will be done as a series of accesses with smaller sizes.
 161         */
 162        unsigned max_access_size;
 163        /* If true, unaligned accesses are supported.  Otherwise all accesses
 164         * are converted to (possibly multiple) naturally aligned accesses.
 165         */
 166        bool unaligned;
 167    } impl;
 168
 169    /* If .read and .write are not present, old_mmio may be used for
 170     * backwards compatibility with old mmio registration
 171     */
 172    const MemoryRegionMmio old_mmio;
 173};
 174
 175typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
 176
 177struct MemoryRegionIOMMUOps {
 178    /* Return a TLB entry that contains a given address. */
 179    IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
 180    IOMMUTLBEntry (*translate_attr)(MemoryRegion *iommu, hwaddr addr,
 181                                    bool is_write, MemTxAttrs *attr);
 182};
 183
 184typedef struct CoalescedMemoryRange CoalescedMemoryRange;
 185typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
 186
 187struct MemoryRegion {
 188    Object parent_obj;
 189
 190    /* All fields are private - violators will be prosecuted */
 191
 192    /* The following fields should fit in a cache line */
 193    bool romd_mode;
 194    uint8_t ram;
 195    bool subpage;
 196    bool readonly; /* For RAM regions */
 197    bool rom_device;
 198    bool flush_coalesced_mmio;
 199    bool global_locking;
 200    uint8_t dirty_log_mask;
 201    RAMBlock *ram_block;
 202    Object *owner;
 203    const MemoryRegionIOMMUOps *iommu_ops;
 204
 205    const MemoryRegionOps *ops;
 206    void *opaque;
 207    MemoryRegion *container;
 208    Int128 size;
 209    hwaddr addr;
 210    void (*destructor)(MemoryRegion *mr);
 211    uint64_t align;
 212    bool terminates;
 213    bool skip_dump;
 214    bool enabled;
 215    bool warning_printed; /* For reservations */
 216    uint8_t vga_logging_count;
 217    MemoryRegion *alias;
 218    hwaddr alias_offset;
 219    int32_t priority;
 220    bool may_overlap;
 221    QTAILQ_HEAD(subregions, MemoryRegion) subregions;
 222    QTAILQ_ENTRY(MemoryRegion) subregions_link;
 223    QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
 224    const char *name;
 225    unsigned ioeventfd_nb;
 226    MemoryRegionIoeventfd *ioeventfds;
 227    NotifierList iommu_notify;
 228};
 229
 230/**
 231 * MemoryListener: callbacks structure for updates to the physical memory map
 232 *
 233 * Allows a component to adjust to changes in the guest-visible memory map.
 234 * Use with memory_listener_register() and memory_listener_unregister().
 235 */
 236struct MemoryListener {
 237    void (*begin)(MemoryListener *listener);
 238    void (*commit)(MemoryListener *listener);
 239    void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
 240    void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
 241    void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
 242    void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
 243                      int old, int new);
 244    void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
 245                     int old, int new);
 246    void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
 247    void (*log_global_start)(MemoryListener *listener);
 248    void (*log_global_stop)(MemoryListener *listener);
 249    void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
 250                        bool match_data, uint64_t data, EventNotifier *e);
 251    void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
 252                        bool match_data, uint64_t data, EventNotifier *e);
 253    void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
 254                               hwaddr addr, hwaddr len);
 255    void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
 256                               hwaddr addr, hwaddr len);
 257    /* Lower = earlier (during add), later (during del) */
 258    unsigned priority;
 259    AddressSpace *address_space_filter;
 260    QTAILQ_ENTRY(MemoryListener) link;
 261};
 262
 263/**
 264 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
 265 */
 266struct AddressSpace {
 267    /* All fields are private. */
 268    struct rcu_head rcu;
 269    char *name;
 270    MemoryRegion *root;
 271    int ref_count;
 272    bool malloced;
 273
 274    /* Accessed via RCU.  */
 275    struct FlatView *current_map;
 276
 277    int ioeventfd_nb;
 278    struct MemoryRegionIoeventfd *ioeventfds;
 279    struct AddressSpaceDispatch *dispatch;
 280    struct AddressSpaceDispatch *next_dispatch;
 281    MemoryListener dispatch_listener;
 282
 283    QTAILQ_ENTRY(AddressSpace) address_spaces_link;
 284};
 285
 286/**
 287 * MemoryRegionSection: describes a fragment of a #MemoryRegion
 288 *
 289 * @mr: the region, or %NULL if empty
 290 * @address_space: the address space the region is mapped in
 291 * @offset_within_region: the beginning of the section, relative to @mr's start
 292 * @size: the size of the section; will not exceed @mr's boundaries
 293 * @offset_within_address_space: the address of the first byte of the section
 294 *     relative to the region's address space
 295 * @readonly: writes to this section are ignored
 296 */
 297struct MemoryRegionSection {
 298    MemoryRegion *mr;
 299    AddressSpace *address_space;
 300    hwaddr offset_within_region;
 301    Int128 size;
 302    hwaddr offset_within_address_space;
 303    bool readonly;
 304};
 305
 306/**
 307 * memory_region_init: Initialize a memory region
 308 *
 309 * The region typically acts as a container for other memory regions.  Use
 310 * memory_region_add_subregion() to add subregions.
 311 *
 312 * @mr: the #MemoryRegion to be initialized
 313 * @owner: the object that tracks the region's reference count
 314 * @name: used for debugging; not visible to the user or ABI
 315 * @size: size of the region; any subregions beyond this size will be clipped
 316 */
 317void memory_region_init(MemoryRegion *mr,
 318                        struct Object *owner,
 319                        const char *name,
 320                        uint64_t size);
 321
 322/**
 323 * memory_region_ref: Add 1 to a memory region's reference count
 324 *
 325 * Whenever memory regions are accessed outside the BQL, they need to be
 326 * preserved against hot-unplug.  MemoryRegions actually do not have their
 327 * own reference count; they piggyback on a QOM object, their "owner".
 328 * This function adds a reference to the owner.
 329 *
 330 * All MemoryRegions must have an owner if they can disappear, even if the
 331 * device they belong to operates exclusively under the BQL.  This is because
 332 * the region could be returned at any time by memory_region_find, and this
 333 * is usually under guest control.
 334 *
 335 * @mr: the #MemoryRegion
 336 */
 337void memory_region_ref(MemoryRegion *mr);
 338
 339/**
 340 * memory_region_unref: Remove 1 to a memory region's reference count
 341 *
 342 * Whenever memory regions are accessed outside the BQL, they need to be
 343 * preserved against hot-unplug.  MemoryRegions actually do not have their
 344 * own reference count; they piggyback on a QOM object, their "owner".
 345 * This function removes a reference to the owner and possibly destroys it.
 346 *
 347 * @mr: the #MemoryRegion
 348 */
 349void memory_region_unref(MemoryRegion *mr);
 350
 351/**
 352 * memory_region_init_io: Initialize an I/O memory region.
 353 *
 354 * Accesses into the region will cause the callbacks in @ops to be called.
 355 * if @size is nonzero, subregions will be clipped to @size.
 356 *
 357 * @mr: the #MemoryRegion to be initialized.
 358 * @owner: the object that tracks the region's reference count
 359 * @ops: a structure containing read and write callbacks to be used when
 360 *       I/O is performed on the region.
 361 * @opaque: passed to the read and write callbacks of the @ops structure.
 362 * @name: used for debugging; not visible to the user or ABI
 363 * @size: size of the region.
 364 */
 365void memory_region_init_io(MemoryRegion *mr,
 366                           struct Object *owner,
 367                           const MemoryRegionOps *ops,
 368                           void *opaque,
 369                           const char *name,
 370                           uint64_t size);
 371
 372/**
 373 * memory_region_init_ram:  Initialize RAM memory region.  Accesses into the
 374 *                          region will modify memory directly.
 375 *
 376 * @mr: the #MemoryRegion to be initialized.
 377 * @owner: the object that tracks the region's reference count
 378 * @name: the name of the region.
 379 * @size: size of the region.
 380 * @errp: pointer to Error*, to store an error if it happens.
 381 */
 382void memory_region_init_ram(MemoryRegion *mr,
 383                            struct Object *owner,
 384                            const char *name,
 385                            uint64_t size,
 386                            Error **errp);
 387
 388/**
 389 * memory_region_init_resizeable_ram:  Initialize memory region with resizeable
 390 *                                     RAM.  Accesses into the region will
 391 *                                     modify memory directly.  Only an initial
 392 *                                     portion of this RAM is actually used.
 393 *                                     The used size can change across reboots.
 394 *
 395 * @mr: the #MemoryRegion to be initialized.
 396 * @owner: the object that tracks the region's reference count
 397 * @name: the name of the region.
 398 * @size: used size of the region.
 399 * @max_size: max size of the region.
 400 * @resized: callback to notify owner about used size change.
 401 * @errp: pointer to Error*, to store an error if it happens.
 402 */
 403void memory_region_init_resizeable_ram(MemoryRegion *mr,
 404                                       struct Object *owner,
 405                                       const char *name,
 406                                       uint64_t size,
 407                                       uint64_t max_size,
 408                                       void (*resized)(const char*,
 409                                                       uint64_t length,
 410                                                       void *host),
 411                                       Error **errp);
 412#ifdef __linux__
 413/**
 414 * memory_region_init_ram_from_file:  Initialize RAM memory region with a
 415 *                                    mmap-ed backend.
 416 *
 417 * @mr: the #MemoryRegion to be initialized.
 418 * @owner: the object that tracks the region's reference count
 419 * @name: the name of the region.
 420 * @size: size of the region.
 421 * @share: %true if memory must be mmaped with the MAP_SHARED flag
 422 * @path: the path in which to allocate the RAM.
 423 * @errp: pointer to Error*, to store an error if it happens.
 424 */
 425void memory_region_init_ram_from_file(MemoryRegion *mr,
 426                                      struct Object *owner,
 427                                      const char *name,
 428                                      uint64_t size,
 429                                      bool share,
 430                                      const char *path,
 431                                      Error **errp);
 432#endif
 433
 434/**
 435 * memory_region_init_ram_ptr:  Initialize RAM memory region from a
 436 *                              user-provided pointer.  Accesses into the
 437 *                              region will modify memory directly.
 438 *
 439 * @mr: the #MemoryRegion to be initialized.
 440 * @owner: the object that tracks the region's reference count
 441 * @name: the name of the region.
 442 * @size: size of the region.
 443 * @ptr: memory to be mapped; must contain at least @size bytes.
 444 */
 445void memory_region_init_ram_ptr(MemoryRegion *mr,
 446                                struct Object *owner,
 447                                const char *name,
 448                                uint64_t size,
 449                                void *ptr);
 450
 451/**
 452 * memory_region_init_alias: Initialize a memory region that aliases all or a
 453 *                           part of another memory region.
 454 *
 455 * @mr: the #MemoryRegion to be initialized.
 456 * @owner: the object that tracks the region's reference count
 457 * @name: used for debugging; not visible to the user or ABI
 458 * @orig: the region to be referenced; @mr will be equivalent to
 459 *        @orig between @offset and @offset + @size - 1.
 460 * @offset: start of the section in @orig to be referenced.
 461 * @size: size of the region.
 462 */
 463void memory_region_init_alias(MemoryRegion *mr,
 464                              struct Object *owner,
 465                              const char *name,
 466                              MemoryRegion *orig,
 467                              hwaddr offset,
 468                              uint64_t size);
 469
 470/**
 471 * memory_region_init_rom_device:  Initialize a ROM memory region.  Writes are
 472 *                                 handled via callbacks.
 473 *
 474 * If NULL callbacks pointer is given, then I/O space is not supposed to be
 475 * handled by QEMU itself. Any access via the memory API will cause an abort().
 476 *
 477 * @mr: the #MemoryRegion to be initialized.
 478 * @owner: the object that tracks the region's reference count
 479 * @ops: callbacks for write access handling.
 480 * @name: the name of the region.
 481 * @size: size of the region.
 482 * @errp: pointer to Error*, to store an error if it happens.
 483 */
 484void memory_region_init_rom_device(MemoryRegion *mr,
 485                                   struct Object *owner,
 486                                   const MemoryRegionOps *ops,
 487                                   void *opaque,
 488                                   const char *name,
 489                                   uint64_t size,
 490                                   Error **errp);
 491
 492/**
 493 * memory_region_init_reservation: Initialize a memory region that reserves
 494 *                                 I/O space.
 495 *
 496 * A reservation region primariy serves debugging purposes.  It claims I/O
 497 * space that is not supposed to be handled by QEMU itself.  Any access via
 498 * the memory API will cause an abort().
 499 * This function is deprecated. Use memory_region_init_io() with NULL
 500 * callbacks instead.
 501 *
 502 * @mr: the #MemoryRegion to be initialized
 503 * @owner: the object that tracks the region's reference count
 504 * @name: used for debugging; not visible to the user or ABI
 505 * @size: size of the region.
 506 */
 507static inline void memory_region_init_reservation(MemoryRegion *mr,
 508                                    Object *owner,
 509                                    const char *name,
 510                                    uint64_t size)
 511{
 512    memory_region_init_io(mr, owner, NULL, mr, name, size);
 513}
 514
 515/**
 516 * memory_region_init_iommu: Initialize a memory region that translates
 517 * addresses
 518 *
 519 * An IOMMU region translates addresses and forwards accesses to a target
 520 * memory region.
 521 *
 522 * @mr: the #MemoryRegion to be initialized
 523 * @owner: the object that tracks the region's reference count
 524 * @ops: a function that translates addresses into the @target region
 525 * @name: used for debugging; not visible to the user or ABI
 526 * @size: size of the region.
 527 */
 528void memory_region_init_iommu(MemoryRegion *mr,
 529                              struct Object *owner,
 530                              const MemoryRegionIOMMUOps *ops,
 531                              const char *name,
 532                              uint64_t size);
 533
 534/**
 535 * memory_region_owner: get a memory region's owner.
 536 *
 537 * @mr: the memory region being queried.
 538 */
 539struct Object *memory_region_owner(MemoryRegion *mr);
 540
 541/**
 542 * memory_region_size: get a memory region's size.
 543 *
 544 * @mr: the memory region being queried.
 545 */
 546uint64_t memory_region_size(MemoryRegion *mr);
 547
 548/**
 549 * memory_region_is_ram: check whether a memory region is random access
 550 *
 551 * Returns %true is a memory region is random access.
 552 *
 553 * @mr: the memory region being queried
 554 */
 555static inline bool memory_region_is_ram(MemoryRegion *mr)
 556{
 557    return mr->ram;
 558}
 559
 560/**
 561 * memory_region_is_skip_dump: check whether a memory region should not be
 562 *                             dumped
 563 *
 564 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
 565 *
 566 * @mr: the memory region being queried
 567 */
 568bool memory_region_is_skip_dump(MemoryRegion *mr);
 569
 570/**
 571 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
 572 *                              region
 573 *
 574 * @mr: the memory region being queried
 575 */
 576void memory_region_set_skip_dump(MemoryRegion *mr);
 577
 578/**
 579 * memory_region_is_romd: check whether a memory region is in ROMD mode
 580 *
 581 * Returns %true if a memory region is a ROM device and currently set to allow
 582 * direct reads.
 583 *
 584 * @mr: the memory region being queried
 585 */
 586static inline bool memory_region_is_romd(MemoryRegion *mr)
 587{
 588    return mr->rom_device && mr->romd_mode;
 589}
 590
 591/**
 592 * memory_region_is_iommu: check whether a memory region is an iommu
 593 *
 594 * Returns %true is a memory region is an iommu.
 595 *
 596 * @mr: the memory region being queried
 597 */
 598static inline bool memory_region_is_iommu(MemoryRegion *mr)
 599{
 600    return mr->iommu_ops;
 601}
 602
 603
 604/**
 605 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
 606 *
 607 * @mr: the memory region that was changed
 608 * @entry: the new entry in the IOMMU translation table.  The entry
 609 *         replaces all old entries for the same virtual I/O address range.
 610 *         Deleted entries have .@perm == 0.
 611 */
 612void memory_region_notify_iommu(MemoryRegion *mr,
 613                                IOMMUTLBEntry entry);
 614
 615/**
 616 * memory_region_register_iommu_notifier: register a notifier for changes to
 617 * IOMMU translation entries.
 618 *
 619 * @mr: the memory region to observe
 620 * @n: the notifier to be added; the notifier receives a pointer to an
 621 *     #IOMMUTLBEntry as the opaque value; the pointer ceases to be
 622 *     valid on exit from the notifier.
 623 */
 624void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
 625
 626/**
 627 * memory_region_iommu_replay: replay existing IOMMU translations to
 628 * a notifier
 629 *
 630 * @mr: the memory region to observe
 631 * @n: the notifier to which to replay iommu mappings
 632 * @granularity: Minimum page granularity to replay notifications for
 633 * @is_write: Whether to treat the replay as a translate "write"
 634 *     through the iommu
 635 */
 636void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n,
 637                                hwaddr granularity, bool is_write);
 638
 639/**
 640 * memory_region_unregister_iommu_notifier: unregister a notifier for
 641 * changes to IOMMU translation entries.
 642 *
 643 * @n: the notifier to be removed.
 644 */
 645void memory_region_unregister_iommu_notifier(Notifier *n);
 646
 647/**
 648 * memory_region_name: get a memory region's name
 649 *
 650 * Returns the string that was used to initialize the memory region.
 651 *
 652 * @mr: the memory region being queried
 653 */
 654const char *memory_region_name(const MemoryRegion *mr);
 655
 656/**
 657 * memory_region_is_logging: return whether a memory region is logging writes
 658 *
 659 * Returns %true if the memory region is logging writes for the given client
 660 *
 661 * @mr: the memory region being queried
 662 * @client: the client being queried
 663 */
 664bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
 665
 666/**
 667 * memory_region_get_dirty_log_mask: return the clients for which a
 668 * memory region is logging writes.
 669 *
 670 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
 671 * are the bit indices.
 672 *
 673 * @mr: the memory region being queried
 674 */
 675uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
 676
 677/**
 678 * memory_region_is_rom: check whether a memory region is ROM
 679 *
 680 * Returns %true is a memory region is read-only memory.
 681 *
 682 * @mr: the memory region being queried
 683 */
 684static inline bool memory_region_is_rom(MemoryRegion *mr)
 685{
 686    return mr->ram && mr->readonly;
 687}
 688
 689
 690/**
 691 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
 692 *
 693 * Returns a file descriptor backing a file-based RAM memory region,
 694 * or -1 if the region is not a file-based RAM memory region.
 695 *
 696 * @mr: the RAM or alias memory region being queried.
 697 */
 698int memory_region_get_fd(MemoryRegion *mr);
 699
 700/**
 701 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
 702 *
 703 * Returns a host pointer to a RAM memory region (created with
 704 * memory_region_init_ram() or memory_region_init_ram_ptr()).
 705 *
 706 * Use with care; by the time this function returns, the returned pointer is
 707 * not protected by RCU anymore.  If the caller is not within an RCU critical
 708 * section and does not hold the iothread lock, it must have other means of
 709 * protecting the pointer, such as a reference to the region that includes
 710 * the incoming ram_addr_t.
 711 *
 712 * @mr: the memory region being queried.
 713 */
 714void *memory_region_get_ram_ptr(MemoryRegion *mr);
 715
 716/* memory_region_ram_resize: Resize a RAM region.
 717 *
 718 * Only legal before guest might have detected the memory size: e.g. on
 719 * incoming migration, or right after reset.
 720 *
 721 * @mr: a memory region created with @memory_region_init_resizeable_ram.
 722 * @newsize: the new size the region
 723 * @errp: pointer to Error*, to store an error if it happens.
 724 */
 725void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
 726                              Error **errp);
 727
 728/**
 729 * memory_region_set_log: Turn dirty logging on or off for a region.
 730 *
 731 * Turns dirty logging on or off for a specified client (display, migration).
 732 * Only meaningful for RAM regions.
 733 *
 734 * @mr: the memory region being updated.
 735 * @log: whether dirty logging is to be enabled or disabled.
 736 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
 737 */
 738void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
 739
 740/**
 741 * memory_region_get_dirty: Check whether a range of bytes is dirty
 742 *                          for a specified client.
 743 *
 744 * Checks whether a range of bytes has been written to since the last
 745 * call to memory_region_reset_dirty() with the same @client.  Dirty logging
 746 * must be enabled.
 747 *
 748 * @mr: the memory region being queried.
 749 * @addr: the address (relative to the start of the region) being queried.
 750 * @size: the size of the range being queried.
 751 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
 752 *          %DIRTY_MEMORY_VGA.
 753 */
 754bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
 755                             hwaddr size, unsigned client);
 756
 757/**
 758 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
 759 *
 760 * Marks a range of bytes as dirty, after it has been dirtied outside
 761 * guest code.
 762 *
 763 * @mr: the memory region being dirtied.
 764 * @addr: the address (relative to the start of the region) being dirtied.
 765 * @size: size of the range being dirtied.
 766 */
 767void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
 768                             hwaddr size);
 769
 770/**
 771 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
 772 *                                     for a specified client. It clears them.
 773 *
 774 * Checks whether a range of bytes has been written to since the last
 775 * call to memory_region_reset_dirty() with the same @client.  Dirty logging
 776 * must be enabled.
 777 *
 778 * @mr: the memory region being queried.
 779 * @addr: the address (relative to the start of the region) being queried.
 780 * @size: the size of the range being queried.
 781 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
 782 *          %DIRTY_MEMORY_VGA.
 783 */
 784bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
 785                                        hwaddr size, unsigned client);
 786/**
 787 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
 788 *                                  any external TLBs (e.g. kvm)
 789 *
 790 * Flushes dirty information from accelerators such as kvm and vhost-net
 791 * and makes it available to users of the memory API.
 792 *
 793 * @mr: the region being flushed.
 794 */
 795void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
 796
 797/**
 798 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
 799 *                            client.
 800 *
 801 * Marks a range of pages as no longer dirty.
 802 *
 803 * @mr: the region being updated.
 804 * @addr: the start of the subrange being cleaned.
 805 * @size: the size of the subrange being cleaned.
 806 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
 807 *          %DIRTY_MEMORY_VGA.
 808 */
 809void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
 810                               hwaddr size, unsigned client);
 811
 812/**
 813 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
 814 *
 815 * Allows a memory region to be marked as read-only (turning it into a ROM).
 816 * only useful on RAM regions.
 817 *
 818 * @mr: the region being updated.
 819 * @readonly: whether rhe region is to be ROM or RAM.
 820 */
 821void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
 822
 823/**
 824 * memory_region_rom_device_set_romd: enable/disable ROMD mode
 825 *
 826 * Allows a ROM device (initialized with memory_region_init_rom_device() to
 827 * set to ROMD mode (default) or MMIO mode.  When it is in ROMD mode, the
 828 * device is mapped to guest memory and satisfies read access directly.
 829 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
 830 * Writes are always handled by the #MemoryRegion.write function.
 831 *
 832 * @mr: the memory region to be updated
 833 * @romd_mode: %true to put the region into ROMD mode
 834 */
 835void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
 836
 837/**
 838 * memory_region_set_coalescing: Enable memory coalescing for the region.
 839 *
 840 * Enabled writes to a region to be queued for later processing. MMIO ->write
 841 * callbacks may be delayed until a non-coalesced MMIO is issued.
 842 * Only useful for IO regions.  Roughly similar to write-combining hardware.
 843 *
 844 * @mr: the memory region to be write coalesced
 845 */
 846void memory_region_set_coalescing(MemoryRegion *mr);
 847
 848/**
 849 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
 850 *                               a region.
 851 *
 852 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
 853 * Multiple calls can be issued coalesced disjoint ranges.
 854 *
 855 * @mr: the memory region to be updated.
 856 * @offset: the start of the range within the region to be coalesced.
 857 * @size: the size of the subrange to be coalesced.
 858 */
 859void memory_region_add_coalescing(MemoryRegion *mr,
 860                                  hwaddr offset,
 861                                  uint64_t size);
 862
 863/**
 864 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
 865 *
 866 * Disables any coalescing caused by memory_region_set_coalescing() or
 867 * memory_region_add_coalescing().  Roughly equivalent to uncacheble memory
 868 * hardware.
 869 *
 870 * @mr: the memory region to be updated.
 871 */
 872void memory_region_clear_coalescing(MemoryRegion *mr);
 873
 874/**
 875 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
 876 *                                    accesses.
 877 *
 878 * Ensure that pending coalesced MMIO request are flushed before the memory
 879 * region is accessed. This property is automatically enabled for all regions
 880 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
 881 *
 882 * @mr: the memory region to be updated.
 883 */
 884void memory_region_set_flush_coalesced(MemoryRegion *mr);
 885
 886/**
 887 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
 888 *                                      accesses.
 889 *
 890 * Clear the automatic coalesced MMIO flushing enabled via
 891 * memory_region_set_flush_coalesced. Note that this service has no effect on
 892 * memory regions that have MMIO coalescing enabled for themselves. For them,
 893 * automatic flushing will stop once coalescing is disabled.
 894 *
 895 * @mr: the memory region to be updated.
 896 */
 897void memory_region_clear_flush_coalesced(MemoryRegion *mr);
 898
 899/**
 900 * memory_region_set_global_locking: Declares the access processing requires
 901 *                                   QEMU's global lock.
 902 *
 903 * When this is invoked, accesses to the memory region will be processed while
 904 * holding the global lock of QEMU. This is the default behavior of memory
 905 * regions.
 906 *
 907 * @mr: the memory region to be updated.
 908 */
 909void memory_region_set_global_locking(MemoryRegion *mr);
 910
 911/**
 912 * memory_region_clear_global_locking: Declares that access processing does
 913 *                                     not depend on the QEMU global lock.
 914 *
 915 * By clearing this property, accesses to the memory region will be processed
 916 * outside of QEMU's global lock (unless the lock is held on when issuing the
 917 * access request). In this case, the device model implementing the access
 918 * handlers is responsible for synchronization of concurrency.
 919 *
 920 * @mr: the memory region to be updated.
 921 */
 922void memory_region_clear_global_locking(MemoryRegion *mr);
 923
 924/**
 925 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
 926 *                            is written to a location.
 927 *
 928 * Marks a word in an IO region (initialized with memory_region_init_io())
 929 * as a trigger for an eventfd event.  The I/O callback will not be called.
 930 * The caller must be prepared to handle failure (that is, take the required
 931 * action if the callback _is_ called).
 932 *
 933 * @mr: the memory region being updated.
 934 * @addr: the address within @mr that is to be monitored
 935 * @size: the size of the access to trigger the eventfd
 936 * @match_data: whether to match against @data, instead of just @addr
 937 * @data: the data to match against the guest write
 938 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
 939 **/
 940void memory_region_add_eventfd(MemoryRegion *mr,
 941                               hwaddr addr,
 942                               unsigned size,
 943                               bool match_data,
 944                               uint64_t data,
 945                               EventNotifier *e);
 946
 947/**
 948 * memory_region_del_eventfd: Cancel an eventfd.
 949 *
 950 * Cancels an eventfd trigger requested by a previous
 951 * memory_region_add_eventfd() call.
 952 *
 953 * @mr: the memory region being updated.
 954 * @addr: the address within @mr that is to be monitored
 955 * @size: the size of the access to trigger the eventfd
 956 * @match_data: whether to match against @data, instead of just @addr
 957 * @data: the data to match against the guest write
 958 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
 959 */
 960void memory_region_del_eventfd(MemoryRegion *mr,
 961                               hwaddr addr,
 962                               unsigned size,
 963                               bool match_data,
 964                               uint64_t data,
 965                               EventNotifier *e);
 966
 967/**
 968 * memory_region_add_subregion: Add a subregion to a container.
 969 *
 970 * Adds a subregion at @offset.  The subregion may not overlap with other
 971 * subregions (except for those explicitly marked as overlapping).  A region
 972 * may only be added once as a subregion (unless removed with
 973 * memory_region_del_subregion()); use memory_region_init_alias() if you
 974 * want a region to be a subregion in multiple locations.
 975 *
 976 * @mr: the region to contain the new subregion; must be a container
 977 *      initialized with memory_region_init().
 978 * @offset: the offset relative to @mr where @subregion is added.
 979 * @subregion: the subregion to be added.
 980 */
 981void memory_region_add_subregion(MemoryRegion *mr,
 982                                 hwaddr offset,
 983                                 MemoryRegion *subregion);
 984/**
 985 * memory_region_add_subregion_overlap: Add a subregion to a container
 986 *                                      with overlap.
 987 *
 988 * Adds a subregion at @offset.  The subregion may overlap with other
 989 * subregions.  Conflicts are resolved by having a higher @priority hide a
 990 * lower @priority. Subregions without priority are taken as @priority 0.
 991 * A region may only be added once as a subregion (unless removed with
 992 * memory_region_del_subregion()); use memory_region_init_alias() if you
 993 * want a region to be a subregion in multiple locations.
 994 *
 995 * @mr: the region to contain the new subregion; must be a container
 996 *      initialized with memory_region_init().
 997 * @offset: the offset relative to @mr where @subregion is added.
 998 * @subregion: the subregion to be added.
 999 * @priority: used for resolving overlaps; highest priority wins.
1000 */
1001void memory_region_add_subregion_overlap(MemoryRegion *mr,
1002                                         hwaddr offset,
1003                                         MemoryRegion *subregion,
1004                                         int priority);
1005
1006/**
1007 * memory_region_get_ram_addr: Get the ram address associated with a memory
1008 *                             region
1009 */
1010ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
1011
1012uint64_t memory_region_get_alignment(const MemoryRegion *mr);
1013/**
1014 * memory_region_del_subregion: Remove a subregion.
1015 *
1016 * Removes a subregion from its container.
1017 *
1018 * @mr: the container to be updated.
1019 * @subregion: the region being removed; must be a current subregion of @mr.
1020 */
1021void memory_region_del_subregion(MemoryRegion *mr,
1022                                 MemoryRegion *subregion);
1023
1024/*
1025 * memory_region_set_enabled: dynamically enable or disable a region
1026 *
1027 * Enables or disables a memory region.  A disabled memory region
1028 * ignores all accesses to itself and its subregions.  It does not
1029 * obscure sibling subregions with lower priority - it simply behaves as
1030 * if it was removed from the hierarchy.
1031 *
1032 * Regions default to being enabled.
1033 *
1034 * @mr: the region to be updated
1035 * @enabled: whether to enable or disable the region
1036 */
1037void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1038
1039/*
1040 * memory_region_set_address: dynamically update the address of a region
1041 *
1042 * Dynamically updates the address of a region, relative to its container.
1043 * May be used on regions are currently part of a memory hierarchy.
1044 *
1045 * @mr: the region to be updated
1046 * @addr: new address, relative to container region
1047 */
1048void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
1049
1050/*
1051 * memory_region_set_size: dynamically update the size of a region.
1052 *
1053 * Dynamically updates the size of a region.
1054 *
1055 * @mr: the region to be updated
1056 * @size: used size of the region.
1057 */
1058void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1059
1060/*
1061 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1062 *
1063 * Dynamically updates the offset into the target region that an alias points
1064 * to, as if the fourth argument to memory_region_init_alias() has changed.
1065 *
1066 * @mr: the #MemoryRegion to be updated; should be an alias.
1067 * @offset: the new offset into the target memory region
1068 */
1069void memory_region_set_alias_offset(MemoryRegion *mr,
1070                                    hwaddr offset);
1071
1072/**
1073 * memory_region_present: checks if an address relative to a @container
1074 * translates into #MemoryRegion within @container
1075 *
1076 * Answer whether a #MemoryRegion within @container covers the address
1077 * @addr.
1078 *
1079 * @container: a #MemoryRegion within which @addr is a relative address
1080 * @addr: the area within @container to be searched
1081 */
1082bool memory_region_present(MemoryRegion *container, hwaddr addr);
1083
1084/**
1085 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1086 * into any address space.
1087 *
1088 * @mr: a #MemoryRegion which should be checked if it's mapped
1089 */
1090bool memory_region_is_mapped(MemoryRegion *mr);
1091
1092/**
1093 * memory_region_find: translate an address/size relative to a
1094 * MemoryRegion into a #MemoryRegionSection.
1095 *
1096 * Locates the first #MemoryRegion within @mr that overlaps the range
1097 * given by @addr and @size.
1098 *
1099 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1100 * It will have the following characteristics:
1101 *    .@size = 0 iff no overlap was found
1102 *    .@mr is non-%NULL iff an overlap was found
1103 *
1104 * Remember that in the return value the @offset_within_region is
1105 * relative to the returned region (in the .@mr field), not to the
1106 * @mr argument.
1107 *
1108 * Similarly, the .@offset_within_address_space is relative to the
1109 * address space that contains both regions, the passed and the
1110 * returned one.  However, in the special case where the @mr argument
1111 * has no container (and thus is the root of the address space), the
1112 * following will hold:
1113 *    .@offset_within_address_space >= @addr
1114 *    .@offset_within_address_space + .@size <= @addr + @size
1115 *
1116 * @mr: a MemoryRegion within which @addr is a relative address
1117 * @addr: start of the area within @as to be searched
1118 * @size: size of the area to be searched
1119 */
1120MemoryRegionSection memory_region_find(MemoryRegion *mr,
1121                                       hwaddr addr, uint64_t size);
1122
1123/**
1124 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
1125 *
1126 * Synchronizes the dirty page log for an entire address space.
1127 * @as: the address space that contains the memory being synchronized
1128 */
1129void address_space_sync_dirty_bitmap(AddressSpace *as);
1130
1131/**
1132 * memory_region_transaction_begin: Start a transaction.
1133 *
1134 * During a transaction, changes will be accumulated and made visible
1135 * only when the transaction ends (is committed).
1136 */
1137void memory_region_transaction_begin(void);
1138
1139/**
1140 * memory_region_transaction_commit: Commit a transaction and make changes
1141 *                                   visible to the guest.
1142 */
1143void memory_region_transaction_commit(void);
1144
1145/**
1146 * memory_listener_register: register callbacks to be called when memory
1147 *                           sections are mapped or unmapped into an address
1148 *                           space
1149 *
1150 * @listener: an object containing the callbacks to be called
1151 * @filter: if non-%NULL, only regions in this address space will be observed
1152 */
1153void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
1154
1155/**
1156 * memory_listener_unregister: undo the effect of memory_listener_register()
1157 *
1158 * @listener: an object containing the callbacks to be removed
1159 */
1160void memory_listener_unregister(MemoryListener *listener);
1161
1162/**
1163 * memory_global_dirty_log_start: begin dirty logging for all regions
1164 */
1165void memory_global_dirty_log_start(void);
1166
1167/**
1168 * memory_global_dirty_log_stop: end dirty logging for all regions
1169 */
1170void memory_global_dirty_log_stop(void);
1171
1172void mtree_info(fprintf_function mon_printf, void *f);
1173
1174/**
1175 * memory_region_dispatch_read: perform a read directly to the specified
1176 * MemoryRegion.
1177 *
1178 * @mr: #MemoryRegion to access
1179 * @addr: address within that region
1180 * @pval: pointer to uint64_t which the data is written to
1181 * @size: size of the access in bytes
1182 * @attrs: memory transaction attributes to use for the access
1183 */
1184MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1185                                        hwaddr addr,
1186                                        uint64_t *pval,
1187                                        unsigned size,
1188                                        MemTxAttrs attrs);
1189/**
1190 * memory_region_dispatch_write: perform a write directly to the specified
1191 * MemoryRegion.
1192 *
1193 * @mr: #MemoryRegion to access
1194 * @addr: address within that region
1195 * @data: data to write
1196 * @size: size of the access in bytes
1197 * @attrs: memory transaction attributes to use for the access
1198 */
1199MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1200                                         hwaddr addr,
1201                                         uint64_t data,
1202                                         unsigned size,
1203                                         MemTxAttrs attrs);
1204
1205/**
1206 * address_space_init: initializes an address space
1207 *
1208 * @as: an uninitialized #AddressSpace
1209 * @root: a #MemoryRegion that routes addresses for the address space
1210 * @name: an address space name.  The name is only used for debugging
1211 *        output.
1212 */
1213void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
1214
1215/**
1216 * address_space_init_shareable: return an address space for a memory region,
1217 *                               creating it if it does not already exist
1218 *
1219 * @root: a #MemoryRegion that routes addresses for the address space
1220 * @name: an address space name.  The name is only used for debugging
1221 *        output.
1222 *
1223 * This function will return a pointer to an existing AddressSpace
1224 * which was initialized with the specified MemoryRegion, or it will
1225 * create and initialize one if it does not already exist. The ASes
1226 * are reference-counted, so the memory will be freed automatically
1227 * when the AddressSpace is destroyed via address_space_destroy.
1228 */
1229AddressSpace *address_space_init_shareable(MemoryRegion *root,
1230                                           const char *name);
1231
1232/**
1233 * address_space_destroy: destroy an address space
1234 *
1235 * Releases all resources associated with an address space.  After an address space
1236 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1237 * as well.
1238 *
1239 * @as: address space to be destroyed
1240 */
1241void address_space_destroy(AddressSpace *as);
1242
1243/**
1244 * address_space_rw: read from or write to an address space.
1245 *
1246 * Return a MemTxResult indicating whether the operation succeeded
1247 * or failed (eg unassigned memory, device rejected the transaction,
1248 * IOMMU fault).
1249 *
1250 * @as: #AddressSpace to be accessed
1251 * @addr: address within that address space
1252 * @attrs: memory transaction attributes
1253 * @buf: buffer with the data transferred
1254 * @is_write: indicates the transfer direction
1255 */
1256MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1257                             MemTxAttrs attrs, uint8_t *buf,
1258                             int len, bool is_write);
1259
1260/**
1261 * address_space_write: write to address space.
1262 *
1263 * Return a MemTxResult indicating whether the operation succeeded
1264 * or failed (eg unassigned memory, device rejected the transaction,
1265 * IOMMU fault).
1266 *
1267 * @as: #AddressSpace to be accessed
1268 * @addr: address within that address space
1269 * @attrs: memory transaction attributes
1270 * @buf: buffer with the data transferred
1271 */
1272MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1273                                MemTxAttrs attrs,
1274                                const uint8_t *buf, int len);
1275
1276/* address_space_ld*: load from an address space
1277 * address_space_st*: store to an address space
1278 *
1279 * These functions perform a load or store of the byte, word,
1280 * longword or quad to the specified address within the AddressSpace.
1281 * The _le suffixed functions treat the data as little endian;
1282 * _be indicates big endian; no suffix indicates "same endianness
1283 * as guest CPU".
1284 *
1285 * The "guest CPU endianness" accessors are deprecated for use outside
1286 * target-* code; devices should be CPU-agnostic and use either the LE
1287 * or the BE accessors.
1288 *
1289 * @as #AddressSpace to be accessed
1290 * @addr: address within that address space
1291 * @val: data value, for stores
1292 * @attrs: memory transaction attributes
1293 * @result: location to write the success/failure of the transaction;
1294 *   if NULL, this information is discarded
1295 */
1296uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1297                            MemTxAttrs attrs, MemTxResult *result);
1298uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1299                            MemTxAttrs attrs, MemTxResult *result);
1300uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1301                            MemTxAttrs attrs, MemTxResult *result);
1302uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1303                            MemTxAttrs attrs, MemTxResult *result);
1304uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1305                            MemTxAttrs attrs, MemTxResult *result);
1306uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1307                            MemTxAttrs attrs, MemTxResult *result);
1308uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1309                            MemTxAttrs attrs, MemTxResult *result);
1310void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1311                            MemTxAttrs attrs, MemTxResult *result);
1312void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1313                            MemTxAttrs attrs, MemTxResult *result);
1314void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1315                            MemTxAttrs attrs, MemTxResult *result);
1316void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1317                            MemTxAttrs attrs, MemTxResult *result);
1318void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1319                            MemTxAttrs attrs, MemTxResult *result);
1320void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1321                            MemTxAttrs attrs, MemTxResult *result);
1322void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1323                            MemTxAttrs attrs, MemTxResult *result);
1324
1325#ifdef NEED_CPU_H
1326uint32_t address_space_lduw(AddressSpace *as, hwaddr addr,
1327                            MemTxAttrs attrs, MemTxResult *result);
1328uint32_t address_space_ldl(AddressSpace *as, hwaddr addr,
1329                            MemTxAttrs attrs, MemTxResult *result);
1330uint64_t address_space_ldq(AddressSpace *as, hwaddr addr,
1331                            MemTxAttrs attrs, MemTxResult *result);
1332void address_space_stl_notdirty(AddressSpace *as, hwaddr addr, uint32_t val,
1333                            MemTxAttrs attrs, MemTxResult *result);
1334void address_space_stw(AddressSpace *as, hwaddr addr, uint32_t val,
1335                            MemTxAttrs attrs, MemTxResult *result);
1336void address_space_stl(AddressSpace *as, hwaddr addr, uint32_t val,
1337                            MemTxAttrs attrs, MemTxResult *result);
1338void address_space_stq(AddressSpace *as, hwaddr addr, uint64_t val,
1339                            MemTxAttrs attrs, MemTxResult *result);
1340#endif
1341
1342/* address_space_translate: translate an address range into an address space
1343 * into a MemoryRegion and an address range into that section.  Should be
1344 * called from an RCU critical section, to avoid that the last reference
1345 * to the returned region disappears after address_space_translate returns.
1346 *
1347 * @as: #AddressSpace to be accessed
1348 * @addr: address within that address space
1349 * @xlat: pointer to address within the returned memory region section's
1350 * #MemoryRegion.
1351 * @len: pointer to length
1352 * @is_write: indicates the transfer direction
1353 */
1354MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1355                                      hwaddr *xlat, hwaddr *len,
1356                                      bool is_write);
1357
1358MemoryRegion *address_space_translate_attr(AddressSpace *as, hwaddr addr,
1359                                      hwaddr *xlat, hwaddr *plen,
1360                                      bool is_write, MemTxAttrs *attr);
1361
1362/* address_space_access_valid: check for validity of accessing an address
1363 * space range
1364 *
1365 * Check whether memory is assigned to the given address space range, and
1366 * access is permitted by any IOMMU regions that are active for the address
1367 * space.
1368 *
1369 * For now, addr and len should be aligned to a page size.  This limitation
1370 * will be lifted in the future.
1371 *
1372 * @as: #AddressSpace to be accessed
1373 * @addr: address within that address space
1374 * @len: length of the area to be checked
1375 * @is_write: indicates the transfer direction
1376 * @attr: Passes down specific memory transaction attributes
1377 *        (such as secure/nonsecure)
1378 */
1379bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len,
1380                                bool is_write, MemTxAttrs attr);
1381
1382/* address_space_map: map a physical memory region into a host virtual address
1383 *
1384 * May map a subset of the requested range, given by and returned in @plen.
1385 * May return %NULL if resources needed to perform the mapping are exhausted.
1386 * Use only for reads OR writes - not for read-modify-write operations.
1387 * Use cpu_register_map_client() to know when retrying the map operation is
1388 * likely to succeed.
1389 *
1390 * @as: #AddressSpace to be accessed
1391 * @addr: address within that address space
1392 * @plen: pointer to length of buffer; updated on return
1393 * @is_write: indicates the transfer direction
1394 */
1395void *address_space_map(AddressSpace *as, hwaddr addr,
1396                        hwaddr *plen, bool is_write);
1397
1398/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1399 *
1400 * Will also mark the memory as dirty if @is_write == %true.  @access_len gives
1401 * the amount of memory that was actually read or written by the caller.
1402 *
1403 * @as: #AddressSpace used
1404 * @addr: address within that address space
1405 * @len: buffer length as returned by address_space_map()
1406 * @access_len: amount of data actually transferred
1407 * @is_write: indicates the transfer direction
1408 */
1409void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1410                         int is_write, hwaddr access_len);
1411
1412
1413/* Internal functions, part of the implementation of address_space_read.  */
1414MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr,
1415                                        MemTxAttrs attrs, uint8_t *buf,
1416                                        int len, hwaddr addr1, hwaddr l,
1417                                        MemoryRegion *mr);
1418MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
1419                                    MemTxAttrs attrs, uint8_t *buf, int len);
1420void *qemu_get_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
1421
1422static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1423{
1424    if (is_write) {
1425        return memory_region_is_ram(mr) && !mr->readonly;
1426    } else {
1427        return memory_region_is_ram(mr) || memory_region_is_romd(mr);
1428    }
1429}
1430
1431/**
1432 * address_space_read: read from an address space.
1433 *
1434 * Return a MemTxResult indicating whether the operation succeeded
1435 * or failed (eg unassigned memory, device rejected the transaction,
1436 * IOMMU fault).
1437 *
1438 * @as: #AddressSpace to be accessed
1439 * @addr: address within that address space
1440 * @attrs: memory transaction attributes
1441 * @buf: buffer with the data transferred
1442 */
1443static inline __attribute__((__always_inline__))
1444MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
1445                               uint8_t *buf, int len)
1446{
1447    MemTxResult result = MEMTX_OK;
1448    hwaddr l, addr1;
1449    void *ptr;
1450    MemoryRegion *mr;
1451
1452    if (__builtin_constant_p(len)) {
1453        if (len) {
1454            rcu_read_lock();
1455            l = len;
1456            mr = address_space_translate_attr(as, addr, &addr1, &l, false,
1457                                              &attrs);
1458            if (len == l && memory_access_is_direct(mr, false)) {
1459                addr1 += memory_region_get_ram_addr(mr);
1460                ptr = qemu_get_ram_ptr(mr->ram_block, addr1);
1461                memcpy(buf, ptr, len);
1462            } else {
1463                result = address_space_read_continue(as, addr, attrs, buf, len,
1464                                                     addr1, l, mr);
1465            }
1466            rcu_read_unlock();
1467        }
1468    } else {
1469        result = address_space_read_full(as, addr, attrs, buf, len);
1470    }
1471    return result;
1472}
1473
1474#endif
1475
1476#endif
1477