KProcessCapabilities.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using Ryujinx.HLE.HOS.Kernel.Common;
  2. using Ryujinx.HLE.HOS.Kernel.Memory;
  3. using Ryujinx.HLE.HOS.Kernel.Threading;
  4. using Ryujinx.Horizon.Common;
  5. using System;
  6. using System.Numerics;
  7. namespace Ryujinx.HLE.HOS.Kernel.Process
  8. {
  9. class KProcessCapabilities
  10. {
  11. public byte[] SvcAccessMask { get; private set; }
  12. public byte[] IrqAccessMask { get; private set; }
  13. public ulong AllowedCpuCoresMask { get; private set; }
  14. public ulong AllowedThreadPriosMask { get; private set; }
  15. public int DebuggingFlags { get; private set; }
  16. public int HandleTableSize { get; private set; }
  17. public int KernelReleaseVersion { get; private set; }
  18. public int ApplicationType { get; private set; }
  19. public KProcessCapabilities()
  20. {
  21. SvcAccessMask = new byte[0x10];
  22. IrqAccessMask = new byte[0x80];
  23. }
  24. public Result InitializeForKernel(ReadOnlySpan<int> capabilities, KPageTableBase memoryManager)
  25. {
  26. AllowedCpuCoresMask = 0xf;
  27. AllowedThreadPriosMask = ulong.MaxValue;
  28. DebuggingFlags &= ~3;
  29. KernelReleaseVersion = KProcess.KernelVersionPacked;
  30. return Parse(capabilities, memoryManager);
  31. }
  32. public Result InitializeForUser(ReadOnlySpan<int> capabilities, KPageTableBase memoryManager)
  33. {
  34. return Parse(capabilities, memoryManager);
  35. }
  36. private Result Parse(ReadOnlySpan<int> capabilities, KPageTableBase memoryManager)
  37. {
  38. int mask0 = 0;
  39. int mask1 = 0;
  40. for (int index = 0; index < capabilities.Length; index++)
  41. {
  42. int cap = capabilities[index];
  43. if (((cap + 1) & ~cap) != 0x40)
  44. {
  45. Result result = ParseCapability(cap, ref mask0, ref mask1, memoryManager);
  46. if (result != Result.Success)
  47. {
  48. return result;
  49. }
  50. }
  51. else
  52. {
  53. if ((uint)index + 1 >= capabilities.Length)
  54. {
  55. return KernelResult.InvalidCombination;
  56. }
  57. int prevCap = cap;
  58. cap = capabilities[++index];
  59. if (((cap + 1) & ~cap) != 0x40)
  60. {
  61. return KernelResult.InvalidCombination;
  62. }
  63. if ((cap & 0x78000000) != 0)
  64. {
  65. return KernelResult.MaximumExceeded;
  66. }
  67. if ((cap & 0x7ffff80) == 0)
  68. {
  69. return KernelResult.InvalidSize;
  70. }
  71. long address = ((long)(uint)prevCap << 5) & 0xffffff000;
  72. long size = ((long)(uint)cap << 5) & 0xfffff000;
  73. if (((ulong)(address + size - 1) >> 36) != 0)
  74. {
  75. return KernelResult.InvalidAddress;
  76. }
  77. KMemoryPermission perm = (prevCap >> 31) != 0
  78. ? KMemoryPermission.Read
  79. : KMemoryPermission.ReadAndWrite;
  80. Result result;
  81. if ((cap >> 31) != 0)
  82. {
  83. result = memoryManager.MapNormalMemory(address, size, perm);
  84. }
  85. else
  86. {
  87. result = memoryManager.MapIoMemory(address, size, perm);
  88. }
  89. if (result != Result.Success)
  90. {
  91. return result;
  92. }
  93. }
  94. }
  95. return Result.Success;
  96. }
  97. private Result ParseCapability(int cap, ref int mask0, ref int mask1, KPageTableBase memoryManager)
  98. {
  99. int code = (cap + 1) & ~cap;
  100. if (code == 1)
  101. {
  102. return KernelResult.InvalidCapability;
  103. }
  104. else if (code == 0)
  105. {
  106. return Result.Success;
  107. }
  108. int codeMask = 1 << (32 - BitOperations.LeadingZeroCount((uint)code + 1));
  109. // Check if the property was already set.
  110. if (((mask0 & codeMask) & 0x1e008) != 0)
  111. {
  112. return KernelResult.InvalidCombination;
  113. }
  114. mask0 |= codeMask;
  115. switch (code)
  116. {
  117. case 8:
  118. {
  119. if (AllowedCpuCoresMask != 0 || AllowedThreadPriosMask != 0)
  120. {
  121. return KernelResult.InvalidCapability;
  122. }
  123. int lowestCpuCore = (cap >> 16) & 0xff;
  124. int highestCpuCore = (cap >> 24) & 0xff;
  125. if (lowestCpuCore > highestCpuCore)
  126. {
  127. return KernelResult.InvalidCombination;
  128. }
  129. int highestThreadPrio = (cap >> 4) & 0x3f;
  130. int lowestThreadPrio = (cap >> 10) & 0x3f;
  131. if (lowestThreadPrio > highestThreadPrio)
  132. {
  133. return KernelResult.InvalidCombination;
  134. }
  135. if (highestCpuCore >= KScheduler.CpuCoresCount)
  136. {
  137. return KernelResult.InvalidCpuCore;
  138. }
  139. AllowedCpuCoresMask = GetMaskFromMinMax(lowestCpuCore, highestCpuCore);
  140. AllowedThreadPriosMask = GetMaskFromMinMax(lowestThreadPrio, highestThreadPrio);
  141. break;
  142. }
  143. case 0x10:
  144. {
  145. int slot = (cap >> 29) & 7;
  146. int svcSlotMask = 1 << slot;
  147. if ((mask1 & svcSlotMask) != 0)
  148. {
  149. return KernelResult.InvalidCombination;
  150. }
  151. mask1 |= svcSlotMask;
  152. int svcMask = (cap >> 5) & 0xffffff;
  153. int baseSvc = slot * 24;
  154. for (int index = 0; index < 24; index++)
  155. {
  156. if (((svcMask >> index) & 1) == 0)
  157. {
  158. continue;
  159. }
  160. int svcId = baseSvc + index;
  161. if (svcId > 0x7f)
  162. {
  163. return KernelResult.MaximumExceeded;
  164. }
  165. SvcAccessMask[svcId / 8] |= (byte)(1 << (svcId & 7));
  166. }
  167. break;
  168. }
  169. case 0x80:
  170. {
  171. long address = ((long)(uint)cap << 4) & 0xffffff000;
  172. memoryManager.MapIoMemory(address, KPageTableBase.PageSize, KMemoryPermission.ReadAndWrite);
  173. break;
  174. }
  175. case 0x800:
  176. {
  177. // TODO: GIC distributor check.
  178. int irq0 = (cap >> 12) & 0x3ff;
  179. int irq1 = (cap >> 22) & 0x3ff;
  180. if (irq0 != 0x3ff)
  181. {
  182. IrqAccessMask[irq0 / 8] |= (byte)(1 << (irq0 & 7));
  183. }
  184. if (irq1 != 0x3ff)
  185. {
  186. IrqAccessMask[irq1 / 8] |= (byte)(1 << (irq1 & 7));
  187. }
  188. break;
  189. }
  190. case 0x2000:
  191. {
  192. int applicationType = cap >> 14;
  193. if ((uint)applicationType > 7)
  194. {
  195. return KernelResult.ReservedValue;
  196. }
  197. ApplicationType = applicationType;
  198. break;
  199. }
  200. case 0x4000:
  201. {
  202. // Note: This check is bugged on kernel too, we are just replicating the bug here.
  203. if ((KernelReleaseVersion >> 17) != 0 || cap < 0x80000)
  204. {
  205. return KernelResult.ReservedValue;
  206. }
  207. KernelReleaseVersion = cap;
  208. break;
  209. }
  210. case 0x8000:
  211. {
  212. int handleTableSize = cap >> 26;
  213. if ((uint)handleTableSize > 0x3ff)
  214. {
  215. return KernelResult.ReservedValue;
  216. }
  217. HandleTableSize = handleTableSize;
  218. break;
  219. }
  220. case 0x10000:
  221. {
  222. int debuggingFlags = cap >> 19;
  223. if ((uint)debuggingFlags > 3)
  224. {
  225. return KernelResult.ReservedValue;
  226. }
  227. DebuggingFlags &= ~3;
  228. DebuggingFlags |= debuggingFlags;
  229. break;
  230. }
  231. default: return KernelResult.InvalidCapability;
  232. }
  233. return Result.Success;
  234. }
  235. private static ulong GetMaskFromMinMax(int min, int max)
  236. {
  237. int range = max - min + 1;
  238. if (range == 64)
  239. {
  240. return ulong.MaxValue;
  241. }
  242. ulong mask = (1UL << range) - 1;
  243. return mask << min;
  244. }
  245. }
  246. }