From e25a4ca8d74174b06291a5b079793acb1c791993 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 30 Jan 2020 17:07:08 -0800 Subject: [PATCH] kern: implement KAffinityMask --- .../mesosphere/kern_k_affinity_mask.hpp | 61 +++++++++++++++++++ .../include/mesosphere/kern_k_thread.hpp | 1 + 2 files changed, 62 insertions(+) create mode 100644 libraries/libmesosphere/include/mesosphere/kern_k_affinity_mask.hpp diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_affinity_mask.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_affinity_mask.hpp new file mode 100644 index 000000000..a3a263fba --- /dev/null +++ b/libraries/libmesosphere/include/mesosphere/kern_k_affinity_mask.hpp @@ -0,0 +1,61 @@ +/* + * 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 + +namespace ams::kern { + + class KAffinityMask { + private: + static constexpr u64 AllowedAffinityMask = (1ul << cpu::NumCores) - 1; + private: + u64 mask; + private: + static constexpr ALWAYS_INLINE u64 GetCoreBit(s32 core) { + MESOSPHERE_ASSERT(0 <= core && core < static_cast(cpu::NumCores)); + return (1ul << core); + } + public: + constexpr ALWAYS_INLINE KAffinityMask() : mask(0) { MESOSPHERE_ASSERT_THIS(); } + + constexpr ALWAYS_INLINE u64 GetAffinityMask() const { return this->mask; } + + constexpr ALWAYS_INLINE void SetAffinityMask(u64 new_mask) { + MESOSPHERE_ASSERT((new_mask & ~AllowedAffinityMask) == 0); + this->mask = new_mask; + } + + constexpr ALWAYS_INLINE bool GetAffinity(s32 core) const { + return this->mask & GetCoreBit(core); + } + + constexpr ALWAYS_INLINE void SetAffinity(s32 core, bool set) { + MESOSPHERE_ASSERT(0 <= core && core < static_cast(cpu::NumCores)); + + if (set) { + this->mask |= GetCoreBit(core); + } else { + this->mask &= ~GetCoreBit(core); + } + } + + constexpr ALWAYS_INLINE void SetAll() { + this->mask = AllowedAffinityMask; + } + }; + +} diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp index a34f4e800..64d8c1888 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp @@ -16,6 +16,7 @@ #pragma once #include #include +#include namespace ams::kern {