os: implement Tick api, make build with -Werror

This commit is contained in:
Michael Scire
2020-03-16 01:02:55 -07:00
parent 70367e3e7c
commit 43bd733f0a
13 changed files with 405 additions and 6 deletions

View File

@@ -0,0 +1,24 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "os_resource_manager.hpp"
namespace ams::os::impl {
/* TODO: C++20 constinit */
OsResourceManager ResourceManagerHolder::s_resource_manager = {};
}

View File

@@ -0,0 +1,48 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "os_tick_manager_impl.hpp"
namespace ams::os::impl {
class OsResourceManager {
private:
/* TODO */
TickManager tick_manager{};
/* TODO */
public:
constexpr OsResourceManager() = default;
constexpr ALWAYS_INLINE TickManager &GetTickManager() { return this->tick_manager; }
};
class ResourceManagerHolder {
private:
static /* TODO: C++20 constinit */ OsResourceManager s_resource_manager;
private:
constexpr ResourceManagerHolder() { /* ... */ }
public:
static ALWAYS_INLINE OsResourceManager &GetResourceManagerInstance() {
return s_resource_manager;
}
};
ALWAYS_INLINE OsResourceManager &GetResourceManager() {
return ResourceManagerHolder::GetResourceManagerInstance();
}
}

View File

@@ -0,0 +1,30 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "os_resource_manager.hpp"
namespace ams::os::impl {
ALWAYS_INLINE TickManager &GetTickManager() {
return GetResourceManager().GetTickManager();
}
ALWAYS_INLINE Tick GetCurrentTick() {
return GetTickManager().GetTick();
}
}

View File

@@ -0,0 +1,84 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <stratosphere.hpp>
#include "os_tick_manager.hpp"
namespace ams::os::impl {
TimeSpan TickManager::ConvertToTimeSpan(Tick tick) const {
/* Get the tick value. */
const s64 tick_val = tick.GetInt64Value();
/* Get the tick frequency. */
const s64 tick_freq = GetTickFrequency();
AMS_AUDIT(tick_freq < MaxTickFrequency);
/* Clamp tick to range. */
if (tick_val > GetMaxTick()) {
return TimeSpan::FromNanoSeconds(std::numeric_limits<s64>::max());
} else if (tick_val < -GetMaxTick()) {
return TimeSpan::FromNanoSeconds(std::numeric_limits<s64>::min());
} else {
/* Convert to timespan. */
constexpr s64 NanoSecondsPerSecond = TimeSpan::FromSeconds(1).GetNanoSeconds();
const s64 seconds = tick_val / tick_freq;
const s64 frac = tick_val % tick_freq;
const TimeSpan ts = TimeSpan::FromSeconds(seconds) + TimeSpan::FromNanoSeconds(frac * NanoSecondsPerSecond / tick_freq);
constexpr TimeSpan ZeroTS = TimeSpan::FromNanoSeconds(0);
AMS_ASSERT(!((tick_val > 0 && ts < ZeroTS) || (tick_val < 0 && ts > ZeroTS)));
return ts;
}
}
Tick TickManager::ConvertToTick(TimeSpan ts) const {
/* Get the TimeSpan in nanoseconds. */
const s64 ns = ts.GetNanoSeconds();
/* Clamp ns to range. */
if (ns > GetMaxTimeSpanNs()) {
return Tick(std::numeric_limits<s64>::max());
} else if (ns < -GetMaxTimeSpanNs()) {
return Tick(std::numeric_limits<s64>::min());
} else {
/* Get the tick frequency. */
const s64 tick_freq = GetTickFrequency();
AMS_AUDIT(tick_freq < MaxTickFrequency);
/* Convert to tick. */
constexpr s64 NanoSecondsPerSecond = TimeSpan::FromSeconds(1).GetNanoSeconds();
const bool negative = ns < 0;
s64 seconds = ns / NanoSecondsPerSecond;
s64 frac = ns % NanoSecondsPerSecond;
/* If negative, negate seconds/frac. */
if (negative) {
seconds = -seconds;
frac = -frac;
}
/* Calculate the tick, and invert back to negative if needed. */
s64 tick = (seconds * tick_freq) + ((frac * tick_freq + NanoSecondsPerSecond - 1) / NanoSecondsPerSecond);
if (negative) {
tick = -tick;
}
return Tick(tick);
}
}
}

View File

@@ -0,0 +1,56 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
#ifdef ATMOSPHERE_OS_HORIZON
#include "os_tick_manager_impl.os.horizon.hpp"
#else
#error "Unknown OS for TickManagerImpl"
#endif
namespace ams::os::impl {
/* Tick frequency must be less than INT64_MAX / 1 second. */
static constexpr s64 MaxTickFrequency = (std::numeric_limits<s64>::max() / TimeSpan::FromSeconds(1).GetNanoSeconds()) - 1;
class TickManager {
private:
TickManagerImpl impl;
public:
constexpr TickManager() : impl() { /* ... */ }
ALWAYS_INLINE Tick GetTick() const {
return this->impl.GetTick();
}
ALWAYS_INLINE s64 GetTickFrequency() const {
return this->impl.GetTickFrequency();
}
ALWAYS_INLINE s64 GetMaxTick() const {
return this->impl.GetMaxTick();
}
ALWAYS_INLINE s64 GetMaxTimeSpanNs() const {
return this->impl.GetMaxTimeSpanNs();
}
TimeSpan ConvertToTimeSpan(Tick tick) const;
Tick ConvertToTick(TimeSpan ts) const;
};
}

View File

@@ -0,0 +1,46 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::os::impl {
class TickManagerImpl {
public:
constexpr TickManagerImpl() { /* ... */ }
ALWAYS_INLINE Tick GetTick() const {
s64 tick;
__asm__ __volatile__("mrs %[tick], cntpct_el0" : [tick]"=&r"(tick) :: "memory");
return Tick(tick);
}
static constexpr ALWAYS_INLINE s64 GetTickFrequency() {
return static_cast<s64>(::ams::svc::TicksPerSecond);
}
static constexpr ALWAYS_INLINE s64 GetMaxTick() {
static_assert(GetTickFrequency() <= TimeSpan::FromSeconds(1).GetNanoSeconds());
return (std::numeric_limits<s64>::max() / TimeSpan::FromSeconds(1).GetNanoSeconds()) * GetTickFrequency();
}
static constexpr ALWAYS_INLINE s64 GetMaxTimeSpanNs() {
static_assert(GetTickFrequency() <= TimeSpan::FromSeconds(1).GetNanoSeconds());
return TimeSpan::FromNanoSeconds(std::numeric_limits<s64>::max()).GetNanoSeconds();
}
};
}