memory.h 9.2 KB

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