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/test/test_vmm.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 na/test/test_vmm.cc (limited to 'na/test/test_vmm.cc') diff --git a/na/test/test_vmm.cc b/na/test/test_vmm.cc new file mode 100644 index 0000000..cd76140 --- /dev/null +++ b/na/test/test_vmm.cc @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +/* + * Boot Self-Test: VMM and Address Translation + */ + +#include +#include +#include +#include + +/* Assuming pmap namespace is accessible or we can test basic structures */ + +void selftest_vmm() { + st_begin("vmm: virtual/physical address translation"); + { + uint64 pa = 0x80000000ULL; + uint64 va = pa_to_va(pa); + ST_ASSERT_EQ(va, 0xFFFFFFC080000000ULL); + ST_ASSERT_EQ(va_to_pa(va), pa); + + uint64 pfn = pa_to_pfn(pa); + ST_ASSERT_EQ(pfn, 0x80000ULL); + ST_ASSERT_EQ(pfn_to_pa(pfn), pa); + + ST_ASSERT(is_kernel_va(va)); + ST_ASSERT(!is_kernel_va(pa)); + + ST_ASSERT_EQ(ensure_pa(va), pa); + ST_ASSERT_EQ(ensure_pa(pa), pa); + + ST_ASSERT_EQ(ensure_va(va), va); + ST_ASSERT_EQ(ensure_va(pa), va); + } + st_pass(); + + st_begin("vmm: page alignment macros"); + { + uint64 addr = 0x80000FFFULL; + ST_ASSERT_EQ(page_align_down(addr), 0x80000000ULL); + ST_ASSERT_EQ(page_align_up(addr), 0x80001000ULL); + ST_ASSERT_EQ(page_align_down(0x80000000ULL), 0x80000000ULL); + ST_ASSERT_EQ(page_align_up(0x80000000ULL), 0x80000000ULL); + } + st_pass(); + + st_begin("vmm: mapping kernel MMIO (driver API)"); + { + // Map 4KB of MMIO at 0x10001000 (UART is at 0x10000000) + vmm_map_kernel_mmio(0x10001000, 0x1000); + + // Verify it doesn't crash and address is reachable in higher half + uint64 va = pa_to_va(0x10001000); + ST_ASSERT(is_kernel_va(va)); + + // We can't safely dereference it here without knowing QEMU layout perfectly, + // but vmm_init should have mapped it. + } + st_pass(); +} -- cgit v1.2.3