memory.cpp 6.0 KB

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