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