SyscallTable.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. using ARMeilleure.State;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Reflection;
  7. using System.Reflection.Emit;
  8. namespace Ryujinx.HLE.HOS.Kernel.SupervisorCall
  9. {
  10. static class SyscallTable
  11. {
  12. private const int SvcFuncMaxArguments64 = 8;
  13. private const int SvcFuncMaxArguments32 = 4;
  14. private const int SvcMax = 0x80;
  15. public static Action<Syscall32, ExecutionContext>[] SvcTable32 { get; }
  16. public static Action<Syscall64, ExecutionContext>[] SvcTable64 { get; }
  17. static SyscallTable()
  18. {
  19. SvcTable32 = new Action<Syscall32, ExecutionContext>[SvcMax];
  20. SvcTable64 = new Action<Syscall64, ExecutionContext>[SvcMax];
  21. Dictionary<int, string> svcFuncs64 = new Dictionary<int, string>
  22. {
  23. { 0x01, nameof(Syscall64.SetHeapSize64) },
  24. { 0x03, nameof(Syscall64.SetMemoryAttribute64) },
  25. { 0x04, nameof(Syscall64.MapMemory64) },
  26. { 0x05, nameof(Syscall64.UnmapMemory64) },
  27. { 0x06, nameof(Syscall64.QueryMemory64) },
  28. { 0x07, nameof(Syscall64.ExitProcess64) },
  29. { 0x08, nameof(Syscall64.CreateThread64) },
  30. { 0x09, nameof(Syscall64.StartThread64) },
  31. { 0x0a, nameof(Syscall64.ExitThread64) },
  32. { 0x0b, nameof(Syscall64.SleepThread64) },
  33. { 0x0c, nameof(Syscall64.GetThreadPriority64) },
  34. { 0x0d, nameof(Syscall64.SetThreadPriority64) },
  35. { 0x0e, nameof(Syscall64.GetThreadCoreMask64) },
  36. { 0x0f, nameof(Syscall64.SetThreadCoreMask64) },
  37. { 0x10, nameof(Syscall64.GetCurrentProcessorNumber64) },
  38. { 0x11, nameof(Syscall64.SignalEvent64) },
  39. { 0x12, nameof(Syscall64.ClearEvent64) },
  40. { 0x13, nameof(Syscall64.MapSharedMemory64) },
  41. { 0x14, nameof(Syscall64.UnmapSharedMemory64) },
  42. { 0x15, nameof(Syscall64.CreateTransferMemory64) },
  43. { 0x16, nameof(Syscall64.CloseHandle64) },
  44. { 0x17, nameof(Syscall64.ResetSignal64) },
  45. { 0x18, nameof(Syscall64.WaitSynchronization64) },
  46. { 0x19, nameof(Syscall64.CancelSynchronization64) },
  47. { 0x1a, nameof(Syscall64.ArbitrateLock64) },
  48. { 0x1b, nameof(Syscall64.ArbitrateUnlock64) },
  49. { 0x1c, nameof(Syscall64.WaitProcessWideKeyAtomic64) },
  50. { 0x1d, nameof(Syscall64.SignalProcessWideKey64) },
  51. { 0x1e, nameof(Syscall64.GetSystemTick64) },
  52. { 0x1f, nameof(Syscall64.ConnectToNamedPort64) },
  53. { 0x21, nameof(Syscall64.SendSyncRequest64) },
  54. { 0x22, nameof(Syscall64.SendSyncRequestWithUserBuffer64) },
  55. { 0x24, nameof(Syscall64.GetProcessId64) },
  56. { 0x25, nameof(Syscall64.GetThreadId64) },
  57. { 0x26, nameof(Syscall64.Break64) },
  58. { 0x27, nameof(Syscall64.OutputDebugString64) },
  59. { 0x29, nameof(Syscall64.GetInfo64) },
  60. { 0x2c, nameof(Syscall64.MapPhysicalMemory64) },
  61. { 0x2d, nameof(Syscall64.UnmapPhysicalMemory64) },
  62. { 0x32, nameof(Syscall64.SetThreadActivity64) },
  63. { 0x33, nameof(Syscall64.GetThreadContext364) },
  64. { 0x34, nameof(Syscall64.WaitForAddress64) },
  65. { 0x35, nameof(Syscall64.SignalToAddress64) },
  66. { 0x40, nameof(Syscall64.CreateSession64) },
  67. { 0x41, nameof(Syscall64.AcceptSession64) },
  68. { 0x43, nameof(Syscall64.ReplyAndReceive64) },
  69. { 0x45, nameof(Syscall64.CreateEvent64) },
  70. { 0x65, nameof(Syscall64.GetProcessList64) },
  71. { 0x6f, nameof(Syscall64.GetSystemInfo64) },
  72. { 0x70, nameof(Syscall64.CreatePort64) },
  73. { 0x71, nameof(Syscall64.ManageNamedPort64) },
  74. { 0x72, nameof(Syscall64.ConnectToPort64) },
  75. { 0x73, nameof(Syscall64.SetProcessMemoryPermission64) },
  76. { 0x77, nameof(Syscall64.MapProcessCodeMemory64) },
  77. { 0x78, nameof(Syscall64.UnmapProcessCodeMemory64) },
  78. { 0x7B, nameof(Syscall64.TerminateProcess64) }
  79. };
  80. foreach (KeyValuePair<int, string> value in svcFuncs64)
  81. {
  82. SvcTable64[value.Key] = GenerateMethod<Syscall64>(value.Value, SvcFuncMaxArguments64);
  83. }
  84. Dictionary<int, string> svcFuncs32 = new Dictionary<int, string>
  85. {
  86. { 0x01, nameof(Syscall32.SetHeapSize32) },
  87. { 0x03, nameof(Syscall32.SetMemoryAttribute32) },
  88. { 0x04, nameof(Syscall32.MapMemory32) },
  89. { 0x05, nameof(Syscall32.UnmapMemory32) },
  90. { 0x06, nameof(Syscall32.QueryMemory32) },
  91. { 0x07, nameof(Syscall32.ExitProcess32) },
  92. { 0x08, nameof(Syscall32.CreateThread32) },
  93. { 0x09, nameof(Syscall32.StartThread32) },
  94. { 0x0a, nameof(Syscall32.ExitThread32) },
  95. { 0x0b, nameof(Syscall32.SleepThread32) },
  96. { 0x0c, nameof(Syscall32.GetThreadPriority32) },
  97. { 0x0d, nameof(Syscall32.SetThreadPriority32) },
  98. { 0x0e, nameof(Syscall32.GetThreadCoreMask32) },
  99. { 0x0f, nameof(Syscall32.SetThreadCoreMask32) },
  100. { 0x10, nameof(Syscall32.GetCurrentProcessorNumber32) },
  101. { 0x11, nameof(Syscall32.SignalEvent32) },
  102. { 0x12, nameof(Syscall32.ClearEvent32) },
  103. { 0x13, nameof(Syscall32.MapSharedMemory32) },
  104. { 0x14, nameof(Syscall32.UnmapSharedMemory32) },
  105. { 0x15, nameof(Syscall32.CreateTransferMemory32) },
  106. { 0x16, nameof(Syscall32.CloseHandle32) },
  107. { 0x17, nameof(Syscall32.ResetSignal32) },
  108. { 0x18, nameof(Syscall32.WaitSynchronization32) },
  109. { 0x19, nameof(Syscall32.CancelSynchronization32) },
  110. { 0x1a, nameof(Syscall32.ArbitrateLock32) },
  111. { 0x1b, nameof(Syscall32.ArbitrateUnlock32) },
  112. { 0x1c, nameof(Syscall32.WaitProcessWideKeyAtomic32) },
  113. { 0x1d, nameof(Syscall32.SignalProcessWideKey32) },
  114. { 0x1e, nameof(Syscall32.GetSystemTick32) },
  115. { 0x1f, nameof(Syscall32.ConnectToNamedPort32) },
  116. { 0x21, nameof(Syscall32.SendSyncRequest32) },
  117. { 0x22, nameof(Syscall32.SendSyncRequestWithUserBuffer32) },
  118. { 0x24, nameof(Syscall32.GetProcessId32) },
  119. { 0x25, nameof(Syscall32.GetThreadId32) },
  120. { 0x26, nameof(Syscall32.Break32) },
  121. { 0x27, nameof(Syscall32.OutputDebugString32) },
  122. { 0x29, nameof(Syscall32.GetInfo32) },
  123. { 0x2c, nameof(Syscall32.MapPhysicalMemory32) },
  124. { 0x2d, nameof(Syscall32.UnmapPhysicalMemory32) },
  125. { 0x32, nameof(Syscall32.SetThreadActivity32) },
  126. { 0x33, nameof(Syscall32.GetThreadContext332) },
  127. { 0x34, nameof(Syscall32.WaitForAddress32) },
  128. { 0x35, nameof(Syscall32.SignalToAddress32) },
  129. { 0x40, nameof(Syscall32.CreateSession32) },
  130. { 0x41, nameof(Syscall32.AcceptSession32) },
  131. { 0x43, nameof(Syscall32.ReplyAndReceive32) },
  132. { 0x45, nameof(Syscall32.CreateEvent32) },
  133. { 0x5F, nameof(Syscall32.FlushProcessDataCache32) },
  134. { 0x65, nameof(Syscall32.GetProcessList32) },
  135. { 0x6f, nameof(Syscall32.GetSystemInfo32) },
  136. { 0x70, nameof(Syscall32.CreatePort32) },
  137. { 0x71, nameof(Syscall32.ManageNamedPort32) },
  138. { 0x72, nameof(Syscall32.ConnectToPort32) },
  139. { 0x73, nameof(Syscall32.SetProcessMemoryPermission32) },
  140. { 0x77, nameof(Syscall32.MapProcessCodeMemory32) },
  141. { 0x78, nameof(Syscall32.UnmapProcessCodeMemory32) },
  142. { 0x7B, nameof(Syscall32.TerminateProcess32) }
  143. };
  144. foreach (KeyValuePair<int, string> value in svcFuncs32)
  145. {
  146. SvcTable32[value.Key] = GenerateMethod<Syscall32>(value.Value, SvcFuncMaxArguments32);
  147. }
  148. }
  149. private static Action<T, ExecutionContext> GenerateMethod<T>(string svcName, int registerCleanCount)
  150. {
  151. Type[] argTypes = new Type[] { typeof(T), typeof(ExecutionContext) };
  152. DynamicMethod method = new DynamicMethod(svcName, null, argTypes);
  153. MethodInfo methodInfo = typeof(T).GetMethod(svcName);
  154. ParameterInfo[] methodArgs = methodInfo.GetParameters();
  155. ILGenerator generator = method.GetILGenerator();
  156. void ConvertToArgType(Type sourceType)
  157. {
  158. CheckIfTypeIsSupported(sourceType, svcName);
  159. switch (Type.GetTypeCode(sourceType))
  160. {
  161. case TypeCode.UInt32: generator.Emit(OpCodes.Conv_U4); break;
  162. case TypeCode.Int32: generator.Emit(OpCodes.Conv_I4); break;
  163. case TypeCode.UInt16: generator.Emit(OpCodes.Conv_U2); break;
  164. case TypeCode.Int16: generator.Emit(OpCodes.Conv_I2); break;
  165. case TypeCode.Byte: generator.Emit(OpCodes.Conv_U1); break;
  166. case TypeCode.SByte: generator.Emit(OpCodes.Conv_I1); break;
  167. case TypeCode.Boolean:
  168. generator.Emit(OpCodes.Conv_I4);
  169. generator.Emit(OpCodes.Ldc_I4_1);
  170. generator.Emit(OpCodes.And);
  171. break;
  172. }
  173. }
  174. void ConvertToFieldType(Type sourceType)
  175. {
  176. CheckIfTypeIsSupported(sourceType, svcName);
  177. switch (Type.GetTypeCode(sourceType))
  178. {
  179. case TypeCode.UInt32:
  180. case TypeCode.Int32:
  181. case TypeCode.UInt16:
  182. case TypeCode.Int16:
  183. case TypeCode.Byte:
  184. case TypeCode.SByte:
  185. case TypeCode.Boolean:
  186. generator.Emit(OpCodes.Conv_U8);
  187. break;
  188. }
  189. }
  190. RAttribute GetRegisterAttribute(ParameterInfo parameterInfo)
  191. {
  192. RAttribute argumentAttribute = (RAttribute)parameterInfo.GetCustomAttribute(typeof(RAttribute));
  193. if (argumentAttribute == null)
  194. {
  195. throw new InvalidOperationException($"Method \"{svcName}\" is missing a {typeof(RAttribute).Name} attribute on parameter \"{parameterInfo.Name}\"");
  196. }
  197. return argumentAttribute;
  198. }
  199. // For functions returning output values, the first registers
  200. // are used to hold pointers where the value will be stored,
  201. // so they can't be used to pass argument and we must
  202. // skip them.
  203. int byRefArgsCount = 0;
  204. for (int index = 0; index < methodArgs.Length; index++)
  205. {
  206. if (methodArgs[index].ParameterType.IsByRef)
  207. {
  208. byRefArgsCount++;
  209. }
  210. }
  211. BindingFlags staticNonPublic = BindingFlags.NonPublic | BindingFlags.Static;
  212. // Print all the arguments for debugging purposes.
  213. int inputArgsCount = methodArgs.Length - byRefArgsCount;
  214. if (inputArgsCount != 0)
  215. {
  216. generator.Emit(OpCodes.Ldc_I4, inputArgsCount);
  217. generator.Emit(OpCodes.Newarr, typeof(object));
  218. string argsFormat = svcName;
  219. for (int index = 0; index < methodArgs.Length; index++)
  220. {
  221. Type argType = methodArgs[index].ParameterType;
  222. // Ignore out argument for printing
  223. if (argType.IsByRef)
  224. {
  225. continue;
  226. }
  227. RAttribute registerAttribute = GetRegisterAttribute(methodArgs[index]);
  228. argsFormat += $" {methodArgs[index].Name}: 0x{{{index}:X8}},";
  229. generator.Emit(OpCodes.Dup);
  230. generator.Emit(OpCodes.Ldc_I4, index);
  231. generator.Emit(OpCodes.Ldarg_1);
  232. generator.Emit(OpCodes.Ldc_I4, registerAttribute.Index);
  233. MethodInfo info = typeof(ExecutionContext).GetMethod(nameof(ExecutionContext.GetX));
  234. generator.Emit(OpCodes.Call, info);
  235. generator.Emit(OpCodes.Box, typeof(ulong));
  236. generator.Emit(OpCodes.Stelem_Ref);
  237. }
  238. argsFormat = argsFormat.Substring(0, argsFormat.Length - 1);
  239. generator.Emit(OpCodes.Ldstr, argsFormat);
  240. }
  241. else
  242. {
  243. generator.Emit(OpCodes.Ldnull);
  244. generator.Emit(OpCodes.Ldstr, svcName);
  245. }
  246. MethodInfo printArgsMethod = typeof(SyscallTable).GetMethod(nameof(PrintArguments), staticNonPublic);
  247. generator.Emit(OpCodes.Call, printArgsMethod);
  248. // Call the SVC function handler.
  249. generator.Emit(OpCodes.Ldarg_0);
  250. List<(LocalBuilder, RAttribute)> locals = new List<(LocalBuilder, RAttribute)>();
  251. for (int index = 0; index < methodArgs.Length; index++)
  252. {
  253. Type argType = methodArgs[index].ParameterType;
  254. RAttribute registerAttribute = GetRegisterAttribute(methodArgs[index]);
  255. if (argType.IsByRef)
  256. {
  257. argType = argType.GetElementType();
  258. LocalBuilder local = generator.DeclareLocal(argType);
  259. locals.Add((local, registerAttribute));
  260. if (!methodArgs[index].IsOut)
  261. {
  262. generator.Emit(OpCodes.Ldarg_1);
  263. generator.Emit(OpCodes.Ldc_I4, registerAttribute.Index);
  264. MethodInfo info = typeof(ExecutionContext).GetMethod(nameof(ExecutionContext.GetX));
  265. generator.Emit(OpCodes.Call, info);
  266. ConvertToArgType(argType);
  267. generator.Emit(OpCodes.Stloc, local);
  268. }
  269. generator.Emit(OpCodes.Ldloca, local);
  270. }
  271. else
  272. {
  273. generator.Emit(OpCodes.Ldarg_1);
  274. generator.Emit(OpCodes.Ldc_I4, registerAttribute.Index);
  275. MethodInfo info = typeof(ExecutionContext).GetMethod(nameof(ExecutionContext.GetX));
  276. generator.Emit(OpCodes.Call, info);
  277. ConvertToArgType(argType);
  278. }
  279. }
  280. generator.Emit(OpCodes.Call, methodInfo);
  281. Type retType = methodInfo.ReturnType;
  282. // Print result code.
  283. if (retType == typeof(KernelResult))
  284. {
  285. MethodInfo printResultMethod = typeof(SyscallTable).GetMethod(nameof(PrintResult), staticNonPublic);
  286. generator.Emit(OpCodes.Dup);
  287. generator.Emit(OpCodes.Ldstr, svcName);
  288. generator.Emit(OpCodes.Call, printResultMethod);
  289. }
  290. uint registerInUse = 0;
  291. // Save return value into register X0 (when the method has a return value).
  292. if (retType != typeof(void))
  293. {
  294. CheckIfTypeIsSupported(retType, svcName);
  295. LocalBuilder tempLocal = generator.DeclareLocal(retType);
  296. generator.Emit(OpCodes.Stloc, tempLocal);
  297. generator.Emit(OpCodes.Ldarg_1);
  298. generator.Emit(OpCodes.Ldc_I4, 0);
  299. generator.Emit(OpCodes.Ldloc, tempLocal);
  300. ConvertToFieldType(retType);
  301. MethodInfo info = typeof(ExecutionContext).GetMethod(nameof(ExecutionContext.SetX));
  302. generator.Emit(OpCodes.Call, info);
  303. registerInUse |= 1u << 0;
  304. }
  305. for (int index = 0; index < locals.Count; index++)
  306. {
  307. (LocalBuilder local, RAttribute attribute) = locals[index];
  308. if ((registerInUse & (1u << attribute.Index)) != 0)
  309. {
  310. throw new InvalidSvcException($"Method \"{svcName}\" has conflicting output values at register index \"{attribute.Index}\".");
  311. }
  312. generator.Emit(OpCodes.Ldarg_1);
  313. generator.Emit(OpCodes.Ldc_I4, attribute.Index);
  314. generator.Emit(OpCodes.Ldloc, local);
  315. ConvertToFieldType(local.LocalType);
  316. MethodInfo info = typeof(ExecutionContext).GetMethod(nameof(ExecutionContext.SetX));
  317. generator.Emit(OpCodes.Call, info);
  318. registerInUse |= 1u << attribute.Index;
  319. }
  320. // Zero out the remaining unused registers.
  321. for (int i = 0; i < registerCleanCount; i++)
  322. {
  323. if ((registerInUse & (1u << i)) != 0)
  324. {
  325. continue;
  326. }
  327. generator.Emit(OpCodes.Ldarg_1);
  328. generator.Emit(OpCodes.Ldc_I4, i);
  329. generator.Emit(OpCodes.Ldc_I8, 0L);
  330. MethodInfo info = typeof(ExecutionContext).GetMethod(nameof(ExecutionContext.SetX));
  331. generator.Emit(OpCodes.Call, info);
  332. }
  333. generator.Emit(OpCodes.Ret);
  334. return (Action<T, ExecutionContext>)method.CreateDelegate(typeof(Action<T, ExecutionContext>));
  335. }
  336. private static void CheckIfTypeIsSupported(Type type, string svcName)
  337. {
  338. switch (Type.GetTypeCode(type))
  339. {
  340. case TypeCode.UInt64:
  341. case TypeCode.Int64:
  342. case TypeCode.UInt32:
  343. case TypeCode.Int32:
  344. case TypeCode.UInt16:
  345. case TypeCode.Int16:
  346. case TypeCode.Byte:
  347. case TypeCode.SByte:
  348. case TypeCode.Boolean:
  349. return;
  350. }
  351. throw new InvalidSvcException($"Method \"{svcName}\" has a invalid ref type \"{type.Name}\".");
  352. }
  353. private static void PrintArguments(object[] argValues, string formatOrSvcName)
  354. {
  355. if (argValues != null)
  356. {
  357. Logger.PrintDebug(LogClass.KernelSvc, string.Format(formatOrSvcName, argValues));
  358. }
  359. else
  360. {
  361. Logger.PrintDebug(LogClass.KernelSvc, formatOrSvcName);
  362. }
  363. }
  364. private static void PrintResult(KernelResult result, string svcName)
  365. {
  366. if (result != KernelResult.Success &&
  367. result != KernelResult.TimedOut &&
  368. result != KernelResult.Cancelled &&
  369. result != KernelResult.InvalidState)
  370. {
  371. Logger.PrintWarning(LogClass.KernelSvc, $"{svcName} returned error {result}.");
  372. }
  373. else
  374. {
  375. Logger.PrintDebug(LogClass.KernelSvc, $"{svcName} returned result {result}.");
  376. }
  377. }
  378. }
  379. }