memory.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include "video_core/memory_manager.h"
  13. namespace Kernel {
  14. class Process;
  15. }
  16. namespace Memory {
  17. /**
  18. * Page size used by the ARM architecture. This is the smallest granularity with which memory can
  19. * be mapped.
  20. */
  21. constexpr size_t PAGE_BITS = 12;
  22. constexpr u64 PAGE_SIZE = 1 << PAGE_BITS;
  23. constexpr u64 PAGE_MASK = PAGE_SIZE - 1;
  24. constexpr size_t ADDRESS_SPACE_BITS = 36;
  25. constexpr size_t PAGE_TABLE_NUM_ENTRIES = 1ULL << (ADDRESS_SPACE_BITS - PAGE_BITS);
  26. enum class PageType : u8 {
  27. /// Page is unmapped and should cause an access error.
  28. Unmapped,
  29. /// Page is mapped to regular memory. This is the only type you can get pointers to.
  30. Memory,
  31. /// Page is mapped to regular memory, but also needs to check for rasterizer cache flushing and
  32. /// invalidation
  33. RasterizerCachedMemory,
  34. /// Page is mapped to a I/O region. Writing and reading to this page is handled by functions.
  35. Special,
  36. };
  37. struct SpecialRegion {
  38. enum class Type {
  39. DebugHook,
  40. IODevice,
  41. } type;
  42. MemoryHookPointer handler;
  43. bool operator<(const SpecialRegion& other) const {
  44. return std::tie(type, handler) < std::tie(other.type, other.handler);
  45. }
  46. bool operator==(const SpecialRegion& other) const {
  47. return std::tie(type, handler) == std::tie(other.type, other.handler);
  48. }
  49. };
  50. /**
  51. * A (reasonably) fast way of allowing switchable and remappable process address spaces. It loosely
  52. * mimics the way a real CPU page table works.
  53. */
  54. struct PageTable {
  55. /**
  56. * Array of memory pointers backing each page. An entry can only be non-null if the
  57. * corresponding entry in the `attributes` array is of type `Memory`.
  58. */
  59. std::array<u8*, PAGE_TABLE_NUM_ENTRIES> pointers;
  60. /**
  61. * Contains MMIO handlers that back memory regions whose entries in the `attribute` array is of
  62. * type `Special`.
  63. */
  64. boost::icl::interval_map<VAddr, std::set<SpecialRegion>> special_regions;
  65. /**
  66. * Array of fine grained page attributes. If it is set to any value other than `Memory`, then
  67. * the corresponding entry in `pointers` MUST be set to null.
  68. */
  69. std::array<PageType, PAGE_TABLE_NUM_ENTRIES> attributes;
  70. };
  71. /// Virtual user-space memory regions
  72. enum : VAddr {
  73. /// Where the application text, data and bss reside.
  74. PROCESS_IMAGE_VADDR = 0x08000000,
  75. PROCESS_IMAGE_MAX_SIZE = 0x08000000,
  76. PROCESS_IMAGE_VADDR_END = PROCESS_IMAGE_VADDR + PROCESS_IMAGE_MAX_SIZE,
  77. /// Read-only page containing kernel and system configuration values.
  78. CONFIG_MEMORY_VADDR = 0x1FF80000,
  79. CONFIG_MEMORY_SIZE = 0x00001000,
  80. CONFIG_MEMORY_VADDR_END = CONFIG_MEMORY_VADDR + CONFIG_MEMORY_SIZE,
  81. /// Usually read-only page containing mostly values read from hardware.
  82. SHARED_PAGE_VADDR = 0x1FF81000,
  83. SHARED_PAGE_SIZE = 0x00001000,
  84. SHARED_PAGE_VADDR_END = SHARED_PAGE_VADDR + SHARED_PAGE_SIZE,
  85. /// Area where TLS (Thread-Local Storage) buffers are allocated.
  86. TLS_AREA_VADDR = 0x40000000,
  87. TLS_ENTRY_SIZE = 0x200,
  88. TLS_AREA_SIZE = 0x10000000,
  89. TLS_AREA_VADDR_END = TLS_AREA_VADDR + TLS_AREA_SIZE,
  90. /// Application stack
  91. STACK_AREA_VADDR = TLS_AREA_VADDR_END,
  92. STACK_AREA_SIZE = 0x10000000,
  93. STACK_AREA_VADDR_END = STACK_AREA_VADDR + STACK_AREA_SIZE,
  94. DEFAULT_STACK_SIZE = 0x100000,
  95. /// Application heap
  96. /// Size is confirmed to be a static value on fw 3.0.0
  97. HEAP_VADDR = 0x108000000,
  98. HEAP_SIZE = 0x180000000,
  99. HEAP_VADDR_END = HEAP_VADDR + HEAP_SIZE,
  100. /// New map region
  101. /// Size is confirmed to be a static value on fw 3.0.0
  102. NEW_MAP_REGION_VADDR = HEAP_VADDR_END,
  103. NEW_MAP_REGION_SIZE = 0x80000000,
  104. NEW_MAP_REGION_VADDR_END = NEW_MAP_REGION_VADDR + NEW_MAP_REGION_SIZE,
  105. /// Map region
  106. /// Size is confirmed to be a static value on fw 3.0.0
  107. MAP_REGION_VADDR = NEW_MAP_REGION_VADDR_END,
  108. MAP_REGION_SIZE = 0x1000000000,
  109. MAP_REGION_VADDR_END = MAP_REGION_VADDR + MAP_REGION_SIZE,
  110. /// Kernel Virtual Address Range
  111. KERNEL_REGION_VADDR = 0xFFFFFF8000000000,
  112. KERNEL_REGION_SIZE = 0x7FFFE00000,
  113. KERNEL_REGION_END = KERNEL_REGION_VADDR + KERNEL_REGION_SIZE,
  114. };
  115. /// Currently active page table
  116. void SetCurrentPageTable(PageTable* page_table);
  117. PageTable* GetCurrentPageTable();
  118. /// Determines if the given VAddr is valid for the specified process.
  119. bool IsValidVirtualAddress(const Kernel::Process& process, VAddr vaddr);
  120. bool IsValidVirtualAddress(VAddr addr);
  121. /// Determines if the given VAddr is a kernel address
  122. bool IsKernelVirtualAddress(VAddr addr);
  123. u8 Read8(VAddr addr);
  124. u16 Read16(VAddr addr);
  125. u32 Read32(VAddr addr);
  126. u64 Read64(VAddr addr);
  127. void Write8(VAddr addr, u8 data);
  128. void Write16(VAddr addr, u16 data);
  129. void Write32(VAddr addr, u32 data);
  130. void Write64(VAddr addr, u64 data);
  131. void ReadBlock(const Kernel::Process& process, VAddr src_addr, void* dest_buffer, size_t size);
  132. void ReadBlock(VAddr src_addr, void* dest_buffer, size_t size);
  133. void WriteBlock(const Kernel::Process& process, VAddr dest_addr, const void* src_buffer,
  134. size_t size);
  135. void WriteBlock(VAddr dest_addr, const void* src_buffer, size_t size);
  136. void ZeroBlock(const Kernel::Process& process, VAddr dest_addr, size_t size);
  137. void CopyBlock(VAddr dest_addr, VAddr src_addr, size_t size);
  138. u8* GetPointer(VAddr virtual_address);
  139. std::string ReadCString(VAddr virtual_address, std::size_t max_length);
  140. enum class FlushMode {
  141. /// Write back modified surfaces to RAM
  142. Flush,
  143. /// Remove region from the cache
  144. Invalidate,
  145. /// Write back modified surfaces to RAM, and also remove them from the cache
  146. FlushAndInvalidate,
  147. };
  148. /**
  149. * Mark each page touching the region as cached.
  150. */
  151. void RasterizerMarkRegionCached(Tegra::GPUVAddr start, u64 size, bool cached);
  152. /**
  153. * Flushes and invalidates any externally cached rasterizer resources touching the given virtual
  154. * address region.
  155. */
  156. void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode);
  157. } // namespace Memory