From c6d7174dd38d01e60dae29e1bb99c4d746475a5a Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 20 Oct 2021 08:52:54 -0700 Subject: [PATCH] exo: use the fancy new optimal atomics --- .../source/secmon_exception_handler.cpp | 4 +-- .../program/source/smc/secmon_smc_se_lock.cpp | 30 +++++++++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/exosphere/program/source/secmon_exception_handler.cpp b/exosphere/program/source/secmon_exception_handler.cpp index bcb7f35b7..1f033315d 100644 --- a/exosphere/program/source/secmon_exception_handler.cpp +++ b/exosphere/program/source/secmon_exception_handler.cpp @@ -22,7 +22,7 @@ namespace ams::secmon { constexpr inline uintptr_t PMC = MemoryRegionVirtualDevicePmc.GetAddress(); - constinit std::atomic_bool g_is_locked = false; + constinit util::Atomic g_is_locked{false}; } @@ -72,7 +72,7 @@ namespace ams::secmon { } /* Acquire exclusive access to exception handling logic. */ - if (!g_is_locked.exchange(true)) { + if (!g_is_locked.Exchange(true)) { /* Invoke the exception handler impl. */ ExceptionHandlerImpl(lr, sp); diff --git a/exosphere/program/source/smc/secmon_smc_se_lock.cpp b/exosphere/program/source/smc/secmon_smc_se_lock.cpp index f8e03b228..b96eba8eb 100644 --- a/exosphere/program/source/smc/secmon_smc_se_lock.cpp +++ b/exosphere/program/source/smc/secmon_smc_se_lock.cpp @@ -21,35 +21,47 @@ namespace ams::secmon::smc { namespace { - constinit std::atomic_bool g_is_locked = false; + constinit util::Atomic g_is_locked{false}; + + ALWAYS_INLINE bool TryLockSecurityEngineImpl() { + bool value = false; + return g_is_locked.CompareExchangeStrong(value, true); + } + + ALWAYS_INLINE void UnlockSecurityEngineImpl() { + g_is_locked = false; + } + + ALWAYS_INLINE bool IsSecurityEngineLockedImpl() { + return g_is_locked.Load(); + } } bool TryLockSecurityEngine() { - bool value = false; - return g_is_locked.compare_exchange_strong(value, true); + return TryLockSecurityEngineImpl(); } void UnlockSecurityEngine() { - g_is_locked = false; + return UnlockSecurityEngineImpl(); } bool IsSecurityEngineLocked() { - return g_is_locked; + return IsSecurityEngineLockedImpl(); } SmcResult LockSecurityEngineAndInvoke(SmcArguments &args, SmcHandler impl) { /* Try to lock the security engine. */ - SMC_R_UNLESS(TryLockSecurityEngine(), Busy); - ON_SCOPE_EXIT { UnlockSecurityEngine(); }; + SMC_R_UNLESS(TryLockSecurityEngineImpl(), Busy); + ON_SCOPE_EXIT { UnlockSecurityEngineImpl(); }; return impl(args); } SmcResult LockSecurityEngineAndInvokeAsync(SmcArguments &args, SmcHandler impl, GetResultHandler result_handler) { /* Try to lock the security engine. */ - SMC_R_UNLESS(TryLockSecurityEngine(), Busy); - auto se_guard = SCOPE_GUARD { UnlockSecurityEngine(); }; + SMC_R_UNLESS(TryLockSecurityEngineImpl(), Busy); + auto se_guard = SCOPE_GUARD { UnlockSecurityEngineImpl(); }; /* Try to start an async operation. */ const u64 async_key = BeginAsyncOperation(result_handler);