From 57222e8301546deea64542929b4fd8bd8d68149e Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Fri, 31 Jan 2020 16:25:17 -0800 Subject: [PATCH] kern: implement KSchedulerLock --- .../include/mesosphere/kern_k_scheduler.hpp | 20 ++++ .../mesosphere/kern_k_scheduler_lock.hpp | 111 ++++++++++++++++++ libraries/libvapours/include/vapours/util.hpp | 1 + .../vapours/util/util_specialization_of.hpp | 28 +++++ 4 files changed, 160 insertions(+) create mode 100644 libraries/libmesosphere/include/mesosphere/kern_k_scheduler_lock.hpp create mode 100644 libraries/libvapours/include/vapours/util/util_specialization_of.hpp diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_scheduler.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_scheduler.hpp index 620566b46..19639f326 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_scheduler.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_scheduler.hpp @@ -17,6 +17,7 @@ #include #include #include +#include namespace ams::kern { @@ -25,10 +26,14 @@ namespace ams::kern { static_assert(KSchedulerPriorityQueue::NumCores == cpu::NumCores); static_assert(KSchedulerPriorityQueue::NumPriority == BITSIZEOF(u64)); + class KScopedSchedulerLock; + class KScheduler { NON_COPYABLE(KScheduler); NON_MOVEABLE(KScheduler); public: + using LockType = KAbstractSchedulerLock; + struct SchedulingState { std::atomic needs_scheduling; bool interrupt_task_thread_runnable; @@ -37,6 +42,9 @@ namespace ams::kern { KThread *highest_priority_thread; void *idle_thread_stack; }; + private: + friend class KScopedSchedulerLock; + static inline LockType s_scheduler_lock; private: SchedulingState state; bool is_active; @@ -47,6 +55,18 @@ namespace ams::kern { public: KScheduler(); /* TODO: Actually implement KScheduler. This is a placeholder. */ + public: + /* API used by KSchedulerLock */ + static void DisableScheduling(); + static void EnableScheduling(); + static u64 UpdateHighestPriorityThreads(); + static void EnableSchedulingAndSchedule(u64 cores_needing_scheduling); + }; + + class KScopedSchedulerLock { + public: + ALWAYS_INLINE KScopedSchedulerLock() { KScheduler::s_scheduler_lock.Lock(); } + ALWAYS_INLINE ~KScopedSchedulerLock() { KScheduler::s_scheduler_lock.Unlock(); } }; } diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_scheduler_lock.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_scheduler_lock.hpp new file mode 100644 index 000000000..5c951a438 --- /dev/null +++ b/libraries/libmesosphere/include/mesosphere/kern_k_scheduler_lock.hpp @@ -0,0 +1,111 @@ +/* + * 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 +#include +#include + +namespace ams::kern { + + class KThread; + + /* + TODO: C++20 + + template + concept KSchedulerLockable = !std::is_reference::value && requires { + { T::DisableScheduling() } -> std::same_as; + { T::EnableScheduling() } -> std::same_as; + { T::UpdateHighestPriorityThreads() } -> std::convertible_to; + { T::EnableSchedulingAndSchedule(std::declval()) } -> std::same_as; + }; + + */ + + template /* TODO C++20: requires KSchedulerLockable */ + class KAbstractSchedulerLock { + private: + KAlignedSpinLock spin_lock; + s32 lock_count; + KThread *owner_thread; + public: + constexpr ALWAYS_INLINE KAbstractSchedulerLock() : spin_lock(), lock_count(0), owner_thread(nullptr) { MESOSPHERE_ASSERT_THIS(); } + + ALWAYS_INLINE bool IsLockedByCurrentThread() const { + MESOSPHERE_ASSERT_THIS(); + + return this->owner_thread == GetCurrentThreadPointer(); + } + + ALWAYS_INLINE void Lock() { + MESOSPHERE_ASSERT_THIS(); + + if (this->IsLockedByCurrentThread()) { + /* If we already own the lock, we can just increment the count. */ + MESOSPHERE_ASSERT(this->lock_count > 0); + this->lock_count++; + } else { + /* Otherwise, we want to disable scheduling and acquire the spinlock. */ + SchedulerType::DisableScheduling(); + this->spin_lock.Lock(); + + /* For debug, ensure that our state is valid. */ + MESOSPHERE_ASSERT(this->lock_count == 0); + MESOSPHERE_ASSERT(this->owner_thread == nullptr); + + /* Increment count, take ownership. */ + this->lock_count = 1; + this->owner_thread = GetCurrentThreadPointer(); + } + } + + ALWAYS_INLINE void Unlock() { + MESOSPHERE_ASSERT_THIS(); + MESOSPHERE_ASSERT(this->IsLockedByCurrentThread()); + MESOSPHERE_ASSERT(this->lock_count > 0); + + /* Release an instance of the lock. */ + if ((--this->lock_count) == 0) { + /* We're no longer going to hold the lock. Take note of what cores need scheduling. */ + const u64 cores_needing_scheduling = SchedulerType::UpdateHighestPriorityThreads(); + + /* Note that we no longer hold the lock, and unlock the spinlock. */ + this->owner_thread = nullptr; + this->spin_lock.Unlock(); + + /* Enable scheduling, and perform a rescheduling operation. */ + SchedulerType::EnableSchedulingAndSchedule(cores_needing_scheduling); + } + } + + ALWAYS_INLINE void UnlockWithoutRescheduling() { + MESOSPHERE_ASSERT_THIS(); + MESOSPHERE_ASSERT(this->IsLockedByCurrentThread()); + MESOSPHERE_ASSERT(this->lock_count > 0); + + /* Release an instance of the lock. */ + if ((--this->lock_count) == 0) { + /* Note that we no longer hold the lock, and unlock the spinlock. */ + this->owner_thread = nullptr; + this->spin_lock.Unlock(); + + /* Enable scheduling, and perform a rescheduling operation. */ + SchedulerType::EnableScheduling(); + } + } + }; + +} diff --git a/libraries/libvapours/include/vapours/util.hpp b/libraries/libvapours/include/vapours/util.hpp index 5656d33e3..97d6c0cba 100644 --- a/libraries/libvapours/include/vapours/util.hpp +++ b/libraries/libvapours/include/vapours/util.hpp @@ -23,6 +23,7 @@ #include "util/util_bitpack.hpp" #include "util/util_bitset.hpp" #include "util/util_scope_guard.hpp" +#include "util/util_specialization_of.hpp" #include "util/util_typed_storage.hpp" #include "util/util_intrusive_list.hpp" #include "util/util_intrusive_red_black_tree.hpp" diff --git a/libraries/libvapours/include/vapours/util/util_specialization_of.hpp b/libraries/libvapours/include/vapours/util/util_specialization_of.hpp new file mode 100644 index 000000000..ce3454def --- /dev/null +++ b/libraries/libvapours/include/vapours/util/util_specialization_of.hpp @@ -0,0 +1,28 @@ +/* + * 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" + +namespace ams::util { + + template class Template> + struct is_specialization_of : std::false_type{}; + + template