SyscallTable.cs 23 KB

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