memory.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <cstddef>
  5. #include <memory>
  6. #include <optional>
  7. #include <span>
  8. #include <string>
  9. #include <vector>
  10. #include "common/scratch_buffer.h"
  11. #include "common/typed_address.h"
  12. #include "core/guest_memory.h"
  13. #include "core/hle/result.h"
  14. namespace Common {
  15. enum class MemoryPermission : u32;
  16. struct PageTable;
  17. } // namespace Common
  18. namespace Core {
  19. class System;
  20. class GPUDirtyMemoryManager;
  21. } // namespace Core
  22. namespace Kernel {
  23. class KProcess;
  24. } // namespace Kernel
  25. namespace Tegra {
  26. class MemoryManager;
  27. }
  28. namespace Core::Memory {
  29. /**
  30. * Page size used by the ARM architecture. This is the smallest granularity with which memory can
  31. * be mapped.
  32. */
  33. constexpr std::size_t YUZU_PAGEBITS = 12;
  34. constexpr u64 YUZU_PAGESIZE = 1ULL << YUZU_PAGEBITS;
  35. constexpr u64 YUZU_PAGEMASK = YUZU_PAGESIZE - 1;
  36. /// Virtual user-space memory regions
  37. enum : u64 {
  38. /// TLS (Thread-Local Storage) related.
  39. TLS_ENTRY_SIZE = 0x200,
  40. /// Application stack
  41. DEFAULT_STACK_SIZE = 0x100000,
  42. };
  43. /// Central class that handles all memory operations and state.
  44. class Memory {
  45. public:
  46. explicit Memory(Core::System& system);
  47. ~Memory();
  48. Memory(const Memory&) = delete;
  49. Memory& operator=(const Memory&) = delete;
  50. Memory(Memory&&) = default;
  51. Memory& operator=(Memory&&) = delete;
  52. /**
  53. * Resets the state of the Memory system.
  54. */
  55. void Reset();
  56. /**
  57. * Changes the currently active page table to that of the given process instance.
  58. *
  59. * @param process The process to use the page table of.
  60. */
  61. void SetCurrentPageTable(Kernel::KProcess& process);
  62. /**
  63. * Maps an allocated buffer onto a region of the emulated process address space.
  64. *
  65. * @param page_table The page table of the emulated process.
  66. * @param base The address to start mapping at. Must be page-aligned.
  67. * @param size The amount of bytes to map. Must be page-aligned.
  68. * @param target Buffer with the memory backing the mapping. Must be of length at least
  69. * `size`.
  70. * @param perms The permissions to map the memory with.
  71. */
  72. void MapMemoryRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size,
  73. Common::PhysicalAddress target, Common::MemoryPermission perms,
  74. bool separate_heap);
  75. /**
  76. * Unmaps a region of the emulated process address space.
  77. *
  78. * @param page_table The page table of the emulated process.
  79. * @param base The address to begin unmapping at.
  80. * @param size The amount of bytes to unmap.
  81. */
  82. void UnmapRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size,
  83. bool separate_heap);
  84. /**
  85. * Protects a region of the emulated process address space with the new permissions.
  86. *
  87. * @param page_table The page table of the emulated process.
  88. * @param base The start address to re-protect. Must be page-aligned.
  89. * @param size The amount of bytes to protect. Must be page-aligned.
  90. * @param perms The permissions the address range is mapped.
  91. */
  92. void ProtectRegion(Common::PageTable& page_table, Common::ProcessAddress base, u64 size,
  93. Common::MemoryPermission perms);
  94. /**
  95. * Checks whether or not the supplied address is a valid virtual
  96. * address for the current process.
  97. *
  98. * @param vaddr The virtual address to check the validity of.
  99. *
  100. * @returns True if the given virtual address is valid, false otherwise.
  101. */
  102. [[nodiscard]] bool IsValidVirtualAddress(Common::ProcessAddress vaddr) const;
  103. /**
  104. * Checks whether or not the supplied range of addresses are all valid
  105. * virtual addresses for the current process.
  106. *
  107. * @param base The address to begin checking.
  108. * @param size The amount of bytes to check.
  109. *
  110. * @returns True if all bytes in the given range are valid, false otherwise.
  111. */
  112. [[nodiscard]] bool IsValidVirtualAddressRange(Common::ProcessAddress base, u64 size) const;
  113. /**
  114. * Gets a pointer to the given address.
  115. *
  116. * @param vaddr Virtual address to retrieve a pointer to.
  117. *
  118. * @returns The pointer to the given address, if the address is valid.
  119. * If the address is not valid, nullptr will be returned.
  120. */
  121. u8* GetPointer(Common::ProcessAddress vaddr);
  122. u8* GetPointerSilent(Common::ProcessAddress vaddr);
  123. template <typename T>
  124. T* GetPointer(Common::ProcessAddress vaddr) {
  125. return reinterpret_cast<T*>(GetPointer(vaddr));
  126. }
  127. /**
  128. * Gets a pointer to the given address.
  129. *
  130. * @param vaddr Virtual address to retrieve a pointer to.
  131. *
  132. * @returns The pointer to the given address, if the address is valid.
  133. * If the address is not valid, nullptr will be returned.
  134. */
  135. [[nodiscard]] const u8* GetPointer(Common::ProcessAddress vaddr) const;
  136. template <typename T>
  137. const T* GetPointer(Common::ProcessAddress vaddr) const {
  138. return reinterpret_cast<T*>(GetPointer(vaddr));
  139. }
  140. /**
  141. * Reads an 8-bit unsigned value from the current process' address space
  142. * at the given virtual address.
  143. *
  144. * @param addr The virtual address to read the 8-bit value from.
  145. *
  146. * @returns the read 8-bit unsigned value.
  147. */
  148. u8 Read8(Common::ProcessAddress addr);
  149. /**
  150. * Reads a 16-bit unsigned value from the current process' address space
  151. * at the given virtual address.
  152. *
  153. * @param addr The virtual address to read the 16-bit value from.
  154. *
  155. * @returns the read 16-bit unsigned value.
  156. */
  157. u16 Read16(Common::ProcessAddress addr);
  158. /**
  159. * Reads a 32-bit unsigned value from the current process' address space
  160. * at the given virtual address.
  161. *
  162. * @param addr The virtual address to read the 32-bit value from.
  163. *
  164. * @returns the read 32-bit unsigned value.
  165. */
  166. u32 Read32(Common::ProcessAddress addr);
  167. /**
  168. * Reads a 64-bit unsigned value from the current process' address space
  169. * at the given virtual address.
  170. *
  171. * @param addr The virtual address to read the 64-bit value from.
  172. *
  173. * @returns the read 64-bit value.
  174. */
  175. u64 Read64(Common::ProcessAddress addr);
  176. /**
  177. * Writes an 8-bit unsigned integer to the given virtual address in
  178. * the current process' address space.
  179. *
  180. * @param addr The virtual address to write the 8-bit unsigned integer to.
  181. * @param data The 8-bit unsigned integer to write to the given virtual address.
  182. *
  183. * @post The memory at the given virtual address contains the specified data value.
  184. */
  185. void Write8(Common::ProcessAddress addr, u8 data);
  186. /**
  187. * Writes a 16-bit unsigned integer to the given virtual address in
  188. * the current process' address space.
  189. *
  190. * @param addr The virtual address to write the 16-bit unsigned integer to.
  191. * @param data The 16-bit unsigned integer to write to the given virtual address.
  192. *
  193. * @post The memory range [addr, sizeof(data)) contains the given data value.
  194. */
  195. void Write16(Common::ProcessAddress addr, u16 data);
  196. /**
  197. * Writes a 32-bit unsigned integer to the given virtual address in
  198. * the current process' address space.
  199. *
  200. * @param addr The virtual address to write the 32-bit unsigned integer to.
  201. * @param data The 32-bit unsigned integer to write to the given virtual address.
  202. *
  203. * @post The memory range [addr, sizeof(data)) contains the given data value.
  204. */
  205. void Write32(Common::ProcessAddress addr, u32 data);
  206. /**
  207. * Writes a 64-bit unsigned integer to the given virtual address in
  208. * the current process' address space.
  209. *
  210. * @param addr The virtual address to write the 64-bit unsigned integer to.
  211. * @param data The 64-bit unsigned integer to write to the given virtual address.
  212. *
  213. * @post The memory range [addr, sizeof(data)) contains the given data value.
  214. */
  215. void Write64(Common::ProcessAddress addr, u64 data);
  216. /**
  217. * Writes a 8-bit unsigned integer to the given virtual address in
  218. * the current process' address space if and only if the address contains
  219. * the expected value. This operation is atomic.
  220. *
  221. * @param addr The virtual address to write the 8-bit unsigned integer to.
  222. * @param data The 8-bit unsigned integer to write to the given virtual address.
  223. * @param expected The 8-bit unsigned integer to check against the given virtual address.
  224. *
  225. * @post The memory range [addr, sizeof(data)) contains the given data value.
  226. */
  227. bool WriteExclusive8(Common::ProcessAddress addr, u8 data, u8 expected);
  228. /**
  229. * Writes a 16-bit unsigned integer to the given virtual address in
  230. * the current process' address space if and only if the address contains
  231. * the expected value. This operation is atomic.
  232. *
  233. * @param addr The virtual address to write the 16-bit unsigned integer to.
  234. * @param data The 16-bit unsigned integer to write to the given virtual address.
  235. * @param expected The 16-bit unsigned integer to check against the given virtual address.
  236. *
  237. * @post The memory range [addr, sizeof(data)) contains the given data value.
  238. */
  239. bool WriteExclusive16(Common::ProcessAddress addr, u16 data, u16 expected);
  240. /**
  241. * Writes a 32-bit unsigned integer to the given virtual address in
  242. * the current process' address space if and only if the address contains
  243. * the expected value. This operation is atomic.
  244. *
  245. * @param addr The virtual address to write the 32-bit unsigned integer to.
  246. * @param data The 32-bit unsigned integer to write to the given virtual address.
  247. * @param expected The 32-bit unsigned integer to check against the given virtual address.
  248. *
  249. * @post The memory range [addr, sizeof(data)) contains the given data value.
  250. */
  251. bool WriteExclusive32(Common::ProcessAddress addr, u32 data, u32 expected);
  252. /**
  253. * Writes a 64-bit unsigned integer to the given virtual address in
  254. * the current process' address space if and only if the address contains
  255. * the expected value. This operation is atomic.
  256. *
  257. * @param addr The virtual address to write the 64-bit unsigned integer to.
  258. * @param data The 64-bit unsigned integer to write to the given virtual address.
  259. * @param expected The 64-bit unsigned integer to check against the given virtual address.
  260. *
  261. * @post The memory range [addr, sizeof(data)) contains the given data value.
  262. */
  263. bool WriteExclusive64(Common::ProcessAddress addr, u64 data, u64 expected);
  264. /**
  265. * Writes a 128-bit unsigned integer to the given virtual address in
  266. * the current process' address space if and only if the address contains
  267. * the expected value. This operation is atomic.
  268. *
  269. * @param addr The virtual address to write the 128-bit unsigned integer to.
  270. * @param data The 128-bit unsigned integer to write to the given virtual address.
  271. * @param expected The 128-bit unsigned integer to check against the given virtual address.
  272. *
  273. * @post The memory range [addr, sizeof(data)) contains the given data value.
  274. */
  275. bool WriteExclusive128(Common::ProcessAddress addr, u128 data, u128 expected);
  276. /**
  277. * Reads a null-terminated string from the given virtual address.
  278. * This function will continually read characters until either:
  279. *
  280. * - A null character ('\0') is reached.
  281. * - max_length characters have been read.
  282. *
  283. * @note The final null-terminating character (if found) is not included
  284. * in the returned string.
  285. *
  286. * @param vaddr The address to begin reading the string from.
  287. * @param max_length The maximum length of the string to read in characters.
  288. *
  289. * @returns The read string.
  290. */
  291. std::string ReadCString(Common::ProcessAddress vaddr, std::size_t max_length);
  292. /**
  293. * Reads a contiguous block of bytes from the current process' address space.
  294. *
  295. * @param src_addr The virtual address to begin reading from.
  296. * @param dest_buffer The buffer to place the read bytes into.
  297. * @param size The amount of data to read, in bytes.
  298. *
  299. * @note If a size of 0 is specified, then this function reads nothing and
  300. * no attempts to access memory are made at all.
  301. *
  302. * @pre dest_buffer must be at least size bytes in length, otherwise a
  303. * buffer overrun will occur.
  304. *
  305. * @post The range [dest_buffer, size) contains the read bytes from the
  306. * current process' address space.
  307. */
  308. bool ReadBlock(Common::ProcessAddress src_addr, void* dest_buffer, std::size_t size);
  309. /**
  310. * Reads a contiguous block of bytes from the current process' address space.
  311. * This unsafe version does not trigger GPU flushing.
  312. *
  313. * @param src_addr The virtual address to begin reading from.
  314. * @param dest_buffer The buffer to place the read bytes into.
  315. * @param size The amount of data to read, in bytes.
  316. *
  317. * @note If a size of 0 is specified, then this function reads nothing and
  318. * no attempts to access memory are made at all.
  319. *
  320. * @pre dest_buffer must be at least size bytes in length, otherwise a
  321. * buffer overrun will occur.
  322. *
  323. * @post The range [dest_buffer, size) contains the read bytes from the
  324. * current process' address space.
  325. */
  326. bool ReadBlockUnsafe(Common::ProcessAddress src_addr, void* dest_buffer, std::size_t size);
  327. const u8* GetSpan(const VAddr src_addr, const std::size_t size) const;
  328. u8* GetSpan(const VAddr src_addr, const std::size_t size);
  329. /**
  330. * Writes a range of bytes into the current process' address space at the specified
  331. * virtual address.
  332. *
  333. * @param dest_addr The destination virtual address to begin writing the data at.
  334. * @param src_buffer The data to write into the current process' address space.
  335. * @param size The size of the data to write, in bytes.
  336. *
  337. * @post The address range [dest_addr, size) in the current process' address space
  338. * contains the data that was within src_buffer.
  339. *
  340. * @post If an attempt is made to write into an unmapped region of memory, the writes
  341. * will be ignored and an error will be logged.
  342. *
  343. * @post If a write is performed into a region of memory that is considered cached
  344. * rasterizer memory, will cause the currently active rasterizer to be notified
  345. * and will mark that region as invalidated to caches that the active
  346. * graphics backend may be maintaining over the course of execution.
  347. */
  348. bool WriteBlock(Common::ProcessAddress dest_addr, const void* src_buffer, std::size_t size);
  349. /**
  350. * Writes a range of bytes into the current process' address space at the specified
  351. * virtual address.
  352. * This unsafe version does not invalidate GPU Memory.
  353. *
  354. * @param dest_addr The destination virtual address to begin writing the data at.
  355. * @param src_buffer The data to write into the current process' address space.
  356. * @param size The size of the data to write, in bytes.
  357. *
  358. * @post The address range [dest_addr, size) in the current process' address space
  359. * contains the data that was within src_buffer.
  360. *
  361. * @post If an attempt is made to write into an unmapped region of memory, the writes
  362. * will be ignored and an error will be logged.
  363. *
  364. */
  365. bool WriteBlockUnsafe(Common::ProcessAddress dest_addr, const void* src_buffer,
  366. std::size_t size);
  367. /**
  368. * Copies data within a process' address space to another location within the
  369. * same address space.
  370. *
  371. * @param dest_addr The destination virtual address to begin copying the data into.
  372. * @param src_addr The source virtual address to begin copying the data from.
  373. * @param size The size of the data to copy, in bytes.
  374. *
  375. * @post The range [dest_addr, size) within the process' address space contains the
  376. * same data within the range [src_addr, size).
  377. */
  378. bool CopyBlock(Common::ProcessAddress dest_addr, Common::ProcessAddress src_addr,
  379. std::size_t size);
  380. /**
  381. * Zeros a range of bytes within the current process' address space at the specified
  382. * virtual address.
  383. *
  384. * @param dest_addr The destination virtual address to zero the data from.
  385. * @param size The size of the range to zero out, in bytes.
  386. *
  387. * @post The range [dest_addr, size) within the process' address space contains the
  388. * value 0.
  389. */
  390. bool ZeroBlock(Common::ProcessAddress dest_addr, std::size_t size);
  391. /**
  392. * Invalidates a range of bytes within the current process' address space at the specified
  393. * virtual address.
  394. *
  395. * @param dest_addr The destination virtual address to invalidate the data from.
  396. * @param size The size of the range to invalidate, in bytes.
  397. *
  398. */
  399. Result InvalidateDataCache(Common::ProcessAddress dest_addr, std::size_t size);
  400. /**
  401. * Stores a range of bytes within the current process' address space at the specified
  402. * virtual address.
  403. *
  404. * @param dest_addr The destination virtual address to store the data from.
  405. * @param size The size of the range to store, in bytes.
  406. *
  407. */
  408. Result StoreDataCache(Common::ProcessAddress dest_addr, std::size_t size);
  409. /**
  410. * Flushes a range of bytes within the current process' address space at the specified
  411. * virtual address.
  412. *
  413. * @param dest_addr The destination virtual address to flush the data from.
  414. * @param size The size of the range to flush, in bytes.
  415. *
  416. */
  417. Result FlushDataCache(Common::ProcessAddress dest_addr, std::size_t size);
  418. /**
  419. * Marks each page within the specified address range as cached or uncached.
  420. *
  421. * @param vaddr The virtual address indicating the start of the address range.
  422. * @param size The size of the address range in bytes.
  423. * @param cached Whether or not any pages within the address range should be
  424. * marked as cached or uncached.
  425. */
  426. void RasterizerMarkRegionCached(Common::ProcessAddress vaddr, u64 size, bool cached);
  427. /**
  428. * Marks each page within the specified address range as debug or non-debug.
  429. * Debug addresses are not accessible from fastmem pointers.
  430. *
  431. * @param vaddr The virtual address indicating the start of the address range.
  432. * @param size The size of the address range in bytes.
  433. * @param debug Whether or not any pages within the address range should be
  434. * marked as debug or non-debug.
  435. */
  436. void MarkRegionDebug(Common::ProcessAddress vaddr, u64 size, bool debug);
  437. void SetGPUDirtyManagers(std::span<Core::GPUDirtyMemoryManager> managers);
  438. bool InvalidateNCE(Common::ProcessAddress vaddr, size_t size);
  439. bool InvalidateSeparateHeap(void* fault_address);
  440. private:
  441. Core::System& system;
  442. struct Impl;
  443. std::unique_ptr<Impl> impl;
  444. };
  445. template <typename T, GuestMemoryFlags FLAGS>
  446. using CpuGuestMemory = GuestMemory<Core::Memory::Memory, T, FLAGS>;
  447. template <typename T, GuestMemoryFlags FLAGS>
  448. using CpuGuestMemoryScoped = GuestMemoryScoped<Core::Memory::Memory, T, FLAGS>;
  449. } // namespace Core::Memory