SvcHandler.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. { 0x10, SvcGetCurrentProcessorNumber },
  37. { 0x12, SvcClearEvent },
  38. { 0x13, SvcMapSharedMemory },
  39. { 0x14, SvcUnmapSharedMemory },
  40. { 0x15, SvcCreateTransferMemory },
  41. { 0x16, SvcCloseHandle },
  42. { 0x17, SvcResetSignal },
  43. { 0x18, SvcWaitSynchronization },
  44. { 0x1a, SvcArbitrateLock },
  45. { 0x1b, SvcArbitrateUnlock },
  46. { 0x1c, SvcWaitProcessWideKeyAtomic },
  47. { 0x1d, SvcSignalProcessWideKey },
  48. { 0x1e, SvcGetSystemTick },
  49. { 0x1f, SvcConnectToNamedPort },
  50. { 0x21, SvcSendSyncRequest },
  51. { 0x22, SvcSendSyncRequestWithUserBuffer },
  52. { 0x25, SvcGetThreadId },
  53. { 0x26, SvcBreak },
  54. { 0x27, SvcOutputDebugString },
  55. { 0x29, SvcGetInfo }
  56. };
  57. this.Ns = Ns;
  58. this.Process = Process;
  59. this.Memory = Process.Memory;
  60. MappedSharedMems = new HashSet<(HSharedMem, long)>();
  61. }
  62. static SvcHandler()
  63. {
  64. Rng = new Random();
  65. }
  66. public void SvcCall(object sender, AInstExceptionEventArgs e)
  67. {
  68. AThreadState ThreadState = (AThreadState)sender;
  69. if (SvcFuncs.TryGetValue(e.Id, out SvcFunc Func))
  70. {
  71. Logging.Trace($"(Thread {ThreadState.ThreadId}) {Func.Method.Name} called.");
  72. Func(ThreadState);
  73. Logging.Trace($"(Thread {ThreadState.ThreadId}) {Func.Method.Name} ended.");
  74. }
  75. else
  76. {
  77. throw new NotImplementedException(e.Id.ToString("x4"));
  78. }
  79. }
  80. public void Dispose()
  81. {
  82. Dispose(true);
  83. }
  84. protected virtual void Dispose(bool Disposing)
  85. {
  86. if (Disposing)
  87. {
  88. lock (MappedSharedMems)
  89. {
  90. foreach ((HSharedMem SharedMem, long Position) in MappedSharedMems)
  91. {
  92. SharedMem.RemoveVirtualPosition(Memory, Position);
  93. }
  94. MappedSharedMems.Clear();
  95. }
  96. }
  97. }
  98. }
  99. }