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