AMemoryMgr.cs 8.0 KB

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