From d3d0f2f451041a23214978ee224051a8575cf119 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 2 Apr 2021 22:16:58 -0700 Subject: hle: kernel: Add initial impl. of slab setup. --- src/core/hle/kernel/init/init_slab_setup.cpp | 183 +++++++++++++++++++++++++++ src/core/hle/kernel/init/init_slab_setup.h | 42 ++++++ 2 files changed, 225 insertions(+) create mode 100644 src/core/hle/kernel/init/init_slab_setup.cpp create mode 100644 src/core/hle/kernel/init/init_slab_setup.h (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp new file mode 100644 index 000000000..a290249c7 --- /dev/null +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -0,0 +1,183 @@ +// Copyright 2021 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/alignment.h" +#include "common/assert.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "core/core.h" +#include "core/hardware_properties.h" +#include "core/hle/kernel/init/init_slab_setup.h" +#include "core/hle/kernel/k_memory_layout.h" +#include "core/hle/kernel/k_memory_manager.h" +#include "core/hle/kernel/k_system_control.h" +#include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/memory_types.h" +#include "core/memory.h" + +namespace Kernel::Init { + +#define SLAB_COUNT(CLASS) g_slab_resource_counts.num_##CLASS + +#define FOREACH_SLAB_TYPE(HANDLER, ...) HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) + +namespace { + +#define DEFINE_SLAB_TYPE_ENUM_MEMBER(NAME, COUNT, ...) KSlabType_##NAME, + +enum KSlabType : u32 { + FOREACH_SLAB_TYPE(DEFINE_SLAB_TYPE_ENUM_MEMBER) KSlabType_Count, +}; + +#undef DEFINE_SLAB_TYPE_ENUM_MEMBER + +// Constexpr counts. +constexpr size_t SlabCountKProcess = 80; +constexpr size_t SlabCountKThread = 800; +constexpr size_t SlabCountKEvent = 700; +constexpr size_t SlabCountKInterruptEvent = 100; +constexpr size_t SlabCountKPort = 256 + 0x20; // Extra 0x20 ports over Nintendo for homebrew. +constexpr size_t SlabCountKSharedMemory = 80; +constexpr size_t SlabCountKTransferMemory = 200; +constexpr size_t SlabCountKCodeMemory = 10; +constexpr size_t SlabCountKDeviceAddressSpace = 300; +constexpr size_t SlabCountKSession = 933; +constexpr size_t SlabCountKLightSession = 100; +constexpr size_t SlabCountKObjectName = 7; +constexpr size_t SlabCountKResourceLimit = 5; +constexpr size_t SlabCountKDebug = Core::Hardware::NUM_CPU_CORES; +constexpr size_t SlabCountKAlpha = 1; +constexpr size_t SlabCountKBeta = 6; + +constexpr size_t SlabCountExtraKThread = 160; + +// Global to hold our resource counts. +KSlabResourceCounts g_slab_resource_counts = { + .num_KProcess = SlabCountKProcess, + .num_KThread = SlabCountKThread, + .num_KEvent = SlabCountKEvent, + .num_KInterruptEvent = SlabCountKInterruptEvent, + .num_KPort = SlabCountKPort, + .num_KSharedMemory = SlabCountKSharedMemory, + .num_KTransferMemory = SlabCountKTransferMemory, + .num_KCodeMemory = SlabCountKCodeMemory, + .num_KDeviceAddressSpace = SlabCountKDeviceAddressSpace, + .num_KSession = SlabCountKSession, + .num_KLightSession = SlabCountKLightSession, + .num_KObjectName = SlabCountKObjectName, + .num_KResourceLimit = SlabCountKResourceLimit, + .num_KDebug = SlabCountKDebug, + .num_KAlpha = SlabCountKAlpha, + .num_KBeta = SlabCountKBeta, +}; + +template +VAddr InitializeSlabHeap(Core::System& system, KMemoryLayout& memory_layout, VAddr address, + size_t num_objects) { + const size_t size = Common::AlignUp(sizeof(T) * num_objects, alignof(void*)); + VAddr start = Common::AlignUp(address, alignof(T)); + + if (size > 0) { + const KMemoryRegion* region = memory_layout.FindVirtual(start + size - 1); + ASSERT(region != nullptr); + ASSERT(region->IsDerivedFrom(KMemoryRegionType_KernelSlab)); + T::InitializeSlabHeap(system.Kernel(), system.Memory().GetKernelBuffer(start, size), size); + } + + return start + size; +} + +} // namespace + +const KSlabResourceCounts& GetSlabResourceCounts() { + return g_slab_resource_counts; +} + +void InitializeSlabResourceCounts() { + // Note: Nintendo initializes all fields here, but we initialize all constants at compile-time. + + if (KSystemControl::Init::ShouldIncreaseThreadResourceLimit()) { + g_slab_resource_counts.num_KThread += SlabCountExtraKThread; + } +} + +size_t CalculateSlabHeapGapSize() { + return KernelSlabHeapGapsSize; +} + +size_t CalculateTotalSlabHeapSize() { + size_t size = 0; + +#define ADD_SLAB_SIZE(NAME, COUNT, ...) \ + { \ + size += alignof(NAME); \ + size += Common::AlignUp(sizeof(NAME) * (COUNT), alignof(void*)); \ + }; + + // Add the size required for each slab. + FOREACH_SLAB_TYPE(ADD_SLAB_SIZE) + +#undef ADD_SLAB_SIZE + + // Add the reserved size. + size += CalculateSlabHeapGapSize(); + + return size; +} + +void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { + // Get the start of the slab region, since that's where we'll be working. + VAddr address = memory_layout.GetSlabRegionAddress(); + + // Initialize slab type array to be in sorted order. + KSlabType slab_types[KSlabType_Count]; + for (size_t i = 0; i < Common::Size(slab_types); i++) { + slab_types[i] = static_cast(i); + } + + // N shuffles the slab type array with the following simple algorithm. + for (size_t i = 0; i < Common::Size(slab_types); i++) { + const size_t rnd = KSystemControl::GenerateRandomRange(0, Common::Size(slab_types) - 1); + std::swap(slab_types[i], slab_types[rnd]); + } + + // Create an array to represent the gaps between the slabs. + const size_t total_gap_size = CalculateSlabHeapGapSize(); + size_t slab_gaps[Common::Size(slab_types)]; + for (size_t i = 0; i < Common::Size(slab_gaps); i++) { + // Note: This is an off-by-one error from Nintendo's intention, because GenerateRandomRange + // is inclusive. However, Nintendo also has the off-by-one error, and it's "harmless", so we + // will include it ourselves. + slab_gaps[i] = KSystemControl::GenerateRandomRange(0, total_gap_size); + } + + // Sort the array, so that we can treat differences between values as offsets to the starts of + // slabs. + for (size_t i = 1; i < Common::Size(slab_gaps); i++) { + for (size_t j = i; j > 0 && slab_gaps[j - 1] > slab_gaps[j]; j--) { + std::swap(slab_gaps[j], slab_gaps[j - 1]); + } + } + + for (size_t i = 0; i < Common::Size(slab_types); i++) { + // Add the random gap to the address. + address += (i == 0) ? slab_gaps[0] : slab_gaps[i] - slab_gaps[i - 1]; + +#define INITIALIZE_SLAB_HEAP(NAME, COUNT, ...) \ + case KSlabType_##NAME: \ + address = InitializeSlabHeap(system, memory_layout, address, COUNT); \ + break; + + // Initialize the slabheap. + switch (slab_types[i]) { + // For each of the slab types, we want to initialize that heap. + FOREACH_SLAB_TYPE(INITIALIZE_SLAB_HEAP) + // If we somehow get an invalid type, abort. + default: + UNREACHABLE(); + } + } +} + +} // namespace Kernel::Init diff --git a/src/core/hle/kernel/init/init_slab_setup.h b/src/core/hle/kernel/init/init_slab_setup.h new file mode 100644 index 000000000..6418b97ac --- /dev/null +++ b/src/core/hle/kernel/init/init_slab_setup.h @@ -0,0 +1,42 @@ +// Copyright 2021 yuzu emulator team +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +namespace Core { +class System; +} // namespace Core + +namespace Kernel { +class KMemoryLayout; +} // namespace Kernel + +namespace Kernel::Init { + +struct KSlabResourceCounts { + size_t num_KProcess; + size_t num_KThread; + size_t num_KEvent; + size_t num_KInterruptEvent; + size_t num_KPort; + size_t num_KSharedMemory; + size_t num_KTransferMemory; + size_t num_KCodeMemory; + size_t num_KDeviceAddressSpace; + size_t num_KSession; + size_t num_KLightSession; + size_t num_KObjectName; + size_t num_KResourceLimit; + size_t num_KDebug; + size_t num_KAlpha; + size_t num_KBeta; +}; + +void InitializeSlabResourceCounts(); +const KSlabResourceCounts& GetSlabResourceCounts(); + +size_t CalculateTotalSlabHeapSize(); +void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout); + +} // namespace Kernel::Init -- cgit v1.2.3 From 7ccbdd4d8d3dea7294d2cac38779cceea9745d52 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 3 Apr 2021 22:22:36 -0700 Subject: hle: kernel: Migrate KProcess to KAutoObject. --- src/core/hle/kernel/init/init_slab_setup.cpp | 9 ++++++--- src/core/hle/kernel/init/init_slab_setup.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index a290249c7..a6fed524b 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -14,13 +14,16 @@ #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/memory_types.h" +#include "core/hle/kernel/process.h" #include "core/memory.h" namespace Kernel::Init { #define SLAB_COUNT(CLASS) g_slab_resource_counts.num_##CLASS -#define FOREACH_SLAB_TYPE(HANDLER, ...) HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) +#define FOREACH_SLAB_TYPE(HANDLER, ...) \ + HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ + HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) namespace { @@ -33,7 +36,7 @@ enum KSlabType : u32 { #undef DEFINE_SLAB_TYPE_ENUM_MEMBER // Constexpr counts. -constexpr size_t SlabCountKProcess = 80; +constexpr size_t SlabCountProcess = 80; constexpr size_t SlabCountKThread = 800; constexpr size_t SlabCountKEvent = 700; constexpr size_t SlabCountKInterruptEvent = 100; @@ -54,7 +57,7 @@ constexpr size_t SlabCountExtraKThread = 160; // Global to hold our resource counts. KSlabResourceCounts g_slab_resource_counts = { - .num_KProcess = SlabCountKProcess, + .num_Process = SlabCountProcess, .num_KThread = SlabCountKThread, .num_KEvent = SlabCountKEvent, .num_KInterruptEvent = SlabCountKInterruptEvent, diff --git a/src/core/hle/kernel/init/init_slab_setup.h b/src/core/hle/kernel/init/init_slab_setup.h index 6418b97ac..8876678b3 100644 --- a/src/core/hle/kernel/init/init_slab_setup.h +++ b/src/core/hle/kernel/init/init_slab_setup.h @@ -15,7 +15,7 @@ class KMemoryLayout; namespace Kernel::Init { struct KSlabResourceCounts { - size_t num_KProcess; + size_t num_Process; size_t num_KThread; size_t num_KEvent; size_t num_KInterruptEvent; -- cgit v1.2.3 From 086db71e942dc3468bccb741cabf62fdd221e790 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 3 Apr 2021 23:22:07 -0700 Subject: hle: kernel: Migrate KSharedMemory to KAutoObject. --- src/core/hle/kernel/init/init_slab_setup.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index a6fed524b..eb9c8e2e4 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -11,6 +11,7 @@ #include "core/hle/kernel/init/init_slab_setup.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" +#include "core/hle/kernel/k_shared_memory.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/memory_types.h" @@ -23,7 +24,8 @@ namespace Kernel::Init { #define FOREACH_SLAB_TYPE(HANDLER, ...) \ HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ - HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) + HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ + HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) namespace { -- cgit v1.2.3 From addc0bf0379e075786048921bede6e089552a6db Mon Sep 17 00:00:00 2001 From: bunnei Date: Sun, 4 Apr 2021 00:56:09 -0700 Subject: hle: kernel: Migrate KEvent to KAutoObject. --- src/core/hle/kernel/init/init_slab_setup.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index eb9c8e2e4..b292f7db2 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -9,6 +9,7 @@ #include "core/core.h" #include "core/hardware_properties.h" #include "core/hle/kernel/init/init_slab_setup.h" +#include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_shared_memory.h" @@ -25,6 +26,7 @@ namespace Kernel::Init { #define FOREACH_SLAB_TYPE(HANDLER, ...) \ HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ + HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) namespace { -- cgit v1.2.3 From 7444963bbb300cff269e410948de7fa577f5ff16 Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 13 Apr 2021 17:48:37 -0700 Subject: hle: kernel: Migrate KSession, KClientSession, and KServerSession to KAutoObject. --- src/core/hle/kernel/init/init_slab_setup.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index b292f7db2..84d509d52 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -12,6 +12,7 @@ #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" +#include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" @@ -27,7 +28,8 @@ namespace Kernel::Init { HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ - HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) + HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) \ + HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) namespace { -- cgit v1.2.3 From c7d8b7421cd6bdb64410bbb0094ce540f0280c27 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 17 Apr 2021 00:52:53 -0700 Subject: hle: kernel: Migrate KTransferMemory to KAutoObject. --- src/core/hle/kernel/init/init_slab_setup.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index 84d509d52..ce7a24c40 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -14,6 +14,7 @@ #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" +#include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/memory_types.h" @@ -28,6 +29,7 @@ namespace Kernel::Init { HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ + HANDLER(KTransferMemory, (SLAB_COUNT(KTransferMemory)), ##__VA_ARGS__) \ HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) \ HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) -- cgit v1.2.3 From b57c5a9b54b23a348d7e80e51943f27a54fb8c2f Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 20 Apr 2021 21:28:11 -0700 Subject: hle: kernel: Migrate KResourceLimit to KAutoObject. --- src/core/hle/kernel/init/init_slab_setup.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index ce7a24c40..a5ddd7344 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -12,11 +12,12 @@ #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" +#include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" -#include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/k_system_control.h" #include "core/hle/kernel/k_thread.h" +#include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/memory_types.h" #include "core/hle/kernel/process.h" #include "core/memory.h" @@ -31,7 +32,8 @@ namespace Kernel::Init { HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ HANDLER(KTransferMemory, (SLAB_COUNT(KTransferMemory)), ##__VA_ARGS__) \ HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) \ - HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) + HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) \ + HANDLER(KResourceLimit, (SLAB_COUNT(KResourceLimit)), ##__VA_ARGS__) namespace { -- cgit v1.2.3 From 626f746971d1d3216a38b20680959df3a1f5f256 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 23 Apr 2021 17:00:15 -0700 Subject: hle: kernel: Migrate KPort, KClientPort, and KServerPort to KAutoObject. --- src/core/hle/kernel/init/init_slab_setup.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index a5ddd7344..f8c255732 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -12,6 +12,7 @@ #include "core/hle/kernel/k_event.h" #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" +#include "core/hle/kernel/k_port.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" @@ -30,8 +31,9 @@ namespace Kernel::Init { HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ - HANDLER(KTransferMemory, (SLAB_COUNT(KTransferMemory)), ##__VA_ARGS__) \ + HANDLER(KPort, (SLAB_COUNT(KPort)), ##__VA_ARGS__) \ HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) \ + HANDLER(KTransferMemory, (SLAB_COUNT(KTransferMemory)), ##__VA_ARGS__) \ HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) \ HANDLER(KResourceLimit, (SLAB_COUNT(KResourceLimit)), ##__VA_ARGS__) -- cgit v1.2.3 From 2a7eff57a8048933a89c1a8f8d6dced7b5d604f2 Mon Sep 17 00:00:00 2001 From: bunnei Date: Fri, 23 Apr 2021 22:04:28 -0700 Subject: hle: kernel: Rename Process to KProcess. --- src/core/hle/kernel/init/init_slab_setup.cpp | 8 ++++---- src/core/hle/kernel/init/init_slab_setup.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index f8c255732..04e481a0a 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -13,6 +13,7 @@ #include "core/hle/kernel/k_memory_layout.h" #include "core/hle/kernel/k_memory_manager.h" #include "core/hle/kernel/k_port.h" +#include "core/hle/kernel/k_process.h" #include "core/hle/kernel/k_resource_limit.h" #include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_shared_memory.h" @@ -20,7 +21,6 @@ #include "core/hle/kernel/k_thread.h" #include "core/hle/kernel/k_transfer_memory.h" #include "core/hle/kernel/memory_types.h" -#include "core/hle/kernel/process.h" #include "core/memory.h" namespace Kernel::Init { @@ -28,7 +28,7 @@ namespace Kernel::Init { #define SLAB_COUNT(CLASS) g_slab_resource_counts.num_##CLASS #define FOREACH_SLAB_TYPE(HANDLER, ...) \ - HANDLER(Process, (SLAB_COUNT(Process)), ##__VA_ARGS__) \ + HANDLER(KProcess, (SLAB_COUNT(KProcess)), ##__VA_ARGS__) \ HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ HANDLER(KPort, (SLAB_COUNT(KPort)), ##__VA_ARGS__) \ @@ -48,7 +48,7 @@ enum KSlabType : u32 { #undef DEFINE_SLAB_TYPE_ENUM_MEMBER // Constexpr counts. -constexpr size_t SlabCountProcess = 80; +constexpr size_t SlabCountKProcess = 80; constexpr size_t SlabCountKThread = 800; constexpr size_t SlabCountKEvent = 700; constexpr size_t SlabCountKInterruptEvent = 100; @@ -69,7 +69,7 @@ constexpr size_t SlabCountExtraKThread = 160; // Global to hold our resource counts. KSlabResourceCounts g_slab_resource_counts = { - .num_Process = SlabCountProcess, + .num_KProcess = SlabCountKProcess, .num_KThread = SlabCountKThread, .num_KEvent = SlabCountKEvent, .num_KInterruptEvent = SlabCountKInterruptEvent, diff --git a/src/core/hle/kernel/init/init_slab_setup.h b/src/core/hle/kernel/init/init_slab_setup.h index 8876678b3..6418b97ac 100644 --- a/src/core/hle/kernel/init/init_slab_setup.h +++ b/src/core/hle/kernel/init/init_slab_setup.h @@ -15,7 +15,7 @@ class KMemoryLayout; namespace Kernel::Init { struct KSlabResourceCounts { - size_t num_Process; + size_t num_KProcess; size_t num_KThread; size_t num_KEvent; size_t num_KInterruptEvent; -- cgit v1.2.3 From 91d865795933136fa57fe7fdceffddd1b6c49084 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 12:25:51 -0700 Subject: fixup! hle: kernel: Add initial impl. of slab setup. --- src/core/hle/kernel/init/init_slab_setup.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index 04e481a0a..3aa76ee33 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -146,21 +146,21 @@ void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { VAddr address = memory_layout.GetSlabRegionAddress(); // Initialize slab type array to be in sorted order. - KSlabType slab_types[KSlabType_Count]; - for (size_t i = 0; i < Common::Size(slab_types); i++) { + std::array slab_types; + for (size_t i = 0; i < slab_types.size(); i++) { slab_types[i] = static_cast(i); } // N shuffles the slab type array with the following simple algorithm. - for (size_t i = 0; i < Common::Size(slab_types); i++) { - const size_t rnd = KSystemControl::GenerateRandomRange(0, Common::Size(slab_types) - 1); + for (size_t i = 0; i < slab_types.size(); i++) { + const size_t rnd = KSystemControl::GenerateRandomRange(0, slab_types.size() - 1); std::swap(slab_types[i], slab_types[rnd]); } // Create an array to represent the gaps between the slabs. const size_t total_gap_size = CalculateSlabHeapGapSize(); - size_t slab_gaps[Common::Size(slab_types)]; - for (size_t i = 0; i < Common::Size(slab_gaps); i++) { + std::array slab_gaps; + for (size_t i = 0; i < slab_gaps.size(); i++) { // Note: This is an off-by-one error from Nintendo's intention, because GenerateRandomRange // is inclusive. However, Nintendo also has the off-by-one error, and it's "harmless", so we // will include it ourselves. @@ -169,13 +169,13 @@ void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { // Sort the array, so that we can treat differences between values as offsets to the starts of // slabs. - for (size_t i = 1; i < Common::Size(slab_gaps); i++) { + for (size_t i = 1; i < slab_gaps.size(); i++) { for (size_t j = i; j > 0 && slab_gaps[j - 1] > slab_gaps[j]; j--) { std::swap(slab_gaps[j], slab_gaps[j - 1]); } } - for (size_t i = 0; i < Common::Size(slab_types); i++) { + for (size_t i = 0; i < slab_types.size(); i++) { // Add the random gap to the address. address += (i == 0) ? slab_gaps[0] : slab_gaps[i] - slab_gaps[i - 1]; -- cgit v1.2.3 From f23760b1e1b4dc703dffaa245b7b469141935342 Mon Sep 17 00:00:00 2001 From: bunnei Date: Sat, 1 May 2021 22:30:42 -0700 Subject: fixup! hle: kernel: Add initial impl. of slab setup. --- src/core/hle/kernel/init/init_slab_setup.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index 3aa76ee33..2dd792e71 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -117,10 +117,6 @@ void InitializeSlabResourceCounts() { } } -size_t CalculateSlabHeapGapSize() { - return KernelSlabHeapGapsSize; -} - size_t CalculateTotalSlabHeapSize() { size_t size = 0; @@ -136,7 +132,7 @@ size_t CalculateTotalSlabHeapSize() { #undef ADD_SLAB_SIZE // Add the reserved size. - size += CalculateSlabHeapGapSize(); + size += KernelSlabHeapGapsSize; return size; } @@ -158,7 +154,7 @@ void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { } // Create an array to represent the gaps between the slabs. - const size_t total_gap_size = CalculateSlabHeapGapSize(); + const size_t total_gap_size = KernelSlabHeapGapsSize; std::array slab_gaps; for (size_t i = 0; i < slab_gaps.size(); i++) { // Note: This is an off-by-one error from Nintendo's intention, because GenerateRandomRange -- cgit v1.2.3 From b805ee653f3d178ed2b4b2e0a403bb0ab61dad8b Mon Sep 17 00:00:00 2001 From: bunnei Date: Tue, 4 May 2021 21:35:42 -0700 Subject: hle: kernel: Move slab resource counts to Kernel. --- src/core/hle/kernel/init/init_slab_setup.cpp | 54 ++++++++++++++-------------- src/core/hle/kernel/init/init_slab_setup.h | 9 ++--- 2 files changed, 31 insertions(+), 32 deletions(-) (limited to 'src/core/hle/kernel/init') diff --git a/src/core/hle/kernel/init/init_slab_setup.cpp b/src/core/hle/kernel/init/init_slab_setup.cpp index 2dd792e71..69ae405e6 100644 --- a/src/core/hle/kernel/init/init_slab_setup.cpp +++ b/src/core/hle/kernel/init/init_slab_setup.cpp @@ -25,7 +25,7 @@ namespace Kernel::Init { -#define SLAB_COUNT(CLASS) g_slab_resource_counts.num_##CLASS +#define SLAB_COUNT(CLASS) kernel.SlabResourceCounts().num_##CLASS #define FOREACH_SLAB_TYPE(HANDLER, ...) \ HANDLER(KProcess, (SLAB_COUNT(KProcess)), ##__VA_ARGS__) \ @@ -67,26 +67,6 @@ constexpr size_t SlabCountKBeta = 6; constexpr size_t SlabCountExtraKThread = 160; -// Global to hold our resource counts. -KSlabResourceCounts g_slab_resource_counts = { - .num_KProcess = SlabCountKProcess, - .num_KThread = SlabCountKThread, - .num_KEvent = SlabCountKEvent, - .num_KInterruptEvent = SlabCountKInterruptEvent, - .num_KPort = SlabCountKPort, - .num_KSharedMemory = SlabCountKSharedMemory, - .num_KTransferMemory = SlabCountKTransferMemory, - .num_KCodeMemory = SlabCountKCodeMemory, - .num_KDeviceAddressSpace = SlabCountKDeviceAddressSpace, - .num_KSession = SlabCountKSession, - .num_KLightSession = SlabCountKLightSession, - .num_KObjectName = SlabCountKObjectName, - .num_KResourceLimit = SlabCountKResourceLimit, - .num_KDebug = SlabCountKDebug, - .num_KAlpha = SlabCountKAlpha, - .num_KBeta = SlabCountKBeta, -}; - template VAddr InitializeSlabHeap(Core::System& system, KMemoryLayout& memory_layout, VAddr address, size_t num_objects) { @@ -105,19 +85,35 @@ VAddr InitializeSlabHeap(Core::System& system, KMemoryLayout& memory_layout, VAd } // namespace -const KSlabResourceCounts& GetSlabResourceCounts() { - return g_slab_resource_counts; +KSlabResourceCounts KSlabResourceCounts::CreateDefault() { + return { + .num_KProcess = SlabCountKProcess, + .num_KThread = SlabCountKThread, + .num_KEvent = SlabCountKEvent, + .num_KInterruptEvent = SlabCountKInterruptEvent, + .num_KPort = SlabCountKPort, + .num_KSharedMemory = SlabCountKSharedMemory, + .num_KTransferMemory = SlabCountKTransferMemory, + .num_KCodeMemory = SlabCountKCodeMemory, + .num_KDeviceAddressSpace = SlabCountKDeviceAddressSpace, + .num_KSession = SlabCountKSession, + .num_KLightSession = SlabCountKLightSession, + .num_KObjectName = SlabCountKObjectName, + .num_KResourceLimit = SlabCountKResourceLimit, + .num_KDebug = SlabCountKDebug, + .num_KAlpha = SlabCountKAlpha, + .num_KBeta = SlabCountKBeta, + }; } -void InitializeSlabResourceCounts() { - // Note: Nintendo initializes all fields here, but we initialize all constants at compile-time. - +void InitializeSlabResourceCounts(KernelCore& kernel) { + kernel.SlabResourceCounts() = KSlabResourceCounts::CreateDefault(); if (KSystemControl::Init::ShouldIncreaseThreadResourceLimit()) { - g_slab_resource_counts.num_KThread += SlabCountExtraKThread; + kernel.SlabResourceCounts().num_KThread += SlabCountExtraKThread; } } -size_t CalculateTotalSlabHeapSize() { +size_t CalculateTotalSlabHeapSize(const KernelCore& kernel) { size_t size = 0; #define ADD_SLAB_SIZE(NAME, COUNT, ...) \ @@ -138,6 +134,8 @@ size_t CalculateTotalSlabHeapSize() { } void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { + auto& kernel = system.Kernel(); + // Get the start of the slab region, since that's where we'll be working. VAddr address = memory_layout.GetSlabRegionAddress(); diff --git a/src/core/hle/kernel/init/init_slab_setup.h b/src/core/hle/kernel/init/init_slab_setup.h index 6418b97ac..a8f7e0918 100644 --- a/src/core/hle/kernel/init/init_slab_setup.h +++ b/src/core/hle/kernel/init/init_slab_setup.h @@ -9,12 +9,15 @@ class System; } // namespace Core namespace Kernel { +class KernelCore; class KMemoryLayout; } // namespace Kernel namespace Kernel::Init { struct KSlabResourceCounts { + static KSlabResourceCounts CreateDefault(); + size_t num_KProcess; size_t num_KThread; size_t num_KEvent; @@ -33,10 +36,8 @@ struct KSlabResourceCounts { size_t num_KBeta; }; -void InitializeSlabResourceCounts(); -const KSlabResourceCounts& GetSlabResourceCounts(); - -size_t CalculateTotalSlabHeapSize(); +void InitializeSlabResourceCounts(KernelCore& kernel); +size_t CalculateTotalSlabHeapSize(const KernelCore& kernel); void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout); } // namespace Kernel::Init -- cgit v1.2.3