KMemoryRegionManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. using Ryujinx.Common;
  2. namespace Ryujinx.HLE.HOS.Kernel
  3. {
  4. class KMemoryRegionManager
  5. {
  6. private static readonly int[] BlockOrders = new int[] { 12, 16, 21, 22, 25, 29, 30 };
  7. public ulong Address { get; private set; }
  8. public ulong EndAddr { get; private set; }
  9. public ulong Size { get; private set; }
  10. private int BlockOrdersCount;
  11. private KMemoryRegionBlock[] Blocks;
  12. public KMemoryRegionManager(ulong Address, ulong Size, ulong EndAddr)
  13. {
  14. Blocks = new KMemoryRegionBlock[BlockOrders.Length];
  15. this.Address = Address;
  16. this.Size = Size;
  17. this.EndAddr = EndAddr;
  18. BlockOrdersCount = BlockOrders.Length;
  19. for (int BlockIndex = 0; BlockIndex < BlockOrdersCount; BlockIndex++)
  20. {
  21. Blocks[BlockIndex] = new KMemoryRegionBlock();
  22. Blocks[BlockIndex].Order = BlockOrders[BlockIndex];
  23. int NextOrder = BlockIndex == BlockOrdersCount - 1 ? 0 : BlockOrders[BlockIndex + 1];
  24. Blocks[BlockIndex].NextOrder = NextOrder;
  25. int CurrBlockSize = 1 << BlockOrders[BlockIndex];
  26. int NextBlockSize = CurrBlockSize;
  27. if (NextOrder != 0)
  28. {
  29. NextBlockSize = 1 << NextOrder;
  30. }
  31. ulong StartAligned = BitUtils.AlignDown(Address, NextBlockSize);
  32. ulong EndAddrAligned = BitUtils.AlignDown(EndAddr, CurrBlockSize);
  33. ulong SizeInBlocksTruncated = (EndAddrAligned - StartAligned) >> BlockOrders[BlockIndex];
  34. ulong EndAddrRounded = BitUtils.AlignUp(Address + Size, NextBlockSize);
  35. ulong SizeInBlocksRounded = (EndAddrRounded - StartAligned) >> BlockOrders[BlockIndex];
  36. Blocks[BlockIndex].StartAligned = StartAligned;
  37. Blocks[BlockIndex].SizeInBlocksTruncated = SizeInBlocksTruncated;
  38. Blocks[BlockIndex].SizeInBlocksRounded = SizeInBlocksRounded;
  39. ulong CurrSizeInBlocks = SizeInBlocksRounded;
  40. int MaxLevel = 0;
  41. do
  42. {
  43. MaxLevel++;
  44. }
  45. while ((CurrSizeInBlocks /= 64) != 0);
  46. Blocks[BlockIndex].MaxLevel = MaxLevel;
  47. Blocks[BlockIndex].Masks = new long[MaxLevel][];
  48. CurrSizeInBlocks = SizeInBlocksRounded;
  49. for (int Level = MaxLevel - 1; Level >= 0; Level--)
  50. {
  51. CurrSizeInBlocks = (CurrSizeInBlocks + 63) / 64;
  52. Blocks[BlockIndex].Masks[Level] = new long[CurrSizeInBlocks];
  53. }
  54. }
  55. if (Size != 0)
  56. {
  57. FreePages(Address, Size / KMemoryManager.PageSize);
  58. }
  59. }
  60. public KernelResult AllocatePages(ulong PagesCount, bool Backwards, out KPageList PageList)
  61. {
  62. lock (Blocks)
  63. {
  64. return AllocatePagesImpl(PagesCount, Backwards, out PageList);
  65. }
  66. }
  67. private KernelResult AllocatePagesImpl(ulong PagesCount, bool Backwards, out KPageList PageList)
  68. {
  69. PageList = new KPageList();
  70. if (BlockOrdersCount > 0)
  71. {
  72. if (GetFreePagesImpl() < PagesCount)
  73. {
  74. return KernelResult.OutOfMemory;
  75. }
  76. }
  77. else if (PagesCount != 0)
  78. {
  79. return KernelResult.OutOfMemory;
  80. }
  81. for (int BlockIndex = BlockOrdersCount - 1; BlockIndex >= 0; BlockIndex--)
  82. {
  83. KMemoryRegionBlock Block = Blocks[BlockIndex];
  84. ulong BestFitBlockSize = 1UL << Block.Order;
  85. ulong BlockPagesCount = BestFitBlockSize / KMemoryManager.PageSize;
  86. //Check if this is the best fit for this page size.
  87. //If so, try allocating as much requested pages as possible.
  88. while (BlockPagesCount <= PagesCount)
  89. {
  90. ulong Address = 0;
  91. for (int CurrBlockIndex = BlockIndex;
  92. CurrBlockIndex < BlockOrdersCount && Address == 0;
  93. CurrBlockIndex++)
  94. {
  95. Block = Blocks[CurrBlockIndex];
  96. int Index = 0;
  97. bool ZeroMask = false;
  98. for (int Level = 0; Level < Block.MaxLevel; Level++)
  99. {
  100. long Mask = Block.Masks[Level][Index];
  101. if (Mask == 0)
  102. {
  103. ZeroMask = true;
  104. break;
  105. }
  106. if (Backwards)
  107. {
  108. Index = (Index * 64 + 63) - BitUtils.CountLeadingZeros64(Mask);
  109. }
  110. else
  111. {
  112. Index = Index * 64 + BitUtils.CountLeadingZeros64(BitUtils.ReverseBits64(Mask));
  113. }
  114. }
  115. if (Block.SizeInBlocksTruncated <= (ulong)Index || ZeroMask)
  116. {
  117. continue;
  118. }
  119. Block.FreeCount--;
  120. int TempIdx = Index;
  121. for (int Level = Block.MaxLevel - 1; Level >= 0; Level--, TempIdx /= 64)
  122. {
  123. Block.Masks[Level][TempIdx / 64] &= ~(1L << (TempIdx & 63));
  124. if (Block.Masks[Level][TempIdx / 64] != 0)
  125. {
  126. break;
  127. }
  128. }
  129. Address = Block.StartAligned + ((ulong)Index << Block.Order);
  130. }
  131. for (int CurrBlockIndex = BlockIndex;
  132. CurrBlockIndex < BlockOrdersCount && Address == 0;
  133. CurrBlockIndex++)
  134. {
  135. Block = Blocks[CurrBlockIndex];
  136. int Index = 0;
  137. bool ZeroMask = false;
  138. for (int Level = 0; Level < Block.MaxLevel; Level++)
  139. {
  140. long Mask = Block.Masks[Level][Index];
  141. if (Mask == 0)
  142. {
  143. ZeroMask = true;
  144. break;
  145. }
  146. if (Backwards)
  147. {
  148. Index = Index * 64 + BitUtils.CountLeadingZeros64(BitUtils.ReverseBits64(Mask));
  149. }
  150. else
  151. {
  152. Index = (Index * 64 + 63) - BitUtils.CountLeadingZeros64(Mask);
  153. }
  154. }
  155. if (Block.SizeInBlocksTruncated <= (ulong)Index || ZeroMask)
  156. {
  157. continue;
  158. }
  159. Block.FreeCount--;
  160. int TempIdx = Index;
  161. for (int Level = Block.MaxLevel - 1; Level >= 0; Level--, TempIdx /= 64)
  162. {
  163. Block.Masks[Level][TempIdx / 64] &= ~(1L << (TempIdx & 63));
  164. if (Block.Masks[Level][TempIdx / 64] != 0)
  165. {
  166. break;
  167. }
  168. }
  169. Address = Block.StartAligned + ((ulong)Index << Block.Order);
  170. }
  171. //The address being zero means that no free space was found on that order,
  172. //just give up and try with the next one.
  173. if (Address == 0)
  174. {
  175. break;
  176. }
  177. //If we are using a larger order than best fit, then we should
  178. //split it into smaller blocks.
  179. ulong FirstFreeBlockSize = 1UL << Block.Order;
  180. if (FirstFreeBlockSize > BestFitBlockSize)
  181. {
  182. FreePages(Address + BestFitBlockSize, (FirstFreeBlockSize - BestFitBlockSize) / KMemoryManager.PageSize);
  183. }
  184. //Add new allocated page(s) to the pages list.
  185. //If an error occurs, then free all allocated pages and fail.
  186. KernelResult Result = PageList.AddRange(Address, BlockPagesCount);
  187. if (Result != KernelResult.Success)
  188. {
  189. FreePages(Address, BlockPagesCount);
  190. foreach (KPageNode PageNode in PageList)
  191. {
  192. FreePages(PageNode.Address, PageNode.PagesCount);
  193. }
  194. return Result;
  195. }
  196. PagesCount -= BlockPagesCount;
  197. }
  198. }
  199. //Success case, all requested pages were allocated successfully.
  200. if (PagesCount == 0)
  201. {
  202. return KernelResult.Success;
  203. }
  204. //Error case, free allocated pages and return out of memory.
  205. foreach (KPageNode PageNode in PageList)
  206. {
  207. FreePages(PageNode.Address, PageNode.PagesCount);
  208. }
  209. PageList = null;
  210. return KernelResult.OutOfMemory;
  211. }
  212. public void FreePages(KPageList PageList)
  213. {
  214. lock (Blocks)
  215. {
  216. foreach (KPageNode PageNode in PageList)
  217. {
  218. FreePages(PageNode.Address, PageNode.PagesCount);
  219. }
  220. }
  221. }
  222. private void FreePages(ulong Address, ulong PagesCount)
  223. {
  224. ulong EndAddr = Address + PagesCount * KMemoryManager.PageSize;
  225. int BlockIndex = BlockOrdersCount - 1;
  226. ulong AddressRounded = 0;
  227. ulong EndAddrTruncated = 0;
  228. for (; BlockIndex >= 0; BlockIndex--)
  229. {
  230. KMemoryRegionBlock AllocInfo = Blocks[BlockIndex];
  231. int BlockSize = 1 << AllocInfo.Order;
  232. AddressRounded = BitUtils.AlignUp (Address, BlockSize);
  233. EndAddrTruncated = BitUtils.AlignDown(EndAddr, BlockSize);
  234. if (AddressRounded < EndAddrTruncated)
  235. {
  236. break;
  237. }
  238. }
  239. void FreeRegion(ulong CurrAddress)
  240. {
  241. for (int CurrBlockIndex = BlockIndex;
  242. CurrBlockIndex < BlockOrdersCount && CurrAddress != 0;
  243. CurrBlockIndex++)
  244. {
  245. KMemoryRegionBlock Block = Blocks[CurrBlockIndex];
  246. Block.FreeCount++;
  247. ulong FreedBlocks = (CurrAddress - Block.StartAligned) >> Block.Order;
  248. int Index = (int)FreedBlocks;
  249. for (int Level = Block.MaxLevel - 1; Level >= 0; Level--, Index /= 64)
  250. {
  251. long Mask = Block.Masks[Level][Index / 64];
  252. Block.Masks[Level][Index / 64] = Mask | (1L << (Index & 63));
  253. if (Mask != 0)
  254. {
  255. break;
  256. }
  257. }
  258. int BlockSizeDelta = 1 << (Block.NextOrder - Block.Order);
  259. int FreedBlocksTruncated = BitUtils.AlignDown((int)FreedBlocks, BlockSizeDelta);
  260. if (!Block.TryCoalesce(FreedBlocksTruncated, BlockSizeDelta))
  261. {
  262. break;
  263. }
  264. CurrAddress = Block.StartAligned + ((ulong)FreedBlocksTruncated << Block.Order);
  265. }
  266. }
  267. //Free inside aligned region.
  268. ulong BaseAddress = AddressRounded;
  269. while (BaseAddress < EndAddrTruncated)
  270. {
  271. ulong BlockSize = 1UL << Blocks[BlockIndex].Order;
  272. FreeRegion(BaseAddress);
  273. BaseAddress += BlockSize;
  274. }
  275. int NextBlockIndex = BlockIndex - 1;
  276. //Free region between Address and aligned region start.
  277. BaseAddress = AddressRounded;
  278. for (BlockIndex = NextBlockIndex; BlockIndex >= 0; BlockIndex--)
  279. {
  280. ulong BlockSize = 1UL << Blocks[BlockIndex].Order;
  281. while (BaseAddress - BlockSize >= Address)
  282. {
  283. BaseAddress -= BlockSize;
  284. FreeRegion(BaseAddress);
  285. }
  286. }
  287. //Free region between aligned region end and End Address.
  288. BaseAddress = EndAddrTruncated;
  289. for (BlockIndex = NextBlockIndex; BlockIndex >= 0; BlockIndex--)
  290. {
  291. ulong BlockSize = 1UL << Blocks[BlockIndex].Order;
  292. while (BaseAddress + BlockSize <= EndAddr)
  293. {
  294. FreeRegion(BaseAddress);
  295. BaseAddress += BlockSize;
  296. }
  297. }
  298. }
  299. public ulong GetFreePages()
  300. {
  301. lock (Blocks)
  302. {
  303. return GetFreePagesImpl();
  304. }
  305. }
  306. private ulong GetFreePagesImpl()
  307. {
  308. ulong AvailablePages = 0;
  309. for (int BlockIndex = 0; BlockIndex < BlockOrdersCount; BlockIndex++)
  310. {
  311. KMemoryRegionBlock Block = Blocks[BlockIndex];
  312. ulong BlockPagesCount = (1UL << Block.Order) / KMemoryManager.PageSize;
  313. AvailablePages += BlockPagesCount * Block.FreeCount;
  314. }
  315. return AvailablePages;
  316. }
  317. }
  318. }