AMemoryMgr.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. namespace ChocolArm64.Memory
  2. {
  3. public class AMemoryMgr
  4. {
  5. public const long AddrSize = RamSize;
  6. public const long RamSize = 4L * 1024 * 1024 * 1024;
  7. private const int PTLvl0Bits = 11;
  8. private const int PTLvl1Bits = 13;
  9. private const int PTPageBits = 12;
  10. private const int PTLvl0Size = 1 << PTLvl0Bits;
  11. private const int PTLvl1Size = 1 << PTLvl1Bits;
  12. public const int PageSize = 1 << PTPageBits;
  13. private const int PTLvl0Mask = PTLvl0Size - 1;
  14. private const int PTLvl1Mask = PTLvl1Size - 1;
  15. public const int PageMask = PageSize - 1;
  16. private const int PTLvl0Bit = PTPageBits + PTLvl0Bits;
  17. private const int PTLvl1Bit = PTPageBits;
  18. private AMemoryAlloc Allocator;
  19. private enum PTMap
  20. {
  21. Unmapped,
  22. Mapped
  23. }
  24. private struct PTEntry
  25. {
  26. public PTMap Map;
  27. public AMemoryPerm Perm;
  28. public int Type;
  29. public int Attr;
  30. public PTEntry(PTMap Map, AMemoryPerm Perm, int Type, int Attr)
  31. {
  32. this.Map = Map;
  33. this.Perm = Perm;
  34. this.Type = Type;
  35. this.Attr = Attr;
  36. }
  37. }
  38. private PTEntry[][] PageTable;
  39. private bool IsHeapInitialized;
  40. public long HeapAddr { get; private set; }
  41. public long HeapSize { get; private set; }
  42. public AMemoryMgr(AMemoryAlloc Allocator)
  43. {
  44. this.Allocator = Allocator;
  45. PageTable = new PTEntry[PTLvl0Size][];
  46. }
  47. public long GetTotalMemorySize()
  48. {
  49. return Allocator.GetFreeMem() + GetUsedMemorySize();
  50. }
  51. public long GetUsedMemorySize()
  52. {
  53. long Size = 0;
  54. for (int L0 = 0; L0 < PageTable.Length; L0++)
  55. {
  56. if (PageTable[L0] == null)
  57. {
  58. continue;
  59. }
  60. for (int L1 = 0; L1 < PageTable[L0].Length; L1++)
  61. {
  62. Size += PageTable[L0][L1].Map != PTMap.Unmapped ? PageSize : 0;
  63. }
  64. }
  65. return Size;
  66. }
  67. public bool SetHeapAddr(long Position)
  68. {
  69. if (!IsHeapInitialized)
  70. {
  71. HeapAddr = Position;
  72. IsHeapInitialized = true;
  73. return true;
  74. }
  75. return false;
  76. }
  77. public void SetHeapSize(long Size, int Type)
  78. {
  79. //TODO: Return error when theres no enough space to allocate heap.
  80. Size = AMemoryHelper.PageRoundUp(Size);
  81. long Position = HeapAddr;
  82. if ((ulong)Size < (ulong)HeapSize)
  83. {
  84. //Try to free now free area if size is smaller than old size.
  85. Position += Size;
  86. while ((ulong)Size < (ulong)HeapSize)
  87. {
  88. Allocator.Free(Position);
  89. Position += PageSize;
  90. }
  91. }
  92. else
  93. {
  94. //Allocate extra needed size.
  95. Position += HeapSize;
  96. Size -= HeapSize;
  97. MapPhys(Position, Size, Type, AMemoryPerm.RW);
  98. }
  99. HeapSize = Size;
  100. }
  101. public void MapPhys(long Position, long Size, int Type, AMemoryPerm Perm)
  102. {
  103. while (Size > 0)
  104. {
  105. if (!IsMapped(Position))
  106. {
  107. SetPTEntry(Position, new PTEntry(PTMap.Mapped, Perm, Type, 0));
  108. }
  109. long CPgSize = PageSize - (Position & PageMask);
  110. Position += CPgSize;
  111. Size -= CPgSize;
  112. }
  113. }
  114. public void MapMirror(long Src, long Dst, long Size, int Type)
  115. {
  116. Src = AMemoryHelper.PageRoundDown(Src);
  117. Dst = AMemoryHelper.PageRoundDown(Dst);
  118. Size = AMemoryHelper.PageRoundUp(Size);
  119. long PagesCount = Size / PageSize;
  120. while (PagesCount-- > 0)
  121. {
  122. PTEntry SrcEntry = GetPTEntry(Src);
  123. PTEntry DstEntry = GetPTEntry(Dst);
  124. DstEntry.Map = PTMap.Mapped;
  125. DstEntry.Type = Type;
  126. DstEntry.Perm = SrcEntry.Perm;
  127. SrcEntry.Perm = AMemoryPerm.None;
  128. SrcEntry.Attr |= 1;
  129. SetPTEntry(Src, SrcEntry);
  130. SetPTEntry(Dst, DstEntry);
  131. Src += PageSize;
  132. Dst += PageSize;
  133. }
  134. }
  135. public void Reprotect(long Position, long Size, AMemoryPerm Perm)
  136. {
  137. Position = AMemoryHelper.PageRoundDown(Position);
  138. Size = AMemoryHelper.PageRoundUp(Size);
  139. long PagesCount = Size / PageSize;
  140. while (PagesCount-- > 0)
  141. {
  142. PTEntry Entry = GetPTEntry(Position);
  143. Entry.Perm = Perm;
  144. SetPTEntry(Position, Entry);
  145. Position += PageSize;
  146. }
  147. }
  148. public AMemoryMapInfo GetMapInfo(long Position)
  149. {
  150. Position = AMemoryHelper.PageRoundDown(Position);
  151. PTEntry BaseEntry = GetPTEntry(Position);
  152. bool IsSameSegment(long Pos)
  153. {
  154. PTEntry Entry = GetPTEntry(Pos);
  155. return Entry.Map == BaseEntry.Map &&
  156. Entry.Perm == BaseEntry.Perm &&
  157. Entry.Type == BaseEntry.Type &&
  158. Entry.Attr == BaseEntry.Attr;
  159. }
  160. long Start = Position;
  161. long End = Position + PageSize;
  162. while (Start > 0 && IsSameSegment(Start - PageSize))
  163. {
  164. Start -= PageSize;
  165. }
  166. while (End < AddrSize && IsSameSegment(End))
  167. {
  168. End += PageSize;
  169. }
  170. long Size = End - Start;
  171. return new AMemoryMapInfo(
  172. Start,
  173. Size,
  174. BaseEntry.Type,
  175. BaseEntry.Attr,
  176. BaseEntry.Perm);
  177. }
  178. public bool HasPermission(long Position, AMemoryPerm Perm)
  179. {
  180. return GetPTEntry(Position).Perm.HasFlag(Perm);
  181. }
  182. public bool IsMapped(long Position)
  183. {
  184. if (Position >> PTLvl0Bits + PTLvl1Bits + PTPageBits != 0)
  185. {
  186. return false;
  187. }
  188. long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
  189. long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
  190. if (PageTable[L0] == null)
  191. {
  192. return false;
  193. }
  194. return PageTable[L0][L1].Map != PTMap.Unmapped;
  195. }
  196. private PTEntry GetPTEntry(long Position)
  197. {
  198. long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
  199. long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
  200. if (PageTable[L0] == null)
  201. {
  202. return default(PTEntry);
  203. }
  204. return PageTable[L0][L1];
  205. }
  206. private void SetPTEntry(long Position, PTEntry Entry)
  207. {
  208. long L0 = (Position >> PTLvl0Bit) & PTLvl0Mask;
  209. long L1 = (Position >> PTLvl1Bit) & PTLvl1Mask;
  210. if (PageTable[L0] == null)
  211. {
  212. PageTable[L0] = new PTEntry[PTLvl1Size];
  213. }
  214. PageTable[L0][L1] = Entry;
  215. }
  216. }
  217. }