kern: implement dpc + skeleton rest of main

This commit is contained in:
Michael Scire 2020-02-07 19:16:09 -08:00
parent e9e949ec36
commit 1224ed8abe
14 changed files with 559 additions and 40 deletions

View file

@ -47,6 +47,7 @@
#include <mesosphere/kern_k_core_local_region.hpp>
#include <mesosphere/kern_k_slab_heap.hpp>
#include <mesosphere/kern_k_light_lock.hpp>
#include <mesosphere/kern_k_dpc_manager.hpp>
#include <mesosphere/kern_kernel.hpp>
#include <mesosphere/kern_k_page_table_manager.hpp>

View file

@ -0,0 +1,44 @@
/*
* 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>
namespace ams::kern {
class KDpcManager {
private:
static constexpr s32 DpcManagerNormalThreadPriority = 59;
static constexpr s32 DpcManagerPreemptionThreadPriority = 63;
static_assert(ams::svc::HighestThreadPriority <= DpcManagerNormalThreadPriority && DpcManagerNormalThreadPriority <= ams::svc::LowestThreadPriority);
static_assert(ams::svc::HighestThreadPriority <= DpcManagerPreemptionThreadPriority && DpcManagerPreemptionThreadPriority <= ams::svc::LowestThreadPriority);
private:
static NOINLINE void Initialize(s32 core_id, s32 priority);
public:
static void Initialize() {
const s32 core_id = GetCurrentCoreId();
if (core_id == static_cast<s32>(cpu::NumCores) - 1) {
Initialize(core_id, DpcManagerPreemptionThreadPriority);
} else {
Initialize(core_id, DpcManagerNormalThreadPriority);
}
}
static NOINLINE void HandleDpc();
};
}

View file

@ -49,7 +49,7 @@ namespace ams::kern {
}
}
public:
void Wait(KLightLock *lock, s64 timeout) {
void Wait(KLightLock *lock, s64 timeout = -1ll) {
this->WaitImpl(lock, timeout);
lock->Lock();
}

View file

@ -471,6 +471,10 @@ namespace ams::kern {
return *GetVirtualMemoryRegionTree().FindFirstRegionByType(KMemoryRegionType_VirtualKernelPtHeap);
}
static NOINLINE KMemoryRegion &GetKernelStackRegion() {
return *GetVirtualMemoryRegionTree().FindFirstRegionByType(KMemoryRegionType_KernelStack);
}
static NOINLINE KMemoryRegion &GetVirtualLinearRegion(KVirtualAddress address) {
return *GetVirtualLinearMemoryRegionTree().FindContainingRegion(GetInteger(address));
}

View file

@ -142,7 +142,7 @@ namespace ams::kern {
/* ... */
}
void Initialize(KVirtualAddress metadata_region, size_t metadata_region_size);
NOINLINE void Initialize(KVirtualAddress metadata_region, size_t metadata_region_size);
KVirtualAddress AllocateContinuous(size_t num_pages, size_t align_pages, u32 option);

View file

