memory.cpp 27 KB

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