KProcessCapabilities.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using Ryujinx.HLE.HOS.Kernel.Memory;
  2. using Ryujinx.HLE.HOS.Kernel.Threading;
  3. using Ryujinx.Horizon.Common;
  4. using System;
  5. using System.Numerics;
  6. namespace Ryujinx.HLE.HOS.Kernel.Process
  7. {
  8. class KProcessCapabilities
  9. {
  10. public byte[] SvcAccessMask { get; }
  11. public byte[] IrqAccessMask { get; }
  12. public ulong AllowedCpuCoresMask { get; private set; }
  13. public ulong AllowedThreadPriosMask { get; private set; }
  14. public uint DebuggingFlags { get; private set; }
  15. public uint HandleTableSize { get; private set; }
  16. public uint KernelReleaseVersion { get; private set; }
  17. public uint ApplicationType { get; private set; }
  18. public KProcessCapabilities()
  19. {
  20. // length / number of bits of the underlying type
  21. SvcAccessMask = new byte[KernelConstants.SupervisorCallCount / 8];
  22. IrqAccessMask = new byte[0x80];
  23. }
  24. public Result InitializeForKernel(ReadOnlySpan<uint> capabilities, KPageTableBase memoryManager)
  25. {
  26. AllowedCpuCoresMask = 0xf;
  27. AllowedThreadPriosMask = ulong.MaxValue;
  28. DebuggingFlags &= ~3u;
  29. KernelReleaseVersion = KProcess.KernelVersionPacked;
  30. return Parse(capabilities, memoryManager);
  31. }
  32. public Result InitializeForUser(ReadOnlySpan<uint> capabilities, KPageTableBase memoryManager)
  33. {
  34. return Parse(capabilities, memoryManager);
  35. }
  36. private Result Parse(ReadOnlySpan<uint> capabilities, KPageTableBase memoryManager)
  37. {
  38. int mask0 = 0;
  39. int mask1 = 0;
  40. for (int index = 0; index < capabilities.Length; index++)
  41. {
  42. uint cap = capabilities[index];
  43. if (cap.GetCapabilityType() != CapabilityType.MapRange)
  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. uint 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)prevCap << 5) & 0xffffff000;
  72. long size = ((long)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(uint cap, ref int mask0, ref int mask1, KPageTableBase memoryManager)
  98. {
  99. CapabilityType code = cap.GetCapabilityType();
  100. if (code == CapabilityType.Invalid)
  101. {
  102. return KernelResult.InvalidCapability;
  103. }
  104. else if (code == CapabilityType.Padding)
  105. {
  106. return Result.Success;
  107. }
  108. int codeMask = 1 << (32 - BitOperations.LeadingZeroCount(code.GetFlag() + 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 CapabilityType.CorePriority:
  118. {
  119. if (AllowedCpuCoresMask != 0 || AllowedThreadPriosMask != 0)
  120. {
  121. return KernelResult.InvalidCapability;
  122. }
  123. uint lowestCpuCore = (cap >> 16) & 0xff;
  124. uint highestCpuCore = (cap >> 24) & 0xff;
  125. if (lowestCpuCore > highestCpuCore)
  126. {
  127. return KernelResult.InvalidCombination;
  128. }
  129. uint highestThreadPrio = (cap >> 4) & 0x3f;
  130. uint 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 CapabilityType.SyscallMask:
  144. {
  145. int slot = ((int)cap >> 29) & 7;
  146. int svcSlotMask = 1 << slot;
  147. if ((mask1 & svcSlotMask) != 0)
  148. {
  149. return KernelResult.InvalidCombination;
  150. }
  151. mask1 |= svcSlotMask;
  152. uint 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 >= KernelConstants.SupervisorCallCount)
  162. {
  163. return KernelResult.MaximumExceeded;
  164. }
  165. SvcAccessMask[svcId / 8] |= (byte)(1 << (svcId & 7));
  166. }
  167. break;
  168. }
  169. case CapabilityType.MapIoPage:
  170. {
  171. long address = ((long)cap << 4) & 0xffffff000;
  172. memoryManager.MapIoMemory(address, KPageTableBase.PageSize, KMemoryPermission.ReadAndWrite);
  173. break;
  174. }
  175. case CapabilityType.MapRegion:
  176. {
  177. // TODO: Implement capabilities for MapRegion
  178. break;
  179. }
  180. case CapabilityType.InterruptPair:
  181. {
  182. // TODO: GIC distributor check.
  183. int irq0 = ((int)cap >> 12) & 0x3ff;
  184. int irq1 = ((int)cap >> 22) & 0x3ff;
  185. if (irq0 != 0x3ff)
  186. {
  187. IrqAccessMask[irq0 / 8] |= (byte)(1 << (irq0 & 7));
  188. }
  189. if (irq1 != 0x3ff)
  190. {
  191. IrqAccessMask[irq1 / 8] |= (byte)(1 << (irq1 & 7));
  192. }
  193. break;
  194. }
  195. case CapabilityType.ProgramType:
  196. {
  197. uint applicationType = (cap >> 14);
  198. if (applicationType > 7)
  199. {
  200. return KernelResult.ReservedValue;
  201. }
  202. ApplicationType = applicationType;
  203. break;
  204. }
  205. case CapabilityType.KernelVersion:
  206. {
  207. // Note: This check is bugged on kernel too, we are just replicating the bug here.
  208. if ((KernelReleaseVersion >> 17) != 0 || cap < 0x80000)
  209. {
  210. return KernelResult.ReservedValue;
  211. }
  212. KernelReleaseVersion = cap;
  213. break;
  214. }
  215. case CapabilityType.HandleTable:
  216. {
  217. uint handleTableSize = cap >> 26;
  218. if (handleTableSize > 0x3ff)
  219. {
  220. return KernelResult.ReservedValue;
  221. }
  222. HandleTableSize = handleTableSize;
  223. break;
  224. }
  225. case CapabilityType.DebugFlags:
  226. {
  227. uint debuggingFlags = cap >> 19;
  228. if (debuggingFlags > 3)
  229. {
  230. return KernelResult.ReservedValue;
  231. }
  232. DebuggingFlags &= ~3u;
  233. DebuggingFlags |= debuggingFlags;
  234. break;
  235. }
  236. default: return KernelResult.InvalidCapability;
  237. }
  238. return Result.Success;
  239. }
  240. private static ulong GetMaskFromMinMax(uint min, uint max)
  241. {
  242. uint range = max - min + 1;
  243. if (range == 64)
  244. {
  245. return ulong.MaxValue;
  246. }
  247. ulong mask = (1UL << (int)range) - 1;
  248. return mask << (int)min;
  249. }
  250. }
  251. }