Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 28 additions & 105 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,96 +55,22 @@ HOST_CXX ?= g++
HOST_BUILD_DIR := $(BUILD_DIR)/host-tests
HOST_COV_DIR := $(BUILD_DIR)/host-coverage

HOST_CXXFLAGS := -std=c++20 -O1 -g -Wall -Wextra -Werror -Wpedantic \
-fno-exceptions -fno-rtti -DHOST_TEST -DSNITCH_WITH_EXCEPTIONS=0 \
-Wshadow -Wconversion -Wsign-conversion -Wnull-dereference \
-fsanitize=address,undefined -fno-omit-frame-pointer -MMD -MP
HOST_LDFLAGS := -fsanitize=address,undefined -L/usr/local/lib -lsnitch
HOST_COMMON_CXXFLAGS := -std=c++20 -g -Wall -Wextra -Werror -Wpedantic \
-Wshadow -Wconversion -Wsign-conversion -Wnull-dereference \
-fno-exceptions -fno-rtti -DHOST_TEST -DSNITCH_WITH_EXCEPTIONS=0 \
-MMD -MP
HOST_COMMON_LDFLAGS := -L/usr/local/lib -lsnitch

HOST_CXXFLAGS := $(HOST_COMMON_CXXFLAGS) -O1 -fsanitize=address,undefined -fno-omit-frame-pointer
HOST_LDFLAGS := -fsanitize=address,undefined $(HOST_COMMON_LDFLAGS)
HOST_INCLUDES := -Iinclude -Ikernel -Iarch/$(ARCH) -Itests/host

HOST_COV_CXXFLAGS := -std=c++20 -O0 -g --coverage -Wall -Wextra -Werror -Wpedantic \
-Wshadow -Wconversion -Wsign-conversion -Wnull-dereference \
-fno-exceptions -fno-rtti -DHOST_TEST -DSNITCH_WITH_EXCEPTIONS=0 -MMD -MP
HOST_COV_LDFLAGS := --coverage -L/usr/local/lib -lsnitch

HOST_ONLY_TEST_SRCS := \
tests/host/mocks/host_hal.cpp \
tests/host/af_unix_test.cpp \
tests/host/distro_abi_test.cpp \
tests/host/ddk_dummy_test.cpp \
tests/host/ddk_kernel_services_test.cpp \
tests/host/file_syscalls_test.cpp \
tests/host/syscall_helpers_test.cpp \
tests/host/process_subsystem_test.cpp \
tests/host/cpio_duration_test.cpp \
tests/host/virtio_blk_test.cpp \
drivers/dummy/DummyDriver.cpp \
tests/host/socket_syscall_test.cpp \
tests/host/gicv2_test.cpp \
tests/host/pl011_uart_test.cpp \
tests/host/page_cache_test.cpp \
tests/host/vma_test.cpp \
tests/host/elf_interp_test.cpp \
tests/host/evdev_test.cpp \
tests/host/ext2_test.cpp \
tests/host/fb0_devfs_test.cpp \
tests/host/pfa_test.cpp \
tests/host/vfs_test.cpp \
tests/host/rootfs_test.cpp \
tests/host/tmpfs_test.cpp \
tests/host/stack_builder_test.cpp \
tests/host/fdt_test.cpp \
tests/host/log_test.cpp \
tests/host/fw_cfg_test.cpp \
tests/host/ramfb_test.cpp \
tests/host/virtio_ring_test.cpp \
tests/host/virtio_bus_test.cpp \
tests/host/virtio_gpu_test.cpp \
tests/host/virtio_input_test.cpp \
tests/host/virtio_net_test.cpp

HOST_KERNEL_SRCS := \
kernel/core/log.cpp \
kernel/ddk/KernelPlatformServices.cpp \
kernel/boot/fdt.cpp \
kernel/drivers/irq/gicv2.cpp \
kernel/drivers/uart/pl011.cpp \
kernel/drivers/virtio/SplitVirtqueue.cpp \
kernel/drivers/virtio/VirtioMmioTransport.cpp \
kernel/drivers/virtio/VirtioMmioBus.cpp \
kernel/drivers/virtio/virtio_blk.cpp \
kernel/drivers/virtio/virtio_gpu.cpp \
kernel/drivers/virtio/virtio_input.cpp \
kernel/drivers/virtio/virtio_net.cpp \
kernel/drivers/firmware/fw_cfg.cpp \
kernel/drivers/video/framebuffer.cpp \
kernel/drivers/video/ramfb.cpp \
kernel/fs/cpio.cpp \
kernel/fs/PageCache.cpp \
kernel/fs/ext2/ext2.cpp \
kernel/fs/Vfs.cpp \
kernel/fs/rootfs/rootfs.cpp \
kernel/fs/devfs/devfs.cpp \
kernel/fs/tmpfs/tmpfs.cpp \
kernel/input/InputQueue.cpp \
kernel/ipc/Pipe.cpp \
kernel/net/af_unix.cpp \
kernel/loader/elf64.cpp \
kernel/loader/elf_loader.cpp \
kernel/mm/pfa.cpp \
kernel/mm/heap.cpp \
kernel/mm/AddressSpace.cpp \
kernel/process/FdTable.cpp \
kernel/process/Signal.cpp \
kernel/process/Process.cpp \
kernel/process/resource.cpp \
kernel/process/scheduler.cpp \
kernel/process/stack_builder.cpp \
kernel/process/wait_queue.cpp \
kernel/syscalls/FileSyscalls.cpp \
kernel/syscalls/SocketSyscalls.cpp \
kernel/syscalls/MemorySyscalls.cpp \
kernel/syscalls/SyscallHelpers.cpp
HOST_COV_CXXFLAGS := $(HOST_COMMON_CXXFLAGS) -O0 --coverage
HOST_COV_LDFLAGS := --coverage $(HOST_COMMON_LDFLAGS)

