kern: implement KResourceLimit

This commit is contained in:
Michael Scire 2020-02-06 05:34:38 -08:00
parent 23f5d77f37
commit 1de607c183
17 changed files with 566 additions and 22 deletions

View file

@ -199,7 +199,7 @@ namespace ams::kern {
g_call_smc_on_panic = kernel_config.Get<smc::KernelConfiguration::UseSecureMonitorPanicCall>();
}
/* Set Program Verification. */
/* Set Kernel Debugging. */
{
/* 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. */
@ -212,7 +212,25 @@ namespace ams::kern {
smc::ConfigureCarveout(0, carveout.GetAddress(), carveout.GetSize());
}
/* TODO: KResourceLimit initialization. */
/* System ResourceLimit initialization. */
{
/* Construct the resource limit object. */
KResourceLimit &sys_res_limit = Kernel::GetSystemResourceLimit();
KAutoObject::Create(std::addressof(sys_res_limit));
sys_res_limit.Initialize();
/* Set the initial limits. */
const auto [total_memory_size, kernel_memory_size] = KMemoryLayout::GetTotalAndKernelMemorySizes();
const auto &slab_counts = init::GetSlabResourceCounts();
MESOSPHERE_R_ABORT_UNLESS(sys_res_limit.SetLimitValue(ams::svc::LimitableResource_PhysicalMemoryMax, total_memory_size));
MESOSPHERE_R_ABORT_UNLESS(sys_res_limit.SetLimitValue(ams::svc::LimitableResource_ThreadCountMax, slab_counts.num_KThread));
MESOSPHERE_R_ABORT_UNLESS(sys_res_limit.SetLimitValue(ams::svc::LimitableResource_EventCountMax, slab_counts.num_KEvent));
MESOSPHERE_R_ABORT_UNLESS(sys_res_limit.SetLimitValue(ams::svc::LimitableResource_TransferMemoryCountMax, slab_counts.num_KTransferMemory));
MESOSPHERE_R_ABORT_UNLESS(sys_res_limit.SetLimitValue(ams::svc::LimitableResource_SessionCountMax, slab_counts.num_KSession));
/* Reserve system memory. */
MESOSPHERE_ABORT_UNLESS(sys_res_limit.Reserve(ams::svc::LimitableResource_PhysicalMemoryMax, kernel_memory_size));
}
}
/* Randomness. */

View file

