memory.cpp 20 KB

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