From 3c8e7de91562c1ae46893f43f0ab98592d56d641 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 1 Dec 2020 18:41:44 -0800 Subject: [PATCH] kern: fix bugs caused by UB + transition to -Os --- .../arch/arm64/kern_k_spin_lock.hpp | 8 +- .../kern_select_interrupt_manager.hpp | 4 +- .../board/nintendo/nx/kern_secure_monitor.cpp | 80 ++++++++++--------- .../libmesosphere/source/kern_k_thread.cpp | 2 +- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_k_spin_lock.hpp b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_k_spin_lock.hpp index f1997ecb8..c6bcc85ce 100644 --- a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_k_spin_lock.hpp +++ b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_k_spin_lock.hpp @@ -25,7 +25,7 @@ namespace ams::kern::arch::arm64 { public: constexpr KNotAlignedSpinLock() : packed_tickets(0) { /* ... */ } - void Lock() { + ALWAYS_INLINE void Lock() { u32 tmp0, tmp1, tmp2; __asm__ __volatile__( @@ -52,7 +52,7 @@ namespace ams::kern::arch::arm64 { ); } - void Unlock() { + ALWAYS_INLINE void Unlock() { const u32 value = this->packed_tickets + 1; __asm__ __volatile__( " stlrh %w[value], %[packed_tickets]\n" @@ -71,7 +71,7 @@ namespace ams::kern::arch::arm64 { public: constexpr KAlignedSpinLock() : current_ticket(0), next_ticket(0) { /* ... */ } - void Lock() { + ALWAYS_INLINE void Lock() { u32 tmp0, tmp1, got_lock; __asm__ __volatile__( @@ -94,7 +94,7 @@ namespace ams::kern::arch::arm64 { ); } - void Unlock() { + ALWAYS_INLINE void Unlock() { const u32 value = this->current_ticket + 1; __asm__ __volatile__( " stlrh %w[value], %[current_ticket]\n" diff --git a/libraries/libmesosphere/include/mesosphere/kern_select_interrupt_manager.hpp b/libraries/libmesosphere/include/mesosphere/kern_select_interrupt_manager.hpp index 53790958d..178ce8c36 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_select_interrupt_manager.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_select_interrupt_manager.hpp @@ -42,7 +42,7 @@ namespace ams::kern { u32 prev_intr_state; public: ALWAYS_INLINE KScopedInterruptDisable() : prev_intr_state(KInterruptManager::DisableInterrupts()) { /* ... */ } - ~KScopedInterruptDisable() { KInterruptManager::RestoreInterrupts(prev_intr_state); } + ALWAYS_INLINE ~KScopedInterruptDisable() { KInterruptManager::RestoreInterrupts(prev_intr_state); } }; class KScopedInterruptEnable { @@ -52,7 +52,7 @@ namespace ams::kern { u32 prev_intr_state; public: ALWAYS_INLINE KScopedInterruptEnable() : prev_intr_state(KInterruptManager::EnableInterrupts()) { /* ... */ } - ~KScopedInterruptEnable() { KInterruptManager::RestoreInterrupts(prev_intr_state); } + ALWAYS_INLINE ~KScopedInterruptEnable() { KInterruptManager::RestoreInterrupts(prev_intr_state); } }; } diff --git a/libraries/libmesosphere/source/board/nintendo/nx/kern_secure_monitor.cpp b/libraries/libmesosphere/source/board/nintendo/nx/kern_secure_monitor.cpp index 628785dab..6e48b5f40 100644 --- a/libraries/libmesosphere/source/board/nintendo/nx/kern_secure_monitor.cpp +++ b/libraries/libmesosphere/source/board/nintendo/nx/kern_secure_monitor.cpp @@ -58,28 +58,30 @@ namespace ams::kern::board::nintendo::nx::smc { /* Disable interrupts while making the call. */ KScopedInterruptDisable intr_disable; - /* Backup the current thread pointer. */ - const uintptr_t current_thread_pointer_value = cpu::GetCurrentThreadPointerValue(); + { + /* Backup the current thread pointer. */ + const uintptr_t current_thread_pointer_value = cpu::GetCurrentThreadPointerValue(); - __asm__ __volatile__("smc #1" - : "+r"(x0), "+r"(x1), "+r"(x2), "+r"(x3), "+r"(x4), "+r"(x5), "+r"(x6), "+r"(x7) - : - : "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "cc", "memory" - ); + __asm__ __volatile__("smc #1" + : "+r"(x0), "+r"(x1), "+r"(x2), "+r"(x3), "+r"(x4), "+r"(x5), "+r"(x6), "+r"(x7) + : + : "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "cc", "memory" + ); - /* Restore the current thread pointer into X18. */ - cpu::SetCurrentThreadPointerValue(current_thread_pointer_value); + /* Restore the current thread pointer into X18. */ + cpu::SetCurrentThreadPointerValue(current_thread_pointer_value); + + /* Store arguments to output. */ + args.x[0] = x0; + args.x[1] = x1; + args.x[2] = x2; + args.x[3] = x3; + args.x[4] = x4; + args.x[5] = x5; + args.x[6] = x6; + args.x[7] = x7; + } } - - /* Store arguments to output. */ - args.x[0] = x0; - args.x[1] = x1; - args.x[2] = x2; - args.x[3] = x3; - args.x[4] = x4; - args.x[5] = x5; - args.x[6] = x6; - args.x[7] = x7; } void CallUserSecureMonitorFunction(ams::svc::lp64::SecureMonitorArguments *args) { @@ -98,28 +100,30 @@ namespace ams::kern::board::nintendo::nx::smc { /* Disable interrupts while making the call. */ KScopedInterruptDisable intr_disable; - /* Backup the current thread pointer. */ - const uintptr_t current_thread_pointer_value = cpu::GetCurrentThreadPointerValue(); + { + /* Backup the current thread pointer. */ + const uintptr_t current_thread_pointer_value = cpu::GetCurrentThreadPointerValue(); - __asm__ __volatile__("smc #0" - : "+r"(x0), "+r"(x1), "+r"(x2), "+r"(x3), "+r"(x4), "+r"(x5), "+r"(x6), "+r"(x7) - : - : "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "cc", "memory" - ); + __asm__ __volatile__("smc #1" + : "+r"(x0), "+r"(x1), "+r"(x2), "+r"(x3), "+r"(x4), "+r"(x5), "+r"(x6), "+r"(x7) + : + : "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "cc", "memory" + ); - /* Restore the current thread pointer into X18. */ - cpu::SetCurrentThreadPointerValue(current_thread_pointer_value); + /* Restore the current thread pointer into X18. */ + cpu::SetCurrentThreadPointerValue(current_thread_pointer_value); + + /* Store arguments to output. */ + args->r[0] = x0; + args->r[1] = x1; + args->r[2] = x2; + args->r[3] = x3; + args->r[4] = x4; + args->r[5] = x5; + args->r[6] = x6; + args->r[7] = x7; + } } - - /* Store arguments to output. */ - args->r[0] = x0; - args->r[1] = x1; - args->r[2] = x2; - args->r[3] = x3; - args->r[4] = x4; - args->r[5] = x5; - args->r[6] = x6; - args->r[7] = x7; } void CallPrivilegedSecureMonitorFunctionForInit(SecureMonitorArguments &args) { diff --git a/libraries/libmesosphere/source/kern_k_thread.cpp b/libraries/libmesosphere/source/kern_k_thread.cpp index 600c7f4cd..f6f327d74 100644 --- a/libraries/libmesosphere/source/kern_k_thread.cpp +++ b/libraries/libmesosphere/source/kern_k_thread.cpp @@ -63,7 +63,7 @@ namespace ams::kern { } [[fallthrough]]; case ThreadType_HighPriority: - { + if (type != ThreadType_Main) { MESOSPHERE_ASSERT(phys_core == GetCurrentCoreId()); } [[fallthrough]];