SvcHandler.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using ChocolArm64.Events;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  4. using Ryujinx.HLE.Logging;
  5. using Ryujinx.HLE.OsHle.Handles;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Threading;
  10. namespace Ryujinx.HLE.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 HashSet<(HSharedMem, long)> MappedSharedMems;
  21. private ulong CurrentHeapSize;
  22. private const uint SelfThreadHandle = 0xffff8000;
  23. private const uint SelfProcessHandle = 0xffff8001;
  24. private static Random Rng;
  25. public SvcHandler(Switch Ns, Process Process)
  26. {
  27. SvcFuncs = new Dictionary<int, SvcFunc>()
  28. {
  29. { 0x01, SvcSetHeapSize },
  30. { 0x03, SvcSetMemoryAttribute },
  31. { 0x04, SvcMapMemory },
  32. { 0x05, SvcUnmapMemory },
  33. { 0x06, SvcQueryMemory },
  34. { 0x07, SvcExitProcess },
  35. { 0x08, SvcCreateThread },
  36. { 0x09, SvcStartThread },
  37. { 0x0a, SvcExitThread },
  38. { 0x0b, SvcSleepThread },
  39. { 0x0c, SvcGetThreadPriority },
  40. { 0x0d, SvcSetThreadPriority },
  41. { 0x0e, SvcGetThreadCoreMask },
  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. { 0x2c, SvcMapPhysicalMemory },
  65. { 0x2d, SvcUnmapPhysicalMemory },
  66. { 0x32, SvcSetThreadActivity },
  67. { 0x33, SvcGetThreadContext3 }
  68. };
  69. this.Ns = Ns;
  70. this.Process = Process;
  71. this.Memory = Process.Memory;
  72. SyncWaits = new ConcurrentDictionary<KThread, AutoResetEvent>();
  73. MappedSharedMems = new HashSet<(HSharedMem, long)>();
  74. }
  75. static SvcHandler()
  76. {
  77. Rng = new Random();
  78. }
  79. public void SvcCall(object sender, AInstExceptionEventArgs e)
  80. {
  81. AThreadState ThreadState = (AThreadState)sender;
  82. Process.GetThread(ThreadState.Tpidr).LastPc = e.Position;
  83. if (SvcFuncs.TryGetValue(e.Id, out SvcFunc Func))
  84. {
  85. Ns.Log.PrintDebug(LogClass.KernelSvc, $"{Func.Method.Name} called.");
  86. Func(ThreadState);
  87. Process.Scheduler.Reschedule(Process.GetThread(ThreadState.Tpidr));
  88. Ns.Log.PrintDebug(LogClass.KernelSvc, $"{Func.Method.Name} ended.");
  89. }
  90. else
  91. {
  92. Process.PrintStackTrace(ThreadState);
  93. throw new NotImplementedException(e.Id.ToString("x4"));
  94. }
  95. }
  96. private KThread GetThread(long Tpidr, int Handle)
  97. {
  98. if ((uint)Handle == SelfThreadHandle)
  99. {
  100. return Process.GetThread(Tpidr);
  101. }
  102. else
  103. {
  104. return Process.HandleTable.GetData<KThread>(Handle);
  105. }
  106. }
  107. public void Dispose()
  108. {
  109. Dispose(true);
  110. }
  111. protected virtual void Dispose(bool Disposing)
  112. {
  113. if (Disposing)
  114. {
  115. lock (MappedSharedMems)
  116. {
  117. foreach ((HSharedMem SharedMem, long Position) in MappedSharedMems)
  118. {
  119. SharedMem.RemoveVirtualPosition(Memory, Position);
  120. }
  121. MappedSharedMems.Clear();
  122. }
  123. }
  124. }
  125. }
  126. }