IStaticServiceForPsc.cs 18 KB

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