1#include <acpi/lapic.h>
2#include <asm.h>
3#include <console/panic.h>
4#include <console/printf.h>
5#include <global.h>
6#include <logo.h>
7#include <smp/core.h>
8#include <stdarg.h>
9#include <sync/spinlock.h>
10#include <time/spin_sleep.h>
11#include <time/time.h>
12
13#define TEN_LINES "=========="
14#define EIGHTY_LINES \
15 TEN_LINES TEN_LINES TEN_LINES TEN_LINES TEN_LINES TEN_LINES TEN_LINES \
16 TEN_LINES
17
18void panic_broadcast_nmi() {
19 panic_broadcast(exclude_core: smp_core_id());
20}
21
22void panic_handler(struct panic_regs *regs) {
23 (void) regs;
24 disable_interrupts();
25
26 /*
27 k_printf(" [RAX]: %016lx [RBX]: %016lx [RCX]: %016lx\n", regs->rax,
28 regs->rbx, regs->rcx);
29 k_printf(" [RDX]: %016lx [RSI]: %016lx [RDI]: %016lx\n", regs->rdx,
30 regs->rsi, regs->rdi);
31 k_printf(" [RBP]: %016lx [RSP]: %016lx\n\n", regs->rbp, regs->rsp);
32
33 k_printf(" [ R8]: %016lx [ R9]: %016lx [R10]: %016lx\n", regs->r8,
34 regs->r9, regs->r10);
35 k_printf(" [R11]: %016lx [R12]: %016lx [R13]: %016lx\n", regs->r11,
36 regs->r12, regs->r13);
37 k_printf(" [R14]: %016lx [R15]: %016lx\n\n", regs->r14, regs->r15);*/
38
39 if (global.current_bootstage >= BOOTSTAGE_MID_MP) {
40 panic_broadcast(exclude_core: smp_core_id());
41 sleep_spin_ms(msec: 50);
42 }
43}
44
45static struct spinlock panic_lock = SPINLOCK_INIT;
46
47__noreturn void panic_impl_default(const char *file, int line, const char *func,
48 const char *fmt, ...) {
49 disable_interrupts();
50
51 spin_lock_raw(lock: &panic_lock);
52 atomic_store(&global.panicked, true);
53
54 printf(format: "\n" EIGHTY_LINES "\n");
55
56 if (global.current_bootstage < BOOTSTAGE_EARLY_DEVICES) {
57 printf(format: "\n [" ANSI_BG_RED
58 "KERNEL PANIC" ANSI_RESET "] @ time unknown\n");
59 } else {
60 printf(format: "\n [" ANSI_BG_RED
61 "KERNEL PANIC" ANSI_RESET "] @ time %llu\n",
62 time_get_ms());
63 }
64
65 printf(ANSI_RED "%s\n" ANSI_RESET, OS_LOGO_PANIC_CENTERED);
66
67 panic_entry();
68
69 printf(format: " [" ANSI_BRIGHT_BLUE "AT" ANSI_RESET " ");
70 if (global.current_bootstage > BOOTSTAGE_EARLY_DEVICES)
71 time_print_current();
72
73 printf(format: "]\n");
74
75 printf(format: " [" ANSI_BRIGHT_GREEN "FROM" ANSI_RESET "] " ANSI_GREEN
76 "%s" ANSI_RESET ":" ANSI_GREEN "%d" ANSI_RESET ":" ANSI_CYAN
77 "%s()" ANSI_RESET "\n"
78 " [" ANSI_YELLOW "MESSAGE" ANSI_RESET "] ",
79 file, line, func);
80
81 va_list args;
82 va_start(args, fmt);
83 vprintf(NULL, format: fmt, args);
84 va_end(args);
85 printf(format: "\n");
86
87 debug_print_stack();
88
89 printf(format: "\n" EIGHTY_LINES "\n");
90
91 spin_unlock_raw(lock: &panic_lock);
92 while (true)
93 wait_for_interrupt();
94}
95