diff options
| author | 2021-05-07 23:30:17 -0700 | |
|---|---|---|
| committer | 2021-05-07 23:30:17 -0700 | |
| commit | faa067f175cbf5e916ed75776817f0046e6731c4 (patch) | |
| tree | 8ab02a72a6e4d6578848c8da2c02af02684aeec7 /src/core/hle/kernel/init | |
| parent | Merge pull request #6287 from lioncash/ldr-copy (diff) | |
| parent | hle: kernel: KPageTable: CanContain should not be constexpr. (diff) | |
| download | yuzu-faa067f175cbf5e916ed75776817f0046e6731c4.tar.gz yuzu-faa067f175cbf5e916ed75776817f0046e6731c4.tar.xz yuzu-faa067f175cbf5e916ed75776817f0046e6731c4.zip | |
Merge pull request #6266 from bunnei/kautoobject-refactor
Kernel Rework: Migrate kernel objects to KAutoObject
Diffstat (limited to 'src/core/hle/kernel/init')
| -rw-r--r-- | src/core/hle/kernel/init/init_slab_setup.cpp | 192 | ||||
| -rw-r--r-- | src/core/hle/kernel/init/init_slab_setup.h | 43 |
2 files changed, 235 insertions, 0 deletions
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..69ae405e6 --- /dev/null +++ b/src/core/hle/kernel/init/init_slab_setup.cpp | |||
| @@ -0,0 +1,192 @@ | |||
| 1 | // Copyright 2021 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/alignment.h" | ||
| 6 | #include "common/assert.h" | ||
| 7 | #include "common/common_funcs.h" | ||
| 8 | #include "common/common_types.h" | ||
| 9 | #include "core/core.h" | ||
| 10 | #include "core/hardware_properties.h" | ||
| 11 | #include "core/hle/kernel/init/init_slab_setup.h" | ||
| 12 | #include "core/hle/kernel/k_event.h" | ||
| 13 | #include "core/hle/kernel/k_memory_layout.h" | ||
| 14 | #include "core/hle/kernel/k_memory_manager.h" | ||
| 15 | #include "core/hle/kernel/k_port.h" | ||
| 16 | #include "core/hle/kernel/k_process.h" | ||
| 17 | #include "core/hle/kernel/k_resource_limit.h" | ||
| 18 | #include "core/hle/kernel/k_session.h" | ||
| 19 | #include "core/hle/kernel/k_shared_memory.h" | ||
| 20 | #include "core/hle/kernel/k_system_control.h" | ||
| 21 | #include "core/hle/kernel/k_thread.h" | ||
| 22 | #include "core/hle/kernel/k_transfer_memory.h" | ||
| 23 | #include "core/hle/kernel/memory_types.h" | ||
| 24 | #include "core/memory.h" | ||
| 25 | |||
| 26 | namespace Kernel::Init { | ||
| 27 | |||
| 28 | #define SLAB_COUNT(CLASS) kernel.SlabResourceCounts().num_##CLASS | ||
| 29 | |||
| 30 | #define FOREACH_SLAB_TYPE(HANDLER, ...) \ | ||
| 31 | HANDLER(KProcess, (SLAB_COUNT(KProcess)), ##__VA_ARGS__) \ | ||
| 32 | HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__) \ | ||
| 33 | HANDLER(KEvent, (SLAB_COUNT(KEvent)), ##__VA_ARGS__) \ | ||
| 34 | HANDLER(KPort, (SLAB_COUNT(KPort)), ##__VA_ARGS__) \ | ||
| 35 | HANDLER(KSharedMemory, (SLAB_COUNT(KSharedMemory)), ##__VA_ARGS__) \ | ||
| 36 | HANDLER(KTransferMemory, (SLAB_COUNT(KTransferMemory)), ##__VA_ARGS__) \ | ||
| 37 | HANDLER(KSession, (SLAB_COUNT(KSession)), ##__VA_ARGS__) \ | ||
| 38 | HANDLER(KResourceLimit, (SLAB_COUNT(KResourceLimit)), ##__VA_ARGS__) | ||
| 39 | |||
| 40 | namespace { | ||
| 41 | |||
| 42 | #define DEFINE_SLAB_TYPE_ENUM_MEMBER(NAME, COUNT, ...) KSlabType_##NAME, | ||
| 43 | |||
| 44 | enum KSlabType : u32 { | ||
| 45 | FOREACH_SLAB_TYPE(DEFINE_SLAB_TYPE_ENUM_MEMBER) KSlabType_Count, | ||
| 46 | }; | ||
| 47 | |||
| 48 | #undef DEFINE_SLAB_TYPE_ENUM_MEMBER | ||
| 49 | |||
| 50 | // Constexpr counts. | ||
| 51 | constexpr size_t SlabCountKProcess = 80; | ||
| 52 | constexpr size_t SlabCountKThread = 800; | ||
| 53 | constexpr size_t SlabCountKEvent = 700; | ||
| 54 | constexpr size_t SlabCountKInterruptEvent = 100; | ||
| 55 | constexpr size_t SlabCountKPort = 256 + 0x20; // Extra 0x20 ports over Nintendo for homebrew. | ||
| 56 | constexpr size_t SlabCountKSharedMemory = 80; | ||
| 57 | constexpr size_t SlabCountKTransferMemory = 200; | ||
| 58 | constexpr size_t SlabCountKCodeMemory = 10; | ||
| 59 | constexpr size_t SlabCountKDeviceAddressSpace = 300; | ||
| 60 | constexpr size_t SlabCountKSession = 933; | ||
| 61 | constexpr size_t SlabCountKLightSession = 100; | ||
| 62 | constexpr size_t SlabCountKObjectName = 7; | ||
| 63 | constexpr size_t SlabCountKResourceLimit = 5; | ||
| 64 | constexpr size_t SlabCountKDebug = Core::Hardware::NUM_CPU_CORES; | ||
| 65 | constexpr size_t SlabCountKAlpha = 1; | ||
| 66 | constexpr size_t SlabCountKBeta = 6; | ||
| 67 | |||
| 68 | constexpr size_t SlabCountExtraKThread = 160; | ||
| 69 | |||
| 70 | template <typename T> | ||
| 71 | VAddr InitializeSlabHeap(Core::System& system, KMemoryLayout& memory_layout, VAddr address, | ||
| 72 | size_t num_objects) { | ||
| 73 | const size_t size = Common::AlignUp(sizeof(T) * num_objects, alignof(void*)); | ||
| 74 | VAddr start = Common::AlignUp(address, alignof(T)); | ||
| 75 | |||
| 76 | if (size > 0) { | ||
| 77 | const KMemoryRegion* region = memory_layout.FindVirtual(start + size - 1); | ||
| 78 | ASSERT(region != nullptr); | ||
| 79 | ASSERT(region->IsDerivedFrom(KMemoryRegionType_KernelSlab)); | ||
| 80 | T::InitializeSlabHeap(system.Kernel(), system.Memory().GetKernelBuffer(start, size), size); | ||
| 81 | } | ||
| 82 | |||
| 83 | return start + size; | ||
| 84 | } | ||
| 85 | |||
| 86 | } // namespace | ||
| 87 | |||
| 88 | KSlabResourceCounts KSlabResourceCounts::CreateDefault() { | ||
| 89 | return { | ||
| 90 | .num_KProcess = SlabCountKProcess, | ||
| 91 | .num_KThread = SlabCountKThread, | ||
| 92 | .num_KEvent = SlabCountKEvent, | ||
| 93 | .num_KInterruptEvent = SlabCountKInterruptEvent, | ||
| 94 | .num_KPort = SlabCountKPort, | ||
| 95 | .num_KSharedMemory = SlabCountKSharedMemory, | ||
| 96 | .num_KTransferMemory = SlabCountKTransferMemory, | ||
| 97 | .num_KCodeMemory = SlabCountKCodeMemory, | ||
| 98 | .num_KDeviceAddressSpace = SlabCountKDeviceAddressSpace, | ||
| 99 | .num_KSession = SlabCountKSession, | ||
| 100 | .num_KLightSession = SlabCountKLightSession, | ||
| 101 | .num_KObjectName = SlabCountKObjectName, | ||
| 102 | .num_KResourceLimit = SlabCountKResourceLimit, | ||
| 103 | .num_KDebug = SlabCountKDebug, | ||
| 104 | .num_KAlpha = SlabCountKAlpha, | ||
| 105 | .num_KBeta = SlabCountKBeta, | ||
| 106 | }; | ||
| 107 | } | ||
| 108 | |||
| 109 | void InitializeSlabResourceCounts(KernelCore& kernel) { | ||
| 110 | kernel.SlabResourceCounts() = KSlabResourceCounts::CreateDefault(); | ||
| 111 | if (KSystemControl::Init::ShouldIncreaseThreadResourceLimit()) { | ||
| 112 | kernel.SlabResourceCounts().num_KThread += SlabCountExtraKThread; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | size_t CalculateTotalSlabHeapSize(const KernelCore& kernel) { | ||
| 117 | size_t size = 0; | ||
| 118 | |||
| 119 | #define ADD_SLAB_SIZE(NAME, COUNT, ...) \ | ||
| 120 | { \ | ||
| 121 | size += alignof(NAME); \ | ||
| 122 | size += Common::AlignUp(sizeof(NAME) * (COUNT), alignof(void*)); \ | ||
| 123 | }; | ||
| 124 | |||
| 125 | // Add the size required for each slab. | ||
| 126 | FOREACH_SLAB_TYPE(ADD_SLAB_SIZE) | ||
| 127 | |||
| 128 | #undef ADD_SLAB_SIZE | ||
| 129 | |||
| 130 | // Add the reserved size. | ||
| 131 | size += KernelSlabHeapGapsSize; | ||
| 132 | |||
| 133 | return size; | ||
| 134 | } | ||
| 135 | |||
| 136 | void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) { | ||
| 137 | auto& kernel = system.Kernel(); | ||
| 138 | |||
| 139 | // Get the start of the slab region, since that's where we'll be working. | ||
| 140 | VAddr address = memory_layout.GetSlabRegionAddress(); | ||
| 141 | |||
| 142 | // Initialize slab type array to be in sorted order. | ||
| 143 | std::array<KSlabType, KSlabType_Count> slab_types; | ||
| 144 | for (size_t i = 0; i < slab_types.size(); i++) { | ||
| 145 | slab_types[i] = static_cast<KSlabType>(i); | ||
| 146 | } | ||
| 147 | |||
| 148 | // N shuffles the slab type array with the following simple algorithm. | ||
| 149 | for (size_t i = 0; i < slab_types.size(); i++) { | ||
| 150 | const size_t rnd = KSystemControl::GenerateRandomRange(0, slab_types.size() - 1); | ||
| 151 | std::swap(slab_types[i], slab_types[rnd]); | ||
| 152 | } | ||
| 153 | |||
| 154 | // Create an array to represent the gaps between the slabs. | ||
| 155 | const size_t total_gap_size = KernelSlabHeapGapsSize; | ||
| 156 | std::array<size_t, slab_types.size()> slab_gaps; | ||
| 157 | for (size_t i = 0; i < slab_gaps.size(); i++) { | ||
| 158 | // Note: This is an off-by-one error from Nintendo's intention, because GenerateRandomRange | ||
| 159 | // is inclusive. However, Nintendo also has the off-by-one error, and it's "harmless", so we | ||
| 160 | // will include it ourselves. | ||
| 161 | slab_gaps[i] = KSystemControl::GenerateRandomRange(0, total_gap_size); | ||
| 162 | } | ||
| 163 | |||
| 164 | // Sort the array, so that we can treat differences between values as offsets to the starts of | ||
| 165 | // slabs. | ||
| 166 | for (size_t i = 1; i < slab_gaps.size(); i++) { | ||
| 167 | for (size_t j = i; j > 0 && slab_gaps[j - 1] > slab_gaps[j]; j--) { | ||
| 168 | std::swap(slab_gaps[j], slab_gaps[j - 1]); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | for (size_t i = 0; i < slab_types.size(); i++) { | ||
| 173 | // Add the random gap to the address. | ||
| 174 | address += (i == 0) ? slab_gaps[0] : slab_gaps[i] - slab_gaps[i - 1]; | ||
| 175 | |||
| 176 | #define INITIALIZE_SLAB_HEAP(NAME, COUNT, ...) \ | ||
| 177 | case KSlabType_##NAME: \ | ||
| 178 | address = InitializeSlabHeap<NAME>(system, memory_layout, address, COUNT); \ | ||
| 179 | break; | ||
| 180 | |||
| 181 | // Initialize the slabheap. | ||
| 182 | switch (slab_types[i]) { | ||
| 183 | // For each of the slab types, we want to initialize that heap. | ||
| 184 | FOREACH_SLAB_TYPE(INITIALIZE_SLAB_HEAP) | ||
| 185 | // If we somehow get an invalid type, abort. | ||
| 186 | default: | ||
| 187 | UNREACHABLE(); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | } // 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..a8f7e0918 --- /dev/null +++ b/src/core/hle/kernel/init/init_slab_setup.h | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | // Copyright 2021 yuzu emulator team | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | namespace Core { | ||
| 8 | class System; | ||
| 9 | } // namespace Core | ||
| 10 | |||
| 11 | namespace Kernel { | ||
| 12 | class KernelCore; | ||
| 13 | class KMemoryLayout; | ||
| 14 | } // namespace Kernel | ||
| 15 | |||
| 16 | namespace Kernel::Init { | ||
| 17 | |||
| 18 | struct KSlabResourceCounts { | ||
| 19 | static KSlabResourceCounts CreateDefault(); | ||
| 20 | |||
| 21 | size_t num_KProcess; | ||
| 22 | size_t num_KThread; | ||
| 23 | size_t num_KEvent; | ||
| 24 | size_t num_KInterruptEvent; | ||
| 25 | size_t num_KPort; | ||
| 26 | size_t num_KSharedMemory; | ||
| 27 | size_t num_KTransferMemory; | ||
| 28 | size_t num_KCodeMemory; | ||
| 29 | size_t num_KDeviceAddressSpace; | ||
| 30 | size_t num_KSession; | ||
| 31 | size_t num_KLightSession; | ||
| 32 | size_t num_KObjectName; | ||
| 33 | size_t num_KResourceLimit; | ||
| 34 | size_t num_KDebug; | ||
| 35 | size_t num_KAlpha; | ||
| 36 | size_t num_KBeta; | ||
| 37 | }; | ||
| 38 | |||
| 39 | void InitializeSlabResourceCounts(KernelCore& kernel); | ||
| 40 | size_t CalculateTotalSlabHeapSize(const KernelCore& kernel); | ||
| 41 | void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout); | ||
| 42 | |||
| 43 | } // namespace Kernel::Init | ||