memory.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <cstring>
  6. #include "common/assert.h"
  7. #include "common/common_types.h"
  8. #include "common/logging/log.h"
  9. #include "common/swap.h"
  10. #include "core/hle/kernel/process.h"
  11. #include "core/memory.h"
  12. #include "core/memory_setup.h"
  13. #include "core/mmio.h"
  14. #include "video_core/renderer_base.h"
  15. #include "video_core/video_core.h"
  16. namespace Memory {
  17. enum class PageType {
  18. /// Page is unmapped and should cause an access error.
  19. Unmapped,
  20. /// Page is mapped to regular memory. This is the only type you can get pointers to.
  21. Memory,
  22. /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and
  23. /// invalidation
  24. RasterizerCachedMemory,
  25. /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions.
  26. Special,
  27. /// Page is mapped to a I/O region, but also needs to check for rasterizer cache flushing and
  28. /// invalidation
  29. RasterizerCachedSpecial,
  30. };
  31. struct SpecialRegion {
  32. VAddr base;
  33. u64 size;
  34. MMIORegionPointer handler;
  35. };
  36. /**
  37. * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely
  38. * mimics the way a real CPU page table works, but instead is optimized for minimal decoding and
  39. * fetching requirements when accessing. In the usual case of an access to regular memory, it only
  40. * requires an indexed fetch and a check for NULL.
  41. */
  42. struct PageTable {
  43. /**
  44. * Array of memory pointers backing each page. An entry can only be non-null if the
  45. * corresponding entry in the `attributes` array is of type `Memory`.
  46. */
  47. std::map<u64, u8*> pointers;
  48. /**
  49. * Contains MMIO handlers that back memory regions whose entries in the `attribute` array is of
  50. * type `Special`.
  51. */
  52. std::vector<SpecialRegion> special_regions;
  53. /**
  54. * Array of fine grained page attributes. If it is set to any value other than `Memory`, then
  55. * the corresponding entry in `pointers` MUST be set to null.
  56. */
  57. std::map<u64, PageType> attributes;
  58. /**
  59. * Indicates the number of externally cached resources touching a page that should be
  60. * flushed before the memory is accessed
  61. */
  62. std::map<u64, u8> cached_res_count;
  63. };
  64. /// Singular page table used for the singleton process
  65. static PageTable main_page_table;
  66. /// Currently active page table
  67. static PageTable* current_page_table = &main_page_table;
  68. //std::array<u8*, PAGE_TABLE_NUM_ENTRIES>* GetCurrentPageTablePointers() {
  69. // return &current_page_table->pointers;
  70. //}
  71. static void MapPages(u64 base, u64 size, u8* memory, PageType type) {
  72. LOG_DEBUG(HW_Memory, "Mapping %p onto %08X-%08X", memory, base * PAGE_SIZE,
  73. (base + size) * PAGE_SIZE);
  74. RasterizerFlushVirtualRegion(base << PAGE_BITS, size * PAGE_SIZE,
  75. FlushMode::FlushAndInvalidate);
  76. u64 end = base + size;
  77. while (base != end) {
  78. ASSERT_MSG(base < PAGE_TABLE_NUM_ENTRIES, "out of range mapping at %08X", base);
  79. current_page_table->attributes[base] = type;
  80. current_page_table->pointers[base] = memory;
  81. current_page_table->cached_res_count[base] = 0;
  82. base += 1;
  83. if (memory != nullptr)
  84. memory += PAGE_SIZE;
  85. }
  86. }
  87. void InitMemoryMap() {
  88. //main_page_table.pointers.fill(nullptr);
  89. //main_page_table.attributes.fill(PageType::Unmapped);
  90. //main_page_table.cached_res_count.fill(0);
  91. }
  92. void MapMemoryRegion(VAddr base, u64 size, u8* target) {
  93. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  94. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  95. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, target, PageType::Memory);
  96. }
  97. void MapIoRegion(VAddr base, u64 size, MMIORegionPointer mmio_handler) {
  98. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  99. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  100. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Special);
  101. current_page_table->special_regions.emplace_back(SpecialRegion{base, size, mmio_handler});
  102. }
  103. void UnmapRegion(VAddr base, u64 size) {
  104. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  105. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  106. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Unmapped);
  107. }
  108. /**
  109. * Gets a pointer to the exact memory at the virtual address (i.e. not page aligned)
  110. * using a VMA from the current process
  111. */
  112. static u8* GetPointerFromVMA(VAddr vaddr) {
  113. u8* direct_pointer = nullptr;
  114. auto& vm_manager = Kernel::g_current_process->vm_manager;
  115. auto it = vm_manager.FindVMA(vaddr);
  116. ASSERT(it != vm_manager.vma_map.end());
  117. auto& vma = it->second;
  118. switch (vma.type) {
  119. case Kernel::VMAType::AllocatedMemoryBlock:
  120. direct_pointer = vma.backing_block->data() + vma.offset;
  121. break;
  122. case Kernel::VMAType::BackingMemory:
  123. direct_pointer = vma.backing_memory;
  124. break;
  125. case Kernel::VMAType::Free:
  126. return nullptr;
  127. default:
  128. UNREACHABLE();
  129. }
  130. return direct_pointer + (vaddr - vma.base);
  131. }
  132. /**
  133. * This function should only be called for virtual addreses with attribute `PageType::Special`.
  134. */
  135. static MMIORegionPointer GetMMIOHandler(VAddr vaddr) {
  136. for (const auto& region : current_page_table->special_regions) {
  137. if (vaddr >= region.base && vaddr < (region.base + region.size)) {
  138. return region.handler;
  139. }
  140. }
  141. ASSERT_MSG(false, "Mapped IO page without a handler @ %08X", vaddr);
  142. return nullptr; // Should never happen
  143. }
  144. template <typename T>
  145. T ReadMMIO(MMIORegionPointer mmio_handler, VAddr addr);
  146. template <typename T>
  147. T Read(const VAddr vaddr) {
  148. const u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  149. if (page_pointer) {
  150. // NOTE: Avoid adding any extra logic to this fast-path block
  151. T value;
  152. std::memcpy(&value, &page_pointer[vaddr & PAGE_MASK], sizeof(T));
  153. return value;
  154. }
  155. PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  156. switch (type) {
  157. case PageType::Unmapped:
  158. LOG_ERROR(HW_Memory, "unmapped Read%lu @ 0x%llx", sizeof(T) * 8, vaddr);
  159. return 0;
  160. case PageType::Memory:
  161. ASSERT_MSG(false, "Mapped memory page without a pointer @ %08X", vaddr);
  162. break;
  163. case PageType::RasterizerCachedMemory: {
  164. RasterizerFlushVirtualRegion(vaddr, sizeof(T), FlushMode::Flush);
  165. T value;
  166. std::memcpy(&value, GetPointerFromVMA(vaddr), sizeof(T));
  167. return value;
  168. }
  169. case PageType::Special:
  170. return ReadMMIO<T>(GetMMIOHandler(vaddr), vaddr);
  171. case PageType::RasterizerCachedSpecial: {
  172. RasterizerFlushVirtualRegion(vaddr, sizeof(T), FlushMode::Flush);
  173. return ReadMMIO<T>(GetMMIOHandler(vaddr), vaddr);
  174. }
  175. default:
  176. UNREACHABLE();
  177. }
  178. }
  179. template <typename T>
  180. void WriteMMIO(MMIORegionPointer mmio_handler, VAddr addr, const T data);
  181. template <typename T>
  182. void Write(const VAddr vaddr, const T data) {
  183. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  184. if (page_pointer) {
  185. // NOTE: Avoid adding any extra logic to this fast-path block
  186. std::memcpy(&page_pointer[vaddr & PAGE_MASK], &data, sizeof(T));
  187. return;
  188. }
  189. PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  190. switch (type) {
  191. case PageType::Unmapped:
  192. LOG_ERROR(HW_Memory, "unmapped Write%lu 0x%llx @ 0x%llx", sizeof(data) * 8, (u64)data,
  193. vaddr);
  194. return;
  195. case PageType::Memory:
  196. ASSERT_MSG(false, "Mapped memory page without a pointer @ %08X", vaddr);
  197. break;
  198. case PageType::RasterizerCachedMemory: {
  199. RasterizerFlushVirtualRegion(vaddr, sizeof(T), FlushMode::FlushAndInvalidate);
  200. std::memcpy(GetPointerFromVMA(vaddr), &data, sizeof(T));
  201. break;
  202. }
  203. case PageType::Special:
  204. WriteMMIO<T>(GetMMIOHandler(vaddr), vaddr, data);
  205. break;
  206. case PageType::RasterizerCachedSpecial: {
  207. RasterizerFlushVirtualRegion(vaddr, sizeof(T), FlushMode::FlushAndInvalidate);
  208. WriteMMIO<T>(GetMMIOHandler(vaddr), vaddr, data);
  209. break;
  210. }
  211. default:
  212. UNREACHABLE();
  213. }
  214. }
  215. bool IsValidVirtualAddress(const VAddr vaddr) {
  216. const u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  217. if (page_pointer)
  218. return true;
  219. if (current_page_table->attributes[vaddr >> PAGE_BITS] == PageType::RasterizerCachedMemory)
  220. return true;
  221. if (current_page_table->attributes[vaddr >> PAGE_BITS] != PageType::Special)
  222. return false;
  223. MMIORegionPointer mmio_region = GetMMIOHandler(vaddr);
  224. if (mmio_region) {
  225. return mmio_region->IsValidAddress(vaddr);
  226. }
  227. return false;
  228. }
  229. bool IsValidPhysicalAddress(const PAddr paddr) {
  230. boost::optional<VAddr> vaddr = PhysicalToVirtualAddress(paddr);
  231. return vaddr && IsValidVirtualAddress(*vaddr);
  232. }
  233. u8* GetPointer(const VAddr vaddr) {
  234. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  235. if (page_pointer) {
  236. return page_pointer + (vaddr & PAGE_MASK);
  237. }
  238. if (current_page_table->attributes[vaddr >> PAGE_BITS] == PageType::RasterizerCachedMemory) {
  239. return GetPointerFromVMA(vaddr);
  240. }
  241. LOG_ERROR(HW_Memory, "unknown GetPointer @ 0x%llx", vaddr);
  242. return nullptr;
  243. }
  244. std::string ReadCString(VAddr vaddr, std::size_t max_length) {
  245. std::string string;
  246. string.reserve(max_length);
  247. for (std::size_t i = 0; i < max_length; ++i) {
  248. char c = Read8(vaddr);
  249. if (c == '\0')
  250. break;
  251. string.push_back(c);
  252. ++vaddr;
  253. }
  254. string.shrink_to_fit();
  255. return string;
  256. }
  257. u8* GetPhysicalPointer(PAddr address) {
  258. // TODO(Subv): This call should not go through the application's memory mapping.
  259. boost::optional<VAddr> vaddr = PhysicalToVirtualAddress(address);
  260. return vaddr ? GetPointer(*vaddr) : nullptr;
  261. }
  262. void RasterizerMarkRegionCached(PAddr start, u64 size, int count_delta) {
  263. if (start == 0) {
  264. return;
  265. }
  266. u64 num_pages = ((start + size - 1) >> PAGE_BITS) - (start >> PAGE_BITS) + 1;
  267. PAddr paddr = start;
  268. for (unsigned i = 0; i < num_pages; ++i, paddr += PAGE_SIZE) {
  269. boost::optional<VAddr> maybe_vaddr = PhysicalToVirtualAddress(paddr);
  270. if (!maybe_vaddr)
  271. continue;
  272. VAddr vaddr = *maybe_vaddr;
  273. u8& res_count = current_page_table->cached_res_count[vaddr >> PAGE_BITS];
  274. ASSERT_MSG(count_delta <= UINT8_MAX - res_count,
  275. "Rasterizer resource cache counter overflow!");
  276. ASSERT_MSG(count_delta >= -res_count, "Rasterizer resource cache counter underflow!");
  277. // Switch page type to cached if now cached
  278. if (res_count == 0) {
  279. PageType& page_type = current_page_table->attributes[vaddr >> PAGE_BITS];
  280. switch (page_type) {
  281. case PageType::Memory:
  282. page_type = PageType::RasterizerCachedMemory;
  283. current_page_table->pointers[vaddr >> PAGE_BITS] = nullptr;
  284. break;
  285. case PageType::Special:
  286. page_type = PageType::RasterizerCachedSpecial;
  287. break;
  288. default:
  289. UNREACHABLE();
  290. }
  291. }
  292. res_count += count_delta;
  293. // Switch page type to uncached if now uncached
  294. if (res_count == 0) {
  295. PageType& page_type = current_page_table->attributes[vaddr >> PAGE_BITS];
  296. switch (page_type) {
  297. case PageType::RasterizerCachedMemory: {
  298. u8* pointer = GetPointerFromVMA(vaddr & ~PAGE_MASK);
  299. if (pointer == nullptr) {
  300. // It's possible that this function has called been while updating the pagetable
  301. // after unmapping a VMA. In that case the underlying VMA will no longer exist,
  302. // and we should just leave the pagetable entry blank.
  303. page_type = PageType::Unmapped;
  304. } else {
  305. page_type = PageType::Memory;
  306. current_page_table->pointers[vaddr >> PAGE_BITS] = pointer;
  307. }
  308. break;
  309. }
  310. case PageType::RasterizerCachedSpecial:
  311. page_type = PageType::Special;
  312. break;
  313. default:
  314. UNREACHABLE();
  315. }
  316. }
  317. }
  318. }
  319. void RasterizerFlushRegion(PAddr start, u64 size) {
  320. if (VideoCore::g_renderer != nullptr) {
  321. VideoCore::g_renderer->Rasterizer()->FlushRegion(start, size);
  322. }
  323. }
  324. void RasterizerFlushAndInvalidateRegion(PAddr start, u64 size) {
  325. // Since pages are unmapped on shutdown after video core is shutdown, the renderer may be
  326. // null here
  327. if (VideoCore::g_renderer != nullptr) {
  328. VideoCore::g_renderer->Rasterizer()->FlushAndInvalidateRegion(start, size);
  329. }
  330. }
  331. void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode) {
  332. // Since pages are unmapped on shutdown after video core is shutdown, the renderer may be
  333. // null here
  334. if (VideoCore::g_renderer != nullptr) {
  335. VAddr end = start + size;
  336. auto CheckRegion = [&](VAddr region_start, VAddr region_end) {
  337. if (start >= region_end || end <= region_start) {
  338. // No overlap with region
  339. return;
  340. }
  341. VAddr overlap_start = std::max(start, region_start);
  342. VAddr overlap_end = std::min(end, region_end);
  343. PAddr physical_start = TryVirtualToPhysicalAddress(overlap_start).value();
  344. u64 overlap_size = overlap_end - overlap_start;
  345. auto* rasterizer = VideoCore::g_renderer->Rasterizer();
  346. switch (mode) {
  347. case FlushMode::Flush:
  348. rasterizer->FlushRegion(physical_start, overlap_size);
  349. break;
  350. case FlushMode::FlushAndInvalidate:
  351. rasterizer->FlushAndInvalidateRegion(physical_start, overlap_size);
  352. break;
  353. }
  354. };
  355. CheckRegion(LINEAR_HEAP_VADDR, LINEAR_HEAP_VADDR_END);
  356. CheckRegion(NEW_LINEAR_HEAP_VADDR, NEW_LINEAR_HEAP_VADDR_END);
  357. CheckRegion(VRAM_VADDR, VRAM_VADDR_END);
  358. }
  359. }
  360. u8 Read8(const VAddr addr) {
  361. return Read<u8>(addr);
  362. }
  363. u16 Read16(const VAddr addr) {
  364. return Read<u16_le>(addr);
  365. }
  366. u32 Read32(const VAddr addr) {
  367. return Read<u32_le>(addr);
  368. }
  369. u64 Read64(const VAddr addr) {
  370. return Read<u64_le>(addr);
  371. }
  372. void ReadBlock(const VAddr src_addr, void* dest_buffer, const size_t size) {
  373. size_t remaining_size = size;
  374. size_t page_index = src_addr >> PAGE_BITS;
  375. size_t page_offset = src_addr & PAGE_MASK;
  376. while (remaining_size > 0) {
  377. const size_t copy_amount = std::min(PAGE_SIZE - page_offset, remaining_size);
  378. const VAddr current_vaddr = (page_index << PAGE_BITS) + page_offset;
  379. switch (current_page_table->attributes[page_index]) {
  380. case PageType::Unmapped: {
  381. LOG_ERROR(HW_Memory, "unmapped ReadBlock @ 0x%llx (start address = 0x%llx, size = %zu)",
  382. current_vaddr, src_addr, size);
  383. std::memset(dest_buffer, 0, copy_amount);
  384. break;
  385. }
  386. case PageType::Memory: {
  387. DEBUG_ASSERT(current_page_table->pointers[page_index]);
  388. const u8* src_ptr = current_page_table->pointers[page_index] + page_offset;
  389. std::memcpy(dest_buffer, src_ptr, copy_amount);
  390. break;
  391. }
  392. case PageType::Special: {
  393. DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
  394. GetMMIOHandler(current_vaddr)->ReadBlock(current_vaddr, dest_buffer, copy_amount);
  395. break;
  396. }
  397. case PageType::RasterizerCachedMemory: {
  398. RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::Flush);
  399. std::memcpy(dest_buffer, GetPointerFromVMA(current_vaddr), copy_amount);
  400. break;
  401. }
  402. case PageType::RasterizerCachedSpecial: {
  403. DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
  404. RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::Flush);
  405. GetMMIOHandler(current_vaddr)->ReadBlock(current_vaddr, dest_buffer, copy_amount);
  406. break;
  407. }
  408. default:
  409. UNREACHABLE();
  410. }
  411. page_index++;
  412. page_offset = 0;
  413. dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
  414. remaining_size -= copy_amount;
  415. }
  416. }
  417. void Write8(const VAddr addr, const u8 data) {
  418. Write<u8>(addr, data);
  419. }
  420. void Write16(const VAddr addr, const u16 data) {
  421. Write<u16_le>(addr, data);
  422. }
  423. void Write32(const VAddr addr, const u32 data) {
  424. Write<u32_le>(addr, data);
  425. }
  426. void Write64(const VAddr addr, const u64 data) {
  427. Write<u64_le>(addr, data);
  428. }
  429. void WriteBlock(const VAddr dest_addr, const void* src_buffer, const size_t size) {
  430. size_t remaining_size = size;
  431. size_t page_index = dest_addr >> PAGE_BITS;
  432. size_t page_offset = dest_addr & PAGE_MASK;
  433. while (remaining_size > 0) {
  434. const size_t copy_amount = std::min(PAGE_SIZE - page_offset, remaining_size);
  435. const VAddr current_vaddr = (page_index << PAGE_BITS) + page_offset;
  436. switch (current_page_table->attributes[page_index]) {
  437. case PageType::Unmapped: {
  438. LOG_ERROR(HW_Memory,
  439. "unmapped WriteBlock @ 0x%llx (start address = 0x%llx, size = %zu)",
  440. current_vaddr, dest_addr, size);
  441. break;
  442. }
  443. case PageType::Memory: {
  444. DEBUG_ASSERT(current_page_table->pointers[page_index]);
  445. u8* dest_ptr = current_page_table->pointers[page_index] + page_offset;
  446. std::memcpy(dest_ptr, src_buffer, copy_amount);
  447. break;
  448. }
  449. case PageType::Special: {
  450. DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
  451. GetMMIOHandler(current_vaddr)->WriteBlock(current_vaddr, src_buffer, copy_amount);
  452. break;
  453. }
  454. case PageType::RasterizerCachedMemory: {
  455. RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::FlushAndInvalidate);
  456. std::memcpy(GetPointerFromVMA(current_vaddr), src_buffer, copy_amount);
  457. break;
  458. }
  459. case PageType::RasterizerCachedSpecial: {
  460. DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
  461. RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::FlushAndInvalidate);
  462. GetMMIOHandler(current_vaddr)->WriteBlock(current_vaddr, src_buffer, copy_amount);
  463. break;
  464. }
  465. default:
  466. UNREACHABLE();
  467. }
  468. page_index++;
  469. page_offset = 0;
  470. src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
  471. remaining_size -= copy_amount;
  472. }
  473. }
  474. void ZeroBlock(const VAddr dest_addr, const size_t size) {
  475. size_t remaining_size = size;
  476. size_t page_index = dest_addr >> PAGE_BITS;
  477. size_t page_offset = dest_addr & PAGE_MASK;
  478. static const std::array<u8, PAGE_SIZE> zeros = {};
  479. while (remaining_size > 0) {
  480. const size_t copy_amount = std::min(PAGE_SIZE - page_offset, remaining_size);
  481. const VAddr current_vaddr = (page_index << PAGE_BITS) + page_offset;
  482. switch (current_page_table->attributes[page_index]) {
  483. case PageType::Unmapped: {
  484. LOG_ERROR(HW_Memory, "unmapped ZeroBlock @ 0x%llx (start address = 0x%llx, size = %zu)",
  485. current_vaddr, dest_addr, size);
  486. break;
  487. }
  488. case PageType::Memory: {
  489. DEBUG_ASSERT(current_page_table->pointers[page_index]);
  490. u8* dest_ptr = current_page_table->pointers[page_index] + page_offset;
  491. std::memset(dest_ptr, 0, copy_amount);
  492. break;
  493. }
  494. case PageType::Special: {
  495. DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
  496. GetMMIOHandler(current_vaddr)->WriteBlock(current_vaddr, zeros.data(), copy_amount);
  497. break;
  498. }
  499. case PageType::RasterizerCachedMemory: {
  500. RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::FlushAndInvalidate);
  501. std::memset(GetPointerFromVMA(current_vaddr), 0, copy_amount);
  502. break;
  503. }
  504. case PageType::RasterizerCachedSpecial: {
  505. DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
  506. RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::FlushAndInvalidate);
  507. GetMMIOHandler(current_vaddr)->WriteBlock(current_vaddr, zeros.data(), copy_amount);
  508. break;
  509. }
  510. default:
  511. UNREACHABLE();
  512. }
  513. page_index++;
  514. page_offset = 0;
  515. remaining_size -= copy_amount;
  516. }
  517. }
  518. void CopyBlock(VAddr dest_addr, VAddr src_addr, const size_t size) {
  519. size_t remaining_size = size;
  520. size_t page_index = src_addr >> PAGE_BITS;
  521. size_t page_offset = src_addr & PAGE_MASK;
  522. while (remaining_size > 0) {
  523. const size_t copy_amount = std::min(PAGE_SIZE - page_offset, remaining_size);
  524. const VAddr current_vaddr = (page_index << PAGE_BITS) + page_offset;
  525. switch (current_page_table->attributes[page_index]) {
  526. case PageType::Unmapped: {
  527. LOG_ERROR(HW_Memory, "unmapped CopyBlock @ 0x%llx (start address = 0x%llx, size = %zu)",
  528. current_vaddr, src_addr, size);
  529. ZeroBlock(dest_addr, copy_amount);
  530. break;
  531. }
  532. case PageType::Memory: {
  533. DEBUG_ASSERT(current_page_table->pointers[page_index]);
  534. const u8* src_ptr = current_page_table->pointers[page_index] + page_offset;
  535. WriteBlock(dest_addr, src_ptr, copy_amount);
  536. break;
  537. }
  538. case PageType::Special: {
  539. DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
  540. std::vector<u8> buffer(copy_amount);
  541. GetMMIOHandler(current_vaddr)->ReadBlock(current_vaddr, buffer.data(), buffer.size());
  542. WriteBlock(dest_addr, buffer.data(), buffer.size());
  543. break;
  544. }
  545. case PageType::RasterizerCachedMemory: {
  546. RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::Flush);
  547. WriteBlock(dest_addr, GetPointerFromVMA(current_vaddr), copy_amount);
  548. break;
  549. }
  550. case PageType::RasterizerCachedSpecial: {
  551. DEBUG_ASSERT(GetMMIOHandler(current_vaddr));
  552. RasterizerFlushVirtualRegion(current_vaddr, copy_amount, FlushMode::Flush);
  553. std::vector<u8> buffer(copy_amount);
  554. GetMMIOHandler(current_vaddr)->ReadBlock(current_vaddr, buffer.data(), buffer.size());
  555. WriteBlock(dest_addr, buffer.data(), buffer.size());
  556. break;
  557. }
  558. default:
  559. UNREACHABLE();
  560. }
  561. page_index++;
  562. page_offset = 0;
  563. dest_addr += copy_amount;
  564. src_addr += copy_amount;
  565. remaining_size -= copy_amount;
  566. }
  567. }
  568. template <>
  569. u8 ReadMMIO<u8>(MMIORegionPointer mmio_handler, VAddr addr) {
  570. return mmio_handler->Read8(addr);
  571. }
  572. template <>
  573. u16 ReadMMIO<u16>(MMIORegionPointer mmio_handler, VAddr addr) {
  574. return mmio_handler->Read16(addr);
  575. }
  576. template <>
  577. u32 ReadMMIO<u32>(MMIORegionPointer mmio_handler, VAddr addr) {
  578. return mmio_handler->Read32(addr);
  579. }
  580. template <>
  581. u64 ReadMMIO<u64>(MMIORegionPointer mmio_handler, VAddr addr) {
  582. return mmio_handler->Read64(addr);
  583. }
  584. template <>
  585. void WriteMMIO<u8>(MMIORegionPointer mmio_handler, VAddr addr, const u8 data) {
  586. mmio_handler->Write8(addr, data);
  587. }
  588. template <>
  589. void WriteMMIO<u16>(MMIORegionPointer mmio_handler, VAddr addr, const u16 data) {
  590. mmio_handler->Write16(addr, data);
  591. }
  592. template <>
  593. void WriteMMIO<u32>(MMIORegionPointer mmio_handler, VAddr addr, const u32 data) {
  594. mmio_handler->Write32(addr, data);
  595. }
  596. template <>
  597. void WriteMMIO<u64>(MMIORegionPointer mmio_handler, VAddr addr, const u64 data) {
  598. mmio_handler->Write64(addr, data);
  599. }
  600. boost::optional<PAddr> TryVirtualToPhysicalAddress(const VAddr addr) {
  601. if (addr == 0) {
  602. return 0;
  603. } else if (addr >= VRAM_VADDR && addr < VRAM_VADDR_END) {
  604. return addr - VRAM_VADDR + VRAM_PADDR;
  605. } else if (addr >= LINEAR_HEAP_VADDR && addr < LINEAR_HEAP_VADDR_END) {
  606. return addr - LINEAR_HEAP_VADDR + FCRAM_PADDR;
  607. } else if (addr >= NEW_LINEAR_HEAP_VADDR && addr < NEW_LINEAR_HEAP_VADDR_END) {
  608. return addr - NEW_LINEAR_HEAP_VADDR + FCRAM_PADDR;
  609. } else if (addr >= DSP_RAM_VADDR && addr < DSP_RAM_VADDR_END) {
  610. return addr - DSP_RAM_VADDR + DSP_RAM_PADDR;
  611. } else if (addr >= IO_AREA_VADDR && addr < IO_AREA_VADDR_END) {
  612. return addr - IO_AREA_VADDR + IO_AREA_PADDR;
  613. } else if (addr >= N3DS_EXTRA_RAM_VADDR && addr < N3DS_EXTRA_RAM_VADDR_END) {
  614. return addr - N3DS_EXTRA_RAM_VADDR + N3DS_EXTRA_RAM_PADDR;
  615. }
  616. return boost::none;
  617. }
  618. PAddr VirtualToPhysicalAddress(const VAddr addr) {
  619. auto paddr = TryVirtualToPhysicalAddress(addr);
  620. if (!paddr) {
  621. LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x%llx", addr);
  622. // To help with debugging, set bit on address so that it's obviously invalid.
  623. return addr | 0x80000000;
  624. }
  625. return *paddr;
  626. }
  627. boost::optional<VAddr> PhysicalToVirtualAddress(const PAddr addr) {
  628. if (addr == 0) {
  629. return 0;
  630. } else if (addr >= VRAM_PADDR && addr < VRAM_PADDR_END) {
  631. return addr - VRAM_PADDR + VRAM_VADDR;
  632. } else if (addr >= FCRAM_PADDR && addr < FCRAM_PADDR_END) {
  633. return addr - FCRAM_PADDR + Kernel::g_current_process->GetLinearHeapAreaAddress();
  634. } else if (addr >= DSP_RAM_PADDR && addr < DSP_RAM_PADDR_END) {
  635. return addr - DSP_RAM_PADDR + DSP_RAM_VADDR;
  636. } else if (addr >= IO_AREA_PADDR && addr < IO_AREA_PADDR_END) {
  637. return addr - IO_AREA_PADDR + IO_AREA_VADDR;
  638. } else if (addr >= N3DS_EXTRA_RAM_PADDR && addr < N3DS_EXTRA_RAM_PADDR_END) {
  639. return addr - N3DS_EXTRA_RAM_PADDR + N3DS_EXTRA_RAM_VADDR;
  640. }
  641. return boost::none;
  642. }
  643. } // namespace