memory.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <memory>
  7. #include <string>
  8. #include "common/common_types.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 PAGE_BITS = 12;
  25. constexpr u64 PAGE_SIZE = 1ULL << PAGE_BITS;
  26. constexpr u64 PAGE_MASK = PAGE_SIZE - 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. /// Kernel Virtual Address Range
  34. KERNEL_REGION_VADDR = 0xFFFFFF8000000000,
  35. KERNEL_REGION_SIZE = 0x7FFFE00000,
  36. KERNEL_REGION_END = KERNEL_REGION_VADDR + KERNEL_REGION_SIZE,
  37. };
  38. /// Central class that handles all memory operations and state.
  39. class Memory {
  40. public:
  41. explicit Memory(Core::System& system);
  42. ~Memory();
  43. Memory(const Memory&) = delete;
  44. Memory& operator=(const Memory&) = delete;
  45. Memory(Memory&&) = default;
  46. Memory& operator=(Memory&&) = default;
  47. /**
  48. * Resets the state of the Memory system.
  49. */
  50. void Reset();
  51. /**
  52. * Changes the currently active page table to that of the given process instance.
  53. *
  54. * @param process The process to use the page table of.
  55. */
  56. void SetCurrentPageTable(Kernel::KProcess& process, u32 core_id);
  57. /**
  58. * Maps an allocated buffer onto a region of the emulated process address space.
  59. *
  60. * @param page_table The page table of the emulated process.
  61. * @param base The address to start mapping at. Must be page-aligned.
  62. * @param size The amount of bytes to map. Must be page-aligned.
  63. * @param target Buffer with the memory backing the mapping. Must be of length at least
  64. * `size`.
  65. */
  66. void MapMemoryRegion(Common::PageTable& page_table, VAddr base, u64 size, PAddr target);
  67. /**
  68. * Unmaps a region of the emulated process address space.
  69. *
  70. * @param page_table The page table of the emulated process.
  71. * @param base The address to begin unmapping at.
  72. * @param size The amount of bytes to unmap.
  73. */
  74. void UnmapRegion(Common::PageTable& page_table, VAddr base, u64 size);
  75. /**
  76. * Checks whether or not the supplied address is a valid virtual
  77. * address for the given process.
  78. *
  79. * @param process The emulated process to check the address against.
  80. * @param vaddr The virtual address to check the validity of.
  81. *
  82. * @returns True if the given virtual address is valid, false otherwise.
  83. */
  84. bool IsValidVirtualAddress(const Kernel::KProcess& process, VAddr vaddr) const;
  85. /**
  86. * Checks whether or not the supplied address is a valid virtual
  87. * address for the current process.
  88. *
  89. * @param vaddr The virtual address to check the validity of.
  90. *
  91. * @returns True if the given virtual address is valid, false otherwise.
  92. */
  93. bool IsValidVirtualAddress(VAddr vaddr) const;
  94. /**
  95. * Gets a pointer to the given address.
  96. *
  97. * @param vaddr Virtual address to retrieve a pointer to.
  98. *
  99. * @returns The pointer to the given address, if the address is valid.
  100. * If the address is not valid, nullptr will be returned.
  101. */
  102. u8* GetPointer(VAddr vaddr);
  103. template <typename T>
  104. T* GetPointer(VAddr vaddr) {
  105. return reinterpret_cast<T*>(GetPointer(vaddr));
  106. }
  107. /**
  108. * Gets a pointer to the given address.
  109. *
  110. * @param vaddr Virtual address to retrieve a pointer to.
  111. *
  112. * @returns The pointer to the given address, if the address is valid.
  113. * If the address is not valid, nullptr will be returned.
  114. */
  115. const u8* GetPointer(VAddr vaddr) const;
  116. template <typename T>
  117. const T* GetPointer(VAddr vaddr) const {
  118. return reinterpret_cast<T*>(GetPointer(vaddr));
  119. }
  120. /**
  121. * Reads an 8-bit unsigned value from the current process' address space
  122. * at the given virtual address.
  123. *
  124. * @param addr The virtual address to read the 8-bit value from.
  125. *
  126. * @returns the read 8-bit unsigned value.
  127. */
  128. u8 Read8(VAddr addr);
  129. /**
  130. * Reads a 16-bit unsigned value from the current process' address space
  131. * at the given virtual address.
  132. *
  133. * @param addr The virtual address to read the 16-bit value from.
  134. *
  135. * @returns the read 16-bit unsigned value.
  136. */
  137. u16 Read16(VAddr addr);
  138. /**
  139. * Reads a 32-bit unsigned value from the current process' address space
  140. * at the given virtual address.
  141. *
  142. * @param addr The virtual address to read the 32-bit value from.
  143. *
  144. * @returns the read 32-bit unsigned value.
  145. */
  146. u32 Read32(VAddr addr);
  147. /**
  148. * Reads a 64-bit unsigned value from the current process' address space
  149. * at the given virtual address.
  150. *
  151. * @param addr The virtual address to read the 64-bit value from.
  152. *
  153. * @returns the read 64-bit value.
  154. */
  155. u64 Read64(VAddr addr);
  156. /**
  157. * Writes an 8-bit unsigned integer to the given virtual address in
  158. * the current process' address space.
  159. *
  160. * @param addr The virtual address to write the 8-bit unsigned integer to.
  161. * @param data The 8-bit unsigned integer to write to the given virtual address.
  162. *
  163. * @post The memory at the given virtual address contains the specified data value.
  164. */
  165. void Write8(VAddr addr, u8 data);
  166. /**
  167. * Writes a 16-bit unsigned integer to the given virtual address in
  168. * the current process' address space.
  169. *
  170. * @param addr The virtual address to write the 16-bit unsigned integer to.
  171. * @param data The 16-bit unsigned integer to write to the given virtual address.
  172. *
  173. * @post The memory range [addr, sizeof(data)) contains the given data value.
  174. */
  175. void Write16(VAddr addr, u16 data);
  176. /**
  177. * Writes a 32-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 32-bit unsigned integer to.
  181. * @param data The 32-bit unsigned integer to write to the given virtual address.
  182. *
  183. * @post The memory range [addr, sizeof(data)) contains the given data value.
  184. */
  185. void Write32(VAddr addr, u32 data);
  186. /**
  187. * Writes a 64-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 64-bit unsigned integer to.
  191. * @param data The 64-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 Write64(VAddr addr, u64 data);
  196. /**
  197. * Writes a 8-bit unsigned integer to the given virtual address in
  198. * the current process' address space if and only if the address contains
  199. * the expected value. This operation is atomic.
  200. *
  201. * @param addr The virtual address to write the 8-bit unsigned integer to.
  202. * @param data The 8-bit unsigned integer to write to the given virtual address.
  203. * @param expected The 8-bit unsigned integer to check against the given virtual address.
  204. *
  205. * @post The memory range [addr, sizeof(data)) contains the given data value.
  206. */
  207. bool WriteExclusive8(VAddr addr, u8 data, u8 expected);
  208. /**
  209. * Writes a 16-bit unsigned integer to the given virtual address in
  210. * the current process' address space if and only if the address contains
  211. * the expected value. This operation is atomic.
  212. *
  213. * @param addr The virtual address to write the 16-bit unsigned integer to.
  214. * @param data The 16-bit unsigned integer to write to the given virtual address.
  215. * @param expected The 16-bit unsigned integer to check against the given virtual address.
  216. *
  217. * @post The memory range [addr, sizeof(data)) contains the given data value.
  218. */
  219. bool WriteExclusive16(VAddr addr, u16 data, u16 expected);
  220. /**
  221. * Writes a 32-bit unsigned integer to the given virtual address in
  222. * the current process' address space if and only if the address contains
  223. * the expected value. This operation is atomic.
  224. *
  225. * @param addr The virtual address to write the 32-bit unsigned integer to.
  226. * @param data The 32-bit unsigned integer to write to the given virtual address.
  227. * @param expected The 32-bit unsigned integer to check against the given virtual address.
  228. *
  229. * @post The memory range [addr, sizeof(data)) contains the given data value.
  230. */
  231. bool WriteExclusive32(VAddr addr, u32 data, u32 expected);
  232. /**
  233. * Writes a 64-bit unsigned integer to the given virtual address in
  234. * the current process' address space if and only if the address contains
  235. * the expected value. This operation is atomic.
  236. *
  237. * @param addr The virtual address to write the 64-bit unsigned integer to.
  238. * @param data The 64-bit unsigned integer to write to the given virtual address.
  239. * @param expected The 64-bit unsigned integer to check against the given virtual address.
  240. *
  241. * @post The memory range [addr, sizeof(data)) contains the given data value.
  242. */
  243. bool WriteExclusive64(VAddr addr, u64 data, u64 expected);
  244. /**
  245. * Writes a 128-bit unsigned integer to the given virtual address in
  246. * the current process' address space if and only if the address contains
  247. * the expected value. This operation is atomic.
  248. *
  249. * @param addr The virtual address to write the 128-bit unsigned integer to.
  250. * @param data The 128-bit unsigned integer to write to the given virtual address.
  251. * @param expected The 128-bit unsigned integer to check against the given virtual address.
  252. *
  253. * @post The memory range [addr, sizeof(data)) contains the given data value.
  254. */
  255. bool WriteExclusive128(VAddr addr, u128 data, u128 expected);
  256. /**
  257. * Reads a null-terminated string from the given virtual address.
  258. * This function will continually read characters until either:
  259. *
  260. * - A null character ('\0') is reached.
  261. * - max_length characters have been read.
  262. *
  263. * @note The final null-terminating character (if found) is not included
  264. * in the returned string.
  265. *
  266. * @param vaddr The address to begin reading the string from.
  267. * @param max_length The maximum length of the string to read in characters.
  268. *
  269. * @returns The read string.
  270. */
  271. std::string ReadCString(VAddr vaddr, std::size_t max_length);
  272. /**
  273. * Reads a contiguous block of bytes from a specified process' address space.
  274. *
  275. * @param process The process to read the data from.
  276. * @param src_addr The virtual address to begin reading from.
  277. * @param dest_buffer The buffer to place the read bytes into.
  278. * @param size The amount of data to read, in bytes.
  279. *
  280. * @note If a size of 0 is specified, then this function reads nothing and
  281. * no attempts to access memory are made at all.
  282. *
  283. * @pre dest_buffer must be at least size bytes in length, otherwise a
  284. * buffer overrun will occur.
  285. *
  286. * @post The range [dest_buffer, size) contains the read bytes from the
  287. * process' address space.
  288. */
  289. void ReadBlock(const Kernel::KProcess& process, VAddr src_addr, void* dest_buffer,
  290. std::size_t size);
  291. /**
  292. * Reads a contiguous block of bytes from a specified process' address space.
  293. * This unsafe version does not trigger GPU flushing.
  294. *
  295. * @param process The process to read the data from.
  296. * @param src_addr The virtual address to begin reading from.
  297. * @param dest_buffer The buffer to place the read bytes into.
  298. * @param size The amount of data to read, in bytes.
  299. *
  300. * @note If a size of 0 is specified, then this function reads nothing and
  301. * no attempts to access memory are made at all.
  302. *
  303. * @pre dest_buffer must be at least size bytes in length, otherwise a
  304. * buffer overrun will occur.
  305. *
  306. * @post The range [dest_buffer, size) contains the read bytes from the
  307. * process' address space.
  308. */
  309. void ReadBlockUnsafe(const Kernel::KProcess& process, VAddr src_addr, void* dest_buffer,
  310. std::size_t size);
  311. /**
  312. * Reads a contiguous block of bytes from the current process' address space.
  313. *
  314. * @param src_addr The virtual address to begin reading from.
  315. * @param dest_buffer The buffer to place the read bytes into.
  316. * @param size The amount of data to read, in bytes.
  317. *
  318. * @note If a size of 0 is specified, then this function reads nothing and
  319. * no attempts to access memory are made at all.
  320. *
  321. * @pre dest_buffer must be at least size bytes in length, otherwise a
  322. * buffer overrun will occur.
  323. *
  324. * @post The range [dest_buffer, size) contains the read bytes from the
  325. * current process' address space.
  326. */
  327. void ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size);
  328. /**
  329. * Reads a contiguous block of bytes from the current process' address space.
  330. * This unsafe version does not trigger GPU flushing.
  331. *
  332. * @param src_addr The virtual address to begin reading from.
  333. * @param dest_buffer The buffer to place the read bytes into.
  334. * @param size The amount of data to read, in bytes.
  335. *
  336. * @note If a size of 0 is specified, then this function reads nothing and
  337. * no attempts to access memory are made at all.
  338. *
  339. * @pre dest_buffer must be at least size bytes in length, otherwise a
  340. * buffer overrun will occur.
  341. *
  342. * @post The range [dest_buffer, size) contains the read bytes from the
  343. * current process' address space.
  344. */
  345. void ReadBlockUnsafe(VAddr src_addr, void* dest_buffer, std::size_t size);
  346. /**
  347. * Writes a range of bytes into a given process' address space at the specified
  348. * virtual address.
  349. *
  350. * @param process The process to write data into the address space of.
  351. * @param dest_addr The destination virtual address to begin writing the data at.
  352. * @param src_buffer The data to write into the process' address space.
  353. * @param size The size of the data to write, in bytes.
  354. *
  355. * @post The address range [dest_addr, size) in the process' address space
  356. * contains the data that was within src_buffer.
  357. *
  358. * @post If an attempt is made to write into an unmapped region of memory, the writes
  359. * will be ignored and an error will be logged.
  360. *
  361. * @post If a write is performed into a region of memory that is considered cached
  362. * rasterizer memory, will cause the currently active rasterizer to be notified
  363. * and will mark that region as invalidated to caches that the active
  364. * graphics backend may be maintaining over the course of execution.
  365. */
  366. void WriteBlock(const Kernel::KProcess& process, VAddr dest_addr, const void* src_buffer,
  367. std::size_t size);
  368. /**
  369. * Writes a range of bytes into a given process' address space at the specified
  370. * virtual address.
  371. * This unsafe version does not invalidate GPU Memory.
  372. *
  373. * @param process The process to write data into the address space of.
  374. * @param dest_addr The destination virtual address to begin writing the data at.
  375. * @param src_buffer The data to write into the process' address space.
  376. * @param size The size of the data to write, in bytes.
  377. *
  378. * @post The address range [dest_addr, size) in the process' address space
  379. * contains the data that was within src_buffer.
  380. *
  381. * @post If an attempt is made to write into an unmapped region of memory, the writes
  382. * will be ignored and an error will be logged.
  383. *
  384. */
  385. void WriteBlockUnsafe(const Kernel::KProcess& process, VAddr dest_addr, const void* src_buffer,
  386. std::size_t size);
  387. /**
  388. * Writes a range of bytes into the current process' address space at the specified
  389. * virtual address.
  390. *
  391. * @param dest_addr The destination virtual address to begin writing the data at.
  392. * @param src_buffer The data to write into the current process' address space.
  393. * @param size The size of the data to write, in bytes.
  394. *
  395. * @post The address range [dest_addr, size) in the current process' address space
  396. * contains the data that was within src_buffer.
  397. *
  398. * @post If an attempt is made to write into an unmapped region of memory, the writes
  399. * will be ignored and an error will be logged.
  400. *
  401. * @post If a write is performed into a region of memory that is considered cached
  402. * rasterizer memory, will cause the currently active rasterizer to be notified
  403. * and will mark that region as invalidated to caches that the active
  404. * graphics backend may be maintaining over the course of execution.
  405. */
  406. void WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size);
  407. /**
  408. * Writes a range of bytes into the current process' address space at the specified
  409. * virtual address.
  410. * This unsafe version does not invalidate GPU Memory.
  411. *
  412. * @param dest_addr The destination virtual address to begin writing the data at.
  413. * @param src_buffer The data to write into the current process' address space.
  414. * @param size The size of the data to write, in bytes.
  415. *
  416. * @post The address range [dest_addr, size) in the current process' address space
  417. * contains the data that was within src_buffer.
  418. *
  419. * @post If an attempt is made to write into an unmapped region of memory, the writes
  420. * will be ignored and an error will be logged.
  421. *
  422. */
  423. void WriteBlockUnsafe(VAddr dest_addr, const void* src_buffer, std::size_t size);
  424. /**
  425. * Fills the specified address range within a process' address space with zeroes.
  426. *
  427. * @param process The process that will have a portion of its memory zeroed out.
  428. * @param dest_addr The starting virtual address of the range to zero out.
  429. * @param size The size of the address range to zero out, in bytes.
  430. *
  431. * @post The range [dest_addr, size) within the process' address space is
  432. * filled with zeroes.
  433. */
  434. void ZeroBlock(const Kernel::KProcess& process, VAddr dest_addr, std::size_t size);
  435. /**
  436. * Fills the specified address range within the current process' address space with zeroes.
  437. *
  438. * @param dest_addr The starting virtual address of the range to zero out.
  439. * @param size The size of the address range to zero out, in bytes.
  440. *
  441. * @post The range [dest_addr, size) within the current process' address space is
  442. * filled with zeroes.
  443. */
  444. void ZeroBlock(VAddr dest_addr, std::size_t size);
  445. /**
  446. * Copies data within a process' address space to another location within the
  447. * same address space.
  448. *
  449. * @param process The process that will have data copied within its address space.
  450. * @param dest_addr The destination virtual address to begin copying the data into.
  451. * @param src_addr The source virtual address to begin copying the data from.
  452. * @param size The size of the data to copy, in bytes.
  453. *
  454. * @post The range [dest_addr, size) within the process' address space contains the
  455. * same data within the range [src_addr, size).
  456. */
  457. void CopyBlock(const Kernel::KProcess& process, VAddr dest_addr, VAddr src_addr,
  458. std::size_t size);
  459. /**
  460. * Copies data within the current process' address space to another location within the
  461. * same address space.
  462. *
  463. * @param dest_addr The destination virtual address to begin copying the data into.
  464. * @param src_addr The source virtual address to begin copying the data from.
  465. * @param size The size of the data to copy, in bytes.
  466. *
  467. * @post The range [dest_addr, size) within the current process' address space
  468. * contains the same data within the range [src_addr, size).
  469. */
  470. void CopyBlock(VAddr dest_addr, VAddr src_addr, std::size_t size);
  471. /**
  472. * Marks each page within the specified address range as cached or uncached.
  473. *
  474. * @param vaddr The virtual address indicating the start of the address range.
  475. * @param size The size of the address range in bytes.
  476. * @param cached Whether or not any pages within the address range should be
  477. * marked as cached or uncached.
  478. */
  479. void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached);
  480. private:
  481. Core::System& system;
  482. struct Impl;
  483. std::unique_ptr<Impl> impl;
  484. };
  485. /// Determines if the given VAddr is a kernel address
  486. bool IsKernelVirtualAddress(VAddr vaddr);
  487. } // namespace Core::Memory