memory.cpp 27 KB

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