memory.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 <cstring>
  6. #include <optional>
  7. #include <utility>
  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/process.h"
  15. #include "core/hle/kernel/vm_manager.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. namespace Memory {
  21. static PageTable* current_page_table = nullptr;
  22. void SetCurrentPageTable(PageTable* page_table) {
  23. current_page_table = page_table;
  24. auto& system = Core::System::GetInstance();
  25. if (system.IsPoweredOn()) {
  26. system.ArmInterface(0).PageTableChanged();
  27. system.ArmInterface(1).PageTableChanged();
  28. system.ArmInterface(2).PageTableChanged();
  29. system.ArmInterface(3).PageTableChanged();
  30. }
  31. }
  32. PageTable* GetCurrentPageTable() {
  33. return current_page_table;
  34. }
  35. PageTable::PageTable() = default;
  36. PageTable::PageTable(std::size_t address_space_width_in_bits) {
  37. Resize(address_space_width_in_bits);
  38. }
  39. PageTable::~PageTable() = default;
  40. void PageTable::Resize(std::size_t address_space_width_in_bits) {
  41. const std::size_t num_page_table_entries = 1ULL << (address_space_width_in_bits - PAGE_BITS);
  42. pointers.resize(num_page_table_entries);
  43. attributes.resize(num_page_table_entries);
  44. // The default is a 39-bit address space, which causes an initial 1GB allocation size. If the
  45. // vector size is subsequently decreased (via resize), the vector might not automatically
  46. // actually reallocate/resize its underlying allocation, which wastes up to ~800 MB for
  47. // 36-bit titles. Call shrink_to_fit to reduce capacity to what's actually in use.
  48. pointers.shrink_to_fit();
  49. attributes.shrink_to_fit();
  50. }
  51. static void MapPages(PageTable& page_table, VAddr base, u64 size, u8* memory, PageType type) {
  52. LOG_DEBUG(HW_Memory, "Mapping {} onto {:016X}-{:016X}", fmt::ptr(memory), base * PAGE_SIZE,
  53. (base + size) * PAGE_SIZE);
  54. RasterizerFlushVirtualRegion(base << PAGE_BITS, size * PAGE_SIZE,
  55. FlushMode::FlushAndInvalidate);
  56. VAddr end = base + size;
  57. ASSERT_MSG(end <= page_table.pointers.size(), "out of range mapping at {:016X}",
  58. base + page_table.pointers.size());
  59. std::fill(page_table.attributes.begin() + base, page_table.attributes.begin() + end, type);
  60. if (memory == nullptr) {
  61. std::fill(page_table.pointers.begin() + base, page_table.pointers.begin() + end, memory);
  62. } else {
  63. while (base != end) {
  64. page_table.pointers[base] = memory;
  65. base += 1;
  66. memory += PAGE_SIZE;
  67. }
  68. }
  69. }
  70. void MapMemoryRegion(PageTable& page_table, VAddr base, u64 size, u8* target) {
  71. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: {:016X}", size);
  72. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: {:016X}", base);
  73. MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, target, PageType::Memory);
  74. }
  75. void MapIoRegion(PageTable& page_table, VAddr base, u64 size, MemoryHookPointer mmio_handler) {
  76. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: {:016X}", size);
  77. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: {:016X}", base);
  78. MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Special);
  79. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  80. SpecialRegion region{SpecialRegion::Type::IODevice, std::move(mmio_handler)};
  81. page_table.special_regions.add(std::make_pair(interval, std::set<SpecialRegion>{region}));
  82. }
  83. void UnmapRegion(PageTable& page_table, VAddr base, u64 size) {
  84. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: {:016X}", size);
  85. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: {:016X}", base);
  86. MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Unmapped);
  87. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  88. page_table.special_regions.erase(interval);
  89. }
  90. void AddDebugHook(PageTable& page_table, VAddr base, u64 size, MemoryHookPointer hook) {
  91. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  92. SpecialRegion region{SpecialRegion::Type::DebugHook, std::move(hook)};
  93. page_table.special_regions.add(std::make_pair(interval, std::set<SpecialRegion>{region}));
  94. }
  95. void RemoveDebugHook(PageTable& page_table, VAddr base, u64 size, MemoryHookPointer hook) {
  96. auto interval = boost::icl::discrete_interval<VAddr>::closed(base, base + size - 1);
  97. SpecialRegion region{SpecialRegion::Type::DebugHook, std::move(hook)};
  98. page_table.special_regions.subtract(std::make_pair(interval, std::set<SpecialRegion>{region}));
  99. }
  100. /**
  101. * Gets a pointer to the exact memory at the virtual address (i.e. not page aligned)
  102. * using a VMA from the current process
  103. */
  104. static u8* GetPointerFromVMA(const Kernel::Process& process, VAddr vaddr) {
  105. const auto& vm_manager = process.VMManager();
  106. const auto it = vm_manager.FindVMA(vaddr);
  107. DEBUG_ASSERT(vm_manager.IsValidHandle(it));
  108. u8* direct_pointer = nullptr;
  109. const auto& vma = it->second;
  110. switch (vma.type) {
  111. case Kernel::VMAType::AllocatedMemoryBlock:
  112. direct_pointer = vma.backing_block->data() + vma.offset;
  113. break;
  114. case Kernel::VMAType::BackingMemory:
  115. direct_pointer = vma.backing_memory;
  116. break;
  117. case Kernel::VMAType::Free:
  118. return nullptr;
  119. default:
  120. UNREACHABLE();
  121. }
  122. return direct_pointer + (vaddr - vma.base);
  123. }
  124. /**
  125. * Gets a pointer to the exact memory at the virtual address (i.e. not page aligned)
  126. * using a VMA from the current process.
  127. */
  128. static u8* GetPointerFromVMA(VAddr vaddr) {
  129. return GetPointerFromVMA(*Core::CurrentProcess(), vaddr);
  130. }
  131. template <typename T>
  132. T Read(const VAddr vaddr) {
  133. const u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  134. if (page_pointer) {
  135. // NOTE: Avoid adding any extra logic to this fast-path block
  136. T value;
  137. std::memcpy(&value, &page_pointer[vaddr & PAGE_MASK], sizeof(T));
  138. return value;
  139. }
  140. PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  141. switch (type) {
  142. case PageType::Unmapped:
  143. LOG_ERROR(HW_Memory, "Unmapped Read{} @ 0x{:08X}", sizeof(T) * 8, vaddr);
  144. return 0;
  145. case PageType::Memory:
  146. ASSERT_MSG(false, "Mapped memory page without a pointer @ {:016X}", 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. return {};
  158. }
  159. template <typename T>
  160. void Write(const VAddr vaddr, const T data) {
  161. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  162. if (page_pointer) {
  163. // NOTE: Avoid adding any extra logic to this fast-path block
  164. std::memcpy(&page_pointer[vaddr & PAGE_MASK], &data, sizeof(T));
  165. return;
  166. }
  167. PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  168. switch (type) {
  169. case PageType::Unmapped:
  170. LOG_ERROR(HW_Memory, "Unmapped Write{} 0x{:08X} @ 0x{:016X}", sizeof(data) * 8,
  171. static_cast<u32>(data), vaddr);
  172. return;
  173. case PageType::Memory:
  174. ASSERT_MSG(false, "Mapped memory page without a pointer @ {:016X}", vaddr);
  175. break;
  176. case PageType::RasterizerCachedMemory: {
  177. RasterizerFlushVirtualRegion(vaddr, sizeof(T), FlushMode::Invalidate);
  178. std::memcpy(GetPointerFromVMA(vaddr), &data, sizeof(T));
  179. break;
  180. }
  181. default:
  182. UNREACHABLE();
  183. }
  184. }
  185. bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) {
  186. const auto& page_table = process.VMManager().page_table;
  187. const u8* page_pointer = page_table.pointers[vaddr >> PAGE_BITS];
  188. if (page_pointer)
  189. return true;
  190. if (page_table.attributes[vaddr >> PAGE_BITS] == PageType::RasterizerCachedMemory)
  191. return true;
  192. if (page_table.attributes[vaddr >> PAGE_BITS] != PageType::Special)
  193. return false;
  194. return false;
  195. }
  196. bool IsValidVirtualAddress(const VAddr vaddr) {
  197. return IsValidVirtualAddress(*Core::CurrentProcess(), vaddr);
  198. }
  199. bool IsKernelVirtualAddress(const VAddr vaddr) {
  200. return KERNEL_REGION_VADDR <= vaddr && vaddr < KERNEL_REGION_END;
  201. }
  202. u8* GetPointer(const VAddr vaddr) {
  203. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  204. if (page_pointer) {
  205. return page_pointer + (vaddr & PAGE_MASK);
  206. }
  207. if (current_page_table->attributes[vaddr >> PAGE_BITS] == PageType::RasterizerCachedMemory) {
  208. return GetPointerFromVMA(vaddr);
  209. }
  210. LOG_ERROR(HW_Memory, "Unknown GetPointer @ 0x{:016X}", vaddr);
  211. return nullptr;
  212. }
  213. std::string ReadCString(VAddr vaddr, std::size_t max_length) {
  214. std::string string;
  215. string.reserve(max_length);
  216. for (std::size_t i = 0; i < max_length; ++i) {
  217. char c = Read8(vaddr);
  218. if (c == '\0')
  219. break;
  220. string.push_back(c);
  221. ++vaddr;
  222. }
  223. string.shrink_to_fit();
  224. return string;
  225. }
  226. void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached) {
  227. if (vaddr == 0) {
  228. return;
  229. }
  230. // Iterate over a contiguous CPU address space, which corresponds to the specified GPU address
  231. // space, marking the region as un/cached. The region is marked un/cached at a granularity of
  232. // CPU pages, hence why we iterate on a CPU page basis (note: GPU page size is different). This
  233. // assumes the specified GPU address region is contiguous as well.
  234. u64 num_pages = ((vaddr + size - 1) >> PAGE_BITS) - (vaddr >> PAGE_BITS) + 1;
  235. for (unsigned i = 0; i < num_pages; ++i, vaddr += PAGE_SIZE) {
  236. PageType& page_type = current_page_table->attributes[vaddr >> PAGE_BITS];
  237. if (cached) {
  238. // Switch page type to cached if now cached
  239. switch (page_type) {
  240. case PageType::Unmapped:
  241. // It is not necessary for a process to have this region mapped into its address
  242. // space, for example, a system module need not have a VRAM mapping.
  243. break;
  244. case PageType::Memory:
  245. page_type = PageType::RasterizerCachedMemory;
  246. current_page_table->pointers[vaddr >> PAGE_BITS] = nullptr;
  247. break;
  248. case PageType::RasterizerCachedMemory:
  249. // There can be more than one GPU region mapped per CPU region, so it's common that
  250. // this area is already marked as cached.
  251. break;
  252. default:
  253. UNREACHABLE();
  254. }
  255. } else {
  256. // Switch page type to uncached if now uncached
  257. switch (page_type) {
  258. case PageType::Unmapped:
  259. // It is not necessary for a process to have this region mapped into its address
  260. // space, for example, a system module need not have a VRAM mapping.
  261. break;
  262. case PageType::Memory:
  263. // There can be more than one GPU region mapped per CPU region, so it's common that
  264. // this area is already unmarked as cached.
  265. break;
  266. case PageType::RasterizerCachedMemory: {
  267. u8* pointer = GetPointerFromVMA(vaddr & ~PAGE_MASK);
  268. if (pointer == nullptr) {
  269. // It's possible that this function has been called while updating the pagetable
  270. // after unmapping a VMA. In that case the underlying VMA will no longer exist,
  271. // and we should just leave the pagetable entry blank.
  272. page_type = PageType::Unmapped;
  273. } else {
  274. page_type = PageType::Memory;
  275. current_page_table->pointers[vaddr >> PAGE_BITS] = pointer;
  276. }
  277. break;
  278. }
  279. default:
  280. UNREACHABLE();
  281. }
  282. }
  283. }
  284. }
  285. void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode) {
  286. auto& system_instance = Core::System::GetInstance();
  287. // Since pages are unmapped on shutdown after video core is shutdown, the renderer may be
  288. // null here
  289. if (!system_instance.IsPoweredOn()) {
  290. return;
  291. }
  292. const VAddr end = start + size;
  293. const auto CheckRegion = [&](VAddr region_start, VAddr region_end) {
  294. if (start >= region_end || end <= region_start) {
  295. // No overlap with region
  296. return;
  297. }
  298. const VAddr overlap_start = std::max(start, region_start);
  299. const VAddr overlap_end = std::min(end, region_end);
  300. const VAddr overlap_size = overlap_end - overlap_start;
  301. auto& gpu = system_instance.GPU();
  302. switch (mode) {
  303. case FlushMode::Flush:
  304. gpu.FlushRegion(overlap_start, overlap_size);
  305. break;
  306. case FlushMode::Invalidate:
  307. gpu.InvalidateRegion(overlap_start, overlap_size);
  308. break;
  309. case FlushMode::FlushAndInvalidate:
  310. gpu.FlushAndInvalidateRegion(overlap_start, overlap_size);
  311. break;
  312. }
  313. };
  314. const auto& vm_manager = Core::CurrentProcess()->VMManager();
  315. CheckRegion(vm_manager.GetCodeRegionBaseAddress(), vm_manager.GetCodeRegionEndAddress());
  316. CheckRegion(vm_manager.GetHeapRegionBaseAddress(), vm_manager.GetHeapRegionEndAddress());
  317. }
  318. u8 Read8(const VAddr addr) {
  319. return Read<u8>(addr);
  320. }
  321. u16 Read16(const VAddr addr) {
  322. return Read<u16_le>(addr);
  323. }
  324. u32 Read32(const VAddr addr) {
  325. return Read<u32_le>(addr);
  326. }
  327. u64 Read64(const VAddr addr) {
  328. return Read<u64_le>(addr);
  329. }
  330. void ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer,
  331. const std::size_t size) {
  332. const auto& page_table = process.VMManager().page_table;
  333. std::size_t remaining_size = size;
  334. std::size_t page_index = src_addr >> PAGE_BITS;
  335. std::size_t page_offset = src_addr & PAGE_MASK;
  336. while (remaining_size > 0) {
  337. const std::size_t copy_amount =
  338. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  339. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  340. switch (page_table.attributes[page_index]) {
  341. case PageType::Unmapped: {
  342. LOG_ERROR(HW_Memory,
  343. "Unmapped ReadBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  344. current_vaddr, src_addr, size);
  345. std::memset(dest_buffer, 0, copy_amount);
  346. break;
  347. }
  348. case PageType::Memory: {
  349. DEBUG_ASSERT(page_table.pointers[page_index]);
  350. const u8* src_ptr = page_table.pointers[page_index] + page_offset;
  351. std::memcpy(dest_buffer, src_ptr, copy_amount);
  352. break;
  353. }
  354. case PageType::RasterizerCachedMemory: {
  355. RasterizerFlushVirtualRegion(current_vaddr, static_cast<u32>(copy_amount),
  356. FlushMode::Flush);
  357. std::memcpy(dest_buffer, GetPointerFromVMA(process, current_vaddr), copy_amount);
  358. break;
  359. }
  360. default:
  361. UNREACHABLE();
  362. }
  363. page_index++;
  364. page_offset = 0;
  365. dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
  366. remaining_size -= copy_amount;
  367. }
  368. }
  369. void ReadBlock(const VAddr src_addr, void* dest_buffer, const std::size_t size) {
  370. ReadBlock(*Core::CurrentProcess(), src_addr, dest_buffer, size);
  371. }
  372. void Write8(const VAddr addr, const u8 data) {
  373. Write<u8>(addr, data);
  374. }
  375. void Write16(const VAddr addr, const u16 data) {
  376. Write<u16_le>(addr, data);
  377. }
  378. void Write32(const VAddr addr, const u32 data) {
  379. Write<u32_le>(addr, data);
  380. }
  381. void Write64(const VAddr addr, const u64 data) {
  382. Write<u64_le>(addr, data);
  383. }
  384. void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const void* src_buffer,
  385. const std::size_t size) {
  386. const auto& page_table = process.VMManager().page_table;
  387. std::size_t remaining_size = size;
  388. std::size_t page_index = dest_addr >> PAGE_BITS;
  389. std::size_t page_offset = dest_addr & PAGE_MASK;
  390. while (remaining_size > 0) {
  391. const std::size_t copy_amount =
  392. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  393. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  394. switch (page_table.attributes[page_index]) {
  395. case PageType::Unmapped: {
  396. LOG_ERROR(HW_Memory,
  397. "Unmapped WriteBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  398. current_vaddr, dest_addr, size);
  399. break;
  400. }
  401. case PageType::Memory: {
  402. DEBUG_ASSERT(page_table.pointers[page_index]);
  403. u8* dest_ptr = page_table.pointers[page_index] + page_offset;
  404. std::memcpy(dest_ptr, src_buffer, copy_amount);
  405. break;
  406. }
  407. case PageType::RasterizerCachedMemory: {
  408. RasterizerFlushVirtualRegion(current_vaddr, static_cast<u32>(copy_amount),
  409. FlushMode::Invalidate);
  410. std::memcpy(GetPointerFromVMA(process, current_vaddr), src_buffer, copy_amount);
  411. break;
  412. }
  413. default:
  414. UNREACHABLE();
  415. }
  416. page_index++;
  417. page_offset = 0;
  418. src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
  419. remaining_size -= copy_amount;
  420. }
  421. }
  422. void WriteBlock(const VAddr dest_addr, const void* src_buffer, const std::size_t size) {
  423. WriteBlock(*Core::CurrentProcess(), dest_addr, src_buffer, size);
  424. }
  425. void ZeroBlock(const Kernel::Process& process, const VAddr dest_addr, const std::size_t size) {
  426. const auto& page_table = process.VMManager().page_table;
  427. std::size_t remaining_size = size;
  428. std::size_t page_index = dest_addr >> PAGE_BITS;
  429. std::size_t page_offset = dest_addr & PAGE_MASK;
  430. while (remaining_size > 0) {
  431. const std::size_t copy_amount =
  432. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  433. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  434. switch (page_table.attributes[page_index]) {
  435. case PageType::Unmapped: {
  436. LOG_ERROR(HW_Memory,
  437. "Unmapped ZeroBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  438. current_vaddr, dest_addr, size);
  439. break;
  440. }
  441. case PageType::Memory: {
  442. DEBUG_ASSERT(page_table.pointers[page_index]);
  443. u8* dest_ptr = page_table.pointers[page_index] + page_offset;
  444. std::memset(dest_ptr, 0, copy_amount);
  445. break;
  446. }
  447. case PageType::RasterizerCachedMemory: {
  448. RasterizerFlushVirtualRegion(current_vaddr, static_cast<u32>(copy_amount),
  449. FlushMode::Invalidate);
  450. std::memset(GetPointerFromVMA(process, current_vaddr), 0, copy_amount);
  451. break;
  452. }
  453. default:
  454. UNREACHABLE();
  455. }
  456. page_index++;
  457. page_offset = 0;
  458. remaining_size -= copy_amount;
  459. }
  460. }
  461. void CopyBlock(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr,
  462. const std::size_t size) {
  463. const auto& page_table = process.VMManager().page_table;
  464. std::size_t remaining_size = size;
  465. std::size_t page_index = src_addr >> PAGE_BITS;
  466. std::size_t page_offset = src_addr & PAGE_MASK;
  467. while (remaining_size > 0) {
  468. const std::size_t copy_amount =
  469. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  470. const VAddr current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  471. switch (page_table.attributes[page_index]) {
  472. case PageType::Unmapped: {
  473. LOG_ERROR(HW_Memory,
  474. "Unmapped CopyBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  475. current_vaddr, src_addr, size);
  476. ZeroBlock(process, dest_addr, copy_amount);
  477. break;
  478. }
  479. case PageType::Memory: {
  480. DEBUG_ASSERT(page_table.pointers[page_index]);
  481. const u8* src_ptr = page_table.pointers[page_index] + page_offset;
  482. WriteBlock(process, dest_addr, src_ptr, copy_amount);
  483. break;
  484. }
  485. case PageType::RasterizerCachedMemory: {
  486. RasterizerFlushVirtualRegion(current_vaddr, static_cast<u32>(copy_amount),
  487. FlushMode::Flush);
  488. WriteBlock(process, dest_addr, GetPointerFromVMA(process, current_vaddr), copy_amount);
  489. break;
  490. }
  491. default:
  492. UNREACHABLE();
  493. }
  494. page_index++;
  495. page_offset = 0;
  496. dest_addr += static_cast<VAddr>(copy_amount);
  497. src_addr += static_cast<VAddr>(copy_amount);
  498. remaining_size -= copy_amount;
  499. }
  500. }
  501. void CopyBlock(VAddr dest_addr, VAddr src_addr, std::size_t size) {
  502. CopyBlock(*Core::CurrentProcess(), dest_addr, src_addr, size);
  503. }
  504. } // namespace Memory