summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle/kernel')
-rw-r--r--src/core/hle/kernel/init/init_slab_setup.cpp183
-rw-r--r--src/core/hle/kernel/init/init_slab_setup.h42
2 files changed, 225 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..a290249c7
--- /dev/null
+++ b/src/core/hle/kernel/init/init_slab_setup.cpp
@@ -0,0 +1,183 @@
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_memory_layout.h"
13#include "core/hle/kernel/k_memory_manager.h"
14#include "core/hle/kernel/k_system_control.h"
15#include "core/hle/kernel/k_thread.h"
16#include "core/hle/kernel/memory_types.h"
17#include "core/memory.h"
18
19namespace Kernel::Init {
20
21#define SLAB_COUNT(CLASS) g_slab_resource_counts.num_##CLASS
22
23#define FOREACH_SLAB_TYPE(HANDLER, ...) HANDLER(KThread, (SLAB_COUNT(KThread)), ##__VA_ARGS__)
24
25namespace {
26
27#define DEFINE_SLAB_TYPE_ENUM_MEMBER(NAME, COUNT, ...) KSlabType_##NAME,
28
29enum KSlabType : u32 {
30 FOREACH_SLAB_TYPE(DEFINE_SLAB_TYPE_ENUM_MEMBER) KSlabType_Count,
31};
32
33#undef DEFINE_SLAB_TYPE_ENUM_MEMBER
34
35// Constexpr counts.
36constexpr size_t SlabCountKProcess = 80;
37constexpr size_t SlabCountKThread = 800;
38constexpr size_t SlabCountKEvent = 700;
39constexpr size_t SlabCountKInterruptEvent = 100;
40constexpr size_t SlabCountKPort = 256 + 0x20; // Extra 0x20 ports over Nintendo for homebrew.
41constexpr size_t SlabCountKSharedMemory = 80;
42constexpr size_t SlabCountKTransferMemory = 200;
43constexpr size_t SlabCountKCodeMemory = 10;
44constexpr size_t SlabCountKDeviceAddressSpace = 300;
45constexpr size_t SlabCountKSession = 933;
46constexpr size_t SlabCountKLightSession = 100;
47constexpr size_t SlabCountKObjectName = 7;
48constexpr size_t SlabCountKResourceLimit = 5;
49constexpr size_t SlabCountKDebug = Core::Hardware::NUM_CPU_CORES;
50constexpr size_t SlabCountKAlpha = 1;
51constexpr size_t SlabCountKBeta = 6;
52
53constexpr size_t SlabCountExtraKThread = 160;
54
55// Global to hold our resource counts.
56KSlabResourceCounts g_slab_resource_counts = {
57 .num_KProcess = SlabCountKProcess,
58 .num_KThread = SlabCountKThread,
59 .num_KEvent = SlabCountKEvent,
60 .num_KInterruptEvent = SlabCountKInterruptEvent,
61 .num_KPort = SlabCountKPort,
62 .num_KSharedMemory = SlabCountKSharedMemory,
63 .num_KTransferMemory = SlabCountKTransferMemory,
64 .num_KCodeMemory = SlabCountKCodeMemory,
65 .num_KDeviceAddressSpace = SlabCountKDeviceAddressSpace,
66 .num_KSession = SlabCountKSession,
67 .num_KLightSession = SlabCountKLightSession,
68 .num_KObjectName = SlabCountKObjectName,
69 .num_KResourceLimit = SlabCountKResourceLimit,
70 .num_KDebug = SlabCountKDebug,
71 .num_KAlpha = SlabCountKAlpha,
72 .num_KBeta = SlabCountKBeta,
73};
74
75template <typename T>
76VAddr InitializeSlabHeap(Core::System& system, KMemoryLayout& memory_layout, VAddr address,
77 size_t num_objects) {
78 const size_t size = Common::AlignUp(sizeof(T) * num_objects, alignof(void*));
79 VAddr start = Common::AlignUp(address, alignof(T));
80
81 if (size > 0) {
82 const KMemoryRegion* region = memory_layout.FindVirtual(start + size - 1);
83 ASSERT(region != nullptr);
84 ASSERT(region->IsDerivedFrom(KMemoryRegionType_KernelSlab));
85 T::InitializeSlabHeap(system.Kernel(), system.Memory().GetKernelBuffer(start, size), size);
86 }
87
88 return start + size;
89}
90
91} // namespace
92
93const KSlabResourceCounts& GetSlabResourceCounts() {
94 return g_slab_resource_counts;
95}
96
97void InitializeSlabResourceCounts() {
98 // Note: Nintendo initializes all fields here, but we initialize all constants at compile-time.
99
100 if (KSystemControl::Init::ShouldIncreaseThreadResourceLimit()) {
101 g_slab_resource_counts.num_KThread += SlabCountExtraKThread;
102 }
103}
104
105size_t CalculateSlabHeapGapSize() {
106 return KernelSlabHeapGapsSize;
107}
108
109size_t CalculateTotalSlabHeapSize() {
110 size_t size = 0;
111
112#define ADD_SLAB_SIZE(NAME, COUNT, ...) \
113 { \
114 size += alignof(NAME); \
115 size += Common::AlignUp(sizeof(NAME) * (COUNT), alignof(void*)); \
116 };
117
118 // Add the size required for each slab.
119 FOREACH_SLAB_TYPE(ADD_SLAB_SIZE)
120
121#undef ADD_SLAB_SIZE
122
123 // Add the reserved size.
124 size += CalculateSlabHeapGapSize();
125
126 return size;
127}
128
129void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout) {
130 // Get the start of the slab region, since that's where we'll be working.
131 VAddr address = memory_layout.GetSlabRegionAddress();
132
133 // Initialize slab type array to be in sorted order.
134 KSlabType slab_types[KSlabType_Count];
135 for (size_t i = 0; i < Common::Size(slab_types); i++) {
136 slab_types[i] = static_cast<KSlabType>(i);
137 }
138
139 // N shuffles the slab type array with the following simple algorithm.
140 for (size_t i = 0; i < Common::Size(slab_types); i++) {
141 const size_t rnd = KSystemControl::GenerateRandomRange(0, Common::Size(slab_types) - 1);
142 std::swap(slab_types[i], slab_types[rnd]);
143 }
144
145 // Create an array to represent the gaps between the slabs.
146 const size_t total_gap_size = CalculateSlabHeapGapSize();
147 size_t slab_gaps[Common::Size(slab_types)];
148 for (size_t i = 0; i < Common::Size(slab_gaps); i++) {
149 // Note: This is an off-by-one error from Nintendo's intention, because GenerateRandomRange
150 // is inclusive. However, Nintendo also has the off-by-one error, and it's "harmless", so we
151 // will include it ourselves.
152 slab_gaps[i] = KSystemControl::GenerateRandomRange(0, total_gap_size);
153 }
154
155 // Sort the array, so that we can treat differences between values as offsets to the starts of
156 // slabs.
157 for (size_t i = 1; i < Common::Size(slab_gaps); i++) {
158 for (size_t j = i; j > 0 && slab_gaps[j - 1] > slab_gaps[j]; j--) {
159 std::swap(slab_gaps[j], slab_gaps[j - 1]);
160 }
161 }
162
163 for (size_t i = 0; i < Common::Size(slab_types); i++) {
164 // Add the random gap to the address.
165 address += (i == 0) ? slab_gaps[0] : slab_gaps[i] - slab_gaps[i - 1];
166
167#define INITIALIZE_SLAB_HEAP(NAME, COUNT, ...) \
168 case KSlabType_##NAME: \
169 address = InitializeSlabHeap<NAME>(system, memory_layout, address, COUNT); \
170 break;
171
172 // Initialize the slabheap.
173 switch (slab_types[i]) {
174 // For each of the slab types, we want to initialize that heap.
175 FOREACH_SLAB_TYPE(INITIALIZE_SLAB_HEAP)
176 // If we somehow get an invalid type, abort.
177 default:
178 UNREACHABLE();
179 }
180 }
181}
182
183} // 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 @@
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
7namespace Core {
8class System;
9} // namespace Core
10
11namespace Kernel {
12class KMemoryLayout;
13} // namespace Kernel
14
15namespace Kernel::Init {
16
17struct KSlabResourceCounts {
18 size_t num_KProcess;
19 size_t num_KThread;
20 size_t num_KEvent;
21 size_t num_KInterruptEvent;
22 size_t num_KPort;
23 size_t num_KSharedMemory;
24 size_t num_KTransferMemory;
25 size_t num_KCodeMemory;
26 size_t num_KDeviceAddressSpace;
27 size_t num_KSession;
28 size_t num_KLightSession;
29 size_t num_KObjectName;
30 size_t num_KResourceLimit;
31 size_t num_KDebug;
32 size_t num_KAlpha;
33 size_t num_KBeta;
34};
35
36void InitializeSlabResourceCounts();
37const KSlabResourceCounts& GetSlabResourceCounts();
38
39size_t CalculateTotalSlabHeapSize();
40void InitializeSlabHeaps(Core::System& system, KMemoryLayout& memory_layout);
41
42} // namespace Kernel::Init