AMemoryMgr.cs 8.1 KB

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