diff --git a/libraries/libmesosphere/source/kern_k_dpc_manager.cpp b/libraries/libmesosphere/source/kern_k_dpc_manager.cpp index 5666a2763..bef4397aa 100644 --- a/libraries/libmesosphere/source/kern_k_dpc_manager.cpp +++ b/libraries/libmesosphere/source/kern_k_dpc_manager.cpp @@ -79,7 +79,7 @@ namespace ams::kern { /* Convenience definitions. */ constexpr s32 DpcManagerThreadPriority = 3; - constexpr s64 DpcManagerTimeout = 192'000ll; /* TODO: Constexpr conversion from 10ms */ + constexpr s64 DpcManagerTimeout = ams::svc::Tick(TimeSpan::FromMilliSeconds(10)); /* Globals. */ s64 g_preemption_priorities[cpu::NumCores]; diff --git a/libraries/libmesosphere/source/kern_k_resource_limit.cpp b/libraries/libmesosphere/source/kern_k_resource_limit.cpp index 25ee89f82..7261919be 100644 --- a/libraries/libmesosphere/source/kern_k_resource_limit.cpp +++ b/libraries/libmesosphere/source/kern_k_resource_limit.cpp @@ -17,6 +17,12 @@ namespace ams::kern { + namespace { + + constexpr s64 DefaultTimeout = ams::svc::Tick(TimeSpan::FromSeconds(10)); + + } + void KResourceLimit::Initialize() { /* This should be unnecessary for us, because our constructor will clear all fields. */ /* The following is analagous to what Nintendo's implementation (no constexpr constructor) would do, though. */ @@ -91,8 +97,7 @@ namespace ams::kern { } bool KResourceLimit::Reserve(ams::svc::LimitableResource which, s64 value) { - /* TODO: constexpr definition for this default timeout (it's 10 seconds) */ - return this->Reserve(which, value, KHardwareTimer::GetTick() + 192'000'000); + return this->Reserve(which, value, KHardwareTimer::GetTick() + DefaultTimeout); } bool KResourceLimit::Reserve(ams::svc::LimitableResource which, s64 value, s64 timeout) { diff --git a/libraries/libstratosphere/source/map/map_api.cpp b/libraries/libstratosphere/source/map/map_api.cpp index 2bbffff44..5c482c903 100644 --- a/libraries/libstratosphere/source/map/map_api.cpp +++ b/libraries/libstratosphere/source/map/map_api.cpp @@ -100,7 +100,7 @@ namespace ams::map { MappedCodeMemory tmp_mcm(process_handle, try_address, base_address, size); R_TRY_CATCH(tmp_mcm.GetResult()) { - R_CATCH(svc::ResultInvalidCurrentMemoryState) { continue; } + R_CATCH(svc::ResultInvalidCurrentMemory) { continue; } } R_END_TRY_CATCH; if (!CanAddGuardRegionsInProcess(process_handle, try_address, size)) { @@ -136,7 +136,7 @@ namespace ams::map { MappedCodeMemory tmp_mcm(process_handle, try_address, base_address, size); R_TRY_CATCH(tmp_mcm.GetResult()) { - R_CATCH(svc::ResultInvalidCurrentMemoryState) { continue; } + R_CATCH(svc::ResultInvalidCurrentMemory) { continue; } } R_END_TRY_CATCH; if (!CanAddGuardRegionsInProcess(process_handle, try_address, size)) { diff --git a/libraries/libvapours/include/vapours.hpp b/libraries/libvapours/include/vapours.hpp index 431313b80..68cadda3e 100644 --- a/libraries/libvapours/include/vapours.hpp +++ b/libraries/libvapours/include/vapours.hpp @@ -19,6 +19,8 @@ #include "vapours/defines.hpp" #include "vapours/literals.hpp" +#include "vapours/timespan.hpp" + #include "vapours/util.hpp" #include "vapours/results.hpp" #include "vapours/svc.hpp" diff --git a/libraries/libvapours/include/vapours/results/svc_results.hpp b/libraries/libvapours/include/vapours/results/svc_results.hpp index b6a170531..0d797fb3a 100644 --- a/libraries/libvapours/include/vapours/results/svc_results.hpp +++ b/libraries/libvapours/include/vapours/results/svc_results.hpp @@ -38,7 +38,7 @@ namespace ams::svc { R_DEFINE_ERROR_RESULT(OutOfResource, 103); R_DEFINE_ERROR_RESULT(OutOfMemory, 104); R_DEFINE_ERROR_RESULT(OutOfHandles, 105); - R_DEFINE_ERROR_RESULT(InvalidCurrentMemoryState, 106); + R_DEFINE_ERROR_RESULT(InvalidCurrentMemory, 106); R_DEFINE_ERROR_RESULT(InvalidNewMemoryPermissions, 108); diff --git a/libraries/libvapours/include/vapours/svc/board/nintendo/switch/svc_device_name.hpp b/libraries/libvapours/include/vapours/svc/board/nintendo/switch/svc_device_name.hpp index 9f4ffafb8..f1175d773 100644 --- a/libraries/libvapours/include/vapours/svc/board/nintendo/switch/svc_device_name.hpp +++ b/libraries/libvapours/include/vapours/svc/board/nintendo/switch/svc_device_name.hpp @@ -16,7 +16,7 @@ #pragma once #include -namespace ams::svc { +namespace ams::svc::board::nintendo_switch { enum DeviceName { DeviceName_Afi = 0, @@ -62,4 +62,4 @@ namespace ams::svc { DeviceName_Count, }; -} \ No newline at end of file +} diff --git a/libraries/libvapours/include/vapours/svc/board/nintendo/switch/svc_hardware_constants.hpp b/libraries/libvapours/include/vapours/svc/board/nintendo/switch/svc_hardware_constants.hpp new file mode 100644 index 000000000..55d95f04e --- /dev/null +++ b/libraries/libvapours/include/vapours/svc/board/nintendo/switch/svc_hardware_constants.hpp @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once +#include + +namespace ams::svc::board::nintendo_switch { + + constexpr inline const s64 TicksPerSecond = 19'200'000; + +} diff --git a/libraries/libvapours/include/vapours/svc/svc_select_device_name.hpp b/libraries/libvapours/include/vapours/svc/svc_select_device_name.hpp new file mode 100644 index 000000000..190350676 --- /dev/null +++ b/libraries/libvapours/include/vapours/svc/svc_select_device_name.hpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include "svc_common.hpp" + +#if defined(ATMOSPHERE_BOARD_NINTENDO_SWITCH) + + #include "board/nintendo/switch/svc_device_name.hpp" + namespace ams::svc { + using namespace ams::svc::board::nintendo_switch; + } + +#else + + #error "Unknown board for svc::DeviceName" + +#endif diff --git a/libraries/libvapours/include/vapours/svc/svc_select_hardware_constants.hpp b/libraries/libvapours/include/vapours/svc/svc_select_hardware_constants.hpp new file mode 100644 index 000000000..c7bba8105 --- /dev/null +++ b/libraries/libvapours/include/vapours/svc/svc_select_hardware_constants.hpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include "svc_common.hpp" + +#if defined(ATMOSPHERE_BOARD_NINTENDO_SWITCH) + + #include "board/nintendo/switch/svc_hardware_constants.hpp" + namespace ams::svc { + using namespace ams::svc::board::nintendo_switch; + } + +#else + + #error "Unknown board for svc::DeviceName" + +#endif diff --git a/libraries/libvapours/include/vapours/svc/svc_tick.hpp b/libraries/libvapours/include/vapours/svc/svc_tick.hpp new file mode 100644 index 000000000..bc393cffa --- /dev/null +++ b/libraries/libvapours/include/vapours/svc/svc_tick.hpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include "svc_common.hpp" +#include "svc_select_hardware_constants.hpp" + +namespace ams::svc { + + class Tick { + public: + static constexpr s64 TicksPerSecond = ::ams::svc::TicksPerSecond; + static constexpr s64 GetTicksPerSecond() { return TicksPerSecond; } + private: + s64 tick; + private: + static constexpr s64 NanoSecondsPerSecond = INT64_C(1'000'000'000); + + static constexpr void DivNs(s64 &out, const s64 value) { + out = value / NanoSecondsPerSecond; + } + + static constexpr void DivModNs(s64 &out_div, s64 &out_mod, const s64 value) { + out_div = value / NanoSecondsPerSecond; + out_mod = value % NanoSecondsPerSecond; + } + + static constexpr s64 ConvertTimeSpanToTickImpl(TimeSpan ts) { + /* Split up timespan and ticks-per-second by ns. */ + s64 ts_div = 0, ts_mod = 0; + s64 tick_div = 0, tick_mod = 0; + DivModNs(ts_div, ts_mod, ts.GetNanoSeconds()); + DivModNs(tick_div, tick_mod, TicksPerSecond); + + /* Convert the timespan into a tick count. */ + s64 value = 0; + DivNs(value, ts_mod * tick_mod + NanoSecondsPerSecond - 1); + + return (ts_div * tick_div) * NanoSecondsPerSecond + ts_div * tick_mod + ts_mod * tick_div + value; + } + public: + constexpr explicit Tick(s64 t = 0) : tick(t) { /* ... */ } + constexpr Tick(TimeSpan ts) : tick(ConvertTimeSpanToTickImpl(ts)) { /* ... */ } + + constexpr operator s64() const { return this->tick; } + + /* Tick arithmetic. */ + constexpr Tick &operator+=(Tick rhs) { this->tick += rhs.tick; return *this; } + constexpr Tick &operator-=(Tick rhs) { this->tick -= rhs.tick; return *this; } + constexpr Tick operator+(Tick rhs) const { Tick r(*this); return r += rhs; } + constexpr Tick operator-(Tick rhs) const { Tick r(*this); return r -= rhs; } + + constexpr Tick &operator+=(TimeSpan rhs) { this->tick += Tick(rhs).tick; return *this; } + constexpr Tick &operator-=(TimeSpan rhs) { this->tick -= Tick(rhs).tick; return *this; } + constexpr Tick operator+(TimeSpan rhs) const { Tick r(*this); return r += rhs; } + constexpr Tick operator-(TimeSpan rhs) const { Tick r(*this); return r -= rhs; } + }; + +} diff --git a/libraries/libvapours/include/vapours/svc/svc_types.hpp b/libraries/libvapours/include/vapours/svc/svc_types.hpp index 70ebb09a3..078172c16 100644 --- a/libraries/libvapours/include/vapours/svc/svc_types.hpp +++ b/libraries/libvapours/include/vapours/svc/svc_types.hpp @@ -16,6 +16,7 @@ #pragma once #include "svc_common.hpp" +#include "svc_tick.hpp" #include "svc_types_common.hpp" #include "svc_types_base.hpp" #include "svc_types_dd.hpp" diff --git a/libraries/libvapours/include/vapours/svc/svc_types_dd.hpp b/libraries/libvapours/include/vapours/svc/svc_types_dd.hpp index ece84ef6e..2a2529496 100644 --- a/libraries/libvapours/include/vapours/svc/svc_types_dd.hpp +++ b/libraries/libvapours/include/vapours/svc/svc_types_dd.hpp @@ -15,16 +15,8 @@ */ #pragma once #include "svc_types_common.hpp" - -#ifdef ATMOSPHERE_BOARD_NINTENDO_SWITCH - - #include "board/nintendo/switch/svc_device_name.hpp" - -#else - - #error "Unknown board for svc::DeviceName" - -#endif +#include "svc_select_hardware_constants.hpp" +#include "svc_select_device_name.hpp" namespace ams::svc { diff --git a/libraries/libvapours/include/vapours/timespan.hpp b/libraries/libvapours/include/vapours/timespan.hpp new file mode 100644 index 000000000..b269e5d4b --- /dev/null +++ b/libraries/libvapours/include/vapours/timespan.hpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include "defines.hpp" +#include + +namespace ams { + + class TimeSpan { + private: + s64 ns; + private: + constexpr explicit ALWAYS_INLINE TimeSpan(s64 v) : ns(v) { /* ... */ } + public: + constexpr explicit ALWAYS_INLINE TimeSpan() : TimeSpan(0) { /* ... */ } + + static constexpr ALWAYS_INLINE TimeSpan FromNanoSeconds(s64 ns) { return TimeSpan(ns); } + static constexpr ALWAYS_INLINE TimeSpan FromMicroSeconds(s64 ms) { return FromNanoSeconds(ms * INT64_C(1000)); } + static constexpr ALWAYS_INLINE TimeSpan FromMilliSeconds(s64 ms) { return FromMicroSeconds(ms * INT64_C(1000)); } + static constexpr ALWAYS_INLINE TimeSpan FromSeconds(s64 s) { return FromMilliSeconds(s * INT64_C(1000)); } + static constexpr ALWAYS_INLINE TimeSpan FromMinutes(s64 m) { return FromSeconds(m * INT64_C(60)); } + static constexpr ALWAYS_INLINE TimeSpan FromHours(s64 h) { return FromMinutes(h * INT64_C(60)); } + static constexpr ALWAYS_INLINE TimeSpan FromDays(s64 d) { return FromMinutes(d * INT64_C(24)); } + + template + constexpr explicit ALWAYS_INLINE TimeSpan(const std::chrono::duration& c) : TimeSpan(static_cast(c).count()) { /* ... */ } + public: + constexpr ALWAYS_INLINE s64 GetNanoSeconds() const { return this->ns; } + constexpr ALWAYS_INLINE s64 GetMicroSeconds() const { return this->GetNanoSeconds() / (INT64_C(1000)); } + constexpr ALWAYS_INLINE s64 GetMilliSeconds() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000)); } + constexpr ALWAYS_INLINE s64 GetSeconds() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000) * INT64_C(1000)); } + constexpr ALWAYS_INLINE s64 GetMinutes() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000) * INT64_C(1000) * INT64_C( 60)); } + constexpr ALWAYS_INLINE s64 GetHours() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000) * INT64_C(1000) * INT64_C( 60) * INT64_C( 60)); } + constexpr ALWAYS_INLINE s64 GetDays() const { return this->GetNanoSeconds() / (INT64_C(1000) * INT64_C(1000) * INT64_C(1000) * INT64_C( 60) * INT64_C( 60) * INT64_C( 24)); } + + constexpr ALWAYS_INLINE bool operator==(const TimeSpan &rhs) const { return this->ns == rhs.ns; } + constexpr ALWAYS_INLINE bool operator!=(const TimeSpan &rhs) const { return this->ns != rhs.ns; } + constexpr ALWAYS_INLINE bool operator<=(const TimeSpan &rhs) const { return this->ns <= rhs.ns; } + constexpr ALWAYS_INLINE bool operator>=(const TimeSpan &rhs) const { return this->ns >= rhs.ns; } + constexpr ALWAYS_INLINE bool operator< (const TimeSpan &rhs) const { return this->ns < rhs.ns; } + constexpr ALWAYS_INLINE bool operator> (const TimeSpan &rhs) const { return this->ns > rhs.ns; } + + constexpr ALWAYS_INLINE TimeSpan &operator+=(TimeSpan rhs) { this->ns += rhs.ns; return *this; } + constexpr ALWAYS_INLINE TimeSpan &operator-=(TimeSpan rhs) { this->ns -= rhs.ns; return *this; } + constexpr ALWAYS_INLINE TimeSpan operator+(TimeSpan rhs) const { TimeSpan r(*this); return r += rhs; } + constexpr ALWAYS_INLINE TimeSpan operator-(TimeSpan rhs) const { TimeSpan r(*this); return r -= rhs; } + }; + +} \ No newline at end of file