memory.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <array>
  6. #include <cstddef>
  7. #include <map>
  8. #include <string>
  9. #include <vector>
  10. #include <boost/optional.hpp>
  11. #include "common/common_types.h"
  12. #include "core/mmio.h"
  13. namespace Kernel {
  14. class Process;
  15. }
  16. namespace Memory {
  17. /**
  18. * Page size used by the ARM architecture. This is the smallest granularity with which memory can
  19. * be mapped.
  20. */
  21. const int PAGE_BITS = 12;
  22. const u64 PAGE_SIZE = 1 << PAGE_BITS;
  23. const u64 PAGE_MASK = PAGE_SIZE - 1;
  24. const size_t PAGE_TABLE_NUM_ENTRIES = 1ULL << (36 - PAGE_BITS);
  25. enum class PageType {
  26. /// Page is unmapped and should cause an access error.
  27. Unmapped,
  28. /// Page is mapped to regular memory. This is the only type you can get pointers to.
  29. Memory,
  30. /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and
  31. /// invalidation
  32. RasterizerCachedMemory,
  33. /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions.
  34. Special,
  35. /// Page is mapped to a I/O region, but also needs to check for rasterizer cache flushing and
  36. /// invalidation
  37. RasterizerCachedSpecial,
  38. };
  39. struct SpecialRegion {
  40. VAddr base;
  41. u64 size;
  42. MMIORegionPointer handler;
  43. };
  44. /**
  45. * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely
  46. * mimics the way a real CPU page table works, but instead is optimized for minimal decoding and
  47. * fetching requirements when accessing. In the usual case of an access to regular memory, it only
  48. * requires an indexed fetch and a check for NULL.
  49. */
  50. struct PageTable {
  51. /**
  52. * Array of memory pointers backing each page. An entry can only be non-null if the
  53. * corresponding entry in the `attributes` array is of type `Memory`.
  54. */
  55. std::array<u8*, PAGE_TABLE_NUM_ENTRIES> pointers;
  56. /**
  57. * Contains MMIO handlers that back memory regions whose entries in the `attribute` array is of
  58. * type `Special`.
  59. */
  60. std::vector<SpecialRegion> special_regions;
  61. /**
  62. * Array of fine grained page attributes. If it is set to any value other than `Memory`, then
  63. * the corresponding entry in `pointers` MUST be set to null.
  64. */
  65. std::array<PageType, PAGE_TABLE_NUM_ENTRIES> attributes;
  66. /**
  67. * Indicates the number of externally cached resources touching a page that should be
  68. * flushed before the memory is accessed
  69. */
  70. std::array<u8, PAGE_TABLE_NUM_ENTRIES> cached_res_count;
  71. };
  72. /// Physical memory regions as seen from the ARM11
  73. enum : PAddr {
  74. /// IO register area
  75. IO_AREA_PADDR = 0x10100000,
  76. IO_AREA_SIZE = 0x01000000, ///< IO area size (16MB)
  77. IO_AREA_PADDR_END = IO_AREA_PADDR + IO_AREA_SIZE,
  78. /// MPCore internal memory region
  79. MPCORE_RAM_PADDR = 0x17E00000,
  80. MPCORE_RAM_SIZE = 0x00002000, ///< MPCore internal memory size (8KB)
  81. MPCORE_RAM_PADDR_END = MPCORE_RAM_PADDR + MPCORE_RAM_SIZE,
  82. /// Video memory
  83. VRAM_PADDR = 0x18000000,
  84. VRAM_SIZE = 0x00600000, ///< VRAM size (6MB)
  85. VRAM_PADDR_END = VRAM_PADDR + VRAM_SIZE,
  86. /// New 3DS additional memory. Supposedly faster than regular FCRAM. Part of it can be used by
  87. /// applications and system modules if mapped via the ExHeader.
  88. N3DS_EXTRA_RAM_PADDR = 0x1F000000,
  89. N3DS_EXTRA_RAM_SIZE = 0x00400000, ///< New 3DS additional memory size (4MB)
  90. N3DS_EXTRA_RAM_PADDR_END = N3DS_EXTRA_RAM_PADDR + N3DS_EXTRA_RAM_SIZE,
  91. /// DSP memory
  92. DSP_RAM_PADDR = 0x1FF00000,
  93. DSP_RAM_SIZE = 0x00080000, ///< DSP memory size (512KB)
  94. DSP_RAM_PADDR_END = DSP_RAM_PADDR + DSP_RAM_SIZE,
  95. /// AXI WRAM
  96. AXI_WRAM_PADDR = 0x1FF80000,
  97. AXI_WRAM_SIZE = 0x00080000, ///< AXI WRAM size (512KB)
  98. AXI_WRAM_PADDR_END = AXI_WRAM_PADDR + AXI_WRAM_SIZE,
  99. /// Main FCRAM
  100. FCRAM_PADDR = 0x20000000,
  101. FCRAM_SIZE = 0x08000000, ///< FCRAM size on the Old 3DS (128MB)
  102. FCRAM_N3DS_SIZE = 0x10000000, ///< FCRAM size on the New 3DS (256MB)
  103. FCRAM_PADDR_END = FCRAM_PADDR + FCRAM_SIZE,
  104. FCRAM_N3DS_PADDR_END = FCRAM_PADDR + FCRAM_N3DS_SIZE,
  105. };
  106. /// Virtual user-space memory regions
  107. enum : VAddr {
  108. /// Where the application text, data and bss reside.
  109. PROCESS_IMAGE_VADDR = 0x08000000,
  110. PROCESS_IMAGE_MAX_SIZE = 0x08000000,
  111. PROCESS_IMAGE_VADDR_END = PROCESS_IMAGE_VADDR + PROCESS_IMAGE_MAX_SIZE,
  112. /// Area where IPC buffers are mapped onto.
  113. IPC_MAPPING_VADDR = 0x04000000,
  114. IPC_MAPPING_SIZE = 0x04000000,
  115. IPC_MAPPING_VADDR_END = IPC_MAPPING_VADDR + IPC_MAPPING_SIZE,
  116. /// Application heap (includes stack).
  117. HEAP_VADDR = 0x108000000,
  118. HEAP_SIZE = 0x18000000,
  119. HEAP_VADDR_END = HEAP_VADDR + HEAP_SIZE,
  120. /// Area where shared memory buffers are mapped onto.
  121. SHARED_MEMORY_VADDR = 0x10000000,
  122. SHARED_MEMORY_SIZE = 0x04000000,
  123. SHARED_MEMORY_VADDR_END = SHARED_MEMORY_VADDR + SHARED_MEMORY_SIZE,
  124. /// Maps 1:1 to an offset in FCRAM. Used for HW allocations that need to be linear in physical
  125. /// memory.
  126. LINEAR_HEAP_VADDR = 0x14000000,
  127. LINEAR_HEAP_SIZE = 0x08000000,
  128. LINEAR_HEAP_VADDR_END = LINEAR_HEAP_VADDR + LINEAR_HEAP_SIZE,
  129. /// Maps 1:1 to New 3DS additional memory
  130. N3DS_EXTRA_RAM_VADDR = 0x1E800000,
  131. N3DS_EXTRA_RAM_VADDR_END = N3DS_EXTRA_RAM_VADDR + N3DS_EXTRA_RAM_SIZE,
  132. /// Maps 1:1 to the IO register area.
  133. IO_AREA_VADDR = 0x1EC00000,
  134. IO_AREA_VADDR_END = IO_AREA_VADDR + IO_AREA_SIZE,
  135. /// Maps 1:1 to VRAM.
  136. VRAM_VADDR = 0x1F000000,
  137. VRAM_VADDR_END = VRAM_VADDR + VRAM_SIZE,
  138. /// Maps 1:1 to DSP memory.
  139. DSP_RAM_VADDR = 0x1FF00000,
  140. DSP_RAM_VADDR_END = DSP_RAM_VADDR + DSP_RAM_SIZE,
  141. /// Read-only page containing kernel and system configuration values.
  142. CONFIG_MEMORY_VADDR = 0x1FF80000,
  143. CONFIG_MEMORY_SIZE = 0x00001000,
  144. CONFIG_MEMORY_VADDR_END = CONFIG_MEMORY_VADDR + CONFIG_MEMORY_SIZE,
  145. /// Usually read-only page containing mostly values read from hardware.
  146. SHARED_PAGE_VADDR = 0x1FF81000,
  147. SHARED_PAGE_SIZE = 0x00001000,
  148. SHARED_PAGE_VADDR_END = SHARED_PAGE_VADDR + SHARED_PAGE_SIZE,
  149. /// Area where TLS (Thread-Local Storage) buffers are allocated.
  150. TLS_AREA_VADDR = 0x1FF82000,
  151. TLS_ENTRY_SIZE = 0x200,
  152. /// Equivalent to LINEAR_HEAP_VADDR, but expanded to cover the extra memory in the New 3DS.
  153. NEW_LINEAR_HEAP_VADDR = 0x30000000,
  154. NEW_LINEAR_HEAP_SIZE = 0x10000000,
  155. NEW_LINEAR_HEAP_VADDR_END = NEW_LINEAR_HEAP_VADDR + NEW_LINEAR_HEAP_SIZE,
  156. };
  157. /// Currently active page table
  158. void SetCurrentPageTable(PageTable* page_table);
  159. PageTable* GetCurrentPageTable();
  160. /// Determines if the given VAddr is valid for the specified process.
  161. bool IsValidVirtualAddress(const Kernel::Process& process, const VAddr vaddr);
  162. bool IsValidVirtualAddress(const VAddr addr);
  163. bool IsValidPhysicalAddress(const PAddr addr);
  164. u8 Read8(VAddr addr);
  165. u16 Read16(VAddr addr);
  166. u32 Read32(VAddr addr);
  167. u64 Read64(VAddr addr);
  168. void Write8(VAddr addr, u8 data);
  169. void Write16(VAddr addr, u16 data);
  170. void Write32(VAddr addr, u32 data);
  171. void Write64(VAddr addr, u64 data);
  172. void ReadBlock(const Kernel::Process& process, const VAddr src_addr, void* dest_buffer,
  173. size_t size);
  174. void ReadBlock(const VAddr src_addr, void* dest_buffer, size_t size);
  175. void WriteBlock(const Kernel::Process& process, const VAddr dest_addr, const void* src_buffer,
  176. size_t size);
  177. void WriteBlock(const VAddr dest_addr, const void* src_buffer, size_t size);
  178. void ZeroBlock(const VAddr dest_addr, const size_t size);
  179. void CopyBlock(VAddr dest_addr, VAddr src_addr, size_t size);
  180. u8* GetPointer(VAddr virtual_address);
  181. std::string ReadCString(VAddr virtual_address, std::size_t max_length);
  182. /**
  183. * Converts a virtual address inside a region with 1:1 mapping to physical memory to a physical
  184. * address. This should be used by services to translate addresses for use by the hardware.
  185. */
  186. boost::optional<PAddr> TryVirtualToPhysicalAddress(VAddr addr);
  187. /**
  188. * Converts a virtual address inside a region with 1:1 mapping to physical memory to a physical
  189. * address. This should be used by services to translate addresses for use by the hardware.
  190. *
  191. * @deprecated Use TryVirtualToPhysicalAddress(), which reports failure.
  192. */
  193. PAddr VirtualToPhysicalAddress(VAddr addr);
  194. /**
  195. * Undoes a mapping performed by VirtualToPhysicalAddress().
  196. */
  197. boost::optional<VAddr> PhysicalToVirtualAddress(PAddr addr);
  198. /**
  199. * Gets a pointer to the memory region beginning at the specified physical address.
  200. */
  201. u8* GetPhysicalPointer(PAddr address);
  202. /**
  203. * Adds the supplied value to the rasterizer resource cache counter of each
  204. * page touching the region.
  205. */
  206. void RasterizerMarkRegionCached(PAddr start, u64 size, int count_delta);
  207. /**
  208. * Flushes any externally cached rasterizer resources touching the given region.
  209. */
  210. void RasterizerFlushRegion(PAddr start, u64 size);
  211. /**
  212. * Flushes and invalidates any externally cached rasterizer resources touching the given region.
  213. */
  214. void RasterizerFlushAndInvalidateRegion(PAddr start, u64 size);
  215. enum class FlushMode {
  216. /// Write back modified surfaces to RAM
  217. Flush,
  218. /// Write back modified surfaces to RAM, and also remove them from the cache
  219. FlushAndInvalidate,
  220. };
  221. /**
  222. * Flushes and invalidates any externally cached rasterizer resources touching the given virtual
  223. * address region.
  224. */
  225. void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode);
  226. } // namespace Memory