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