kern: Kill KCoreLocalRegion
This commit is contained in:
@@ -48,7 +48,6 @@
|
||||
#include <mesosphere/kern_k_spin_lock.hpp>
|
||||
#include <mesosphere/kern_k_memory_manager.hpp>
|
||||
#include <mesosphere/kern_k_interrupt_task_manager.hpp>
|
||||
#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>
|
||||
|
||||
@@ -29,6 +29,7 @@ namespace ams::kern::init {
|
||||
u64 entrypoint;
|
||||
u64 argument;
|
||||
u64 setup_function;
|
||||
u64 exception_stack;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -220,16 +220,19 @@ namespace ams::kern::arch::arm64::cpu {
|
||||
DataSynchronizationBarrier();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE uintptr_t GetCoreLocalRegionAddress() {
|
||||
ALWAYS_INLINE uintptr_t GetCurrentThreadPointerValue() {
|
||||
register uintptr_t x18 asm("x18");
|
||||
__asm__ __volatile__("" : [x18]"=r"(x18));
|
||||
return x18;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void SetCoreLocalRegionAddress(uintptr_t value) {
|
||||
ALWAYS_INLINE void SetCurrentThreadPointerValue(uintptr_t value) {
|
||||
register uintptr_t x18 asm("x18") = value;
|
||||
__asm__ __volatile__("":: [x18]"r"(x18));
|
||||
SetTpidrEl1(value);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void SetExceptionThreadStackTop(uintptr_t top) {
|
||||
SetTpidrEl1(top);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void SwitchThreadLocalRegion(uintptr_t tlr) {
|
||||
|
||||
@@ -25,6 +25,5 @@
|
||||
namespace ams::kern::init {
|
||||
|
||||
KPhysicalAddress GetInitArgumentsAddress(s32 core_id);
|
||||
void SetInitArguments(s32 core_id, KPhysicalAddress address, uintptr_t arg);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* 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_current_context.hpp>
|
||||
#include <mesosphere/kern_k_scheduler.hpp>
|
||||
#include <mesosphere/kern_k_interrupt_task_manager.hpp>
|
||||
#include <mesosphere/kern_select_interrupt_manager.hpp>
|
||||
#include <mesosphere/kern_select_hardware_timer.hpp>
|
||||
#include <mesosphere/kern_k_memory_manager.hpp>
|
||||
|
||||
namespace ams::kern {
|
||||
|
||||
struct KCoreLocalContext {
|
||||
KCurrentContext current;
|
||||
};
|
||||
static_assert(sizeof(KCoreLocalContext) < PageSize);
|
||||
|
||||
struct KCoreLocalPage {
|
||||
KCoreLocalContext context;
|
||||
u8 padding[PageSize - sizeof(KCoreLocalContext)];
|
||||
};
|
||||
static_assert(sizeof(KCoreLocalPage) == PageSize);
|
||||
|
||||
struct KCoreLocalRegion {
|
||||
KCoreLocalPage current;
|
||||
KCoreLocalPage absolute[cpu::NumCores];
|
||||
};
|
||||
static_assert(sizeof(KCoreLocalRegion) == PageSize * (1 + cpu::NumCores));
|
||||
|
||||
}
|
||||
@@ -21,80 +21,24 @@ namespace ams::kern {
|
||||
class KThread;
|
||||
class KProcess;
|
||||
class KScheduler;
|
||||
class KInterruptTaskManager;
|
||||
|
||||
struct KCurrentContext {
|
||||
std::atomic<KThread *> current_thread;
|
||||
std::atomic<KProcess *> current_process;
|
||||
KScheduler *scheduler;
|
||||
KInterruptTaskManager *interrupt_task_manager;
|
||||
s32 core_id;
|
||||
void *exception_stack_top;
|
||||
ams::svc::ThreadLocalRegion *tlr;
|
||||
};
|
||||
static_assert(std::is_standard_layout<KCurrentContext>::value && std::is_trivially_destructible<KCurrentContext>::value);
|
||||
static_assert(sizeof(KCurrentContext) <= cpu::DataCacheLineSize);
|
||||
static_assert(sizeof(std::atomic<KThread *>) == sizeof(KThread *));
|
||||
static_assert(sizeof(std::atomic<KProcess *>) == sizeof(KProcess *));
|
||||
|
||||
namespace impl {
|
||||
|
||||
ALWAYS_INLINE KCurrentContext &GetCurrentContext() {
|
||||
return *reinterpret_cast<KCurrentContext *>(cpu::GetCoreLocalRegionAddress());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KThread *GetCurrentThreadPointer() {
|
||||
return impl::GetCurrentContext().current_thread.load(std::memory_order_relaxed);
|
||||
return reinterpret_cast<KThread *>(cpu::GetCurrentThreadPointerValue());
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KThread &GetCurrentThread() {
|
||||
return *GetCurrentThreadPointer();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KProcess *GetCurrentProcessPointer() {
|
||||
return impl::GetCurrentContext().current_process.load(std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KProcess &GetCurrentProcess() {
|
||||
return *GetCurrentProcessPointer();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KScheduler *GetCurrentSchedulerPointer() {
|
||||
return impl::GetCurrentContext().scheduler;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KScheduler &GetCurrentScheduler() {
|
||||
return *GetCurrentSchedulerPointer();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KInterruptTaskManager *GetCurrentInterruptTaskManagerPointer() {
|
||||
return impl::GetCurrentContext().interrupt_task_manager;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KInterruptTaskManager &GetCurrentInterruptTaskManager() {
|
||||
return *GetCurrentInterruptTaskManagerPointer();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE s32 GetCurrentCoreId() {
|
||||
return impl::GetCurrentContext().core_id;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE ams::svc::ThreadLocalRegion *GetCurrentThreadLocalRegion() {
|
||||
return impl::GetCurrentContext().tlr;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void SetCurrentThread(KThread *new_thread) {
|
||||
impl::GetCurrentContext().current_thread = new_thread;
|
||||
cpu::SetCurrentThreadPointerValue(reinterpret_cast<uintptr_t>(new_thread));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE void SetCurrentProcess(KProcess *new_process) {
|
||||
impl::GetCurrentContext().current_process = new_process;
|
||||
}
|
||||
ALWAYS_INLINE KProcess *GetCurrentProcessPointer();
|
||||
ALWAYS_INLINE KProcess &GetCurrentProcess();
|
||||
|
||||
ALWAYS_INLINE void SetCurrentThreadLocalRegion(void *address) {
|
||||
impl::GetCurrentContext().tlr = static_cast<ams::svc::ThreadLocalRegion *>(address);
|
||||
}
|
||||
ALWAYS_INLINE s32 GetCurrentCoreId();
|
||||
|
||||
ALWAYS_INLINE KScheduler &GetCurrentScheduler();
|
||||
|
||||
}
|
||||
|
||||
@@ -134,7 +134,6 @@ namespace ams::kern {
|
||||
static NOINLINE KVirtualAddress GetExceptionStackTopAddress(s32 core_id) { return GetStackTopAddress(core_id, KMemoryRegionType_KernelMiscExceptionStack); }
|
||||
|
||||
static NOINLINE KVirtualAddress GetSlabRegionAddress() { return Dereference(GetVirtualMemoryRegionTree().FindByType(KMemoryRegionType_KernelSlab)).GetAddress(); }
|
||||
static NOINLINE KVirtualAddress GetCoreLocalRegionAddress() { return Dereference(GetVirtualMemoryRegionTree().FindByType(KMemoryRegionType_CoreLocalRegion)).GetAddress(); }
|
||||
|
||||
static NOINLINE const KMemoryRegion &GetDeviceRegion(KMemoryRegionType type) { return Dereference(GetPhysicalMemoryRegionTree().FindFirstDerived(type)); }
|
||||
static KPhysicalAddress GetDevicePhysicalAddress(KMemoryRegionType type) { return GetDeviceRegion(type).GetAddress(); }
|
||||
@@ -144,7 +143,6 @@ namespace ams::kern {
|
||||
static NOINLINE const KMemoryRegion &GetPageTableHeapRegion() { return Dereference(GetVirtualMemoryRegionTree().FindByType(KMemoryRegionType_VirtualDramKernelPtHeap)); }
|
||||
static NOINLINE const KMemoryRegion &GetKernelStackRegion() { return Dereference(GetVirtualMemoryRegionTree().FindByType(KMemoryRegionType_KernelStack)); }
|
||||
static NOINLINE const KMemoryRegion &GetTempRegion() { return Dereference(GetVirtualMemoryRegionTree().FindByType(KMemoryRegionType_KernelTemp)); }
|
||||
static NOINLINE const KMemoryRegion &GetCoreLocalRegion() { return Dereference(GetVirtualMemoryRegionTree().FindByType(KMemoryRegionType_CoreLocalRegion)); }
|
||||
|
||||
static NOINLINE const KMemoryRegion &GetKernelTraceBufferRegion() { return Dereference(GetVirtualLinearMemoryRegionTree().FindByType(KMemoryRegionType_VirtualDramKernelTraceBuffer)); }
|
||||
|
||||
@@ -216,7 +214,6 @@ namespace ams::kern {
|
||||
namespace init {
|
||||
|
||||
/* These should be generic, regardless of board. */
|
||||
void SetupCoreLocalRegionMemoryRegions(KInitialPageTable &page_table, KInitialPageAllocator &page_allocator);
|
||||
void SetupPoolPartitionMemoryRegions();
|
||||
|
||||
/* These may be implemented in a board-specific manner. */
|
||||
|
||||
@@ -156,12 +156,13 @@ namespace ams::kern {
|
||||
|
||||
constexpr inline const auto KMemoryRegionType_None = impl::KMemoryRegionTypeValue();
|
||||
|
||||
constexpr inline const auto KMemoryRegionType_Kernel = KMemoryRegionType_None.DeriveInitial(0, 2);
|
||||
constexpr inline const auto KMemoryRegionType_Dram = KMemoryRegionType_None.DeriveInitial(1, 2);
|
||||
constexpr inline const auto KMemoryRegionType_CoreLocalRegion = KMemoryRegionType_None.DeriveInitial(2).Finalize();
|
||||
static_assert(KMemoryRegionType_Kernel .GetValue() == 0x1);
|
||||
static_assert(KMemoryRegionType_Dram .GetValue() == 0x2);
|
||||
static_assert(KMemoryRegionType_CoreLocalRegion.GetValue() == 0x4);
|
||||
constexpr inline const auto KMemoryRegionType_Kernel = KMemoryRegionType_None.DeriveInitial(0, 2);
|
||||
constexpr inline const auto KMemoryRegionType_Dram = KMemoryRegionType_None.DeriveInitial(1, 2);
|
||||
static_assert(KMemoryRegionType_Kernel.GetValue() == 0x1);
|
||||
static_assert(KMemoryRegionType_Dram .GetValue() == 0x2);
|
||||
|
||||
/* constexpr inline const auto KMemoryRegionType_CoreLocalRegion = KMemoryRegionType_None.DeriveInitial(2).Finalize(); */
|
||||
/* static_assert(KMemoryRegionType_CoreLocalRegion.GetValue() == 0x4); */
|
||||
|
||||
constexpr inline const auto KMemoryRegionType_DramKernelBase = KMemoryRegionType_Dram.DeriveSparse(0, 3, 0).SetAttribute(KMemoryRegionAttr_NoUserMap).SetAttribute(KMemoryRegionAttr_CarveoutProtected);
|
||||
constexpr inline const auto KMemoryRegionType_DramReservedBase = KMemoryRegionType_Dram.DeriveSparse(0, 3, 1);
|
||||
@@ -274,15 +275,15 @@ namespace ams::kern {
|
||||
/* UNUSED: .Derive(7, 0); */
|
||||
constexpr inline const auto KMemoryRegionType_KernelMiscMainStack = KMemoryRegionType_KernelMiscDerivedBase.Derive(7, 1);
|
||||
constexpr inline const auto KMemoryRegionType_KernelMiscMappedDevice = KMemoryRegionType_KernelMiscDerivedBase.Derive(7, 2);
|
||||
constexpr inline const auto KMemoryRegionType_KernelMiscIdleStack = KMemoryRegionType_KernelMiscDerivedBase.Derive(7, 3);
|
||||
constexpr inline const auto KMemoryRegionType_KernelMiscExceptionStack = KMemoryRegionType_KernelMiscDerivedBase.Derive(7, 3);
|
||||
constexpr inline const auto KMemoryRegionType_KernelMiscUnknownDebug = KMemoryRegionType_KernelMiscDerivedBase.Derive(7, 4);
|
||||
/* UNUSED: .Derive(7, 5); */
|
||||
constexpr inline const auto KMemoryRegionType_KernelMiscExceptionStack = KMemoryRegionType_KernelMiscDerivedBase.Derive(7, 6);
|
||||
constexpr inline const auto KMemoryRegionType_KernelMiscIdleStack = KMemoryRegionType_KernelMiscDerivedBase.Derive(7, 6);
|
||||
static_assert(KMemoryRegionType_KernelMiscMainStack .GetValue() == 0xB49);
|
||||
static_assert(KMemoryRegionType_KernelMiscMappedDevice .GetValue() == 0xD49);
|
||||
static_assert(KMemoryRegionType_KernelMiscIdleStack .GetValue() == 0x1349);
|
||||
static_assert(KMemoryRegionType_KernelMiscExceptionStack.GetValue() == 0x1349);
|
||||
static_assert(KMemoryRegionType_KernelMiscUnknownDebug .GetValue() == 0x1549);
|
||||
static_assert(KMemoryRegionType_KernelMiscExceptionStack.GetValue() == 0x2349);
|
||||
static_assert(KMemoryRegionType_KernelMiscIdleStack .GetValue() == 0x2349);
|
||||
|
||||
constexpr inline const auto KMemoryRegionType_KernelTemp = KMemoryRegionType_Kernel.Advance(2).Derive(2, 0);
|
||||
static_assert(KMemoryRegionType_KernelTemp.GetValue() == 0x31);
|
||||
|
||||
@@ -361,9 +361,6 @@ namespace ams::kern {
|
||||
static void Switch(KProcess *cur_process, KProcess *next_process) {
|
||||
MESOSPHERE_UNUSED(cur_process);
|
||||
|
||||
/* Set the current process pointer. */
|
||||
SetCurrentProcess(next_process);
|
||||
|
||||
/* Update the current page table. */
|
||||
if (next_process) {
|
||||
next_process->GetPageTable().Activate(next_process->GetProcessId());
|
||||
|
||||
@@ -56,9 +56,10 @@ namespace ams::kern {
|
||||
KThread *prev_thread;
|
||||
s64 last_context_switch_time;
|
||||
KThread *idle_thread;
|
||||
std::atomic<KThread *> current_thread;
|
||||
public:
|
||||
constexpr KScheduler()
|
||||
: state(), is_active(false), core_id(0), prev_thread(nullptr), last_context_switch_time(0), idle_thread(nullptr)
|
||||
: state(), is_active(false), core_id(0), prev_thread(nullptr), last_context_switch_time(0), idle_thread(nullptr), current_thread(nullptr)
|
||||
{
|
||||
this->state.needs_scheduling = true;
|
||||
this->state.interrupt_task_thread_runnable = false;
|
||||
@@ -96,6 +97,10 @@ namespace ams::kern {
|
||||
return this->prev_thread;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KThread *GetSchedulerCurrentThread() const {
|
||||
return this->current_thread;
|
||||
}
|
||||
|
||||
ALWAYS_INLINE s64 GetLastContextSwitchTime() const {
|
||||
return this->last_context_switch_time;
|
||||
}
|
||||
|
||||
@@ -90,6 +90,7 @@ namespace ams::kern {
|
||||
bool is_pinned;
|
||||
s32 disable_count;
|
||||
KThreadContext *context;
|
||||
KThread *cur_thread;
|
||||
};
|
||||
static_assert(alignof(StackParameters) == 0x10);
|
||||
|
||||
@@ -181,6 +182,7 @@ namespace ams::kern {
|
||||
Result wait_result;
|
||||
Result debug_exception_result;
|
||||
s32 priority{};
|
||||
s32 current_core_id{};
|
||||
s32 core_id{};
|
||||
s32 base_priority{};
|
||||
s32 ideal_core_id{};
|
||||
@@ -380,6 +382,9 @@ namespace ams::kern {
|
||||
constexpr s32 GetActiveCore() const { return this->core_id; }
|
||||
constexpr void SetActiveCore(s32 core) { this->core_id = core; }
|
||||
|
||||
constexpr ALWAYS_INLINE s32 GetCurrentCore() const { return this->current_core_id; }
|
||||
constexpr void SetCurrentCore(s32 core) { this->current_core_id = core; }
|
||||
|
||||
constexpr s32 GetPriority() const { return this->priority; }
|
||||
constexpr void SetPriority(s32 prio) { this->priority = prio; }
|
||||
|
||||
@@ -570,4 +575,16 @@ namespace ams::kern {
|
||||
return reinterpret_cast<const KExceptionContext *>(reinterpret_cast<uintptr_t>(thread->GetKernelStackTop()) - sizeof(KThread::StackParameters) - sizeof(KExceptionContext));
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KProcess *GetCurrentProcessPointer() {
|
||||
return GetCurrentThread().GetOwnerProcess();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE KProcess &GetCurrentProcess() {
|
||||
return *GetCurrentProcessPointer();
|
||||
}
|
||||
|
||||
ALWAYS_INLINE s32 GetCurrentCoreId() {
|
||||
return GetCurrentThread().GetCurrentCore();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,7 +19,10 @@
|
||||
#include <mesosphere/kern_select_cpu.hpp>
|
||||
#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_scheduler.hpp>
|
||||
#include <mesosphere/kern_k_interrupt_task_manager.hpp>
|
||||
#include <mesosphere/kern_select_interrupt_manager.hpp>
|
||||
#include <mesosphere/kern_select_hardware_timer.hpp>
|
||||
#include <mesosphere/kern_k_worker_task_manager.hpp>
|
||||
|
||||
namespace ams::kern {
|
||||
@@ -77,13 +80,6 @@ namespace ams::kern {
|
||||
static KScheduler s_schedulers[cpu::NumCores];
|
||||
static KInterruptTaskManager s_interrupt_task_managers[cpu::NumCores];
|
||||
static KHardwareTimer s_hardware_timers[cpu::NumCores];
|
||||
private:
|
||||
static ALWAYS_INLINE KCoreLocalContext &GetCoreLocalContext() {
|
||||
return reinterpret_cast<KCoreLocalRegion *>(cpu::GetCoreLocalRegionAddress())->current.context;
|
||||
}
|
||||
static ALWAYS_INLINE KCoreLocalContext &GetCoreLocalContext(s32 core_id) {
|
||||
return reinterpret_cast<KCoreLocalRegion *>(cpu::GetCoreLocalRegionAddress())->absolute[core_id].context;
|
||||
}
|
||||
public:
|
||||
static NOINLINE void InitializeCoreLocalRegion(s32 core_id);
|
||||
static NOINLINE void InitializeMainAndIdleThreads(s32 core_id);
|
||||
@@ -96,10 +92,6 @@ namespace ams::kern {
|
||||
static KThread &GetMainThread(s32 core_id);
|
||||
static KThread &GetIdleThread(s32 core_id);
|
||||
|
||||
static ALWAYS_INLINE KCurrentContext &GetCurrentContext(s32 core_id) {
|
||||
return GetCoreLocalContext(core_id).current;
|
||||
}
|
||||
|
||||
static ALWAYS_INLINE KScheduler &GetScheduler() {
|
||||
return s_schedulers[GetCurrentCoreId()];
|
||||
}
|
||||
@@ -166,4 +158,8 @@ namespace ams::kern {
|
||||
}
|
||||
};
|
||||
|
||||
ALWAYS_INLINE KScheduler &GetCurrentScheduler() {
|
||||
return Kernel::GetScheduler();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user