memory.cpp 25 KB

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