memory.h 8.3 KB

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