IStaticServiceForPsc.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. using Ryujinx.Common;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using Ryujinx.HLE.HOS.Kernel.Threading;
  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. [Command(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. [Command(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. [Command(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. [Command(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. [Command(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. [Command(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. [Command(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. [Command(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. [Command(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. [Command(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. [Command(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. ResultCode result = userClock.SetAutomaticCorrectionEnabled(context.Thread, autoCorrectionEnabled);
  136. if (result == ResultCode.Success)
  137. {
  138. _timeManager.SharedMemory.SetAutomaticCorrectionEnabled(autoCorrectionEnabled);
  139. SteadyClockTimePoint currentTimePoint = userClock.GetSteadyClockCore().GetCurrentTimePoint(context.Thread);
  140. userClock.SetAutomaticCorrectionUpdatedTime(currentTimePoint);
  141. userClock.SignalAutomaticCorrectionEvent();
  142. }
  143. return result;
  144. }
  145. [Command(102)] // 5.0.0+
  146. // GetStandardUserSystemClockInitialYear() -> u32
  147. public ResultCode GetStandardUserSystemClockInitialYear(ServiceCtx context)
  148. {
  149. // This is only implemented in glue's StaticService.
  150. return ResultCode.NotImplemented;
  151. }
  152. [Command(200)] // 3.0.0+
  153. // IsStandardNetworkSystemClockAccuracySufficient() -> bool
  154. public ResultCode IsStandardNetworkSystemClockAccuracySufficient(ServiceCtx context)
  155. {
  156. context.ResponseData.Write(_timeManager.StandardNetworkSystemClock.IsStandardNetworkSystemClockAccuracySufficient(context.Thread));
  157. return ResultCode.Success;
  158. }
  159. [Command(201)] // 6.0.0+
  160. // GetStandardUserSystemClockAutomaticCorrectionUpdatedTime() -> nn::time::SteadyClockTimePoint
  161. public ResultCode GetStandardUserSystemClockAutomaticCorrectionUpdatedTime(ServiceCtx context)
  162. {
  163. StandardUserSystemClockCore userClock = _timeManager.StandardUserSystemClock;
  164. if (!userClock.IsInitialized())
  165. {
  166. return ResultCode.UninitializedClock;
  167. }
  168. context.ResponseData.WriteStruct(userClock.GetAutomaticCorrectionUpdatedTime());
  169. return ResultCode.Success;
  170. }
  171. [Command(300)] // 4.0.0+
  172. // CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> s64
  173. public ResultCode CalculateMonotonicSystemClockBaseTimePoint(ServiceCtx context)
  174. {
  175. SteadyClockCore steadyClock = _timeManager.StandardSteadyClock;
  176. if (!steadyClock.IsInitialized())
  177. {
  178. return ResultCode.UninitializedClock;
  179. }
  180. SystemClockContext otherContext = context.RequestData.ReadStruct<SystemClockContext>();
  181. SteadyClockTimePoint currentTimePoint = steadyClock.GetCurrentTimePoint(context.Thread);
  182. ResultCode result = ResultCode.TimeMismatch;
  183. if (currentTimePoint.ClockSourceId == otherContext.SteadyTimePoint.ClockSourceId)
  184. {
  185. TimeSpanType ticksTimeSpan = TimeSpanType.FromTicks(context.Thread.Context.CntpctEl0, context.Thread.Context.CntfrqEl0);
  186. long baseTimePoint = otherContext.Offset + currentTimePoint.TimePoint - ticksTimeSpan.ToSeconds();
  187. context.ResponseData.Write(baseTimePoint);
  188. result = ResultCode.Success;
  189. }
  190. return result;
  191. }
  192. [Command(400)] // 4.0.0+
  193. // GetClockSnapshot(u8) -> buffer<nn::time::sf::ClockSnapshot, 0x1a>
  194. public ResultCode GetClockSnapshot(ServiceCtx context)
  195. {
  196. byte type = context.RequestData.ReadByte();
  197. ResultCode result = _timeManager.StandardUserSystemClock.GetClockContext(context.Thread, out SystemClockContext userContext);
  198. if (result == ResultCode.Success)
  199. {
  200. result = _timeManager.StandardNetworkSystemClock.GetClockContext(context.Thread, out SystemClockContext networkContext);
  201. if (result == ResultCode.Success)
  202. {
  203. result = GetClockSnapshotFromSystemClockContextInternal(context.Thread, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
  204. if (result == ResultCode.Success)
  205. {
  206. WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot);
  207. }
  208. }
  209. }
  210. return result;
  211. }
  212. [Command(401)] // 4.0.0+
  213. // GetClockSnapshotFromSystemClockContext(u8, nn::time::SystemClockContext, nn::time::SystemClockContext) -> buffer<nn::time::sf::ClockSnapshot, 0x1a>
  214. public ResultCode GetClockSnapshotFromSystemClockContext(ServiceCtx context)
  215. {
  216. byte type = context.RequestData.ReadByte();
  217. context.RequestData.BaseStream.Position += 7;
  218. SystemClockContext userContext = context.RequestData.ReadStruct<SystemClockContext>();
  219. SystemClockContext networkContext = context.RequestData.ReadStruct<SystemClockContext>();
  220. ResultCode result = GetClockSnapshotFromSystemClockContextInternal(context.Thread, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
  221. if (result == ResultCode.Success)
  222. {
  223. WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot);
  224. }
  225. return result;
  226. }
  227. [Command(500)] // 4.0.0+
  228. // CalculateStandardUserSystemClockDifferenceByUser(buffer<nn::time::sf::ClockSnapshot, 0x19>, buffer<nn::time::sf::ClockSnapshot, 0x19>) -> nn::TimeSpanType
  229. public ResultCode CalculateStandardUserSystemClockDifferenceByUser(ServiceCtx context)
  230. {
  231. ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[0]);
  232. ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[1]);
  233. TimeSpanType difference = TimeSpanType.FromSeconds(clockSnapshotB.UserContext.Offset - clockSnapshotA.UserContext.Offset);
  234. if (clockSnapshotB.UserContext.SteadyTimePoint.ClockSourceId != clockSnapshotA.UserContext.SteadyTimePoint.ClockSourceId || (clockSnapshotB.IsAutomaticCorrectionEnabled && clockSnapshotA.IsAutomaticCorrectionEnabled))
  235. {
  236. difference = new TimeSpanType(0);
  237. }
  238. context.ResponseData.Write(difference.NanoSeconds);
  239. return ResultCode.Success;
  240. }
  241. [Command(501)] // 4.0.0+
  242. // CalculateSpanBetween(buffer<nn::time::sf::ClockSnapshot, 0x19>, buffer<nn::time::sf::ClockSnapshot, 0x19>) -> nn::TimeSpanType
  243. public ResultCode CalculateSpanBetween(ServiceCtx context)
  244. {
  245. ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[0]);
  246. ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.PtrBuff[1]);
  247. TimeSpanType result;
  248. ResultCode resultCode = clockSnapshotA.SteadyClockTimePoint.GetSpanBetween(clockSnapshotB.SteadyClockTimePoint, out long timeSpan);
  249. if (resultCode != ResultCode.Success)
  250. {
  251. resultCode = ResultCode.TimeNotFound;
  252. if (clockSnapshotA.NetworkTime != 0 && clockSnapshotB.NetworkTime != 0)
  253. {
  254. result = TimeSpanType.FromSeconds(clockSnapshotB.NetworkTime - clockSnapshotA.NetworkTime);
  255. resultCode = ResultCode.Success;
  256. }
  257. else
  258. {
  259. return resultCode;
  260. }
  261. }
  262. else
  263. {
  264. result = TimeSpanType.FromSeconds(timeSpan);
  265. }
  266. context.ResponseData.Write(result.NanoSeconds);
  267. return resultCode;
  268. }
  269. private ResultCode GetClockSnapshotFromSystemClockContextInternal(KThread thread, SystemClockContext userContext, SystemClockContext networkContext, byte type, out ClockSnapshot clockSnapshot)
  270. {
  271. clockSnapshot = new ClockSnapshot();
  272. SteadyClockCore steadyClockCore = _timeManager.StandardSteadyClock;
  273. SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(thread);
  274. clockSnapshot.IsAutomaticCorrectionEnabled = _timeManager.StandardUserSystemClock.IsAutomaticCorrectionEnabled();
  275. clockSnapshot.UserContext = userContext;
  276. clockSnapshot.NetworkContext = networkContext;
  277. ResultCode result = _timeManager.TimeZone.Manager.GetDeviceLocationName(out string deviceLocationName);
  278. if (result != ResultCode.Success)
  279. {
  280. return result;
  281. }
  282. char[] tzName = deviceLocationName.ToCharArray();
  283. char[] locationName = new char[0x24];
  284. Array.Copy(tzName, locationName, tzName.Length);
  285. clockSnapshot.LocationName = locationName;
  286. result = ClockSnapshot.GetCurrentTime(out clockSnapshot.UserTime, currentTimePoint, clockSnapshot.UserContext);
  287. if (result == ResultCode.Success)
  288. {
  289. result = _timeManager.TimeZone.Manager.ToCalendarTimeWithMyRules(clockSnapshot.UserTime, out CalendarInfo userCalendarInfo);
  290. if (result == ResultCode.Success)
  291. {
  292. clockSnapshot.UserCalendarTime = userCalendarInfo.Time;
  293. clockSnapshot.UserCalendarAdditionalTime = userCalendarInfo.AdditionalInfo;
  294. if (ClockSnapshot.GetCurrentTime(out clockSnapshot.NetworkTime, currentTimePoint, clockSnapshot.NetworkContext) != ResultCode.Success)
  295. {
  296. clockSnapshot.NetworkTime = 0;
  297. }
  298. result = _timeManager.TimeZone.Manager.ToCalendarTimeWithMyRules(clockSnapshot.NetworkTime, out CalendarInfo networkCalendarInfo);
  299. if (result == ResultCode.Success)
  300. {
  301. clockSnapshot.NetworkCalendarTime = networkCalendarInfo.Time;
  302. clockSnapshot.NetworkCalendarAdditionalTime = networkCalendarInfo.AdditionalInfo;
  303. clockSnapshot.Type = type;
  304. // Probably a version field?
  305. clockSnapshot.Unknown = 0;
  306. }
  307. }
  308. }
  309. return result;
  310. }
  311. private ClockSnapshot ReadClockSnapshotFromBuffer(ServiceCtx context, IpcPtrBuffDesc ipcDesc)
  312. {
  313. Debug.Assert(ipcDesc.Size == Marshal.SizeOf<ClockSnapshot>());
  314. byte[] temp = new byte[ipcDesc.Size];
  315. context.Memory.Read((ulong)ipcDesc.Position, temp);
  316. using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(temp)))
  317. {
  318. return bufferReader.ReadStruct<ClockSnapshot>();
  319. }
  320. }
  321. private void WriteClockSnapshotFromBuffer(ServiceCtx context, IpcRecvListBuffDesc ipcDesc, ClockSnapshot clockSnapshot)
  322. {
  323. Debug.Assert(ipcDesc.Size == Marshal.SizeOf<ClockSnapshot>());
  324. MemoryStream memory = new MemoryStream((int)ipcDesc.Size);
  325. using (BinaryWriter bufferWriter = new BinaryWriter(memory))
  326. {
  327. bufferWriter.WriteStruct(clockSnapshot);
  328. }
  329. context.Memory.Write((ulong)ipcDesc.Position, memory.ToArray());
  330. memory.Dispose();
  331. }
  332. }
  333. }