mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-06-01 07:18:22 -04:00
kern: Kill KCoreLocalRegion
This commit is contained in:
parent
24d545701c
commit
b0debd72a7
24 changed files with 165 additions and 334 deletions
|
@ -32,7 +32,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
|
|||
mrs x9, elr_el1
|
||||
mrs x10, spsr_el1
|
||||
mrs x11, tpidr_el0
|
||||
mrs x18, tpidr_el1
|
||||
ldr x18, [sp, #(0x120 + 0x28)]
|
||||
|
||||
/* Save callee-saved registers. */
|
||||
stp x19, x20, [sp, #(8 * 19)]
|
||||
|
@ -66,7 +66,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
|
|||
b.eq 3f
|
||||
|
||||
/* Check if our disable count allows us to call SVCs. */
|
||||
ldr x10, [x18, #0x30]
|
||||
mrs x10, tpidrro_el0
|
||||
ldrh w10, [x10, #0x100]
|
||||
cbz w10, 1f
|
||||
|
||||
|
@ -138,7 +138,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
|
|||
stp xzr, xzr, [sp, #(8 * 12)]
|
||||
stp xzr, xzr, [sp, #(8 * 14)]
|
||||
stp xzr, xzr, [sp, #(8 * 16)]
|
||||
stp xzr, x19, [sp, #(8 * 18)]
|
||||
str x19, [sp, #(8 * 19)]
|
||||
stp x20, x21, [sp, #(8 * 20)]
|
||||
stp x22, x23, [sp, #(8 * 22)]
|
||||
stp x24, x25, [sp, #(8 * 24)]
|
||||
|
@ -146,7 +146,6 @@ _ZN3ams4kern4arch5arm6412SvcHandler64Ev:
|
|||
stp x28, x29, [sp, #(8 * 28)]
|
||||
|
||||
/* Call ams::kern::arch::arm64::HandleException(ams::kern::arch::arm64::KExceptionContext *) */
|
||||
mrs x18, tpidr_el1
|
||||
mov x0, sp
|
||||
bl _ZN3ams4kern4arch5arm6415HandleExceptionEPNS2_17KExceptionContextE
|
||||
|
||||
|
@ -246,7 +245,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
|
|||
mrs x17, elr_el1
|
||||
mrs x20, spsr_el1
|
||||
mrs x19, tpidr_el0
|
||||
mrs x18, tpidr_el1
|
||||
ldr x18, [sp, #(0x120 + 0x28)]
|
||||
stp x17, x20, [sp, #(8 * 32)]
|
||||
str x19, [sp, #(8 * 34)]
|
||||
|
||||
|
@ -276,7 +275,7 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
|
|||
b.eq 3f
|
||||
|
||||
/* Check if our disable count allows us to call SVCs. */
|
||||
ldr x15, [x18, #0x30]
|
||||
mrs x15, tpidrro_el0
|
||||
ldrh w15, [x15, #0x100]
|
||||
cbz w15, 1f
|
||||
|
||||
|
@ -353,7 +352,6 @@ _ZN3ams4kern4arch5arm6412SvcHandler32Ev:
|
|||
stp xzr, xzr, [sp, #(8 * 30)]
|
||||
|
||||
/* Call ams::kern::arch::arm64::HandleException(ams::kern::arch::arm64::KExceptionContext *) */
|
||||
mrs x18, tpidr_el1
|
||||
mov x0, sp
|
||||
bl _ZN3ams4kern4arch5arm6415HandleExceptionEPNS2_17KExceptionContextE
|
||||
|
||||
|
|
|
@ -249,7 +249,6 @@ _ZN3ams4kern5board8nintendo2nx13KSleepManager11ResumeEntryEm:
|
|||
|
||||
/* Set the global context back into x18/tpidr. */
|
||||
msr tpidr_el1, x2
|
||||
mov x18, x2
|
||||
dsb sy
|
||||
isb
|
||||
|
||||
|
|
|
@ -57,14 +57,18 @@ namespace ams::kern::board::nintendo::nx::smc {
|
|||
{
|
||||
/* Disable interrupts while making the call. */
|
||||
KScopedInterruptDisable intr_disable;
|
||||
|
||||
/* Backup the current thread pointer. */
|
||||
const uintptr_t current_thread_pointer_value = cpu::GetCurrentThreadPointerValue();
|
||||
|
||||
__asm__ __volatile__("smc #1"
|
||||
: "+r"(x0), "+r"(x1), "+r"(x2), "+r"(x3), "+r"(x4), "+r"(x5), "+r"(x6), "+r"(x7)
|
||||
:
|
||||
: "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "cc", "memory"
|
||||
);
|
||||
|
||||
/* Restore the CoreLocalRegion into X18. */
|
||||
cpu::SetCoreLocalRegionAddress(cpu::GetTpidrEl1());
|
||||
/* Restore the current thread pointer into X18. */
|
||||
cpu::SetCurrentThreadPointerValue(current_thread_pointer_value);
|
||||
}
|
||||
|
||||
/* Store arguments to output. */
|
||||
|
@ -93,14 +97,18 @@ namespace ams::kern::board::nintendo::nx::smc {
|
|||
{
|
||||
/* Disable interrupts while making the call. */
|
||||
KScopedInterruptDisable intr_disable;
|
||||
|
||||
/* Backup the current thread pointer. */
|
||||
const uintptr_t current_thread_pointer_value = cpu::GetCurrentThreadPointerValue();
|
||||
|
||||
__asm__ __volatile__("smc #0"
|
||||
: "+r"(x0), "+r"(x1), "+r"(x2), "+r"(x3), "+r"(x4), "+r"(x5), "+r"(x6), "+r"(x7)
|
||||
:
|
||||
: "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "cc", "memory"
|
||||
);
|
||||
|
||||
/* Restore the CoreLocalRegion into X18. */
|
||||
cpu::SetCoreLocalRegionAddress(cpu::GetTpidrEl1());
|
||||
/* Restore the current thread pointer into X18. */
|
||||
cpu::SetCurrentThreadPointerValue(current_thread_pointer_value);
|
||||
}
|
||||
|
||||
/* Store arguments to output. */
|
||||
|
|
|
@ -496,7 +496,7 @@ namespace ams::kern {
|
|||
if (thread->GetRawState() != KThread::ThreadState_Runnable) {
|
||||
bool current = false;
|
||||
for (auto i = 0; i < static_cast<s32>(cpu::NumCores); ++i) {
|
||||
if (thread == Kernel::GetCurrentContext(i).current_thread) {
|
||||
if (thread == Kernel::GetScheduler(i).GetSchedulerCurrentThread()) {
|
||||
current = true;
|
||||
}
|
||||
break;
|
||||
|
@ -543,7 +543,7 @@ namespace ams::kern {
|
|||
if (thread->GetRawState() != KThread::ThreadState_Runnable) {
|
||||
bool current = false;
|
||||
for (auto i = 0; i < static_cast<s32>(cpu::NumCores); ++i) {
|
||||
if (thread == Kernel::GetCurrentContext(i).current_thread) {
|
||||
if (thread == Kernel::GetScheduler(i).GetSchedulerCurrentThread()) {
|
||||
current = true;
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -181,89 +181,4 @@ namespace ams::kern {
|
|||
return resource_region_size;
|
||||
}
|
||||
|
||||
namespace init {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr PageTableEntry KernelRwDataAttribute(PageTableEntry::Permission_KernelRW, PageTableEntry::PageAttribute_NormalMemory, PageTableEntry::Shareable_InnerShareable, PageTableEntry::MappingFlag_Mapped);
|
||||
|
||||
constexpr size_t CoreLocalRegionAlign = PageSize;
|
||||
constexpr size_t CoreLocalRegionSize = PageSize * (1 + cpu::NumCores);
|
||||
constexpr size_t CoreLocalRegionSizeWithGuards = CoreLocalRegionSize + 2 * PageSize;
|
||||
constexpr size_t CoreLocalRegionBoundsAlign = 1_GB;
|
||||
static_assert(CoreLocalRegionSize == sizeof(KCoreLocalRegion));
|
||||
|
||||
KVirtualAddress GetCoreLocalRegionVirtualAddress() {
|
||||
while (true) {
|
||||
const uintptr_t candidate_start = GetInteger(KMemoryLayout::GetVirtualMemoryRegionTree().GetRandomAlignedRegion(CoreLocalRegionSizeWithGuards, CoreLocalRegionAlign, KMemoryRegionType_None));
|
||||
const uintptr_t candidate_end = candidate_start + CoreLocalRegionSizeWithGuards;
|
||||
const uintptr_t candidate_last = candidate_end - 1;
|
||||
|
||||
const auto &containing_region = *KMemoryLayout::GetVirtualMemoryRegionTree().Find(candidate_start);
|
||||
|
||||
if (candidate_last > containing_region.GetLastAddress()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (containing_region.GetType() != KMemoryRegionType_None) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (util::AlignDown(candidate_start, CoreLocalRegionBoundsAlign) != util::AlignDown(candidate_last, CoreLocalRegionBoundsAlign)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (containing_region.GetAddress() > util::AlignDown(candidate_start, CoreLocalRegionBoundsAlign)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (util::AlignUp(candidate_last, CoreLocalRegionBoundsAlign) - 1 > containing_region.GetLastAddress()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return candidate_start + PageSize;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void SetupCoreLocalRegionMemoryRegions(KInitialPageTable &page_table, KInitialPageAllocator &page_allocator) {
|
||||
/* NOTE: Nintendo passes page table here to use num_l1_entries; we don't use this at present. */
|
||||
MESOSPHERE_UNUSED(page_table);
|
||||
|
||||
/* Get the virtual address of the core local reigon. */
|
||||
const KVirtualAddress core_local_virt_start = GetCoreLocalRegionVirtualAddress();
|
||||
MESOSPHERE_INIT_ABORT_UNLESS(KMemoryLayout::GetVirtualMemoryRegionTree().Insert(GetInteger(core_local_virt_start), CoreLocalRegionSize, KMemoryRegionType_CoreLocalRegion));
|
||||
|
||||
/* Allocate a page for each core. */
|
||||
KPhysicalAddress core_local_region_start_phys[cpu::NumCores] = {};
|
||||
for (size_t i = 0; i < cpu::NumCores; i++) {
|
||||
core_local_region_start_phys[i] = page_allocator.Allocate();
|
||||
}
|
||||
|
||||
/* Allocate an l1 page table for each core. */
|
||||
KPhysicalAddress core_l1_ttbr1_phys[cpu::NumCores] = {};
|
||||
core_l1_ttbr1_phys[0] = util::AlignDown(cpu::GetTtbr1El1(), PageSize);
|
||||
for (size_t i = 1; i < cpu::NumCores; i++) {
|
||||
core_l1_ttbr1_phys[i] = page_allocator.Allocate();
|
||||
std::memcpy(reinterpret_cast<void *>(GetInteger(core_l1_ttbr1_phys[i])), reinterpret_cast<void *>(GetInteger(core_l1_ttbr1_phys[0])), PageSize);
|
||||
}
|
||||
|
||||
/* Use the l1 page table for each core to map the core local region for each core. */
|
||||
for (size_t i = 0; i < cpu::NumCores; i++) {
|
||||
KInitialPageTable temp_pt(core_l1_ttbr1_phys[i], KInitialPageTable::NoClear{});
|
||||
temp_pt.Map(core_local_virt_start, PageSize, core_local_region_start_phys[i], KernelRwDataAttribute, page_allocator);
|
||||
for (size_t j = 0; j < cpu::NumCores; j++) {
|
||||
temp_pt.Map(core_local_virt_start + (j + 1) * PageSize, PageSize, core_local_region_start_phys[j], KernelRwDataAttribute, page_allocator);
|
||||
}
|
||||
|
||||
/* Setup the InitArguments. */
|
||||
SetInitArguments(static_cast<s32>(i), core_local_region_start_phys[i], GetInteger(core_l1_ttbr1_phys[i]));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -78,11 +78,6 @@ namespace ams::kern {
|
|||
}
|
||||
|
||||
void KProcess::Finalize() {
|
||||
/* Ensure we're not executing on any core. */
|
||||
for (size_t i = 0; i < cpu::NumCores; ++i) {
|
||||
MESOSPHERE_ASSERT(Kernel::GetCurrentContext(static_cast<s32>(i)).current_process.load(std::memory_order_relaxed) != this);
|
||||
}
|
||||
|
||||
/* Delete the process local region. */
|
||||
this->DeleteThreadLocalRegion(this->plr_address);
|
||||
|
||||
|
|
|
@ -66,6 +66,9 @@ namespace ams::kern {
|
|||
|
||||
/* Bind interrupt handler. */
|
||||
Kernel::GetInterruptManager().BindHandler(GetSchedulerInterruptTask(), KInterruptName_Scheduler, this->core_id, KInterruptController::PriorityLevel_Scheduler, false, false);
|
||||
|
||||
/* Set the current thread. */
|
||||
this->current_thread = GetCurrentThreadPointer();
|
||||
}
|
||||
|
||||
void KScheduler::Activate() {
|
||||
|
@ -259,18 +262,21 @@ namespace ams::kern {
|
|||
|
||||
MESOSPHERE_KTRACE_THREAD_SWITCH(next_thread);
|
||||
|
||||
if (next_thread->GetCurrentCore() != this->core_id) {
|
||||
next_thread->SetCurrentCore(this->core_id);
|
||||
}
|
||||
|
||||
/* Switch the current process, if we're switching processes. */
|
||||
if (KProcess *next_process = next_thread->GetOwnerProcess(); next_process != cur_process) {
|
||||
/* MESOSPHERE_LOG("!!! PROCESS SWITCH !!! %s -> %s\n", cur_process != nullptr ? cur_process->GetName() : nullptr, next_process != nullptr ? next_process->GetName() : nullptr); */
|
||||
KProcess::Switch(cur_process, next_process);
|
||||
}
|
||||
|
||||
/* Set the new thread. */
|
||||
SetCurrentThread(next_thread);
|
||||
this->current_thread = next_thread;
|
||||
|
||||
/* Set the new Thread Local region. */
|
||||
cpu::SwitchThreadLocalRegion(GetInteger(next_thread->GetThreadLocalRegionAddress()));
|
||||
SetCurrentThreadLocalRegion(next_thread->GetThreadLocalRegionHeapAddress());
|
||||
}
|
||||
|
||||
void KScheduler::ClearPreviousThread(KThread *thread) {
|
||||
|
|
|
@ -143,6 +143,9 @@ namespace ams::kern {
|
|||
this->num_kernel_waiters = 0;
|
||||
this->entrypoint = reinterpret_cast<uintptr_t>(func);
|
||||
|
||||
/* Set our current core id. */
|
||||
this->current_core_id = core;
|
||||
|
||||
/* We haven't released our resource limit hint, and we've spent no time on the cpu. */
|
||||
this->resource_limit_release_hint = 0;
|
||||
this->cpu_time = 0;
|
||||
|
@ -177,6 +180,7 @@ namespace ams::kern {
|
|||
this->parent->CopySvcPermissionsTo(sp);
|
||||
}
|
||||
sp.context = std::addressof(this->thread_context);
|
||||
sp.cur_thread = this;
|
||||
sp.disable_count = 1;
|
||||
this->SetInExceptionHandler();
|
||||
|
||||
|
@ -362,7 +366,7 @@ namespace ams::kern {
|
|||
for (size_t i = 0; i < cpu::NumCores; ++i) {
|
||||
KThread *core_thread;
|
||||
do {
|
||||
core_thread = Kernel::GetCurrentContext(i).current_thread.load(std::memory_order_acquire);
|
||||
core_thread = Kernel::GetScheduler(i).GetSchedulerCurrentThread();
|
||||
} while (core_thread == this);
|
||||
}
|
||||
}
|
||||
|
@ -619,7 +623,7 @@ namespace ams::kern {
|
|||
bool thread_is_current = false;
|
||||
s32 thread_core;
|
||||
for (thread_core = 0; thread_core < static_cast<s32>(cpu::NumCores); ++thread_core) {
|
||||
if (Kernel::GetCurrentContext(thread_core).current_thread == this) {
|
||||
if (Kernel::GetScheduler(thread_core).GetSchedulerCurrentThread() == this) {
|
||||
thread_is_current = true;
|
||||
break;
|
||||
}
|
||||
|
@ -834,7 +838,7 @@ namespace ams::kern {
|
|||
thread_is_current = false;
|
||||
|
||||
for (auto i = 0; i < static_cast<s32>(cpu::NumCores); ++i) {
|
||||
if (Kernel::GetCurrentContext(i).current_thread == this) {
|
||||
if (Kernel::GetScheduler(i).GetSchedulerCurrentThread() == this) {
|
||||
thread_is_current = true;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -39,20 +39,9 @@ namespace ams::kern {
|
|||
}
|
||||
|
||||
void Kernel::InitializeCoreLocalRegion(s32 core_id) {
|
||||
/* Construct the core local region object in place. */
|
||||
KCoreLocalContext *clc = GetPointer<KCoreLocalContext>(KMemoryLayout::GetCoreLocalRegionAddress());
|
||||
new (clc) KCoreLocalContext;
|
||||
|
||||
/* Set the core local region address into the global register. */
|
||||
cpu::SetCoreLocalRegionAddress(reinterpret_cast<uintptr_t>(clc));
|
||||
|
||||
/* Initialize current context. */
|
||||
clc->current.current_thread = nullptr;
|
||||
clc->current.current_process = nullptr;
|
||||
clc->current.core_id = core_id;
|
||||
clc->current.scheduler = std::addressof(Kernel::GetScheduler());
|
||||
clc->current.interrupt_task_manager = std::addressof(Kernel::GetInterruptTaskManager());
|
||||
clc->current.exception_stack_top = GetVoidPointer(KMemoryLayout::GetExceptionStackTopAddress(core_id) - sizeof(KThread::StackParameters));
|
||||
/* The core local region no longer exists, so just clear the current thread. */
|
||||
AMS_UNUSED(core_id);
|
||||
SetCurrentThread(nullptr);
|
||||
}
|
||||
|
||||
void Kernel::InitializeMainAndIdleThreads(s32 core_id) {
|
||||
|
@ -68,7 +57,6 @@ namespace ams::kern {
|
|||
|
||||
/* Set the current thread to be the main thread, and we have no processes running yet. */
|
||||
SetCurrentThread(main_thread);
|
||||
SetCurrentProcess(nullptr);
|
||||
|
||||
/* Initialize the interrupt manager, hardware timer, and scheduler */
|
||||
GetInterruptManager().Initialize(core_id);
|
||||
|
@ -126,7 +114,6 @@ namespace ams::kern {
|
|||
PrintMemoryRegion(" Stack", KMemoryLayout::GetKernelStackRegionExtents());
|
||||
PrintMemoryRegion(" Misc", KMemoryLayout::GetKernelMiscRegionExtents());
|
||||
PrintMemoryRegion(" Slab", KMemoryLayout::GetKernelSlabRegionExtents());
|
||||
PrintMemoryRegion(" CoreLocalRegion", KMemoryLayout::GetCoreLocalRegion());
|
||||
PrintMemoryRegion(" LinearRegion", KMemoryLayout::GetLinearRegionVirtualExtents());
|
||||
MESOSPHERE_LOG("\n");
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue