kern: invert meaning of KTargetSystem/KSystemControl bools

This commit is contained in:
Michael Scire 2025-04-30 18:23:16 -07:00 committed by SciresM
parent d147f6f93b
commit 66fcf33a2c
4 changed files with 41 additions and 41 deletions

View file

@ -41,7 +41,7 @@ namespace ams::kern {
/* Nintendo uses std::mt19937_t for randomness. */ /* Nintendo uses std::mt19937_t for randomness. */
/* To save space (and because mt19337_t isn't secure anyway), */ /* To save space (and because mt19337_t isn't secure anyway), */
/* We will use TinyMT. */ /* 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 util::TinyMT s_random_generator{util::ConstantInitialize};
static constinit inline KSpinLock s_random_lock; static constinit inline KSpinLock s_random_lock;
public: public:

View file

@ -25,35 +25,35 @@ namespace ams::kern {
friend class KSystemControl; friend class KSystemControl;
private: private:
struct KTargetSystemData { struct KTargetSystemData {
bool is_debug_mode; bool is_not_debug_mode;
bool enable_debug_logging; bool disable_debug_logging;
bool enable_user_exception_handlers; bool disable_user_exception_handlers;
bool enable_debug_memory_fill; bool disable_debug_memory_fill;
bool enable_user_pmu_access; bool disable_user_pmu_access;
bool enable_kernel_debugging; bool disable_kernel_debugging;
bool enable_dynamic_resource_limits; bool disable_dynamic_resource_limits;
}; };
private: 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 = { static inline constinit const volatile KTargetSystemData s_data = {
.is_debug_mode = true, .is_not_debug_mode = false,
.enable_debug_logging = true, .disable_debug_logging = false,
.enable_user_exception_handlers = true, .disable_user_exception_handlers = false,
.enable_debug_memory_fill = true, .disable_debug_memory_fill = false,
.enable_user_pmu_access = true, .disable_user_pmu_access = false,
.enable_kernel_debugging = true, .disable_kernel_debugging = false,
.enable_dynamic_resource_limits = false, .disable_dynamic_resource_limits = true,
}; };
private: private:
static ALWAYS_INLINE void SetInitialized() { s_is_initialized = true; } static ALWAYS_INLINE void SetInitialized() { s_is_uninitialized = false; }
public: public:
static ALWAYS_INLINE bool IsDebugMode() { return s_is_initialized && s_data.is_debug_mode; } static ALWAYS_INLINE bool IsDebugMode() { return !(s_is_uninitialized | s_data.is_not_debug_mode); }
static ALWAYS_INLINE bool IsDebugLoggingEnabled() { return s_is_initialized && s_data.enable_debug_logging; } static ALWAYS_INLINE bool IsDebugLoggingEnabled() { return !(s_is_uninitialized | s_data.disable_debug_logging); }
static ALWAYS_INLINE bool IsUserExceptionHandlersEnabled() { return s_is_initialized && s_data.enable_user_exception_handlers; } static ALWAYS_INLINE bool IsUserExceptionHandlersEnabled() { return !(s_is_uninitialized | s_data.disable_user_exception_handlers); }
static ALWAYS_INLINE bool IsDebugMemoryFillEnabled() { return s_is_initialized && s_data.enable_debug_memory_fill; } static ALWAYS_INLINE bool IsDebugMemoryFillEnabled() { return !(s_is_uninitialized | s_data.disable_debug_memory_fill); }
static ALWAYS_INLINE bool IsUserPmuAccessEnabled() { return s_is_initialized && s_data.enable_user_pmu_access; } static ALWAYS_INLINE bool IsUserPmuAccessEnabled() { return !(s_is_uninitialized | s_data.disable_user_pmu_access); }
static ALWAYS_INLINE bool IsKernelDebuggingEnabled() { return s_is_initialized && s_data.enable_kernel_debugging; } static ALWAYS_INLINE bool IsKernelDebuggingEnabled() { return !(s_is_uninitialized | s_data.disable_kernel_debugging); }
static ALWAYS_INLINE bool IsDynamicResourceLimitsEnabled() { return s_is_initialized && s_data.enable_dynamic_resource_limits; } static ALWAYS_INLINE bool IsDynamicResourceLimitsEnabled() { return !(s_is_uninitialized | s_data.disable_dynamic_resource_limits); }
}; };
} }

View file

@ -405,22 +405,22 @@ namespace ams::kern::board::nintendo::nx {
/* Configure KTargetSystem. */ /* Configure KTargetSystem. */
volatile auto *ts = const_cast<volatile KTargetSystem::KTargetSystemData *>(std::addressof(KTargetSystem::s_data)); volatile auto *ts = const_cast<volatile KTargetSystem::KTargetSystemData *>(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. */ /* If we're not in debug mode, we don't want to initialize uart logging. */
ts->enable_debug_logging = ts->is_debug_mode; ts->disable_debug_logging = ts->is_not_debug_mode;
} }
/* Set Kernel Configuration. */ /* Set Kernel Configuration. */
{ {
const auto kernel_config = util::BitPack32{GetConfigU32(smc::ConfigItem::KernelConfiguration)}; const auto kernel_config = util::BitPack32{GetConfigU32(smc::ConfigItem::KernelConfiguration)};
ts->enable_debug_memory_fill = kernel_config.Get<smc::KernelConfiguration::DebugFillMemory>(); ts->disable_debug_memory_fill = !kernel_config.Get<smc::KernelConfiguration::DebugFillMemory>();
ts->enable_user_exception_handlers = kernel_config.Get<smc::KernelConfiguration::EnableUserExceptionHandlers>(); ts->disable_user_exception_handlers = !kernel_config.Get<smc::KernelConfiguration::EnableUserExceptionHandlers>();
ts->enable_dynamic_resource_limits = !kernel_config.Get<smc::KernelConfiguration::DisableDynamicResourceLimits>(); ts->disable_dynamic_resource_limits = kernel_config.Get<smc::KernelConfiguration::DisableDynamicResourceLimits>();
ts->enable_user_pmu_access = kernel_config.Get<smc::KernelConfiguration::EnableUserPmuAccess>(); ts->disable_user_pmu_access = !kernel_config.Get<smc::KernelConfiguration::EnableUserPmuAccess>();
/* Configure call smc on panic. */ /* Configure call smc on panic. */
*const_cast<volatile bool *>(std::addressof(g_call_smc_on_panic)) = kernel_config.Get<smc::KernelConfiguration::UseSecureMonitorPanicCall>(); *const_cast<volatile bool *>(std::addressof(g_call_smc_on_panic)) = kernel_config.Get<smc::KernelConfiguration::UseSecureMonitorPanicCall>();
@ -430,7 +430,7 @@ namespace ams::kern::board::nintendo::nx {
{ {
/* NOTE: This is used to restrict access to SvcKernelDebug/SvcChangeKernelTraceState. */ /* 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. */ /* 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); 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(); }); return KSystemControlBase::GenerateUniformRange(min, max, []() ALWAYS_INLINE_LAMBDA -> u64 { return s_random_generator.GenerateRandomU64(); });
} else { } else {
return KSystemControlBase::GenerateUniformRange(min, max, GenerateRandomU64FromSmc); return KSystemControlBase::GenerateUniformRange(min, max, GenerateRandomU64FromSmc);
@ -535,7 +535,7 @@ namespace ams::kern::board::nintendo::nx {
KScopedInterruptDisable intr_disable; KScopedInterruptDisable intr_disable;
KScopedSpinLock lk(s_random_lock); KScopedSpinLock lk(s_random_lock);
if (AMS_LIKELY(s_initialized_random_generator)) { if (AMS_LIKELY(!s_uninitialized_random_generator)) {
return s_random_generator.GenerateRandomU64(); return s_random_generator.GenerateRandomU64();
} else { } else {
return GenerateRandomU64FromSmc(); return GenerateRandomU64FromSmc();

View file

@ -102,10 +102,10 @@ namespace ams::kern {
/* Randomness for Initialization. */ /* Randomness for Initialization. */
void KSystemControlBase::Init::GenerateRandom(u64 *dst, size_t count) { 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(); const u64 seed = KHardwareTimer::GetTick();
s_random_generator.Initialize(reinterpret_cast<const u32*>(std::addressof(seed)), sizeof(seed) / sizeof(u32)); s_random_generator.Initialize(reinterpret_cast<const u32*>(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) { for (size_t i = 0; i < count; ++i) {
@ -114,10 +114,10 @@ namespace ams::kern {
} }
u64 KSystemControlBase::Init::GenerateRandomRange(u64 min, u64 max) { 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(); const u64 seed = KHardwareTimer::GetTick();
s_random_generator.Initialize(reinterpret_cast<const u32*>(std::addressof(seed)), sizeof(seed) / sizeof(u32)); s_random_generator.Initialize(reinterpret_cast<const u32*>(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(); }); 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) { void KSystemControlBase::InitializePhase1Base(u64 seed) {
/* Initialize the rng, if we somehow haven't already. */ /* 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<const u32*>(std::addressof(seed)), sizeof(seed) / sizeof(u32)); s_random_generator.Initialize(reinterpret_cast<const u32*>(std::addressof(seed)), sizeof(seed) / sizeof(u32));
s_initialized_random_generator = true; s_uninitialized_random_generator = false;
} }
/* Initialize debug logging. */ /* Initialize debug logging. */