memory.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. #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 : VAddr {
  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, VAddr base, u64 size, PAddr target);
  63. /**
  64. * Unmaps a region of the emulated process address space.
  65. *
  66. * @param page_table The page table of the emulated process.
  67. * @param base The address to begin unmapping at.
  68. * @param size The amount of bytes to unmap.
  69. */
  70. void UnmapRegion(Common::PageTable& page_table, VAddr base, u64 size);
  71. /**
  72. * Checks whether or not the supplied address is a valid virtual
  73. * address for the current process.
  74. *
  75. * @param vaddr The virtual address to check the validity of.
  76. *
  77. * @returns True if the given virtual address is valid, false otherwise.
  78. */
  79. [[nodiscard]] bool IsValidVirtualAddress(VAddr vaddr) const;
  80. /**
  81. * Checks whether or not the supplied range of addresses are all valid
  82. * virtual addresses for the current process.
  83. *
  84. * @param base The address to begin checking.
  85. * @param size The amount of bytes to check.
  86. *
  87. * @returns True if all bytes in the given range are valid, false otherwise.
  88. */
  89. [[nodiscard]] bool IsValidVirtualAddressRange(VAddr base, u64 size) const;
  90. /**
  91. * Gets a pointer to the given address.
  92. *
  93. * @param vaddr Virtual address to retrieve a pointer to.
  94. *
  95. * @returns The pointer to the given address, if the address is valid.
  96. * If the address is not valid, nullptr will be returned.
  97. */
  98. u8* GetPointer(VAddr vaddr);
  99. u8* GetPointerSilent(VAddr vaddr);
  100. template <typename T>
  101. T* GetPointer(VAddr vaddr) {
  102. return reinterpret_cast<T*>(GetPointer(vaddr));
  103. }
  104. /**
  105. * Gets a pointer to the given address.
  106. *
  107. * @param vaddr Virtual address to retrieve a pointer to.
  108. *
  109. * @returns The pointer to the given address, if the address is valid.
  110. * If the address is not valid, nullptr will be returned.
  111. */
  112. [[nodiscard]] const u8* GetPointer(VAddr vaddr) const;
  113. template <typename T>
  114. const T* GetPointer(VAddr vaddr) const {
  115. return reinterpret_cast<T*>(GetPointer(vaddr));
  116. }
  117. /**
  118. * Reads an 8-bit unsigned value from the current process' address space
  119. * at the given virtual address.
  120. *
  121. * @param addr The virtual address to read the 8-bit value from.
  122. *
  123. * @returns the read 8-bit unsigned value.
  124. */
  125. u8 Read8(VAddr addr);
  126. /**
  127. * Reads a 16-bit unsigned value from the current process' address space
  128. * at the given virtual address.
  129. *
  130. * @param addr The virtual address to read the 16-bit value from.
  131. *
  132. * @returns the read 16-bit unsigned value.
  133. */
  134. u16 Read16(VAddr addr);
  135. /**
  136. * Reads a 32-bit unsigned value from the current process' address space
  137. * at the given virtual address.
  138. *
  139. * @param addr The virtual address to read the 32-bit value from.
  140. *
  141. * @returns the read 32-bit unsigned value.
  142. */
  143. u32 Read32(VAddr addr);
  144. /**
  145. * Reads a 64-bit unsigned value from the current process' address space
  146. * at the given virtual address.
  147. *
  148. * @param addr The virtual address to read the 64-bit value from.
  149. *
  150. * @returns the read 64-bit value.
  151. */
  152. u64 Read64(VAddr addr);
  153. /**
  154. * Writes an 8-bit unsigned integer to the given virtual address in
  155. * the current process' address space.
  156. *
  157. * @param addr The virtual address to write the 8-bit unsigned integer to.
  158. * @param data The 8-bit unsigned integer to write to the given virtual address.
  159. *
  160. * @post The memory at the given virtual address contains the specified data value.
  161. */
  162. void Write8(VAddr addr, u8 data);
  163. /**
  164. * Writes a 16-bit unsigned integer to the given virtual address in
  165. * the current process' address space.
  166. *
  167. * @param addr The virtual address to write the 16-bit unsigned integer to.
  168. * @param data The 16-bit unsigned integer to write to the given virtual address.
  169. *
  170. * @post The memory range [addr, sizeof(data)) contains the given data value.
  171. */
  172. void Write16(VAddr addr, u16 data);
  173. /**
  174. * Writes a 32-bit unsigned integer to the given virtual address in
  175. * the current process' address space.
  176. *
  177. * @param addr The virtual address to write the 32-bit unsigned integer to.
  178. * @param data The 32-bit unsigned integer to write to the given virtual address.
  179. *
  180. * @post The memory range [addr, sizeof(data)) contains the given data value.
  181. */
  182. void Write32(VAddr addr, u32 data);
  183. /**
  184. * Writes a 64-bit unsigned integer to the given virtual address in
  185. * the current process' address space.
  186. *
  187. * @param addr The virtual address to write the 64-bit unsigned integer to.
  188. * @param data The 64-bit unsigned integer to write to the given virtual address.
  189. *
  190. * @post The memory range [addr, sizeof(data)) contains the given data value.
  191. */
  192. void Write64(VAddr addr, u64 data);
  193. /**
  194. * Writes a 8-bit unsigned integer to the given virtual address in
  195. * the current process' address space if and only if the address contains
  196. * the expected value. This operation is atomic.
  197. *
  198. * @param addr The virtual address to write the 8-bit unsigned integer to.
  199. * @param data The 8-bit unsigned integer to write to the given virtual address.
  200. * @param expected The 8-bit unsigned integer to check against the given virtual address.
  201. *
  202. * @post The memory range [addr, sizeof(data)) contains the given data value.
  203. */
  204. bool WriteExclusive8(VAddr addr, u8 data, u8 expected);
  205. /**
  206. * Writes a 16-bit unsigned integer to the given virtual address in
  207. * the current process' address space if and only if the address contains
  208. * the expected value. This operation is atomic.
  209. *
  210. * @param addr The virtual address to write the 16-bit unsigned integer to.
  211. * @param data The 16-bit unsigned integer to write to the given virtual address.
  212. * @param expected The 16-bit unsigned integer to check against the given virtual address.
  213. *
  214. * @post The memory range [addr, sizeof(data)) contains the given data value.
  215. */
  216. bool WriteExclusive16(VAddr addr, u16 data, u16 expected);
  217. /**
  218. * Writes a 32-bit unsigned integer to the given virtual address in
  219. * the current process' address space if and only if the address contains
  220. * the expected value. This operation is atomic.
  221. *
  222. * @param addr The virtual address to write the 32-bit unsigned integer to.
  223. * @param data The 32-bit unsigned integer to write to the given virtual address.
  224. * @param expected The 32-bit unsigned integer to check against the given virtual address.
  225. *
  226. * @post The memory range [addr, sizeof(data)) contains the given data value.
  227. */
  228. bool WriteExclusive32(VAddr addr, u32 data, u32 expected);
  229. /**
  230. * Writes a 64-bit unsigned integer to the given virtual address in
  231. * the current process' address space if and only if the address contains
  232. * the expected value. This operation is atomic.
  233. *
  234. * @param addr The virtual address to write the 64-bit unsigned integer to.
  235. * @param data The 64-bit unsigned integer to write to the given virtual address.
  236. * @param expected The 64-bit unsigned integer to check against the given virtual address.
  237. *
  238. * @post The memory range [addr, sizeof(data)) contains the given data value.
  239. */
  240. bool WriteExclusive64(VAddr addr, u64 data, u64 expected);
  241. /**
  242. * Writes a 128-bit unsigned integer to the given virtual address in
  243. * the current process' address space if and only if the address contains
  244. * the expected value. This operation is atomic.
  245. *
  246. * @param addr The virtual address to write the 128-bit unsigned integer to.
  247. * @param data The 128-bit unsigned integer to write to the given virtual address.
  248. * @param expected The 128-bit unsigned integer to check against the given virtual address.
  249. *
  250. * @post The memory range [addr, sizeof(data)) contains the given data value.
  251. */
  252. bool WriteExclusive128(VAddr addr, u128 data, u128 expected);
  253. /**
  254. * Reads a null-terminated string from the given virtual address.
  255. * This function will continually read characters until either:
  256. *
  257. * - A null character ('\0') is reached.
  258. * - max_length characters have been read.
  259. *
  260. * @note The final null-terminating character (if found) is not included
  261. * in the returned string.
  262. *
  263. * @param vaddr The address to begin reading the string from.
  264. * @param max_length The maximum length of the string to read in characters.
  265. *
  266. * @returns The read string.
  267. */
  268. std::string ReadCString(VAddr vaddr, std::size_t max_length);
  269. /**
  270. * Reads a contiguous block of bytes from a specified process' address space.
  271. *
  272. * @param process The process to read the data from.
  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. * process' address space.
  285. */
  286. void ReadBlock(const Kernel::KProcess& process, VAddr src_addr, void* dest_buffer,
  287. std::size_t size);
  288. /**
  289. * Reads a contiguous block of bytes from the current process' address space.
  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 ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size);
  305. /**
  306. * Reads a contiguous block of bytes from the current process' address space.
  307. * This unsafe version does not trigger GPU flushing.
  308. *
  309. * @param src_addr The virtual address to begin reading from.
  310. * @param dest_buffer The buffer to place the read bytes into.
  311. * @param size The amount of data to read, in bytes.
  312. *
  313. * @note If a size of 0 is specified, then this function reads nothing and
  314. * no attempts to access memory are made at all.
  315. *
  316. * @pre dest_buffer must be at least size bytes in length, otherwise a
  317. * buffer overrun will occur.
  318. *
  319. * @post The range [dest_buffer, size) contains the read bytes from the
  320. * current process' address space.
  321. */
  322. void ReadBlockUnsafe(VAddr src_addr, void* dest_buffer, std::size_t size);
  323. /**
  324. * Writes a range of bytes into a given process' address space at the specified
  325. * virtual address.
  326. *
  327. * @param process The process to write data into the address space of.
  328. * @param dest_addr The destination virtual address to begin writing the data at.
  329. * @param src_buffer The data to write into the process' address space.
  330. * @param size The size of the data to write, in bytes.
  331. *
  332. * @post The address range [dest_addr, size) in the process' address space
  333. * contains the data that was within src_buffer.
  334. *
  335. * @post If an attempt is made to write into an unmapped region of memory, the writes
  336. * will be ignored and an error will be logged.
  337. *
  338. * @post If a write is performed into a region of memory that is considered cached
  339. * rasterizer memory, will cause the currently active rasterizer to be notified
  340. * and will mark that region as invalidated to caches that the active
  341. * graphics backend may be maintaining over the course of execution.
  342. */
  343. void WriteBlock(const Kernel::KProcess& process, VAddr dest_addr, const void* src_buffer,
  344. std::size_t size);
  345. /**
  346. * Writes a range of bytes into the current process' address space at the specified
  347. * virtual address.
  348. *
  349. * @param dest_addr The destination virtual address to begin writing the data at.
  350. * @param src_buffer The data to write into the current process' address space.
  351. * @param size The size of the data to write, in bytes.
  352. *
  353. * @post The address range [dest_addr, size) in the current process' address space
  354. * contains the data that was within src_buffer.
  355. *
  356. * @post If an attempt is made to write into an unmapped region of memory, the writes
  357. * will be ignored and an error will be logged.
  358. *
  359. * @post If a write is performed into a region of memory that is considered cached
  360. * rasterizer memory, will cause the currently active rasterizer to be notified
  361. * and will mark that region as invalidated to caches that the active
  362. * graphics backend may be maintaining over the course of execution.
  363. */
  364. void WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size);
  365. /**
  366. * Writes a range of bytes into the current process' address space at the specified
  367. * virtual address.
  368. * This unsafe version does not invalidate GPU Memory.
  369. *
  370. * @param dest_addr The destination virtual address to begin writing the data at.
  371. * @param src_buffer The data to write into the current process' address space.
  372. * @param size The size of the data to write, in bytes.
  373. *
  374. * @post The address range [dest_addr, size) in the current process' address space
  375. * contains the data that was within src_buffer.
  376. *
  377. * @post If an attempt is made to write into an unmapped region of memory, the writes
  378. * will be ignored and an error will be logged.
  379. *
  380. */
  381. void WriteBlockUnsafe(VAddr dest_addr, const void* src_buffer, std::size_t size);
  382. /**
  383. * Copies data within a process' address space to another location within the
  384. * same address space.
  385. *
  386. * @param process The process that will have data copied within its address space.
  387. * @param dest_addr The destination virtual address to begin copying the data into.
  388. * @param src_addr The source virtual address to begin copying the data from.
  389. * @param size The size of the data to copy, in bytes.
  390. *
  391. * @post The range [dest_addr, size) within the process' address space contains the
  392. * same data within the range [src_addr, size).
  393. */
  394. void CopyBlock(const Kernel::KProcess& process, VAddr dest_addr, VAddr src_addr,
  395. std::size_t size);
  396. /**
  397. * Zeros a range of bytes within the current process' address space at the specified
  398. * virtual address.
  399. *
  400. * @param process The process that will have data zeroed within its address space.
  401. * @param dest_addr The destination virtual address to zero the data from.
  402. * @param size The size of the range to zero out, in bytes.
  403. *
  404. * @post The range [dest_addr, size) within the process' address space contains the
  405. * value 0.
  406. */
  407. void ZeroBlock(const Kernel::KProcess& process, VAddr dest_addr, std::size_t size);
  408. /**
  409. * Invalidates a range of bytes within the current process' address space at the specified
  410. * virtual address.
  411. *
  412. * @param process The process that will have data invalidated within its address space.
  413. * @param dest_addr The destination virtual address to invalidate the data from.
  414. * @param size The size of the range to invalidate, in bytes.
  415. *
  416. */
  417. Result InvalidateDataCache(const Kernel::KProcess& process, VAddr dest_addr, std::size_t size);
  418. /**
  419. * Stores a range of bytes within the current process' address space at the specified
  420. * virtual address.
  421. *
  422. * @param process The process that will have data stored within its address space.
  423. * @param dest_addr The destination virtual address to store the data from.
  424. * @param size The size of the range to store, in bytes.
  425. *
  426. */
  427. Result StoreDataCache(const Kernel::KProcess& process, VAddr dest_addr, std::size_t size);
  428. /**
  429. * Flushes a range of bytes within the current process' address space at the specified
  430. * virtual address.
  431. *
  432. * @param process The process that will have data flushed within its address space.
  433. * @param dest_addr The destination virtual address to flush the data from.
  434. * @param size The size of the range to flush, in bytes.
  435. *
  436. */
  437. Result FlushDataCache(const Kernel::KProcess& process, VAddr dest_addr, std::size_t size);
  438. /**
  439. * Marks each page within the specified address range as cached or uncached.
  440. *
  441. * @param vaddr The virtual address indicating the start of the address range.
  442. * @param size The size of the address range in bytes.
  443. * @param cached Whether or not any pages within the address range should be
  444. * marked as cached or uncached.
  445. */
  446. void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached);
  447. /**
  448. * Marks each page within the specified address range as debug or non-debug.
  449. * Debug addresses are not accessible from fastmem pointers.
  450. *
  451. * @param vaddr The virtual address indicating the start of the address range.
  452. * @param size The size of the address range in bytes.
  453. * @param debug Whether or not any pages within the address range should be
  454. * marked as debug or non-debug.
  455. */
  456. void MarkRegionDebug(VAddr vaddr, u64 size, bool debug);
  457. private:
  458. Core::System& system;
  459. struct Impl;
  460. std::unique_ptr<Impl> impl;
  461. };
  462. } // namespace Core::Memory