forked from Simple-XX/SimpleKernel
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.cpp
More file actions
338 lines (273 loc) · 9.63 KB
/
main.cpp
File metadata and controls
338 lines (273 loc) · 9.63 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* @copyright Copyright The SimpleKernel Contributors
*/
#include <atomic>
#include <cstdint>
#include <new>
#include "arch.h"
#include "basic_info.hpp"
#include "kernel.h"
#include "kernel_log.hpp"
#include "kstd_cstdio"
#include "kstd_libcxx.h"
#include "kstd_memory"
#include "per_cpu.hpp"
#include "spinlock.hpp"
#include "syscall.hpp"
#include "system_test.h"
#include "task_control_block.hpp"
#include "task_manager.hpp"
namespace {
struct test_case {
const char* name;
bool (*func)(void);
bool is_smp_test = false;
};
constexpr size_t kTestCount = 27;
std::array<test_case, kTestCount> test_cases = {
test_case{"ctor_dtor_test", ctor_dtor_test, false},
test_case{"spinlock_test", spinlock_test, true},
test_case{"memory_test", memory_test, false},
test_case{"virtual_memory_test", virtual_memory_test, false},
test_case{"interrupt_test", interrupt_test, false},
test_case{"kernel_task_test", kernel_task_test, false},
test_case{"user_task_test", user_task_test, false},
test_case{"fifo_scheduler_test", fifo_scheduler_test, false},
test_case{"rr_scheduler_test", rr_scheduler_test, false},
test_case{"cfs_scheduler_test", cfs_scheduler_test, false},
test_case{"idle_scheduler_test", idle_scheduler_test, false},
test_case{"thread_group_test", thread_group_test, false},
test_case{"wait_test", wait_test, false},
test_case{"clone_test", clone_test, false},
test_case{"exit_test", exit_test, false},
test_case{"cross_core_test", cross_core_test, false},
test_case{"balance_test", balance_test, false},
test_case{"mutex_test", mutex_test, false},
test_case{"yield_test", yield_test, false},
test_case{"fork_test", fork_test, false},
test_case{"signal_test", signal_test, false},
test_case{"affinity_test", affinity_test, false},
test_case{"tick_test", tick_test, false},
test_case{"zombie_reap_test", zombie_reap_test, false},
test_case{"stress_test", stress_test, false},
test_case{"ramfs_test", ramfs_test, false},
test_case{"fatfs_test", fatfs_test, false},
};
std::array<TestResult, kTestCount> test_results{};
std::atomic<size_t> g_cores_ready{0};
void test_thread_entry(void* arg) {
auto idx = reinterpret_cast<uint64_t>(arg);
auto& test = test_cases[idx];
klog::Info("[TEST] Starting: {}", test.name);
bool passed = test.func();
klog::Info("[TEST] Finished: {} — {}", test.name, passed ? "PASS" : "FAIL");
sys_exit(passed ? 0 : 1);
}
void print_test_summary() {
int passed = 0;
int failed = 0;
int timed_out = 0;
klog::Info("========================================");
klog::Info(" System Test Results");
klog::Info("========================================");
for (size_t i = 0; i < kTestCount; ++i) {
auto& r = test_results[i];
const char* tag = "???";
switch (r.status) {
case TestThreadStatus::kPassed:
tag = "PASS";
passed++;
break;
case TestThreadStatus::kFailed:
tag = "FAIL";
failed++;
break;
case TestThreadStatus::kTimeout:
tag = "TIMEOUT";
timed_out++;
break;
case TestThreadStatus::kRunning:
tag = "TIMEOUT";
timed_out++;
break;
default:
tag = "SKIP";
break;
}
if (test_cases[i].is_smp_test) {
klog::Info(" [{}] {} (SMP, exit_code={})", tag, r.name, r.exit_code);
} else {
klog::Info(" [{}] {} (pid={}, exit_code={})", tag, r.name, r.pid,
r.exit_code);
}
}
int total = static_cast<int>(kTestCount);
klog::Info("========================================");
klog::Info(" Total: {} | Passed: {} | Failed: {} | Timeout: {}", total,
passed, failed, timed_out);
klog::Info("========================================");
}
void test_runner_entry(void* /*arg*/) {
auto& task_mgr = TaskManagerSingleton::instance();
auto* runner = task_mgr.GetCurrentTask();
// Phase 2: 为每个非 SMP 测试创建独立线程,建立父子关系
int thread_test_count = 0;
for (size_t i = 0; i < kTestCount; ++i) {
if (test_cases[i].is_smp_test) {
continue;
}
auto task = kstd::make_unique<TaskControlBlock>(
test_cases[i].name, 10, test_thread_entry, reinterpret_cast<void*>(i));
// Wait() requires parent-child relationship to collect exit status
task->aux->parent_pid = runner->pid;
task->aux->pgid = runner->aux->pgid;
// Save raw pointer: pid is assigned inside AddTask() by AllocatePid(),
// so we must read it *after* the call (unique_ptr is moved).
auto* task_ptr = task.get();
test_results[i].status = TestThreadStatus::kRunning;
task_mgr.AddTask(std::move(task));
test_results[i].pid = task_ptr->pid;
thread_test_count++;
}
klog::Info("[RUNNER] Spawned {} test threads, collecting via Wait()...",
thread_test_count);
// Phase 3: 通过 Wait() 收集所有测试线程的退出状态
int collected = 0;
constexpr int kMaxWaitRetries = 1200; // 1200 * 50ms = 60s 超时
int retries = 0;
while (collected < thread_test_count && retries < kMaxWaitRetries) {
int status = 0;
auto wait_result =
task_mgr.Wait(static_cast<Pid>(-1), &status, true, false);
if (wait_result.has_value() && wait_result.value() > 0) {
Pid exited_pid = wait_result.value();
bool is_test_thread = false;
for (size_t i = 0; i < kTestCount; ++i) {
if (test_results[i].pid == static_cast<int64_t>(exited_pid)) {
test_results[i].exit_code = status;
test_results[i].status = (status == 0) ? TestThreadStatus::kPassed
: TestThreadStatus::kFailed;
klog::Info("[RUNNER] Collected: {} (pid={}, exit_code={}) — {}",
test_cases[i].name, exited_pid, status,
(status == 0) ? "PASS" : "FAIL");
is_test_thread = true;
break;
}
}
if (is_test_thread) {
collected++;
} else {
klog::Debug("[RUNNER] Reaped orphan pid={}, not a test thread",
exited_pid);
}
} else {
(void)sys_sleep(50);
retries++;
}
}
for (size_t i = 0; i < kTestCount; ++i) {
if (test_cases[i].is_smp_test) {
continue;
}
if (test_results[i].status == TestThreadStatus::kPending ||
test_results[i].status == TestThreadStatus::kRunning) {
test_results[i].status = TestThreadStatus::kTimeout;
klog::Err("[RUNNER] Timeout: {} (pid={})", test_cases[i].name,
test_results[i].pid);
}
}
// Phase 4: 汇总打印
print_test_summary();
bool all_passed = true;
for (size_t i = 0; i < kTestCount; ++i) {
if (test_results[i].status != TestThreadStatus::kPassed) {
all_passed = false;
break;
}
}
if (all_passed) {
klog::Info("RESULT: ALL TESTS PASSED");
}
QemuExit(all_passed);
}
void run_tests_smp() {
for (const auto& test : test_cases) {
if (test.is_smp_test) {
test.func();
}
}
}
auto main_smp(int argc, const char** argv) -> int {
per_cpu::GetCurrentCore() = per_cpu::PerCpu(cpu_io::GetCurrentCoreId());
ArchInitSMP(argc, argv);
MemoryInitSMP();
InterruptInitSMP(argc, argv);
TaskManagerSingleton::instance().InitCurrentCore();
TimerInitSMP();
klog::Info("Hello SimpleKernel SMP");
g_cores_ready.fetch_add(1, std::memory_order_release);
run_tests_smp();
TaskManagerSingleton::instance().Schedule();
__builtin_unreachable();
}
} // namespace
std::atomic_flag primary_booted_ = ATOMIC_FLAG_INIT;
auto _start(int argc, const char** argv) -> void {
if (!primary_booted_.test_and_set(std::memory_order_acquire)) {
CppInit();
main(argc, argv);
} else {
main_smp(argc, argv);
}
while (true) {
cpu_io::Pause();
}
}
auto main(int argc, const char** argv) -> int {
per_cpu::PerCpuArraySingleton::create();
per_cpu::GetCurrentCore() = per_cpu::PerCpu(cpu_io::GetCurrentCoreId());
ArchInit(argc, argv);
MemoryInit();
InterruptInit(argc, argv);
DeviceInit();
FileSystemInit();
TaskManagerSingleton::create();
TaskManagerSingleton::instance().InitCurrentCore();
TimerInit();
WakeUpOtherCores();
DumpStack();
klog::Info("Hello SimpleKernel");
for (size_t i = 0; i < kTestCount; ++i) {
test_results[i].name = test_cases[i].name;
test_results[i].status = TestThreadStatus::kPending;
test_results[i].exit_code = -1;
test_results[i].pid = 0;
}
// Phase 1: SMP 测试同步运行(需要跨核屏障协调,必须在调度器启动前完成)
size_t expected_cores = BasicInfoSingleton::instance().core_count - 1;
klog::Info("[RUNNER] Waiting for {} secondary core(s) to initialize...",
expected_cores);
while (g_cores_ready.load(std::memory_order_acquire) < expected_cores) {
cpu_io::Pause();
}
klog::Info("[RUNNER] All cores ready, starting SMP tests");
for (size_t i = 0; i < kTestCount; ++i) {
if (!test_cases[i].is_smp_test) {
continue;
}
auto& result = test_results[i];
result.status = TestThreadStatus::kRunning;
klog::Info("[SMP] Running: {}", test_cases[i].name);
bool smp_passed = test_cases[i].func();
result.exit_code = smp_passed ? 0 : 1;
result.status =
smp_passed ? TestThreadStatus::kPassed : TestThreadStatus::kFailed;
klog::Info("[SMP] Finished: {} — {}", test_cases[i].name,
smp_passed ? "PASS" : "FAIL");
}
auto runner = kstd::make_unique<TaskControlBlock>("test_runner", 10,
test_runner_entry, nullptr);
TaskManagerSingleton::instance().AddTask(std::move(runner));
TaskManagerSingleton::instance().Schedule();
__builtin_unreachable();
}