SvcHandler.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. };
  60. this.Ns = Ns;
  61. this.Process = Process;
  62. this.Memory = Process.Memory;
  63. Mutexes = new ConcurrentDictionary<long, MutualExclusion>();
  64. CondVars = new ConcurrentDictionary<long, ConditionVariable>();
  65. MappedSharedMems = new HashSet<(HSharedMem, long)>();
  66. }
  67. static SvcHandler()
  68. {
  69. Rng = new Random();
  70. }
  71. public void SvcCall(object sender, AInstExceptionEventArgs e)
  72. {
  73. AThreadState ThreadState = (AThreadState)sender;
  74. if (SvcFuncs.TryGetValue(e.Id, out SvcFunc Func))
  75. {
  76. Logging.Trace(LogClass.KernelSvc, $"(Thread {ThreadState.ThreadId}) {Func.Method.Name} called.");
  77. Func(ThreadState);
  78. Logging.Trace(LogClass.KernelSvc, $"(Thread {ThreadState.ThreadId}) {Func.Method.Name} ended.");
  79. }
  80. else
  81. {
  82. throw new NotImplementedException(e.Id.ToString("x4"));
  83. }
  84. }
  85. public void Dispose()
  86. {
  87. Dispose(true);
  88. }
  89. protected virtual void Dispose(bool Disposing)
  90. {
  91. if (Disposing)
  92. {
  93. lock (MappedSharedMems)
  94. {
  95. foreach ((HSharedMem SharedMem, long Position) in MappedSharedMems)
  96. {
  97. SharedMem.RemoveVirtualPosition(Memory, Position);
  98. }
  99. MappedSharedMems.Clear();
  100. }
  101. }
  102. }
  103. }
  104. }