SvcHandler.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. 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 object CondVarLock;
  18. private HashSet<(HSharedMem, long)> MappedSharedMems;
  19. private ulong CurrentHeapSize;
  20. private static Random Rng;
  21. public SvcHandler(Switch Ns, Process Process)
  22. {
  23. SvcFuncs = new Dictionary<int, SvcFunc>()
  24. {
  25. { 0x01, SvcSetHeapSize },
  26. { 0x03, SvcSetMemoryAttribute },
  27. { 0x04, SvcMapMemory },
  28. { 0x05, SvcUnmapMemory },
  29. { 0x06, SvcQueryMemory },
  30. { 0x07, SvcExitProcess },
  31. { 0x08, SvcCreateThread },
  32. { 0x09, SvcStartThread },
  33. { 0x0a, SvcExitThread },
  34. { 0x0b, SvcSleepThread },
  35. { 0x0c, SvcGetThreadPriority },
  36. { 0x0d, SvcSetThreadPriority },
  37. { 0x0f, SvcSetThreadCoreMask },
  38. { 0x10, SvcGetCurrentProcessorNumber },
  39. { 0x12, SvcClearEvent },
  40. { 0x13, SvcMapSharedMemory },
  41. { 0x14, SvcUnmapSharedMemory },
  42. { 0x15, SvcCreateTransferMemory },
  43. { 0x16, SvcCloseHandle },
  44. { 0x17, SvcResetSignal },
  45. { 0x18, SvcWaitSynchronization },
  46. { 0x1a, SvcArbitrateLock },
  47. { 0x1b, SvcArbitrateUnlock },
  48. { 0x1c, SvcWaitProcessWideKeyAtomic },
  49. { 0x1d, SvcSignalProcessWideKey },
  50. { 0x1e, SvcGetSystemTick },
  51. { 0x1f, SvcConnectToNamedPort },
  52. { 0x21, SvcSendSyncRequest },
  53. { 0x22, SvcSendSyncRequestWithUserBuffer },
  54. { 0x25, SvcGetThreadId },
  55. { 0x26, SvcBreak },
  56. { 0x27, SvcOutputDebugString },
  57. { 0x29, SvcGetInfo },
  58. { 0x32, SvcSetThreadActivity }
  59. };
  60. this.Ns = Ns;
  61. this.Process = Process;
  62. this.Memory = Process.Memory;
  63. CondVarLock = new object();
  64. MappedSharedMems = new HashSet<(HSharedMem, long)>();
  65. }
  66. static SvcHandler()
  67. {
  68. Rng = new Random();
  69. }
  70. public void SvcCall(object sender, AInstExceptionEventArgs e)
  71. {
  72. AThreadState ThreadState = (AThreadState)sender;
  73. if (SvcFuncs.TryGetValue(e.Id, out SvcFunc Func))
  74. {
  75. Ns.Log.PrintDebug(LogClass.KernelSvc, $"{Func.Method.Name} called.");
  76. Func(ThreadState);
  77. Ns.Log.PrintDebug(LogClass.KernelSvc, $"{Func.Method.Name} ended.");
  78. }
  79. else
  80. {
  81. Process.PrintStackTrace(ThreadState);
  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. }