memory.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 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 physical 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 memory Physical buffer with the memory backing the mapping. Must be of length
  61. * at least `size + offset`.
  62. * @param offset The offset within the physical memory. Must be page-aligned.
  63. */
  64. void MapMemoryRegion(Common::PageTable& page_table, VAddr base, u64 size,
  65. Kernel::PhysicalMemory& memory, VAddr offset);
  66. /**
  67. * Maps an allocated buffer onto a region of the emulated process address space.
  68. *
  69. * @param page_table The page table of the emulated process.
  70. * @param base The address to start mapping at. Must be page-aligned.
  71. * @param size The amount of bytes to map. Must be page-aligned.
  72. * @param target Buffer with the memory backing the mapping. Must be of length at least
  73. * `size`.
  74. */
  75. void MapMemoryRegion(Common::PageTable& page_table, VAddr base, u64 size, u8* target);
  76. /**
  77. * Maps a region of the emulated process address space as a IO region.
  78. *
  79. * @param page_table The page table of the emulated process.
  80. * @param base The address to start mapping at. Must be page-aligned.
  81. * @param size The amount of bytes to map. Must be page-aligned.
  82. * @param mmio_handler The handler that backs the mapping.
  83. */
  84. void MapIoRegion(Common::PageTable& page_table, VAddr base, u64 size,
  85. Common::MemoryHookPointer mmio_handler);
  86. /**
  87. * Unmaps a region of the emulated process address space.
  88. *
  89. * @param page_table The page table of the emulated process.
  90. * @param base The address to begin unmapping at.
  91. * @param size The amount of bytes to unmap.
  92. */
  93. void UnmapRegion(Common::PageTable& page_table, VAddr base, u64 size);
  94. /**
  95. * Adds a memory hook to intercept reads and writes to given region of memory.
  96. *
  97. * @param page_table The page table of the emulated process
  98. * @param base The starting address to apply the hook to.
  99. * @param size The size of the memory region to apply the hook to, in bytes.
  100. * @param hook The hook to apply to the region of memory.
  101. */
  102. void AddDebugHook(Common::PageTable& page_table, VAddr base, u64 size,
  103. Common::MemoryHookPointer hook);
  104. /**
  105. * Removes a memory hook from a given range of memory.
  106. *
  107. * @param page_table The page table of the emulated process.
  108. * @param base The starting address to remove the hook from.
  109. * @param size The size of the memory region to remove the hook from, in bytes.
  110. * @param hook The hook to remove from the specified region of memory.
  111. */
  112. void RemoveDebugHook(Common::PageTable& page_table, VAddr base, u64 size,
  113. Common::MemoryHookPointer hook);
  114. /**
  115. * Checks whether or not the supplied address is a valid virtual
  116. * address for the given process.
  117. *
  118. * @param process The emulated process to check the address against.
  119. * @param vaddr The virtual address to check the validity of.
  120. *
  121. * @returns True if the given virtual address is valid, false otherwise.
  122. */
  123. bool IsValidVirtualAddress(const Kernel::Process& process, VAddr vaddr) const;
  124. /**
  125. * Checks whether or not the supplied address is a valid virtual
  126. * address for the current process.
  127. *
  128. * @param vaddr The virtual address to check the validity of.
  129. *
  130. * @returns True if the given virtual address is valid, false otherwise.
  131. */
  132. bool IsValidVirtualAddress(VAddr vaddr) const;
  133. /**
  134. * Gets a pointer to the given address.
  135. *
  136. * @param vaddr Virtual address to retrieve a pointer to.
  137. *
  138. * @returns The pointer to the given address, if the address is valid.
  139. * If the address is not valid, nullptr will be returned.
  140. */
  141. u8* GetPointer(VAddr vaddr);
  142. /**
  143. * Gets a pointer to the given address.
  144. *
  145. * @param vaddr Virtual address to retrieve a pointer to.
  146. *
  147. * @returns The pointer to the given address, if the address is valid.
  148. * If the address is not valid, nullptr will be returned.
  149. */
  150. const u8* GetPointer(VAddr vaddr) const;
  151. /**
  152. * Reads an 8-bit unsigned value from the current process' address space
  153. * at the given virtual address.
  154. *
  155. * @param addr The virtual address to read the 8-bit value from.
  156. *
  157. * @returns the read 8-bit unsigned value.
  158. */
  159. u8 Read8(VAddr addr);
  160. /**
  161. * Reads a 16-bit unsigned value from the current process' address space
  162. * at the given virtual address.
  163. *
  164. * @param addr The virtual address to read the 16-bit value from.
  165. *
  166. * @returns the read 16-bit unsigned value.
  167. */
  168. u16 Read16(VAddr addr);
  169. /**
  170. * Reads a 32-bit unsigned value from the current process' address space
  171. * at the given virtual address.
  172. *
  173. * @param addr The virtual address to read the 32-bit value from.
  174. *
  175. * @returns the read 32-bit unsigned value.
  176. */
  177. u32 Read32(VAddr addr);
  178. /**
  179. * Reads a 64-bit unsigned value from the current process' address space
  180. * at the given virtual address.
  181. *
  182. * @param addr The virtual address to read the 64-bit value from.
  183. *
  184. * @returns the read 64-bit value.
  185. */
  186. u64 Read64(VAddr addr);
  187. /**
  188. * Writes an 8-bit unsigned integer to the given virtual address in
  189. * the current process' address space.
  190. *
  191. * @param addr The virtual address to write the 8-bit unsigned integer to.
  192. * @param data The 8-bit unsigned integer to write to the given virtual address.
  193. *
  194. * @post The memory at the given virtual address contains the specified data value.
  195. */
  196. void Write8(VAddr addr, u8 data);
  197. /**
  198. * Writes a 16-bit unsigned integer to the given virtual address in
  199. * the current process' address space.
  200. *
  201. * @param addr The virtual address to write the 16-bit unsigned integer to.
  202. * @param data The 16-bit unsigned integer to write to the given virtual address.
  203. *
  204. * @post The memory range [addr, sizeof(data)) contains the given data value.
  205. */
  206. void Write16(VAddr addr, u16 data);
  207. /**
  208. * Writes a 32-bit unsigned integer to the given virtual address in
  209. * the current process' address space.
  210. *
  211. * @param addr The virtual address to write the 32-bit unsigned integer to.
  212. * @param data The 32-bit unsigned integer to write to the given virtual address.
  213. *
  214. * @post The memory range [addr, sizeof(data)) contains the given data value.
  215. */
  216. void Write32(VAddr addr, u32 data);
  217. /**
  218. * Writes a 64-bit unsigned integer to the given virtual address in
  219. * the current process' address space.
  220. *
  221. * @param addr The virtual address to write the 64-bit unsigned integer to.
  222. * @param data The 64-bit unsigned integer to write to the given virtual address.
  223. *
  224. * @post The memory range [addr, sizeof(data)) contains the given data value.
  225. */
  226. void Write64(VAddr addr, u64 data);
  227. /**
  228. * Reads a null-terminated string from the given virtual address.
  229. * This function will continually read characters until either:
  230. *
  231. * - A null character ('\0') is reached.
  232. * - max_length characters have been read.
  233. *
  234. * @note The final null-terminating character (if found) is not included
  235. * in the returned string.
  236. *
  237. * @param vaddr The address to begin reading the string from.
  238. * @param max_length The maximum length of the string to read in characters.
  239. *
  240. * @returns The read string.
  241. */
  242. std::string ReadCString(VAddr vaddr, std::size_t max_length);
  243. /**
  244. * Reads a contiguous block of bytes from a specified process' address space.
  245. *
  246. * @param process The process to read the data from.
  247. * @param src_addr The virtual address to begin reading from.
  248. * @param dest_buffer The buffer to place the read bytes into.
  249. * @param size The amount of data to read, in bytes.
  250. *
  251. * @note If a size of 0 is specified, then this function reads nothing and
  252. * no attempts to access memory are made at all.
  253. *
  254. * @pre dest_buffer must be at least size bytes in length, otherwise a
  255. * buffer overrun will occur.
  256. *
  257. * @post The range [dest_buffer, size) contains the read bytes from the
  258. * process' address space.
  259. */
  260. void ReadBlock(const Kernel::Process& process, VAddr src_addr, void* dest_buffer,
  261. std::size_t size);
  262. /**
  263. * Reads a contiguous block of bytes from the current process' address space.
  264. *
  265. * @param src_addr The virtual address to begin reading from.
  266. * @param dest_buffer The buffer to place the read bytes into.
  267. * @param size The amount of data to read, in bytes.
  268. *
  269. * @note If a size of 0 is specified, then this function reads nothing and
  270. * no attempts to access memory are made at all.
  271. *
  272. * @pre dest_buffer must be at least size bytes in length, otherwise a
  273. * buffer overrun will occur.
  274. *
  275. * @post The range [dest_buffer, size) contains the read bytes from the
  276. * current process' address space.
  277. */
  278. void ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size);
  279. /**
  280. * Writes a range of bytes into a given process' address space at the specified
  281. * virtual address.
  282. *
  283. * @param process The process to write data into the address space of.
  284. * @param dest_addr The destination virtual address to begin writing the data at.
  285. * @param src_buffer The data to write into the process' address space.
  286. * @param size The size of the data to write, in bytes.
  287. *
  288. * @post The address range [dest_addr, size) in the process' address space
  289. * contains the data that was within src_buffer.
  290. *
  291. * @post If an attempt is made to write into an unmapped region of memory, the writes
  292. * will be ignored and an error will be logged.
  293. *
  294. * @post If a write is performed into a region of memory that is considered cached
  295. * rasterizer memory, will cause the currently active rasterizer to be notified
  296. * and will mark that region as invalidated to caches that the active
  297. * graphics backend may be maintaining over the course of execution.
  298. */
  299. void WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer,
  300. std::size_t size);
  301. /**
  302. * Writes a range of bytes into the current process' address space at the specified
  303. * virtual address.
  304. *
  305. * @param dest_addr The destination virtual address to begin writing the data at.
  306. * @param src_buffer The data to write into the current process' address space.
  307. * @param size The size of the data to write, in bytes.
  308. *
  309. * @post The address range [dest_addr, size) in the current process' address space
  310. * contains the data that was within src_buffer.
  311. *
  312. * @post If an attempt is made to write into an unmapped region of memory, the writes
  313. * will be ignored and an error will be logged.
  314. *
  315. * @post If a write is performed into a region of memory that is considered cached
  316. * rasterizer memory, will cause the currently active rasterizer to be notified
  317. * and will mark that region as invalidated to caches that the active
  318. * graphics backend may be maintaining over the course of execution.
  319. */
  320. void WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size);
  321. /**
  322. * Fills the specified address range within a process' address space with zeroes.
  323. *
  324. * @param process The process that will have a portion of its memory zeroed out.
  325. * @param dest_addr The starting virtual address of the range to zero out.
  326. * @param size The size of the address range to zero out, in bytes.
  327. *
  328. * @post The range [dest_addr, size) within the process' address space is
  329. * filled with zeroes.
  330. */
  331. void ZeroBlock(const Kernel::Process& process, VAddr dest_addr, std::size_t size);
  332. /**
  333. * Fills the specified address range within the current process' address space with zeroes.
  334. *
  335. * @param dest_addr The starting virtual address of the range to zero out.
  336. * @param size The size of the address range to zero out, in bytes.
  337. *
  338. * @post The range [dest_addr, size) within the current process' address space is
  339. * filled with zeroes.
  340. */
  341. void ZeroBlock(VAddr dest_addr, std::size_t size);
  342. /**
  343. * Copies data within a process' address space to another location within the
  344. * same address space.
  345. *
  346. * @param process The process that will have data copied within its address space.
  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(const Kernel::Process& process, VAddr dest_addr, VAddr src_addr,
  355. std::size_t size);
  356. /**
  357. * Copies data within the current process' address space to another location within the
  358. * same address space.
  359. *
  360. * @param dest_addr The destination virtual address to begin copying the data into.
  361. * @param src_addr The source virtual address to begin copying the data from.
  362. * @param size The size of the data to copy, in bytes.
  363. *
  364. * @post The range [dest_addr, size) within the current process' address space
  365. * contains the same data within the range [src_addr, size).
  366. */
  367. void CopyBlock(VAddr dest_addr, VAddr src_addr, std::size_t size);
  368. /**
  369. * Marks each page within the specified address range as cached or uncached.
  370. *
  371. * @param vaddr The virtual address indicating the start of the address range.
  372. * @param size The size of the address range in bytes.
  373. * @param cached Whether or not any pages within the address range should be
  374. * marked as cached or uncached.
  375. */
  376. void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached);
  377. private:
  378. struct Impl;
  379. std::unique_ptr<Impl> impl;
  380. };
  381. /// Determines if the given VAddr is a kernel address
  382. bool IsKernelVirtualAddress(VAddr vaddr);
  383. } // namespace Memory