HOST_TEST_CASE_SRCS := $(sort $(wildcard tests/host/*.cpp) $(wildcard tests/host/mocks/*.cpp))
HOST_ONLY_TEST_SRCS := $(HOST_TEST_CASE_SRCS) $(DRIVER_SRCS)
HOST_KERNEL_SRCS := $(KERNEL_CORE_SRCS)

HOST_TEST_SRCS := $(HOST_ONLY_TEST_SRCS) $(HOST_KERNEL_SRCS)
HOST_ONLY_TEST_OBJS := $(patsubst %.cpp, $(HOST_BUILD_DIR)/%.o, $(HOST_ONLY_TEST_SRCS))
Expand Down Expand Up @@ -195,23 +121,20 @@ $(BUILD_DIR)/%.o: %.s
@mkdir -p $(dir $@)
$(AS) $(ASFLAGS) $< -o $@

# --- Host Unit Tests (snitch) ---
$(HOST_BUILD_DIR)/%.o: %.cpp arch/memory_config.hpp
@mkdir -p $(dir $@)
$(HOST_CXX) $(HOST_CXXFLAGS) $(HOST_INCLUDES) -c $< -o $@

$(HOST_TEST_BIN): $(HOST_TEST_OBJS)
@mkdir -p $(dir $@)
$(HOST_CXX) $^ $(HOST_LDFLAGS) -o $@
# --- Host Unit Tests (snitch) & Coverage (gcov) ---
# Arguments are variable names to avoid splitting flag lists that contain commas.
define define_host_variant
$($(1))/%.o: %.cpp arch/memory_config.hpp
@mkdir -p $$(dir $$@)
$$(HOST_CXX) $$($(4)) $$(HOST_INCLUDES) -c $$< -o $$@

# --- Host Test Coverage (gcov instrumentation) ---
$(HOST_COV_DIR)/%.o: %.cpp arch/memory_config.hpp
@mkdir -p $(dir $@)
$(HOST_CXX) $(HOST_COV_CXXFLAGS) $(HOST_INCLUDES) -c $< -o $@
$($(2)): $($(3))
@mkdir -p $$(dir $$@)
$$(HOST_CXX) $$^ $$($(5)) -o $$@
endef

$(HOST_COV_BIN): $(HOST_COV_OBJS)
@mkdir -p $(dir $@)
$(HOST_CXX) $^ $(HOST_COV_LDFLAGS) -o $@
$(eval $(call define_host_variant,HOST_BUILD_DIR,HOST_TEST_BIN,HOST_TEST_OBJS,HOST_CXXFLAGS,HOST_LDFLAGS))
$(eval $(call define_host_variant,HOST_COV_DIR,HOST_COV_BIN,HOST_COV_OBJS,HOST_COV_CXXFLAGS,HOST_COV_LDFLAGS))

# --- Unified Test Execution & Reporting ---
test-host: $(HOST_TEST_BIN) $(HOST_COV_BIN)
Expand Down Expand Up @@ -475,10 +398,10 @@ gdb: $(BUILD_DIR)/kernel.elf

# Find all project source/header files for formatting/linting targets
HDRS := $(shell find include kernel arch tests drivers -name '*.hpp' -o -name '*.h' 2>/dev/null)
TEST_FORMAT_SRCS := $(shell find tests -name '*.cpp' 2>/dev/null)
TEST_FORMAT_SRCS := $(HOST_TEST_CASE_SRCS)

# --- Compilation Database Generation (incremental, without clean rebuilds) ---
compile_commands.json: Makefile kernel/kernel.mk arch/$(ARCH)/arch.mk arch/memory_config.hpp scripts/generate_compile_commands.py
compile_commands.json: Makefile kernel/kernel.mk arch/$(ARCH)/arch.mk arch/memory_config.hpp scripts/generate_compile_commands.py tests/host tests/host/mocks kernel/shell/tests
@echo "[COMPILE-DB] Updating compile_commands.json..."
@python3 scripts/generate_compile_commands.py \
--cxx "$(CXX)" \
Expand Down
4 changes: 2 additions & 2 deletions docs/07-contributing-and-debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ This guide covers debugging **Milo** in QEMU and GDB, interpreting faults and pa

1. **Implement the driver (`kernel/drivers/...` or `kernel/input/`):** Follow freestanding C++20 rules (no exceptions/RTTI, `IrqGuard` around shared/IRQ state, `phys_to_virt` / `virt_to_phys` for MMIO and PFA DMA frames, `flush_dcache_range` around DMA buffers, and `GicV2::register_spi_handler` / `unregister_spi_handler` for SPIs).
2. **Wire into platform & boot (`kernel/hal/targets/qemu_virt/platform_*.cpp`, `kernel/fs/devfs/devfs.cpp`, `kernel/main.cpp`):** Bind via `VirtioMmioBus::instance().bind_device` / `bind_all` (or FDT helpers in `kernel/boot/fdt.cpp`), expose VFS/devfs interfaces where appropriate, and call initialization during `kmain` bring-up.
3. **Register build sources:** Add `.cpp` files to `SRCS_CXX` in `kernel/kernel.mk` and to `HOST_KERNEL_SRCS` in `Makefile` if the driver runs in `HOST_TEST`.
3. **Register build sources:** Add `.cpp` files to `KERNEL_CORE_SRCS` in `kernel/kernel.mk` if the driver runs in `HOST_TEST` (automatically mirrored into `HOST_KERNEL_SRCS`), or to `KERNEL_TARGET_SRCS` if it is bare-metal/target-only.

### Adding a Host Unit Test (`docs/adr/0001-unit-testing-framework.md`, `Makefile`)

1. **Create `tests/host/<feature>_test.cpp`:** Include `<snitch/snitch.hpp>` (linked against the pre-installed `-L/usr/local/lib -lsnitch` library with `-DSNITCH_WITH_EXCEPTIONS=0`), use `HostTest::reset_host_hal()` to reset simulated RAM/roots/mappings/console, and assert that `pfa_free_pages_count()` and heap stats return to baseline after teardown.
2. **Add to `HOST_ONLY_TEST_SRCS` in `Makefile`:** Ensure the new test file is compiled into both `$(HOST_TEST_BIN)` and `$(HOST_COV_BIN)`.
2. **Verify auto-discovery:** `Makefile` automatically discovers `tests/host/*.cpp` (and `tests/host/mocks/*.cpp`) via `HOST_TEST_CASE_SRCS` / `HOST_ONLY_TEST_SRCS`, wiring new tests into `$(HOST_TEST_BIN)`, `$(HOST_COV_BIN)`, `TEST_FORMAT_SRCS`, and `compile_commands.json` without manual edits.
3. **Format, lint & run `make quality`:** Run `make compile_commands.json`, `make format`, and `make -j"$(nproc)" quality`.
2 changes: 1 addition & 1 deletion docs/08-testing-and-ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Host unit tests execute hardware-independent kernel code directly on the host us
### Host Test Suites & HAL Mocks (`tests/host/`)

* **HAL & Memory Mock (`tests/host/mocks/host_hal.{hpp,cpp}`):** Provides a `16 MiB` simulated physical RAM arena (`HostTest::SIM_RAM_BASE = 0x40000000ULL`, `HostTest::SIM_RAM_SIZE = 16 MiB`, `HostTest::SIM_PAGE_SIZE = 4096U`), mock `Console` (`HostTest::get_captured_console_output()`), mock `CpuControl`, mock `MemoryControl`, mock `AddressSpaceBackend` (supporting roots, cloning, `PageFlags::Shared`, CoW, and failure injection via `HostTest::set_fail_create_root` and `HostTest::set_fail_map_after`), and `HostTest::reset_host_hal()`.
* **Test Suites in `HOST_ONLY_TEST_SRCS` (`Makefile`):** Cover `af_unix`, `socket_syscall`, `gicv2`, `pl011_uart`, `page_cache`, `vma`, `elf_interp`, `evdev`, `ext2`, `fb0_devfs`, `pfa`, `vfs`, `rootfs`, `tmpfs`, `stack_builder`, `fdt`, `fw_cfg`, `ramfb`, `virtio_ring`, `virtio_bus`, `virtio_gpu`, `virtio_input`, and `virtio_net` (`tests/host/virtio_blk_test.cpp` also exists in `tests/host/` and is not yet wired into `HOST_ONLY_TEST_SRCS` `[WIP]`).
* **Auto-Discovered Test Suites (`HOST_ONLY_TEST_SRCS` in `Makefile`):** Automatically discovers all `tests/host/*.cpp` and `tests/host/mocks/*.cpp` files (plus `DRIVER_SRCS`), covering `af_unix`, `cpio_duration`, `ddk_dummy`, `ddk_kernel_services`, `distro_abi`, `elf_interp`, `evdev`, `ext2`, `fb0_devfs`, `fdt`, `file_syscalls`, `fw_cfg`, `gicv2`, `log`, `page_cache`, `pfa`, `pl011_uart`, `process_subsystem`, `ramfb`, `rootfs`, `socket_syscall`, `stack_builder`, `syscall_helpers`, `tmpfs`, `vfs`, `virtio_blk`, `virtio_bus`, `virtio_gpu`, `virtio_input`, `virtio_net`, `virtio_ring`, and `vma`. `HOST_KERNEL_SRCS` is derived directly from `KERNEL_CORE_SRCS` in `kernel/kernel.mk`.
* **Host-Specific `.clang-tidy` (`tests/host/.clang-tidy`):** Configures `clang-tidy` rules for `tests/host/.*` with `-fno-exceptions -fno-rtti -std=c++20`.

## Quality Gates & Static Analysis (`Makefile`, `scripts/`)
Expand Down
167 changes: 66 additions & 101 deletions kernel/kernel.mk
Original file line number Diff line number Diff line change
@@ -1,104 +1,69 @@
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (c) 2026 Marco Buschini <marco.buschini@gmail.com>

SRCS_CXX += kernel/main.cpp \
kernel/core/log.cpp \
kernel/ddk/KernelPlatformServices.cpp \
kernel/runtime/cxx_abi.cpp \
kernel/syscalls/SyscallDispatcher.cpp \
kernel/syscalls/FileSyscalls.cpp \
kernel/syscalls/SocketSyscalls.cpp \
kernel/syscalls/SyscallHelpers.cpp \
kernel/syscalls/MemorySyscalls.cpp \
kernel/syscalls/TimeSyscalls.cpp \
kernel/syscalls/SignalSyscalls.cpp \
kernel/syscalls/ProcessSyscalls.cpp \
arch/$(ARCH)/cpu.cpp \
kernel/shell/shell.cpp \
kernel/shell/tests/test_common.cpp \
kernel/shell/tests/test_virtio.cpp \
kernel/shell/tests/test_gpu.cpp \
kernel/shell/tests/test_fb.cpp \
kernel/shell/tests/test_input.cpp \
kernel/shell/tests/test_net.cpp \
kernel/shell/tests/test_blk.cpp \
kernel/shell/tests/test_vmm.cpp \
kernel/shell/tests/test_vma_mmap.cpp \
kernel/shell/tests/test_file_vma.cpp \
kernel/shell/tests/test_page_cache.cpp \
kernel/shell/tests/test_loader_pipeline.cpp \
kernel/shell/tests/test_elf_interp.cpp \
kernel/shell/tests/test_dyn_exec.cpp \
kernel/shell/tests/test_user_isolation.cpp \
kernel/shell/tests/test_elf.cpp \
kernel/shell/tests/test_cpu.cpp \
kernel/shell/tests/test_mmu.cpp \
kernel/shell/tests/test_el0.cpp \
kernel/shell/tests/test_cpp.cpp \
kernel/shell/tests/test_svc_trap.cpp \
kernel/shell/tests/test_dynamic_io.cpp \
kernel/shell/tests/test_data_abort.cpp \
kernel/shell/tests/test_pfa.cpp \
kernel/shell/tests/test_ro.cpp \
kernel/shell/tests/test_nx.cpp \
kernel/shell/tests/test_sys.cpp \
kernel/shell/tests/test_cpio.cpp \
kernel/shell/tests/test_stack_and_abi.cpp \
kernel/shell/tests/test_user_echo.cpp \
kernel/shell/tests/test_uxn_fault.cpp \
kernel/shell/tests/test_syscall.cpp \
kernel/shell/tests/test_tty_echo.cpp \
kernel/shell/tests/test_user_mm.cpp \
kernel/shell/tests/test_proc_exit.cpp \
kernel/shell/tests/test_fork_exec.cpp \
kernel/shell/tests/test_musl.cpp \
kernel/shell/tests/test_sched.cpp \
kernel/shell/tests/test_heap.cpp \
kernel/shell/tests/test_concurrent_refcount.cpp \
kernel/shell/tests/test_cow_isolation.cpp \
kernel/shell/tests/test_demand_paging.cpp \
kernel/shell/tests/test_lazy_mm_suite.cpp \
kernel/shell/tests/test_af_unix.cpp \
kernel/shell/tests/test_unix_socket.cpp \
kernel/shell/tests/test_distro_abi.cpp \
kernel/shell/tests/test_ddk.cpp \
kernel/mm/pfa.cpp \
kernel/mm/AddressSpace.cpp \
kernel/boot/fdt.cpp \
kernel/fs/cpio.cpp \
kernel/fs/ext2/ext2.cpp \
kernel/fs/PageCache.cpp \
kernel/fs/Vfs.cpp \
kernel/fs/rootfs/rootfs.cpp \
kernel/fs/devfs/devfs.cpp \
kernel/fs/tmpfs/tmpfs.cpp \
kernel/ipc/Pipe.cpp \
kernel/net/af_unix.cpp \
kernel/loader/elf64.cpp \
kernel/loader/elf_loader.cpp \
kernel/process/stack_builder.cpp \
kernel/process/FdTable.cpp \
kernel/process/Signal.cpp \
kernel/process/Process.cpp \
kernel/process/resource.cpp \
kernel/process/scheduler.cpp \
kernel/process/wait_queue.cpp \
kernel/drivers/irq/gicv2.cpp \
kernel/drivers/uart/pl011.cpp \
kernel/drivers/virtio/SplitVirtqueue.cpp \
kernel/drivers/virtio/VirtioMmioTransport.cpp \
kernel/drivers/virtio/VirtioMmioBus.cpp \
kernel/drivers/virtio/virtio_blk.cpp \
kernel/drivers/virtio/virtio_gpu.cpp \
kernel/drivers/virtio/virtio_input.cpp \
kernel/drivers/virtio/virtio_net.cpp \
kernel/drivers/firmware/fw_cfg.cpp \
kernel/drivers/video/framebuffer.cpp \
kernel/drivers/video/ramfb.cpp \
kernel/hal/targets/qemu_virt/platform_block.cpp \
kernel/hal/targets/qemu_virt/platform_board.cpp \
kernel/hal/targets/qemu_virt/platform_gpu.cpp \
kernel/hal/targets/qemu_virt/platform_input.cpp \
kernel/hal/targets/qemu_virt/platform_net.cpp \
kernel/input/InputQueue.cpp \
kernel/mm/heap.cpp
# Portable kernel subsystems shared between the AArch64 kernel and host unit tests
KERNEL_CORE_SRCS := \
kernel/boot/fdt.cpp \
kernel/core/log.cpp \
kernel/ddk/KernelPlatformServices.cpp \
kernel/drivers/firmware/fw_cfg.cpp \
kernel/drivers/irq/gicv2.cpp \
kernel/drivers/uart/pl011.cpp \
kernel/drivers/video/framebuffer.cpp \
kernel/drivers/video/ramfb.cpp \
kernel/drivers/virtio/SplitVirtqueue.cpp \
kernel/drivers/virtio/VirtioMmioBus.cpp \
kernel/drivers/virtio/VirtioMmioTransport.cpp \
kernel/drivers/virtio/virtio_blk.cpp \
kernel/drivers/virtio/virtio_gpu.cpp \
kernel/drivers/virtio/virtio_input.cpp \
kernel/drivers/virtio/virtio_net.cpp \
kernel/fs/PageCache.cpp \
kernel/fs/Vfs.cpp \
kernel/fs/cpio.cpp \
kernel/fs/devfs/devfs.cpp \
kernel/fs/ext2/ext2.cpp \
kernel/fs/rootfs/rootfs.cpp \
kernel/fs/tmpfs/tmpfs.cpp \
kernel/input/InputQueue.cpp \
kernel/ipc/Pipe.cpp \
kernel/loader/elf64.cpp \
kernel/loader/elf_loader.cpp \
kernel/mm/AddressSpace.cpp \
kernel/mm/heap.cpp \
kernel/mm/pfa.cpp \
kernel/net/af_unix.cpp \
kernel/process/FdTable.cpp \
kernel/process/Process.cpp \
kernel/process/Signal.cpp \
kernel/process/resource.cpp \
kernel/process/scheduler.cpp \
kernel/process/stack_builder.cpp \
kernel/process/wait_queue.cpp \
kernel/syscalls/FileSyscalls.cpp \
kernel/syscalls/MemorySyscalls.cpp \
kernel/syscalls/SocketSyscalls.cpp \
kernel/syscalls/SyscallHelpers.cpp

# Bare-metal / target-only kernel sources
KERNEL_TARGET_SRCS := \
arch/$(ARCH)/cpu.cpp \
kernel/hal/targets/qemu_virt/platform_block.cpp \
kernel/hal/targets/qemu_virt/platform_board.cpp \
kernel/hal/targets/qemu_virt/platform_gpu.cpp \
kernel/hal/targets/qemu_virt/platform_input.cpp \
kernel/hal/targets/qemu_virt/platform_net.cpp \
kernel/main.cpp \
kernel/runtime/cxx_abi.cpp \
kernel/shell/shell.cpp \
kernel/syscalls/ProcessSyscalls.cpp \
kernel/syscalls/SignalSyscalls.cpp \
kernel/syscalls/SyscallDispatcher.cpp \
kernel/syscalls/TimeSyscalls.cpp

# Auto-discovered in-kernel shell test suites
KERNEL_SHELL_TEST_SRCS := $(sort $(wildcard kernel/shell/tests/*.cpp))

SRCS_CXX += $(KERNEL_CORE_SRCS) \
$(KERNEL_TARGET_SRCS) \
$(KERNEL_SHELL_TEST_SRCS)
Loading