@ -17,6 +17,8 @@
#include "kern_debug_log_impl.hpp"
namespace ams::kern {
#pragma GCC push_options
#pragma GCC optimize ("-Os")
namespace {
@ -328,7 +330,7 @@ namespace ams::kern {
n = static_cast<signed long long>(va_arg(vl, signed long long));
} else if (HasFlag(FormatSpecifierFlag_Long)) {
n = static_cast<signed long>(va_arg(vl, signed long));
} if (HasFlag(FormatSpecifierFlag_Char)) {
} else if (HasFlag(FormatSpecifierFlag_Char)) {
n = static_cast<signed char>(va_arg(vl, signed int));
} else if (HasFlag(FormatSpecifierFlag_Short)) {
n = static_cast<signed short>(va_arg(vl, signed int));
@ -416,6 +418,9 @@ namespace ams::kern {
}
#pragma GCC pop_options
void KDebugLog::Initialize() {
if (KTargetSystem::IsDebugLoggingEnabled()) {
KScopedInterruptDisable di;

View file

@ -0,0 +1,160 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <mesosphere.hpp>
namespace ams::kern {
void KResourceLimit::Initialize() {
/* This should be unnecessary for us, because our constructor will clear all fields. */
/* The following is analagous to what Nintendo's implementation (no constexpr constructor) would do, though. */
/*
this->waiter_count = 0;
for (size_t i = 0; i < util::size(this->limit_values); i++) {
this->limit_values[i] = 0;
this->current_values[i] = 0;
this->current_hints[i] = 0;
}
*/
}
void KResourceLimit::Finalize() {
/* ... */
}
s64 KResourceLimit::GetLimitValue(ams::svc::LimitableResource which) const {
MESOSPHERE_ASSERT_THIS();
s64 value;
{
KScopedLightLock lk(this->lock);
value = this->limit_values[which];
MESOSPHERE_ASSERT(value >= 0);
MESOSPHERE_ASSERT(this->current_values[which] <= this->limit_values[which]);
MESOSPHERE_ASSERT(this->current_hints[which] <= this->current_values[which]);
}
return value;
}
s64 KResourceLimit::GetCurrentValue(ams::svc::LimitableResource which) const {
MESOSPHERE_ASSERT_THIS();
s64 value;
{
KScopedLightLock lk(this->lock);
value = this->current_values[which];
MESOSPHERE_ASSERT(value >= 0);
MESOSPHERE_ASSERT(this->current_values[which] <= this->limit_values[which]);
MESOSPHERE_ASSERT(this->current_hints[which] <= this->current_values[which]);
}
return value;
}
s64 KResourceLimit::GetFreeValue(ams::svc::LimitableResource which) const {
MESOSPHERE_ASSERT_THIS();
s64 value;
{
KScopedLightLock lk(this->lock);
MESOSPHERE_ASSERT(this->current_values[which] >= 0);
MESOSPHERE_ASSERT(this->current_values[which] <= this->limit_values[which]);
MESOSPHERE_ASSERT(this->current_hints[which] <= this->current_values[which]);
value = this->limit_values[which] - this->current_values[which];
}
return value;
}
Result KResourceLimit::SetLimitValue(ams::svc::LimitableResource which, s64 value) {
MESOSPHERE_ASSERT_THIS();
KScopedLightLock lk(this->lock);
R_UNLESS(this->current_values[which] <= value, svc::ResultInvalidState());
this->limit_values[which] = value;
return ResultSuccess();
}
bool KResourceLimit::Reserve(ams::svc::LimitableResource which, s64 value) {
/* TODO: constexpr definition for this default timeout (it's 10 seconds) */
return this->Reserve(which, value, KHardwareTimer::GetTick() + 192'000'000);
}
bool KResourceLimit::Reserve(ams::svc::LimitableResource which, s64 value, s64 timeout) {
MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(value >= 0);
KScopedLightLock lk(this->lock);
MESOSPHERE_ASSERT(this->current_hints[which] <= this->current_values[which]);
if (this->current_hints[which] >= this->limit_values[which]) {
return false;
}
/* Loop until we reserve or run out of time. */
while (true) {
MESOSPHERE_ASSERT(this->current_values[which] <= this->limit_values[which]);
MESOSPHERE_ASSERT(this->current_hints[which] <= this->current_values[which]);
/* If we would overflow, don't allow to succeed. */
if (this->current_values[which] + value <= this->current_values[which]) {
break;
}
if (this->current_values[which] + value <= this->limit_values[which]) {
this->current_values[which] += value;
this->current_hints[which] += value;
return true;
}
if (this->current_hints[which] + value <= this->limit_values[which] && (timeout < 0 || KHardwareTimer::GetTick() < timeout)) {
this->waiter_count++;
this->cond_var.Wait(&this->lock, timeout);
this->waiter_count--;
} else {
break;
}
}
return false;
}
void KResourceLimit::Release(ams::svc::LimitableResource which, s64 value) {
this->Release(which, value, value);
}
void KResourceLimit::Release(ams::svc::LimitableResource which, s64 value, s64 hint) {
MESOSPHERE_ASSERT_THIS();
MESOSPHERE_ASSERT(value >= 0);
MESOSPHERE_ASSERT(hint >= 0);
KScopedLightLock lk(this->lock);
MESOSPHERE_ASSERT(this->current_values[which] <= this->limit_values[which]);
MESOSPHERE_ASSERT(this->current_hints[which] <= this->current_values[which]);
MESOSPHERE_ASSERT(value <= this->current_values[which]);
MESOSPHERE_ASSERT(hint <= this->current_hints[which]);
this->current_values[which] -= value;
this->current_hints[which] -= hint;
if (this->waiter_count != 0) {
this->cond_var.Broadcast();
}
}
}

View file

@ -119,7 +119,7 @@ namespace ams::kern {
if (KProcess *parent = top_thread->GetOwnerProcess(); parent != nullptr) {
if (KThread *suggested = parent->GetSuggestedTopThread(core_id); suggested != nullptr && suggested != top_thread) {
/* We prefer our parent's suggestion whenever possible. However, we also don't want to schedule un-runnable threads. */
if (suggested->GetRawThreadState() == KThread::ThreadState_Runnable) {
if (suggested->GetRawState() == KThread::ThreadState_Runnable) {
top_thread = suggested;
} else {
top_thread = nullptr;
@ -201,7 +201,7 @@ namespace ams::kern {
KThread *task_thread = Kernel::GetInterruptTaskManager().GetThread();
{
KScopedSchedulerLock sl;
if (AMS_LIKELY(task_thread->GetThreadState() == KThread::ThreadState_Waiting)) {
if (AMS_LIKELY(task_thread->GetState() == KThread::ThreadState_Waiting)) {
task_thread->SetState(KThread::ThreadState_Runnable);
}
}
@ -259,7 +259,7 @@ namespace ams::kern {
MESOSPHERE_ASSERT(IsSchedulerLockedByCurrentThread());
/* Check if the state has changed, because if it hasn't there's nothing to do. */
const KThread::ThreadState cur_state = thread->GetRawThreadState();
const KThread::ThreadState cur_state = thread->GetRawState();
if (cur_state == old_state) {
return;
}
@ -282,7 +282,7 @@ namespace ams::kern {
MESOSPHERE_ASSERT(IsSchedulerLockedByCurrentThread());
/* If the thread is runnable, we want to change its priority in the queue. */
if (thread->GetRawThreadState() == KThread::ThreadState_Runnable) {
if (thread->GetRawState() == KThread::ThreadState_Runnable) {
GetPriorityQueue().ChangePriority(old_priority, thread == GetCurrentThreadPointer(), thread);
IncrementScheduledCount(thread);
SetSchedulerUpdateNeeded();
@ -293,7 +293,7 @@ namespace ams::kern {
MESOSPHERE_ASSERT(IsSchedulerLockedByCurrentThread());
/* If the thread is runnable, we want to change its affinity in the queue. */
if (thread->GetRawThreadState() == KThread::ThreadState_Runnable) {
if (thread->GetRawState() == KThread::ThreadState_Runnable) {
GetPriorityQueue().ChangeAffinityMask(old_core, old_affinity, thread);
IncrementScheduledCount(thread);
SetSchedulerUpdateNeeded();

View file

@ -17,6 +17,12 @@
namespace ams::kern {
/* Declare kernel data members in kernel TU. */
Kernel::State Kernel::s_state = Kernel::State::Invalid;
KThread Kernel::s_main_threads[cpu::NumCores];
KThread Kernel::s_idle_threads[cpu::NumCores];
KResourceLimit Kernel::s_system_resource_limit;
void Kernel::InitializeCoreLocalRegion(s32 core_id) {
/* Construct the core local region object in place. */
KCoreLocalContext *clc = GetPointer<KCoreLocalContext>(KMemoryLayout::GetCoreLocalRegionAddress());

View file

@ -45,6 +45,7 @@ namespace ams::kern {
}
/* TODO: Implement more of Main() */
cpu::SynchronizeAllCores();
while (true) { /* ... */ }
}