memory.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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/atomic_ops.h"
  10. #include "common/common_types.h"
  11. #include "common/logging/log.h"
  12. #include "common/page_table.h"
  13. #include "common/swap.h"
  14. #include "core/arm/arm_interface.h"
  15. #include "core/core.h"
  16. #include "core/device_memory.h"
  17. #include "core/hle/kernel/memory/page_table.h"
  18. #include "core/hle/kernel/physical_memory.h"
  19. #include "core/hle/kernel/process.h"
  20. #include "core/memory.h"
  21. #include "video_core/gpu.h"
  22. namespace Core::Memory {
  23. // Implementation class used to keep the specifics of the memory subsystem hidden
  24. // from outside classes. This also allows modification to the internals of the memory
  25. // subsystem without needing to rebuild all files that make use of the memory interface.
  26. struct Memory::Impl {
  27. explicit Impl(Core::System& system_) : system{system_} {}
  28. void SetCurrentPageTable(Kernel::Process& process, u32 core_id) {
  29. current_page_table = &process.PageTable().PageTableImpl();
  30. const std::size_t address_space_width = process.PageTable().GetAddressSpaceWidth();
  31. system.ArmInterface(core_id).PageTableChanged(*current_page_table, address_space_width);
  32. }
  33. void MapMemoryRegion(Common::PageTable& page_table, VAddr base, u64 size, PAddr 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. UNIMPLEMENTED();
  41. }
  42. void UnmapRegion(Common::PageTable& page_table, VAddr base, u64 size) {
  43. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: {:016X}", size);
  44. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: {:016X}", base);
  45. MapPages(page_table, base / PAGE_SIZE, size / PAGE_SIZE, 0, Common::PageType::Unmapped);
  46. }
  47. void AddDebugHook(Common::PageTable& page_table, VAddr base, u64 size,
  48. Common::MemoryHookPointer hook) {
  49. UNIMPLEMENTED();
  50. }
  51. void RemoveDebugHook(Common::PageTable& page_table, VAddr base, u64 size,
  52. Common::MemoryHookPointer hook) {
  53. UNIMPLEMENTED();
  54. }
  55. bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) const {
  56. const auto& page_table = process.PageTable().PageTableImpl();
  57. const u8* const page_pointer = page_table.pointers[vaddr >> PAGE_BITS];
  58. if (page_pointer != nullptr) {
  59. return true;
  60. }
  61. if (page_table.attributes[vaddr >> PAGE_BITS] == Common::PageType::RasterizerCachedMemory) {
  62. return true;
  63. }
  64. if (page_table.attributes[vaddr >> PAGE_BITS] != Common::PageType::Special) {
  65. return false;
  66. }
  67. return false;
  68. }
  69. bool IsValidVirtualAddress(VAddr vaddr) const {
  70. return IsValidVirtualAddress(*system.CurrentProcess(), vaddr);
  71. }
  72. u8* GetPointerFromRasterizerCachedMemory(VAddr vaddr) const {
  73. const PAddr paddr{current_page_table->backing_addr[vaddr >> PAGE_BITS]};
  74. if (!paddr) {
  75. return {};
  76. }
  77. return system.DeviceMemory().GetPointer(paddr) + vaddr;
  78. }
  79. u8* GetPointer(const VAddr vaddr) const {
  80. u8* const page_pointer{current_page_table->pointers[vaddr >> PAGE_BITS]};
  81. if (page_pointer) {
  82. return page_pointer + vaddr;
  83. }
  84. if (current_page_table->attributes[vaddr >> PAGE_BITS] ==
  85. Common::PageType::RasterizerCachedMemory) {
  86. return GetPointerFromRasterizerCachedMemory(vaddr);
  87. }
  88. return {};
  89. }
  90. u8 Read8(const VAddr addr) {
  91. return Read<u8>(addr);
  92. }
  93. u16 Read16(const VAddr addr) {
  94. if ((addr & 1) == 0) {
  95. return Read<u16_le>(addr);
  96. } else {
  97. const u32 a{Read<u8>(addr)};
  98. const u32 b{Read<u8>(addr + sizeof(u8))};
  99. return static_cast<u16>((b << 8) | a);
  100. }
  101. }
  102. u32 Read32(const VAddr addr) {
  103. if ((addr & 3) == 0) {
  104. return Read<u32_le>(addr);
  105. } else {
  106. const u32 a{Read16(addr)};
  107. const u32 b{Read16(addr + sizeof(u16))};
  108. return (b << 16) | a;
  109. }
  110. }
  111. u64 Read64(const VAddr addr) {
  112. if ((addr & 7) == 0) {
  113. return Read<u64_le>(addr);
  114. } else {
  115. const u32 a{Read32(addr)};
  116. const u32 b{Read32(addr + sizeof(u32))};
  117. return (static_cast<u64>(b) << 32) | a;
  118. }
  119. }
  120. void Write8(const VAddr addr, const u8 data) {
  121. Write<u8>(addr, data);
  122. }
  123. void Write16(const VAddr addr, const u16 data) {
  124. if ((addr & 1) == 0) {
  125. Write<u16_le>(addr, data);
  126. } else {
  127. Write<u8>(addr, static_cast<u8>(data));
  128. Write<u8>(addr + sizeof(u8), static_cast<u8>(data >> 8));
  129. }
  130. }
  131. void Write32(const VAddr addr, const u32 data) {
  132. if ((addr & 3) == 0) {
  133. Write<u32_le>(addr, data);
  134. } else {
  135. Write16(addr, static_cast<u16>(data));
  136. Write16(addr + sizeof(u16), static_cast<u16>(data >> 16));
  137. }
  138. }
  139. void Write64(const VAddr addr, const u64 data) {
  140. if ((addr & 7) == 0) {
  141. Write<u64_le>(addr, data);
  142. } else {
  143. Write32(addr, static_cast<u32>(data));
  144. Write32(addr + sizeof(u32), static_cast<u32>(data >> 32));
  145. }
  146. }
  147. bool WriteExclusive8(const VAddr addr, const u8 data, const u8 expected) {
  148. return WriteExclusive<u8>(addr, data, expected);
  149. }
  150. bool WriteExclusive16(const VAddr addr, const u16 data, const u16 expected) {
  151. return WriteExclusive<u16_le>(addr, data, expected);
  152. }
  153. bool WriteExclusive32(const VAddr addr, const u32 data, const u32 expected) {
  154. return WriteExclusive<u32_le>(addr, data, expected);
  155. }
  156. bool WriteExclusive64(const VAddr addr, const u64 data, const u64 expected) {
  157. return WriteExclusive<u64_le>(addr, data, expected);
  158. }
  159. std::string ReadCString(VAddr vaddr, std::size_t max_length) {
  160. std::string string;
  161. string.reserve(max_length);
  162. for (std::size_t i = 0; i < max_length; ++i) {
  163. const char c = Read8(vaddr);
  164. if (c == '\0') {
  165. break;
  166. }
  167. string.push_back(c);
  168. ++vaddr;
  169. }
  170. string.shrink_to_fit();
  171. return string;
  172. }
  173. void ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer,
  174. const std::size_t size) {
  175. const auto& page_table = process.PageTable().PageTableImpl();
  176. std::size_t remaining_size = size;
  177. std::size_t page_index = src_addr >> PAGE_BITS;
  178. std::size_t page_offset = src_addr & PAGE_MASK;
  179. while (remaining_size > 0) {
  180. const std::size_t copy_amount =
  181. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  182. const auto current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  183. switch (page_table.attributes[page_index]) {
  184. case Common::PageType::Unmapped: {
  185. LOG_ERROR(HW_Memory,
  186. "Unmapped ReadBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  187. current_vaddr, src_addr, size);
  188. std::memset(dest_buffer, 0, copy_amount);
  189. break;
  190. }
  191. case Common::PageType::Memory: {
  192. DEBUG_ASSERT(page_table.pointers[page_index]);
  193. const u8* const src_ptr =
  194. page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS);
  195. std::memcpy(dest_buffer, src_ptr, copy_amount);
  196. break;
  197. }
  198. case Common::PageType::RasterizerCachedMemory: {
  199. const u8* const host_ptr{GetPointerFromRasterizerCachedMemory(current_vaddr)};
  200. system.GPU().FlushRegion(current_vaddr, copy_amount);
  201. std::memcpy(dest_buffer, host_ptr, copy_amount);
  202. break;
  203. }
  204. default:
  205. UNREACHABLE();
  206. }
  207. page_index++;
  208. page_offset = 0;
  209. dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
  210. remaining_size -= copy_amount;
  211. }
  212. }
  213. void ReadBlockUnsafe(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer,
  214. const std::size_t size) {
  215. const auto& page_table = process.PageTable().PageTableImpl();
  216. std::size_t remaining_size = size;
  217. std::size_t page_index = src_addr >> PAGE_BITS;
  218. std::size_t page_offset = src_addr & PAGE_MASK;
  219. while (remaining_size > 0) {
  220. const std::size_t copy_amount =
  221. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  222. const auto current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  223. switch (page_table.attributes[page_index]) {
  224. case Common::PageType::Unmapped: {
  225. LOG_ERROR(HW_Memory,
  226. "Unmapped ReadBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  227. current_vaddr, src_addr, size);
  228. std::memset(dest_buffer, 0, copy_amount);
  229. break;
  230. }
  231. case Common::PageType::Memory: {
  232. DEBUG_ASSERT(page_table.pointers[page_index]);
  233. const u8* const src_ptr =
  234. page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS);
  235. std::memcpy(dest_buffer, src_ptr, copy_amount);
  236. break;
  237. }
  238. case Common::PageType::RasterizerCachedMemory: {
  239. const u8* const host_ptr{GetPointerFromRasterizerCachedMemory(current_vaddr)};
  240. std::memcpy(dest_buffer, host_ptr, copy_amount);
  241. break;
  242. }
  243. default:
  244. UNREACHABLE();
  245. }
  246. page_index++;
  247. page_offset = 0;
  248. dest_buffer = static_cast<u8*>(dest_buffer) + copy_amount;
  249. remaining_size -= copy_amount;
  250. }
  251. }
  252. void ReadBlock(const VAddr src_addr, void* dest_buffer, const std::size_t size) {
  253. ReadBlock(*system.CurrentProcess(), src_addr, dest_buffer, size);
  254. }
  255. void ReadBlockUnsafe(const VAddr src_addr, void* dest_buffer, const std::size_t size) {
  256. ReadBlockUnsafe(*system.CurrentProcess(), src_addr, dest_buffer, size);
  257. }
  258. void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const void* src_buffer,
  259. const std::size_t size) {
  260. const auto& page_table = process.PageTable().PageTableImpl();
  261. std::size_t remaining_size = size;
  262. std::size_t page_index = dest_addr >> PAGE_BITS;
  263. std::size_t page_offset = dest_addr & PAGE_MASK;
  264. while (remaining_size > 0) {
  265. const std::size_t copy_amount =
  266. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  267. const auto current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  268. switch (page_table.attributes[page_index]) {
  269. case Common::PageType::Unmapped: {
  270. LOG_ERROR(HW_Memory,
  271. "Unmapped WriteBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  272. current_vaddr, dest_addr, size);
  273. break;
  274. }
  275. case Common::PageType::Memory: {
  276. DEBUG_ASSERT(page_table.pointers[page_index]);
  277. u8* const dest_ptr =
  278. page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS);
  279. std::memcpy(dest_ptr, src_buffer, copy_amount);
  280. break;
  281. }
  282. case Common::PageType::RasterizerCachedMemory: {
  283. u8* const host_ptr{GetPointerFromRasterizerCachedMemory(current_vaddr)};
  284. system.GPU().InvalidateRegion(current_vaddr, copy_amount);
  285. std::memcpy(host_ptr, src_buffer, copy_amount);
  286. break;
  287. }
  288. default:
  289. UNREACHABLE();
  290. }
  291. page_index++;
  292. page_offset = 0;
  293. src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
  294. remaining_size -= copy_amount;
  295. }
  296. }
  297. void WriteBlockUnsafe(const Kernel::Process& process, const VAddr dest_addr,
  298. const void* src_buffer, const std::size_t size) {
  299. const auto& page_table = process.PageTable().PageTableImpl();
  300. std::size_t remaining_size = size;
  301. std::size_t page_index = dest_addr >> PAGE_BITS;
  302. std::size_t page_offset = dest_addr & PAGE_MASK;
  303. while (remaining_size > 0) {
  304. const std::size_t copy_amount =
  305. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  306. const auto current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  307. switch (page_table.attributes[page_index]) {
  308. case Common::PageType::Unmapped: {
  309. LOG_ERROR(HW_Memory,
  310. "Unmapped WriteBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  311. current_vaddr, dest_addr, size);
  312. break;
  313. }
  314. case Common::PageType::Memory: {
  315. DEBUG_ASSERT(page_table.pointers[page_index]);
  316. u8* const dest_ptr =
  317. page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS);
  318. std::memcpy(dest_ptr, src_buffer, copy_amount);
  319. break;
  320. }
  321. case Common::PageType::RasterizerCachedMemory: {
  322. u8* const host_ptr{GetPointerFromRasterizerCachedMemory(current_vaddr)};
  323. std::memcpy(host_ptr, src_buffer, copy_amount);
  324. break;
  325. }
  326. default:
  327. UNREACHABLE();
  328. }
  329. page_index++;
  330. page_offset = 0;
  331. src_buffer = static_cast<const u8*>(src_buffer) + copy_amount;
  332. remaining_size -= copy_amount;
  333. }
  334. }
  335. void WriteBlock(const VAddr dest_addr, const void* src_buffer, const std::size_t size) {
  336. WriteBlock(*system.CurrentProcess(), dest_addr, src_buffer, size);
  337. }
  338. void WriteBlockUnsafe(const VAddr dest_addr, const void* src_buffer, const std::size_t size) {
  339. WriteBlockUnsafe(*system.CurrentProcess(), dest_addr, src_buffer, size);
  340. }
  341. void ZeroBlock(const Kernel::Process& process, const VAddr dest_addr, const std::size_t size) {
  342. const auto& page_table = process.PageTable().PageTableImpl();
  343. std::size_t remaining_size = size;
  344. std::size_t page_index = dest_addr >> PAGE_BITS;
  345. std::size_t page_offset = dest_addr & PAGE_MASK;
  346. while (remaining_size > 0) {
  347. const std::size_t copy_amount =
  348. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  349. const auto current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  350. switch (page_table.attributes[page_index]) {
  351. case Common::PageType::Unmapped: {
  352. LOG_ERROR(HW_Memory,
  353. "Unmapped ZeroBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  354. current_vaddr, dest_addr, size);
  355. break;
  356. }
  357. case Common::PageType::Memory: {
  358. DEBUG_ASSERT(page_table.pointers[page_index]);
  359. u8* dest_ptr =
  360. page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS);
  361. std::memset(dest_ptr, 0, copy_amount);
  362. break;
  363. }
  364. case Common::PageType::RasterizerCachedMemory: {
  365. u8* const host_ptr{GetPointerFromRasterizerCachedMemory(current_vaddr)};
  366. system.GPU().InvalidateRegion(current_vaddr, copy_amount);
  367. std::memset(host_ptr, 0, copy_amount);
  368. break;
  369. }
  370. default:
  371. UNREACHABLE();
  372. }
  373. page_index++;
  374. page_offset = 0;
  375. remaining_size -= copy_amount;
  376. }
  377. }
  378. void ZeroBlock(const VAddr dest_addr, const std::size_t size) {
  379. ZeroBlock(*system.CurrentProcess(), dest_addr, size);
  380. }
  381. void CopyBlock(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr,
  382. const std::size_t size) {
  383. const auto& page_table = process.PageTable().PageTableImpl();
  384. std::size_t remaining_size = size;
  385. std::size_t page_index = src_addr >> PAGE_BITS;
  386. std::size_t page_offset = src_addr & PAGE_MASK;
  387. while (remaining_size > 0) {
  388. const std::size_t copy_amount =
  389. std::min(static_cast<std::size_t>(PAGE_SIZE) - page_offset, remaining_size);
  390. const auto current_vaddr = static_cast<VAddr>((page_index << PAGE_BITS) + page_offset);
  391. switch (page_table.attributes[page_index]) {
  392. case Common::PageType::Unmapped: {
  393. LOG_ERROR(HW_Memory,
  394. "Unmapped CopyBlock @ 0x{:016X} (start address = 0x{:016X}, size = {})",
  395. current_vaddr, src_addr, size);
  396. ZeroBlock(process, dest_addr, copy_amount);
  397. break;
  398. }
  399. case Common::PageType::Memory: {
  400. DEBUG_ASSERT(page_table.pointers[page_index]);
  401. const u8* src_ptr =
  402. page_table.pointers[page_index] + page_offset + (page_index << PAGE_BITS);
  403. WriteBlock(process, dest_addr, src_ptr, copy_amount);
  404. break;
  405. }
  406. case Common::PageType::RasterizerCachedMemory: {
  407. const u8* const host_ptr{GetPointerFromRasterizerCachedMemory(current_vaddr)};
  408. system.GPU().FlushRegion(current_vaddr, copy_amount);
  409. WriteBlock(process, dest_addr, host_ptr, copy_amount);
  410. break;
  411. }
  412. default:
  413. UNREACHABLE();
  414. }
  415. page_index++;
  416. page_offset = 0;
  417. dest_addr += static_cast<VAddr>(copy_amount);
  418. src_addr += static_cast<VAddr>(copy_amount);
  419. remaining_size -= copy_amount;
  420. }
  421. }
  422. void CopyBlock(VAddr dest_addr, VAddr src_addr, std::size_t size) {
  423. return CopyBlock(*system.CurrentProcess(), dest_addr, src_addr, size);
  424. }
  425. void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached) {
  426. if (vaddr == 0) {
  427. return;
  428. }
  429. // Iterate over a contiguous CPU address space, which corresponds to the specified GPU
  430. // address space, marking the region as un/cached. The region is marked un/cached at a
  431. // granularity of CPU pages, hence why we iterate on a CPU page basis (note: GPU page size
  432. // is different). This assumes the specified GPU address region is contiguous as well.
  433. u64 num_pages = ((vaddr + size - 1) >> PAGE_BITS) - (vaddr >> PAGE_BITS) + 1;
  434. for (unsigned i = 0; i < num_pages; ++i, vaddr += PAGE_SIZE) {
  435. Common::PageType& page_type{current_page_table->attributes[vaddr >> PAGE_BITS]};
  436. if (cached) {
  437. // Switch page type to cached if now cached
  438. switch (page_type) {
  439. case Common::PageType::Unmapped:
  440. // It is not necessary for a process to have this region mapped into its address
  441. // space, for example, a system module need not have a VRAM mapping.
  442. break;
  443. case Common::PageType::Memory:
  444. page_type = Common::PageType::RasterizerCachedMemory;
  445. current_page_table->pointers[vaddr >> PAGE_BITS] = nullptr;
  446. break;
  447. case Common::PageType::RasterizerCachedMemory:
  448. // There can be more than one GPU region mapped per CPU region, so it's common
  449. // that this area is already marked as cached.
  450. break;
  451. default:
  452. UNREACHABLE();
  453. }
  454. } else {
  455. // Switch page type to uncached if now uncached
  456. switch (page_type) {
  457. case Common::PageType::Unmapped:
  458. // It is not necessary for a process to have this region mapped into its address
  459. // space, for example, a system module need not have a VRAM mapping.
  460. break;
  461. case Common::PageType::Memory:
  462. // There can be more than one GPU region mapped per CPU region, so it's common
  463. // that this area is already unmarked as cached.
  464. break;
  465. case Common::PageType::RasterizerCachedMemory: {
  466. u8* pointer{GetPointerFromRasterizerCachedMemory(vaddr & ~PAGE_MASK)};
  467. if (pointer == nullptr) {
  468. // It's possible that this function has been called while updating the
  469. // pagetable after unmapping a VMA. In that case the underlying VMA will no
  470. // longer exist, and we should just leave the pagetable entry blank.
  471. page_type = Common::PageType::Unmapped;
  472. } else {
  473. current_page_table->pointers[vaddr >> PAGE_BITS] =
  474. pointer - (vaddr & ~PAGE_MASK);
  475. page_type = Common::PageType::Memory;
  476. }
  477. break;
  478. }
  479. default:
  480. UNREACHABLE();
  481. }
  482. }
  483. }
  484. }
  485. /**
  486. * Maps a region of pages as a specific type.
  487. *
  488. * @param page_table The page table to use to perform the mapping.
  489. * @param base The base address to begin mapping at.
  490. * @param size The total size of the range in bytes.
  491. * @param target The target address to begin mapping from.
  492. * @param type The page type to map the memory as.
  493. */
  494. void MapPages(Common::PageTable& page_table, VAddr base, u64 size, PAddr target,
  495. Common::PageType type) {
  496. LOG_DEBUG(HW_Memory, "Mapping {:016X} onto {:016X}-{:016X}", target, base * PAGE_SIZE,
  497. (base + size) * PAGE_SIZE);
  498. // During boot, current_page_table might not be set yet, in which case we need not flush
  499. if (system.IsPoweredOn()) {
  500. auto& gpu = system.GPU();
  501. for (u64 i = 0; i < size; i++) {
  502. const auto page = base + i;
  503. if (page_table.attributes[page] == Common::PageType::RasterizerCachedMemory) {
  504. gpu.FlushAndInvalidateRegion(page << PAGE_BITS, PAGE_SIZE);
  505. }
  506. }
  507. }
  508. const VAddr end = base + size;
  509. ASSERT_MSG(end <= page_table.pointers.size(), "out of range mapping at {:016X}",
  510. base + page_table.pointers.size());
  511. if (!target) {
  512. ASSERT_MSG(type != Common::PageType::Memory,
  513. "Mapping memory page without a pointer @ {:016x}", base * PAGE_SIZE);
  514. while (base != end) {
  515. page_table.attributes[base] = type;
  516. page_table.pointers[base] = nullptr;
  517. page_table.backing_addr[base] = 0;
  518. base += 1;
  519. }
  520. } else {
  521. while (base != end) {
  522. page_table.pointers[base] =
  523. system.DeviceMemory().GetPointer(target) - (base << PAGE_BITS);
  524. page_table.attributes[base] = type;
  525. page_table.backing_addr[base] = target - (base << PAGE_BITS);
  526. ASSERT_MSG(page_table.pointers[base],
  527. "memory mapping base yield a nullptr within the table");
  528. base += 1;
  529. target += PAGE_SIZE;
  530. }
  531. }
  532. }
  533. /**
  534. * Reads a particular data type out of memory at the given virtual address.
  535. *
  536. * @param vaddr The virtual address to read the data type from.
  537. *
  538. * @tparam T The data type to read out of memory. This type *must* be
  539. * trivially copyable, otherwise the behavior of this function
  540. * is undefined.
  541. *
  542. * @returns The instance of T read from the specified virtual address.
  543. */
  544. template <typename T>
  545. T Read(const VAddr vaddr) {
  546. const u8* const page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  547. if (page_pointer != nullptr) {
  548. // NOTE: Avoid adding any extra logic to this fast-path block
  549. T value;
  550. std::memcpy(&value, &page_pointer[vaddr], sizeof(T));
  551. return value;
  552. }
  553. const Common::PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  554. switch (type) {
  555. case Common::PageType::Unmapped:
  556. LOG_ERROR(HW_Memory, "Unmapped Read{} @ 0x{:08X}", sizeof(T) * 8, vaddr);
  557. return 0;
  558. case Common::PageType::Memory:
  559. ASSERT_MSG(false, "Mapped memory page without a pointer @ {:016X}", vaddr);
  560. break;
  561. case Common::PageType::RasterizerCachedMemory: {
  562. const u8* const host_ptr{GetPointerFromRasterizerCachedMemory(vaddr)};
  563. system.GPU().FlushRegion(vaddr, sizeof(T));
  564. T value;
  565. std::memcpy(&value, host_ptr, sizeof(T));
  566. return value;
  567. }
  568. default:
  569. UNREACHABLE();
  570. }
  571. return {};
  572. }
  573. /**
  574. * Writes a particular data type to memory at the given virtual address.
  575. *
  576. * @param vaddr The virtual address to write the data type to.
  577. *
  578. * @tparam T The data type to write to memory. This type *must* be
  579. * trivially copyable, otherwise the behavior of this function
  580. * is undefined.
  581. */
  582. template <typename T>
  583. void Write(const VAddr vaddr, const T data) {
  584. u8* const page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  585. if (page_pointer != nullptr) {
  586. // NOTE: Avoid adding any extra logic to this fast-path block
  587. std::memcpy(&page_pointer[vaddr], &data, sizeof(T));
  588. return;
  589. }
  590. const Common::PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  591. switch (type) {
  592. case Common::PageType::Unmapped:
  593. LOG_ERROR(HW_Memory, "Unmapped Write{} 0x{:08X} @ 0x{:016X}", sizeof(data) * 8,
  594. static_cast<u32>(data), vaddr);
  595. return;
  596. case Common::PageType::Memory:
  597. ASSERT_MSG(false, "Mapped memory page without a pointer @ {:016X}", vaddr);
  598. break;
  599. case Common::PageType::RasterizerCachedMemory: {
  600. u8* const host_ptr{GetPointerFromRasterizerCachedMemory(vaddr)};
  601. system.GPU().InvalidateRegion(vaddr, sizeof(T));
  602. std::memcpy(host_ptr, &data, sizeof(T));
  603. break;
  604. }
  605. default:
  606. UNREACHABLE();
  607. }
  608. }
  609. template <typename T>
  610. bool WriteExclusive(const VAddr vaddr, const T data, const T expected) {
  611. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  612. if (page_pointer != nullptr) {
  613. // NOTE: Avoid adding any extra logic to this fast-path block
  614. auto* pointer = reinterpret_cast<volatile T*>(&page_pointer[vaddr]);
  615. return Common::AtomicCompareAndSwap(pointer, data, expected);
  616. }
  617. const Common::PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  618. switch (type) {
  619. case Common::PageType::Unmapped:
  620. LOG_ERROR(HW_Memory, "Unmapped Write{} 0x{:08X} @ 0x{:016X}", sizeof(data) * 8,
  621. static_cast<u32>(data), vaddr);
  622. return true;
  623. case Common::PageType::Memory:
  624. ASSERT_MSG(false, "Mapped memory page without a pointer @ {:016X}", vaddr);
  625. break;
  626. case Common::PageType::RasterizerCachedMemory: {
  627. u8* host_ptr{GetPointerFromRasterizerCachedMemory(vaddr)};
  628. system.GPU().InvalidateRegion(vaddr, sizeof(T));
  629. auto* pointer = reinterpret_cast<volatile T*>(&host_ptr);
  630. return Common::AtomicCompareAndSwap(pointer, data, expected);
  631. }
  632. default:
  633. UNREACHABLE();
  634. }
  635. return true;
  636. }
  637. bool WriteExclusive128(const VAddr vaddr, const u128 data, const u128 expected) {
  638. u8* const page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  639. if (page_pointer != nullptr) {
  640. // NOTE: Avoid adding any extra logic to this fast-path block
  641. auto* pointer = reinterpret_cast<volatile u64*>(&page_pointer[vaddr]);
  642. return Common::AtomicCompareAndSwap(pointer, data, expected);
  643. }
  644. const Common::PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  645. switch (type) {
  646. case Common::PageType::Unmapped:
  647. LOG_ERROR(HW_Memory, "Unmapped Write{} 0x{:08X} @ 0x{:016X}{:016X}", sizeof(data) * 8,
  648. static_cast<u64>(data[1]), static_cast<u64>(data[0]), vaddr);
  649. return true;
  650. case Common::PageType::Memory:
  651. ASSERT_MSG(false, "Mapped memory page without a pointer @ {:016X}", vaddr);
  652. break;
  653. case Common::PageType::RasterizerCachedMemory: {
  654. u8* host_ptr{GetPointerFromRasterizerCachedMemory(vaddr)};
  655. system.GPU().InvalidateRegion(vaddr, sizeof(u128));
  656. auto* pointer = reinterpret_cast<volatile u64*>(&host_ptr);
  657. return Common::AtomicCompareAndSwap(pointer, data, expected);
  658. }
  659. default:
  660. UNREACHABLE();
  661. }
  662. return true;
  663. }
  664. Common::PageTable* current_page_table = nullptr;
  665. Core::System& system;
  666. };
  667. Memory::Memory(Core::System& system) : impl{std::make_unique<Impl>(system)} {}
  668. Memory::~Memory() = default;
  669. void Memory::SetCurrentPageTable(Kernel::Process& process, u32 core_id) {
  670. impl->SetCurrentPageTable(process, core_id);
  671. }
  672. void Memory::MapMemoryRegion(Common::PageTable& page_table, VAddr base, u64 size, PAddr target) {
  673. impl->MapMemoryRegion(page_table, base, size, target);
  674. }
  675. void Memory::MapIoRegion(Common::PageTable& page_table, VAddr base, u64 size,
  676. Common::MemoryHookPointer mmio_handler) {
  677. impl->MapIoRegion(page_table, base, size, std::move(mmio_handler));
  678. }
  679. void Memory::UnmapRegion(Common::PageTable& page_table, VAddr base, u64 size) {
  680. impl->UnmapRegion(page_table, base, size);
  681. }
  682. void Memory::AddDebugHook(Common::PageTable& page_table, VAddr base, u64 size,
  683. Common::MemoryHookPointer hook) {
  684. impl->AddDebugHook(page_table, base, size, std::move(hook));
  685. }
  686. void Memory::RemoveDebugHook(Common::PageTable& page_table, VAddr base, u64 size,
  687. Common::MemoryHookPointer hook) {
  688. impl->RemoveDebugHook(page_table, base, size, std::move(hook));
  689. }
  690. bool Memory::IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr) const {
  691. return impl->IsValidVirtualAddress(process, vaddr);
  692. }
  693. bool Memory::IsValidVirtualAddress(const VAddr vaddr) const {
  694. return impl->IsValidVirtualAddress(vaddr);
  695. }
  696. u8* Memory::GetPointer(VAddr vaddr) {
  697. return impl->GetPointer(vaddr);
  698. }
  699. const u8* Memory::GetPointer(VAddr vaddr) const {
  700. return impl->GetPointer(vaddr);
  701. }
  702. u8 Memory::Read8(const VAddr addr) {
  703. return impl->Read8(addr);
  704. }
  705. u16 Memory::Read16(const VAddr addr) {
  706. return impl->Read16(addr);
  707. }
  708. u32 Memory::Read32(const VAddr addr) {
  709. return impl->Read32(addr);
  710. }
  711. u64 Memory::Read64(const VAddr addr) {
  712. return impl->Read64(addr);
  713. }
  714. void Memory::Write8(VAddr addr, u8 data) {
  715. impl->Write8(addr, data);
  716. }
  717. void Memory::Write16(VAddr addr, u16 data) {
  718. impl->Write16(addr, data);
  719. }
  720. void Memory::Write32(VAddr addr, u32 data) {
  721. impl->Write32(addr, data);
  722. }
  723. void Memory::Write64(VAddr addr, u64 data) {
  724. impl->Write64(addr, data);
  725. }
  726. bool Memory::WriteExclusive8(VAddr addr, u8 data, u8 expected) {
  727. return impl->WriteExclusive8(addr, data, expected);
  728. }
  729. bool Memory::WriteExclusive16(VAddr addr, u16 data, u16 expected) {
  730. return impl->WriteExclusive16(addr, data, expected);
  731. }
  732. bool Memory::WriteExclusive32(VAddr addr, u32 data, u32 expected) {
  733. return impl->WriteExclusive32(addr, data, expected);
  734. }
  735. bool Memory::WriteExclusive64(VAddr addr, u64 data, u64 expected) {
  736. return impl->WriteExclusive64(addr, data, expected);
  737. }
  738. bool Memory::WriteExclusive128(VAddr addr, u128 data, u128 expected) {
  739. return impl->WriteExclusive128(addr, data, expected);
  740. }
  741. std::string Memory::ReadCString(VAddr vaddr, std::size_t max_length) {
  742. return impl->ReadCString(vaddr, max_length);
  743. }
  744. void Memory::ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer,
  745. const std::size_t size) {
  746. impl->ReadBlock(process, src_addr, dest_buffer, size);
  747. }
  748. void Memory::ReadBlock(const VAddr src_addr, void* dest_buffer, const std::size_t size) {
  749. impl->ReadBlock(src_addr, dest_buffer, size);
  750. }
  751. void Memory::ReadBlockUnsafe(const Kernel::Process& process, const VAddr src_addr,
  752. void* dest_buffer, const std::size_t size) {
  753. impl->ReadBlockUnsafe(process, src_addr, dest_buffer, size);
  754. }
  755. void Memory::ReadBlockUnsafe(const VAddr src_addr, void* dest_buffer, const std::size_t size) {
  756. impl->ReadBlockUnsafe(src_addr, dest_buffer, size);
  757. }
  758. void Memory::WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer,
  759. std::size_t size) {
  760. impl->WriteBlock(process, dest_addr, src_buffer, size);
  761. }
  762. void Memory::WriteBlock(const VAddr dest_addr, const void* src_buffer, const std::size_t size) {
  763. impl->WriteBlock(dest_addr, src_buffer, size);
  764. }
  765. void Memory::WriteBlockUnsafe(const Kernel::Process& process, VAddr dest_addr,
  766. const void* src_buffer, std::size_t size) {
  767. impl->WriteBlockUnsafe(process, dest_addr, src_buffer, size);
  768. }
  769. void Memory::WriteBlockUnsafe(const VAddr dest_addr, const void* src_buffer,
  770. const std::size_t size) {
  771. impl->WriteBlockUnsafe(dest_addr, src_buffer, size);
  772. }
  773. void Memory::ZeroBlock(const Kernel::Process& process, VAddr dest_addr, std::size_t size) {
  774. impl->ZeroBlock(process, dest_addr, size);
  775. }
  776. void Memory::ZeroBlock(VAddr dest_addr, std::size_t size) {
  777. impl->ZeroBlock(dest_addr, size);
  778. }
  779. void Memory::CopyBlock(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr,
  780. const std::size_t size) {
  781. impl->CopyBlock(process, dest_addr, src_addr, size);
  782. }
  783. void Memory::CopyBlock(VAddr dest_addr, VAddr src_addr, std::size_t size) {
  784. impl->CopyBlock(dest_addr, src_addr, size);
  785. }
  786. void Memory::RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached) {
  787. impl->RasterizerMarkRegionCached(vaddr, size, cached);
  788. }
  789. bool IsKernelVirtualAddress(const VAddr vaddr) {
  790. return KERNEL_REGION_VADDR <= vaddr && vaddr < KERNEL_REGION_END;
  791. }
  792. } // namespace Core::Memory