SvcHandler.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using ChocolArm64.Events;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  4. using Ryujinx.Common.Logging;
  5. using Ryujinx.HLE.HOS.Ipc;
  6. using System;
  7. using System.Collections.Generic;
  8. namespace Ryujinx.HLE.HOS.Kernel
  9. {
  10. partial class SvcHandler
  11. {
  12. private delegate void SvcFunc(CpuThreadState ThreadState);
  13. private Dictionary<int, SvcFunc> SvcFuncs;
  14. private Switch Device;
  15. private Process Process;
  16. private Horizon System;
  17. private MemoryManager Memory;
  18. private struct HleIpcMessage
  19. {
  20. public KThread Thread { get; private set; }
  21. public KSession Session { get; private set; }
  22. public IpcMessage Message { get; private set; }
  23. public long MessagePtr { get; private set; }
  24. public HleIpcMessage(
  25. KThread Thread,
  26. KSession Session,
  27. IpcMessage Message,
  28. long MessagePtr)
  29. {
  30. this.Thread = Thread;
  31. this.Session = Session;
  32. this.Message = Message;
  33. this.MessagePtr = MessagePtr;
  34. }
  35. }
  36. private static Random Rng;
  37. public SvcHandler(Switch Device, Process Process)
  38. {
  39. SvcFuncs = new Dictionary<int, SvcFunc>()
  40. {
  41. { 0x01, SvcSetHeapSize },
  42. { 0x03, SvcSetMemoryAttribute },
  43. { 0x04, SvcMapMemory },
  44. { 0x05, SvcUnmapMemory },
  45. { 0x06, SvcQueryMemory },
  46. { 0x07, SvcExitProcess },
  47. { 0x08, SvcCreateThread },
  48. { 0x09, SvcStartThread },
  49. { 0x0a, SvcExitThread },
  50. { 0x0b, SvcSleepThread },
  51. { 0x0c, SvcGetThreadPriority },
  52. { 0x0d, SvcSetThreadPriority },
  53. { 0x0e, SvcGetThreadCoreMask },
  54. { 0x0f, SvcSetThreadCoreMask },
  55. { 0x10, SvcGetCurrentProcessorNumber },
  56. { 0x11, SignalEvent64 },
  57. { 0x12, ClearEvent64 },
  58. { 0x13, SvcMapSharedMemory },
  59. { 0x14, SvcUnmapSharedMemory },
  60. { 0x15, SvcCreateTransferMemory },
  61. { 0x16, SvcCloseHandle },
  62. { 0x17, ResetSignal64 },
  63. { 0x18, SvcWaitSynchronization },
  64. { 0x19, SvcCancelSynchronization },
  65. { 0x1a, SvcArbitrateLock },
  66. { 0x1b, SvcArbitrateUnlock },
  67. { 0x1c, SvcWaitProcessWideKeyAtomic },
  68. { 0x1d, SvcSignalProcessWideKey },
  69. { 0x1e, SvcGetSystemTick },
  70. { 0x1f, SvcConnectToNamedPort },
  71. { 0x21, SvcSendSyncRequest },
  72. { 0x22, SvcSendSyncRequestWithUserBuffer },
  73. { 0x25, SvcGetThreadId },
  74. { 0x26, SvcBreak },
  75. { 0x27, SvcOutputDebugString },
  76. { 0x29, SvcGetInfo },
  77. { 0x2c, SvcMapPhysicalMemory },
  78. { 0x2d, SvcUnmapPhysicalMemory },
  79. { 0x32, SvcSetThreadActivity },
  80. { 0x33, SvcGetThreadContext3 },
  81. { 0x34, SvcWaitForAddress },
  82. { 0x35, SvcSignalToAddress },
  83. { 0x45, CreateEvent64 }
  84. };
  85. this.Device = Device;
  86. this.Process = Process;
  87. this.System = Process.Device.System;
  88. this.Memory = Process.Memory;
  89. }
  90. static SvcHandler()
  91. {
  92. Rng = new Random();
  93. }
  94. public void SvcCall(object sender, InstExceptionEventArgs e)
  95. {
  96. CpuThreadState ThreadState = (CpuThreadState)sender;
  97. Process.GetThread(ThreadState.Tpidr).LastPc = e.Position;
  98. if (SvcFuncs.TryGetValue(e.Id, out SvcFunc Func))
  99. {
  100. Logger.PrintDebug(LogClass.KernelSvc, $"{Func.Method.Name} called.");
  101. Func(ThreadState);
  102. Logger.PrintDebug(LogClass.KernelSvc, $"{Func.Method.Name} ended.");
  103. }
  104. else
  105. {
  106. Process.PrintStackTrace(ThreadState);
  107. throw new NotImplementedException($"0x{e.Id:x4}");
  108. }
  109. }
  110. }
  111. }