From e2564d112ba03b432f9f44f445cbc4a3ed85751a Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Fri, 17 Apr 2026 01:05:31 +0800 Subject: feat: add initial RISC-V architecture support and core kernel functionality - Implement MMU hardware primitives for RISC-V SV39 in mmu.h - Create trap vector control primitives in trap.h - Define trap frame structure for RISC-V in trapframe.h - Add assembly routines for context switching in swtch.S - Implement trap vector entry/exit in trap_vector.S - Introduce basic error number definitions in errno.h - Provide placement new/delete operators in new.h - Define kernel panic handler in panic.h and implement in panic.cc - Establish basic type definitions in types.h - Create the kernel main entry point in main.cc with global initialization - Add synchronization primitives using spinlocks in spinlock.h and spinlock.cc - Define per-CPU state tracking for interrupts in cpu_state.h - Create a linker script for the kernel in linker.ld - Update module makefiles for kernel and library components --- na/lib/panic.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 na/lib/panic.cc (limited to 'na/lib/panic.cc') diff --git a/na/lib/panic.cc b/na/lib/panic.cc new file mode 100644 index 0000000..4745272 --- /dev/null +++ b/na/lib/panic.cc @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +/* + * Kernel Panic — minimal fatal error handler. + * + * Before console_init(): silent death (no UART). + * After console_init(): prints message then halts. + * + * Reference: docs/specs/global_init.md §6 + */ + +#include +#include + +// Minimal direct UART output (same as main.cc early boot) +// This can be used by other early-boot code before console_init() + +constexpr uint64 kUart0VA = 0x10000000UL + 0xFFFFFFC000000000ULL; + +extern "C" void early_putc(char c) { + volatile auto* uart = reinterpret_cast(kUart0VA); + *uart = static_cast(c); +} + +extern "C" void early_puts(const char* s) { + while (*s) early_putc(*s++); +} + +extern "C" [[noreturn]] void kernel_panic(const char* msg, const char* detail) { + arch::cpu::intr_off(); + + early_puts("\n*** KERNEL PANIC: "); + if (msg) { + early_puts(msg); + } + if (detail) { + early_puts(": "); + early_puts(detail); + } + early_puts(" ***\n"); + + while (true) { + arch::cpu::halt_until_interrupt(); + } +} -- cgit v1.2.3