m1x/x86_64 + kern: Add panic() implementation

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-03-25 23:07:55 -04:00
parent 47fcd53739
commit 0162ce542b
4 changed files with 84 additions and 1 deletions

View File

@@ -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:

View File

@@ -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 <sys/cdefs.h>
#include <stdarg.h>
/*
* 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_ */

View File

@@ -4,6 +4,7 @@
*/
#include <dev/cons/cons.h>
#include <kern/panic.h>
#include <lib/printf.h>
#include <hal/cpu.h>
@@ -35,5 +36,6 @@ kmain(void)
/* Initialize the BSP */
hal_cpu_init();
for (;;);
panic("end of kernel\n");
__builtin_unreachable();
}

28
src/m1x/kern/kern_panic.c Normal file
View File

@@ -0,0 +1,28 @@
/*
* Copyright (c) 2026, Mirocom Laboratories
* Provided under the BSD-3 clause
*/
#include <kern/panic.h>
#include <lib/printf.h>
#include <hal/cpu.h>
/*
* 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();
}
}