SvcHandler.cs 5.0 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 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. { 0x0f, SvcSetThreadCoreMask },
  42. { 0x10, SvcGetCurrentProcessorNumber },
  43. { 0x12, SvcClearEvent },
  44. { 0x13, SvcMapSharedMemory },
  45. { 0x14, SvcUnmapSharedMemory },
  46. { 0x15, SvcCreateTransferMemory },
  47. { 0x16, SvcCloseHandle },
  48. { 0x17, SvcResetSignal },
  49. { 0x18, SvcWaitSynchronization },
  50. { 0x19, SvcCancelSynchronization },
  51. { 0x1a, SvcArbitrateLock },
  52. { 0x1b, SvcArbitrateUnlock },
  53. { 0x1c, SvcWaitProcessWideKeyAtomic },
  54. { 0x1d, SvcSignalProcessWideKey },
  55. { 0x1e, SvcGetSystemTick },
  56. { 0x1f, SvcConnectToNamedPort },
  57. { 0x21, SvcSendSyncRequest },
  58. { 0x22, SvcSendSyncRequestWithUserBuffer },
  59. { 0x25, SvcGetThreadId },
  60. { 0x26, SvcBreak },
  61. { 0x27, SvcOutputDebugString },
  62. { 0x29, SvcGetInfo },
  63. { 0x2c, SvcMapPhysicalMemory },
  64. { 0x2d, SvcUnmapPhysicalMemory },
  65. { 0x32, SvcSetThreadActivity }
  66. };
  67. this.Ns = Ns;
  68. this.Process = Process;
  69. this.Memory = Process.Memory;
  70. SyncWaits = new ConcurrentDictionary<KThread, AutoResetEvent>();
  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. Process.Scheduler.Reschedule(Process.GetThread(ThreadState.Tpidr));
  85. Ns.Log.PrintDebug(LogClass.KernelSvc, $"{Func.Method.Name} ended.");
  86. }
  87. else
  88. {
  89. Process.PrintStackTrace(ThreadState);
  90. throw new NotImplementedException(e.Id.ToString("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. public void Dispose()
  105. {
  106. Dispose(true);
  107. }
  108. protected virtual void Dispose(bool Disposing)
  109. {
  110. if (Disposing)
  111. {
  112. lock (MappedSharedMems)
  113. {
  114. foreach ((HSharedMem SharedMem, long Position) in MappedSharedMems)
  115. {
  116. SharedMem.RemoveVirtualPosition(Memory, Position);
  117. }
  118. MappedSharedMems.Clear();
  119. }
  120. }
  121. }
  122. }
  123. }