vm_manager.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <iterator>
  5. #include "common/assert.h"
  6. #include "core/hle/kernel/vm_manager.h"
  7. #include "core/memory_setup.h"
  8. #include "core/mmio.h"
  9. namespace Kernel {
  10. static const char* GetMemoryStateName(MemoryState state) {
  11. static const char* names[] = {
  12. "Free", "Reserved", "IO", "Static", "Code", "Private", "Shared", "Continuous", "Aliased",
  13. "Alias", "AliasCode", "Locked",
  14. };
  15. return names[(int)state];
  16. }
  17. bool VirtualMemoryArea::CanBeMergedWith(const VirtualMemoryArea& next) const {
  18. ASSERT(base + size == next.base);
  19. if (permissions != next.permissions ||
  20. meminfo_state != next.meminfo_state ||
  21. type != next.type) {
  22. return false;
  23. }
  24. if (type == VMAType::AllocatedMemoryBlock &&
  25. (backing_block != next.backing_block || offset + size != next.offset)) {
  26. return false;
  27. }
  28. if (type == VMAType::BackingMemory && backing_memory + size != next.backing_memory) {
  29. return false;
  30. }
  31. if (type == VMAType::MMIO && paddr + size != next.paddr) {
  32. return false;
  33. }
  34. return true;
  35. }
  36. VMManager::VMManager() {
  37. Reset();
  38. }
  39. VMManager::~VMManager() {
  40. Reset();
  41. }
  42. void VMManager::Reset() {
  43. vma_map.clear();
  44. // Initialize the map with a single free region covering the entire managed space.
  45. VirtualMemoryArea initial_vma;
  46. initial_vma.size = MAX_ADDRESS;
  47. vma_map.emplace(initial_vma.base, initial_vma);
  48. UpdatePageTableForVMA(initial_vma);
  49. }
  50. VMManager::VMAHandle VMManager::FindVMA(VAddr target) const {
  51. if (target >= MAX_ADDRESS) {
  52. return vma_map.end();
  53. } else {
  54. return std::prev(vma_map.upper_bound(target));
  55. }
  56. }
  57. ResultVal<VMManager::VMAHandle> VMManager::MapMemoryBlock(VAddr target,
  58. std::shared_ptr<std::vector<u8>> block, size_t offset, u32 size, MemoryState state) {
  59. ASSERT(block != nullptr);
  60. ASSERT(offset + size <= block->size());
  61. // This is the appropriately sized VMA that will turn into our allocation.
  62. CASCADE_RESULT(VMAIter vma_handle, CarveVMA(target, size));
  63. VirtualMemoryArea& final_vma = vma_handle->second;
  64. ASSERT(final_vma.size == size);
  65. final_vma.type = VMAType::AllocatedMemoryBlock;
  66. final_vma.permissions = VMAPermission::ReadWrite;
  67. final_vma.meminfo_state = state;
  68. final_vma.backing_block = block;
  69. final_vma.offset = offset;
  70. UpdatePageTableForVMA(final_vma);
  71. return MakeResult<VMAHandle>(MergeAdjacent(vma_handle));
  72. }
  73. ResultVal<VMManager::VMAHandle> VMManager::MapBackingMemory(VAddr target, u8 * memory, u32 size, MemoryState state) {
  74. ASSERT(memory != nullptr);
  75. // This is the appropriately sized VMA that will turn into our allocation.
  76. CASCADE_RESULT(VMAIter vma_handle, CarveVMA(target, size));
  77. VirtualMemoryArea& final_vma = vma_handle->second;
  78. ASSERT(final_vma.size == size);
  79. final_vma.type = VMAType::BackingMemory;
  80. final_vma.permissions = VMAPermission::ReadWrite;
  81. final_vma.meminfo_state = state;
  82. final_vma.backing_memory = memory;
  83. UpdatePageTableForVMA(final_vma);
  84. return MakeResult<VMAHandle>(MergeAdjacent(vma_handle));
  85. }
  86. ResultVal<VMManager::VMAHandle> VMManager::MapMMIO(VAddr target, PAddr paddr, u32 size, MemoryState state, Memory::MMIORegionPointer mmio_handler) {
  87. // This is the appropriately sized VMA that will turn into our allocation.
  88. CASCADE_RESULT(VMAIter vma_handle, CarveVMA(target, size));
  89. VirtualMemoryArea& final_vma = vma_handle->second;
  90. ASSERT(final_vma.size == size);
  91. final_vma.type = VMAType::MMIO;
  92. final_vma.permissions = VMAPermission::ReadWrite;
  93. final_vma.meminfo_state = state;
  94. final_vma.paddr = paddr;
  95. final_vma.mmio_handler = mmio_handler;
  96. UpdatePageTableForVMA(final_vma);
  97. return MakeResult<VMAHandle>(MergeAdjacent(vma_handle));
  98. }
  99. VMManager::VMAIter VMManager::Unmap(VMAIter vma_handle) {
  100. VirtualMemoryArea& vma = vma_handle->second;
  101. vma.type = VMAType::Free;
  102. vma.permissions = VMAPermission::None;
  103. vma.meminfo_state = MemoryState::Free;
  104. vma.backing_block = nullptr;
  105. vma.offset = 0;
  106. vma.backing_memory = nullptr;
  107. vma.paddr = 0;
  108. UpdatePageTableForVMA(vma);
  109. return MergeAdjacent(vma_handle);
  110. }
  111. ResultCode VMManager::UnmapRange(VAddr target, u32 size) {
  112. CASCADE_RESULT(VMAIter vma, CarveVMARange(target, size));
  113. VAddr target_end = target + size;
  114. VMAIter end = vma_map.end();
  115. // The comparison against the end of the range must be done using addresses since VMAs can be
  116. // merged during this process, causing invalidation of the iterators.
  117. while (vma != end && vma->second.base < target_end) {
  118. vma = std::next(Unmap(vma));
  119. }
  120. ASSERT(FindVMA(target)->second.size >= size);
  121. return RESULT_SUCCESS;
  122. }
  123. VMManager::VMAHandle VMManager::Reprotect(VMAHandle vma_handle, VMAPermission new_perms) {
  124. VMAIter iter = StripIterConstness(vma_handle);
  125. VirtualMemoryArea& vma = iter->second;
  126. vma.permissions = new_perms;
  127. UpdatePageTableForVMA(vma);
  128. return MergeAdjacent(iter);
  129. }
  130. ResultCode VMManager::ReprotectRange(VAddr target, u32 size, VMAPermission new_perms) {
  131. CASCADE_RESULT(VMAIter vma, CarveVMARange(target, size));
  132. VAddr target_end = target + size;
  133. VMAIter end = vma_map.end();
  134. // The comparison against the end of the range must be done using addresses since VMAs can be
  135. // merged during this process, causing invalidation of the iterators.
  136. while (vma != end && vma->second.base < target_end) {
  137. vma = std::next(StripIterConstness(Reprotect(vma, new_perms)));
  138. }
  139. return RESULT_SUCCESS;
  140. }
  141. void VMManager::RefreshMemoryBlockMappings(const std::vector<u8>* block) {
  142. // If this ever proves to have a noticeable performance impact, allow users of the function to
  143. // specify a specific range of addresses to limit the scan to.
  144. for (const auto& p : vma_map) {
  145. const VirtualMemoryArea& vma = p.second;
  146. if (block == vma.backing_block.get()) {
  147. UpdatePageTableForVMA(vma);
  148. }
  149. }
  150. }
  151. void VMManager::LogLayout(Log::Level log_level) const {
  152. for (const auto& p : vma_map) {
  153. const VirtualMemoryArea& vma = p.second;
  154. LOG_GENERIC(Log::Class::Kernel, log_level, "%08X - %08X size: %8X %c%c%c %s",
  155. vma.base, vma.base + vma.size, vma.size,
  156. (u8)vma.permissions & (u8)VMAPermission::Read ? 'R' : '-',
  157. (u8)vma.permissions & (u8)VMAPermission::Write ? 'W' : '-',
  158. (u8)vma.permissions & (u8)VMAPermission::Execute ? 'X' : '-', GetMemoryStateName(vma.meminfo_state));
  159. }
  160. }
  161. VMManager::VMAIter VMManager::StripIterConstness(const VMAHandle & iter) {
  162. // This uses a neat C++ trick to convert a const_iterator to a regular iterator, given
  163. // non-const access to its container.
  164. return vma_map.erase(iter, iter); // Erases an empty range of elements
  165. }
  166. ResultVal<VMManager::VMAIter> VMManager::CarveVMA(VAddr base, u32 size) {
  167. ASSERT_MSG((size & Memory::PAGE_MASK) == 0, "non-page aligned size: 0x%8X", size);
  168. ASSERT_MSG((base & Memory::PAGE_MASK) == 0, "non-page aligned base: 0x%08X", base);
  169. VMAIter vma_handle = StripIterConstness(FindVMA(base));
  170. if (vma_handle == vma_map.end()) {
  171. // Target address is outside the range managed by the kernel
  172. return ERR_INVALID_ADDRESS;
  173. }
  174. VirtualMemoryArea& vma = vma_handle->second;
  175. if (vma.type != VMAType::Free) {
  176. // Region is already allocated
  177. return ERR_INVALID_ADDRESS_STATE;
  178. }
  179. u32 start_in_vma = base - vma.base;
  180. u32 end_in_vma = start_in_vma + size;
  181. if (end_in_vma > vma.size) {
  182. // Requested allocation doesn't fit inside VMA
  183. return ERR_INVALID_ADDRESS_STATE;
  184. }
  185. if (end_in_vma != vma.size) {
  186. // Split VMA at the end of the allocated region
  187. SplitVMA(vma_handle, end_in_vma);
  188. }
  189. if (start_in_vma != 0) {
  190. // Split VMA at the start of the allocated region
  191. vma_handle = SplitVMA(vma_handle, start_in_vma);
  192. }
  193. return MakeResult<VMAIter>(vma_handle);
  194. }
  195. ResultVal<VMManager::VMAIter> VMManager::CarveVMARange(VAddr target, u32 size) {
  196. ASSERT_MSG((size & Memory::PAGE_MASK) == 0, "non-page aligned size: 0x%8X", size);
  197. ASSERT_MSG((target & Memory::PAGE_MASK) == 0, "non-page aligned base: 0x%08X", target);
  198. VAddr target_end = target + size;
  199. ASSERT(target_end >= target);
  200. ASSERT(target_end <= MAX_ADDRESS);
  201. ASSERT(size > 0);
  202. VMAIter begin_vma = StripIterConstness(FindVMA(target));
  203. VMAIter i_end = vma_map.lower_bound(target_end);
  204. for (auto i = begin_vma; i != i_end; ++i) {
  205. if (i->second.type == VMAType::Free) {
  206. return ERR_INVALID_ADDRESS_STATE;
  207. }
  208. }
  209. if (target != begin_vma->second.base) {
  210. begin_vma = SplitVMA(begin_vma, target - begin_vma->second.base);
  211. }
  212. VMAIter end_vma = StripIterConstness(FindVMA(target_end));
  213. if (end_vma != vma_map.end() && target_end != end_vma->second.base) {
  214. end_vma = SplitVMA(end_vma, target_end - end_vma->second.base);
  215. }
  216. return MakeResult<VMAIter>(begin_vma);
  217. }
  218. VMManager::VMAIter VMManager::SplitVMA(VMAIter vma_handle, u32 offset_in_vma) {
  219. VirtualMemoryArea& old_vma = vma_handle->second;
  220. VirtualMemoryArea new_vma = old_vma; // Make a copy of the VMA
  221. // For now, don't allow no-op VMA splits (trying to split at a boundary) because it's probably
  222. // a bug. This restriction might be removed later.
  223. ASSERT(offset_in_vma < old_vma.size);
  224. ASSERT(offset_in_vma > 0);
  225. old_vma.size = offset_in_vma;
  226. new_vma.base += offset_in_vma;
  227. new_vma.size -= offset_in_vma;
  228. switch (new_vma.type) {
  229. case VMAType::Free:
  230. break;
  231. case VMAType::AllocatedMemoryBlock:
  232. new_vma.offset += offset_in_vma;
  233. break;
  234. case VMAType::BackingMemory:
  235. new_vma.backing_memory += offset_in_vma;
  236. break;
  237. case VMAType::MMIO:
  238. new_vma.paddr += offset_in_vma;
  239. break;
  240. }
  241. ASSERT(old_vma.CanBeMergedWith(new_vma));
  242. return vma_map.emplace_hint(std::next(vma_handle), new_vma.base, new_vma);
  243. }
  244. VMManager::VMAIter VMManager::MergeAdjacent(VMAIter iter) {
  245. VMAIter next_vma = std::next(iter);
  246. if (next_vma != vma_map.end() && iter->second.CanBeMergedWith(next_vma->second)) {
  247. iter->second.size += next_vma->second.size;
  248. vma_map.erase(next_vma);
  249. }
  250. if (iter != vma_map.begin()) {
  251. VMAIter prev_vma = std::prev(iter);
  252. if (prev_vma->second.CanBeMergedWith(iter->second)) {
  253. prev_vma->second.size += iter->second.size;
  254. vma_map.erase(iter);
  255. iter = prev_vma;
  256. }
  257. }
  258. return iter;
  259. }
  260. void VMManager::UpdatePageTableForVMA(const VirtualMemoryArea& vma) {
  261. switch (vma.type) {
  262. case VMAType::Free:
  263. Memory::UnmapRegion(vma.base, vma.size);
  264. break;
  265. case VMAType::AllocatedMemoryBlock:
  266. Memory::MapMemoryRegion(vma.base, vma.size, vma.backing_block->data() + vma.offset);
  267. break;
  268. case VMAType::BackingMemory:
  269. Memory::MapMemoryRegion(vma.base, vma.size, vma.backing_memory);
  270. break;
  271. case VMAType::MMIO:
  272. Memory::MapIoRegion(vma.base, vma.size, vma.mmio_handler);
  273. break;
  274. }
  275. }
  276. }