xref: /openbmc/qemu/include/exec/memory.h (revision f702e62a)
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 "qemu-common.h"
27 #include "exec/cpu-common.h"
28 #ifndef CONFIG_USER_ONLY
29 #include "exec/hwaddr.h"
30 #endif
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 
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 /*
71  * Memory region callbacks
72  */
73 struct MemoryRegionOps {
74     /* Read from the memory region. @addr is relative to @mr; @size is
75      * in bytes. */
76     uint64_t (*read)(void *opaque,
77                      hwaddr addr,
78                      unsigned size);
79     /* Write to the memory region. @addr is relative to @mr; @size is
80      * in bytes. */
81     void (*write)(void *opaque,
82                   hwaddr addr,
83                   uint64_t data,
84                   unsigned size);
85 
86     enum device_endian endianness;
87     /* Guest-visible constraints: */
88     struct {
89         /* If nonzero, specify bounds on access sizes beyond which a machine
90          * check is thrown.
91          */
92         unsigned min_access_size;
93         unsigned max_access_size;
94         /* If true, unaligned accesses are supported.  Otherwise unaligned
95          * accesses throw machine checks.
96          */
97          bool unaligned;
98         /*
99          * If present, and returns #false, the transaction is not accepted
100          * by the device (and results in machine dependent behaviour such
101          * as a machine check exception).
102          */
103         bool (*accepts)(void *opaque, hwaddr addr,
104                         unsigned size, bool is_write);
105     } valid;
106     /* Internal implementation constraints: */
107     struct {
108         /* If nonzero, specifies the minimum size implemented.  Smaller sizes
109          * will be rounded upwards and a partial result will be returned.
110          */
111         unsigned min_access_size;
112         /* If nonzero, specifies the maximum size implemented.  Larger sizes
113          * will be done as a series of accesses with smaller sizes.
114          */
115         unsigned max_access_size;
116         /* If true, unaligned accesses are supported.  Otherwise all accesses
117          * are converted to (possibly multiple) naturally aligned accesses.
118          */
119         bool unaligned;
120     } impl;
121 
122     /* If .read and .write are not present, old_mmio may be used for
123      * backwards compatibility with old mmio registration
124      */
125     const MemoryRegionMmio old_mmio;
126 };
127 
128 typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
129 
130 struct MemoryRegionIOMMUOps {
131     /* Return a TLB entry that contains a given address. */
132     IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr);
133 };
134 
135 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
136 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
137 
138 struct MemoryRegion {
139     Object parent_obj;
140     /* All fields are private - violators will be prosecuted */
141     const MemoryRegionOps *ops;
142     const MemoryRegionIOMMUOps *iommu_ops;
143     void *opaque;
144     MemoryRegion *container;
145     Int128 size;
146     hwaddr addr;
147     void (*destructor)(MemoryRegion *mr);
148     ram_addr_t ram_addr;
149     bool subpage;
150     bool terminates;
151     bool romd_mode;
152     bool ram;
153     bool readonly; /* For RAM regions */
154     bool enabled;
155     bool rom_device;
156     bool warning_printed; /* For reservations */
157     bool flush_coalesced_mmio;
158     MemoryRegion *alias;
159     hwaddr alias_offset;
160     int32_t priority;
161     bool may_overlap;
162     QTAILQ_HEAD(subregions, MemoryRegion) subregions;
163     QTAILQ_ENTRY(MemoryRegion) subregions_link;
164     QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
165     const char *name;
166     uint8_t dirty_log_mask;
167     unsigned ioeventfd_nb;
168     MemoryRegionIoeventfd *ioeventfds;
169     NotifierList iommu_notify;
170 };
171 
172 /**
173  * MemoryListener: callbacks structure for updates to the physical memory map
174  *
175  * Allows a component to adjust to changes in the guest-visible memory map.
176  * Use with memory_listener_register() and memory_listener_unregister().
177  */
178 struct MemoryListener {
179     void (*begin)(MemoryListener *listener);
180     void (*commit)(MemoryListener *listener);
181     void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
182     void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
183     void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
184     void (*log_start)(MemoryListener *listener, MemoryRegionSection *section);
185     void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section);
186     void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
187     void (*log_global_start)(MemoryListener *listener);
188     void (*log_global_stop)(MemoryListener *listener);
189     void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
190                         bool match_data, uint64_t data, EventNotifier *e);
191     void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
192                         bool match_data, uint64_t data, EventNotifier *e);
193     void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
194                                hwaddr addr, hwaddr len);
195     void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
196                                hwaddr addr, hwaddr len);
197     /* Lower = earlier (during add), later (during del) */
198     unsigned priority;
199     AddressSpace *address_space_filter;
200     QTAILQ_ENTRY(MemoryListener) link;
201 };
202 
203 /**
204  * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
205  */
206 struct AddressSpace {
207     /* All fields are private. */
208     char *name;
209     MemoryRegion *root;
210     struct FlatView *current_map;
211     int ioeventfd_nb;
212     struct MemoryRegionIoeventfd *ioeventfds;
213     struct AddressSpaceDispatch *dispatch;
214     struct AddressSpaceDispatch *next_dispatch;
215     MemoryListener dispatch_listener;
216 
217     QTAILQ_ENTRY(AddressSpace) address_spaces_link;
218 };
219 
220 /**
221  * MemoryRegionSection: describes a fragment of a #MemoryRegion
222  *
223  * @mr: the region, or %NULL if empty
224  * @address_space: the address space the region is mapped in
225  * @offset_within_region: the beginning of the section, relative to @mr's start
226  * @size: the size of the section; will not exceed @mr's boundaries
227  * @offset_within_address_space: the address of the first byte of the section
228  *     relative to the region's address space
229  * @readonly: writes to this section are ignored
230  */
231 struct MemoryRegionSection {
232     MemoryRegion *mr;
233     AddressSpace *address_space;
234     hwaddr offset_within_region;
235     Int128 size;
236     hwaddr offset_within_address_space;
237     bool readonly;
238 };
239 
240 /**
241  * memory_region_init: Initialize a memory region
242  *
243  * The region typically acts as a container for other memory regions.  Use
244  * memory_region_add_subregion() to add subregions.
245  *
246  * @mr: the #MemoryRegion to be initialized
247  * @owner: the object that tracks the region's reference count
248  * @name: used for debugging; not visible to the user or ABI
249  * @size: size of the region; any subregions beyond this size will be clipped
250  */
251 void memory_region_init(MemoryRegion *mr,
252                         struct Object *owner,
253                         const char *name,
254                         uint64_t size);
255 
256 /**
257  * memory_region_ref: Add 1 to a memory region's reference count
258  *
259  * Whenever memory regions are accessed outside the BQL, they need to be
260  * preserved against hot-unplug.  MemoryRegions actually do not have their
261  * own reference count; they piggyback on a QOM object, their "owner".
262  * This function adds a reference to the owner.
263  *
264  * All MemoryRegions must have an owner if they can disappear, even if the
265  * device they belong to operates exclusively under the BQL.  This is because
266  * the region could be returned at any time by memory_region_find, and this
267  * is usually under guest control.
268  *
269  * @mr: the #MemoryRegion
270  */
271 void memory_region_ref(MemoryRegion *mr);
272 
273 /**
274  * memory_region_unref: Remove 1 to a memory region's reference count
275  *
276  * Whenever memory regions are accessed outside the BQL, they need to be
277  * preserved against hot-unplug.  MemoryRegions actually do not have their
278  * own reference count; they piggyback on a QOM object, their "owner".
279  * This function removes a reference to the owner and possibly destroys it.
280  *
281  * @mr: the #MemoryRegion
282  */
283 void memory_region_unref(MemoryRegion *mr);
284 
285 /**
286  * memory_region_init_io: Initialize an I/O memory region.
287  *
288  * Accesses into the region will cause the callbacks in @ops to be called.
289  * if @size is nonzero, subregions will be clipped to @size.
290  *
291  * @mr: the #MemoryRegion to be initialized.
292  * @owner: the object that tracks the region's reference count
293  * @ops: a structure containing read and write callbacks to be used when
294  *       I/O is performed on the region.
295  * @opaque: passed to to the read and write callbacks of the @ops structure.
296  * @name: used for debugging; not visible to the user or ABI
297  * @size: size of the region.
298  */
299 void memory_region_init_io(MemoryRegion *mr,
300                            struct Object *owner,
301                            const MemoryRegionOps *ops,
302                            void *opaque,
303                            const char *name,
304                            uint64_t size);
305 
306 /**
307  * memory_region_init_ram:  Initialize RAM memory region.  Accesses into the
308  *                          region will modify memory directly.
309  *
310  * @mr: the #MemoryRegion to be initialized.
311  * @owner: the object that tracks the region's reference count
312  * @name: the name of the region.
313  * @size: size of the region.
314  */
315 void memory_region_init_ram(MemoryRegion *mr,
316                             struct Object *owner,
317                             const char *name,
318                             uint64_t size);
319 
320 #ifdef __linux__
321 /**
322  * memory_region_init_ram_from_file:  Initialize RAM memory region with a
323  *                                    mmap-ed backend.
324  *
325  * @mr: the #MemoryRegion to be initialized.
326  * @owner: the object that tracks the region's reference count
327  * @name: the name of the region.
328  * @size: size of the region.
329  * @share: %true if memory must be mmaped with the MAP_SHARED flag
330  * @path: the path in which to allocate the RAM.
331  * @errp: pointer to Error*, to store an error if it happens.
332  */
333 void memory_region_init_ram_from_file(MemoryRegion *mr,
334                                       struct Object *owner,
335                                       const char *name,
336                                       uint64_t size,
337                                       bool share,
338                                       const char *path,
339                                       Error **errp);
340 #endif
341 
342 /**
343  * memory_region_init_ram_ptr:  Initialize RAM memory region from a
344  *                              user-provided pointer.  Accesses into the
345  *                              region will modify memory directly.
346  *
347  * @mr: the #MemoryRegion to be initialized.
348  * @owner: the object that tracks the region's reference count
349  * @name: the name of the region.
350  * @size: size of the region.
351  * @ptr: memory to be mapped; must contain at least @size bytes.
352  */
353 void memory_region_init_ram_ptr(MemoryRegion *mr,
354                                 struct Object *owner,
355                                 const char *name,
356                                 uint64_t size,
357                                 void *ptr);
358 
359 /**
360  * memory_region_init_alias: Initialize a memory region that aliases all or a
361  *                           part of another memory region.
362  *
363  * @mr: the #MemoryRegion to be initialized.
364  * @owner: the object that tracks the region's reference count
365  * @name: used for debugging; not visible to the user or ABI
366  * @orig: the region to be referenced; @mr will be equivalent to
367  *        @orig between @offset and @offset + @size - 1.
368  * @offset: start of the section in @orig to be referenced.
369  * @size: size of the region.
370  */
371 void memory_region_init_alias(MemoryRegion *mr,
372                               struct Object *owner,
373                               const char *name,
374                               MemoryRegion *orig,
375                               hwaddr offset,
376                               uint64_t size);
377 
378 /**
379  * memory_region_init_rom_device:  Initialize a ROM memory region.  Writes are
380  *                                 handled via callbacks.
381  *
382  * @mr: the #MemoryRegion to be initialized.
383  * @owner: the object that tracks the region's reference count
384  * @ops: callbacks for write access handling.
385  * @name: the name of the region.
386  * @size: size of the region.
387  */
388 void memory_region_init_rom_device(MemoryRegion *mr,
389                                    struct Object *owner,
390                                    const MemoryRegionOps *ops,
391                                    void *opaque,
392                                    const char *name,
393                                    uint64_t size);
394 
395 /**
396  * memory_region_init_reservation: Initialize a memory region that reserves
397  *                                 I/O space.
398  *
399  * A reservation region primariy serves debugging purposes.  It claims I/O
400  * space that is not supposed to be handled by QEMU itself.  Any access via
401  * the memory API will cause an abort().
402  *
403  * @mr: the #MemoryRegion to be initialized
404  * @owner: the object that tracks the region's reference count
405  * @name: used for debugging; not visible to the user or ABI
406  * @size: size of the region.
407  */
408 void memory_region_init_reservation(MemoryRegion *mr,
409                                     struct Object *owner,
410                                     const char *name,
411                                     uint64_t size);
412 
413 /**
414  * memory_region_init_iommu: Initialize a memory region that translates
415  * addresses
416  *
417  * An IOMMU region translates addresses and forwards accesses to a target
418  * memory region.
419  *
420  * @mr: the #MemoryRegion to be initialized
421  * @owner: the object that tracks the region's reference count
422  * @ops: a function that translates addresses into the @target region
423  * @name: used for debugging; not visible to the user or ABI
424  * @size: size of the region.
425  */
426 void memory_region_init_iommu(MemoryRegion *mr,
427                               struct Object *owner,
428                               const MemoryRegionIOMMUOps *ops,
429                               const char *name,
430                               uint64_t size);
431 
432 /**
433  * memory_region_destroy: Destroy a memory region and reclaim all resources.
434  *
435  * @mr: the region to be destroyed.  May not currently be a subregion
436  *      (see memory_region_add_subregion()) or referenced in an alias
437  *      (see memory_region_init_alias()).
438  */
439 void memory_region_destroy(MemoryRegion *mr);
440 
441 /**
442  * memory_region_owner: get a memory region's owner.
443  *
444  * @mr: the memory region being queried.
445  */
446 struct Object *memory_region_owner(MemoryRegion *mr);
447 
448 /**
449  * memory_region_size: get a memory region's size.
450  *
451  * @mr: the memory region being queried.
452  */
453 uint64_t memory_region_size(MemoryRegion *mr);
454 
455 /**
456  * memory_region_is_ram: check whether a memory region is random access
457  *
458  * Returns %true is a memory region is random access.
459  *
460  * @mr: the memory region being queried
461  */
462 bool memory_region_is_ram(MemoryRegion *mr);
463 
464 /**
465  * memory_region_is_romd: check whether a memory region is in ROMD mode
466  *
467  * Returns %true if a memory region is a ROM device and currently set to allow
468  * direct reads.
469  *
470  * @mr: the memory region being queried
471  */
472 static inline bool memory_region_is_romd(MemoryRegion *mr)
473 {
474     return mr->rom_device && mr->romd_mode;
475 }
476 
477 /**
478  * memory_region_is_iommu: check whether a memory region is an iommu
479  *
480  * Returns %true is a memory region is an iommu.
481  *
482  * @mr: the memory region being queried
483  */
484 bool memory_region_is_iommu(MemoryRegion *mr);
485 
486 /**
487  * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
488  *
489  * @mr: the memory region that was changed
490  * @entry: the new entry in the IOMMU translation table.  The entry
491  *         replaces all old entries for the same virtual I/O address range.
492  *         Deleted entries have .@perm == 0.
493  */
494 void memory_region_notify_iommu(MemoryRegion *mr,
495                                 IOMMUTLBEntry entry);
496 
497 /**
498  * memory_region_register_iommu_notifier: register a notifier for changes to
499  * IOMMU translation entries.
500  *
501  * @mr: the memory region to observe
502  * @n: the notifier to be added; the notifier receives a pointer to an
503  *     #IOMMUTLBEntry as the opaque value; the pointer ceases to be
504  *     valid on exit from the notifier.
505  */
506 void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
507 
508 /**
509  * memory_region_unregister_iommu_notifier: unregister a notifier for
510  * changes to IOMMU translation entries.
511  *
512  * @n: the notifier to be removed.
513  */
514 void memory_region_unregister_iommu_notifier(Notifier *n);
515 
516 /**
517  * memory_region_name: get a memory region's name
518  *
519  * Returns the string that was used to initialize the memory region.
520  *
521  * @mr: the memory region being queried
522  */
523 const char *memory_region_name(MemoryRegion *mr);
524 
525 /**
526  * memory_region_is_logging: return whether a memory region is logging writes
527  *
528  * Returns %true if the memory region is logging writes
529  *
530  * @mr: the memory region being queried
531  */
532 bool memory_region_is_logging(MemoryRegion *mr);
533 
534 /**
535  * memory_region_is_rom: check whether a memory region is ROM
536  *
537  * Returns %true is a memory region is read-only memory.
538  *
539  * @mr: the memory region being queried
540  */
541 bool memory_region_is_rom(MemoryRegion *mr);
542 
543 /**
544  * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
545  *
546  * Returns a file descriptor backing a file-based RAM memory region,
547  * or -1 if the region is not a file-based RAM memory region.
548  *
549  * @mr: the RAM or alias memory region being queried.
550  */
551 int memory_region_get_fd(MemoryRegion *mr);
552 
553 /**
554  * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
555  *
556  * Returns a host pointer to a RAM memory region (created with
557  * memory_region_init_ram() or memory_region_init_ram_ptr()).  Use with
558  * care.
559  *
560  * @mr: the memory region being queried.
561  */
562 void *memory_region_get_ram_ptr(MemoryRegion *mr);
563 
564 /**
565  * memory_region_set_log: Turn dirty logging on or off for a region.
566  *
567  * Turns dirty logging on or off for a specified client (display, migration).
568  * Only meaningful for RAM regions.
569  *
570  * @mr: the memory region being updated.
571  * @log: whether dirty logging is to be enabled or disabled.
572  * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
573  *          %DIRTY_MEMORY_VGA.
574  */
575 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
576 
577 /**
578  * memory_region_get_dirty: Check whether a range of bytes is dirty
579  *                          for a specified client.
580  *
581  * Checks whether a range of bytes has been written to since the last
582  * call to memory_region_reset_dirty() with the same @client.  Dirty logging
583  * must be enabled.
584  *
585  * @mr: the memory region being queried.
586  * @addr: the address (relative to the start of the region) being queried.
587  * @size: the size of the range being queried.
588  * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
589  *          %DIRTY_MEMORY_VGA.
590  */
591 bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
592                              hwaddr size, unsigned client);
593 
594 /**
595  * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
596  *
597  * Marks a range of bytes as dirty, after it has been dirtied outside
598  * guest code.
599  *
600  * @mr: the memory region being dirtied.
601  * @addr: the address (relative to the start of the region) being dirtied.
602  * @size: size of the range being dirtied.
603  */
604 void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
605                              hwaddr size);
606 
607 /**
608  * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
609  *                                     for a specified client. It clears them.
610  *
611  * Checks whether a range of bytes has been written to since the last
612  * call to memory_region_reset_dirty() with the same @client.  Dirty logging
613  * must be enabled.
614  *
615  * @mr: the memory region being queried.
616  * @addr: the address (relative to the start of the region) being queried.
617  * @size: the size of the range being queried.
618  * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
619  *          %DIRTY_MEMORY_VGA.
620  */
621 bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
622                                         hwaddr size, unsigned client);
623 /**
624  * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
625  *                                  any external TLBs (e.g. kvm)
626  *
627  * Flushes dirty information from accelerators such as kvm and vhost-net
628  * and makes it available to users of the memory API.
629  *
630  * @mr: the region being flushed.
631  */
632 void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
633 
634 /**
635  * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
636  *                            client.
637  *
638  * Marks a range of pages as no longer dirty.
639  *
640  * @mr: the region being updated.
641  * @addr: the start of the subrange being cleaned.
642  * @size: the size of the subrange being cleaned.
643  * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
644  *          %DIRTY_MEMORY_VGA.
645  */
646 void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
647                                hwaddr size, unsigned client);
648 
649 /**
650  * memory_region_set_readonly: Turn a memory region read-only (or read-write)
651  *
652  * Allows a memory region to be marked as read-only (turning it into a ROM).
653  * only useful on RAM regions.
654  *
655  * @mr: the region being updated.
656  * @readonly: whether rhe region is to be ROM or RAM.
657  */
658 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
659 
660 /**
661  * memory_region_rom_device_set_romd: enable/disable ROMD mode
662  *
663  * Allows a ROM device (initialized with memory_region_init_rom_device() to
664  * set to ROMD mode (default) or MMIO mode.  When it is in ROMD mode, the
665  * device is mapped to guest memory and satisfies read access directly.
666  * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
667  * Writes are always handled by the #MemoryRegion.write function.
668  *
669  * @mr: the memory region to be updated
670  * @romd_mode: %true to put the region into ROMD mode
671  */
672 void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
673 
674 /**
675  * memory_region_set_coalescing: Enable memory coalescing for the region.
676  *
677  * Enabled writes to a region to be queued for later processing. MMIO ->write
678  * callbacks may be delayed until a non-coalesced MMIO is issued.
679  * Only useful for IO regions.  Roughly similar to write-combining hardware.
680  *
681  * @mr: the memory region to be write coalesced
682  */
683 void memory_region_set_coalescing(MemoryRegion *mr);
684 
685 /**
686  * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
687  *                               a region.
688  *
689  * Like memory_region_set_coalescing(), but works on a sub-range of a region.
690  * Multiple calls can be issued coalesced disjoint ranges.
691  *
692  * @mr: the memory region to be updated.
693  * @offset: the start of the range within the region to be coalesced.
694  * @size: the size of the subrange to be coalesced.
695  */
696 void memory_region_add_coalescing(MemoryRegion *mr,
697                                   hwaddr offset,
698                                   uint64_t size);
699 
700 /**
701  * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
702  *
703  * Disables any coalescing caused by memory_region_set_coalescing() or
704  * memory_region_add_coalescing().  Roughly equivalent to uncacheble memory
705  * hardware.
706  *
707  * @mr: the memory region to be updated.
708  */
709 void memory_region_clear_coalescing(MemoryRegion *mr);
710 
711 /**
712  * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
713  *                                    accesses.
714  *
715  * Ensure that pending coalesced MMIO request are flushed before the memory
716  * region is accessed. This property is automatically enabled for all regions
717  * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
718  *
719  * @mr: the memory region to be updated.
720  */
721 void memory_region_set_flush_coalesced(MemoryRegion *mr);
722 
723 /**
724  * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
725  *                                      accesses.
726  *
727  * Clear the automatic coalesced MMIO flushing enabled via
728  * memory_region_set_flush_coalesced. Note that this service has no effect on
729  * memory regions that have MMIO coalescing enabled for themselves. For them,
730  * automatic flushing will stop once coalescing is disabled.
731  *
732  * @mr: the memory region to be updated.
733  */
734 void memory_region_clear_flush_coalesced(MemoryRegion *mr);
735 
736 /**
737  * memory_region_add_eventfd: Request an eventfd to be triggered when a word
738  *                            is written to a location.
739  *
740  * Marks a word in an IO region (initialized with memory_region_init_io())
741  * as a trigger for an eventfd event.  The I/O callback will not be called.
742  * The caller must be prepared to handle failure (that is, take the required
743  * action if the callback _is_ called).
744  *
745  * @mr: the memory region being updated.
746  * @addr: the address within @mr that is to be monitored
747  * @size: the size of the access to trigger the eventfd
748  * @match_data: whether to match against @data, instead of just @addr
749  * @data: the data to match against the guest write
750  * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
751  **/
752 void memory_region_add_eventfd(MemoryRegion *mr,
753                                hwaddr addr,
754                                unsigned size,
755                                bool match_data,
756                                uint64_t data,
757                                EventNotifier *e);
758 
759 /**
760  * memory_region_del_eventfd: Cancel an eventfd.
761  *
762  * Cancels an eventfd trigger requested by a previous
763  * memory_region_add_eventfd() call.
764  *
765  * @mr: the memory region being updated.
766  * @addr: the address within @mr that is to be monitored
767  * @size: the size of the access to trigger the eventfd
768  * @match_data: whether to match against @data, instead of just @addr
769  * @data: the data to match against the guest write
770  * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
771  */
772 void memory_region_del_eventfd(MemoryRegion *mr,
773                                hwaddr addr,
774                                unsigned size,
775                                bool match_data,
776                                uint64_t data,
777                                EventNotifier *e);
778 
779 /**
780  * memory_region_add_subregion: Add a subregion to a container.
781  *
782  * Adds a subregion at @offset.  The subregion may not overlap with other
783  * subregions (except for those explicitly marked as overlapping).  A region
784  * may only be added once as a subregion (unless removed with
785  * memory_region_del_subregion()); use memory_region_init_alias() if you
786  * want a region to be a subregion in multiple locations.
787  *
788  * @mr: the region to contain the new subregion; must be a container
789  *      initialized with memory_region_init().
790  * @offset: the offset relative to @mr where @subregion is added.
791  * @subregion: the subregion to be added.
792  */
793 void memory_region_add_subregion(MemoryRegion *mr,
794                                  hwaddr offset,
795                                  MemoryRegion *subregion);
796 /**
797  * memory_region_add_subregion_overlap: Add a subregion to a container
798  *                                      with overlap.
799  *
800  * Adds a subregion at @offset.  The subregion may overlap with other
801  * subregions.  Conflicts are resolved by having a higher @priority hide a
802  * lower @priority. Subregions without priority are taken as @priority 0.
803  * A region may only be added once as a subregion (unless removed with
804  * memory_region_del_subregion()); use memory_region_init_alias() if you
805  * want a region to be a subregion in multiple locations.
806  *
807  * @mr: the region to contain the new subregion; must be a container
808  *      initialized with memory_region_init().
809  * @offset: the offset relative to @mr where @subregion is added.
810  * @subregion: the subregion to be added.
811  * @priority: used for resolving overlaps; highest priority wins.
812  */
813 void memory_region_add_subregion_overlap(MemoryRegion *mr,
814                                          hwaddr offset,
815                                          MemoryRegion *subregion,
816                                          int priority);
817 
818 /**
819  * memory_region_get_ram_addr: Get the ram address associated with a memory
820  *                             region
821  *
822  * DO NOT USE THIS FUNCTION.  This is a temporary workaround while the Xen
823  * code is being reworked.
824  */
825 ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
826 
827 /**
828  * memory_region_del_subregion: Remove a subregion.
829  *
830  * Removes a subregion from its container.
831  *
832  * @mr: the container to be updated.
833  * @subregion: the region being removed; must be a current subregion of @mr.
834  */
835 void memory_region_del_subregion(MemoryRegion *mr,
836                                  MemoryRegion *subregion);
837 
838 /*
839  * memory_region_set_enabled: dynamically enable or disable a region
840  *
841  * Enables or disables a memory region.  A disabled memory region
842  * ignores all accesses to itself and its subregions.  It does not
843  * obscure sibling subregions with lower priority - it simply behaves as
844  * if it was removed from the hierarchy.
845  *
846  * Regions default to being enabled.
847  *
848  * @mr: the region to be updated
849  * @enabled: whether to enable or disable the region
850  */
851 void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
852 
853 /*
854  * memory_region_set_address: dynamically update the address of a region
855  *
856  * Dynamically updates the address of a region, relative to its container.
857  * May be used on regions are currently part of a memory hierarchy.
858  *
859  * @mr: the region to be updated
860  * @addr: new address, relative to container region
861  */
862 void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
863 
864 /*
865  * memory_region_set_alias_offset: dynamically update a memory alias's offset
866  *
867  * Dynamically updates the offset into the target region that an alias points
868  * to, as if the fourth argument to memory_region_init_alias() has changed.
869  *
870  * @mr: the #MemoryRegion to be updated; should be an alias.
871  * @offset: the new offset into the target memory region
872  */
873 void memory_region_set_alias_offset(MemoryRegion *mr,
874                                     hwaddr offset);
875 
876 /**
877  * memory_region_present: checks if an address relative to a @container
878  * translates into #MemoryRegion within @container
879  *
880  * Answer whether a #MemoryRegion within @container covers the address
881  * @addr.
882  *
883  * @container: a #MemoryRegion within which @addr is a relative address
884  * @addr: the area within @container to be searched
885  */
886 bool memory_region_present(MemoryRegion *container, hwaddr addr);
887 
888 /**
889  * memory_region_is_mapped: returns true if #MemoryRegion is mapped
890  * into any address space.
891  *
892  * @mr: a #MemoryRegion which should be checked if it's mapped
893  */
894 bool memory_region_is_mapped(MemoryRegion *mr);
895 
896 /**
897  * memory_region_find: translate an address/size relative to a
898  * MemoryRegion into a #MemoryRegionSection.
899  *
900  * Locates the first #MemoryRegion within @mr that overlaps the range
901  * given by @addr and @size.
902  *
903  * Returns a #MemoryRegionSection that describes a contiguous overlap.
904  * It will have the following characteristics:
905  *    .@size = 0 iff no overlap was found
906  *    .@mr is non-%NULL iff an overlap was found
907  *
908  * Remember that in the return value the @offset_within_region is
909  * relative to the returned region (in the .@mr field), not to the
910  * @mr argument.
911  *
912  * Similarly, the .@offset_within_address_space is relative to the
913  * address space that contains both regions, the passed and the
914  * returned one.  However, in the special case where the @mr argument
915  * has no container (and thus is the root of the address space), the
916  * following will hold:
917  *    .@offset_within_address_space >= @addr
918  *    .@offset_within_address_space + .@size <= @addr + @size
919  *
920  * @mr: a MemoryRegion within which @addr is a relative address
921  * @addr: start of the area within @as to be searched
922  * @size: size of the area to be searched
923  */
924 MemoryRegionSection memory_region_find(MemoryRegion *mr,
925                                        hwaddr addr, uint64_t size);
926 
927 /**
928  * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
929  *
930  * Synchronizes the dirty page log for an entire address space.
931  * @as: the address space that contains the memory being synchronized
932  */
933 void address_space_sync_dirty_bitmap(AddressSpace *as);
934 
935 /**
936  * memory_region_transaction_begin: Start a transaction.
937  *
938  * During a transaction, changes will be accumulated and made visible
939  * only when the transaction ends (is committed).
940  */
941 void memory_region_transaction_begin(void);
942 
943 /**
944  * memory_region_transaction_commit: Commit a transaction and make changes
945  *                                   visible to the guest.
946  */
947 void memory_region_transaction_commit(void);
948 
949 /**
950  * memory_listener_register: register callbacks to be called when memory
951  *                           sections are mapped or unmapped into an address
952  *                           space
953  *
954  * @listener: an object containing the callbacks to be called
955  * @filter: if non-%NULL, only regions in this address space will be observed
956  */
957 void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
958 
959 /**
960  * memory_listener_unregister: undo the effect of memory_listener_register()
961  *
962  * @listener: an object containing the callbacks to be removed
963  */
964 void memory_listener_unregister(MemoryListener *listener);
965 
966 /**
967  * memory_global_dirty_log_start: begin dirty logging for all regions
968  */
969 void memory_global_dirty_log_start(void);
970 
971 /**
972  * memory_global_dirty_log_stop: end dirty logging for all regions
973  */
974 void memory_global_dirty_log_stop(void);
975 
976 void mtree_info(fprintf_function mon_printf, void *f);
977 
978 /**
979  * address_space_init: initializes an address space
980  *
981  * @as: an uninitialized #AddressSpace
982  * @root: a #MemoryRegion that routes addesses for the address space
983  * @name: an address space name.  The name is only used for debugging
984  *        output.
985  */
986 void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
987 
988 
989 /**
990  * address_space_destroy: destroy an address space
991  *
992  * Releases all resources associated with an address space.  After an address space
993  * is destroyed, its root memory region (given by address_space_init()) may be destroyed
994  * as well.
995  *
996  * @as: address space to be destroyed
997  */
998 void address_space_destroy(AddressSpace *as);
999 
1000 /**
1001  * address_space_rw: read from or write to an address space.
1002  *
1003  * Return true if the operation hit any unassigned memory or encountered an
1004  * IOMMU fault.
1005  *
1006  * @as: #AddressSpace to be accessed
1007  * @addr: address within that address space
1008  * @buf: buffer with the data transferred
1009  * @is_write: indicates the transfer direction
1010  */
1011 bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
1012                       int len, bool is_write);
1013 
1014 /**
1015  * address_space_write: write to address space.
1016  *
1017  * Return true if the operation hit any unassigned memory or encountered an
1018  * IOMMU fault.
1019  *
1020  * @as: #AddressSpace to be accessed
1021  * @addr: address within that address space
1022  * @buf: buffer with the data transferred
1023  */
1024 bool address_space_write(AddressSpace *as, hwaddr addr,
1025                          const uint8_t *buf, int len);
1026 
1027 /**
1028  * address_space_read: read from an address space.
1029  *
1030  * Return true if the operation hit any unassigned memory or encountered an
1031  * IOMMU fault.
1032  *
1033  * @as: #AddressSpace to be accessed
1034  * @addr: address within that address space
1035  * @buf: buffer with the data transferred
1036  */
1037 bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
1038 
1039 /* address_space_translate: translate an address range into an address space
1040  * into a MemoryRegion and an address range into that section
1041  *
1042  * @as: #AddressSpace to be accessed
1043  * @addr: address within that address space
1044  * @xlat: pointer to address within the returned memory region section's
1045  * #MemoryRegion.
1046  * @len: pointer to length
1047  * @is_write: indicates the transfer direction
1048  */
1049 MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1050                                       hwaddr *xlat, hwaddr *len,
1051                                       bool is_write);
1052 
1053 /* address_space_access_valid: check for validity of accessing an address
1054  * space range
1055  *
1056  * Check whether memory is assigned to the given address space range, and
1057  * access is permitted by any IOMMU regions that are active for the address
1058  * space.
1059  *
1060  * For now, addr and len should be aligned to a page size.  This limitation
1061  * will be lifted in the future.
1062  *
1063  * @as: #AddressSpace to be accessed
1064  * @addr: address within that address space
1065  * @len: length of the area to be checked
1066  * @is_write: indicates the transfer direction
1067  */
1068 bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1069 
1070 /* address_space_map: map a physical memory region into a host virtual address
1071  *
1072  * May map a subset of the requested range, given by and returned in @plen.
1073  * May return %NULL if resources needed to perform the mapping are exhausted.
1074  * Use only for reads OR writes - not for read-modify-write operations.
1075  * Use cpu_register_map_client() to know when retrying the map operation is
1076  * likely to succeed.
1077  *
1078  * @as: #AddressSpace to be accessed
1079  * @addr: address within that address space
1080  * @plen: pointer to length of buffer; updated on return
1081  * @is_write: indicates the transfer direction
1082  */
1083 void *address_space_map(AddressSpace *as, hwaddr addr,
1084                         hwaddr *plen, bool is_write);
1085 
1086 /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1087  *
1088  * Will also mark the memory as dirty if @is_write == %true.  @access_len gives
1089  * the amount of memory that was actually read or written by the caller.
1090  *
1091  * @as: #AddressSpace used
1092  * @addr: address within that address space
1093  * @len: buffer length as returned by address_space_map()
1094  * @access_len: amount of data actually transferred
1095  * @is_write: indicates the transfer direction
1096  */
1097 void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1098                          int is_write, hwaddr access_len);
1099 
1100 
1101 #endif
1102 
1103 #endif
1104