KProcessCapabilities.cs 9.2 KB

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