memory.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. The memory API
  2. ==============
  3. The memory API models the memory and I/O buses and controllers of a QEMU
  4. machine. It attempts to allow modelling of:
  5. - ordinary RAM
  6. - memory-mapped I/O (MMIO)
  7. - memory controllers that can dynamically reroute physical memory regions
  8. to different destinations
  9. The memory model provides support for
  10. - tracking RAM changes by the guest
  11. - setting up coalesced memory for kvm
  12. - setting up ioeventfd regions for kvm
  13. Memory is modelled as an acyclic graph of MemoryRegion objects. Sinks
  14. (leaves) are RAM and MMIO regions, while other nodes represent
  15. buses, memory controllers, and memory regions that have been rerouted.
  16. In addition to MemoryRegion objects, the memory API provides AddressSpace
  17. objects for every root and possibly for intermediate MemoryRegions too.
  18. These represent memory as seen from the CPU or a device's viewpoint.
  19. Types of regions
  20. ----------------
  21. There are four types of memory regions (all represented by a single C type
  22. MemoryRegion):
  23. - RAM: a RAM region is simply a range of host memory that can be made available
  24. to the guest.
  25. - MMIO: a range of guest memory that is implemented by host callbacks;
  26. each read or write causes a callback to be called on the host.
  27. - container: a container simply includes other memory regions, each at
  28. a different offset. Containers are useful for grouping several regions
  29. into one unit. For example, a PCI BAR may be composed of a RAM region
  30. and an MMIO region.
  31. A container's subregions are usually non-overlapping. In some cases it is
  32. useful to have overlapping regions; for example a memory controller that
  33. can overlay a subregion of RAM with MMIO or ROM, or a PCI controller
  34. that does not prevent card from claiming overlapping BARs.
  35. - alias: a subsection of another region. Aliases allow a region to be
  36. split apart into discontiguous regions. Examples of uses are memory banks
  37. used when the guest address space is smaller than the amount of RAM
  38. addressed, or a memory controller that splits main memory to expose a "PCI
  39. hole". Aliases may point to any type of region, including other aliases,
  40. but an alias may not point back to itself, directly or indirectly.
  41. It is valid to add subregions to a region which is not a pure container
  42. (that is, to an MMIO, RAM or ROM region). This means that the region
  43. will act like a container, except that any addresses within the container's
  44. region which are not claimed by any subregion are handled by the
  45. container itself (ie by its MMIO callbacks or RAM backing). However
  46. it is generally possible to achieve the same effect with a pure container
  47. one of whose subregions is a low priority "background" region covering
  48. the whole address range; this is often clearer and is preferred.
  49. Subregions cannot be added to an alias region.
  50. Region names
  51. ------------
  52. Regions are assigned names by the constructor. For most regions these are
  53. only used for debugging purposes, but RAM regions also use the name to identify
  54. live migration sections. This means that RAM region names need to have ABI
  55. stability.
  56. Region lifecycle
  57. ----------------
  58. A region is created by one of the constructor functions (memory_region_init*())
  59. and destroyed by the destructor (memory_region_destroy()). In between,
  60. a region can be added to an address space by using memory_region_add_subregion()
  61. and removed using memory_region_del_subregion(). Region attributes may be
  62. changed at any point; they take effect once the region becomes exposed to the
  63. guest.
  64. Overlapping regions and priority
  65. --------------------------------
  66. Usually, regions may not overlap each other; a memory address decodes into
  67. exactly one target. In some cases it is useful to allow regions to overlap,
  68. and sometimes to control which of an overlapping regions is visible to the
  69. guest. This is done with memory_region_add_subregion_overlap(), which
  70. allows the region to overlap any other region in the same container, and
  71. specifies a priority that allows the core to decide which of two regions at
  72. the same address are visible (highest wins).
  73. Priority values are signed, and the default value is zero. This means that
  74. you can use memory_region_add_subregion_overlap() both to specify a region
  75. that must sit 'above' any others (with a positive priority) and also a
  76. background region that sits 'below' others (with a negative priority).
  77. If the higher priority region in an overlap is a container or alias, then
  78. the lower priority region will appear in any "holes" that the higher priority
  79. region has left by not mapping subregions to that area of its address range.
  80. (This applies recursively -- if the subregions are themselves containers or
  81. aliases that leave holes then the lower priority region will appear in these
  82. holes too.)
  83. For example, suppose we have a container A of size 0x8000 with two subregions
  84. B and C. B is a container mapped at 0x2000, size 0x4000, priority 1; C is
  85. an MMIO region mapped at 0x0, size 0x6000, priority 2. B currently has two
  86. of its own subregions: D of size 0x1000 at offset 0 and E of size 0x1000 at
  87. offset 0x2000. As a diagram:
  88. 0 1000 2000 3000 4000 5000 6000 7000 8000
  89. |------|------|------|------|------|------|------|-------|
  90. A: [ ]
  91. C: [CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC]
  92. B: [ ]
  93. D: [DDDDD]
  94. E: [EEEEE]
  95. The regions that will be seen within this address range then are:
  96. [CCCCCCCCCCCC][DDDDD][CCCCC][EEEEE][CCCCC]
  97. Since B has higher priority than C, its subregions appear in the flat map
  98. even where they overlap with C. In ranges where B has not mapped anything
  99. C's region appears.
  100. If B had provided its own MMIO operations (ie it was not a pure container)
  101. then these would be used for any addresses in its range not handled by
  102. D or E, and the result would be:
  103. [CCCCCCCCCCCC][DDDDD][BBBBB][EEEEE][BBBBB]
  104. Priority values are local to a container, because the priorities of two
  105. regions are only compared when they are both children of the same container.
  106. This means that the device in charge of the container (typically modelling
  107. a bus or a memory controller) can use them to manage the interaction of
  108. its child regions without any side effects on other parts of the system.
  109. In the example above, the priorities of D and E are unimportant because
  110. they do not overlap each other. It is the relative priority of B and C
  111. that causes D and E to appear on top of C: D and E's priorities are never
  112. compared against the priority of C.
  113. Visibility
  114. ----------
  115. The memory core uses the following rules to select a memory region when the
  116. guest accesses an address:
  117. - all direct subregions of the root region are matched against the address, in
  118. descending priority order
  119. - if the address lies outside the region offset/size, the subregion is
  120. discarded
  121. - if the subregion is a leaf (RAM or MMIO), the search terminates, returning
  122. this leaf region
  123. - if the subregion is a container, the same algorithm is used within the
  124. subregion (after the address is adjusted by the subregion offset)
  125. - if the subregion is an alias, the search is continued at the alias target
  126. (after the address is adjusted by the subregion offset and alias offset)
  127. - if a recursive search within a container or alias subregion does not
  128. find a match (because of a "hole" in the container's coverage of its
  129. address range), then if this is a container with its own MMIO or RAM
  130. backing the search terminates, returning the container itself. Otherwise
  131. we continue with the next subregion in priority order
  132. - if none of the subregions match the address then the search terminates
  133. with no match found
  134. Example memory map
  135. ------------------
  136. system_memory: container@0-2^48-1
  137. |
  138. +---- lomem: alias@0-0xdfffffff ---> #ram (0-0xdfffffff)
  139. |
  140. +---- himem: alias@0x100000000-0x11fffffff ---> #ram (0xe0000000-0xffffffff)
  141. |
  142. +---- vga-window: alias@0xa0000-0xbfffff ---> #pci (0xa0000-0xbffff)
  143. | (prio 1)
  144. |
  145. +---- pci-hole: alias@0xe0000000-0xffffffff ---> #pci (0xe0000000-0xffffffff)
  146. pci (0-2^32-1)
  147. |
  148. +--- vga-area: container@0xa0000-0xbffff
  149. | |
  150. | +--- alias@0x00000-0x7fff ---> #vram (0x010000-0x017fff)
  151. | |
  152. | +--- alias@0x08000-0xffff ---> #vram (0x020000-0x027fff)
  153. |
  154. +---- vram: ram@0xe1000000-0xe1ffffff
  155. |
  156. +---- vga-mmio: mmio@0xe2000000-0xe200ffff
  157. ram: ram@0x00000000-0xffffffff
  158. This is a (simplified) PC memory map. The 4GB RAM block is mapped into the
  159. system address space via two aliases: "lomem" is a 1:1 mapping of the first
  160. 3.5GB; "himem" maps the last 0.5GB at address 4GB. This leaves 0.5GB for the
  161. so-called PCI hole, that allows a 32-bit PCI bus to exist in a system with
  162. 4GB of memory.
  163. The memory controller diverts addresses in the range 640K-768K to the PCI
  164. address space. This is modelled using the "vga-window" alias, mapped at a
  165. higher priority so it obscures the RAM at the same addresses. The vga window
  166. can be removed by programming the memory controller; this is modelled by
  167. removing the alias and exposing the RAM underneath.
  168. The pci address space is not a direct child of the system address space, since
  169. we only want parts of it to be visible (we accomplish this using aliases).
  170. It has two subregions: vga-area models the legacy vga window and is occupied
  171. by two 32K memory banks pointing at two sections of the framebuffer.
  172. In addition the vram is mapped as a BAR at address e1000000, and an additional
  173. BAR containing MMIO registers is mapped after it.
  174. Note that if the guest maps a BAR outside the PCI hole, it would not be
  175. visible as the pci-hole alias clips it to a 0.5GB range.
  176. Attributes
  177. ----------
  178. Various region attributes (read-only, dirty logging, coalesced mmio, ioeventfd)
  179. can be changed during the region lifecycle. They take effect once the region
  180. is made visible (which can be immediately, later, or never).
  181. MMIO Operations
  182. ---------------
  183. MMIO regions are provided with ->read() and ->write() callbacks; in addition
  184. various constraints can be supplied to control how these callbacks are called:
  185. - .valid.min_access_size, .valid.max_access_size define the access sizes
  186. (in bytes) which the device accepts; accesses outside this range will
  187. have device and bus specific behaviour (ignored, or machine check)
  188. - .valid.aligned specifies that the device only accepts naturally aligned
  189. accesses. Unaligned accesses invoke device and bus specific behaviour.
  190. - .impl.min_access_size, .impl.max_access_size define the access sizes
  191. (in bytes) supported by the *implementation*; other access sizes will be
  192. emulated using the ones available. For example a 4-byte write will be
  193. emulated using four 1-byte writes, if .impl.max_access_size = 1.
  194. - .impl.unaligned specifies that the *implementation* supports unaligned
  195. accesses; if false, unaligned accesses will be emulated by two aligned
  196. accesses.
  197. - .old_mmio can be used to ease porting from code using
  198. cpu_register_io_memory(). It should not be used in new code.