SvcHandler.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using ChocolArm64.Events;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  4. using Ryujinx.Core.OsHle.Handles;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. namespace Ryujinx.Core.OsHle.Kernel
  9. {
  10. partial class SvcHandler : IDisposable
  11. {
  12. private delegate void SvcFunc(AThreadState ThreadState);
  13. private Dictionary<int, SvcFunc> SvcFuncs;
  14. private Switch Ns;
  15. private Process Process;
  16. private AMemory Memory;
  17. private ConcurrentDictionary<long, MutualExclusion> Mutexes;
  18. private ConcurrentDictionary<long, ConditionVariable> CondVars;
  19. private HashSet<(HSharedMem, long)> MappedSharedMems;
  20. private ulong CurrentHeapSize;
  21. private static Random Rng;
  22. public SvcHandler(Switch Ns, 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. { 0x0f, SvcSetThreadCoreMask },
  39. { 0x10, SvcGetCurrentProcessorNumber },
  40. { 0x12, SvcClearEvent },
  41. { 0x13, SvcMapSharedMemory },
  42. { 0x14, SvcUnmapSharedMemory },
  43. { 0x15, SvcCreateTransferMemory },
  44. { 0x16, SvcCloseHandle },
  45. { 0x17, SvcResetSignal },
  46. { 0x18, SvcWaitSynchronization },
  47. { 0x1a, SvcArbitrateLock },
  48. { 0x1b, SvcArbitrateUnlock },
  49. { 0x1c, SvcWaitProcessWideKeyAtomic },
  50. { 0x1d, SvcSignalProcessWideKey },
  51. { 0x1e, SvcGetSystemTick },
  52. { 0x1f, SvcConnectToNamedPort },
  53. { 0x21, SvcSendSyncRequest },
  54. { 0x22, SvcSendSyncRequestWithUserBuffer },
  55. { 0x25, SvcGetThreadId },
  56. { 0x26, SvcBreak },
  57. { 0x27, SvcOutputDebugString },
  58. { 0x29, SvcGetInfo },
  59. { 0x32, SvcSetThreadActivity }
  60. };
  61. this.Ns = Ns;
  62. this.Process = Process;
  63. this.Memory = Process.Memory;
  64. Mutexes = new ConcurrentDictionary<long, MutualExclusion>();
  65. CondVars = new ConcurrentDictionary<long, ConditionVariable>();
  66. MappedSharedMems = new HashSet<(HSharedMem, long)>();
  67. }
  68. static SvcHandler()
  69. {
  70. Rng = new Random();
  71. }
  72. public void SvcCall(object sender, AInstExceptionEventArgs e)
  73. {
  74. AThreadState ThreadState = (AThreadState)sender;
  75. if (SvcFuncs.TryGetValue(e.Id, out SvcFunc Func))
  76. {
  77. Logging.Trace(LogClass.KernelSvc, $"(Thread {ThreadState.ThreadId}) {Func.Method.Name} called.");
  78. Func(ThreadState);
  79. Logging.Trace(LogClass.KernelSvc, $"(Thread {ThreadState.ThreadId}) {Func.Method.Name} ended.");
  80. }
  81. else
  82. {
  83. throw new NotImplementedException(e.Id.ToString("x4"));
  84. }
  85. }
  86. public void Dispose()
  87. {
  88. Dispose(true);
  89. }
  90. protected virtual void Dispose(bool Disposing)
  91. {
  92. if (Disposing)
  93. {
  94. lock (MappedSharedMems)
  95. {
  96. foreach ((HSharedMem SharedMem, long Position) in MappedSharedMems)
  97. {
  98. SharedMem.RemoveVirtualPosition(Memory, Position);
  99. }
  100. MappedSharedMems.Clear();
  101. }
  102. }
  103. }
  104. }
  105. }