memory.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. namespace Memory {
  14. enum class PageType {
  15. /// Page is unmapped and should cause an access error.
  16. Unmapped,
  17. /// Page is mapped to regular memory. This is the only type you can get pointers to.
  18. Memory,
  19. /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions.
  20. Special,
  21. };
  22. /**
  23. * A (reasonably) fast way of allowing switchable and remmapable process address spaces. It loosely
  24. * mimics the way a real CPU page table works, but instead is optimized for minimal decoding and
  25. * fetching requirements when acessing. In the usual case of an access to regular memory, it only
  26. * requires an indexed fetch and a check for NULL.
  27. */
  28. struct PageTable {
  29. static const size_t NUM_ENTRIES = 1 << (32 - PAGE_BITS);
  30. /**
  31. * Array of memory pointers backing each page. An entry can only be non-null if the
  32. * corresponding entry in the `attributes` array is of type `Memory`.
  33. */
  34. std::array<u8*, NUM_ENTRIES> pointers;
  35. /**
  36. * Array of fine grained page attributes. If it is set to any value other than `Memory`, then
  37. * the corresponding entry in `pointer` MUST be set to null.
  38. */
  39. std::array<PageType, NUM_ENTRIES> attributes;
  40. };
  41. /// Singular page table used for the singleton process
  42. static PageTable main_page_table;
  43. /// Currently active page table
  44. static PageTable* current_page_table = &main_page_table;
  45. static void MapPages(u32 base, u32 size, u8* memory, PageType type) {
  46. LOG_DEBUG(HW_Memory, "Mapping %p onto %08X-%08X", memory, base * PAGE_SIZE, (base + size) * PAGE_SIZE);
  47. u32 end = base + size;
  48. while (base != end) {
  49. ASSERT_MSG(base < PageTable::NUM_ENTRIES, "out of range mapping at %08X", base);
  50. current_page_table->attributes[base] = type;
  51. current_page_table->pointers[base] = memory;
  52. base += 1;
  53. if (memory != nullptr)
  54. memory += PAGE_SIZE;
  55. }
  56. }
  57. void InitMemoryMap() {
  58. main_page_table.pointers.fill(nullptr);
  59. main_page_table.attributes.fill(PageType::Unmapped);
  60. }
  61. void MapMemoryRegion(VAddr base, u32 size, u8* target) {
  62. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  63. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  64. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, target, PageType::Memory);
  65. }
  66. void MapIoRegion(VAddr base, u32 size) {
  67. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  68. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  69. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Special);
  70. }
  71. void UnmapRegion(VAddr base, u32 size) {
  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, nullptr, PageType::Unmapped);
  75. }
  76. template <typename T>
  77. T Read(const VAddr vaddr) {
  78. const u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  79. if (page_pointer) {
  80. T value;
  81. std::memcpy(&value, &page_pointer[vaddr & PAGE_MASK], sizeof(T));
  82. return value;
  83. }
  84. PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  85. switch (type) {
  86. case PageType::Unmapped:
  87. LOG_ERROR(HW_Memory, "unmapped Read%lu @ 0x%08X", sizeof(T) * 8, vaddr);
  88. return 0;
  89. case PageType::Memory:
  90. ASSERT_MSG(false, "Mapped memory page without a pointer @ %08X", vaddr);
  91. case PageType::Special:
  92. LOG_ERROR(HW_Memory, "I/O reads aren't implemented yet @ %08X", vaddr);
  93. return 0;
  94. default:
  95. UNREACHABLE();
  96. }
  97. }
  98. template <typename T>
  99. void Write(const VAddr vaddr, const T data) {
  100. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  101. if (page_pointer) {
  102. std::memcpy(&page_pointer[vaddr & PAGE_MASK], &data, sizeof(T));
  103. return;
  104. }
  105. PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  106. switch (type) {
  107. case PageType::Unmapped:
  108. LOG_ERROR(HW_Memory, "unmapped Write%lu 0x%08X @ 0x%08X", sizeof(data) * 8, (u32) data, vaddr);
  109. return;
  110. case PageType::Memory:
  111. ASSERT_MSG(false, "Mapped memory page without a pointer @ %08X", vaddr);
  112. case PageType::Special:
  113. LOG_ERROR(HW_Memory, "I/O writes aren't implemented yet @ %08X", vaddr);
  114. return;
  115. default:
  116. UNREACHABLE();
  117. }
  118. }
  119. u8* GetPointer(const VAddr vaddr) {
  120. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  121. if (page_pointer) {
  122. return page_pointer + (vaddr & PAGE_MASK);
  123. }
  124. LOG_ERROR(HW_Memory, "unknown GetPointer @ 0x%08x", vaddr);
  125. return nullptr;
  126. }
  127. u8* GetPhysicalPointer(PAddr address) {
  128. return GetPointer(PhysicalToVirtualAddress(address));
  129. }
  130. u8 Read8(const VAddr addr) {
  131. return Read<u8>(addr);
  132. }
  133. u16 Read16(const VAddr addr) {
  134. return Read<u16_le>(addr);
  135. }
  136. u32 Read32(const VAddr addr) {
  137. return Read<u32_le>(addr);
  138. }
  139. u64 Read64(const VAddr addr) {
  140. return Read<u64_le>(addr);
  141. }
  142. void Write8(const VAddr addr, const u8 data) {
  143. Write<u8>(addr, data);
  144. }
  145. void Write16(const VAddr addr, const u16 data) {
  146. Write<u16_le>(addr, data);
  147. }
  148. void Write32(const VAddr addr, const u32 data) {
  149. Write<u32_le>(addr, data);
  150. }
  151. void Write64(const VAddr addr, const u64 data) {
  152. Write<u64_le>(addr, data);
  153. }
  154. void WriteBlock(const VAddr addr, const u8* data, const size_t size) {
  155. for (u32 offset = 0; offset < size; offset++) {
  156. Write8(addr + offset, data[offset]);
  157. }
  158. }
  159. PAddr VirtualToPhysicalAddress(const VAddr addr) {
  160. if (addr == 0) {
  161. return 0;
  162. } else if (addr >= VRAM_VADDR && addr < VRAM_VADDR_END) {
  163. return addr - VRAM_VADDR + VRAM_PADDR;
  164. } else if (addr >= LINEAR_HEAP_VADDR && addr < LINEAR_HEAP_VADDR_END) {
  165. return addr - LINEAR_HEAP_VADDR + FCRAM_PADDR;
  166. } else if (addr >= DSP_RAM_VADDR && addr < DSP_RAM_VADDR_END) {
  167. return addr - DSP_RAM_VADDR + DSP_RAM_PADDR;
  168. } else if (addr >= IO_AREA_VADDR && addr < IO_AREA_VADDR_END) {
  169. return addr - IO_AREA_VADDR + IO_AREA_PADDR;
  170. } else if (addr >= NEW_LINEAR_HEAP_VADDR && addr < NEW_LINEAR_HEAP_VADDR_END) {
  171. return addr - NEW_LINEAR_HEAP_VADDR + FCRAM_PADDR;
  172. }
  173. LOG_ERROR(HW_Memory, "Unknown virtual address @ 0x%08X", addr);
  174. // To help with debugging, set bit on address so that it's obviously invalid.
  175. return addr | 0x80000000;
  176. }
  177. VAddr PhysicalToVirtualAddress(const PAddr addr) {
  178. if (addr == 0) {
  179. return 0;
  180. } else if (addr >= VRAM_PADDR && addr < VRAM_PADDR_END) {
  181. return addr - VRAM_PADDR + VRAM_VADDR;
  182. } else if (addr >= FCRAM_PADDR && addr < FCRAM_PADDR_END) {
  183. return addr - FCRAM_PADDR + Kernel::g_current_process->GetLinearHeapBase();
  184. } else if (addr >= DSP_RAM_PADDR && addr < DSP_RAM_PADDR_END) {
  185. return addr - DSP_RAM_PADDR + DSP_RAM_VADDR;
  186. } else if (addr >= IO_AREA_PADDR && addr < IO_AREA_PADDR_END) {
  187. return addr - IO_AREA_PADDR + IO_AREA_VADDR;
  188. }
  189. LOG_ERROR(HW_Memory, "Unknown physical address @ 0x%08X", addr);
  190. // To help with debugging, set bit on address so that it's obviously invalid.
  191. return addr | 0x80000000;
  192. }
  193. } // namespace