SvcHandler.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using ChocolArm64.Events;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  4. using Ryujinx.HLE.Logging;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Threading;
  9. namespace Ryujinx.HLE.HOS.Kernel
  10. {
  11. partial class SvcHandler
  12. {
  13. private delegate void SvcFunc(AThreadState ThreadState);
  14. private Dictionary<int, SvcFunc> SvcFuncs;
  15. private Switch Device;
  16. private Process Process;
  17. private AMemory Memory;
  18. private ConcurrentDictionary<KThread, AutoResetEvent> SyncWaits;
  19. private const uint SelfThreadHandle = 0xffff8000;
  20. private const uint SelfProcessHandle = 0xffff8001;
  21. private static Random Rng;
  22. public SvcHandler(Switch Device, Process Process)
  23. {
  24. SvcFuncs = new Dictionary<int, SvcFunc>()
  25. {
  26. { 0x01, SvcSetHeapSize },
  27. { 0x03, SvcSetMemoryAttribute },
  28. { 0x04, SvcMapMemory },
  29. { 0x05, SvcUnmapMemory },
  30. { 0x06, SvcQueryMemory },
  31. { 0x07, SvcExitProcess },
  32. { 0x08, SvcCreateThread },
  33. { 0x09, SvcStartThread },
  34. { 0x0a, SvcExitThread },
  35. { 0x0b, SvcSleepThread },
  36. { 0x0c, SvcGetThreadPriority },
  37. { 0x0d, SvcSetThreadPriority },
  38. { 0x0e, SvcGetThreadCoreMask },
  39. { 0x0f, SvcSetThreadCoreMask },
  40. { 0x10, SvcGetCurrentProcessorNumber },
  41. { 0x12, SvcClearEvent },
  42. { 0x13, SvcMapSharedMemory },
  43. { 0x14, SvcUnmapSharedMemory },
  44. { 0x15, SvcCreateTransferMemory },
  45. { 0x16, SvcCloseHandle },
  46. { 0x17, SvcResetSignal },
  47. { 0x18, SvcWaitSynchronization },
  48. { 0x19, SvcCancelSynchronization },
  49. { 0x1a, SvcArbitrateLock },
  50. { 0x1b, SvcArbitrateUnlock },
  51. { 0x1c, SvcWaitProcessWideKeyAtomic },
  52. { 0x1d, SvcSignalProcessWideKey },
  53. { 0x1e, SvcGetSystemTick },
  54. { 0x1f, SvcConnectToNamedPort },
  55. { 0x21, SvcSendSyncRequest },
  56. { 0x22, SvcSendSyncRequestWithUserBuffer },
  57. { 0x25, SvcGetThreadId },
  58. { 0x26, SvcBreak },
  59. { 0x27, SvcOutputDebugString },
  60. { 0x29, SvcGetInfo },
  61. { 0x2c, SvcMapPhysicalMemory },
  62. { 0x2d, SvcUnmapPhysicalMemory },
  63. { 0x32, SvcSetThreadActivity },
  64. { 0x33, SvcGetThreadContext3 },
  65. { 0x34, SvcWaitForAddress }
  66. };
  67. this.Device = Device;
  68. this.Process = Process;
  69. this.Memory = Process.Memory;
  70. SyncWaits = new ConcurrentDictionary<KThread, AutoResetEvent>();
  71. }
  72. static SvcHandler()
  73. {
  74. Rng = new Random();
  75. }
  76. public void SvcCall(object sender, AInstExceptionEventArgs e)
  77. {
  78. AThreadState ThreadState = (AThreadState)sender;
  79. Process.GetThread(ThreadState.Tpidr).LastPc = e.Position;
  80. if (SvcFuncs.TryGetValue(e.Id, out SvcFunc Func))
  81. {
  82. Device.Log.PrintDebug(LogClass.KernelSvc, $"{Func.Method.Name} called.");
  83. Func(ThreadState);
  84. Process.Scheduler.Reschedule(Process.GetThread(ThreadState.Tpidr));
  85. Device.Log.PrintDebug(LogClass.KernelSvc, $"{Func.Method.Name} ended.");
  86. }
  87. else
  88. {
  89. Process.PrintStackTrace(ThreadState);
  90. throw new NotImplementedException($"0x{e.Id:x4}");
  91. }
  92. }
  93. private KThread GetThread(long Tpidr, int Handle)
  94. {
  95. if ((uint)Handle == SelfThreadHandle)
  96. {
  97. return Process.GetThread(Tpidr);
  98. }
  99. else
  100. {
  101. return Process.HandleTable.GetData<KThread>(Handle);
  102. }
  103. }
  104. }
  105. }