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

@@ -54,6 +54,7 @@
#include <mesosphere/kern_k_auto_object.hpp>
#include <mesosphere/kern_k_handle_table.hpp>
#include <mesosphere/kern_k_process.hpp>
#include <mesosphere/kern_k_resource_limit.hpp>
/* Supervisor Calls. */
#include <mesosphere/kern_svc.hpp>

View File

@@ -36,6 +36,16 @@ namespace ams::kern::arm64 {
static s64 GetTick() {
return GetCount();
}
void RegisterAbsoluteTask(KTimerTask *task, s64 task_time) {
KScopedDisableDispatch dd;
KScopedSpinLock lk(this->GetLock());
if (this->RegisterAbsoluteTaskImpl(task, task_time)) {
SetCompareValue(task_time);
EnableInterrupt();
}
}
private:
friend class impl::KHardwareTimerInterruptTask;
NOINLINE void DoInterruptTask();

View File

@@ -0,0 +1,66 @@
/*
* 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/>.
*/
#pragma once
#include <mesosphere/kern_common.hpp>
#include <mesosphere/kern_select_cpu.hpp>
#include <mesosphere/kern_k_light_lock.hpp>
#include <mesosphere/kern_k_thread_queue.hpp>
#include <mesosphere/kern_k_scoped_scheduler_lock_and_sleep.hpp>
namespace ams::kern {
class KLightConditionVariable {
private:
KThreadQueue thread_queue;
public:
constexpr ALWAYS_INLINE KLightConditionVariable() : thread_queue() { /* ... */ }
private:
void WaitImpl(KLightLock *lock, s64 timeout) {
KThread *owner = GetCurrentThreadPointer();
KHardwareTimer *timer;
/* Sleep the thread. */
{
KScopedSchedulerLockAndSleep lk(&timer, owner, timeout);
lock->Unlock();
if (!this->thread_queue.SleepThread(owner)) {
lk.CancelSleep();
return;
}
}
/* Cancel the task that the sleep setup. */
if (timer != nullptr) {
timer->CancelTask(owner);
}
}
public:
void Wait(KLightLock *lock, s64 timeout) {
this->WaitImpl(lock, timeout);
lock->Lock();
}
void Broadcast() {
KScopedSchedulerLock lk;
while (this->thread_queue.WakeupFrontThread() != nullptr) {
/* We want to signal all threads, and so should continue waking up until there's nothing to wake. */
}
}
};
}

View File

@@ -82,14 +82,19 @@ namespace ams::kern {
KMemoryRegionType_DramLinearMapped = KMemoryRegionType_Dram | KMemoryRegionAttr_LinearMapped,
KMemoryRegionType_DramReservedEarly = 0x16 | KMemoryRegionAttr_NoUserMap,
KMemoryRegionType_DramPoolPartition = 0x26 | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_LinearMapped,
KMemoryRegionType_DramReservedEarly = 0x16 | KMemoryRegionAttr_NoUserMap,
KMemoryRegionType_DramPoolPartition = 0x26 | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_LinearMapped,
KMemoryRegionType_DramMetadataPool = 0x166 | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_CarveoutProtected,
KMemoryRegionType_DramNonKernel = 0x1A6 | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_LinearMapped,
KMemoryRegionType_DramApplicationPool = 0x7A6 | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_LinearMapped,
KMemoryRegionType_DramAppletPool = 0xBA6 | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_LinearMapped,
KMemoryRegionType_DramSystemNonSecurePool = 0xDA6 | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_LinearMapped,
KMemoryRegionType_DramSystemPool = 0x13A6 | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_LinearMapped | KMemoryRegionAttr_CarveoutProtected,
KMemoryRegionType_DramKernel = 0xE | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_CarveoutProtected,
KMemoryRegionType_DramKernelCode = 0xCE | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_CarveoutProtected,
KMemoryRegionType_DramKernelSlab = 0x14E | KMemoryRegionAttr_NoUserMap | KMemoryRegionAttr_CarveoutProtected,
@@ -274,7 +279,6 @@ namespace ams::kern {
MESOSPHERE_INIT_ABORT();
}
DerivedRegionExtents GetDerivedRegionExtents(u32 type_id) {
DerivedRegionExtents extents;
@@ -462,6 +466,19 @@ namespace ams::kern {
return GetPhysicalMemoryRegionTree().GetDerivedRegionExtents(KMemoryRegionAttr_CarveoutProtected);
}
static NOINLINE std::tuple<size_t, size_t> GetTotalAndKernelMemorySizes() {
size_t total_size = 0, kernel_size = 0;
for (auto it = GetPhysicalMemoryRegionTree().cbegin(); it != GetPhysicalMemoryRegionTree().cend(); it++) {
if (it->IsDerivedFrom(KMemoryRegionType_Dram)) {
total_size += it->GetSize();
if (!it->IsDerivedFrom(KMemoryRegionType_DramNonKernel)) {
kernel_size += it->GetSize();
}
}
}
return std::make_tuple(total_size, kernel_size);
}
static void InitializeLinearMemoryRegionTrees(KPhysicalAddress aligned_linear_phys_start, KVirtualAddress linear_virtual_start);
};

View File

@@ -0,0 +1,55 @@
/*
* 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/>.
*/
#pragma once
#include <mesosphere/kern_common.hpp>
#include <mesosphere/kern_k_auto_object.hpp>
#include <mesosphere/kern_slab_helpers.hpp>
#include <mesosphere/kern_k_light_lock.hpp>
#include <mesosphere/kern_k_light_condition_variable.hpp>
namespace ams::kern {
class KResourceLimit final : public KAutoObjectWithSlabHeapAndContainer<KResourceLimit, KAutoObjectWithList> {
MESOSPHERE_AUTOOBJECT_TRAITS(KResourceLimit, KAutoObject);
private:
s64 limit_values[ams::svc::LimitableResource_Count];
s64 current_values[ams::svc::LimitableResource_Count];
s64 current_hints[ams::svc::LimitableResource_Count];
mutable KLightLock lock;
s32 waiter_count;
KLightConditionVariable cond_var;
public:
constexpr ALWAYS_INLINE KResourceLimit() : limit_values(), current_values(), current_hints(), lock(), waiter_count(), cond_var() { /* ... */ }
virtual ~KResourceLimit() { /* ... */ }
static ALWAYS_INLINE void PostDestroy(uintptr_t arg) { /* ... */ }
void Initialize();
virtual void Finalize() override;
s64 GetLimitValue(ams::svc::LimitableResource which) const;
s64 GetCurrentValue(ams::svc::LimitableResource which) const;
s64 GetFreeValue(ams::svc::LimitableResource which) const;
Result SetLimitValue(ams::svc::LimitableResource which, s64 value);
bool Reserve(ams::svc::LimitableResource which, s64 value);
bool Reserve(ams::svc::LimitableResource which, s64 value, s64 timeout);
void Release(ams::svc::LimitableResource which, s64 value);
void Release(ams::svc::LimitableResource which, s64 value, s64 hint);
};
}

View File

@@ -27,6 +27,7 @@ namespace ams::kern {
static_assert(KSchedulerPriorityQueue::NumPriority == BITSIZEOF(u64));
class KScopedSchedulerLock;
class KScopedSchedulerLockAndSleep;
class KScheduler {
NON_COPYABLE(KScheduler);
@@ -48,6 +49,7 @@ namespace ams::kern {
};
private:
friend class KScopedSchedulerLock;
friend class KScopedSchedulerLockAndSleep;
static inline bool s_scheduler_update_needed;
static inline LockType s_scheduler_lock;
static inline KSchedulerPriorityQueue s_priority_queue;

View File

@@ -0,0 +1,55 @@
/*
* 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/>.
*/
#pragma once
#include <mesosphere/kern_common.hpp>
#include <mesosphere/kern_k_scheduler.hpp>
#include <mesosphere/kern_select_hardware_timer.hpp>
#include <mesosphere/kern_kernel.hpp>
namespace ams::kern {
class KScopedSchedulerLockAndSleep {
private:
s64 timeout_tick;
KThread *thread;
KHardwareTimer *timer;
public:
explicit ALWAYS_INLINE KScopedSchedulerLockAndSleep(KHardwareTimer **out_timer, KThread *t, s64 timeout) : timeout_tick(timeout), thread(t) {
/* Lock the scheduler. */
KScheduler::s_scheduler_lock.Lock();
/* Set our timer only if the absolute time is positive. */
this->timer = (this->timeout_tick > 0) ? std::addressof(Kernel::GetHardwareTimer()) : nullptr;
*out_timer = this->timer;
}
ALWAYS_INLINE ~KScopedSchedulerLockAndSleep() {
/* Register the sleep. */
if (this->timeout_tick > 0) {
this->timer->RegisterAbsoluteTask(this->thread, this->timeout_tick);
}
/* Unlock the scheduler. */
KScheduler::s_scheduler_lock.Unlock();
}
ALWAYS_INLINE void CancelSleep() {
this->timeout_tick = 0;
}
};
}

View File

@@ -24,6 +24,8 @@
namespace ams::kern {
class KThreadQueue;
using KThreadFunction = void (*)(uintptr_t);
class KThread final : public KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>, public KTimerTask, public KWorkerTask {
@@ -133,7 +135,7 @@ namespace ams::kern {
s64 last_scheduled_tick;
QueueEntry per_core_priority_queue_entry[cpu::NumCores];
QueueEntry sleeping_queue_entry;
void /* TODO KThreadQueue */ *sleeping_queue;
KThreadQueue *sleeping_queue;
util::IntrusiveListNode waiter_list_node;
util::IntrusiveRedBlackTreeNode condvar_arbiter_tree_node;
util::IntrusiveListNode process_list_node;
@@ -204,8 +206,8 @@ namespace ams::kern {
}
public:
constexpr ALWAYS_INLINE const KAffinityMask &GetAffinityMask() const { return this->affinity_mask; }
constexpr ALWAYS_INLINE ThreadState GetThreadState() const { return static_cast<ThreadState>(this->thread_state & ThreadState_Mask); }
constexpr ALWAYS_INLINE ThreadState GetRawThreadState() const { return this->thread_state; }
constexpr ALWAYS_INLINE ThreadState GetState() const { return static_cast<ThreadState>(this->thread_state & ThreadState_Mask); }
constexpr ALWAYS_INLINE ThreadState GetRawState() const { return this->thread_state; }
NOINLINE void SetState(ThreadState state);
NOINLINE KThreadContext *GetContextForSchedulerLoop();
@@ -217,6 +219,10 @@ namespace ams::kern {
constexpr ALWAYS_INLINE QueueEntry &GetPriorityQueueEntry(s32 core) { return this->per_core_priority_queue_entry[core]; }
constexpr ALWAYS_INLINE const QueueEntry &GetPriorityQueueEntry(s32 core) const { return this->per_core_priority_queue_entry[core]; }
constexpr ALWAYS_INLINE QueueEntry &GetSleepingQueueEntry() { return this->sleeping_queue_entry; }
constexpr ALWAYS_INLINE const QueueEntry &GetSleepingQueueEntry() const { return this->sleeping_queue_entry; }
constexpr ALWAYS_INLINE void SetSleepingQueue(KThreadQueue *q) { this->sleeping_queue = q; }
constexpr ALWAYS_INLINE s32 GetNumKernelWaiters() const { return this->num_kernel_waiters; }
constexpr ALWAYS_INLINE s64 GetLastScheduledTick() const { return this->last_scheduled_tick; }
@@ -245,7 +251,7 @@ namespace ams::kern {
}
ALWAYS_INLINE bool IsTerminationRequested() const {
return this->termination_requested || this->GetRawThreadState() == ThreadState_Terminated;
return this->termination_requested || this->GetRawState() == ThreadState_Terminated;
}
public:

View File

@@ -0,0 +1,132 @@
/*
* 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/>.
*/
#pragma once
#include <mesosphere/kern_common.hpp>
#include <mesosphere/kern_k_thread.hpp>
namespace ams::kern {
class KThreadQueue {
private:
using Entry = KThread::QueueEntry;
private:
Entry root;
public:
constexpr ALWAYS_INLINE KThreadQueue() : root() { /* ... */ }
constexpr ALWAYS_INLINE bool IsEmpty() const { return this->root.GetNext() == nullptr; }
constexpr ALWAYS_INLINE KThread *GetFront() const { return this->root.GetNext(); }
constexpr ALWAYS_INLINE KThread *GetNext(KThread *t) const { return t->GetSleepingQueueEntry().GetNext(); }
private:
constexpr ALWAYS_INLINE KThread *GetBack() const { return this->root.GetPrev(); }
constexpr ALWAYS_INLINE void Enqueue(KThread *add) {
/* Get the entry associated with the added thread. */
Entry &add_entry = add->GetSleepingQueueEntry();
/* Get the entry associated with the end of the queue. */
KThread *tail = this->GetBack();
Entry &tail_entry = (tail != nullptr) ? tail->GetSleepingQueueEntry() : this->root;
/* Link the entries. */
add_entry.SetPrev(tail);
add_entry.SetNext(nullptr);
tail_entry.SetNext(add);
this->root.SetPrev(add);
}
constexpr ALWAYS_INLINE void Remove(KThread *remove) {
/* Get the entry associated with the thread. */
Entry &remove_entry = remove->GetSleepingQueueEntry();
/* Get the entries associated with next and prev. */
KThread *prev = remove_entry.GetPrev();
KThread *next = remove_entry.GetNext();
Entry &prev_entry = (prev != nullptr) ? prev->GetSleepingQueueEntry() : this->root;
Entry &next_entry = (next != nullptr) ? next->GetSleepingQueueEntry() : this->root;
/* Unlink. */
prev_entry.SetNext(next);
next_entry.SetPrev(prev);
}
public:
constexpr ALWAYS_INLINE void Dequeue() {
/* Get the front of the queue. */
KThread *head = this->GetFront();
if (head == nullptr) {
return;
}
MESOSPHERE_ASSERT(head->GetState() == KThread::ThreadState_Waiting);
/* Get the entry for the next head. */
KThread *next = GetNext(head);
Entry &next_entry = (next != nullptr) ? next->GetSleepingQueueEntry() : this->root;
/* Link the entries. */
this->root.SetNext(next);
next_entry.SetPrev(nullptr);
/* Clear the head's queue. */
head->SetSleepingQueue(nullptr);
}
bool SleepThread(KThread *t) {
/* Set the thread's queue and mark it as waiting. */
t->SetSleepingQueue(this);
t->SetState(KThread::ThreadState_Waiting);
/* Add the thread to the queue. */
this->Enqueue(t);
/* If the thread needs terminating, undo our work. */
if (t->IsTerminationRequested()) {
this->WakeupThread(t);
return false;
}
return true;
}
void WakeupThread(KThread *t) {
MESOSPHERE_ASSERT(t->GetState() == KThread::ThreadState_Waiting);
/* Remove the thread from the queue. */
this->Remove(t);
/* Mark the thread as no longer sleeping. */
t->SetState(KThread::ThreadState_Runnable);
t->SetSleepingQueue(nullptr);
}
KThread *WakeupFrontThread() {
KThread *front = this->GetFront();
if (front != nullptr) {
MESOSPHERE_ASSERT(front->GetState() == KThread::ThreadState_Waiting);
/* Remove the thread from the queue. */
this->Dequeue();
/* Mark the thread as no longer sleeping. */
front->SetState(KThread::ThreadState_Runnable);
front->SetSleepingQueue(nullptr);
}
return front;
}
};
}

View File

@@ -20,11 +20,16 @@
#include <mesosphere/kern_k_memory_layout.hpp>
#include <mesosphere/kern_k_memory_manager.hpp>
#include <mesosphere/kern_k_core_local_region.hpp>
#include <mesosphere/kern_k_thread.hpp>
#include <mesosphere/kern_select_hardware_timer.hpp>
namespace ams::kern {
class KThread;
class KHardwareTimer;
class KResourceLimit;
class KInterruptManager;
class KInterruptTaskManager;
class KScheduler;
class Kernel {
public:
enum class State : u8 {
@@ -33,9 +38,10 @@ namespace ams::kern {
Initialized = 2,
};
private:
static inline State s_state = State::Invalid;
static inline KThread s_main_threads[cpu::NumCores];
static inline KThread s_idle_threads[cpu::NumCores];
static State s_state;
static KThread s_main_threads[cpu::NumCores];
static KThread s_idle_threads[cpu::NumCores];
static KResourceLimit s_system_resource_limit;
private:
static ALWAYS_INLINE KCoreLocalContext &GetCoreLocalContext() {
return reinterpret_cast<KCoreLocalRegion *>(cpu::GetCoreLocalRegionAddress())->current.context;
@@ -77,6 +83,10 @@ namespace ams::kern {
static ALWAYS_INLINE KHardwareTimer &GetHardwareTimer() {
return GetCoreLocalContext().hardware_timer;
}
static ALWAYS_INLINE KResourceLimit &GetSystemResourceLimit() {
return s_system_resource_limit;
}
};
}

View File

@@ -69,8 +69,8 @@ namespace ams::kern {
#define MESOSPHERE_R_ABORT_UNLESS(expr) \
({ \
const ::ams::Result _tmp_meso_r_abort_res = static_cast<::ams::Result>(expr); \
if (AMS_UNLIKELY((R_FAILED(_tmp_meso_r_abort_res))) { \
const ::ams::Result _tmp_meso_r_abort_res = static_cast<::ams::Result>((expr)); \
if (AMS_UNLIKELY((R_FAILED(_tmp_meso_r_abort_res)))) { \
MESOSPHERE_PANIC("Result Abort(): %s 2%03d-%04d", #expr, _tmp_meso_r_abort_res.GetModule(), _tmp_meso_r_abort_res.GetDescription()); \
} \
})