From 7562f807fda98c47563650ee8439503c31791629 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 28 Mar 2024 00:24:45 -0700 Subject: [PATCH] kern: pass kernel base from KernelLdr to Kernel --- .../include/mesosphere/kern_initial_process.hpp | 1 + .../include/mesosphere/kern_k_system_control_base.hpp | 2 +- .../libmesosphere/source/kern_k_system_control_base.cpp | 7 ++++--- .../kernel/source/arch/arm64/init/kern_init_core.cpp | 2 +- mesosphere/kernel_ldr/source/kern_init_loader.cpp | 2 +- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/kern_initial_process.hpp b/libraries/libmesosphere/include/mesosphere/kern_initial_process.hpp index 911298199..a7dfeab71 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_initial_process.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_initial_process.hpp @@ -32,6 +32,7 @@ namespace ams::kern { struct InitialProcessBinaryLayout { uintptr_t address; uintptr_t _08; + uintptr_t kern_address; }; struct InitialProcessBinaryLayoutWithSize { diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_system_control_base.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_system_control_base.hpp index 095bff49a..bb1c1ff0f 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_system_control_base.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_system_control_base.hpp @@ -53,7 +53,7 @@ namespace ams::kern { static size_t GetRealMemorySize(); static size_t GetIntendedMemorySize(); static KPhysicalAddress GetKernelPhysicalBaseAddress(KPhysicalAddress base_address); - static void GetInitialProcessBinaryLayout(InitialProcessBinaryLayout *out); + static void GetInitialProcessBinaryLayout(InitialProcessBinaryLayout *out, KPhysicalAddress kern_base_address); static bool ShouldIncreaseThreadResourceLimit(); static void TurnOnCpu(u64 core_id, const ams::kern::init::KInitArguments *args); static size_t GetApplicationPoolSize(); diff --git a/libraries/libmesosphere/source/kern_k_system_control_base.cpp b/libraries/libmesosphere/source/kern_k_system_control_base.cpp index 77582203c..6764662d9 100644 --- a/libraries/libmesosphere/source/kern_k_system_control_base.cpp +++ b/libraries/libmesosphere/source/kern_k_system_control_base.cpp @@ -46,10 +46,11 @@ namespace ams::kern { } } - void KSystemControlBase::Init::GetInitialProcessBinaryLayout(InitialProcessBinaryLayout *out) { + void KSystemControlBase::Init::GetInitialProcessBinaryLayout(InitialProcessBinaryLayout *out, KPhysicalAddress kern_base_address) { *out = { - .address = GetInteger(KSystemControl::Init::GetKernelPhysicalBaseAddress(ams::kern::MainMemoryAddress)) + KSystemControl::Init::GetIntendedMemorySize() - InitialProcessBinarySizeMax, - ._08 = 0, + .address = GetInteger(KSystemControl::Init::GetKernelPhysicalBaseAddress(ams::kern::MainMemoryAddress)) + KSystemControl::Init::GetIntendedMemorySize() - InitialProcessBinarySizeMax, + ._08 = 0, + .kern_address = GetInteger(kern_base_address), }; } 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 484a2426c..f74cb050c 100644 --- a/mesosphere/kernel/source/arch/arm64/init/kern_init_core.cpp +++ b/mesosphere/kernel/source/arch/arm64/init/kern_init_core.cpp @@ -345,7 +345,7 @@ namespace ams::kern::init { MESOSPHERE_INIT_ABORT_UNLESS(slab_region_size <= resource_region_size); /* Setup the slab region. */ - const KPhysicalAddress code_start_phys_addr = init_pt.GetPhysicalAddressOfRandomizedRange(code_start_virt_addr, code_region_size); + const KPhysicalAddress code_start_phys_addr = g_phase2_initial_process_binary_meta.layout.kern_address; const KPhysicalAddress code_end_phys_addr = code_start_phys_addr + code_region_size; const KPhysicalAddress slab_start_phys_addr = code_end_phys_addr; const KPhysicalAddress slab_end_phys_addr = slab_start_phys_addr + slab_region_size; diff --git a/mesosphere/kernel_ldr/source/kern_init_loader.cpp b/mesosphere/kernel_ldr/source/kern_init_loader.cpp index ce531cf1f..8b991d943 100644 --- a/mesosphere/kernel_ldr/source/kern_init_loader.cpp +++ b/mesosphere/kernel_ldr/source/kern_init_loader.cpp @@ -195,7 +195,7 @@ namespace ams::kern::init::loader { /* Setup the INI1 header in memory for the kernel. */ { /* Get the kernel layout. */ - KSystemControl::Init::GetInitialProcessBinaryLayout(std::addressof(g_initial_process_binary_meta.layout)); + KSystemControl::Init::GetInitialProcessBinaryLayout(std::addressof(g_initial_process_binary_meta.layout), base_address); /* If there's no desired base address, use the ini in place. */ if (g_initial_process_binary_meta.layout.address == 0) {