@ -342,8 +342,8 @@ namespace ams::kern {
this->scheduled_queue.MoveToFront(member->GetPriority(), member->GetActiveCore(), member);
}
constexpr ALWAYS_INLINE void MoveToScheduledBack(Member *member) {
this->scheduled_queue.MoveToBack(member->GetPriority(), member->GetActiveCore(), member);
constexpr ALWAYS_INLINE KThread *MoveToScheduledBack(Member *member) {
return this->scheduled_queue.MoveToBack(member->GetPriority(), member->GetActiveCore(), member);
}
/* First class fancy operations. */

View file

@ -117,6 +117,7 @@ namespace ams::kern {
static NOINLINE void OnThreadAffinityMaskChanged(KThread *thread, const KAffinityMask &old_affinity, s32 old_core);
/* TODO: Yield operations */
static NOINLINE void RotateScheduledQueue(s32 priority, s32 core_id);
private:
/* Instanced private API. */
void ScheduleImpl();

View file

@ -46,7 +46,7 @@ namespace ams::kern {
SuspendType_Thread = 1,
SuspendType_Debug = 2,
SuspendType_Unk3 = 3,
SuspendType_Unk4 = 4,
SuspendType_Init = 4,
SuspendType_Count,
};
@ -64,7 +64,7 @@ namespace ams::kern {
ThreadState_ThreadSuspended = (1 << (SuspendType_Thread + ThreadState_SuspendShift)),
ThreadState_DebugSuspended = (1 << (SuspendType_Debug + ThreadState_SuspendShift)),
ThreadState_Unk3Suspended = (1 << (SuspendType_Unk3 + ThreadState_SuspendShift)),
ThreadState_Unk4Suspended = (1 << (SuspendType_Unk4 + ThreadState_SuspendShift)),
ThreadState_InitSuspended = (1 << (SuspendType_Init + ThreadState_SuspendShift)),
ThreadState_SuspendFlagMask = ((1 << SuspendType_Count) - 1) << ThreadState_SuspendShift,
};
@ -91,17 +91,17 @@ namespace ams::kern {
KThread *prev;
KThread *next;
public:
constexpr ALWAYS_INLINE QueueEntry() : prev(nullptr), next(nullptr) { /* ... */ }
constexpr QueueEntry() : prev(nullptr), next(nullptr) { /* ... */ }
constexpr ALWAYS_INLINE void Initialize() {
constexpr void Initialize() {
this->prev = nullptr;
this->next = nullptr;
}
constexpr ALWAYS_INLINE KThread *GetPrev() const { return this->prev; }
constexpr ALWAYS_INLINE KThread *GetNext() const { return this->next; }
constexpr ALWAYS_INLINE void SetPrev(KThread *t) { this->prev = t; }
constexpr ALWAYS_INLINE void SetNext(KThread *t) { this->next = t; }
constexpr KThread *GetPrev() const { return this->prev; }
constexpr KThread *GetNext() const { return this->next; }
constexpr void SetPrev(KThread *t) { this->prev = t; }
constexpr void SetNext(KThread *t) { this->next = t; }
};
private:
static constexpr size_t PriorityInheritanceCountMax = 10;
@ -179,6 +179,18 @@ namespace ams::kern {
Result Initialize(KThreadFunction func, uintptr_t arg, void *kern_stack_top, KProcessAddress user_stack_top, s32 prio, s32 core, KProcess *owner, ThreadType type);
private:
static Result InitializeThread(KThread *thread, KThreadFunction func, uintptr_t arg, KProcessAddress user_stack_top, s32 prio, s32 core, KProcess *owner, ThreadType type);
public:
static Result InitializeKernelThread(KThread *thread, KThreadFunction func, uintptr_t arg, s32 prio, s32 core) {
return InitializeThread(thread, func, arg, Null<KProcessAddress>, prio, core, nullptr, ThreadType_Kernel);
}
static Result InitializeHighPriorityThread(KThread *thread, KThreadFunction func, uintptr_t arg) {
return InitializeThread(thread, func, arg, Null<KProcessAddress>, 0, GetCurrentCoreId(), nullptr, ThreadType_HighPriority);
}
/* TODO: static Result InitializeUserThread */
private:
StackParameters &GetStackParameters() {
return *(reinterpret_cast<StackParameters *>(this->kernel_stack_top) - 1);
@ -204,39 +216,52 @@ namespace ams::kern {
MESOSPHERE_ASSERT(GetCurrentThread().GetDisableDispatchCount() > 0);
GetStackParameters().disable_count--;
}
private:
void Suspend();
public:
constexpr ALWAYS_INLINE const KAffinityMask &GetAffinityMask() const { return this->affinity_mask; }
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; }
constexpr const KAffinityMask &GetAffinityMask() const { return this->affinity_mask; }
constexpr ThreadState GetState() const { return static_cast<ThreadState>(this->thread_state & ThreadState_Mask); }
constexpr ThreadState GetRawState() const { return this->thread_state; }
NOINLINE void SetState(ThreadState state);
NOINLINE KThreadContext *GetContextForSchedulerLoop();
constexpr ALWAYS_INLINE s32 GetActiveCore() const { return this->core_id; }
constexpr ALWAYS_INLINE void SetActiveCore(s32 core) { this->core_id = core; }
constexpr ALWAYS_INLINE s32 GetPriority() const { return this->priority; }
constexpr s32 GetActiveCore() const { return this->core_id; }
constexpr void SetActiveCore(s32 core) { this->core_id = core; }
constexpr s32 GetPriority() const { return this->priority; }
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 QueueEntry &GetPriorityQueueEntry(s32 core) { return this->per_core_priority_queue_entry[core]; }
constexpr 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 QueueEntry &GetSleepingQueueEntry() { return this->sleeping_queue_entry; }
constexpr const QueueEntry &GetSleepingQueueEntry() const { return this->sleeping_queue_entry; }
constexpr void SetSleepingQueue(KThreadQueue *q) { this->sleeping_queue = q; }
constexpr ALWAYS_INLINE s32 GetNumKernelWaiters() const { return this->num_kernel_waiters; }
constexpr s32 GetNumKernelWaiters() const { return this->num_kernel_waiters; }
constexpr ALWAYS_INLINE s64 GetLastScheduledTick() const { return this->last_scheduled_tick; }
constexpr ALWAYS_INLINE void SetLastScheduledTick(s64 tick) { this->last_scheduled_tick = tick; }
constexpr s64 GetLastScheduledTick() const { return this->last_scheduled_tick; }
constexpr void SetLastScheduledTick(s64 tick) { this->last_scheduled_tick = tick; }
constexpr ALWAYS_INLINE KProcess *GetOwnerProcess() const { return this->parent; }
constexpr KProcess *GetOwnerProcess() const { return this->parent; }
constexpr bool IsUserThread() const { return this->parent != nullptr; }
constexpr ALWAYS_INLINE KProcessAddress GetThreadLocalRegionAddress() const { return this->tls_address; }
constexpr ALWAYS_INLINE void *GetThreadLocalRegionHeapAddress() const { return this->tls_heap_address; }
constexpr KProcessAddress GetThreadLocalRegionAddress() const { return this->tls_address; }
constexpr void *GetThreadLocalRegionHeapAddress() const { return this->tls_heap_address; }
ALWAYS_INLINE void AddCpuTime(s64 amount) {
void AddCpuTime(s64 amount) {
this->cpu_time += amount;
}
constexpr u32 GetSuspendFlags() const { return this->suspend_allowed_flags & this->suspend_request_flags; }
constexpr bool IsSuspended() const { return this->GetSuspendFlags() != 0; }
void RequestSuspend(SuspendType type);
void TrySuspend();
Result SetPriorityToIdle();
Result Run();
void Exit();
ALWAYS_INLINE void *GetStackTop() const { return reinterpret_cast<StackParameters *>(this->kernel_stack_top) - 1; }
ALWAYS_INLINE void *GetKernelStackTop() const { return this->kernel_stack_top; }

View file

@ -26,7 +26,7 @@ namespace ams::kern::svc {
/* 57 */ using ::ams::svc::ResultNoSynchronizationObject;
/* 59 */ using ::ams::svc::ResultThreadTerminating;
/* 59 */ using ::ams::svc::ResultTerminationRequested;
/* 70 */ using ::ams::svc::ResultNoEvent;