From 66fcf33a2c97ca7fa889a18dee39594a6875bbf8 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 30 Apr 2025 18:23:16 -0700 Subject: [PATCH] kern: invert meaning of KTargetSystem/KSystemControl bools --- .../mesosphere/kern_k_system_control_base.hpp | 2 +- .../mesosphere/kern_k_target_system.hpp | 46 +++++++++---------- .../nintendo/nx/kern_k_system_control.cpp | 22 ++++----- .../source/kern_k_system_control_base.cpp | 12 ++--- 4 files changed, 41 insertions(+), 41 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_system_control_base.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_system_control_base.hpp index ab3a18c5b..3d7ea5310 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_system_control_base.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_system_control_base.hpp @@ -41,7 +41,7 @@ namespace ams::kern { /* Nintendo uses std::mt19937_t for randomness. */ /* To save space (and because mt19337_t isn't secure anyway), */ /* We will use TinyMT. */ - static constinit inline bool s_initialized_random_generator; + static constinit inline bool s_uninitialized_random_generator{true}; static constinit inline util::TinyMT s_random_generator{util::ConstantInitialize}; static constinit inline KSpinLock s_random_lock; public: diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_target_system.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_target_system.hpp index 24220bed0..949fd45ba 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_target_system.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_target_system.hpp @@ -25,35 +25,35 @@ namespace ams::kern { friend class KSystemControl; private: struct KTargetSystemData { - bool is_debug_mode; - bool enable_debug_logging; - bool enable_user_exception_handlers; - bool enable_debug_memory_fill; - bool enable_user_pmu_access; - bool enable_kernel_debugging; - bool enable_dynamic_resource_limits; + bool is_not_debug_mode; + bool disable_debug_logging; + bool disable_user_exception_handlers; + bool disable_debug_memory_fill; + bool disable_user_pmu_access; + bool disable_kernel_debugging; + bool disable_dynamic_resource_limits; }; private: - static inline constinit bool s_is_initialized = false; + static inline constinit bool s_is_uninitialized = true; static inline constinit const volatile KTargetSystemData s_data = { - .is_debug_mode = true, - .enable_debug_logging = true, - .enable_user_exception_handlers = true, - .enable_debug_memory_fill = true, - .enable_user_pmu_access = true, - .enable_kernel_debugging = true, - .enable_dynamic_resource_limits = false, + .is_not_debug_mode = false, + .disable_debug_logging = false, + .disable_user_exception_handlers = false, + .disable_debug_memory_fill = false, + .disable_user_pmu_access = false, + .disable_kernel_debugging = false, + .disable_dynamic_resource_limits = true, }; private: - static ALWAYS_INLINE void SetInitialized() { s_is_initialized = true; } + static ALWAYS_INLINE void SetInitialized() { s_is_uninitialized = false; } public: - static ALWAYS_INLINE bool IsDebugMode() { return s_is_initialized && s_data.is_debug_mode; } - static ALWAYS_INLINE bool IsDebugLoggingEnabled() { return s_is_initialized && s_data.enable_debug_logging; } - static ALWAYS_INLINE bool IsUserExceptionHandlersEnabled() { return s_is_initialized && s_data.enable_user_exception_handlers; } - static ALWAYS_INLINE bool IsDebugMemoryFillEnabled() { return s_is_initialized && s_data.enable_debug_memory_fill; } - static ALWAYS_INLINE bool IsUserPmuAccessEnabled() { return s_is_initialized && s_data.enable_user_pmu_access; } - static ALWAYS_INLINE bool IsKernelDebuggingEnabled() { return s_is_initialized && s_data.enable_kernel_debugging; } - static ALWAYS_INLINE bool IsDynamicResourceLimitsEnabled() { return s_is_initialized && s_data.enable_dynamic_resource_limits; } + static ALWAYS_INLINE bool IsDebugMode() { return !(s_is_uninitialized | s_data.is_not_debug_mode); } + static ALWAYS_INLINE bool IsDebugLoggingEnabled() { return !(s_is_uninitialized | s_data.disable_debug_logging); } + static ALWAYS_INLINE bool IsUserExceptionHandlersEnabled() { return !(s_is_uninitialized | s_data.disable_user_exception_handlers); } + static ALWAYS_INLINE bool IsDebugMemoryFillEnabled() { return !(s_is_uninitialized | s_data.disable_debug_memory_fill); } + static ALWAYS_INLINE bool IsUserPmuAccessEnabled() { return !(s_is_uninitialized | s_data.disable_user_pmu_access); } + static ALWAYS_INLINE bool IsKernelDebuggingEnabled() { return !(s_is_uninitialized | s_data.disable_kernel_debugging); } + static ALWAYS_INLINE bool IsDynamicResourceLimitsEnabled() { return !(s_is_uninitialized | s_data.disable_dynamic_resource_limits); } }; } \ No newline at end of file diff --git a/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp b/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp index b82518e6e..22e99faf0 100644 --- a/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp +++ b/libraries/libmesosphere/source/board/nintendo/nx/kern_k_system_control.cpp @@ -405,22 +405,22 @@ namespace ams::kern::board::nintendo::nx { /* Configure KTargetSystem. */ volatile auto *ts = const_cast(std::addressof(KTargetSystem::s_data)); { - /* Set IsDebugMode. */ + /* Set whether we're in debug mode. */ { - ts->is_debug_mode = GetConfigBool(smc::ConfigItem::IsDebugMode); + ts->is_not_debug_mode = !GetConfigBool(smc::ConfigItem::IsDebugMode); - /* If debug mode, we want to initialize uart logging. */ - ts->enable_debug_logging = ts->is_debug_mode; + /* If we're not in debug mode, we don't want to initialize uart logging. */ + ts->disable_debug_logging = ts->is_not_debug_mode; } /* Set Kernel Configuration. */ { const auto kernel_config = util::BitPack32{GetConfigU32(smc::ConfigItem::KernelConfiguration)}; - ts->enable_debug_memory_fill = kernel_config.Get(); - ts->enable_user_exception_handlers = kernel_config.Get(); - ts->enable_dynamic_resource_limits = !kernel_config.Get(); - ts->enable_user_pmu_access = kernel_config.Get(); + ts->disable_debug_memory_fill = !kernel_config.Get(); + ts->disable_user_exception_handlers = !kernel_config.Get(); + ts->disable_dynamic_resource_limits = kernel_config.Get(); + ts->disable_user_pmu_access = !kernel_config.Get(); /* Configure call smc on panic. */ *const_cast(std::addressof(g_call_smc_on_panic)) = kernel_config.Get(); @@ -430,7 +430,7 @@ namespace ams::kern::board::nintendo::nx { { /* NOTE: This is used to restrict access to SvcKernelDebug/SvcChangeKernelTraceState. */ /* Mesosphere may wish to not require this, as we'd ideally keep ProgramVerification enabled for userland. */ - ts->enable_kernel_debugging = GetConfigBool(smc::ConfigItem::DisableProgramVerification); + ts->disable_kernel_debugging = !GetConfigBool(smc::ConfigItem::DisableProgramVerification); } } } @@ -524,7 +524,7 @@ namespace ams::kern::board::nintendo::nx { KScopedSpinLock lk(s_random_lock); - if (AMS_LIKELY(s_initialized_random_generator)) { + if (AMS_LIKELY(!s_uninitialized_random_generator)) { return KSystemControlBase::GenerateUniformRange(min, max, []() ALWAYS_INLINE_LAMBDA -> u64 { return s_random_generator.GenerateRandomU64(); }); } else { return KSystemControlBase::GenerateUniformRange(min, max, GenerateRandomU64FromSmc); @@ -535,7 +535,7 @@ namespace ams::kern::board::nintendo::nx { KScopedInterruptDisable intr_disable; KScopedSpinLock lk(s_random_lock); - if (AMS_LIKELY(s_initialized_random_generator)) { + if (AMS_LIKELY(!s_uninitialized_random_generator)) { return s_random_generator.GenerateRandomU64(); } else { return GenerateRandomU64FromSmc(); diff --git a/libraries/libmesosphere/source/kern_k_system_control_base.cpp b/libraries/libmesosphere/source/kern_k_system_control_base.cpp index 025ec4f9c..dece074ef 100644 --- a/libraries/libmesosphere/source/kern_k_system_control_base.cpp +++ b/libraries/libmesosphere/source/kern_k_system_control_base.cpp @@ -102,10 +102,10 @@ namespace ams::kern { /* Randomness for Initialization. */ void KSystemControlBase::Init::GenerateRandom(u64 *dst, size_t count) { - if (AMS_UNLIKELY(!s_initialized_random_generator)) { + if (AMS_UNLIKELY(s_uninitialized_random_generator)) { const u64 seed = KHardwareTimer::GetTick(); s_random_generator.Initialize(reinterpret_cast(std::addressof(seed)), sizeof(seed) / sizeof(u32)); - s_initialized_random_generator = true; + s_uninitialized_random_generator = false; } for (size_t i = 0; i < count; ++i) { @@ -114,10 +114,10 @@ namespace ams::kern { } u64 KSystemControlBase::Init::GenerateRandomRange(u64 min, u64 max) { - if (AMS_UNLIKELY(!s_initialized_random_generator)) { + if (AMS_UNLIKELY(s_uninitialized_random_generator)) { const u64 seed = KHardwareTimer::GetTick(); s_random_generator.Initialize(reinterpret_cast(std::addressof(seed)), sizeof(seed) / sizeof(u32)); - s_initialized_random_generator = true; + s_uninitialized_random_generator = false; } return KSystemControlBase::GenerateUniformRange(min, max, []() ALWAYS_INLINE_LAMBDA -> u64 { return s_random_generator.GenerateRandomU64(); }); @@ -140,9 +140,9 @@ namespace ams::kern { void KSystemControlBase::InitializePhase1Base(u64 seed) { /* Initialize the rng, if we somehow haven't already. */ - if (AMS_UNLIKELY(!s_initialized_random_generator)) { + if (AMS_UNLIKELY(s_uninitialized_random_generator)) { s_random_generator.Initialize(reinterpret_cast(std::addressof(seed)), sizeof(seed) / sizeof(u32)); - s_initialized_random_generator = true; + s_uninitialized_random_generator = false; } /* Initialize debug logging. */