diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 7fd2260505..23d88d7476 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -461,12 +461,40 @@ add_library(core STATIC hle/service/spl/spl.h hle/service/ssl/ssl.cpp hle/service/ssl/ssl.h + hle/service/time/clock_types.h + hle/service/time/ephemeral_network_system_clock_context_writer.h + hle/service/time/ephemeral_network_system_clock_core.h + hle/service/time/errors.h hle/service/time/interface.cpp hle/service/time/interface.h + hle/service/time/local_system_clock_context_writer.h + hle/service/time/network_system_clock_context_writer.h + hle/service/time/standard_local_system_clock_core.h + hle/service/time/standard_network_system_clock_core.h + hle/service/time/standard_steady_clock_core.cpp + hle/service/time/standard_steady_clock_core.h + hle/service/time/standard_user_system_clock_core.cpp + hle/service/time/standard_user_system_clock_core.h + hle/service/time/steady_clock_core.h + hle/service/time/system_clock_context_update_callback.cpp + hle/service/time/system_clock_context_update_callback.h + hle/service/time/system_clock_core.cpp + hle/service/time/system_clock_core.h + hle/service/time/tick_based_steady_clock_core.cpp + hle/service/time/tick_based_steady_clock_core.h hle/service/time/time.cpp hle/service/time/time.h + hle/service/time/time_manager.cpp + hle/service/time/time_manager.h hle/service/time/time_sharedmemory.cpp hle/service/time/time_sharedmemory.h + hle/service/time/time_zone_content_manager.cpp + hle/service/time/time_zone_content_manager.h + hle/service/time/time_zone_manager.cpp + hle/service/time/time_zone_manager.h + hle/service/time/time_zone_service.cpp + hle/service/time/time_zone_service.h + hle/service/time/time_zone_types.h hle/service/usb/usb.cpp hle/service/usb/usb.h hle/service/vi/display/vi_display.cpp diff --git a/src/core/hle/service/time/clock_types.h b/src/core/hle/service/time/clock_types.h new file mode 100644 index 0000000000..f2ef3ec538 --- /dev/null +++ b/src/core/hle/service/time/clock_types.h @@ -0,0 +1,91 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "common/uuid.h" +#include "core/hle/service/time/errors.h" +#include "core/hle/service/time/time_zone_types.h" + +namespace Service::Time::Clock { + +/// https://switchbrew.org/wiki/Glue_services#SteadyClockTimePoint +struct SteadyClockTimePoint { + s64 time_point; + Common::UUID clock_source_id; + + static SteadyClockTimePoint GetRandom() { + return {0, Common::UUID::Generate()}; + } +}; +static_assert(sizeof(SteadyClockTimePoint) == 0x18, "SteadyClockTimePoint is incorrect size"); +static_assert(std::is_trivially_copyable_v, + "SteadyClockTimePoint must be trivially copyable"); + +struct SteadyClockContext { + u64 internal_offset; + Common::UUID steady_time_point; +}; +static_assert(sizeof(SteadyClockContext) == 0x18, "SteadyClockContext is incorrect size"); +static_assert(std::is_trivially_copyable_v, + "SteadyClockContext must be trivially copyable"); + +struct SystemClockContext { + s64 offset; + SteadyClockTimePoint steady_time_point; +}; +static_assert(sizeof(SystemClockContext) == 0x20, "SystemClockContext is incorrect size"); +static_assert(std::is_trivially_copyable_v, + "SystemClockContext must be trivially copyable"); + +/// https://switchbrew.org/wiki/Glue_services#TimeSpanType +struct TimeSpanType { + s64 nanoseconds{}; + static constexpr s64 ns_per_second{1000000000ULL}; + + s64 ToSeconds() const { + return nanoseconds / ns_per_second; + } + + static TimeSpanType FromSeconds(s64 seconds) { + return {seconds * ns_per_second}; + } + + static TimeSpanType FromTicks(u64 ticks, u64 frequency) { + return FromSeconds(static_cast(ticks) / static_cast(frequency)); + } +}; +static_assert(sizeof(TimeSpanType) == 8, "TimeSpanType is incorrect size"); + +struct ClockSnapshot { + SystemClockContext user_context{}; + SystemClockContext network_context{}; + s64 user_time{}; + s64 network_time{}; + TimeZone::CalendarTime user_calendar_time{}; + TimeZone::CalendarTime network_calendar_time{}; + TimeZone::CalendarAdditionalInfo user_calendar_additional_time{}; + TimeZone::CalendarAdditionalInfo network_calendar_additional_time{}; + SteadyClockTimePoint steady_clock_time_point{}; + TimeZone::LocationName location_name{}; + u8 is_automatic_correction_enabled{}; + u8 type{}; + INSERT_PADDING_BYTES(0x2); + + static ResultCode GetCurrentTime(s64& current_time, + const SteadyClockTimePoint& steady_clock_time_point, + const SystemClockContext& context) { + if (steady_clock_time_point.clock_source_id != context.steady_time_point.clock_source_id) { + current_time = 0; + return ERROR_TIME_MISMATCH; + } + current_time = steady_clock_time_point.time_point + context.offset; + return RESULT_SUCCESS; + } +}; +static_assert(sizeof(ClockSnapshot) == 0xD0, "ClockSnapshot is incorrect size"); + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/ephemeral_network_system_clock_context_writer.h b/src/core/hle/service/time/ephemeral_network_system_clock_context_writer.h new file mode 100644 index 0000000000..42893e3f6b --- /dev/null +++ b/src/core/hle/service/time/ephemeral_network_system_clock_context_writer.h @@ -0,0 +1,16 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/time/system_clock_context_update_callback.h" + +namespace Service::Time::Clock { + +class EphemeralNetworkSystemClockContextWriter final : public SystemClockContextUpdateCallback { +public: + EphemeralNetworkSystemClockContextWriter() : SystemClockContextUpdateCallback{} {} +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/ephemeral_network_system_clock_core.h b/src/core/hle/service/time/ephemeral_network_system_clock_core.h new file mode 100644 index 0000000000..4c6cdef866 --- /dev/null +++ b/src/core/hle/service/time/ephemeral_network_system_clock_core.h @@ -0,0 +1,17 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/time/system_clock_core.h" + +namespace Service::Time::Clock { + +class EphemeralNetworkSystemClockCore final : public SystemClockCore { +public: + explicit EphemeralNetworkSystemClockCore(SteadyClockCore& steady_clock_core) + : SystemClockCore{steady_clock_core} {} +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/errors.h b/src/core/hle/service/time/errors.h new file mode 100644 index 0000000000..8501a3e8c8 --- /dev/null +++ b/src/core/hle/service/time/errors.h @@ -0,0 +1,22 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/result.h" + +namespace Service::Time { + +constexpr ResultCode ERROR_PERMISSION_DENIED{ErrorModule::Time, 1}; +constexpr ResultCode ERROR_TIME_MISMATCH{ErrorModule::Time, 102}; +constexpr ResultCode ERROR_UNINITIALIZED_CLOCK{ErrorModule::Time, 103}; +constexpr ResultCode ERROR_TIME_NOT_FOUND{ErrorModule::Time, 200}; +constexpr ResultCode ERROR_OVERFLOW{ErrorModule::Time, 201}; +constexpr ResultCode ERROR_LOCATION_NAME_TOO_LONG{ErrorModule::Time, 801}; +constexpr ResultCode ERROR_OUT_OF_RANGE{ErrorModule::Time, 902}; +constexpr ResultCode ERROR_TIME_ZONE_CONVERSION_FAILED{ErrorModule::Time, 903}; +constexpr ResultCode ERROR_TIME_ZONE_NOT_FOUND{ErrorModule::Time, 989}; +constexpr ResultCode ERROR_NOT_IMPLEMENTED{ErrorModule::Time, 990}; + +} // namespace Service::Time diff --git a/src/core/hle/service/time/interface.cpp b/src/core/hle/service/time/interface.cpp index bc74f1e1d4..b1307a434d 100644 --- a/src/core/hle/service/time/interface.cpp +++ b/src/core/hle/service/time/interface.cpp @@ -1,4 +1,4 @@ -// Copyright 2018 yuzu emulator team +// Copyright 2019 yuzu emulator team // Licensed under GPLv2 or any later version // Refer to the license.txt file included. @@ -6,31 +6,30 @@ namespace Service::Time { -Time::Time(std::shared_ptr time, std::shared_ptr shared_memory, - Core::System& system, const char* name) - : Module::Interface(std::move(time), std::move(shared_memory), system, name) { +Time::Time(std::shared_ptr module, Core::System& system, const char* name) + : Module::Interface(std::move(module), system, name) { // clang-format off static const FunctionInfo functions[] = { {0, &Time::GetStandardUserSystemClock, "GetStandardUserSystemClock"}, {1, &Time::GetStandardNetworkSystemClock, "GetStandardNetworkSystemClock"}, {2, &Time::GetStandardSteadyClock, "GetStandardSteadyClock"}, {3, &Time::GetTimeZoneService, "GetTimeZoneService"}, - {4, &Time::GetStandardLocalSystemClock, "GetStandardLocalSystemClock"}, + {4, nullptr, "GetStandardLocalSystemClock"}, {5, nullptr, "GetEphemeralNetworkSystemClock"}, {20, &Time::GetSharedMemoryNativeHandle, "GetSharedMemoryNativeHandle"}, {30, nullptr, "GetStandardNetworkClockOperationEventReadableHandle"}, {31, nullptr, "GetEphemeralNetworkClockOperationEventReadableHandle"}, {50, nullptr, "SetStandardSteadyClockInternalOffset"}, {51, nullptr, "GetStandardSteadyClockRtcValue"}, - {100, &Time::IsStandardUserSystemClockAutomaticCorrectionEnabled, "IsStandardUserSystemClockAutomaticCorrectionEnabled"}, - {101, &Time::SetStandardUserSystemClockAutomaticCorrectionEnabled, "SetStandardUserSystemClockAutomaticCorrectionEnabled"}, + {100, nullptr, "IsStandardUserSystemClockAutomaticCorrectionEnabled"}, + {101, nullptr, "SetStandardUserSystemClockAutomaticCorrectionEnabled"}, {102, nullptr, "GetStandardUserSystemClockInitialYear"}, {200, nullptr, "IsStandardNetworkSystemClockAccuracySufficient"}, {201, nullptr, "GetStandardUserSystemClockAutomaticCorrectionUpdatedTime"}, - {300, nullptr, "CalculateMonotonicSystemClockBaseTimePoint"}, + {300, &Time::CalculateMonotonicSystemClockBaseTimePoint, "CalculateMonotonicSystemClockBaseTimePoint"}, {400, &Time::GetClockSnapshot, "GetClockSnapshot"}, {401, nullptr, "GetClockSnapshotFromSystemClockContext"}, - {500, &Time::CalculateStandardUserSystemClockDifferenceByUser, "CalculateStandardUserSystemClockDifferenceByUser"}, + {500, nullptr, "CalculateStandardUserSystemClockDifferenceByUser"}, {501, nullptr, "CalculateSpanBetween"}, }; // clang-format on diff --git a/src/core/hle/service/time/interface.h b/src/core/hle/service/time/interface.h index 5c63a07f45..4f49e1f073 100644 --- a/src/core/hle/service/time/interface.h +++ b/src/core/hle/service/time/interface.h @@ -1,4 +1,4 @@ -// Copyright 2018 yuzu emulator team +// Copyright 2019 yuzu emulator team // Licensed under GPLv2 or any later version // Refer to the license.txt file included. @@ -6,14 +6,15 @@ #include "core/hle/service/time/time.h" -namespace Service::Time { +namespace Core { +class System; +} -class SharedMemory; +namespace Service::Time { class Time final : public Module::Interface { public: - explicit Time(std::shared_ptr time, std::shared_ptr shared_memory, - Core::System& system, const char* name); + explicit Time(std::shared_ptr time, Core::System& system, const char* name); ~Time() override; }; diff --git a/src/core/hle/service/time/local_system_clock_context_writer.h b/src/core/hle/service/time/local_system_clock_context_writer.h new file mode 100644 index 0000000000..7050844c6e --- /dev/null +++ b/src/core/hle/service/time/local_system_clock_context_writer.h @@ -0,0 +1,28 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/time/errors.h" +#include "core/hle/service/time/system_clock_context_update_callback.h" +#include "core/hle/service/time/time_sharedmemory.h" + +namespace Service::Time::Clock { + +class LocalSystemClockContextWriter final : public SystemClockContextUpdateCallback { +public: + explicit LocalSystemClockContextWriter(SharedMemory& shared_memory) + : SystemClockContextUpdateCallback{}, shared_memory{shared_memory} {} + +protected: + ResultCode Update() override { + shared_memory.UpdateLocalSystemClockContext(context); + return RESULT_SUCCESS; + } + +private: + SharedMemory& shared_memory; +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/network_system_clock_context_writer.h b/src/core/hle/service/time/network_system_clock_context_writer.h new file mode 100644 index 0000000000..94d8788ff9 --- /dev/null +++ b/src/core/hle/service/time/network_system_clock_context_writer.h @@ -0,0 +1,28 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/time/errors.h" +#include "core/hle/service/time/system_clock_context_update_callback.h" +#include "core/hle/service/time/time_sharedmemory.h" + +namespace Service::Time::Clock { + +class NetworkSystemClockContextWriter final : public SystemClockContextUpdateCallback { +public: + explicit NetworkSystemClockContextWriter(SharedMemory& shared_memory) + : SystemClockContextUpdateCallback{}, shared_memory{shared_memory} {} + +protected: + ResultCode Update() override { + shared_memory.UpdateNetworkSystemClockContext(context); + return RESULT_SUCCESS; + } + +private: + SharedMemory& shared_memory; +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/standard_local_system_clock_core.h b/src/core/hle/service/time/standard_local_system_clock_core.h new file mode 100644 index 0000000000..8c1882eb11 --- /dev/null +++ b/src/core/hle/service/time/standard_local_system_clock_core.h @@ -0,0 +1,17 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/time/system_clock_core.h" + +namespace Service::Time::Clock { + +class StandardLocalSystemClockCore final : public SystemClockCore { +public: + explicit StandardLocalSystemClockCore(SteadyClockCore& steady_clock_core) + : SystemClockCore{steady_clock_core} {} +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/standard_network_system_clock_core.h b/src/core/hle/service/time/standard_network_system_clock_core.h new file mode 100644 index 0000000000..4672851608 --- /dev/null +++ b/src/core/hle/service/time/standard_network_system_clock_core.h @@ -0,0 +1,26 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/time/clock_types.h" +#include "core/hle/service/time/steady_clock_core.h" +#include "core/hle/service/time/system_clock_core.h" + +namespace Service::Time::Clock { + +class StandardNetworkSystemClockCore final : public SystemClockCore { +public: + explicit StandardNetworkSystemClockCore(SteadyClockCore& steady_clock_core) + : SystemClockCore{steady_clock_core} {} + + void SetStandardNetworkClockSufficientAccuracy(TimeSpanType value) { + standard_network_clock_sufficient_accuracy = value; + } + +private: + TimeSpanType standard_network_clock_sufficient_accuracy{}; +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/standard_steady_clock_core.cpp b/src/core/hle/service/time/standard_steady_clock_core.cpp new file mode 100644 index 0000000000..ca1a783fc4 --- /dev/null +++ b/src/core/hle/service/time/standard_steady_clock_core.cpp @@ -0,0 +1,26 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/core.h" +#include "core/core_timing.h" +#include "core/core_timing_util.h" +#include "core/hle/service/time/standard_steady_clock_core.h" + +namespace Service::Time::Clock { + +TimeSpanType StandardSteadyClockCore::GetCurrentRawTimePoint(Core::System& system) { + const TimeSpanType ticks_time_span{TimeSpanType::FromTicks( + Core::Timing::CpuCyclesToClockCycles(system.CoreTiming().GetTicks()), + Core::Timing::CNTFREQ)}; + TimeSpanType raw_time_point{setup_value.nanoseconds + ticks_time_span.nanoseconds}; + + if (raw_time_point.nanoseconds < cached_raw_time_point.nanoseconds) { + raw_time_point.nanoseconds = cached_raw_time_point.nanoseconds; + } + + cached_raw_time_point = raw_time_point; + return raw_time_point; +} + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/standard_steady_clock_core.h b/src/core/hle/service/time/standard_steady_clock_core.h new file mode 100644 index 0000000000..f56f3fd95a --- /dev/null +++ b/src/core/hle/service/time/standard_steady_clock_core.h @@ -0,0 +1,42 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/time/clock_types.h" +#include "core/hle/service/time/steady_clock_core.h" + +namespace Core { +class System; +} + +namespace Service::Time::Clock { + +class StandardSteadyClockCore final : public SteadyClockCore { +public: + SteadyClockTimePoint GetTimePoint(Core::System& system) override { + return {GetCurrentRawTimePoint(system).ToSeconds(), GetClockSourceId()}; + } + + TimeSpanType GetInternalOffset() const override { + return internal_offset; + } + + void SetInternalOffset(TimeSpanType value) override { + internal_offset = value; + } + + TimeSpanType GetCurrentRawTimePoint(Core::System& system) override; + + void SetSetupValue(TimeSpanType value) { + setup_value = value; + } + +private: + TimeSpanType setup_value{}; + TimeSpanType internal_offset{}; + TimeSpanType cached_raw_time_point{}; +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/standard_user_system_clock_core.cpp b/src/core/hle/service/time/standard_user_system_clock_core.cpp new file mode 100644 index 0000000000..8af17091cd --- /dev/null +++ b/src/core/hle/service/time/standard_user_system_clock_core.cpp @@ -0,0 +1,77 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/assert.h" +#include "core/core.h" +#include "core/hle/kernel/writable_event.h" +#include "core/hle/service/time/standard_local_system_clock_core.h" +#include "core/hle/service/time/standard_network_system_clock_core.h" +#include "core/hle/service/time/standard_user_system_clock_core.h" + +namespace Service::Time::Clock { + +StandardUserSystemClockCore::StandardUserSystemClockCore( + StandardLocalSystemClockCore& local_system_clock_core, + StandardNetworkSystemClockCore& network_system_clock_core, Core::System& system) + : SystemClockCore(local_system_clock_core.GetSteadyClockCore()), + local_system_clock_core{local_system_clock_core}, + network_system_clock_core{network_system_clock_core}, auto_correction_enabled{}, + auto_correction_time{SteadyClockTimePoint::GetRandom()}, + auto_correction_event{Kernel::WritableEvent::CreateEventPair( + system.Kernel(), "StandardUserSystemClockCore:AutoCorrectionEvent")} {} + +ResultCode StandardUserSystemClockCore::SetAutomaticCorrectionEnabled(Core::System& system, + bool value) { + if (const ResultCode result{ApplyAutomaticCorrection(system, value)}; + result != RESULT_SUCCESS) { + return result; + } + + auto_correction_enabled = value; + + return RESULT_SUCCESS; +} + +ResultCode StandardUserSystemClockCore::GetClockContext(Core::System& system, + SystemClockContext& context) const { + if (const ResultCode result{ApplyAutomaticCorrection(system, false)}; + result != RESULT_SUCCESS) { + return result; + } + + return local_system_clock_core.GetClockContext(system, context); +} + +ResultCode StandardUserSystemClockCore::Flush(const SystemClockContext& context) { + UNREACHABLE(); + return ERROR_NOT_IMPLEMENTED; +} + +ResultCode StandardUserSystemClockCore::SetClockContext(const SystemClockContext& context) { + UNREACHABLE(); + return ERROR_NOT_IMPLEMENTED; +} + +ResultCode StandardUserSystemClockCore::ApplyAutomaticCorrection(Core::System& system, + bool value) const { + if (auto_correction_enabled == value) { + return RESULT_SUCCESS; + } + + if (!network_system_clock_core.IsClockSetup(system)) { + return ERROR_UNINITIALIZED_CLOCK; + } + + SystemClockContext context{}; + if (const ResultCode result{network_system_clock_core.GetClockContext(system, context)}; + result != RESULT_SUCCESS) { + return result; + } + + local_system_clock_core.SetClockContext(context); + + return RESULT_SUCCESS; +} + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/standard_user_system_clock_core.h b/src/core/hle/service/time/standard_user_system_clock_core.h new file mode 100644 index 0000000000..ef3d468b70 --- /dev/null +++ b/src/core/hle/service/time/standard_user_system_clock_core.h @@ -0,0 +1,57 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/kernel/writable_event.h" +#include "core/hle/service/time/clock_types.h" +#include "core/hle/service/time/system_clock_core.h" + +namespace Core { +class System; +} + +namespace Service::Time::Clock { + +class StandardLocalSystemClockCore; +class StandardNetworkSystemClockCore; + +class StandardUserSystemClockCore final : public SystemClockCore { +public: + StandardUserSystemClockCore(StandardLocalSystemClockCore& local_system_clock_core, + StandardNetworkSystemClockCore& network_system_clock_core, + Core::System& system); + + ResultCode SetAutomaticCorrectionEnabled(Core::System& system, bool value); + + ResultCode GetClockContext(Core::System& system, SystemClockContext& context) const override; + + bool IsAutomaticCorrectionEnabled() const { + return auto_correction_enabled; + } + + void SetAutomaticCorrectionUpdatedTime(SteadyClockTimePoint steady_clock_time_point) { + auto_correction_time = steady_clock_time_point; + } + +protected: + ResultCode Flush(const SystemClockContext& context) override; + + ResultCode SetClockContext(const SystemClockContext&) override; + + ResultCode ApplyAutomaticCorrection(Core::System& system, bool value) const; + + const SteadyClockTimePoint& GetAutomaticCorrectionUpdatedTime() const { + return auto_correction_time; + } + +private: + StandardLocalSystemClockCore& local_system_clock_core; + StandardNetworkSystemClockCore& network_system_clock_core; + bool auto_correction_enabled{}; + SteadyClockTimePoint auto_correction_time; + Kernel::EventPair auto_correction_event; +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/steady_clock_core.h b/src/core/hle/service/time/steady_clock_core.h new file mode 100644 index 0000000000..84af3d1051 --- /dev/null +++ b/src/core/hle/service/time/steady_clock_core.h @@ -0,0 +1,55 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/uuid.h" +#include "core/hle/service/time/clock_types.h" + +namespace Core { +class System; +} + +namespace Service::Time::Clock { + +class SteadyClockCore { +public: + SteadyClockCore() = default; + + const Common::UUID& GetClockSourceId() const { + return clock_source_id; + } + + void SetClockSourceId(const Common::UUID& value) { + clock_source_id = value; + } + + virtual TimeSpanType GetInternalOffset() const = 0; + + virtual void SetInternalOffset(TimeSpanType internal_offset) = 0; + + virtual SteadyClockTimePoint GetTimePoint(Core::System& system) = 0; + + virtual TimeSpanType GetCurrentRawTimePoint(Core::System& system) = 0; + + SteadyClockTimePoint GetCurrentTimePoint(Core::System& system) { + SteadyClockTimePoint result{GetTimePoint(system)}; + result.time_point += GetInternalOffset().ToSeconds(); + return result; + } + + bool IsInitialized() const { + return is_initialized; + } + + void MarkAsInitialized() { + is_initialized = true; + } + +private: + Common::UUID clock_source_id{Common::UUID::Generate()}; + bool is_initialized{}; +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/system_clock_context_update_callback.cpp b/src/core/hle/service/time/system_clock_context_update_callback.cpp new file mode 100644 index 0000000000..5cdb807036 --- /dev/null +++ b/src/core/hle/service/time/system_clock_context_update_callback.cpp @@ -0,0 +1,55 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/hle/kernel/writable_event.h" +#include "core/hle/service/time/errors.h" +#include "core/hle/service/time/system_clock_context_update_callback.h" + +namespace Service::Time::Clock { + +SystemClockContextUpdateCallback::SystemClockContextUpdateCallback() = default; +SystemClockContextUpdateCallback::~SystemClockContextUpdateCallback() = default; + +bool SystemClockContextUpdateCallback::NeedUpdate(const SystemClockContext& value) const { + if (has_context) { + return context.offset != value.offset || + context.steady_time_point.clock_source_id != value.steady_time_point.clock_source_id; + } + + return true; +} + +void SystemClockContextUpdateCallback::RegisterOperationEvent( + std::shared_ptr&& writable_event) { + operation_event_list.emplace_back(std::move(writable_event)); +} + +void SystemClockContextUpdateCallback::BroadcastOperationEvent() { + for (const auto& writable_event : operation_event_list) { + writable_event->Signal(); + } +} + +ResultCode SystemClockContextUpdateCallback::Update(const SystemClockContext& value) { + ResultCode result{RESULT_SUCCESS}; + + if (NeedUpdate(value)) { + context = value; + has_context = true; + + result = Update(); + + if (result == RESULT_SUCCESS) { + BroadcastOperationEvent(); + } + } + + return result; +} + +ResultCode SystemClockContextUpdateCallback::Update() { + return RESULT_SUCCESS; +} + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/system_clock_context_update_callback.h b/src/core/hle/service/time/system_clock_context_update_callback.h new file mode 100644 index 0000000000..6260de6c30 --- /dev/null +++ b/src/core/hle/service/time/system_clock_context_update_callback.h @@ -0,0 +1,43 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include + +#include "core/hle/service/time/clock_types.h" + +namespace Kernel { +class WritableEvent; +} + +namespace Service::Time::Clock { + +// Parts of this implementation were based on Ryujinx (https://github.com/Ryujinx/Ryujinx/pull/783). +// This code was released under public domain. + +class SystemClockContextUpdateCallback { +public: + SystemClockContextUpdateCallback(); + ~SystemClockContextUpdateCallback(); + + bool NeedUpdate(const SystemClockContext& value) const; + + void RegisterOperationEvent(std::shared_ptr&& writable_event); + + void BroadcastOperationEvent(); + + ResultCode Update(const SystemClockContext& value); + +protected: + virtual ResultCode Update(); + + SystemClockContext context{}; + +private: + bool has_context{}; + std::vector> operation_event_list; +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/system_clock_core.cpp b/src/core/hle/service/time/system_clock_core.cpp new file mode 100644 index 0000000000..1a3ab8cfa6 --- /dev/null +++ b/src/core/hle/service/time/system_clock_core.cpp @@ -0,0 +1,72 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/hle/service/time/steady_clock_core.h" +#include "core/hle/service/time/system_clock_context_update_callback.h" +#include "core/hle/service/time/system_clock_core.h" + +namespace Service::Time::Clock { + +SystemClockCore::SystemClockCore(SteadyClockCore& steady_clock_core) + : steady_clock_core{steady_clock_core}, is_initialized{} { + context.steady_time_point.clock_source_id = steady_clock_core.GetClockSourceId(); +} + +SystemClockCore ::~SystemClockCore() = default; + +ResultCode SystemClockCore::GetCurrentTime(Core::System& system, s64& posix_time) const { + posix_time = 0; + + const SteadyClockTimePoint current_time_point{steady_clock_core.GetCurrentTimePoint(system)}; + + SystemClockContext clock_context{}; + if (const ResultCode result{GetClockContext(system, clock_context)}; result != RESULT_SUCCESS) { + return result; + } + + if (current_time_point.clock_source_id != clock_context.steady_time_point.clock_source_id) { + return ERROR_TIME_MISMATCH; + } + + posix_time = clock_context.offset + current_time_point.time_point; + + return RESULT_SUCCESS; +} + +ResultCode SystemClockCore::SetCurrentTime(Core::System& system, s64 posix_time) { + const SteadyClockTimePoint current_time_point{steady_clock_core.GetCurrentTimePoint(system)}; + const SystemClockContext clock_context{posix_time - current_time_point.time_point, + current_time_point}; + + if (const ResultCode result{SetClockContext(clock_context)}; result != RESULT_SUCCESS) { + return result; + } + return Flush(clock_context); +} + +ResultCode SystemClockCore::Flush(const SystemClockContext& context) { + if (!system_clock_context_update_callback) { + return RESULT_SUCCESS; + } + return system_clock_context_update_callback->Update(context); +} + +ResultCode SystemClockCore::SetSystemClockContext(const SystemClockContext& context) { + if (const ResultCode result{SetClockContext(context)}; result != RESULT_SUCCESS) { + return result; + } + return Flush(context); +} + +bool SystemClockCore::IsClockSetup(Core::System& system) const { + SystemClockContext value{}; + if (GetClockContext(system, value) == RESULT_SUCCESS) { + const SteadyClockTimePoint steady_clock_time_point{ + steady_clock_core.GetCurrentTimePoint(system)}; + return steady_clock_time_point.clock_source_id == value.steady_time_point.clock_source_id; + } + return {}; +} + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/system_clock_core.h b/src/core/hle/service/time/system_clock_core.h new file mode 100644 index 0000000000..54407a6c52 --- /dev/null +++ b/src/core/hle/service/time/system_clock_core.h @@ -0,0 +1,71 @@ +// Copyright 2019 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" +#include "core/hle/service/time/clock_types.h" + +namespace Core { +class System; +} + +namespace Service::Time::Clock { + +class SteadyClockCore; +class SystemClockContextUpdateCallback; + +// Parts of this implementation were based on Ryujinx (https://github.com/Ryujinx/Ryujinx/pull/783). +// This code was released under public domain. + +class SystemClockCore { +public: + explicit SystemClockCore(SteadyClockCore& steady_clock_core); + ~SystemClockCore(); + + SteadyClockCore& GetSteadyClockCore() const { + return steady_clock_core; + } + + ResultCode GetCurrentTime(Core::System& system, s64& posix_time) const; + + ResultCode SetCurrentTime(Core::System& system, s64 posix_time); + + virtual ResultCode GetClockContext([[maybe_unused]] Core::System& system, + SystemClockContext& value) const { + value = context; + return RESULT_SUCCESS; + } + + virtual ResultCode SetClockContext(const SystemClockContext& value) { + context = value; + return RESULT_SUCCESS; + } + + virtual ResultCode Flush(const SystemClockContext& context); + + void SetUpdateCallbackInstance(std::shared_ptr callback) { + system_clock_context_update_callback = std::move(callback); + } + + ResultCode SetSystemClockContext(const SystemClockContext& context); + + bool IsInitialized() const { + return is_initialized; + } + + void MarkAsInitialized() { + is_initialized = true; + } + + bool IsClockSetup(Core::System& system) const; + +private: + SteadyClockCore& steady_clock_core; + SystemClockContext context{}; + bool is_initialized{}; + std::shared_ptr system_clock_context_update_callback; +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/tick_based_steady_clock_core.cpp b/src/core/hle/service/time/tick_based_steady_clock_core.cpp new file mode 100644 index 0000000000..c77b981893 --- /dev/null +++ b/src/core/hle/service/time/tick_based_steady_clock_core.cpp @@ -0,0 +1,24 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/core.h" +#include "core/core_timing.h" +#include "core/core_timing_util.h" +#include "core/hle/service/time/tick_based_steady_clock_core.h" + +namespace Service::Time::Clock { + +SteadyClockTimePoint TickBasedSteadyClockCore::GetTimePoint(Core::System& system) { + const TimeSpanType ticks_time_span{TimeSpanType::FromTicks( + Core::Timing::CpuCyclesToClockCycles(system.CoreTiming().GetTicks()), + Core::Timing::CNTFREQ)}; + + return {ticks_time_span.ToSeconds(), GetClockSourceId()}; +} + +TimeSpanType TickBasedSteadyClockCore::GetCurrentRawTimePoint(Core::System& system) { + return TimeSpanType::FromSeconds(GetTimePoint(system).time_point); +} + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/tick_based_steady_clock_core.h b/src/core/hle/service/time/tick_based_steady_clock_core.h new file mode 100644 index 0000000000..1a5a53fd78 --- /dev/null +++ b/src/core/hle/service/time/tick_based_steady_clock_core.h @@ -0,0 +1,29 @@ +// Copyright 2020 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "core/hle/service/time/clock_types.h" +#include "core/hle/service/time/steady_clock_core.h" + +namespace Core { +class System; +} + +namespace Service::Time::Clock { + +class TickBasedSteadyClockCore final : public SteadyClockCore { +public: + TimeSpanType GetInternalOffset() const override { + return {}; + } + + void SetInternalOffset(TimeSpanType internal_offset) override {} + + SteadyClockTimePoint GetTimePoint(Core::System& system) override; + + TimeSpanType GetCurrentRawTimePoint(Core::System& system) override; +}; + +} // namespace Service::Time::Clock diff --git a/src/core/hle/service/time/time.cpp b/src/core/hle/service/time/time.cpp index 6ee77c5f91..970aed0bbb 100644 --- a/src/core/hle/service/time/time.cpp +++ b/src/core/hle/service/time/time.cpp @@ -1,9 +1,7 @@ -// Copyright 2018 yuzu emulator team +// Copyright 2019 yuzu emulator team // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include -#include #include "common/logging/log.h" #include "core/core.h" #include "core/core_timing.h" @@ -11,429 +9,282 @@ #include "core/hle/ipc_helpers.h" #include "core/hle/kernel/client_port.h" #include "core/hle/kernel/client_session.h" +#include "core/hle/kernel/scheduler.h" #include "core/hle/service/time/interface.h" #include "core/hle/service/time/time.h" #include "core/hle/service/time/time_sharedmemory.h" -#include "core/settings.h" +#include "core/hle/service/time/time_zone_service.h" namespace Service::Time { -static std::chrono::seconds GetSecondsSinceEpoch() { - return std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) + - Settings::values.custom_rtc_differential; -} - -static void PosixToCalendar(u64 posix_time, CalendarTime& calendar_time, - CalendarAdditionalInfo& additional_info, - [[maybe_unused]] const TimeZoneRule& /*rule*/) { - const std::time_t time(posix_time); - const std::tm* tm = std::localtime(&time); - if (tm == nullptr) { - calendar_time = {}; - additional_info = {}; - return; - } - calendar_time.year = static_cast(tm->tm_year + 1900); - calendar_time.month = static_cast(tm->tm_mon + 1); - calendar_time.day = static_cast(tm->tm_mday); - calendar_time.hour = static_cast(tm->tm_hour); - calendar_time.minute = static_cast(tm->tm_min); - calendar_time.second = static_cast(tm->tm_sec); - - additional_info.day_of_week = tm->tm_wday; - additional_info.day_of_year = tm->tm_yday; - std::memcpy(additional_info.name.data(), "UTC", sizeof("UTC")); - additional_info.utc_offset = 0; -} - -static u64 CalendarToPosix(const CalendarTime& calendar_time, - [[maybe_unused]] const TimeZoneRule& /*rule*/) { - std::tm time{}; - time.tm_year = calendar_time.year - 1900; - time.tm_mon = calendar_time.month - 1; - time.tm_mday = calendar_time.day; - - time.tm_hour = calendar_time.hour; - time.tm_min = calendar_time.minute; - time.tm_sec = calendar_time.second; - - std::time_t epoch_time = std::mktime(&time); - return static_cast(epoch_time); -} - -enum class ClockContextType { - StandardSteady, - StandardUserSystem, - StandardNetworkSystem, - StandardLocalSystem, -}; - class ISystemClock final : public ServiceFramework { public: - ISystemClock(std::shared_ptr shared_memory, - ClockContextType clock_type) - : ServiceFramework("ISystemClock"), shared_memory(shared_memory), clock_type(clock_type) { + ISystemClock(Clock::SystemClockCore& clock_core) + : ServiceFramework("ISystemClock"), clock_core{clock_core} { // clang-format off static const FunctionInfo functions[] = { {0, &ISystemClock::GetCurrentTime, "GetCurrentTime"}, {1, nullptr, "SetCurrentTime"}, - {2, &ISystemClock::GetSystemClockContext, "GetSystemClockContext"}, + {2, &ISystemClock::GetSystemClockContext, "GetSystemClockContext"}, {3, nullptr, "SetSystemClockContext"}, {4, nullptr, "GetOperationEventReadableHandle"}, }; // clang-format on RegisterHandlers(functions); - UpdateSharedMemoryContext(system_clock_context); } private: void GetCurrentTime(Kernel::HLERequestContext& ctx) { - const s64 time_since_epoch{GetSecondsSinceEpoch().count()}; LOG_DEBUG(Service_Time, "called"); + if (!clock_core.IsInitialized()) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ERROR_UNINITIALIZED_CLOCK); + return; + } + + s64 posix_time{}; + if (const ResultCode result{ + clock_core.GetCurrentTime(Core::System::GetInstance(), posix_time)}; + result != RESULT_SUCCESS) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(result); + return; + } + IPC::ResponseBuilder rb{ctx, 4}; rb.Push(RESULT_SUCCESS); - rb.Push(time_since_epoch); + rb.Push(posix_time); } void GetSystemClockContext(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_Time, "(STUBBED) called"); + LOG_DEBUG(Service_Time, "called"); - // TODO(ogniK): This should be updated periodically however since we have it stubbed we'll - // only update when we get a new context - UpdateSharedMemoryContext(system_clock_context); + if (!clock_core.IsInitialized()) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ERROR_UNINITIALIZED_CLOCK); + return; + } - IPC::ResponseBuilder rb{ctx, (sizeof(SystemClockContext) / 4) + 2}; + Clock::SystemClockContext system_clock_context{}; + if (const ResultCode result{ + clock_core.GetClockContext(Core::System::GetInstance(), system_clock_context)}; + result != RESULT_SUCCESS) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(result); + return; + } + + IPC::ResponseBuilder rb{ctx, sizeof(Clock::SystemClockContext) / 4 + 2}; rb.Push(RESULT_SUCCESS); rb.PushRaw(system_clock_context); } - void UpdateSharedMemoryContext(const SystemClockContext& clock_context) { - switch (clock_type) { - case ClockContextType::StandardLocalSystem: - shared_memory->SetStandardLocalSystemClockContext(clock_context); - break; - case ClockContextType::StandardNetworkSystem: - shared_memory->SetStandardNetworkSystemClockContext(clock_context); - break; - } - } - - SystemClockContext system_clock_context{}; - std::shared_ptr shared_memory; - ClockContextType clock_type; + Clock::SystemClockCore& clock_core; }; class ISteadyClock final : public ServiceFramework { public: - ISteadyClock(std::shared_ptr shared_memory, Core::System& system) - : ServiceFramework("ISteadyClock"), shared_memory(shared_memory), system(system) { + ISteadyClock(Clock::SteadyClockCore& clock_core) + : ServiceFramework("ISteadyClock"), clock_core{clock_core} { static const FunctionInfo functions[] = { {0, &ISteadyClock::GetCurrentTimePoint, "GetCurrentTimePoint"}, }; RegisterHandlers(functions); - - shared_memory->SetStandardSteadyClockTimepoint(GetCurrentTimePoint()); } private: void GetCurrentTimePoint(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_Time, "called"); - const auto time_point = GetCurrentTimePoint(); - // TODO(ogniK): This should be updated periodically - shared_memory->SetStandardSteadyClockTimepoint(time_point); + if (!clock_core.IsInitialized()) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ERROR_UNINITIALIZED_CLOCK); + return; + } - IPC::ResponseBuilder rb{ctx, (sizeof(SteadyClockTimePoint) / 4) + 2}; + const Clock::SteadyClockTimePoint time_point{ + clock_core.GetCurrentTimePoint(Core::System::GetInstance())}; + IPC::ResponseBuilder rb{ctx, (sizeof(Clock::SteadyClockTimePoint) / 4) + 2}; rb.Push(RESULT_SUCCESS); rb.PushRaw(time_point); } - SteadyClockTimePoint GetCurrentTimePoint() const { - const auto& core_timing = system.CoreTiming(); - const auto ms = Core::Timing::CyclesToMs(core_timing.GetTicks()); - return {static_cast(ms.count() / 1000), {}}; - } - - std::shared_ptr shared_memory; - Core::System& system; + Clock::SteadyClockCore& clock_core; }; -class ITimeZoneService final : public ServiceFramework { -public: - ITimeZoneService() : ServiceFramework("ITimeZoneService") { - // clang-format off - static const FunctionInfo functions[] = { - {0, &ITimeZoneService::GetDeviceLocationName, "GetDeviceLocationName"}, - {1, nullptr, "SetDeviceLocationName"}, - {2, &ITimeZoneService::GetTotalLocationNameCount, "GetTotalLocationNameCount"}, - {3, nullptr, "LoadLocationNameList"}, - {4, &ITimeZoneService::LoadTimeZoneRule, "LoadTimeZoneRule"}, - {5, nullptr, "GetTimeZoneRuleVersion"}, - {6, nullptr, "GetDeviceLocationNameAndUpdatedTime"}, - {7, nullptr, "SetDeviceLocationNameWithTimeZoneRule"}, - {8, nullptr, "ParseTimeZoneBinary"}, - {20, nullptr, "GetDeviceLocationNameOperationEventReadableHandle"}, - {100, &ITimeZoneService::ToCalendarTime, "ToCalendarTime"}, - {101, &ITimeZoneService::ToCalendarTimeWithMyRule, "ToCalendarTimeWithMyRule"}, - {201, &ITimeZoneService::ToPosixTime, "ToPosixTime"}, - {202, &ITimeZoneService::ToPosixTimeWithMyRule, "ToPosixTimeWithMyRule"}, - }; - // clang-format on +ResultCode Module::Interface::GetClockSnapshotFromSystemClockContextInternal( + Kernel::Thread* thread, Clock::SystemClockContext user_context, + Clock::SystemClockContext network_context, u8 type, Clock::ClockSnapshot& clock_snapshot) { - RegisterHandlers(functions); + auto& time_manager{module->GetTimeManager()}; + + clock_snapshot.is_automatic_correction_enabled = + time_manager.GetStandardUserSystemClockCore().IsAutomaticCorrectionEnabled(); + clock_snapshot.user_context = user_context; + clock_snapshot.network_context = network_context; + + if (const ResultCode result{ + time_manager.GetTimeZoneContentManager().GetTimeZoneManager().GetDeviceLocationName( + clock_snapshot.location_name)}; + result != RESULT_SUCCESS) { + return result; } -private: - LocationName location_name{"UTC"}; - TimeZoneRule my_time_zone_rule{}; - - void GetDeviceLocationName(Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_Time, "called"); - - IPC::ResponseBuilder rb{ctx, (sizeof(LocationName) / 4) + 2}; - rb.Push(RESULT_SUCCESS); - rb.PushRaw(location_name); + const auto current_time_point{ + time_manager.GetStandardSteadyClockCore().GetCurrentTimePoint(Core::System::GetInstance())}; + if (const ResultCode result{Clock::ClockSnapshot::GetCurrentTime( + clock_snapshot.user_time, current_time_point, clock_snapshot.user_context)}; + result != RESULT_SUCCESS) { + return result; } - void GetTotalLocationNameCount(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_Time, "(STUBBED) called"); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(RESULT_SUCCESS); - rb.Push(0); + TimeZone::CalendarInfo userCalendarInfo{}; + if (const ResultCode result{ + time_manager.GetTimeZoneContentManager().GetTimeZoneManager().ToCalendarTimeWithMyRules( + clock_snapshot.user_time, userCalendarInfo)}; + result != RESULT_SUCCESS) { + return result; } - void LoadTimeZoneRule(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_Time, "(STUBBED) called"); + clock_snapshot.user_calendar_time = userCalendarInfo.time; + clock_snapshot.user_calendar_additional_time = userCalendarInfo.additiona_info; - ctx.WriteBuffer(&my_time_zone_rule, sizeof(TimeZoneRule)); - - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(RESULT_SUCCESS); + if (Clock::ClockSnapshot::GetCurrentTime(clock_snapshot.network_time, current_time_point, + clock_snapshot.network_context) != RESULT_SUCCESS) { + clock_snapshot.network_time = 0; } - void ToCalendarTime(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u64 posix_time = rp.Pop(); - LOG_WARNING(Service_Time, "(STUBBED) called, posix_time=0x{:016X}", posix_time); - - TimeZoneRule time_zone_rule{}; - auto buffer = ctx.ReadBuffer(); - std::memcpy(&time_zone_rule, buffer.data(), buffer.size()); - - CalendarTime calendar_time{2018, 1, 1, 0, 0, 0}; - CalendarAdditionalInfo additional_info{}; - - PosixToCalendar(posix_time, calendar_time, additional_info, time_zone_rule); - - IPC::ResponseBuilder rb{ctx, 10}; - rb.Push(RESULT_SUCCESS); - rb.PushRaw(calendar_time); - rb.PushRaw(additional_info); + TimeZone::CalendarInfo networkCalendarInfo{}; + if (const ResultCode result{ + time_manager.GetTimeZoneContentManager().GetTimeZoneManager().ToCalendarTimeWithMyRules( + clock_snapshot.network_time, networkCalendarInfo)}; + result != RESULT_SUCCESS) { + return result; } - void ToCalendarTimeWithMyRule(Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const u64 posix_time = rp.Pop(); - LOG_WARNING(Service_Time, "(STUBBED) called, posix_time=0x{:016X}", posix_time); + clock_snapshot.network_calendar_time = networkCalendarInfo.time; + clock_snapshot.network_calendar_additional_time = networkCalendarInfo.additiona_info; + clock_snapshot.type = type; - CalendarTime calendar_time{2018, 1, 1, 0, 0, 0}; - CalendarAdditionalInfo additional_info{}; - - PosixToCalendar(posix_time, calendar_time, additional_info, my_time_zone_rule); - - IPC::ResponseBuilder rb{ctx, 10}; - rb.Push(RESULT_SUCCESS); - rb.PushRaw(calendar_time); - rb.PushRaw(additional_info); - } - - void ToPosixTime(Kernel::HLERequestContext& ctx) { - // TODO(ogniK): Figure out how to handle multiple times - LOG_WARNING(Service_Time, "(STUBBED) called"); - - IPC::RequestParser rp{ctx}; - auto calendar_time = rp.PopRaw(); - auto posix_time = CalendarToPosix(calendar_time, {}); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(RESULT_SUCCESS); - rb.PushRaw(1); // Amount of times we're returning - ctx.WriteBuffer(&posix_time, sizeof(u64)); - } - - void ToPosixTimeWithMyRule(Kernel::HLERequestContext& ctx) { - LOG_WARNING(Service_Time, "(STUBBED) called"); - - IPC::RequestParser rp{ctx}; - auto calendar_time = rp.PopRaw(); - auto posix_time = CalendarToPosix(calendar_time, {}); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(RESULT_SUCCESS); - rb.PushRaw(1); // Amount of times we're returning - ctx.WriteBuffer(&posix_time, sizeof(u64)); - } -}; + return RESULT_SUCCESS; +} void Module::Interface::GetStandardUserSystemClock(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_Time, "called"); - IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(RESULT_SUCCESS); - rb.PushIpcInterface(shared_memory, ClockContextType::StandardUserSystem); + rb.PushIpcInterface(module->GetTimeManager().GetStandardUserSystemClockCore()); } void Module::Interface::GetStandardNetworkSystemClock(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_Time, "called"); - IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(RESULT_SUCCESS); - rb.PushIpcInterface(shared_memory, ClockContextType::StandardNetworkSystem); + rb.PushIpcInterface(module->GetTimeManager().GetStandardNetworkSystemClockCore()); } void Module::Interface::GetStandardSteadyClock(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_Time, "called"); - IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(RESULT_SUCCESS); - rb.PushIpcInterface(shared_memory, system); + rb.PushIpcInterface(module->GetTimeManager().GetStandardSteadyClockCore()); } void Module::Interface::GetTimeZoneService(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_Time, "called"); - IPC::ResponseBuilder rb{ctx, 2, 0, 1}; rb.Push(RESULT_SUCCESS); - rb.PushIpcInterface(); + rb.PushIpcInterface(module->GetTimeManager().GetTimeZoneContentManager()); } -void Module::Interface::GetStandardLocalSystemClock(Kernel::HLERequestContext& ctx) { +void Module::Interface::CalculateMonotonicSystemClockBaseTimePoint(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_Time, "called"); - IPC::ResponseBuilder rb{ctx, 2, 0, 1}; - rb.Push(RESULT_SUCCESS); - rb.PushIpcInterface(shared_memory, ClockContextType::StandardLocalSystem); + auto& steady_clock_core{module->GetTimeManager().GetStandardSteadyClockCore()}; + if (!steady_clock_core.IsInitialized()) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ERROR_UNINITIALIZED_CLOCK); + return; + } + + IPC::RequestParser rp{ctx}; + const auto context{rp.PopRaw()}; + const auto current_time_point{ + steady_clock_core.GetCurrentTimePoint(Core::System::GetInstance())}; + + if (current_time_point.clock_source_id == context.steady_time_point.clock_source_id) { + const auto ticks{Clock::TimeSpanType::FromTicks( + Core::Timing::CpuCyclesToClockCycles(system.CoreTiming().GetTicks()), + Core::Timing::CNTFREQ)}; + const s64 base_time_point{context.offset + current_time_point.time_point - + ticks.ToSeconds()}; + IPC::ResponseBuilder rb{ctx, (sizeof(s64) / 4) + 2}; + rb.Push(RESULT_SUCCESS); + rb.PushRaw(base_time_point); + return; + } + + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(ERROR_TIME_MISMATCH); } void Module::Interface::GetClockSnapshot(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_Time, "called"); - IPC::RequestParser rp{ctx}; - const auto initial_type = rp.PopRaw(); + const auto type = rp.PopRaw(); - const s64 time_since_epoch{GetSecondsSinceEpoch().count()}; - const std::time_t time(time_since_epoch); - const std::tm* tm = std::localtime(&time); - if (tm == nullptr) { - LOG_ERROR(Service_Time, "tm is a nullptr"); + Clock::SystemClockContext user_context{}; + if (const ResultCode result{ + module->GetTimeManager().GetStandardUserSystemClockCore().GetClockContext( + Core::System::GetInstance(), user_context)}; + result != RESULT_SUCCESS) { IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(RESULT_UNKNOWN); // TODO(ogniK): Find appropriate error code + rb.Push(result); + return; + } + Clock::SystemClockContext network_context{}; + if (const ResultCode result{ + module->GetTimeManager().GetStandardNetworkSystemClockCore().GetClockContext( + Core::System::GetInstance(), network_context)}; + result != RESULT_SUCCESS) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(result); return; } - const auto& core_timing = system.CoreTiming(); - const auto ms = Core::Timing::CyclesToMs(core_timing.GetTicks()); - const SteadyClockTimePoint steady_clock_time_point{static_cast(ms.count() / 1000), {}}; - - CalendarTime calendar_time{}; - calendar_time.year = static_cast(tm->tm_year + 1900); - calendar_time.month = static_cast(tm->tm_mon + 1); - calendar_time.day = static_cast(tm->tm_mday); - calendar_time.hour = static_cast(tm->tm_hour); - calendar_time.minute = static_cast(tm->tm_min); - calendar_time.second = static_cast(tm->tm_sec); - - ClockSnapshot clock_snapshot{}; - clock_snapshot.system_posix_time = time_since_epoch; - clock_snapshot.network_posix_time = time_since_epoch; - clock_snapshot.system_calendar_time = calendar_time; - clock_snapshot.network_calendar_time = calendar_time; - - CalendarAdditionalInfo additional_info{}; - PosixToCalendar(time_since_epoch, calendar_time, additional_info, {}); - - clock_snapshot.system_calendar_info = additional_info; - clock_snapshot.network_calendar_info = additional_info; - - clock_snapshot.steady_clock_timepoint = steady_clock_time_point; - clock_snapshot.location_name = LocationName{"UTC"}; - clock_snapshot.clock_auto_adjustment_enabled = 1; - clock_snapshot.type = initial_type; + Clock::ClockSnapshot clock_snapshot{}; + if (const ResultCode result{GetClockSnapshotFromSystemClockContextInternal( + &ctx.GetThread(), user_context, network_context, type, clock_snapshot)}; + result != RESULT_SUCCESS) { + IPC::ResponseBuilder rb{ctx, 2}; + rb.Push(result); + return; + } IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_SUCCESS); - ctx.WriteBuffer(&clock_snapshot, sizeof(ClockSnapshot)); -} - -void Module::Interface::CalculateStandardUserSystemClockDifferenceByUser( - Kernel::HLERequestContext& ctx) { - LOG_DEBUG(Service_Time, "called"); - - IPC::RequestParser rp{ctx}; - const auto snapshot_a = rp.PopRaw(); - const auto snapshot_b = rp.PopRaw(); - const u64 difference = - snapshot_b.user_clock_context.offset - snapshot_a.user_clock_context.offset; - - IPC::ResponseBuilder rb{ctx, 4}; - rb.Push(RESULT_SUCCESS); - rb.PushRaw(difference); + ctx.WriteBuffer(&clock_snapshot, sizeof(Clock::ClockSnapshot)); } void Module::Interface::GetSharedMemoryNativeHandle(Kernel::HLERequestContext& ctx) { LOG_DEBUG(Service_Time, "called"); IPC::ResponseBuilder rb{ctx, 2, 1}; rb.Push(RESULT_SUCCESS); - rb.PushCopyObjects(shared_memory->GetSharedMemoryHolder()); + rb.PushCopyObjects(module->GetTimeManager().GetSharedMemory().GetSharedMemoryHolder()); } -void Module::Interface::IsStandardUserSystemClockAutomaticCorrectionEnabled( - Kernel::HLERequestContext& ctx) { - // ogniK(TODO): When clock contexts are implemented, the value should be read from the context - // instead of our shared memory holder - LOG_DEBUG(Service_Time, "called"); - - IPC::ResponseBuilder rb{ctx, 3}; - rb.Push(RESULT_SUCCESS); - rb.Push(shared_memory->GetStandardUserSystemClockAutomaticCorrectionEnabled()); -} - -void Module::Interface::SetStandardUserSystemClockAutomaticCorrectionEnabled( - Kernel::HLERequestContext& ctx) { - IPC::RequestParser rp{ctx}; - const auto enabled = rp.Pop(); - - LOG_WARNING(Service_Time, "(PARTIAL IMPLEMENTATION) called"); - - // TODO(ogniK): Update clock contexts and correct timespans - - shared_memory->SetStandardUserSystemClockAutomaticCorrectionEnabled(enabled > 0); - IPC::ResponseBuilder rb{ctx, 2}; - rb.Push(RESULT_SUCCESS); -} - -Module::Interface::Interface(std::shared_ptr time, - std::shared_ptr shared_memory, Core::System& system, - const char* name) - : ServiceFramework(name), time(std::move(time)), shared_memory(std::move(shared_memory)), - system(system) {} +Module::Interface::Interface(std::shared_ptr module, Core::System& system, const char* name) + : ServiceFramework(name), module{std::move(module)}, system{system} {} Module::Interface::~Interface() = default; void InstallInterfaces(Core::System& system) { - auto time = std::make_shared(); - auto shared_mem = std::make_shared(system); - - std::make_shared