memory.cpp 27 KB

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