SvcHandler.cs 4.9 KB

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