memory.h 18 KB

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