memory.cpp 26 KB

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