IStaticServiceForPsc.cs 18 KB

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