memory.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "common/assert.h"
  6. #include "common/common_types.h"
  7. #include "common/logging/log.h"
  8. #include "common/swap.h"
  9. #include "core/mem_map.h"
  10. #include "core/memory.h"
  11. #include "core/memory_setup.h"
  12. namespace Memory {
  13. enum class PageType {
  14. /// Page is unmapped and should cause an access error.
  15. Unmapped,
  16. /// Page is mapped to regular memory. This is the only type you can get pointers to.
  17. Memory,
  18. /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions.
  19. Special,
  20. };
  21. /**
  22. * A (reasonably) fast way of allowing switchable and remmapable process address spaces. It loosely
  23. * mimics the way a real CPU page table works, but instead is optimized for minimal decoding and
  24. * fetching requirements when acessing. In the usual case of an access to regular memory, it only
  25. * requires an indexed fetch and a check for NULL.
  26. */
  27. struct PageTable {
  28. static const size_t NUM_ENTRIES = 1 << (32 - PAGE_BITS);
  29. /**
  30. * Array of memory pointers backing each page. An entry can only be non-null if the
  31. * corresponding entry in the `attributes` array is of type `Memory`.
  32. */
  33. std::array<u8*, NUM_ENTRIES> pointers;
  34. /**
  35. * Array of fine grained page attributes. If it is set to any value other than `Memory`, then
  36. * the corresponding entry in `pointer` MUST be set to null.
  37. */
  38. std::array<PageType, NUM_ENTRIES> attributes;
  39. };
  40. /// Singular page table used for the singleton process
  41. static PageTable main_page_table;
  42. /// Currently active page table
  43. static PageTable* current_page_table = &main_page_table;
  44. static void MapPages(u32 base, u32 size, u8* memory, PageType type) {
  45. LOG_DEBUG(HW_Memory, "Mapping %p onto %08X-%08X", memory, base * PAGE_SIZE, (base + size) * PAGE_SIZE);
  46. u32 end = base + size;
  47. while (base != end) {
  48. ASSERT_MSG(base < PageTable::NUM_ENTRIES, "out of range mapping at %08X", base);
  49. current_page_table->attributes[base] = type;
  50. current_page_table->pointers[base] = memory;
  51. base += 1;
  52. if (memory != nullptr)
  53. memory += PAGE_SIZE;
  54. }
  55. }
  56. void InitMemoryMap() {
  57. main_page_table.pointers.fill(nullptr);
  58. main_page_table.attributes.fill(PageType::Unmapped);
  59. }
  60. void MapMemoryRegion(VAddr base, u32 size, u8* target) {
  61. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  62. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  63. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, target, PageType::Memory);
  64. }
  65. void MapIoRegion(VAddr base, u32 size) {
  66. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  67. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  68. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Special);
  69. }
  70. void UnmapRegion(VAddr base, u32 size) {
  71. ASSERT_MSG((size & PAGE_MASK) == 0, "non-page aligned size: %08X", size);
  72. ASSERT_MSG((base & PAGE_MASK) == 0, "non-page aligned base: %08X", base);
  73. MapPages(base / PAGE_SIZE, size / PAGE_SIZE, nullptr, PageType::Unmapped);
  74. }
  75. template <typename T>
  76. T Read(const VAddr vaddr) {
  77. const u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  78. if (page_pointer) {
  79. return *reinterpret_cast<const T*>(page_pointer + (vaddr & PAGE_MASK));
  80. }
  81. PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  82. switch (type) {
  83. case PageType::Unmapped:
  84. LOG_ERROR(HW_Memory, "unmapped Read%lu @ 0x%08X", sizeof(T) * 8, vaddr);
  85. return 0;
  86. case PageType::Memory:
  87. ASSERT_MSG(false, "Mapped memory page without a pointer @ %08X", vaddr);
  88. case PageType::Special:
  89. LOG_ERROR(HW_Memory, "I/O reads aren't implemented yet @ %08X", vaddr);
  90. return 0;
  91. default:
  92. UNREACHABLE();
  93. }
  94. }
  95. template <typename T>
  96. void Write(const VAddr vaddr, const T data) {
  97. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  98. if (page_pointer) {
  99. *reinterpret_cast<T*>(page_pointer + (vaddr & PAGE_MASK)) = data;
  100. return;
  101. }
  102. PageType type = current_page_table->attributes[vaddr >> PAGE_BITS];
  103. switch (type) {
  104. case PageType::Unmapped:
  105. LOG_ERROR(HW_Memory, "unmapped Write%lu 0x%08X @ 0x%08X", sizeof(data) * 8, (u32) data, vaddr);
  106. return;
  107. case PageType::Memory:
  108. ASSERT_MSG(false, "Mapped memory page without a pointer @ %08X", vaddr);
  109. case PageType::Special:
  110. LOG_ERROR(HW_Memory, "I/O writes aren't implemented yet @ %08X", vaddr);
  111. return;
  112. default:
  113. UNREACHABLE();
  114. }
  115. }
  116. u8* GetPointer(const VAddr vaddr) {
  117. u8* page_pointer = current_page_table->pointers[vaddr >> PAGE_BITS];
  118. if (page_pointer) {
  119. return page_pointer + (vaddr & PAGE_MASK);
  120. }
  121. LOG_ERROR(HW_Memory, "unknown GetPointer @ 0x%08x", vaddr);
  122. return nullptr;
  123. }
  124. u8* GetPhysicalPointer(PAddr address) {
  125. return GetPointer(PhysicalToVirtualAddress(address));
  126. }
  127. u8 Read8(const VAddr addr) {
  128. return Read<u8>(addr);
  129. }
  130. u16 Read16(const VAddr addr) {
  131. return Read<u16_le>(addr);
  132. }
  133. u32 Read32(const VAddr addr) {
  134. return Read<u32_le>(addr);
  135. }
  136. u64 Read64(const VAddr addr) {
  137. return Read<u64_le>(addr);
  138. }
  139. void Write8(const VAddr addr, const u8 data) {
  140. Write<u8>(addr, data);
  141. }
  142. void Write16(const VAddr addr, const u16 data) {
  143. Write<u16_le>(addr, data);
  144. }
  145. void Write32(const VAddr addr, const u32 data) {
  146. Write<u32_le>(addr, data);
  147. }
  148. void Write64(const VAddr addr, const u64 data) {
  149. Write<u64_le>(addr, data);
  150. }
  151. void WriteBlock(const VAddr addr, const u8* data, const size_t size) {
  152. u32 offset = 0;
  153. while (offset < (size & ~3)) {
  154. Write32(addr + offset, *(u32*)&data[offset]);
  155. offset += 4;
  156. }
  157. if (size & 2) {
  158. Write16(addr + offset, *(u16*)&data[offset]);
  159. offset += 2;
  160. }
  161. if (size & 1)
  162. Write8(addr + offset, data[offset]);
  163. }
  164. } // namespace