memory.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 a tree (really acyclic graph) of MemoryRegion objects.
  14. The root of the tree is memory as seen from the CPU's viewpoint (the system
  15. bus). Nodes in the tree represent other buses, memory controllers, and
  16. memory regions that have been rerouted. Leaves are RAM and MMIO regions.
  17. Types of regions
  18. ----------------
  19. There are four types of memory regions (all represented by a single C type
  20. MemoryRegion):
  21. - RAM: a RAM region is simply a range of host memory that can be made available
  22. to the guest.
  23. - MMIO: a range of guest memory that is implemented by host callbacks;
  24. each read or write causes a callback to be called on the host.
  25. - container: a container simply includes other memory regions, each at
  26. a different offset. Containers are useful for grouping several regions
  27. into one unit. For example, a PCI BAR may be composed of a RAM region
  28. and an MMIO region.
  29. A container's subregions are usually non-overlapping. In some cases it is
  30. useful to have overlapping regions; for example a memory controller that
  31. can overlay a subregion of RAM with MMIO or ROM, or a PCI controller
  32. that does not prevent card from claiming overlapping BARs.
  33. - alias: a subsection of another region. Aliases allow a region to be
  34. split apart into discontiguous regions. Examples of uses are memory banks
  35. used when the guest address space is smaller than the amount of RAM
  36. addressed, or a memory controller that splits main memory to expose a "PCI
  37. hole". Aliases may point to any type of region, including other aliases,
  38. but an alias may not point back to itself, directly or indirectly.
  39. Region names
  40. ------------
  41. Regions are assigned names by the constructor. For most regions these are
  42. only used for debugging purposes, but RAM regions also use the name to identify
  43. live migration sections. This means that RAM region names need to have ABI
  44. stability.
  45. Region lifecycle
  46. ----------------
  47. A region is created by one of the constructor functions (memory_region_init*())
  48. and destroyed by the destructor (memory_region_destroy()). In between,
  49. a region can be added to an address space by using memory_region_add_subregion()
  50. and removed using memory_region_del_subregion(). Region attributes may be
  51. changed at any point; they take effect once the region becomes exposed to the
  52. guest.
  53. Overlapping regions and priority
  54. --------------------------------
  55. Usually, regions may not overlap each other; a memory address decodes into
  56. exactly one target. In some cases it is useful to allow regions to overlap,
  57. and sometimes to control which of an overlapping regions is visible to the
  58. guest. This is done with memory_region_add_subregion_overlap(), which
  59. allows the region to overlap any other region in the same container, and
  60. specifies a priority that allows the core to decide which of two regions at
  61. the same address are visible (highest wins).
  62. Visibility
  63. ----------
  64. The memory core uses the following rules to select a memory region when the
  65. guest accesses an address:
  66. - all direct subregions of the root region are matched against the address, in
  67. descending priority order
  68. - if the address lies outside the region offset/size, the subregion is
  69. discarded
  70. - if the subregion is a leaf (RAM or MMIO), the search terminates
  71. - if the subregion is a container, the same algorithm is used within the
  72. subregion (after the address is adjusted by the subregion offset)
  73. - if the subregion is an alias, the search is continues at the alias target
  74. (after the address is adjusted by the subregion offset and alias offset)
  75. Example memory map
  76. ------------------
  77. system_memory: container@0-2^48-1
  78. |
  79. +---- lomem: alias@0-0xdfffffff ---> #ram (0-0xdfffffff)
  80. |
  81. +---- himem: alias@0x100000000-0x11fffffff ---> #ram (0xe0000000-0xffffffff)
  82. |
  83. +---- vga-window: alias@0xa0000-0xbfffff ---> #pci (0xa0000-0xbffff)
  84. | (prio 1)
  85. |
  86. +---- pci-hole: alias@0xe0000000-0xffffffff ---> #pci (0xe0000000-0xffffffff)
  87. pci (0-2^32-1)
  88. |
  89. +--- vga-area: container@0xa0000-0xbffff
  90. | |
  91. | +--- alias@0x00000-0x7fff ---> #vram (0x010000-0x017fff)
  92. | |
  93. | +--- alias@0x08000-0xffff ---> #vram (0x020000-0x027fff)
  94. |
  95. +---- vram: ram@0xe1000000-0xe1ffffff
  96. |
  97. +---- vga-mmio: mmio@0xe2000000-0xe200ffff
  98. ram: ram@0x00000000-0xffffffff
  99. This is a (simplified) PC memory map. The 4GB RAM block is mapped into the
  100. system address space via two aliases: "lomem" is a 1:1 mapping of the first
  101. 3.5GB; "himem" maps the last 0.5GB at address 4GB. This leaves 0.5GB for the
  102. so-called PCI hole, that allows a 32-bit PCI bus to exist in a system with
  103. 4GB of memory.
  104. The memory controller diverts addresses in the range 640K-768K to the PCI
  105. address space. This is modelled using the "vga-window" alias, mapped at a
  106. higher priority so it obscures the RAM at the same addresses. The vga window
  107. can be removed by programming the memory controller; this is modelled by
  108. removing the alias and exposing the RAM underneath.
  109. The pci address space is not a direct child of the system address space, since
  110. we only want parts of it to be visible (we accomplish this using aliases).
  111. It has two subregions: vga-area models the legacy vga window and is occupied
  112. by two 32K memory banks pointing at two sections of the framebuffer.
  113. In addition the vram is mapped as a BAR at address e1000000, and an additional
  114. BAR containing MMIO registers is mapped after it.
  115. Note that if the guest maps a BAR outside the PCI hole, it would not be
  116. visible as the pci-hole alias clips it to a 0.5GB range.
  117. Attributes
  118. ----------
  119. Various region attributes (read-only, dirty logging, coalesced mmio, ioeventfd)
  120. can be changed during the region lifecycle. They take effect once the region
  121. is made visible (which can be immediately, later, or never).
  122. MMIO Operations
  123. ---------------
  124. MMIO regions are provided with ->read() and ->write() callbacks; in addition
  125. various constraints can be supplied to control how these callbacks are called:
  126. - .valid.min_access_size, .valid.max_access_size define the access sizes
  127. (in bytes) which the device accepts; accesses outside this range will
  128. have device and bus specific behaviour (ignored, or machine check)
  129. - .valid.aligned specifies that the device only accepts naturally aligned
  130. accesses. Unaligned accesses invoke device and bus specific behaviour.
  131. - .impl.min_access_size, .impl.max_access_size define the access sizes
  132. (in bytes) supported by the *implementation*; other access sizes will be
  133. emulated using the ones available. For example a 4-byte write will be
  134. emulated using four 1-byte writes, if .impl.max_access_size = 1.
  135. - .impl.valid specifies that the *implementation* only supports unaligned
  136. accesses; unaligned accesses will be emulated by two aligned accesses.
  137. - .old_portio and .old_mmio can be used to ease porting from code using
  138. cpu_register_io_memory() and register_ioport(). They should not be used
  139. in new code.