memory.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <cstring>
  7. #include <boost/optional.hpp>
  8. #include "common/assert.h"
  9. #include "common/common_types.h"
  10. #include "common/logging/log.h"
  11. #include "common/swap.h"
  12. #include "core/arm/arm_interface.h"
  13. #include "core/core.h"
  14. #include "core/hle/kernel/memory.h"
  15. #include "core/hle/kernel/process.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, VAddr base, u64 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. VAddr end = base + size;
  37. while (base != end) {
  38. ASSERT_MSG(base < PAGE_TABLE_NUM_ENTRIES, "out of range mapping at %08X", base);
  39. page_table.attributes[base] = type;
  40. page_table.pointers[base] = memory;
  41. base += 1;
  42. if (memory != nullptr)
  43. memory += PAGE_SIZE;
  44. }
  45. }
  46. void MapMemoryRegion(PageTable& page_table, VAddr base, u64 size, u8* target) {
  47. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  48. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  49. MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, target, PageType::Memory);
  50. }
  51. void MapIoRegion(PageTable& page_table, VAddr base, u64 size, MemoryHookPointer mmio_handler) {
  52. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  53. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  54. MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Special);
  55. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  56. SpecialRegion region{SpecialRegion::Type::IODevice, mmio_handler};
  57. page_table.special_regions.add(std::make_pair(interval, std::set<SpecialRegion>{region}));
  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. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  64. page_table.special_regions.erase(interval);
  65. }
  66. void AddDebugHook(PageTable& page_table, VAddr base, u64 size, MemoryHookPointer hook) {
  67. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  68. SpecialRegion region{SpecialRegion::Type::DebugHook, hook};
  69. page_table.special_regions.add(std::make_pair(interval, std::set<SpecialRegion>{region}));
  70. }
  71. void RemoveDebugHook(PageTable& page_table, VAddr base, u64 size, MemoryHookPointer hook) {
  72. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  73. SpecialRegion region{SpecialRegion::Type::DebugHook, hook};
  74. page_table.special_regions.subtract(std::make_pair(interval, std::set<SpecialRegion>{region}));
  75. }
  76. /**
  77. * This function should only be called for virtual addreses with attribute `PageType::Special`.
  78. */
  79. static std::set<MemoryHookPointer> GetSpecialHandlers(const PageTable& page_table, VAddr vaddr,
  80. u64 size) {
  81. std::set<MemoryHookPointer> result;
  82. auto interval = boost::icl::discrete_interval<VAddr>::closed(vaddr, vaddr + size - 1);
  83. auto interval_list = page_table.special_regions.equal_range(interval);
  84. for (auto it = interval_list.first; it != interval_list.second; ++it) {
  85. for (const auto& region : it->second) {
  86. result.insert(region.handler);
  87. }
  88. }
  89. return result;
  90. }
  91. static std::set<MemoryHookPointer> GetSpecialHandlers(VAddr vaddr, u64 size) {
  92. const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
  93. return GetSpecialHandlers(page_table, vaddr, size);
  94. }
  95. template <typename T>
  96. boost::optional<T> ReadSpecial(VAddr addr);
  97. template <typename T>
  98. T Read(const VAddr vaddr) {
  99. const PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  100. switch (type) {
  101. case PageType::Unmapped:
  102. LOG_ERROR(HW_Memory, "unmapped Read%lu @ 0x%016llX", sizeof(T) * 8, vaddr);
  103. return 0;
  104. case PageType::Special: {
  105. if (auto result = ReadSpecial<T>(vaddr))
  106. return *result;
  107. [[fallthrough]];
  108. }
  109. case PageType::Memory: {
  110. const u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  111. ASSERT_MSG(page_pointer, "Mapped memory page without a pointer @ %08X", vaddr);
  112. T value;
  113. std::memcpy(&value, &page_pointer[vaddr & PAGE_MASK], sizeof(T));
  114. return value;
  115. }
  116. }
  117. UNREACHABLE();
  118. return 0;
  119. }
  120. template <typename T>
  121. bool WriteSpecial(VAddr addr, const T data);
  122. template <typename T>
  123. void Write(const VAddr vaddr, const T data) {
  124. const PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  125. switch (type) {
  126. case PageType::Unmapped:
  127. LOG_ERROR(HW_Memory, "unmapped Write%lu 0x%08X @ 0x%08X", sizeof(data) * 8, (u32)data,
  128. vaddr);
  129. return;
  130. case PageType::Special: {
  131. if (WriteSpecial<T>(vaddr, data))
  132. return;
  133. [[fallthrough]];
  134. }
  135. case PageType::Memory: {
  136. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  137. ASSERT_MSG(page_pointer, "Mapped memory page without a pointer @ %08X", vaddr);
  138. std::memcpy(&page_pointer[vaddr & PAGE_MASK], &data, sizeof(T));
  139. return;
  140. }
  141. }
  142. UNREACHABLE();
  143. }
  144. bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) {
  145. auto& page_table = process.vm_manager.page_table;
  146. if ((vaddr >> PAGE_BITS) >= PAGE_TABLE_NUM_ENTRIES)
  147. return false;
  148. const PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  149. switch (type) {
  150. case PageType::Unmapped:
  151. return false;
  152. case PageType::Memory:
  153. return true;
  154. case PageType::Special: {
  155. for (auto handler : GetSpecialHandlers(page_table, vaddr, 1))
  156. if (auto result = handler->IsValidAddress(vaddr))
  157. return *result;
  158. return current_page_table->pointers[vaddr >> PAGE_BITS] != nullptr;
  159. }
  160. }
  161. UNREACHABLE();
  162. return false;
  163. }
  164. bool IsValidVirtualAddress(const VAddr vaddr) {
  165. return IsValidVirtualAddress(*Kernel::g_current_process, vaddr);
  166. }
  167. bool IsValidPhysicalAddress(const PAddr paddr) {
  168. return GetPhysicalPointer(paddr) != nullptr;
  169. }
  170. u8* GetPointer(const VAddr vaddr) {
  171. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  172. if (page_pointer) {
  173. return page_pointer + (vaddr & PAGE_MASK);
  174. }
  175. LOG_ERROR(HW_Memory, "unknown GetPointer @ 0x%08x", vaddr);
  176. return nullptr;
  177. }
  178. std::string ReadCString(VAddr vaddr, std::size_t max_length) {
  179. std::string string;
  180. string.reserve(max_length);
  181. for (std::size_t i = 0; i < max_length; ++i) {
  182. char c = Read8(vaddr);
  183. if (c == '\0')
  184. break;
  185. string.push_back(c);
  186. ++vaddr;
  187. }
  188. string.shrink_to_fit();
  189. return string;
  190. }
  191. u8* GetPhysicalPointer(PAddr address) {
  192. struct MemoryArea {
  193. PAddr paddr_base;
  194. u32 size;
  195. };
  196. static constexpr MemoryArea memory_areas[] = {
  197. {VRAM_PADDR, VRAM_SIZE},
  198. {IO_AREA_PADDR, IO_AREA_SIZE},
  199. {DSP_RAM_PADDR, DSP_RAM_SIZE},
  200. {FCRAM_PADDR, FCRAM_N3DS_SIZE},
  201. {N3DS_EXTRA_RAM_PADDR, N3DS_EXTRA_RAM_SIZE},
  202. };
  203. const auto area =
  204. std::find_if(std::begin(memory_areas), std::end(memory_areas), [&](const auto& area) {
  205. return address >= area.paddr_base && address < area.paddr_base + area.size;
  206. });
  207. if (area == std::end(memory_areas)) {
  208. LOG_ERROR(HW_Memory, "unknown GetPhysicalPointer @ 0x%08X", address);
  209. return nullptr;
  210. }
  211. if (area->paddr_base == IO_AREA_PADDR) {
  212. LOG_ERROR(HW_Memory, "MMIO mappings are not supported yet. phys_addr=0x%08X", address);
  213. return nullptr;
  214. }
  215. u64 offset_into_region = address - area->paddr_base;
  216. u8* target_pointer = nullptr;
  217. switch (area->paddr_base) {
  218. case VRAM_PADDR:
  219. target_pointer = vram.data() + offset_into_region;
  220. break;
  221. case DSP_RAM_PADDR:
  222. break;
  223. case FCRAM_PADDR:
  224. for (const auto& region : Kernel::memory_regions) {
  225. if (offset_into_region >= region.base &&
  226. offset_into_region < region.base + region.size) {
  227. target_pointer =
  228. region.linear_heap_memory->data() + offset_into_region - region.base;
  229. break;
  230. }
  231. }
  232. ASSERT_MSG(target_pointer != nullptr, "Invalid FCRAM address");
  233. break;
  234. case N3DS_EXTRA_RAM_PADDR:
  235. target_pointer = n3ds_extra_ram.data() + offset_into_region;
  236. break;
  237. default:
  238. UNREACHABLE();
  239. }
  240. return target_pointer;
  241. }
  242. u8 Read8(const VAddr addr) {
  243. return Read<u8>(addr);
  244. }
  245. u16 Read16(const VAddr addr) {
  246. return Read<u16_le>(addr);
  247. }
  248. u32 Read32(const VAddr addr) {
  249. return Read<u32_le>(addr);
  250. }
  251. u64 Read64(const VAddr addr) {
  252. return Read<u64_le>(addr);
  253. }
  254. static bool ReadSpecialBlock(const Kernel::Process& process, const VAddr src_addr,
  255. void* dest_buffer, const size_t size) {
  256. auto& page_table = process.vm_manager.page_table;
  257. for (const auto& handler : GetSpecialHandlers(page_table, src_addr, size)) {
  258. if (handler->ReadBlock(src_addr, dest_buffer, size)) {
  259. return true;
  260. }
  261. }
  262. return false;
  263. }
  264. void ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer,
  265. const size_t size) {
  266. auto& page_table = process.vm_manager.page_table;
  267. size_t remaining_size = size;
  268. size_t page_index = src_addr >> PAGE_BITS;
  269. size_t page_offset = src_addr & PAGE_MASK;
  270. while (remaining_size > 0) {
  271. const size_t copy_amount = std::min<size_t>(PAGE_SIZE - page_offset, remaining_size);
  272. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  273. switch (page_table.attributes[page_index]) {
  274. case PageType::Unmapped:
  275. LOG_ERROR(HW_Memory, "unmapped ReadBlock @ 0x%08X (start address = 0xllx, size = %zu)",
  276. current_vaddr, src_addr, size);
  277. std::memset(dest_buffer, 0, copy_amount);
  278. break;
  279. case PageType::Special: {
  280. if (ReadSpecialBlock(process, current_vaddr, dest_buffer, copy_amount))
  281. break;
  282. [[fallthrough]];
  283. }
  284. case PageType::Memory: {
  285. DEBUG_ASSERT(page_table.pointers[page_index]);
  286. const u8* src_ptr = page_table.pointers[page_index] + page_offset;
  287. std::memcpy(dest_buffer, src_ptr, copy_amount);
  288. break;
  289. }
  290. default:
  291. UNREACHABLE();
  292. }
  293. page_index++;
  294. page_offset = 0;
  295. dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
  296. remaining_size -= copy_amount;
  297. }
  298. }
  299. void ReadBlock(const VAddr src_addr, void* dest_buffer, const size_t size) {
  300. ReadBlock(*Kernel::g_current_process, src_addr, dest_buffer, size);
  301. }
  302. void Write8(const VAddr addr, const u8 data) {
  303. Write<u8>(addr, data);
  304. }
  305. void Write16(const VAddr addr, const u16 data) {
  306. Write<u16_le>(addr, data);
  307. }
  308. void Write32(const VAddr addr, const u32 data) {
  309. Write<u32_le>(addr, data);
  310. }
  311. void Write64(const VAddr addr, const u64 data) {
  312. Write<u64_le>(addr, data);
  313. }
  314. static bool WriteSpecialBlock(const Kernel::Process& process, const VAddr dest_addr,
  315. const void* src_buffer, const size_t size) {
  316. auto& page_table = process.vm_manager.page_table;
  317. for (const auto& handler : GetSpecialHandlers(page_table, dest_addr, size)) {
  318. if (handler->WriteBlock(dest_addr, src_buffer, size)) {
  319. return true;
  320. }
  321. }
  322. return false;
  323. }
  324. void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const void* src_buffer,
  325. const size_t size) {
  326. auto& page_table = process.vm_manager.page_table;
  327. size_t remaining_size = size;
  328. size_t page_index = dest_addr >> PAGE_BITS;
  329. size_t page_offset = dest_addr & PAGE_MASK;
  330. while (remaining_size > 0) {
  331. const size_t copy_amount = std::min<size_t>(PAGE_SIZE - page_offset, remaining_size);
  332. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  333. switch (page_table.attributes[page_index]) {
  334. case PageType::Unmapped:
  335. LOG_ERROR(HW_Memory,
  336. "unmapped WriteBlock @ 0x%08X (start address = 0x%08X, size = %zu)",
  337. current_vaddr, dest_addr, size);
  338. break;
  339. case PageType::Special:
  340. if (WriteSpecialBlock(process, current_vaddr, src_buffer, copy_amount))
  341. break;
  342. [[fallthrough]];
  343. case PageType::Memory: {
  344. DEBUG_ASSERT(page_table.pointers[page_index]);
  345. u8* dest_ptr = page_table.pointers[page_index] + page_offset;
  346. std::memcpy(dest_ptr, src_buffer, copy_amount);
  347. break;
  348. }
  349. default:
  350. UNREACHABLE();
  351. }
  352. page_index++;
  353. page_offset = 0;
  354. src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
  355. remaining_size -= copy_amount;
  356. }
  357. }
  358. void WriteBlock(const VAddr dest_addr, const void* src_buffer, const size_t size) {
  359. WriteBlock(*Kernel::g_current_process, dest_addr, src_buffer, size);
  360. }
  361. void ZeroBlock(const VAddr dest_addr, const size_t size) {
  362. const auto& process = *Kernel::g_current_process;
  363. size_t remaining_size = size;
  364. size_t page_index = dest_addr >> PAGE_BITS;
  365. size_t page_offset = dest_addr & PAGE_MASK;
  366. static const std::array<u8, PAGE_SIZE> zeros = {};
  367. while (remaining_size > 0) {
  368. const size_t copy_amount = std::min<size_t>(PAGE_SIZE - page_offset, remaining_size);
  369. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  370. switch (current_page_table->attributes[page_index]) {
  371. case PageType::Unmapped:
  372. LOG_ERROR(HW_Memory, "unmapped ZeroBlock @ 0x%08X (start address = 0x%08X, size = %zu)",
  373. current_vaddr, dest_addr, size);
  374. break;
  375. case PageType::Special:
  376. if (WriteSpecialBlock(process, current_vaddr, zeros.data(), copy_amount))
  377. break;
  378. [[fallthrough]];
  379. case PageType::Memory: {
  380. DEBUG_ASSERT(current_page_table->pointers[page_index]);
  381. u8* dest_ptr = current_page_table->pointers[page_index] + page_offset;
  382. std::memset(dest_ptr, 0, copy_amount);
  383. break;
  384. }
  385. default:
  386. UNREACHABLE();
  387. }
  388. page_index++;
  389. page_offset = 0;
  390. remaining_size -= copy_amount;
  391. }
  392. }
  393. void CopyBlock(VAddr dest_addr, VAddr src_addr, const size_t size) {
  394. const auto& process = *Kernel::g_current_process;
  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<size_t>(PAGE_SIZE - page_offset, remaining_size);
  400. const VAddr current_vaddr = static_cast<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 CopyBlock @ 0x%08X (start address = 0x%08X, size = %zu)",
  404. current_vaddr, src_addr, size);
  405. ZeroBlock(dest_addr, copy_amount);
  406. break;
  407. case PageType::Special: {
  408. std::vector<u8> buffer(copy_amount);
  409. if (ReadSpecialBlock(process, current_vaddr, buffer.data(), buffer.size())) {
  410. WriteBlock(dest_addr, buffer.data(), buffer.size());
  411. break;
  412. }
  413. [[fallthrough]];
  414. }
  415. case PageType::Memory: {
  416. DEBUG_ASSERT(current_page_table->pointers[page_index]);
  417. const u8* src_ptr = current_page_table->pointers[page_index] + page_offset;
  418. WriteBlock(dest_addr, src_ptr, copy_amount);
  419. break;
  420. }
  421. default:
  422. UNREACHABLE();
  423. }
  424. page_index++;
  425. page_offset = 0;
  426. dest_addr += static_cast<VAddr>(copy_amount);
  427. src_addr += static_cast<VAddr>(copy_amount);
  428. remaining_size -= copy_amount;
  429. }
  430. }
  431. template <>
  432. boost::optional<u8> ReadSpecial<u8>(VAddr addr) {
  433. const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
  434. for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u8)))
  435. if (auto result = handler->Read8(addr))
  436. return *result;
  437. return {};
  438. }
  439. template <>
  440. boost::optional<u16> ReadSpecial<u16>(VAddr addr) {
  441. const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
  442. for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u16)))
  443. if (auto result = handler->Read16(addr))
  444. return *result;
  445. return {};
  446. }
  447. template <>
  448. boost::optional<u32> ReadSpecial<u32>(VAddr addr) {
  449. const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
  450. for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u32)))
  451. if (auto result = handler->Read32(addr))
  452. return *result;
  453. return {};
  454. }
  455. template <>
  456. boost::optional<u64> ReadSpecial<u64>(VAddr addr) {
  457. const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
  458. for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u64)))
  459. if (auto result = handler->Read64(addr))
  460. return *result;
  461. return {};
  462. }
  463. template <>
  464. bool WriteSpecial<u8>(VAddr addr, const u8 data) {
  465. const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
  466. for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u8)))
  467. if (handler->Write8(addr, data))
  468. return true;
  469. return false;
  470. }
  471. template <>
  472. bool WriteSpecial<u16>(VAddr addr, const u16 data) {
  473. const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
  474. for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u16)))
  475. if (handler->Write16(addr, data))
  476. return true;
  477. return false;
  478. }
  479. template <>
  480. bool WriteSpecial<u32>(VAddr addr, const u32 data) {
  481. const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
  482. for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u32)))
  483. if (handler->Write32(addr, data))
  484. return true;
  485. return false;
  486. }
  487. template <>
  488. bool WriteSpecial<u64>(VAddr addr, const u64 data) {
  489. const PageTable& page_table = Kernel::g_current_process->vm_manager.page_table;
  490. for (const auto& handler : GetSpecialHandlers(page_table, addr, sizeof(u64)))
  491. if (handler->Write64(addr, data))
  492. return true;
  493. return false;
  494. }
  495. boost::optional<PAddr> TryVirtualToPhysicalAddress(const VAddr addr) {
  496. if (addr == 0) {
  497. return 0;
  498. } else if (addr >= VRAM_VADDR && addr < VRAM_VADDR_END) {
  499. return addr - VRAM_VADDR + VRAM_PADDR;
  500. } else if (addr >= LINEAR_HEAP_VADDR && addr < LINEAR_HEAP_VADDR_END) {
  501. return addr - LINEAR_HEAP_VADDR + FCRAM_PADDR;
  502. } else if (addr >= NEW_LINEAR_HEAP_VADDR && addr < NEW_LINEAR_HEAP_VADDR_END) {
  503. return addr - NEW_LINEAR_HEAP_VADDR + FCRAM_PADDR;
  504. } else if (addr >= DSP_RAM_VADDR && addr < DSP_RAM_VADDR_END) {
  505. return addr - DSP_RAM_VADDR + DSP_RAM_PADDR;
  506. } else if (addr >= IO_AREA_VADDR && addr < IO_AREA_VADDR_END) {
  507. return addr - IO_AREA_VADDR + IO_AREA_PADDR;
  508. } else if (addr >= N3DS_EXTRA_RAM_VADDR && addr < N3DS_EXTRA_RAM_VADDR_END) {
  509. return addr - N3DS_EXTRA_RAM_VADDR + N3DS_EXTRA_RAM_PADDR;
  510. }
  511. return boost::none;
  512. }
  513. PAddr VirtualToPhysicalAddress(const VAddr addr) {
  514. auto paddr = TryVirtualToPhysicalAddress(addr);
  515. if (!paddr) {
  516. LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x%08X", addr);
  517. // To help with debugging, set bit on address so that it's obviously invalid.
  518. return addr | 0x80000000;
  519. }
  520. return *paddr;
  521. }
  522. boost::optional<VAddr> PhysicalToVirtualAddress(const PAddr addr) {
  523. if (addr == 0) {
  524. return 0;
  525. } else if (addr >= VRAM_PADDR && addr < VRAM_PADDR_END) {
  526. return addr - VRAM_PADDR + VRAM_VADDR;
  527. } else if (addr >= FCRAM_PADDR && addr < FCRAM_PADDR_END) {
  528. return addr - FCRAM_PADDR + Kernel::g_current_process->GetLinearHeapAreaAddress();
  529. } else if (addr >= DSP_RAM_PADDR && addr < DSP_RAM_PADDR_END) {
  530. return addr - DSP_RAM_PADDR + DSP_RAM_VADDR;
  531. } else if (addr >= IO_AREA_PADDR && addr < IO_AREA_PADDR_END) {
  532. return addr - IO_AREA_PADDR + IO_AREA_VADDR;
  533. } else if (addr >= N3DS_EXTRA_RAM_PADDR && addr < N3DS_EXTRA_RAM_PADDR_END) {
  534. return addr - N3DS_EXTRA_RAM_PADDR + N3DS_EXTRA_RAM_VADDR;
  535. }
  536. return boost::none;
  537. }
  538. } // namespace Memory