From ec96203cb7f1b9ab44dbf9446f1352852e791a79 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 11 Oct 2023 03:10:08 -0700 Subject: [PATCH] kern: remove unnecessary fields from InitArgs (0x80 -> 0x40) --- .../arch/arm64/init/kern_k_init_arguments.hpp | 10 ------ .../arch/arm64/kern_assembly_offsets.h | 17 ++++----- .../nintendo/nx/kern_k_sleep_manager.cpp | 36 +++++-------------- .../source/arch/arm64/init/kern_init_core.cpp | 20 +++++------ 4 files changed, 22 insertions(+), 61 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/arch/arm64/init/kern_k_init_arguments.hpp b/libraries/libmesosphere/include/mesosphere/arch/arm64/init/kern_k_init_arguments.hpp index 94206bfae..b8e3a9565 100644 --- a/libraries/libmesosphere/include/mesosphere/arch/arm64/init/kern_k_init_arguments.hpp +++ b/libraries/libmesosphere/include/mesosphere/arch/arm64/init/kern_k_init_arguments.hpp @@ -19,13 +19,8 @@ namespace ams::kern::init { struct alignas(util::CeilingPowerOfTwo(INIT_ARGUMENTS_SIZE)) KInitArguments { - u64 ttbr0; - u64 ttbr1; - u64 tcr; - u64 mair; u64 cpuactlr; u64 cpuectlr; - u64 sctlr; u64 sp; u64 entrypoint; u64 argument; @@ -33,13 +28,8 @@ namespace ams::kern::init { static_assert(alignof(KInitArguments) == util::CeilingPowerOfTwo(INIT_ARGUMENTS_SIZE)); static_assert(sizeof(KInitArguments) == std::max(INIT_ARGUMENTS_SIZE, util::CeilingPowerOfTwo(INIT_ARGUMENTS_SIZE))); - static_assert(AMS_OFFSETOF(KInitArguments, ttbr0) == INIT_ARGUMENTS_TTBR0); - static_assert(AMS_OFFSETOF(KInitArguments, ttbr1) == INIT_ARGUMENTS_TTBR1); - static_assert(AMS_OFFSETOF(KInitArguments, tcr) == INIT_ARGUMENTS_TCR); - static_assert(AMS_OFFSETOF(KInitArguments, mair) == INIT_ARGUMENTS_MAIR); static_assert(AMS_OFFSETOF(KInitArguments, cpuactlr) == INIT_ARGUMENTS_CPUACTLR); static_assert(AMS_OFFSETOF(KInitArguments, cpuectlr) == INIT_ARGUMENTS_CPUECTLR); - static_assert(AMS_OFFSETOF(KInitArguments, sctlr) == INIT_ARGUMENTS_SCTLR); static_assert(AMS_OFFSETOF(KInitArguments, sp) == INIT_ARGUMENTS_SP); static_assert(AMS_OFFSETOF(KInitArguments, entrypoint) == INIT_ARGUMENTS_ENTRYPOINT); static_assert(AMS_OFFSETOF(KInitArguments, argument) == INIT_ARGUMENTS_ARGUMENT); diff --git a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h index b585f44eb..46d956e61 100644 --- a/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h +++ b/libraries/libmesosphere/include/mesosphere/arch/arm64/kern_assembly_offsets.h @@ -246,17 +246,12 @@ #define THREAD_LOCAL_REGION_SIZE 0x200 /* ams::kern::init::KInitArguments, https://github.com/Atmosphere-NX/Atmosphere/blob/master/libraries/libmesosphere/include/mesosphere/arch/arm64/init/kern_k_init_arguments.hpp */ -#define INIT_ARGUMENTS_SIZE 0x50 -#define INIT_ARGUMENTS_TTBR0 0x00 -#define INIT_ARGUMENTS_TTBR1 0x08 -#define INIT_ARGUMENTS_TCR 0x10 -#define INIT_ARGUMENTS_MAIR 0x18 -#define INIT_ARGUMENTS_CPUACTLR 0x20 -#define INIT_ARGUMENTS_CPUECTLR 0x28 -#define INIT_ARGUMENTS_SCTLR 0x30 -#define INIT_ARGUMENTS_SP 0x38 -#define INIT_ARGUMENTS_ENTRYPOINT 0x40 -#define INIT_ARGUMENTS_ARGUMENT 0x48 +#define INIT_ARGUMENTS_SIZE 0x28 +#define INIT_ARGUMENTS_CPUACTLR 0x00 +#define INIT_ARGUMENTS_CPUECTLR 0x08 +#define INIT_ARGUMENTS_SP 0x10 +#define INIT_ARGUMENTS_ENTRYPOINT 0x18 +#define INIT_ARGUMENTS_ARGUMENT 0x20 /* ams::kern::KScheduler (::SchedulingState), https://github.com/Atmosphere-NX/Atmosphere/blob/master/libraries/libmesosphere/include/mesosphere/kern_k_scheduler.hpp */ /* NOTE: Due to constraints on ldarb relative offsets, KSCHEDULER_NEEDS_SCHEDULING cannot trivially be changed, and will require assembly edits. */ diff --git a/libraries/libmesosphere/source/board/nintendo/nx/kern_k_sleep_manager.cpp b/libraries/libmesosphere/source/board/nintendo/nx/kern_k_sleep_manager.cpp index 00b606ff5..bb782d390 100644 --- a/libraries/libmesosphere/source/board/nintendo/nx/kern_k_sleep_manager.cpp +++ b/libraries/libmesosphere/source/board/nintendo/nx/kern_k_sleep_manager.cpp @@ -32,7 +32,6 @@ namespace ams::kern::board::nintendo::nx { class SavedSystemRegisters { private: u64 ttbr0_el1; - u64 tcr_el1; u64 elr_el1; u64 sp_el0; u64 spsr_el1; @@ -92,7 +91,6 @@ namespace ams::kern::board::nintendo::nx { void SavedSystemRegisters::Save() { /* Save system registers. */ this->ttbr0_el1 = cpu::GetTtbr0El1(); - this->tcr_el1 = cpu::GetTcrEl1(); this->tpidr_el0 = cpu::GetTpidrEl0(); this->elr_el1 = cpu::GetElrEl1(); this->sp_el0 = cpu::GetSpEl0(); @@ -408,7 +406,6 @@ namespace ams::kern::board::nintendo::nx { /* Restore system registers. */ cpu::SetTtbr0El1 (this->ttbr0_el1); - cpu::SetTcrEl1 (this->tcr_el1); cpu::SetTpidrEl0 (this->tpidr_el0); cpu::SetElrEl1 (this->elr_el1); cpu::SetSpEl0 (this->sp_el0); @@ -515,24 +512,6 @@ namespace ams::kern::board::nintendo::nx { /* Save the system registers for the current core. */ g_sleep_system_registers[core_id].Save(); - /* Change the translation tables to use the kernel table. */ - { - /* Get the current value of the translation control register. */ - const u64 tcr = cpu::GetTcrEl1(); - - /* Disable translation table walks on tlb miss. */ - cpu::TranslationControlRegisterAccessor(tcr).SetEpd0(true).Store(); - cpu::EnsureInstructionConsistency(); - - /* Change the translation table base (ttbr0) to use the kernel table. */ - cpu::SetTtbr0El1(Kernel::GetKernelPageTable().GetIdentityMapTtbr0(core_id)); - cpu::EnsureInstructionConsistency(); - - /* Enable translation table walks on tlb miss. */ - cpu::TranslationControlRegisterAccessor(tcr).SetEpd0(false).Store(); - cpu::EnsureInstructionConsistency(); - } - /* Invalidate the entire tlb. */ cpu::InvalidateEntireTlb(); @@ -552,13 +531,14 @@ namespace ams::kern::board::nintendo::nx { /* Setup the initial arguments. */ { - init_args->ttbr0 = cpu::GetTtbr0El1(); - init_args->ttbr1 = cpu::GetTtbr1El1(); - init_args->tcr = cpu::GetTcrEl1(); - init_args->mair = cpu::GetMairEl1(); - init_args->cpuactlr = cpu::GetCpuActlrEl1(); - init_args->cpuectlr = cpu::GetCpuEctlrEl1(); - init_args->sctlr = cpu::GetSctlrEl1(); + /* Determine whether we're running on a cortex-a53 or a-57. */ + cpu::MainIdRegisterAccessor midr_el1; + const auto implementer = midr_el1.GetImplementer(); + const auto primary_part = midr_el1.GetPrimaryPartNumber(); + const bool needs_cpu_ctlr = (implementer == cpu::MainIdRegisterAccessor::Implementer::ArmLimited) && (primary_part == cpu::MainIdRegisterAccessor::PrimaryPartNumber::CortexA57 || primary_part == cpu::MainIdRegisterAccessor::PrimaryPartNumber::CortexA53); + + init_args->cpuactlr = needs_cpu_ctlr ? cpu::GetCpuActlrEl1() : 0; + init_args->cpuectlr = needs_cpu_ctlr ? cpu::GetCpuEctlrEl1() : 0; init_args->sp = 0; init_args->entrypoint = reinterpret_cast(::ams::kern::board::nintendo::nx::KSleepManager::ResumeEntry); init_args->argument = sleep_buffer; diff --git a/mesosphere/kernel/source/arch/arm64/init/kern_init_core.cpp b/mesosphere/kernel/source/arch/arm64/init/kern_init_core.cpp index c8139fb49..5058e0716 100644 --- a/mesosphere/kernel/source/arch/arm64/init/kern_init_core.cpp +++ b/mesosphere/kernel/source/arch/arm64/init/kern_init_core.cpp @@ -86,27 +86,23 @@ namespace ams::kern::init { } void SetupInitialArguments() { + /* Determine whether we're running on a cortex-a53 or a-57. */ + cpu::MainIdRegisterAccessor midr_el1; + const auto implementer = midr_el1.GetImplementer(); + const auto primary_part = midr_el1.GetPrimaryPartNumber(); + const bool needs_cpu_ctlr = (implementer == cpu::MainIdRegisterAccessor::Implementer::ArmLimited) && (primary_part == cpu::MainIdRegisterAccessor::PrimaryPartNumber::CortexA57 || primary_part == cpu::MainIdRegisterAccessor::PrimaryPartNumber::CortexA53); + /* Get parameters for initial arguments. */ - const u64 ttbr0 = cpu::GetTtbr0El1(); - const u64 ttbr1 = cpu::GetTtbr1El1(); - const u64 tcr = cpu::GetTcrEl1(); - const u64 mair = cpu::GetMairEl1(); - const u64 cpuactlr = cpu::GetCpuActlrEl1(); - const u64 cpuectlr = cpu::GetCpuEctlrEl1(); - const u64 sctlr = cpu::GetSctlrEl1(); + const u64 cpuactlr = needs_cpu_ctlr ? cpu::GetCpuActlrEl1() : 0; + const u64 cpuectlr = needs_cpu_ctlr ? cpu::GetCpuEctlrEl1() : 0; for (s32 i = 0; i < static_cast(cpu::NumCores); ++i) { /* Get the arguments. */ KInitArguments *init_args = g_init_arguments + i; /* Set the arguments. */ - init_args->ttbr0 = ttbr0; - init_args->ttbr1 = ttbr1; - init_args->tcr = tcr; - init_args->mair = mair; init_args->cpuactlr = cpuactlr; init_args->cpuectlr = cpuectlr; - init_args->sctlr = sctlr; init_args->sp = GetInteger(KMemoryLayout::GetMainStackTopAddress(i)) - sizeof(KThread::StackParameters); init_args->entrypoint = reinterpret_cast(::ams::kern::init::InvokeMain); init_args->argument = static_cast(i);