diff --git a/src/m1x/arch/x86_64/kern/panic_thunk.S b/src/m1x/arch/x86_64/kern/panic_thunk.S new file mode 100644 index 0000000..ec6071e --- /dev/null +++ b/src/m1x/arch/x86_64/kern/panic_thunk.S @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2026, Mirocom Laboratories + * Provided under the BSD-3 clause + */ + + .text + .globl panic + .extern __panic +panic: + mov $1, %rax /* Lock ourselves */ + xchg %rax, __mp_sync /* -> __mp_sync */ + testq $1, %rax /* Are we another processor? */ + jnz panic_lockout /* Yes, enter lockout */ + lea stack_top(%rip), %rsp /* Load a sane stack */ + jmp __panic +panic_lockout: + cli + hlt + jmp panic_lockout + + .section .data +__mp_sync: .quad 0 + + .align 8 + .section .bss +stack: .fill 4096, 1, 0 +stack_top: diff --git a/src/m1x/include/kern/panic.h b/src/m1x/include/kern/panic.h new file mode 100644 index 0000000..68f500f --- /dev/null +++ b/src/m1x/include/kern/panic.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2026, Mirocom Laboratories + * Provided under the BSD-3 clause + */ + +#ifndef _KERN_PANIC_H_ +#define _KERN_PANIC_H_ 1 + +#include +#include + +/* + * Signal to the user that something went seriously wrong and + * cease all system operation. + */ +__no_return void panic(const char *fmt, ...); + +/* + * Central panic handler + * + * XXX: This function should not be called directly! Use the + * panic() function instead. + */ +__no_return void __panic(const char *fmt, ...); + +#endif /* !_KERN_PANIC_H_ */ diff --git a/src/m1x/kern/kern_init.c b/src/m1x/kern/kern_init.c index 929c98e..56b922b 100644 --- a/src/m1x/kern/kern_init.c +++ b/src/m1x/kern/kern_init.c @@ -4,6 +4,7 @@ */ #include +#include #include #include @@ -35,5 +36,6 @@ kmain(void) /* Initialize the BSP */ hal_cpu_init(); - for (;;); + panic("end of kernel\n"); + __builtin_unreachable(); } diff --git a/src/m1x/kern/kern_panic.c b/src/m1x/kern/kern_panic.c new file mode 100644 index 0000000..35e3518 --- /dev/null +++ b/src/m1x/kern/kern_panic.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2026, Mirocom Laboratories + * Provided under the BSD-3 clause + */ + +#include +#include +#include + +/* + * Even though the stack is guaranteed to be sane upon entry, + * it would still be wise to minimize its usage and therefore + * rely on globals. + */ +static char panic_buf[128]; +static va_list panic_ap; + +__no_return void +__panic(const char *fmt, ...) +{ + va_start(panic_ap, fmt); + vsnprintf(panic_buf, sizeof(panic_buf), fmt, panic_ap); + printf("\033[31;40mpanic\033[0m: %s", panic_buf); + + for (;;) { + hal_cpu_halt(); + } +}