memory.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <cstring>
  6. #include "common/assert.h"
  7. #include "common/common_types.h"
  8. #include "common/logging/log.h"
  9. #include "common/swap.h"
  10. #include "core/hle/kernel/process.h"
  11. #include "core/memory.h"
  12. #include "core/memory_setup.h"
  13. #include "core/mmio.h"
  14. namespace Memory {
  15. enum class PageType {
  16. /// Page is unmapped and should cause an access error.
  17. Unmapped,
  18. /// Page is mapped to regular memory. This is the only type you can get pointers to.
  19. Memory,
  20. /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions.
  21. Special,
  22. };
  23. struct SpecialRegion {
  24. VAddr base;
  25. u32 size;
  26. MMIORegionPointer handler;
  27. };
  28. /**
  29. * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely
  30. * mimics the way a real CPU page table works, but instead is optimized for minimal decoding and
  31. * fetching requirements when accessing. In the usual case of an access to regular memory, it only
  32. * requires an indexed fetch and a check for NULL.
  33. */
  34. struct PageTable {
  35. static const size_t NUM_ENTRIES = 1 << (32 - PAGE_BITS);
  36. /**
  37. * Array of memory pointers backing each page. An entry can only be non-null if the
  38. * corresponding entry in the `attributes` array is of type `Memory`.
  39. */
  40. std::array<u8*, NUM_ENTRIES> pointers;
  41. /**
  42. * Contains MMIO handlers that back memory regions whose entries in the `attribute` array is of type `Special`.
  43. */
  44. std::vector<SpecialRegion> special_regions;
  45. /**
  46. * Array of fine grained page attributes. If it is set to any value other than `Memory`, then
  47. * the corresponding entry in `pointers` MUST be set to null.
  48. */
  49. std::array<PageType, NUM_ENTRIES> attributes;
  50. };
  51. /// Singular page table used for the singleton process
  52. static PageTable main_page_table;
  53. /// Currently active page table
  54. static PageTable* current_page_table = &main_page_table;
  55. static void MapPages(u32 base, u32 size, u8* memory, PageType type) {
  56. LOG_DEBUG(HW_Memory, "Mapping %p onto %08X-%08X", memory, base * PAGE_SIZE, (base + size) * PAGE_SIZE);
  57. u32 end = base + size;
  58. while (base != end) {
  59. ASSERT_MSG(base < PageTable::NUM_ENTRIES, "out of range mapping at %08X", base);
  60. current_page_table->attributes[base] = type;
  61. current_page_table->pointers[base] = memory;
  62. base += 1;
  63. if (memory != nullptr)
  64. memory += PAGE_SIZE;
  65. }
  66. }
  67. void InitMemoryMap() {
  68. main_page_table.pointers.fill(nullptr);
  69. main_page_table.attributes.fill(PageType::Unmapped);
  70. }
  71. void MapMemoryRegion(VAddr base, u32 size, u8* target) {
  72. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  73. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  74. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, target, PageType::Memory);
  75. }
  76. void MapIoRegion(VAddr base, u32 size, MMIORegionPointer mmio_handler) {
  77. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  78. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  79. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Special);
  80. current_page_table->special_regions.emplace_back(SpecialRegion{base, size, mmio_handler});
  81. }
  82. void UnmapRegion(VAddr base, u32 size) {
  83. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  84. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  85. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Unmapped);
  86. }
  87. /**
  88. * This function should only be called for virtual addreses with attribute `PageType::Special`.
  89. */
  90. static MMIORegionPointer GetMMIOHandler(VAddr vaddr) {
  91. for (const auto& region : current_page_table->special_regions) {
  92. if (vaddr >= region.base && vaddr < (region.base + region.size)) {
  93. return region.handler;
  94. }
  95. }
  96. ASSERT_MSG(false, "Mapped IO page without a handler @ %08X", vaddr);
  97. return nullptr; // Should never happen
  98. }
  99. template<typename T>
  100. T ReadMMIO(MMIORegionPointer mmio_handler, VAddr addr);
  101. template <typename T>
  102. T Read(const VAddr vaddr) {
  103. const u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  104. if (page_pointer) {
  105. T value;
  106. std::memcpy(&value, &page_pointer[vaddr & PAGE_MASK], sizeof(T));
  107. return value;
  108. }
  109. PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  110. switch (type) {
  111. case PageType::Unmapped:
  112. LOG_ERROR(HW_Memory, "unmapped Read%lu @ 0x%08X", sizeof(T) * 8, vaddr);
  113. return 0;
  114. case PageType::Memory:
  115. ASSERT_MSG(false, "Mapped memory page without a pointer @ %08X", vaddr);
  116. break;
  117. case PageType::Special:
  118. return ReadMMIO<T>(GetMMIOHandler(vaddr), vaddr);
  119. default:
  120. UNREACHABLE();
  121. }
  122. }
  123. template<typename T>
  124. void WriteMMIO(MMIORegionPointer mmio_handler, VAddr addr, const T data);
  125. template <typename T>
  126. void Write(const VAddr vaddr, const T data) {
  127. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  128. if (page_pointer) {
  129. std::memcpy(&page_pointer[vaddr & PAGE_MASK], &data, sizeof(T));
  130. return;
  131. }
  132. PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  133. switch (type) {
  134. case PageType::Unmapped:
  135. LOG_ERROR(HW_Memory, "unmapped Write%lu 0x%08X @ 0x%08X", sizeof(data) * 8, (u32) data, vaddr);
  136. return;
  137. case PageType::Memory:
  138. ASSERT_MSG(false, "Mapped memory page without a pointer @ %08X", vaddr);
  139. break;
  140. case PageType::Special:
  141. WriteMMIO<T>(GetMMIOHandler(vaddr), vaddr, data);
  142. break;
  143. default:
  144. UNREACHABLE();
  145. }
  146. }
  147. u8* GetPointer(const VAddr vaddr) {
  148. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  149. if (page_pointer) {
  150. return page_pointer + (vaddr & PAGE_MASK);
  151. }
  152. LOG_ERROR(HW_Memory, "unknown GetPointer @ 0x%08x", vaddr);
  153. return nullptr;
  154. }
  155. u8* GetPhysicalPointer(PAddr address) {
  156. return GetPointer(PhysicalToVirtualAddress(address));
  157. }
  158. u8 Read8(const VAddr addr) {
  159. return Read<u8>(addr);
  160. }
  161. u16 Read16(const VAddr addr) {
  162. return Read<u16_le>(addr);
  163. }
  164. u32 Read32(const VAddr addr) {
  165. return Read<u32_le>(addr);
  166. }
  167. u64 Read64(const VAddr addr) {
  168. return Read<u64_le>(addr);
  169. }
  170. void Write8(const VAddr addr, const u8 data) {
  171. Write<u8>(addr, data);
  172. }
  173. void Write16(const VAddr addr, const u16 data) {
  174. Write<u16_le>(addr, data);
  175. }
  176. void Write32(const VAddr addr, const u32 data) {
  177. Write<u32_le>(addr, data);
  178. }
  179. void Write64(const VAddr addr, const u64 data) {
  180. Write<u64_le>(addr, data);
  181. }
  182. void WriteBlock(const VAddr addr, const u8* data, const size_t size) {
  183. for (u32 offset = 0; offset < size; offset++) {
  184. Write8(addr + offset, data[offset]);
  185. }
  186. }
  187. template<>
  188. u8 ReadMMIO<u8>(MMIORegionPointer mmio_handler, VAddr addr) {
  189. return mmio_handler->Read8(addr);
  190. }
  191. template<>
  192. u16 ReadMMIO<u16>(MMIORegionPointer mmio_handler, VAddr addr) {
  193. return mmio_handler->Read16(addr);
  194. }
  195. template<>
  196. u32 ReadMMIO<u32>(MMIORegionPointer mmio_handler, VAddr addr) {
  197. return mmio_handler->Read32(addr);
  198. }
  199. template<>
  200. u64 ReadMMIO<u64>(MMIORegionPointer mmio_handler, VAddr addr) {
  201. return mmio_handler->Read64(addr);
  202. }
  203. template<>
  204. void WriteMMIO<u8>(MMIORegionPointer mmio_handler, VAddr addr, const u8 data) {
  205. mmio_handler->Write8(addr, data);
  206. }
  207. template<>
  208. void WriteMMIO<u16>(MMIORegionPointer mmio_handler, VAddr addr, const u16 data) {
  209. mmio_handler->Write16(addr, data);
  210. }
  211. template<>
  212. void WriteMMIO<u32>(MMIORegionPointer mmio_handler, VAddr addr, const u32 data) {
  213. mmio_handler->Write32(addr, data);
  214. }
  215. template<>
  216. void WriteMMIO<u64>(MMIORegionPointer mmio_handler, VAddr addr, const u64 data) {
  217. mmio_handler->Write64(addr, data);
  218. }
  219. PAddr VirtualToPhysicalAddress(const VAddr addr) {
  220. if (addr == 0) {
  221. return 0;
  222. } else if (addr >= VRAM_VADDR && addr < VRAM_VADDR_END) {
  223. return addr - VRAM_VADDR + VRAM_PADDR;
  224. } else if (addr >= LINEAR_HEAP_VADDR && addr < LINEAR_HEAP_VADDR_END) {
  225. return addr - LINEAR_HEAP_VADDR + FCRAM_PADDR;
  226. } else if (addr >= DSP_RAM_VADDR && addr < DSP_RAM_VADDR_END) {
  227. return addr - DSP_RAM_VADDR + DSP_RAM_PADDR;
  228. } else if (addr >= IO_AREA_VADDR && addr < IO_AREA_VADDR_END) {
  229. return addr - IO_AREA_VADDR + IO_AREA_PADDR;
  230. } else if (addr >= NEW_LINEAR_HEAP_VADDR && addr < NEW_LINEAR_HEAP_VADDR_END) {
  231. return addr - NEW_LINEAR_HEAP_VADDR + FCRAM_PADDR;
  232. }
  233. LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x%08X", addr);
  234. // To help with debugging, set bit on address so that it's obviously invalid.
  235. return addr | 0x80000000;
  236. }
  237. VAddr PhysicalToVirtualAddress(const PAddr addr) {
  238. if (addr == 0) {
  239. return 0;
  240. } else if (addr >= VRAM_PADDR && addr < VRAM_PADDR_END) {
  241. return addr - VRAM_PADDR + VRAM_VADDR;
  242. } else if (addr >= FCRAM_PADDR && addr < FCRAM_PADDR_END) {
  243. return addr - FCRAM_PADDR + Kernel::g_current_process->GetLinearHeapBase();
  244. } else if (addr >= DSP_RAM_PADDR && addr < DSP_RAM_PADDR_END) {
  245. return addr - DSP_RAM_PADDR + DSP_RAM_VADDR;
  246. } else if (addr >= IO_AREA_PADDR && addr < IO_AREA_PADDR_END) {
  247. return addr - IO_AREA_PADDR + IO_AREA_VADDR;
  248. }
  249. LOG_ERROR(HW_Memory, "Unknown physical address @ 0x%08X", addr);
  250. // To help with debugging, set bit on address so that it's obviously invalid.
  251. return addr | 0x80000000;
  252. }
  253. } // namespace