From ea82889e6c817b367aa1b3c4e9e7a5ea074d6530 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 11 Oct 2022 22:52:09 -0700 Subject: [PATCH] kern: increase stack region size when thread resource limit is increased --- .../include/mesosphere/kern_k_memory_layout.hpp | 2 +- .../source/kern_k_memory_layout.cpp | 7 ++----- .../source/arch/arm64/init/kern_init_core.cpp | 17 ++++++++++------- .../kernel_ldr/source/kern_init_loader.cpp | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_memory_layout.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_memory_layout.hpp index 2c4970727..f53928802 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_memory_layout.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_memory_layout.hpp @@ -187,7 +187,7 @@ namespace ams::kern { static void InitializeLinearMemoryRegionTrees(); - static size_t GetResourceRegionSizeForInit(); + static size_t GetResourceRegionSizeForInit(bool use_extra_resource); static MESOSPHERE_NOINLINE_IF_DEBUG auto GetKernelRegionExtents() { return GetVirtualMemoryRegionTree().GetDerivedRegionExtents(KMemoryRegionType_Kernel); } static MESOSPHERE_NOINLINE_IF_DEBUG auto GetKernelCodeRegionExtents() { return GetVirtualMemoryRegionTree().GetDerivedRegionExtents(KMemoryRegionType_KernelCode); } diff --git a/libraries/libmesosphere/source/kern_k_memory_layout.cpp b/libraries/libmesosphere/source/kern_k_memory_layout.cpp index 9e0c4cf4c..2084f98fe 100644 --- a/libraries/libmesosphere/source/kern_k_memory_layout.cpp +++ b/libraries/libmesosphere/source/kern_k_memory_layout.cpp @@ -126,11 +126,8 @@ namespace ams::kern { } } - size_t KMemoryLayout::GetResourceRegionSizeForInit() { - /* Calculate resource region size based on whether we allow extra threads. */ - const bool use_extra_resources = KSystemControl::Init::ShouldIncreaseThreadResourceLimit(); - - return KernelResourceSize + (use_extra_resources ? KernelSlabHeapAdditionalSize : 0); + size_t KMemoryLayout::GetResourceRegionSizeForInit(bool use_extra_resource) { + return KernelResourceSize + (use_extra_resource ? KernelSlabHeapAdditionalSize : 0); } } 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 4c7d9b11b..d099a0786 100644 --- a/mesosphere/kernel/source/arch/arm64/init/kern_init_core.cpp +++ b/mesosphere/kernel/source/arch/arm64/init/kern_init_core.cpp @@ -34,12 +34,12 @@ namespace ams::kern::init { namespace { /* Global Allocator. */ - KInitialPageAllocator g_initial_page_allocator; + constinit KInitialPageAllocator g_initial_page_allocator; /* Global initial arguments array. */ - KPhysicalAddress g_init_arguments_phys_addr[cpu::NumCores]; + constinit KPhysicalAddress g_init_arguments_phys_addr[cpu::NumCores]; - KInitArguments g_init_arguments[cpu::NumCores]; + constinit KInitArguments g_init_arguments[cpu::NumCores]; /* Page table attributes. */ constexpr PageTableEntry KernelRoDataAttribute(PageTableEntry::Permission_KernelR, PageTableEntry::PageAttribute_NormalMemory, PageTableEntry::Shareable_InnerShareable, PageTableEntry::MappingFlag_Mapped); @@ -263,14 +263,17 @@ namespace ams::kern::init { const KVirtualAddress misc_region_start = GetRandomAlignedRegion(misc_region_size, MiscRegionAlign, init_pt, KMemoryLayout::GetVirtualMemoryRegionTree(), KMemoryRegionType_Kernel); MESOSPHERE_INIT_ABORT_UNLESS(KMemoryLayout::GetVirtualMemoryRegionTree().Insert(GetInteger(misc_region_start), misc_region_size, KMemoryRegionType_KernelMisc)); + /* Determine if we'll use extra thread resources. */ + const bool use_extra_resources = KSystemControl::Init::ShouldIncreaseThreadResourceLimit(); + /* Setup the stack region. */ - constexpr size_t StackRegionSize = 14_MB; + const size_t stack_region_size = use_extra_resources ? 24_MB : 14_MB; constexpr size_t StackRegionAlign = KernelAslrAlignment; - const KVirtualAddress stack_region_start = GetRandomAlignedRegion(StackRegionSize, StackRegionAlign, init_pt, KMemoryLayout::GetVirtualMemoryRegionTree(), KMemoryRegionType_Kernel); - MESOSPHERE_INIT_ABORT_UNLESS(KMemoryLayout::GetVirtualMemoryRegionTree().Insert(GetInteger(stack_region_start), StackRegionSize, KMemoryRegionType_KernelStack)); + const KVirtualAddress stack_region_start = GetRandomAlignedRegion(stack_region_size, StackRegionAlign, init_pt, KMemoryLayout::GetVirtualMemoryRegionTree(), KMemoryRegionType_Kernel); + MESOSPHERE_INIT_ABORT_UNLESS(KMemoryLayout::GetVirtualMemoryRegionTree().Insert(GetInteger(stack_region_start), stack_region_size, KMemoryRegionType_KernelStack)); /* Determine the size of the resource region. */ - const size_t resource_region_size = KMemoryLayout::GetResourceRegionSizeForInit(); + const size_t resource_region_size = KMemoryLayout::GetResourceRegionSizeForInit(use_extra_resources); /* Determine the size of the slab region. */ const size_t slab_region_size = util::AlignUp(CalculateTotalSlabHeapSize(), PageSize); diff --git a/mesosphere/kernel_ldr/source/kern_init_loader.cpp b/mesosphere/kernel_ldr/source/kern_init_loader.cpp index 2eacd8947..7e4a5947d 100644 --- a/mesosphere/kernel_ldr/source/kern_init_loader.cpp +++ b/mesosphere/kernel_ldr/source/kern_init_loader.cpp @@ -161,7 +161,7 @@ namespace ams::kern::init::loader { const uintptr_t init_array_end_offset = layout->init_array_end_offset; /* Determine the size of the resource region. */ - const size_t resource_region_size = KMemoryLayout::GetResourceRegionSizeForInit(); + const size_t resource_region_size = KMemoryLayout::GetResourceRegionSizeForInit(KSystemControl::Init::ShouldIncreaseThreadResourceLimit()); const uintptr_t resource_end_address = base_address + resource_offset + resource_region_size; /* Setup the INI1 header in memory for the kernel. */