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