time.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. // Copyright 2019 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/logging/log.h"
  5. #include "core/core.h"
  6. #include "core/core_timing.h"
  7. #include "core/core_timing_util.h"
  8. #include "core/hardware_properties.h"
  9. #include "core/hle/ipc_helpers.h"
  10. #include "core/hle/kernel/client_port.h"
  11. #include "core/hle/kernel/client_session.h"
  12. #include "core/hle/kernel/k_scheduler.h"
  13. #include "core/hle/kernel/kernel.h"
  14. #include "core/hle/service/time/interface.h"
  15. #include "core/hle/service/time/time.h"
  16. #include "core/hle/service/time/time_sharedmemory.h"
  17. #include "core/hle/service/time/time_zone_service.h"
  18. namespace Service::Time {
  19. class ISystemClock final : public ServiceFramework<ISystemClock> {
  20. public:
  21. explicit ISystemClock(Clock::SystemClockCore& clock_core_, Core::System& system_)
  22. : ServiceFramework{system_, "ISystemClock"}, clock_core{clock_core_} {
  23. // clang-format off
  24. static const FunctionInfo functions[] = {
  25. {0, &ISystemClock::GetCurrentTime, "GetCurrentTime"},
  26. {1, nullptr, "SetCurrentTime"},
  27. {2, &ISystemClock::GetSystemClockContext, "GetSystemClockContext"},
  28. {3, nullptr, "SetSystemClockContext"},
  29. {4, nullptr, "GetOperationEventReadableHandle"},
  30. };
  31. // clang-format on
  32. RegisterHandlers(functions);
  33. }
  34. private:
  35. void GetCurrentTime(Kernel::HLERequestContext& ctx) {
  36. LOG_DEBUG(Service_Time, "called");
  37. if (!clock_core.IsInitialized()) {
  38. IPC::ResponseBuilder rb{ctx, 2};
  39. rb.Push(ERROR_UNINITIALIZED_CLOCK);
  40. return;
  41. }
  42. s64 posix_time{};
  43. if (const ResultCode result{clock_core.GetCurrentTime(system, posix_time)};
  44. result.IsError()) {
  45. IPC::ResponseBuilder rb{ctx, 2};
  46. rb.Push(result);
  47. return;
  48. }
  49. IPC::ResponseBuilder rb{ctx, 4};
  50. rb.Push(RESULT_SUCCESS);
  51. rb.Push<s64>(posix_time);
  52. }
  53. void GetSystemClockContext(Kernel::HLERequestContext& ctx) {
  54. LOG_DEBUG(Service_Time, "called");
  55. if (!clock_core.IsInitialized()) {
  56. IPC::ResponseBuilder rb{ctx, 2};
  57. rb.Push(ERROR_UNINITIALIZED_CLOCK);
  58. return;
  59. }
  60. Clock::SystemClockContext system_clock_context{};
  61. if (const ResultCode result{clock_core.GetClockContext(system, system_clock_context)};
  62. result.IsError()) {
  63. IPC::ResponseBuilder rb{ctx, 2};
  64. rb.Push(result);
  65. return;
  66. }
  67. IPC::ResponseBuilder rb{ctx, sizeof(Clock::SystemClockContext) / 4 + 2};
  68. rb.Push(RESULT_SUCCESS);
  69. rb.PushRaw(system_clock_context);
  70. }
  71. Clock::SystemClockCore& clock_core;
  72. };
  73. class ISteadyClock final : public ServiceFramework<ISteadyClock> {
  74. public:
  75. explicit ISteadyClock(Clock::SteadyClockCore& clock_core_, Core::System& system_)
  76. : ServiceFramework{system_, "ISteadyClock"}, clock_core{clock_core_} {
  77. static const FunctionInfo functions[] = {
  78. {0, &ISteadyClock::GetCurrentTimePoint, "GetCurrentTimePoint"},
  79. {2, nullptr, "GetTestOffset"},
  80. {3, nullptr, "SetTestOffset"},
  81. {100, nullptr, "GetRtcValue"},
  82. {101, nullptr, "IsRtcResetDetected"},
  83. {102, nullptr, "GetSetupResultValue"},
  84. {200, nullptr, "GetInternalOffset"},
  85. {201, nullptr, "SetInternalOffset"},
  86. };
  87. RegisterHandlers(functions);
  88. }
  89. private:
  90. void GetCurrentTimePoint(Kernel::HLERequestContext& ctx) {
  91. LOG_DEBUG(Service_Time, "called");
  92. if (!clock_core.IsInitialized()) {
  93. IPC::ResponseBuilder rb{ctx, 2};
  94. rb.Push(ERROR_UNINITIALIZED_CLOCK);
  95. return;
  96. }
  97. const Clock::SteadyClockTimePoint time_point{clock_core.GetCurrentTimePoint(system)};
  98. IPC::ResponseBuilder rb{ctx, (sizeof(Clock::SteadyClockTimePoint) / 4) + 2};
  99. rb.Push(RESULT_SUCCESS);
  100. rb.PushRaw(time_point);
  101. }
  102. Clock::SteadyClockCore& clock_core;
  103. };
  104. ResultCode Module::Interface::GetClockSnapshotFromSystemClockContextInternal(
  105. Kernel::KThread* thread, Clock::SystemClockContext user_context,
  106. Clock::SystemClockContext network_context, u8 type, Clock::ClockSnapshot& clock_snapshot) {
  107. auto& time_manager{system.GetTimeManager()};
  108. clock_snapshot.is_automatic_correction_enabled =
  109. time_manager.GetStandardUserSystemClockCore().IsAutomaticCorrectionEnabled();
  110. clock_snapshot.user_context = user_context;
  111. clock_snapshot.network_context = network_context;
  112. if (const ResultCode result{
  113. time_manager.GetTimeZoneContentManager().GetTimeZoneManager().GetDeviceLocationName(
  114. clock_snapshot.location_name)};
  115. result != RESULT_SUCCESS) {
  116. return result;
  117. }
  118. const auto current_time_point{
  119. time_manager.GetStandardSteadyClockCore().GetCurrentTimePoint(system)};
  120. if (const ResultCode result{Clock::ClockSnapshot::GetCurrentTime(
  121. clock_snapshot.user_time, current_time_point, clock_snapshot.user_context)};
  122. result != RESULT_SUCCESS) {
  123. return result;
  124. }
  125. TimeZone::CalendarInfo userCalendarInfo{};
  126. if (const ResultCode result{
  127. time_manager.GetTimeZoneContentManager().GetTimeZoneManager().ToCalendarTimeWithMyRules(
  128. clock_snapshot.user_time, userCalendarInfo)};
  129. result != RESULT_SUCCESS) {
  130. return result;
  131. }
  132. clock_snapshot.user_calendar_time = userCalendarInfo.time;
  133. clock_snapshot.user_calendar_additional_time = userCalendarInfo.additiona_info;
  134. if (Clock::ClockSnapshot::GetCurrentTime(clock_snapshot.network_time, current_time_point,
  135. clock_snapshot.network_context) != RESULT_SUCCESS) {
  136. clock_snapshot.network_time = 0;
  137. }
  138. TimeZone::CalendarInfo networkCalendarInfo{};
  139. if (const ResultCode result{
  140. time_manager.GetTimeZoneContentManager().GetTimeZoneManager().ToCalendarTimeWithMyRules(
  141. clock_snapshot.network_time, networkCalendarInfo)};
  142. result != RESULT_SUCCESS) {
  143. return result;
  144. }
  145. clock_snapshot.network_calendar_time = networkCalendarInfo.time;
  146. clock_snapshot.network_calendar_additional_time = networkCalendarInfo.additiona_info;
  147. clock_snapshot.type = type;
  148. return RESULT_SUCCESS;
  149. }
  150. void Module::Interface::GetStandardUserSystemClock(Kernel::HLERequestContext& ctx) {
  151. LOG_DEBUG(Service_Time, "called");
  152. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  153. rb.Push(RESULT_SUCCESS);
  154. rb.PushIpcInterface<ISystemClock>(system.GetTimeManager().GetStandardUserSystemClockCore(),
  155. system);
  156. }
  157. void Module::Interface::GetStandardNetworkSystemClock(Kernel::HLERequestContext& ctx) {
  158. LOG_DEBUG(Service_Time, "called");
  159. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  160. rb.Push(RESULT_SUCCESS);
  161. rb.PushIpcInterface<ISystemClock>(system.GetTimeManager().GetStandardNetworkSystemClockCore(),
  162. system);
  163. }
  164. void Module::Interface::GetStandardSteadyClock(Kernel::HLERequestContext& ctx) {
  165. LOG_DEBUG(Service_Time, "called");
  166. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  167. rb.Push(RESULT_SUCCESS);
  168. rb.PushIpcInterface<ISteadyClock>(system.GetTimeManager().GetStandardSteadyClockCore(), system);
  169. }
  170. void Module::Interface::GetTimeZoneService(Kernel::HLERequestContext& ctx) {
  171. LOG_DEBUG(Service_Time, "called");
  172. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  173. rb.Push(RESULT_SUCCESS);
  174. rb.PushIpcInterface<ITimeZoneService>(system,
  175. system.GetTimeManager().GetTimeZoneContentManager());
  176. }
  177. void Module::Interface::GetStandardLocalSystemClock(Kernel::HLERequestContext& ctx) {
  178. LOG_DEBUG(Service_Time, "called");
  179. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  180. rb.Push(RESULT_SUCCESS);
  181. rb.PushIpcInterface<ISystemClock>(system.GetTimeManager().GetStandardLocalSystemClockCore(),
  182. system);
  183. }
  184. void Module::Interface::IsStandardNetworkSystemClockAccuracySufficient(
  185. Kernel::HLERequestContext& ctx) {
  186. LOG_DEBUG(Service_Time, "called");
  187. auto& clock_core{system.GetTimeManager().GetStandardNetworkSystemClockCore()};
  188. IPC::ResponseBuilder rb{ctx, 3};
  189. rb.Push(RESULT_SUCCESS);
  190. rb.Push<u32>(clock_core.IsStandardNetworkSystemClockAccuracySufficient(system));
  191. }
  192. void Module::Interface::CalculateMonotonicSystemClockBaseTimePoint(Kernel::HLERequestContext& ctx) {
  193. LOG_DEBUG(Service_Time, "called");
  194. auto& steady_clock_core{system.GetTimeManager().GetStandardSteadyClockCore()};
  195. if (!steady_clock_core.IsInitialized()) {
  196. IPC::ResponseBuilder rb{ctx, 2};
  197. rb.Push(ERROR_UNINITIALIZED_CLOCK);
  198. return;
  199. }
  200. IPC::RequestParser rp{ctx};
  201. const auto context{rp.PopRaw<Clock::SystemClockContext>()};
  202. const auto current_time_point{steady_clock_core.GetCurrentTimePoint(system)};
  203. if (current_time_point.clock_source_id == context.steady_time_point.clock_source_id) {
  204. const auto ticks{Clock::TimeSpanType::FromTicks(system.CoreTiming().GetClockTicks(),
  205. Core::Hardware::CNTFREQ)};
  206. const s64 base_time_point{context.offset + current_time_point.time_point -
  207. ticks.ToSeconds()};
  208. IPC::ResponseBuilder rb{ctx, (sizeof(s64) / 4) + 2};
  209. rb.Push(RESULT_SUCCESS);
  210. rb.PushRaw(base_time_point);
  211. return;
  212. }
  213. IPC::ResponseBuilder rb{ctx, 2};
  214. rb.Push(ERROR_TIME_MISMATCH);
  215. }
  216. void Module::Interface::GetClockSnapshot(Kernel::HLERequestContext& ctx) {
  217. LOG_DEBUG(Service_Time, "called");
  218. IPC::RequestParser rp{ctx};
  219. const auto type{rp.PopRaw<u8>()};
  220. Clock::SystemClockContext user_context{};
  221. if (const ResultCode result{
  222. system.GetTimeManager().GetStandardUserSystemClockCore().GetClockContext(system,
  223. user_context)};
  224. result.IsError()) {
  225. IPC::ResponseBuilder rb{ctx, 2};
  226. rb.Push(result);
  227. return;
  228. }
  229. Clock::SystemClockContext network_context{};
  230. if (const ResultCode result{
  231. system.GetTimeManager().GetStandardNetworkSystemClockCore().GetClockContext(
  232. system, network_context)};
  233. result.IsError()) {
  234. IPC::ResponseBuilder rb{ctx, 2};
  235. rb.Push(result);
  236. return;
  237. }
  238. Clock::ClockSnapshot clock_snapshot{};
  239. if (const ResultCode result{GetClockSnapshotFromSystemClockContextInternal(
  240. &ctx.GetThread(), user_context, network_context, type, clock_snapshot)};
  241. result.IsError()) {
  242. IPC::ResponseBuilder rb{ctx, 2};
  243. rb.Push(result);
  244. return;
  245. }
  246. IPC::ResponseBuilder rb{ctx, 2};
  247. rb.Push(RESULT_SUCCESS);
  248. ctx.WriteBuffer(clock_snapshot);
  249. }
  250. void Module::Interface::GetClockSnapshotFromSystemClockContext(Kernel::HLERequestContext& ctx) {
  251. LOG_DEBUG(Service_Time, "called");
  252. IPC::RequestParser rp{ctx};
  253. const auto type{rp.PopRaw<u8>()};
  254. rp.AlignWithPadding();
  255. const Clock::SystemClockContext user_context{rp.PopRaw<Clock::SystemClockContext>()};
  256. const Clock::SystemClockContext network_context{rp.PopRaw<Clock::SystemClockContext>()};
  257. Clock::ClockSnapshot clock_snapshot{};
  258. if (const ResultCode result{GetClockSnapshotFromSystemClockContextInternal(
  259. &ctx.GetThread(), user_context, network_context, type, clock_snapshot)};
  260. result != RESULT_SUCCESS) {
  261. IPC::ResponseBuilder rb{ctx, 2};
  262. rb.Push(result);
  263. return;
  264. }
  265. IPC::ResponseBuilder rb{ctx, 2};
  266. rb.Push(RESULT_SUCCESS);
  267. ctx.WriteBuffer(clock_snapshot);
  268. }
  269. void Module::Interface::CalculateStandardUserSystemClockDifferenceByUser(
  270. Kernel::HLERequestContext& ctx) {
  271. LOG_DEBUG(Service_Time, "called");
  272. IPC::RequestParser rp{ctx};
  273. const auto snapshot_a = rp.PopRaw<Clock::ClockSnapshot>();
  274. const auto snapshot_b = rp.PopRaw<Clock::ClockSnapshot>();
  275. auto time_span_type{Clock::TimeSpanType::FromSeconds(snapshot_b.user_context.offset -
  276. snapshot_a.user_context.offset)};
  277. if ((snapshot_b.user_context.steady_time_point.clock_source_id !=
  278. snapshot_a.user_context.steady_time_point.clock_source_id) ||
  279. (snapshot_b.is_automatic_correction_enabled &&
  280. snapshot_a.is_automatic_correction_enabled)) {
  281. time_span_type.nanoseconds = 0;
  282. }
  283. IPC::ResponseBuilder rb{ctx, (sizeof(s64) / 4) + 2};
  284. rb.Push(RESULT_SUCCESS);
  285. rb.PushRaw(time_span_type.nanoseconds);
  286. }
  287. void Module::Interface::CalculateSpanBetween(Kernel::HLERequestContext& ctx) {
  288. LOG_DEBUG(Service_Time, "called");
  289. Clock::ClockSnapshot snapshot_a;
  290. Clock::ClockSnapshot snapshot_b;
  291. const auto snapshot_a_data = ctx.ReadBuffer(0);
  292. const auto snapshot_b_data = ctx.ReadBuffer(1);
  293. std::memcpy(&snapshot_a, snapshot_a_data.data(), sizeof(Clock::ClockSnapshot));
  294. std::memcpy(&snapshot_b, snapshot_b_data.data(), sizeof(Clock::ClockSnapshot));
  295. Clock::TimeSpanType time_span_type{};
  296. s64 span{};
  297. if (const ResultCode result{snapshot_a.steady_clock_time_point.GetSpanBetween(
  298. snapshot_b.steady_clock_time_point, span)};
  299. result != RESULT_SUCCESS) {
  300. if (snapshot_a.network_time && snapshot_b.network_time) {
  301. time_span_type =
  302. Clock::TimeSpanType::FromSeconds(snapshot_b.network_time - snapshot_a.network_time);
  303. } else {
  304. IPC::ResponseBuilder rb{ctx, 2};
  305. rb.Push(ERROR_TIME_NOT_FOUND);
  306. return;
  307. }
  308. } else {
  309. time_span_type = Clock::TimeSpanType::FromSeconds(span);
  310. }
  311. IPC::ResponseBuilder rb{ctx, (sizeof(s64) / 4) + 2};
  312. rb.Push(RESULT_SUCCESS);
  313. rb.PushRaw(time_span_type.nanoseconds);
  314. }
  315. void Module::Interface::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) {
  316. LOG_DEBUG(Service_Time, "called");
  317. IPC::ResponseBuilder rb{ctx, 2, 1};
  318. rb.Push(RESULT_SUCCESS);
  319. rb.PushCopyObjects(SharedFrom(&system.Kernel().GetTimeSharedMem()));
  320. }
  321. Module::Interface::Interface(std::shared_ptr<Module> module_, Core::System& system_,
  322. const char* name)
  323. : ServiceFramework{system_, name}, module{std::move(module_)} {}
  324. Module::Interface::~Interface() = default;
  325. void InstallInterfaces(Core::System& system) {
  326. auto module{std::make_shared<Module>()};
  327. std::make_shared<Time>(module, system, "time:a")->InstallAsService(system.ServiceManager());
  328. std::make_shared<Time>(module, system, "time:s")->InstallAsService(system.ServiceManager());
  329. std::make_shared<Time>(module, system, "time:u")->InstallAsService(system.ServiceManager());
  330. }
  331. } // namespace Service::Time