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