memory.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <optional>
  7. #include <utility>
  8. #include "common/assert.h"
  9. #include "common/common_types.h"
  10. #include "common/logging/log.h"
  11. #include "common/page_table.h"
  12. #include "common/swap.h"
  13. #include "core/arm/arm_interface.h"
  14. #include "core/core.h"
  15. #include "core/hle/kernel/process.h"
  16. #include "core/hle/kernel/vm_manager.h"
  17. #include "core/hle/lock.h"
  18. #include "core/memory.h"
  19. #include "core/memory_setup.h"
  20. #include "video_core/gpu.h"
  21. #include "video_core/renderer_base.h"
  22. namespace Memory {
  23. static Common::PageTable* current_page_table = nullptr;
  24. void SetCurrentPageTable(Common::PageTable* page_table) {
  25. current_page_table = page_table;
  26. auto& system = Core::System::GetInstance();
  27. if (system.IsPoweredOn()) {
  28. system.ArmInterface(0).PageTableChanged();
  29. system.ArmInterface(1).PageTableChanged();
  30. system.ArmInterface(2).PageTableChanged();
  31. system.ArmInterface(3).PageTableChanged();
  32. }
  33. }
  34. Common::PageTable* GetCurrentPageTable() {
  35. return current_page_table;
  36. }
  37. static void MapPages(Common::PageTable& page_table, VAddr base, u64 size, u8* memory,
  38. Common::PageType type) {
  39. LOG_DEBUG(HW_Memory, "Mapping {} onto {:016X}-{:016X}", fmt::ptr(memory), base * PAGE_SIZE,
  40. (base + size) * PAGE_SIZE);
  41. // During boot, current_page_table might not be set yet, in which case we need not flush
  42. if (Core::System::GetInstance().IsPoweredOn()) {
  43. Core::System::GetInstance().GPU().FlushAndInvalidateRegion(base << PAGE_BITS,
  44. size * PAGE_SIZE);
  45. }
  46. VAddr end = base + size;
  47. ASSERT_MSG(end <= page_table.pointers.size(), "out of range mapping at {:016X}",
  48. base + page_table.pointers.size());
  49. std::fill(page_table.attributes.begin() + base, page_table.attributes.begin() + end, type);
  50. if (memory == nullptr) {
  51. std::fill(page_table.pointers.begin() + base, page_table.pointers.begin() + end, memory);
  52. } else {
  53. while (base != end) {
  54. page_table.pointers[base] = memory;
  55. base += 1;
  56. memory += PAGE_SIZE;
  57. }
  58. }
  59. }
  60. void MapMemoryRegion(Common::PageTable& page_table, VAddr base, u64 size, u8* target) {
  61. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: {:016X}", size);
  62. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: {:016X}", base);
  63. MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, target, Common::PageType::Memory);
  64. }
  65. void MapIoRegion(Common::PageTable& page_table, VAddr base, u64 size,
  66. Common::MemoryHookPointer mmio_handler) {
  67. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: {:016X}", size);
  68. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: {:016X}", base);
  69. MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, nullptr, Common::PageType::Special);
  70. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  71. Common::SpecialRegion region{Common::SpecialRegion::Type::IODevice, std::move(mmio_handler)};
  72. page_table.special_regions.add(
  73. std::make_pair(interval, std::set<Common::SpecialRegion>{region}));
  74. }
  75. void UnmapRegion(Common::PageTable& page_table, VAddr base, u64 size) {
  76. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: {:016X}", size);
  77. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: {:016X}", base);
  78. MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, nullptr, Common::PageType::Unmapped);
  79. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  80. page_table.special_regions.erase(interval);
  81. }
  82. void AddDebugHook(Common::PageTable& page_table, VAddr base, u64 size,
  83. Common::MemoryHookPointer hook) {
  84. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  85. Common::SpecialRegion region{Common::SpecialRegion::Type::DebugHook, std::move(hook)};
  86. page_table.special_regions.add(
  87. std::make_pair(interval, std::set<Common::SpecialRegion>{region}));
  88. }
  89. void RemoveDebugHook(Common::PageTable& page_table, VAddr base, u64 size,
  90. Common::MemoryHookPointer hook) {
  91. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  92. Common::SpecialRegion region{Common::SpecialRegion::Type::DebugHook, std::move(hook)};
  93. page_table.special_regions.subtract(
  94. std::make_pair(interval, std::set<Common::SpecialRegion>{region}));
  95. }
  96. /**
  97. * Gets a pointer to the exact memory at the virtual address (i.e. not page aligned)
  98. * using a VMA from the current process
  99. */
  100. static u8* GetPointerFromVMA(const Kernel::Process& process, VAddr vaddr) {
  101. const auto& vm_manager = process.VMManager();
  102. const auto it = vm_manager.FindVMA(vaddr);
  103. DEBUG_ASSERT(vm_manager.IsValidHandle(it));
  104. u8* direct_pointer = nullptr;
  105. const auto& vma = it->second;
  106. switch (vma.type) {
  107. case Kernel::VMAType::AllocatedMemoryBlock:
  108. direct_pointer = vma.backing_block->data() + vma.offset;
  109. break;
  110. case Kernel::VMAType::BackingMemory:
  111. direct_pointer = vma.backing_memory;
  112. break;
  113. case Kernel::VMAType::Free:
  114. return nullptr;
  115. default:
  116. UNREACHABLE();
  117. }
  118. return direct_pointer + (vaddr - vma.base);
  119. }
  120. /**
  121. * Gets a pointer to the exact memory at the virtual address (i.e. not page aligned)
  122. * using a VMA from the current process.
  123. */
  124. static u8* GetPointerFromVMA(VAddr vaddr) {
  125. return GetPointerFromVMA(*Core::CurrentProcess(), vaddr);
  126. }
  127. template <typename T>
  128. T Read(const VAddr vaddr) {
  129. const u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  130. if (page_pointer) {
  131. // NOTE: Avoid adding any extra logic to this fast-path block
  132. T value;
  133. std::memcpy(&value, &page_pointer[vaddr & PAGE_MASK], sizeof(T));
  134. return value;
  135. }
  136. Common::PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  137. switch (type) {
  138. case Common::PageType::Unmapped:
  139. LOG_ERROR(HW_Memory, "Unmapped Read{} @ 0x{:08X}", sizeof(T) * 8, vaddr);
  140. return 0;
  141. case Common::PageType::Memory:
  142. ASSERT_MSG(false, "Mapped memory page without a pointer @ {:016X}", vaddr);
  143. break;
  144. case Common::PageType::RasterizerCachedMemory: {
  145. auto host_ptr{GetPointerFromVMA(vaddr)};
  146. Core::System::GetInstance().GPU().FlushRegion(ToCacheAddr(host_ptr), sizeof(T));
  147. T value;
  148. std::memcpy(&value, host_ptr, sizeof(T));
  149. return value;
  150. }
  151. default:
  152. UNREACHABLE();
  153. }
  154. return {};
  155. }
  156. template <typename T>
  157. void Write(const VAddr vaddr, const T data) {
  158. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  159. if (page_pointer) {
  160. // NOTE: Avoid adding any extra logic to this fast-path block
  161. std::memcpy(&page_pointer[vaddr & PAGE_MASK], &data, sizeof(T));
  162. return;
  163. }
  164. Common::PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  165. switch (type) {
  166. case Common::PageType::Unmapped:
  167. LOG_ERROR(HW_Memory, "Unmapped Write{} 0x{:08X} @ 0x{:016X}", sizeof(data) * 8,
  168. static_cast<u32>(data), vaddr);
  169. return;
  170. case Common::PageType::Memory:
  171. ASSERT_MSG(false, "Mapped memory page without a pointer @ {:016X}", vaddr);
  172. break;
  173. case Common::PageType::RasterizerCachedMemory: {
  174. auto host_ptr{GetPointerFromVMA(vaddr)};
  175. Core::System::GetInstance().GPU().InvalidateRegion(ToCacheAddr(host_ptr), sizeof(T));
  176. std::memcpy(host_ptr, &data, sizeof(T));
  177. break;
  178. }
  179. default:
  180. UNREACHABLE();
  181. }
  182. }
  183. bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) {
  184. const auto& page_table = process.VMManager().page_table;
  185. const u8* page_pointer = page_table.pointers[vaddr >> PAGE_BITS];
  186. if (page_pointer)
  187. return true;
  188. if (page_table.attributes[vaddr >> PAGE_BITS] == Common::PageType::RasterizerCachedMemory)
  189. return true;
  190. if (page_table.attributes[vaddr >> PAGE_BITS] != Common::PageType::Special)
  191. return false;
  192. return false;
  193. }
  194. bool IsValidVirtualAddress(const VAddr vaddr) {
  195. return IsValidVirtualAddress(*Core::CurrentProcess(), vaddr);
  196. }
  197. bool IsKernelVirtualAddress(const VAddr vaddr) {
  198. return KERNEL_REGION_VADDR <= vaddr && vaddr < KERNEL_REGION_END;
  199. }
  200. u8* GetPointer(const VAddr vaddr) {
  201. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  202. if (page_pointer) {
  203. return page_pointer + (vaddr & PAGE_MASK);
  204. }
  205. if (current_page_table->attributes[vaddr >> PAGE_BITS] ==
  206. Common::PageType::RasterizerCachedMemory) {
  207. return GetPointerFromVMA(vaddr);
  208. }
  209. LOG_ERROR(HW_Memory, "Unknown GetPointer @ 0x{:016X}", vaddr);
  210. return nullptr;
  211. }
  212. std::string ReadCString(VAddr vaddr, std::size_t max_length) {
  213. std::string string;
  214. string.reserve(max_length);
  215. for (std::size_t i = 0; i < max_length; ++i) {
  216. char c = Read8(vaddr);
  217. if (c == '\0')
  218. break;
  219. string.push_back(c);
  220. ++vaddr;
  221. }
  222. string.shrink_to_fit();
  223. return string;
  224. }
  225. void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached) {
  226. if (vaddr == 0) {
  227. return;
  228. }
  229. // Iterate over a contiguous CPU address space, which corresponds to the specified GPU address
  230. // space, marking the region as un/cached. The region is marked un/cached at a granularity of
  231. // CPU pages, hence why we iterate on a CPU page basis (note: GPU page size is different). This
  232. // assumes the specified GPU address region is contiguous as well.
  233. u64 num_pages = ((vaddr + size - 1) >> PAGE_BITS) - (vaddr >> PAGE_BITS) + 1;
  234. for (unsigned i = 0; i < num_pages; ++i, vaddr += PAGE_SIZE) {
  235. Common::PageType& page_type = current_page_table->attributes[vaddr >> PAGE_BITS];
  236. if (cached) {
  237. // Switch page type to cached if now cached
  238. switch (page_type) {
  239. case Common::PageType::Unmapped:
  240. // It is not necessary for a process to have this region mapped into its address
  241. // space, for example, a system module need not have a VRAM mapping.
  242. break;
  243. case Common::PageType::Memory:
  244. page_type = Common::PageType::RasterizerCachedMemory;
  245. current_page_table->pointers[vaddr >> PAGE_BITS] = nullptr;
  246. break;
  247. case Common::PageType::RasterizerCachedMemory:
  248. // There can be more than one GPU region mapped per CPU region, so it's common that
  249. // this area is already marked as cached.
  250. break;
  251. default:
  252. UNREACHABLE();
  253. }
  254. } else {
  255. // Switch page type to uncached if now uncached
  256. switch (page_type) {
  257. case Common::PageType::Unmapped:
  258. // It is not necessary for a process to have this region mapped into its address
  259. // space, for example, a system module need not have a VRAM mapping.
  260. break;
  261. case Common::PageType::Memory:
  262. // There can be more than one GPU region mapped per CPU region, so it's common that
  263. // this area is already unmarked as cached.
  264. break;
  265. case Common::PageType::RasterizerCachedMemory: {
  266. u8* pointer = GetPointerFromVMA(vaddr & ~PAGE_MASK);
  267. if (pointer == nullptr) {
  268. // It's possible that this function has been called while updating the pagetable
  269. // after unmapping a VMA. In that case the underlying VMA will no longer exist,
  270. // and we should just leave the pagetable entry blank.
  271. page_type = Common::PageType::Unmapped;
  272. } else {
  273. page_type = Common::PageType::Memory;
  274. current_page_table->pointers[vaddr >> PAGE_BITS] = pointer;
  275. }
  276. break;
  277. }
  278. default:
  279. UNREACHABLE();
  280. }
  281. }
  282. }
  283. }
  284. u8 Read8(const VAddr addr) {
  285. return Read<u8>(addr);
  286. }
  287. u16 Read16(const VAddr addr) {
  288. return Read<u16_le>(addr);
  289. }
  290. u32 Read32(const VAddr addr) {
  291. return Read<u32_le>(addr);
  292. }
  293. u64 Read64(const VAddr addr) {
  294. return Read<u64_le>(addr);
  295. }
  296. void ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer,
  297. const std::size_t size) {
  298. const auto& page_table = process.VMManager().page_table;
  299. std::size_t remaining_size = size;
  300. std::size_t page_index = src_addr >> PAGE_BITS;
  301. std::size_t page_offset = src_addr & PAGE_MASK;
  302. while (remaining_size > 0) {
  303. const std::size_t copy_amount =
  304. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  305. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  306. switch (page_table.attributes[page_index]) {
  307. case Common::PageType::Unmapped: {
  308. LOG_ERROR(HW_Memory,
  309. "Unmapped ReadBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  310. current_vaddr, src_addr, size);
  311. std::memset(dest_buffer, 0, copy_amount);
  312. break;
  313. }
  314. case Common::PageType::Memory: {
  315. DEBUG_ASSERT(page_table.pointers[page_index]);
  316. const u8* src_ptr = page_table.pointers[page_index] + page_offset;
  317. std::memcpy(dest_buffer, src_ptr, copy_amount);
  318. break;
  319. }
  320. case Common::PageType::RasterizerCachedMemory: {
  321. const auto& host_ptr{GetPointerFromVMA(process, current_vaddr)};
  322. Core::System::GetInstance().GPU().FlushRegion(ToCacheAddr(host_ptr), copy_amount);
  323. std::memcpy(dest_buffer, host_ptr, copy_amount);
  324. break;
  325. }
  326. default:
  327. UNREACHABLE();
  328. }
  329. page_index++;
  330. page_offset = 0;
  331. dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
  332. remaining_size -= copy_amount;
  333. }
  334. }
  335. void ReadBlock(const VAddr src_addr, void* dest_buffer, const std::size_t size) {
  336. ReadBlock(*Core::CurrentProcess(), src_addr, dest_buffer, size);
  337. }
  338. void Write8(const VAddr addr, const u8 data) {
  339. Write<u8>(addr, data);
  340. }
  341. void Write16(const VAddr addr, const u16 data) {
  342. Write<u16_le>(addr, data);
  343. }
  344. void Write32(const VAddr addr, const u32 data) {
  345. Write<u32_le>(addr, data);
  346. }
  347. void Write64(const VAddr addr, const u64 data) {
  348. Write<u64_le>(addr, data);
  349. }
  350. void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const void* src_buffer,
  351. const std::size_t size) {
  352. const auto& page_table = process.VMManager().page_table;
  353. std::size_t remaining_size = size;
  354. std::size_t page_index = dest_addr >> PAGE_BITS;
  355. std::size_t page_offset = dest_addr & PAGE_MASK;
  356. while (remaining_size > 0) {
  357. const std::size_t copy_amount =
  358. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  359. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  360. switch (page_table.attributes[page_index]) {
  361. case Common::PageType::Unmapped: {
  362. LOG_ERROR(HW_Memory,
  363. "Unmapped WriteBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  364. current_vaddr, dest_addr, size);
  365. break;
  366. }
  367. case Common::PageType::Memory: {
  368. DEBUG_ASSERT(page_table.pointers[page_index]);
  369. u8* dest_ptr = page_table.pointers[page_index] + page_offset;
  370. std::memcpy(dest_ptr, src_buffer, copy_amount);
  371. break;
  372. }
  373. case Common::PageType::RasterizerCachedMemory: {
  374. const auto& host_ptr{GetPointerFromVMA(process, current_vaddr)};
  375. Core::System::GetInstance().GPU().InvalidateRegion(ToCacheAddr(host_ptr), copy_amount);
  376. std::memcpy(host_ptr, src_buffer, copy_amount);
  377. break;
  378. }
  379. default:
  380. UNREACHABLE();
  381. }
  382. page_index++;
  383. page_offset = 0;
  384. src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
  385. remaining_size -= copy_amount;
  386. }
  387. }
  388. void WriteBlock(const VAddr dest_addr, const void* src_buffer, const std::size_t size) {
  389. WriteBlock(*Core::CurrentProcess(), dest_addr, src_buffer, size);
  390. }
  391. void ZeroBlock(const Kernel::Process& process, const VAddr dest_addr, const std::size_t size) {
  392. const auto& page_table = process.VMManager().page_table;
  393. std::size_t remaining_size = size;
  394. std::size_t page_index = dest_addr >> PAGE_BITS;
  395. std::size_t page_offset = dest_addr & PAGE_MASK;
  396. while (remaining_size > 0) {
  397. const std::size_t copy_amount =
  398. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  399. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  400. switch (page_table.attributes[page_index]) {
  401. case Common::PageType::Unmapped: {
  402. LOG_ERROR(HW_Memory,
  403. "Unmapped ZeroBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  404. current_vaddr, dest_addr, size);
  405. break;
  406. }
  407. case Common::PageType::Memory: {
  408. DEBUG_ASSERT(page_table.pointers[page_index]);
  409. u8* dest_ptr = page_table.pointers[page_index] + page_offset;
  410. std::memset(dest_ptr, 0, copy_amount);
  411. break;
  412. }
  413. case Common::PageType::RasterizerCachedMemory: {
  414. const auto& host_ptr{GetPointerFromVMA(process, current_vaddr)};
  415. Core::System::GetInstance().GPU().InvalidateRegion(ToCacheAddr(host_ptr), copy_amount);
  416. std::memset(host_ptr, 0, copy_amount);
  417. break;
  418. }
  419. default:
  420. UNREACHABLE();
  421. }
  422. page_index++;
  423. page_offset = 0;
  424. remaining_size -= copy_amount;
  425. }
  426. }
  427. void CopyBlock(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr,
  428. const std::size_t size) {
  429. const auto& page_table = process.VMManager().page_table;
  430. std::size_t remaining_size = size;
  431. std::size_t page_index = src_addr >> PAGE_BITS;
  432. std::size_t page_offset = src_addr & PAGE_MASK;
  433. while (remaining_size > 0) {
  434. const std::size_t copy_amount =
  435. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  436. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  437. switch (page_table.attributes[page_index]) {
  438. case Common::PageType::Unmapped: {
  439. LOG_ERROR(HW_Memory,
  440. "Unmapped CopyBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  441. current_vaddr, src_addr, size);
  442. ZeroBlock(process, dest_addr, copy_amount);
  443. break;
  444. }
  445. case Common::PageType::Memory: {
  446. DEBUG_ASSERT(page_table.pointers[page_index]);
  447. const u8* src_ptr = page_table.pointers[page_index] + page_offset;
  448. WriteBlock(process, dest_addr, src_ptr, copy_amount);
  449. break;
  450. }
  451. case Common::PageType::RasterizerCachedMemory: {
  452. const auto& host_ptr{GetPointerFromVMA(process, current_vaddr)};
  453. Core::System::GetInstance().GPU().FlushRegion(ToCacheAddr(host_ptr), copy_amount);
  454. WriteBlock(process, dest_addr, host_ptr, copy_amount);
  455. break;
  456. }
  457. default:
  458. UNREACHABLE();
  459. }
  460. page_index++;
  461. page_offset = 0;
  462. dest_addr += static_cast<VAddr>(copy_amount);
  463. src_addr += static_cast<VAddr>(copy_amount);
  464. remaining_size -= copy_amount;
  465. }
  466. }
  467. void CopyBlock(VAddr dest_addr, VAddr src_addr, std::size_t size) {
  468. CopyBlock(*Core::CurrentProcess(), dest_addr, src_addr, size);
  469. }
  470. } // namespace Memory