IStaticServiceForPsc.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using Ryujinx.Common;
  2. using Ryujinx.Cpu;
  3. using Ryujinx.HLE.HOS.Ipc;
  4. using Ryujinx.HLE.HOS.Services.Time.Clock;
  5. using Ryujinx.HLE.HOS.Services.Time.StaticService;
  6. using Ryujinx.HLE.HOS.Services.Time.TimeZone;
  7. using Ryujinx.Horizon.Common;
  8. using System;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Runtime.CompilerServices;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. namespace Ryujinx.HLE.HOS.Services.Time
  15. {
  16. [Service("time:s", TimePermissions.System)]
  17. [Service("time:su", TimePermissions.SystemUpdate)]
  18. class IStaticServiceForPsc : IpcService
  19. {
  20. private TimeManager _timeManager;
  21. private TimePermissions _permissions;
  22. private int _timeSharedMemoryNativeHandle = 0;
  23. public IStaticServiceForPsc(ServiceCtx context, TimePermissions permissions) : this(TimeManager.Instance, permissions) {}
  24. public IStaticServiceForPsc(TimeManager manager, TimePermissions permissions)
  25. {
  26. _permissions = permissions;
  27. _timeManager = manager;
  28. }
  29. [CommandHipc(0)]
  30. // GetStandardUserSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  31. public ResultCode GetStandardUserSystemClock(ServiceCtx context)
  32. {
  33. MakeObject(context, new ISystemClock(_timeManager.StandardUserSystemClock,
  34. (_permissions & TimePermissions.UserSystemClockWritableMask) != 0,
  35. (_permissions & TimePermissions.BypassUninitialized) != 0));
  36. return ResultCode.Success;
  37. }
  38. [CommandHipc(1)]
  39. // GetStandardNetworkSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  40. public ResultCode GetStandardNetworkSystemClock(ServiceCtx context)
  41. {
  42. MakeObject(context, new ISystemClock(_timeManager.StandardNetworkSystemClock,
  43. (_permissions & TimePermissions.NetworkSystemClockWritableMask) != 0,
  44. (_permissions & TimePermissions.BypassUninitialized) != 0));
  45. return ResultCode.Success;
  46. }
  47. [CommandHipc(2)]
  48. // GetStandardSteadyClock() -> object<nn::timesrv::detail::service::ISteadyClock>
  49. public ResultCode GetStandardSteadyClock(ServiceCtx context)
  50. {
  51. MakeObject(context, new ISteadyClock(_timeManager.StandardSteadyClock,
  52. (_permissions & TimePermissions.SteadyClockWritableMask) != 0,
  53. (_permissions & TimePermissions.BypassUninitialized) != 0));
  54. return ResultCode.Success;
  55. }
  56. [CommandHipc(3)]
  57. // GetTimeZoneService() -> object<nn::timesrv::detail::service::ITimeZoneService>
  58. public ResultCode GetTimeZoneService(ServiceCtx context)
  59. {
  60. MakeObject(context, new ITimeZoneServiceForPsc(_timeManager.TimeZone.Manager,
  61. (_permissions & TimePermissions.TimeZoneWritableMask) != 0));
  62. return ResultCode.Success;
  63. }
  64. [CommandHipc(4)]
  65. // GetStandardLocalSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  66. public ResultCode GetStandardLocalSystemClock(ServiceCtx context)
  67. {
  68. MakeObject(context, new ISystemClock(_timeManager.StandardLocalSystemClock,
  69. (_permissions & TimePermissions.LocalSystemClockWritableMask) != 0,
  70. (_permissions & TimePermissions.BypassUninitialized) != 0));
  71. return ResultCode.Success;
  72. }
  73. [CommandHipc(5)] // 4.0.0+
  74. // GetEphemeralNetworkSystemClock() -> object<nn::timesrv::detail::service::ISystemClock>
  75. public ResultCode GetEphemeralNetworkSystemClock(ServiceCtx context)
  76. {
  77. MakeObject(context, new ISystemClock(_timeManager.StandardNetworkSystemClock,
  78. (_permissions & TimePermissions.NetworkSystemClockWritableMask) != 0,
  79. (_permissions & TimePermissions.BypassUninitialized) != 0));
  80. return ResultCode.Success;
  81. }
  82. [CommandHipc(20)] // 6.0.0+
  83. // GetSharedMemoryNativeHandle() -> handle<copy>
  84. public ResultCode GetSharedMemoryNativeHandle(ServiceCtx context)
  85. {
  86. if (_timeSharedMemoryNativeHandle == 0)
  87. {
  88. if (context.Process.HandleTable.GenerateHandle(_timeManager.SharedMemory.GetSharedMemory(), out _timeSharedMemoryNativeHandle) != Result.Success)
  89. {
  90. throw new InvalidOperationException("Out of handles!");
  91. }
  92. }
  93. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_timeSharedMemoryNativeHandle);
  94. return ResultCode.Success;
  95. }
  96. [CommandHipc(50)] // 4.0.0+
  97. // SetStandardSteadyClockInternalOffset(nn::TimeSpanType internal_offset)
  98. public ResultCode SetStandardSteadyClockInternalOffset(ServiceCtx context)
  99. {
  100. // This is only implemented in glue's StaticService.
  101. return ResultCode.NotImplemented;
  102. }
  103. [CommandHipc(51)] // 9.0.0+
  104. // GetStandardSteadyClockRtcValue() -> u64
  105. public ResultCode GetStandardSteadyClockRtcValue(ServiceCtx context)
  106. {
  107. // This is only implemented in glue's StaticService.
  108. return ResultCode.NotImplemented;
  109. }
  110. [CommandHipc(100)]
  111. // IsStandardUserSystemClockAutomaticCorrectionEnabled() -> bool
  112. public ResultCode IsStandardUserSystemClockAutomaticCorrectionEnabled(ServiceCtx context)
  113. {
  114. StandardUserSystemClockCore userClock = _timeManager.StandardUserSystemClock;
  115. if (!userClock.IsInitialized())
  116. {
  117. return ResultCode.UninitializedClock;
  118. }
  119. context.ResponseData.Write(userClock.IsAutomaticCorrectionEnabled());
  120. return ResultCode.Success;
  121. }
  122. [CommandHipc(101)]
  123. // SetStandardUserSystemClockAutomaticCorrectionEnabled(b8)
  124. public ResultCode SetStandardUserSystemClockAutomaticCorrectionEnabled(ServiceCtx context)
  125. {
  126. SteadyClockCore steadyClock = _timeManager.StandardSteadyClock;
  127. StandardUserSystemClockCore userClock = _timeManager.StandardUserSystemClock;
  128. if (!userClock.IsInitialized() || !steadyClock.IsInitialized())
  129. {
  130. return ResultCode.UninitializedClock;
  131. }
  132. if ((_permissions & TimePermissions.UserSystemClockWritableMask) == 0)
  133. {
  134. return ResultCode.PermissionDenied;
  135. }
  136. bool autoCorrectionEnabled = context.RequestData.ReadBoolean();
  137. ITickSource tickSource = context.Device.System.TickSource;
  138. ResultCode result = userClock.SetAutomaticCorrectionEnabled(tickSource, autoCorrectionEnabled);
  139. if (result == ResultCode.Success)
  140. {
  141. _timeManager.SharedMemory.SetAutomaticCorrectionEnabled(autoCorrectionEnabled);
  142. SteadyClockTimePoint currentTimePoint = userClock.GetSteadyClockCore().GetCurrentTimePoint(tickSource);
  143. userClock.SetAutomaticCorrectionUpdatedTime(currentTimePoint);
  144. userClock.SignalAutomaticCorrectionEvent();
  145. }
  146. return result;
  147. }
  148. [CommandHipc(102)] // 5.0.0+
  149. // GetStandardUserSystemClockInitialYear() -> u32
  150. public ResultCode GetStandardUserSystemClockInitialYear(ServiceCtx context)
  151. {
  152. // This is only implemented in glue's StaticService.
  153. return ResultCode.NotImplemented;
  154. }
  155. [CommandHipc(200)] // 3.0.0+
  156. // IsStandardNetworkSystemClockAccuracySufficient() -> bool
  157. public ResultCode IsStandardNetworkSystemClockAccuracySufficient(ServiceCtx context)
  158. {
  159. ITickSource tickSource = context.Device.System.TickSource;
  160. context.ResponseData.Write(_timeManager.StandardNetworkSystemClock.IsStandardNetworkSystemClockAccuracySufficient(tickSource));
  161. return ResultCode.Success;
  162. }
  163. [CommandHipc(201)] // 6.0.0+
  164. // GetStandardUserSystemClockAutomaticCorrectionUpdatedTime() -> nn::time::SteadyClockTimePoint
  165. public ResultCode GetStandardUserSystemClockAutomaticCorrectionUpdatedTime(ServiceCtx context)
  166. {
  167. StandardUserSystemClockCore userClock = _timeManager.StandardUserSystemClock;
  168. if (!userClock.IsInitialized())
  169. {
  170. return ResultCode.UninitializedClock;
  171. }
  172. context.ResponseData.WriteStruct(userClock.GetAutomaticCorrectionUpdatedTime());
  173. return ResultCode.Success;
  174. }
  175. [CommandHipc(300)] // 4.0.0+
  176. // CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> s64
  177. public ResultCode CalculateMonotonicSystemClockBaseTimePoint(ServiceCtx context)
  178. {
  179. SteadyClockCore steadyClock = _timeManager.StandardSteadyClock;
  180. if (!steadyClock.IsInitialized())
  181. {
  182. return ResultCode.UninitializedClock;
  183. }
  184. ITickSource tickSource = context.Device.System.TickSource;
  185. SystemClockContext otherContext = context.RequestData.ReadStruct<SystemClockContext>();
  186. SteadyClockTimePoint currentTimePoint = steadyClock.GetCurrentTimePoint(tickSource);
  187. ResultCode result = ResultCode.TimeMismatch;
  188. if (currentTimePoint.ClockSourceId == otherContext.SteadyTimePoint.ClockSourceId)
  189. {
  190. TimeSpanType ticksTimeSpan = TimeSpanType.FromTicks(tickSource.Counter, tickSource.Frequency);
  191. long baseTimePoint = otherContext.Offset + currentTimePoint.TimePoint - ticksTimeSpan.ToSeconds();
  192. context.ResponseData.Write(baseTimePoint);
  193. result = ResultCode.Success;
  194. }
  195. return result;
  196. }
  197. [CommandHipc(400)] // 4.0.0+
  198. // GetClockSnapshot(u8) -> buffer<nn::time::sf::ClockSnapshot, 0x1a>
  199. public ResultCode GetClockSnapshot(ServiceCtx context)
  200. {
  201. byte type = context.RequestData.ReadByte();
  202. context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize((uint)Marshal.SizeOf<ClockSnapshot>());
  203. ITickSource tickSource = context.Device.System.TickSource;
  204. ResultCode result = _timeManager.StandardUserSystemClock.GetClockContext(tickSource, out SystemClockContext userContext);
  205. if (result == ResultCode.Success)
  206. {
  207. result = _timeManager.StandardNetworkSystemClock.GetClockContext(tickSource, out SystemClockContext networkContext);
  208. if (result == ResultCode.Success)
  209. {
  210. result = GetClockSnapshotFromSystemClockContextInternal(tickSource, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
  211. if (result == ResultCode.Success)
  212. {
  213. WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot);
  214. }
  215. }
  216. }
  217. return result;
  218. }
  219. [CommandHipc(401)] // 4.0.0+
  220. // GetClockSnapshotFromSystemClockContext(u8, nn::time::SystemClockContext, nn::time::SystemClockContext) -> buffer<nn::time::sf::ClockSnapshot, 0x1a>
  221. public ResultCode GetClockSnapshotFromSystemClockContext(ServiceCtx context)
  222. {
  223. byte type = context.RequestData.ReadByte();
  224. context.Response.PtrBuff[0] = context.Response.PtrBuff[0].WithSize((uint)Unsafe.SizeOf<ClockSnapshot>());
  225. context.RequestData.BaseStream.Position += 7;
  226. SystemClockContext userContext = context.RequestData.ReadStruct<SystemClockContext>();
  227. SystemClockContext networkContext = context.RequestData.ReadStruct<SystemClockContext>();
  228. ITickSource tickSource = context.Device.System.TickSource;
  229. ResultCode result = GetClockSnapshotFromSystemClockContextInternal(tickSource, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
  230. if (result == ResultCode.Success)
  231. {
  232. WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot);
  233. }
  234. return result;
  235. }
  236. [CommandHipc(500)] // 4.0.0+
  237. // CalculateStandardUserSystemClockDifferenceByUser(buffer<nn::time::sf::ClockSnapshot, 0x19>, buffer<nn::time::sf::ClockSnapshot, 0x19>) -> nn::TimeSpanType
  238. public ResultCode CalculateStandardUserSystemClockDifferenceByUser(ServiceCtx context)
  239. {
  240. ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[0]);
  241. ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[1]);
  242. TimeSpanType difference = TimeSpanType.FromSeconds(clockSnapshotB.UserContext.Offset - clockSnapshotA.UserContext.Offset);
  243. if (clockSnapshotB.UserContext.SteadyTimePoint.ClockSourceId != clockSnapshotA.UserContext.SteadyTimePoint.ClockSourceId || (clockSnapshotB.IsAutomaticCorrectionEnabled && clockSnapshotA.IsAutomaticCorrectionEnabled))
  244. {
  245. difference = new TimeSpanType(0);
  246. }
  247. context.ResponseData.Write(difference.NanoSeconds);
  248. return ResultCode.Success;
  249. }
  250. [CommandHipc(501)] // 4.0.0+
  251. // CalculateSpanBetween(buffer<nn::time::sf::ClockSnapshot, 0x19>, buffer<nn::time::sf::ClockSnapshot, 0x19>) -> nn::TimeSpanType
  252. public ResultCode CalculateSpanBetween(ServiceCtx context)
  253. {
  254. ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[0]);
  255. ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[1]);
  256. TimeSpanType result;
  257. ResultCode resultCode = clockSnapshotA.SteadyClockTimePoint.GetSpanBetween(clockSnapshotB.SteadyClockTimePoint, out long timeSpan);
  258. if (resultCode != ResultCode.Success)
  259. {
  260. resultCode = ResultCode.TimeNotFound;
  261. if (clockSnapshotA.NetworkTime != 0 && clockSnapshotB.NetworkTime != 0)
  262. {
  263. result = TimeSpanType.FromSeconds(clockSnapshotB.NetworkTime - clockSnapshotA.NetworkTime);
  264. resultCode = ResultCode.Success;
  265. }
  266. else
  267. {
  268. return resultCode;
  269. }
  270. }
  271. else
  272. {
  273. result = TimeSpanType.FromSeconds(timeSpan);
  274. }
  275. context.ResponseData.Write(result.NanoSeconds);
  276. return resultCode;
  277. }
  278. private ResultCode GetClockSnapshotFromSystemClockContextInternal(ITickSource tickSource, SystemClockContext userContext, SystemClockContext networkContext, byte type, out ClockSnapshot clockSnapshot)
  279. {
  280. clockSnapshot = new ClockSnapshot();
  281. SteadyClockCore steadyClockCore = _timeManager.StandardSteadyClock;
  282. SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(tickSource);
  283. clockSnapshot.IsAutomaticCorrectionEnabled = _timeManager.StandardUserSystemClock.IsAutomaticCorrectionEnabled();
  284. clockSnapshot.UserContext = userContext;
  285. clockSnapshot.NetworkContext = networkContext;
  286. clockSnapshot.SteadyClockTimePoint = currentTimePoint;
  287. ResultCode result = _timeManager.TimeZone.Manager.GetDeviceLocationName(out string deviceLocationName);
  288. if (result != ResultCode.Success)
  289. {
  290. return result;
  291. }
  292. ReadOnlySpan<byte> tzName = Encoding.ASCII.GetBytes(deviceLocationName);
  293. tzName.CopyTo(clockSnapshot.LocationName);
  294. result = ClockSnapshot.GetCurrentTime(out clockSnapshot.UserTime, currentTimePoint, clockSnapshot.UserContext);
  295. if (result == ResultCode.Success)
  296. {
  297. result = _timeManager.TimeZone.Manager.ToCalendarTimeWithMyRules(clockSnapshot.UserTime, out CalendarInfo userCalendarInfo);
  298. if (result == ResultCode.Success)
  299. {
  300. clockSnapshot.UserCalendarTime = userCalendarInfo.Time;
  301. clockSnapshot.UserCalendarAdditionalTime = userCalendarInfo.AdditionalInfo;
  302. if (ClockSnapshot.GetCurrentTime(out clockSnapshot.NetworkTime, currentTimePoint, clockSnapshot.NetworkContext) != ResultCode.Success)
  303. {
  304. clockSnapshot.NetworkTime = 0;
  305. }
  306. result = _timeManager.TimeZone.Manager.ToCalendarTimeWithMyRules(clockSnapshot.NetworkTime, out CalendarInfo networkCalendarInfo);
  307. if (result == ResultCode.Success)
  308. {
  309. clockSnapshot.NetworkCalendarTime = networkCalendarInfo.Time;
  310. clockSnapshot.NetworkCalendarAdditionalTime = networkCalendarInfo.AdditionalInfo;
  311. clockSnapshot.Type = type;
  312. // Probably a version field?
  313. clockSnapshot.Unknown = 0;
  314. }
  315. }
  316. }
  317. return result;
  318. }
  319. private ClockSnapshot ReadClockSnapshotFromBuffer(ServiceCtx context, IpcPtrBuffDesc ipcDesc)
  320. {
  321. Debug.Assert(ipcDesc.Size == (ulong)Unsafe.SizeOf<ClockSnapshot>());
  322. byte[] temp = new byte[ipcDesc.Size];
  323. context.Memory.Read(ipcDesc.Position, temp);
  324. using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(temp)))
  325. {
  326. return bufferReader.ReadStruct<ClockSnapshot>();
  327. }
  328. }
  329. private void WriteClockSnapshotFromBuffer(ServiceCtx context, IpcRecvListBuffDesc ipcDesc, ClockSnapshot clockSnapshot)
  330. {
  331. MemoryHelper.Write(context.Memory, ipcDesc.Position, clockSnapshot);
  332. }
  333. }
  334. }