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