memory.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. NGLOG_DEBUG(HW_Memory, "Mapping {} onto {:016X}-{:016X}", fmt::ptr(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. NGLOG_ERROR(HW_Memory, "Unmapped Read{} @ {:#010X}", sizeof(T) * 8, vaddr);
  144. return 0;
  145. case PageType::Memory:
  146. ASSERT_MSG(false, "Mapped memory page without a pointer @ %016" PRIX64, 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. NGLOG_ERROR(HW_Memory, "Unmapped Write{} {:#010X} @ {:#018X}", sizeof(data) * 8, (u32)data,
  172. vaddr);
  173. return;
  174. case PageType::Memory:
  175. ASSERT_MSG(false, "Mapped memory page without a pointer @ %016" PRIX64, 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. NGLOG_ERROR(HW_Memory, "Unknown GetPointer @ {:#018X}", 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. NGLOG_ERROR(HW_Memory, "Unknown GetPhysicalPointer @ {:#018X}", address);
  244. return nullptr;
  245. }
  246. if (area->paddr_base == IO_AREA_PADDR) {
  247. NGLOG_ERROR(HW_Memory, "MMIO mappings are not supported yet. phys_addr={:018X}", address);
  248. return nullptr;
  249. }
  250. u64 offset_into_region = address - area->paddr_base;
  251. u8* target_pointer = nullptr;
  252. switch (area->paddr_base) {
  253. case VRAM_PADDR:
  254. target_pointer = vram.data() + offset_into_region;
  255. break;
  256. case DSP_RAM_PADDR:
  257. break;
  258. case FCRAM_PADDR:
  259. for (const auto& region : Kernel::memory_regions) {
  260. if (offset_into_region >= region.base &&
  261. offset_into_region < region.base + region.size) {
  262. target_pointer =
  263. region.linear_heap_memory->data() + offset_into_region - region.base;
  264. break;
  265. }
  266. }
  267. ASSERT_MSG(target_pointer != nullptr, "Invalid FCRAM address");
  268. break;
  269. default:
  270. UNREACHABLE();
  271. }
  272. return target_pointer;
  273. }
  274. void RasterizerMarkRegionCached(Tegra::GPUVAddr gpu_addr, u64 size, bool cached) {
  275. if (gpu_addr == 0) {
  276. return;
  277. }
  278. // Iterate over a contiguous CPU address space, which corresponds to the specified GPU address
  279. // space, marking the region as un/cached. The region is marked un/cached at a granularity of
  280. // CPU pages, hence why we iterate on a CPU page basis (note: GPU page size is different). This
  281. // assumes the specified GPU address region is contiguous as well.
  282. u64 num_pages = ((gpu_addr + size - 1) >> PAGE_BITS) - (gpu_addr >> PAGE_BITS) + 1;
  283. for (unsigned i = 0; i < num_pages; ++i, gpu_addr += PAGE_SIZE) {
  284. boost::optional<VAddr> maybe_vaddr =
  285. Core::System::GetInstance().GPU().memory_manager->GpuToCpuAddress(gpu_addr);
  286. // The GPU <-> CPU virtual memory mapping is not 1:1
  287. if (!maybe_vaddr) {
  288. NGLOG_ERROR(HW_Memory,
  289. "Trying to flush a cached region to an invalid physical address {:016X}",
  290. gpu_addr);
  291. continue;
  292. }
  293. VAddr vaddr = *maybe_vaddr;
  294. PageType& page_type = current_page_table->attributes[vaddr >> PAGE_BITS];
  295. if (cached) {
  296. // Switch page type to cached if now cached
  297. switch (page_type) {
  298. case PageType::Unmapped:
  299. // It is not necessary for a process to have this region mapped into its address
  300. // space, for example, a system module need not have a VRAM mapping.
  301. break;
  302. case PageType::Memory:
  303. page_type = PageType::RasterizerCachedMemory;
  304. current_page_table->pointers[vaddr >> PAGE_BITS] = nullptr;
  305. break;
  306. case PageType::RasterizerCachedMemory:
  307. // There can be more than one GPU region mapped per CPU region, so it's common that
  308. // this area is already marked as cached.
  309. break;
  310. default:
  311. UNREACHABLE();
  312. }
  313. } else {
  314. // Switch page type to uncached if now uncached
  315. switch (page_type) {
  316. case PageType::Unmapped:
  317. // It is not necessary for a process to have this region mapped into its address
  318. // space, for example, a system module need not have a VRAM mapping.
  319. break;
  320. case PageType::Memory:
  321. // There can be more than one GPU region mapped per CPU region, so it's common that
  322. // this area is already unmarked as cached.
  323. break;
  324. case PageType::RasterizerCachedMemory: {
  325. u8* pointer = GetPointerFromVMA(vaddr & ~PAGE_MASK);
  326. if (pointer == nullptr) {
  327. // It's possible that this function has been called while updating the pagetable
  328. // after unmapping a VMA. In that case the underlying VMA will no longer exist,
  329. // and we should just leave the pagetable entry blank.
  330. page_type = PageType::Unmapped;
  331. } else {
  332. page_type = PageType::Memory;
  333. current_page_table->pointers[vaddr >> PAGE_BITS] = pointer;
  334. }
  335. break;
  336. }
  337. default:
  338. UNREACHABLE();
  339. }
  340. }
  341. }
  342. }
  343. void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode) {
  344. // Since pages are unmapped on shutdown after video core is shutdown, the renderer may be
  345. // null here
  346. if (VideoCore::g_renderer == nullptr) {
  347. return;
  348. }
  349. VAddr end = start + size;
  350. auto CheckRegion = [&](VAddr region_start, VAddr region_end) {
  351. if (start >= region_end || end <= region_start) {
  352. // No overlap with region
  353. return;
  354. }
  355. VAddr overlap_start = std::max(start, region_start);
  356. VAddr overlap_end = std::min(end, region_end);
  357. std::vector<Tegra::GPUVAddr> gpu_addresses =
  358. Core::System::GetInstance().GPU().memory_manager->CpuToGpuAddress(overlap_start);
  359. if (gpu_addresses.empty()) {
  360. return;
  361. }
  362. u64 overlap_size = overlap_end - overlap_start;
  363. for (const auto& gpu_address : gpu_addresses) {
  364. auto* rasterizer = VideoCore::g_renderer->Rasterizer();
  365. switch (mode) {
  366. case FlushMode::Flush:
  367. rasterizer->FlushRegion(gpu_address, overlap_size);
  368. break;
  369. case FlushMode::Invalidate:
  370. rasterizer->InvalidateRegion(gpu_address, overlap_size);
  371. break;
  372. case FlushMode::FlushAndInvalidate:
  373. rasterizer->FlushAndInvalidateRegion(gpu_address, overlap_size);
  374. break;
  375. }
  376. }
  377. };
  378. CheckRegion(PROCESS_IMAGE_VADDR, PROCESS_IMAGE_VADDR_END);
  379. CheckRegion(HEAP_VADDR, HEAP_VADDR_END);
  380. }
  381. u8 Read8(const VAddr addr) {
  382. return Read<u8>(addr);
  383. }
  384. u16 Read16(const VAddr addr) {
  385. return Read<u16_le>(addr);
  386. }
  387. u32 Read32(const VAddr addr) {
  388. return Read<u32_le>(addr);
  389. }
  390. u64 Read64(const VAddr addr) {
  391. return Read<u64_le>(addr);
  392. }
  393. void ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer,
  394. const size_t size) {
  395. auto& page_table = process.vm_manager.page_table;
  396. size_t remaining_size = size;
  397. size_t page_index = src_addr >> PAGE_BITS;
  398. size_t page_offset = src_addr & PAGE_MASK;
  399. while (remaining_size > 0) {
  400. const size_t copy_amount =
  401. std::min(static_cast<size_t>(PAGE_SIZE) - page_offset, remaining_size);
  402. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  403. switch (page_table.attributes[page_index]) {
  404. case PageType::Unmapped: {
  405. NGLOG_ERROR(HW_Memory,
  406. "Unmapped ReadBlock @ {:#018X} (start address = {:#018X}, size = {})",
  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. NGLOG_ERROR(HW_Memory,
  460. "Unmapped WriteBlock @ {:#018X} (start address = {:#018X}, size = {})",
  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. NGLOG_ERROR(HW_Memory,
  501. "Unmapped ZeroBlock @ {:#018X} (start address = {#:018X}, size = {})",
  502. current_vaddr, dest_addr, size);
  503. break;
  504. }
  505. case PageType::Memory: {
  506. DEBUG_ASSERT(page_table.pointers[page_index]);
  507. u8* dest_ptr = page_table.pointers[page_index] + page_offset;
  508. std::memset(dest_ptr, 0, copy_amount);
  509. break;
  510. }
  511. case PageType::RasterizerCachedMemory: {
  512. RasterizerFlushVirtualRegion(current_vaddr, static_cast<u32>(copy_amount),
  513. FlushMode::Invalidate);
  514. std::memset(GetPointerFromVMA(process, current_vaddr), 0, copy_amount);
  515. break;
  516. }
  517. default:
  518. UNREACHABLE();
  519. }
  520. page_index++;
  521. page_offset = 0;
  522. remaining_size -= copy_amount;
  523. }
  524. }
  525. void CopyBlock(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr, const size_t size) {
  526. auto& page_table = process.vm_manager.page_table;
  527. size_t remaining_size = size;
  528. size_t page_index = src_addr >> PAGE_BITS;
  529. size_t page_offset = src_addr & PAGE_MASK;
  530. while (remaining_size > 0) {
  531. const size_t copy_amount =
  532. std::min(static_cast<size_t>(PAGE_SIZE) - page_offset, remaining_size);
  533. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  534. switch (page_table.attributes[page_index]) {
  535. case PageType::Unmapped: {
  536. NGLOG_ERROR(HW_Memory,
  537. "Unmapped CopyBlock @ {:#018X} (start address = {:#018X}, size = {})",
  538. current_vaddr, src_addr, size);
  539. ZeroBlock(process, dest_addr, copy_amount);
  540. break;
  541. }
  542. case PageType::Memory: {
  543. DEBUG_ASSERT(page_table.pointers[page_index]);
  544. const u8* src_ptr = page_table.pointers[page_index] + page_offset;
  545. WriteBlock(process, dest_addr, src_ptr, copy_amount);
  546. break;
  547. }
  548. case PageType::RasterizerCachedMemory: {
  549. RasterizerFlushVirtualRegion(current_vaddr, static_cast<u32>(copy_amount),
  550. FlushMode::Flush);
  551. WriteBlock(process, dest_addr, GetPointerFromVMA(process, current_vaddr), copy_amount);
  552. break;
  553. }
  554. default:
  555. UNREACHABLE();
  556. }
  557. page_index++;
  558. page_offset = 0;
  559. dest_addr += static_cast<VAddr>(copy_amount);
  560. src_addr += static_cast<VAddr>(copy_amount);
  561. remaining_size -= copy_amount;
  562. }
  563. }
  564. void CopyBlock(VAddr dest_addr, VAddr src_addr, size_t size) {
  565. CopyBlock(*Core::CurrentProcess(), dest_addr, src_addr, size);
  566. }
  567. boost::optional<PAddr> TryVirtualToPhysicalAddress(const VAddr addr) {
  568. if (addr == 0) {
  569. return 0;
  570. } else if (addr >= VRAM_VADDR && addr < VRAM_VADDR_END) {
  571. return addr - VRAM_VADDR + VRAM_PADDR;
  572. } else if (addr >= LINEAR_HEAP_VADDR && addr < LINEAR_HEAP_VADDR_END) {
  573. return addr - LINEAR_HEAP_VADDR + FCRAM_PADDR;
  574. } else if (addr >= NEW_LINEAR_HEAP_VADDR && addr < NEW_LINEAR_HEAP_VADDR_END) {
  575. return addr - NEW_LINEAR_HEAP_VADDR + FCRAM_PADDR;
  576. } else if (addr >= DSP_RAM_VADDR && addr < DSP_RAM_VADDR_END) {
  577. return addr - DSP_RAM_VADDR + DSP_RAM_PADDR;
  578. } else if (addr >= IO_AREA_VADDR && addr < IO_AREA_VADDR_END) {
  579. return addr - IO_AREA_VADDR + IO_AREA_PADDR;
  580. }
  581. return boost::none;
  582. }
  583. PAddr VirtualToPhysicalAddress(const VAddr addr) {
  584. auto paddr = TryVirtualToPhysicalAddress(addr);
  585. if (!paddr) {
  586. NGLOG_ERROR(HW_Memory, "Unknown virtual address @ {:#018X}", addr);
  587. // To help with debugging, set bit on address so that it's obviously invalid.
  588. return addr | 0x80000000;
  589. }
  590. return *paddr;
  591. }
  592. boost::optional<VAddr> PhysicalToVirtualAddress(const PAddr addr) {
  593. if (addr == 0) {
  594. return 0;
  595. } else if (addr >= VRAM_PADDR && addr < VRAM_PADDR_END) {
  596. return addr - VRAM_PADDR + VRAM_VADDR;
  597. } else if (addr >= FCRAM_PADDR && addr < FCRAM_PADDR_END) {
  598. return addr - FCRAM_PADDR + Core::CurrentProcess()->GetLinearHeapAreaAddress();
  599. } else if (addr >= DSP_RAM_PADDR && addr < DSP_RAM_PADDR_END) {
  600. return addr - DSP_RAM_PADDR + DSP_RAM_VADDR;
  601. } else if (addr >= IO_AREA_PADDR && addr < IO_AREA_PADDR_END) {
  602. return addr - IO_AREA_PADDR + IO_AREA_VADDR;
  603. }
  604. return boost::none;
  605. }
  606. } // namespace Memory