memory.cpp 36 KB

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