summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/init/init_slab_setup.cpp
blob: b292f7db256c39062ea013af958886830be49ff3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// 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_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"
#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(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 {

#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 SlabCountProcess = 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_Process = SlabCountProcess,
    .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 <typename T>
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<KSlabType>(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<NAME>(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