SvcHandler.cs 4.3 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.Generic;
  7. namespace Ryujinx.Core.OsHle.Kernel
  8. {
  9. partial class SvcHandler : IDisposable
  10. {
  11. private delegate void SvcFunc(AThreadState ThreadState);
  12. private Dictionary<int, SvcFunc> SvcFuncs;
  13. private Switch Ns;
  14. private Process Process;
  15. private AMemory Memory;
  16. private object CondVarLock;
  17. private HashSet<(HSharedMem, long)> MappedSharedMems;
  18. private ulong CurrentHeapSize;
  19. private static Random Rng;
  20. public SvcHandler(Switch Ns, Process Process)
  21. {
  22. SvcFuncs = new Dictionary<int, SvcFunc>()
  23. {
  24. { 0x01, SvcSetHeapSize },
  25. { 0x03, SvcSetMemoryAttribute },
  26. { 0x04, SvcMapMemory },
  27. { 0x05, SvcUnmapMemory },
  28. { 0x06, SvcQueryMemory },
  29. { 0x07, SvcExitProcess },
  30. { 0x08, SvcCreateThread },
  31. { 0x09, SvcStartThread },
  32. { 0x0a, SvcExitThread },
  33. { 0x0b, SvcSleepThread },
  34. { 0x0c, SvcGetThreadPriority },
  35. { 0x0d, SvcSetThreadPriority },
  36. { 0x0f, SvcSetThreadCoreMask },
  37. { 0x10, SvcGetCurrentProcessorNumber },
  38. { 0x12, SvcClearEvent },
  39. { 0x13, SvcMapSharedMemory },
  40. { 0x14, SvcUnmapSharedMemory },
  41. { 0x15, SvcCreateTransferMemory },
  42. { 0x16, SvcCloseHandle },
  43. { 0x17, SvcResetSignal },
  44. { 0x18, SvcWaitSynchronization },
  45. { 0x1a, SvcArbitrateLock },
  46. { 0x1b, SvcArbitrateUnlock },
  47. { 0x1c, SvcWaitProcessWideKeyAtomic },
  48. { 0x1d, SvcSignalProcessWideKey },
  49. { 0x1e, SvcGetSystemTick },
  50. { 0x1f, SvcConnectToNamedPort },
  51. { 0x21, SvcSendSyncRequest },
  52. { 0x22, SvcSendSyncRequestWithUserBuffer },
  53. { 0x25, SvcGetThreadId },
  54. { 0x26, SvcBreak },
  55. { 0x27, SvcOutputDebugString },
  56. { 0x29, SvcGetInfo },
  57. { 0x32, SvcSetThreadActivity }
  58. };
  59. this.Ns = Ns;
  60. this.Process = Process;
  61. this.Memory = Process.Memory;
  62. CondVarLock = new object();
  63. MappedSharedMems = new HashSet<(HSharedMem, long)>();
  64. }
  65. static SvcHandler()
  66. {
  67. Rng = new Random();
  68. }
  69. public void SvcCall(object sender, AInstExceptionEventArgs e)
  70. {
  71. AThreadState ThreadState = (AThreadState)sender;
  72. if (SvcFuncs.TryGetValue(e.Id, out SvcFunc Func))
  73. {
  74. Logging.Trace(LogClass.KernelSvc, $"(Thread {ThreadState.ThreadId}) {Func.Method.Name} called.");
  75. Func(ThreadState);
  76. Logging.Trace(LogClass.KernelSvc, $"(Thread {ThreadState.ThreadId}) {Func.Method.Name} ended.");
  77. }
  78. else
  79. {
  80. Process.PrintStackTrace(ThreadState);
  81. throw new NotImplementedException(e.Id.ToString("x4"));
  82. }
  83. }
  84. public void Dispose()
  85. {
  86. Dispose(true);
  87. }
  88. protected virtual void Dispose(bool Disposing)
  89. {
  90. if (Disposing)
  91. {
  92. lock (MappedSharedMems)
  93. {
  94. foreach ((HSharedMem SharedMem, long Position) in MappedSharedMems)
  95. {
  96. SharedMem.RemoveVirtualPosition(Memory, Position);
  97. }
  98. MappedSharedMems.Clear();
  99. }
  100. }
  101. }
  102. }
  103. }