memory.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <string>
  8. #include <tuple>
  9. #include <boost/icl/interval_map.hpp>
  10. #include "common/common_types.h"
  11. #include "core/memory_hook.h"
  12. namespace Kernel {
  13. class Process;
  14. }
  15. namespace Memory {
  16. /**
  17. * Page size used by the ARM architecture. This is the smallest granularity with which memory can
  18. * be mapped.
  19. */
  20. constexpr size_t PAGE_BITS = 12;
  21. constexpr u64 PAGE_SIZE = 1 << PAGE_BITS;
  22. constexpr u64 PAGE_MASK = PAGE_SIZE - 1;
  23. constexpr size_t ADDRESS_SPACE_BITS = 36;
  24. constexpr size_t PAGE_TABLE_NUM_ENTRIES = 1ULL << (ADDRESS_SPACE_BITS - PAGE_BITS);
  25. enum class PageType : u8 {
  26. /// Page is unmapped and should cause an access error.
  27. Unmapped,
  28. /// Page is mapped to regular memory. This is the only type you can get pointers to.
  29. Memory,
  30. /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and
  31. /// invalidation
  32. RasterizerCachedMemory,
  33. /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions.
  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. /// Virtual user-space memory regions
  71. enum : VAddr {
  72. /// Where the application text, data and bss reside.
  73. PROCESS_IMAGE_VADDR = 0x08000000,
  74. PROCESS_IMAGE_MAX_SIZE = 0x08000000,
  75. PROCESS_IMAGE_VADDR_END = PROCESS_IMAGE_VADDR + PROCESS_IMAGE_MAX_SIZE,
  76. /// Read-only page containing kernel and system configuration values.
  77. CONFIG_MEMORY_VADDR = 0x1FF80000,
  78. CONFIG_MEMORY_SIZE = 0x00001000,
  79. CONFIG_MEMORY_VADDR_END = CONFIG_MEMORY_VADDR + CONFIG_MEMORY_SIZE,
  80. /// Usually read-only page containing mostly values read from hardware.
  81. SHARED_PAGE_VADDR = 0x1FF81000,
  82. SHARED_PAGE_SIZE = 0x00001000,
  83. SHARED_PAGE_VADDR_END = SHARED_PAGE_VADDR + SHARED_PAGE_SIZE,
  84. /// Area where TLS (Thread-Local Storage) buffers are allocated.
  85. TLS_AREA_VADDR = 0x40000000,
  86. TLS_ENTRY_SIZE = 0x200,
  87. TLS_AREA_SIZE = 0x10000000,
  88. TLS_AREA_VADDR_END = TLS_AREA_VADDR + TLS_AREA_SIZE,
  89. /// Application stack
  90. STACK_AREA_VADDR = TLS_AREA_VADDR_END,
  91. STACK_AREA_SIZE = 0x10000000,
  92. STACK_AREA_VADDR_END = STACK_AREA_VADDR + STACK_AREA_SIZE,
  93. DEFAULT_STACK_SIZE = 0x100000,
  94. /// Application heap
  95. /// Size is confirmed to be a static value on fw 3.0.0
  96. HEAP_VADDR = 0x108000000,
  97. HEAP_SIZE = 0x180000000,
  98. HEAP_VADDR_END = HEAP_VADDR + HEAP_SIZE,
  99. /// New map region
  100. /// Size is confirmed to be a static value on fw 3.0.0
  101. NEW_MAP_REGION_VADDR = HEAP_VADDR_END,
  102. NEW_MAP_REGION_SIZE = 0x80000000,
  103. NEW_MAP_REGION_VADDR_END = NEW_MAP_REGION_VADDR + NEW_MAP_REGION_SIZE,
  104. /// Map region
  105. /// Size is confirmed to be a static value on fw 3.0.0
  106. MAP_REGION_VADDR = NEW_MAP_REGION_VADDR_END,
  107. MAP_REGION_SIZE = 0x1000000000,
  108. MAP_REGION_VADDR_END = MAP_REGION_VADDR + MAP_REGION_SIZE,
  109. /// Kernel Virtual Address Range
  110. KERNEL_REGION_VADDR = 0xFFFFFF8000000000,
  111. KERNEL_REGION_SIZE = 0x7FFFE00000,
  112. KERNEL_REGION_END = KERNEL_REGION_VADDR + KERNEL_REGION_SIZE,
  113. };
  114. /// Currently active page table
  115. void SetCurrentPageTable(PageTable* page_table);
  116. PageTable* GetCurrentPageTable();
  117. /// Determines if the given VAddr is valid for the specified process.
  118. bool IsValidVirtualAddress(const Kernel::Process& process, VAddr vaddr);
  119. bool IsValidVirtualAddress(VAddr vaddr);
  120. /// Determines if the given VAddr is a kernel address
  121. bool IsKernelVirtualAddress(VAddr vaddr);
  122. u8 Read8(VAddr addr);
  123. u16 Read16(VAddr addr);
  124. u32 Read32(VAddr addr);
  125. u64 Read64(VAddr addr);
  126. void Write8(VAddr addr, u8 data);
  127. void Write16(VAddr addr, u16 data);
  128. void Write32(VAddr addr, u32 data);
  129. void Write64(VAddr addr, u64 data);
  130. void ReadBlock(const Kernel::Process& process, VAddr src_addr, void* dest_buffer, size_t size);
  131. void ReadBlock(VAddr src_addr, void* dest_buffer, size_t size);
  132. void WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer,
  133. size_t size);
  134. void WriteBlock(VAddr dest_addr, const void* src_buffer, size_t size);
  135. void ZeroBlock(const Kernel::Process& process, VAddr dest_addr, size_t size);
  136. void CopyBlock(VAddr dest_addr, VAddr src_addr, size_t size);
  137. u8* GetPointer(VAddr vaddr);
  138. std::string ReadCString(VAddr vaddr, std::size_t max_length);
  139. enum class FlushMode {
  140. /// Write back modified surfaces to RAM
  141. Flush,
  142. /// Remove region from the cache
  143. Invalidate,
  144. /// Write back modified surfaces to RAM, and also remove them from the cache
  145. FlushAndInvalidate,
  146. };
  147. /**
  148. * Mark each page touching the region as cached.
  149. */
  150. void RasterizerMarkRegionCached(VAddr vaddr, u64 size, bool cached);
  151. /**
  152. * Flushes and invalidates any externally cached rasterizer resources touching the given virtual
  153. * address region.
  154. */
  155. void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode);
  156. } // namespace Memory