vm_manager.cpp 13 KB

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