From 51899557c770b3dc9bd02579abf491745e607d92 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Tue, 21 Apr 2026 23:23:49 +0800 Subject: feat: add boot self-tests for various kernel components - Implemented a self-test runner in `selftest.cc` to invoke all test suites. - Added tests for atomic operations in `test_atomic.cc`, covering construction, store/load, exchange, arithmetic, and compare-and-swap. - Created tests for the Flattened Device Tree (FDT) in `test_fdt.cc`, verifying node access and compatibility queries. - Developed a comprehensive Physical Memory Manager (PMM) test suite in `test_pmm.cc`, including allocation, deallocation, reference counting, and boundary conditions. - Implemented Red-Black Tree tests in `test_rbtree.cc`, validating insertion, deletion, and iteration. - Added Slab Allocator tests in `test_slab.cc`, covering various allocation sizes, edge cases, and operator overloads. - Created Spinlock tests in `test_spinlock.cc`, ensuring proper locking behavior and interrupt state preservation. - Developed Virtual Memory Area (VMA) tests in `test_vma.cc`, validating region mapping, unmapping, and fault handling. - Added tests for Virtual Memory Management (VMM) in `test_vmm.cc`, verifying address translation and page alignment. --- na/include/lume/selftest.h | 110 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 na/include/lume/selftest.h (limited to 'na/include') diff --git a/na/include/lume/selftest.h b/na/include/lume/selftest.h new file mode 100644 index 0000000..f5beffd --- /dev/null +++ b/na/include/lume/selftest.h @@ -0,0 +1,110 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +#pragma once + +/* + * Boot Self-Test Framework + * + * Minimal assertion-based test harness that runs during kernel_main() + * after all subsystems are initialized. Each test is a void function + * that calls ST_ASSERT / ST_ASSERT_EQ macros. On failure, prints + * the failing expression and panics. + * + * Usage: + * void selftest_pmm(); // defined in test/test_pmm.cc + * void selftest_slab(); // defined in test/test_slab.cc + * + * All self-tests run only on BSP in single-core mode. + */ + +#include +#include + +extern "C" void early_puts(const char* s); + +/* Print a decimal number (minimal, no printf available) */ +inline void st_print_num(uint64 n) +{ + if (n == 0) { early_puts("0"); return; } + char buf[21]; + int i = 20; + buf[i] = '\0'; + while (n > 0 && i > 0) { + buf[--i] = '0' + static_cast(n % 10); + n /= 10; + } + early_puts(&buf[i]); +} + +inline void st_print_hex(uint64 n) +{ + early_puts("0x"); + char buf[17]; + int i = 16; + buf[i] = '\0'; + if (n == 0) { early_puts("0"); return; } + while (n > 0 && i > 0) { + int d = static_cast(n & 0xF); + buf[--i] = (d < 10) ? ('0' + d) : ('a' + d - 10); + n >>= 4; + } + early_puts(&buf[i]); +} + +#define ST_ASSERT(cond) \ + do { \ + if (!(cond)) { \ + early_puts("[FAIL] "); \ + early_puts(__FILE__); \ + early_puts(":"); \ + st_print_num(__LINE__); \ + early_puts(": assertion failed: " #cond "\n"); \ + kernel_panic("selftest failed", #cond); \ + } \ + } while (0) + +#define ST_ASSERT_EQ(a, b) \ + do { \ + auto _a = (a); auto _b = (b); \ + if (_a != _b) { \ + early_puts("[FAIL] "); \ + early_puts(__FILE__); \ + early_puts(":"); \ + st_print_num(__LINE__); \ + early_puts(": " #a " != " #b " ("); \ + st_print_hex((uint64)(uintptr)(_a)); \ + early_puts(" != "); \ + st_print_hex((uint64)(uintptr)(_b)); \ + early_puts(")\n"); \ + kernel_panic("selftest failed", #a " != " #b); \ + } \ + } while (0) + +#define ST_ASSERT_NE(a, b) \ + do { \ + auto _a = (a); auto _b = (b); \ + if (_a == _b) { \ + early_puts("[FAIL] "); \ + early_puts(__FILE__); \ + early_puts(":"); \ + st_print_num(__LINE__); \ + early_puts(": " #a " == " #b " (both "); \ + st_print_hex((uint64)(uintptr)(_a)); \ + early_puts(")\n"); \ + kernel_panic("selftest failed", #a " == " #b); \ + } \ + } while (0) + +/* Announce a test suite beginning/end */ +inline void st_begin(const char* name) +{ + early_puts("[test] "); + early_puts(name); + early_puts(" ... "); +} + +inline void st_pass() +{ + early_puts("PASS\n"); +} -- cgit v1.2.3