SvcHandler.cs 4.0 KB

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