From d1f3c4904b4e0e26e121cd411ea53d6f43dd5bb2 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 28 Oct 2021 19:16:23 -0700 Subject: [PATCH] kern: fix minor assembly bugs, avoid unnecessary function call in KScheduler hotloop --- .../arch/arm64/kern_assembly_offsets.h | 3 +++ .../include/mesosphere/kern_k_thread.hpp | 19 +++++++++++++------ .../arch/arm64/svc/kern_svc_exception_asm.s | 2 +- .../libmesosphere/source/kern_k_thread.cpp | 4 ---- .../arch/arm64/kern_exception_handlers_asm.s | 4 ++-- .../source/arch/arm64/kern_k_scheduler_asm.s | 4 +--- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h index 346816d7e..4ad886c29 100644 --- a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h +++ b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h @@ -19,6 +19,9 @@ /* TODO: Different header for this? */ #define AMS_KERN_NUM_SUPERVISOR_CALLS 0xC0 +/* ams::kern::KThread, https://github.com/Atmosphere-NX/Atmosphere/blob/master/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp */ +#define THREAD_THREAD_CONTEXT 0xD0 + /* ams::kern::KThread::StackParameters, https://github.com/Atmosphere-NX/Atmosphere/blob/master/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp */ #define THREAD_STACK_PARAMETERS_SIZE 0x30 #define THREAD_STACK_PARAMETERS_SVC_PERMISSION 0x00 diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp index 6ab5b1392..93958cf58 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_thread.hpp @@ -405,8 +405,6 @@ namespace ams::kern { constexpr ThreadState GetState() const { return static_cast(m_thread_state & ThreadState_Mask); } constexpr ThreadState GetRawState() const { return m_thread_state; } - NOINLINE KThreadContext *GetContextForSchedulerLoop(); - constexpr uintptr_t GetConditionVariableKey() const { return m_condvar_key; } constexpr uintptr_t GetAddressArbiterKey() const { return m_condvar_key; } @@ -624,9 +622,7 @@ namespace ams::kern { void OnTimer(); void DoWorkerTaskImpl(); public: - static constexpr bool IsConditionVariableThreadTreeValid() { - return ConditionVariableThreadTreeTraits::IsValid(); - } + static consteval bool IsKThreadStructurallyValid(); static KThread *GetThreadFromId(u64 thread_id); static Result GetThreadList(s32 *out_num_threads, ams::kern::svc::KUserPointer out_thread_ids, s32 max_out_count); @@ -634,7 +630,18 @@ namespace ams::kern { using ConditionVariableThreadTreeType = ConditionVariableThreadTree; }; static_assert(alignof(KThread) == 0x10); - static_assert(KThread::IsConditionVariableThreadTreeValid()); + + consteval bool KThread::IsKThreadStructurallyValid() { + /* Check that the condition variable tree is valid. */ + static_assert(ConditionVariableThreadTreeTraits::IsValid()); + + /* Check that the assembly offsets are valid. */ + static_assert(AMS_OFFSETOF(KThread, m_thread_context) == THREAD_THREAD_CONTEXT); + + return true; + } + + static_assert(KThread::IsKThreadStructurallyValid()); class KScopedDisableDispatch { public: diff --git a/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_exception_asm.s b/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_exception_asm.s index 877f441fe..8a97715aa 100644 --- a/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_exception_asm.s +++ b/libraries/libmesosphere/source/arch/arm64/svc/kern_svc_exception_asm.s @@ -28,7 +28,7 @@ _ZN3ams4kern3svc25CallReturnFromException64Ev: stp x20, x21, [sp, #(EXCEPTION_CONTEXT_X20_X21)] stp x22, x23, [sp, #(EXCEPTION_CONTEXT_X22_X23)] stp x24, x25, [sp, #(EXCEPTION_CONTEXT_X24_X25)] - stp x26, x26, [sp, #(EXCEPTION_CONTEXT_X26_X27)] + stp x26, x27, [sp, #(EXCEPTION_CONTEXT_X26_X27)] stp x28, x29, [sp, #(EXCEPTION_CONTEXT_X28_X29)] /* Call ams::kern::arch::arm64::ReturnFromException(result). */ diff --git a/libraries/libmesosphere/source/kern_k_thread.cpp b/libraries/libmesosphere/source/kern_k_thread.cpp index c88cfef7d..e61605482 100644 --- a/libraries/libmesosphere/source/kern_k_thread.cpp +++ b/libraries/libmesosphere/source/kern_k_thread.cpp @@ -1315,10 +1315,6 @@ namespace ams::kern { } } - KThreadContext *KThread::GetContextForSchedulerLoop() { - return std::addressof(this->GetContext()); - } - KThread *KThread::GetThreadFromId(u64 thread_id) { /* Lock the list. */ KThread::ListAccessor accessor; diff --git a/mesosphere/kernel/source/arch/arm64/kern_exception_handlers_asm.s b/mesosphere/kernel/source/arch/arm64/kern_exception_handlers_asm.s index 4cfccedab..77aa3043f 100644 --- a/mesosphere/kernel/source/arch/arm64/kern_exception_handlers_asm.s +++ b/mesosphere/kernel/source/arch/arm64/kern_exception_handlers_asm.s @@ -376,7 +376,7 @@ _ZN3ams4kern4arch5arm6430EL1SynchronousExceptionHandlerEv: stp x26, x27, [sp, #(EXCEPTION_CONTEXT_X26_X27)] stp x28, x29, [sp, #(EXCEPTION_CONTEXT_X28_X29)] - mrs x20, sp_el0 + ldr x20, [sp] mrs x21, elr_el1 mrs x22, spsr_el1 mrs x23, tpidr_el0 @@ -529,7 +529,7 @@ _ZN3ams4kern4arch5arm6421EL1SystemErrorHandlerEv: stp x26, x27, [sp, #(EXCEPTION_CONTEXT_X26_X27)] stp x28, x29, [sp, #(EXCEPTION_CONTEXT_X28_X29)] - mrs x20, sp_el0 + ldr x20, [sp] mrs x21, elr_el1 mrs x22, spsr_el1 mrs x23, tpidr_el0 diff --git a/mesosphere/kernel/source/arch/arm64/kern_k_scheduler_asm.s b/mesosphere/kernel/source/arch/arm64/kern_k_scheduler_asm.s index ece1e6742..3c0d2ccbb 100644 --- a/mesosphere/kernel/source/arch/arm64/kern_k_scheduler_asm.s +++ b/mesosphere/kernel/source/arch/arm64/kern_k_scheduler_asm.s @@ -183,9 +183,7 @@ _ZN3ams4kern10KScheduler12ScheduleImplEv: /* Get the highest priority thread's context, and save it. */ /* ams::kern::KThread::GetContextForSchedulerLoop() */ - mov x0, x21 - bl _ZN3ams4kern7KThread26GetContextForSchedulerLoopEv - mov x22, x0 + add x22, x21, #(THREAD_THREAD_CONTEXT) /* Prepare to try to acquire the context lock. */ add x1, x22, #(THREAD_CONTEXT_LOCKED)