ISystemClock.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Ryujinx.Common;
  2. using Ryujinx.Cpu;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Kernel.Common;
  5. using Ryujinx.HLE.HOS.Kernel.Threading;
  6. using Ryujinx.HLE.HOS.Services.Time.Clock;
  7. using System;
  8. namespace Ryujinx.HLE.HOS.Services.Time.StaticService
  9. {
  10. class ISystemClock : IpcService
  11. {
  12. private SystemClockCore _clockCore;
  13. private bool _writePermission;
  14. private bool _bypassUninitializedClock;
  15. private int _operationEventReadableHandle;
  16. public ISystemClock(SystemClockCore clockCore, bool writePermission, bool bypassUninitializedClock)
  17. {
  18. _clockCore = clockCore;
  19. _writePermission = writePermission;
  20. _bypassUninitializedClock = bypassUninitializedClock;
  21. _operationEventReadableHandle = 0;
  22. }
  23. [CommandHipc(0)]
  24. // GetCurrentTime() -> nn::time::PosixTime
  25. public ResultCode GetCurrentTime(ServiceCtx context)
  26. {
  27. if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
  28. {
  29. return ResultCode.UninitializedClock;
  30. }
  31. ITickSource tickSource = context.Device.System.TickSource;
  32. ResultCode result = _clockCore.GetCurrentTime(tickSource, out long posixTime);
  33. if (result == ResultCode.Success)
  34. {
  35. context.ResponseData.Write(posixTime);
  36. }
  37. return result;
  38. }
  39. [CommandHipc(1)]
  40. // SetCurrentTime(nn::time::PosixTime)
  41. public ResultCode SetCurrentTime(ServiceCtx context)
  42. {
  43. if (!_writePermission)
  44. {
  45. return ResultCode.PermissionDenied;
  46. }
  47. if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
  48. {
  49. return ResultCode.UninitializedClock;
  50. }
  51. long posixTime = context.RequestData.ReadInt64();
  52. ITickSource tickSource = context.Device.System.TickSource;
  53. return _clockCore.SetCurrentTime(tickSource, posixTime);
  54. }
  55. [CommandHipc(2)]
  56. // GetClockContext() -> nn::time::SystemClockContext
  57. public ResultCode GetSystemClockContext(ServiceCtx context)
  58. {
  59. if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
  60. {
  61. return ResultCode.UninitializedClock;
  62. }
  63. ITickSource tickSource = context.Device.System.TickSource;
  64. ResultCode result = _clockCore.GetClockContext(tickSource, out SystemClockContext clockContext);
  65. if (result == ResultCode.Success)
  66. {
  67. context.ResponseData.WriteStruct(clockContext);
  68. }
  69. return result;
  70. }
  71. [CommandHipc(3)]
  72. // SetClockContext(nn::time::SystemClockContext)
  73. public ResultCode SetSystemClockContext(ServiceCtx context)
  74. {
  75. if (!_writePermission)
  76. {
  77. return ResultCode.PermissionDenied;
  78. }
  79. if (!_bypassUninitializedClock && !_clockCore.IsInitialized())
  80. {
  81. return ResultCode.UninitializedClock;
  82. }
  83. SystemClockContext clockContext = context.RequestData.ReadStruct<SystemClockContext>();
  84. ResultCode result = _clockCore.SetSystemClockContext(clockContext);
  85. return result;
  86. }
  87. [CommandHipc(4)] // 9.0.0+
  88. // GetOperationEventReadableHandle() -> handle<copy>
  89. public ResultCode GetOperationEventReadableHandle(ServiceCtx context)
  90. {
  91. if (_operationEventReadableHandle == 0)
  92. {
  93. KEvent kEvent = new KEvent(context.Device.System.KernelContext);
  94. _clockCore.RegisterOperationEvent(kEvent.WritableEvent);
  95. if (context.Process.HandleTable.GenerateHandle(kEvent.ReadableEvent, out _operationEventReadableHandle) != KernelResult.Success)
  96. {
  97. throw new InvalidOperationException("Out of handles!");
  98. }
  99. }
  100. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_operationEventReadableHandle);
  101. return ResultCode.Success;
  102. }
  103. }
  104. }