vm_manager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <map>
  6. #include <memory>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. #include "core/hle/result.h"
  10. #include "core/memory.h"
  11. #include "core/memory_hook.h"
  12. namespace FileSys {
  13. enum class ProgramAddressSpaceType : u8;
  14. }
  15. namespace Kernel {
  16. enum class VMAType : u8 {
  17. /// VMA represents an unmapped region of the address space.
  18. Free,
  19. /// VMA is backed by a ref-counted allocate memory block.
  20. AllocatedMemoryBlock,
  21. /// VMA is backed by a raw, unmanaged pointer.
  22. BackingMemory,
  23. /// VMA is mapped to MMIO registers at a fixed PAddr.
  24. MMIO,
  25. // TODO(yuriks): Implement MemoryAlias to support MAP/UNMAP
  26. };
  27. /// Permissions for mapped memory blocks
  28. enum class VMAPermission : u8 {
  29. None = 0,
  30. Read = 1,
  31. Write = 2,
  32. Execute = 4,
  33. ReadWrite = Read | Write,
  34. ReadExecute = Read | Execute,
  35. WriteExecute = Write | Execute,
  36. ReadWriteExecute = Read | Write | Execute,
  37. };
  38. /// Set of values returned in MemoryInfo.state by svcQueryMemory.
  39. enum class MemoryState : u32 {
  40. Unmapped = 0x0,
  41. Io = 0x1,
  42. Normal = 0x2,
  43. CodeStatic = 0x3,
  44. CodeMutable = 0x4,
  45. Heap = 0x5,
  46. Shared = 0x6,
  47. ModuleCodeStatic = 0x8,
  48. ModuleCodeMutable = 0x9,
  49. IpcBuffer0 = 0xA,
  50. Mapped = 0xB,
  51. ThreadLocal = 0xC,
  52. TransferMemoryIsolated = 0xD,
  53. TransferMemory = 0xE,
  54. ProcessMemory = 0xF,
  55. IpcBuffer1 = 0x11,
  56. IpcBuffer3 = 0x12,
  57. KernelStack = 0x13,
  58. };
  59. /**
  60. * Represents a VMA in an address space. A VMA is a contiguous region of virtual addressing space
  61. * with homogeneous attributes across its extents. In this particular implementation each VMA is
  62. * also backed by a single host memory allocation.
  63. */
  64. struct VirtualMemoryArea {
  65. /// Virtual base address of the region.
  66. VAddr base = 0;
  67. /// Size of the region.
  68. u64 size = 0;
  69. VMAType type = VMAType::Free;
  70. VMAPermission permissions = VMAPermission::None;
  71. /// Tag returned by svcQueryMemory. Not otherwise used.
  72. MemoryState meminfo_state = MemoryState::Unmapped;
  73. // Settings for type = AllocatedMemoryBlock
  74. /// Memory block backing this VMA.
  75. std::shared_ptr<std::vector<u8>> backing_block = nullptr;
  76. /// Offset into the backing_memory the mapping starts from.
  77. std::size_t offset = 0;
  78. // Settings for type = BackingMemory
  79. /// Pointer backing this VMA. It will not be destroyed or freed when the VMA is removed.
  80. u8* backing_memory = nullptr;
  81. // Settings for type = MMIO
  82. /// Physical address of the register area this VMA maps to.
  83. PAddr paddr = 0;
  84. Memory::MemoryHookPointer mmio_handler = nullptr;
  85. /// Tests if this area can be merged to the right with `next`.
  86. bool CanBeMergedWith(const VirtualMemoryArea& next) const;
  87. };
  88. /**
  89. * Manages a process' virtual addressing space. This class maintains a list of allocated and free
  90. * regions in the address space, along with their attributes, and allows kernel clients to
  91. * manipulate it, adjusting the page table to match.
  92. *
  93. * This is similar in idea and purpose to the VM manager present in operating system kernels, with
  94. * the main difference being that it doesn't have to support swapping or memory mapping of files.
  95. * The implementation is also simplified by not having to allocate page frames. See these articles
  96. * about the Linux kernel for an explantion of the concept and implementation:
  97. * - http://duartes.org/gustavo/blog/post/how-the-kernel-manages-your-memory/
  98. * - http://duartes.org/gustavo/blog/post/page-cache-the-affair-between-memory-and-files/
  99. */
  100. class VMManager final {
  101. public:
  102. /**
  103. * A map covering the entirety of the managed address space, keyed by the `base` field of each
  104. * VMA. It must always be modified by splitting or merging VMAs, so that the invariant
  105. * `elem.base + elem.size == next.base` is preserved, and mergeable regions must always be
  106. * merged when possible so that no two similar and adjacent regions exist that have not been
  107. * merged.
  108. */
  109. std::map<VAddr, VirtualMemoryArea> vma_map;
  110. using VMAHandle = decltype(vma_map)::const_iterator;
  111. VMManager();
  112. ~VMManager();
  113. /// Clears the address space map, re-initializing with a single free area.
  114. void Reset(FileSys::ProgramAddressSpaceType type);
  115. /// Finds the VMA in which the given address is included in, or `vma_map.end()`.
  116. VMAHandle FindVMA(VAddr target) const;
  117. // TODO(yuriks): Should these functions actually return the handle?
  118. /**
  119. * Maps part of a ref-counted block of memory at a given address.
  120. *
  121. * @param target The guest address to start the mapping at.
  122. * @param block The block to be mapped.
  123. * @param offset Offset into `block` to map from.
  124. * @param size Size of the mapping.
  125. * @param state MemoryState tag to attach to the VMA.
  126. */
  127. ResultVal<VMAHandle> MapMemoryBlock(VAddr target, std::shared_ptr<std::vector<u8>> block,
  128. std::size_t offset, u64 size, MemoryState state);
  129. /**
  130. * Maps an unmanaged host memory pointer at a given address.
  131. *
  132. * @param target The guest address to start the mapping at.
  133. * @param memory The memory to be mapped.
  134. * @param size Size of the mapping.
  135. * @param state MemoryState tag to attach to the VMA.
  136. */
  137. ResultVal<VMAHandle> MapBackingMemory(VAddr target, u8* memory, u64 size, MemoryState state);
  138. /**
  139. * Finds the first free address that can hold a region of the desired size.
  140. *
  141. * @param size Size of the desired region.
  142. * @return The found free address.
  143. */
  144. ResultVal<VAddr> FindFreeRegion(u64 size) const;
  145. /**
  146. * Maps a memory-mapped IO region at a given address.
  147. *
  148. * @param target The guest address to start the mapping at.
  149. * @param paddr The physical address where the registers are present.
  150. * @param size Size of the mapping.
  151. * @param state MemoryState tag to attach to the VMA.
  152. * @param mmio_handler The handler that will implement read and write for this MMIO region.
  153. */
  154. ResultVal<VMAHandle> MapMMIO(VAddr target, PAddr paddr, u64 size, MemoryState state,
  155. Memory::MemoryHookPointer mmio_handler);
  156. /// Unmaps a range of addresses, splitting VMAs as necessary.
  157. ResultCode UnmapRange(VAddr target, u64 size);
  158. /// Changes the permissions of the given VMA.
  159. VMAHandle Reprotect(VMAHandle vma, VMAPermission new_perms);
  160. /// Changes the permissions of a range of addresses, splitting VMAs as necessary.
  161. ResultCode ReprotectRange(VAddr target, u64 size, VMAPermission new_perms);
  162. /**
  163. * Scans all VMAs and updates the page table range of any that use the given vector as backing
  164. * memory. This should be called after any operation that causes reallocation of the vector.
  165. */
  166. void RefreshMemoryBlockMappings(const std::vector<u8>* block);
  167. /// Dumps the address space layout to the log, for debugging
  168. void LogLayout() const;
  169. /// Gets the total memory usage, used by svcGetInfo
  170. u64 GetTotalMemoryUsage() const;
  171. /// Gets the total heap usage, used by svcGetInfo
  172. u64 GetTotalHeapUsage() const;
  173. /// Gets the address space base address
  174. VAddr GetAddressSpaceBaseAddress() const;
  175. /// Gets the address space end address
  176. VAddr GetAddressSpaceEndAddress() const;
  177. /// Gets the total address space address size in bytes
  178. u64 GetAddressSpaceSize() const;
  179. /// Gets the address space width in bits.
  180. u64 GetAddressSpaceWidth() const;
  181. /// Gets the base address of the ASLR region.
  182. VAddr GetASLRRegionBaseAddress() const;
  183. /// Gets the end address of the ASLR region.
  184. VAddr GetASLRRegionEndAddress() const;
  185. /// Determines whether or not the specified address range is within the ASLR region.
  186. bool IsWithinASLRRegion(VAddr address, u64 size) const;
  187. /// Gets the size of the ASLR region
  188. u64 GetASLRRegionSize() const;
  189. /// Gets the base address of the code region.
  190. VAddr GetCodeRegionBaseAddress() const;
  191. /// Gets the end address of the code region.
  192. VAddr GetCodeRegionEndAddress() const;
  193. /// Gets the total size of the code region in bytes.
  194. u64 GetCodeRegionSize() const;
  195. /// Gets the base address of the heap region.
  196. VAddr GetHeapRegionBaseAddress() const;
  197. /// Gets the end address of the heap region;
  198. VAddr GetHeapRegionEndAddress() const;
  199. /// Gets the total size of the heap region in bytes.
  200. u64 GetHeapRegionSize() const;
  201. /// Gets the base address of the map region.
  202. VAddr GetMapRegionBaseAddress() const;
  203. /// Gets the end address of the map region.
  204. VAddr GetMapRegionEndAddress() const;
  205. /// Gets the total size of the map region in bytes.
  206. u64 GetMapRegionSize() const;
  207. /// Gets the base address of the new map region.
  208. VAddr GetNewMapRegionBaseAddress() const;
  209. /// Gets the end address of the new map region.
  210. VAddr GetNewMapRegionEndAddress() const;
  211. /// Gets the total size of the new map region in bytes.
  212. u64 GetNewMapRegionSize() const;
  213. /// Gets the base address of the TLS IO region.
  214. VAddr GetTLSIORegionBaseAddress() const;
  215. /// Gets the end address of the TLS IO region.
  216. VAddr GetTLSIORegionEndAddress() const;
  217. /// Gets the total size of the TLS IO region in bytes.
  218. u64 GetTLSIORegionSize() const;
  219. /// Each VMManager has its own page table, which is set as the main one when the owning process
  220. /// is scheduled.
  221. Memory::PageTable page_table;
  222. private:
  223. using VMAIter = decltype(vma_map)::iterator;
  224. /// Converts a VMAHandle to a mutable VMAIter.
  225. VMAIter StripIterConstness(const VMAHandle& iter);
  226. /// Unmaps the given VMA.
  227. VMAIter Unmap(VMAIter vma);
  228. /**
  229. * Carves a VMA of a specific size at the specified address by splitting Free VMAs while doing
  230. * the appropriate error checking.
  231. */
  232. ResultVal<VMAIter> CarveVMA(VAddr base, u64 size);
  233. /**
  234. * Splits the edges of the given range of non-Free VMAs so that there is a VMA split at each
  235. * end of the range.
  236. */
  237. ResultVal<VMAIter> CarveVMARange(VAddr base, u64 size);
  238. /**
  239. * Splits a VMA in two, at the specified offset.
  240. * @returns the right side of the split, with the original iterator becoming the left side.
  241. */
  242. VMAIter SplitVMA(VMAIter vma, u64 offset_in_vma);
  243. /**
  244. * Checks for and merges the specified VMA with adjacent ones if possible.
  245. * @returns the merged VMA or the original if no merging was possible.
  246. */
  247. VMAIter MergeAdjacent(VMAIter vma);
  248. /// Updates the pages corresponding to this VMA so they match the VMA's attributes.
  249. void UpdatePageTableForVMA(const VirtualMemoryArea& vma);
  250. /// Initializes memory region ranges to adhere to a given address space type.
  251. void InitializeMemoryRegionRanges(FileSys::ProgramAddressSpaceType type);
  252. /// Clears the underlying map and page table.
  253. void Clear();
  254. /// Clears out the VMA map, unmapping any previously mapped ranges.
  255. void ClearVMAMap();
  256. /// Clears out the page table
  257. void ClearPageTable();
  258. u32 address_space_width = 0;
  259. VAddr address_space_base = 0;
  260. VAddr address_space_end = 0;
  261. VAddr aslr_region_base = 0;
  262. VAddr aslr_region_end = 0;
  263. VAddr code_region_base = 0;
  264. VAddr code_region_end = 0;
  265. VAddr heap_region_base = 0;
  266. VAddr heap_region_end = 0;
  267. VAddr map_region_base = 0;
  268. VAddr map_region_end = 0;
  269. VAddr new_map_region_base = 0;
  270. VAddr new_map_region_end = 0;
  271. VAddr tls_io_region_base = 0;
  272. VAddr tls_io_region_end = 0;
  273. };
  274. } // namespace Kernel