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