2020-04-17 06:51:42 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-04-17 06:51:42 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
namespace ams::sf {
|
|
|
|
|
|
|
|
namespace cmif {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#if !defined(ATMOSPHERE_COMPILER_CLANG)
|
|
|
|
ALWAYS_INLINE util::AtomicRef<uintptr_t> GetAtomicSfInlineContext(os::ThreadType *thread = os::GetCurrentThread()) {
|
|
|
|
uintptr_t * const p = std::addressof(os::GetSdkInternalTlsArray(thread)->sf_inline_context);
|
|
|
|
|
|
|
|
return util::AtomicRef<uintptr_t>(*p);
|
2020-06-30 06:19:33 +00:00
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
#else
|
|
|
|
ALWAYS_INLINE util::Atomic<uintptr_t> &GetAtomicSfInlineContext(os::ThreadType *thread = os::GetCurrentThread()) {
|
|
|
|
uintptr_t * const p = std::addressof(os::GetSdkInternalTlsArray(thread)->sf_inline_context);
|
|
|
|
static_assert(sizeof(std::atomic<uintptr_t>) == sizeof(uintptr_t));
|
|
|
|
static_assert(sizeof(util::Atomic<uintptr_t>) == sizeof(std::atomic<uintptr_t>));
|
2020-06-30 06:19:33 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
return *reinterpret_cast<util::Atomic<uintptr_t> *>(p);
|
2020-06-30 06:19:33 +00:00
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
#endif
|
2020-04-17 06:51:42 +00:00
|
|
|
|
2020-06-30 06:19:33 +00:00
|
|
|
ALWAYS_INLINE void OnSetInlineContext(os::ThreadType *thread) {
|
2022-03-06 20:08:20 +00:00
|
|
|
#if defined(ATMOSPHERE_OS_HORIZON)
|
2020-04-17 06:51:42 +00:00
|
|
|
/* Ensure that libnx receives the priority value. */
|
2020-06-30 06:19:33 +00:00
|
|
|
::fsSetPriority(static_cast<::FsPriority>(::ams::sf::GetFsInlineContext(thread)));
|
2022-03-06 20:08:20 +00:00
|
|
|
#else
|
|
|
|
AMS_UNUSED(thread);
|
|
|
|
#endif
|
2020-04-17 06:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
InlineContext GetInlineContext() {
|
2020-06-30 06:19:33 +00:00
|
|
|
/* Get the context. */
|
2022-03-06 20:08:20 +00:00
|
|
|
uintptr_t thread_context = GetAtomicSfInlineContext().Load();
|
2020-06-30 06:19:33 +00:00
|
|
|
|
|
|
|
/* Copy it out. */
|
2020-04-17 06:51:42 +00:00
|
|
|
InlineContext ctx;
|
2020-06-30 06:19:33 +00:00
|
|
|
static_assert(sizeof(ctx) <= sizeof(thread_context));
|
|
|
|
std::memcpy(std::addressof(ctx), std::addressof(thread_context), sizeof(ctx));
|
2020-04-17 06:51:42 +00:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
InlineContext SetInlineContext(InlineContext ctx) {
|
2020-06-30 06:19:33 +00:00
|
|
|
/* Get current thread. */
|
|
|
|
os::ThreadType * const cur_thread = os::GetCurrentThread();
|
|
|
|
ON_SCOPE_EXIT { OnSetInlineContext(cur_thread); };
|
|
|
|
|
|
|
|
/* Create the new context. */
|
|
|
|
static_assert(sizeof(ctx) <= sizeof(uintptr_t));
|
|
|
|
uintptr_t new_context_value = 0;
|
|
|
|
std::memcpy(std::addressof(new_context_value), std::addressof(ctx), sizeof(ctx));
|
2020-04-17 06:51:42 +00:00
|
|
|
|
2020-06-30 06:19:33 +00:00
|
|
|
/* Get the old context. */
|
2022-03-06 20:08:20 +00:00
|
|
|
uintptr_t old_context_value = GetAtomicSfInlineContext(cur_thread).Exchange(new_context_value);
|
2020-06-30 06:19:33 +00:00
|
|
|
|
|
|
|
/* Convert and copy it out. */
|
2020-04-17 06:51:42 +00:00
|
|
|
InlineContext old_ctx;
|
2020-06-30 06:19:33 +00:00
|
|
|
std::memcpy(std::addressof(old_ctx), std::addressof(old_context_value), sizeof(old_ctx));
|
2020-04-17 06:51:42 +00:00
|
|
|
return old_ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-30 06:19:33 +00:00
|
|
|
namespace {
|
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
#if !defined(ATMOSPHERE_COMPILER_CLANG)
|
|
|
|
ALWAYS_INLINE util::AtomicRef<u8> GetAtomicFsInlineContext(os::ThreadType *thread) {
|
|
|
|
uintptr_t * const p = std::addressof(os::GetSdkInternalTlsArray(thread)->sf_inline_context);
|
|
|
|
|
|
|
|
return util::AtomicRef<u8>(*reinterpret_cast<u8 *>(p));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ALWAYS_INLINE util::Atomic<u8> &GetAtomicFsInlineContext(os::ThreadType *thread) {
|
|
|
|
uintptr_t * const p = std::addressof(os::GetSdkInternalTlsArray(thread)->sf_inline_context);
|
|
|
|
static_assert(sizeof(std::atomic<u8>) == sizeof(u8));
|
|
|
|
static_assert(sizeof(util::Atomic<u8>) == sizeof(std::atomic<u8>));
|
|
|
|
|
|
|
|
return *reinterpret_cast<util::Atomic<u8> *>(reinterpret_cast<u8 *>(p));
|
2020-06-30 06:19:33 +00:00
|
|
|
}
|
2022-03-06 20:08:20 +00:00
|
|
|
#endif
|
2020-06-30 06:19:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 GetFsInlineContext(os::ThreadType *thread) {
|
2022-03-06 20:08:20 +00:00
|
|
|
return GetAtomicFsInlineContext(thread).Load();
|
2020-04-17 06:51:42 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 06:19:33 +00:00
|
|
|
u8 SetFsInlineContext(os::ThreadType *thread, u8 ctx) {
|
|
|
|
ON_SCOPE_EXIT { cmif::OnSetInlineContext(thread); };
|
2020-04-17 06:51:42 +00:00
|
|
|
|
2022-03-06 20:08:20 +00:00
|
|
|
return GetAtomicFsInlineContext(thread).Exchange(ctx);
|
2020-04-17 06:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|