memory.h 18 KB

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