memory.h 19 KB

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