memory.cpp 25 KB

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