LF OS
Hobby operating system for amd64 with high ambitions
Loading...
Searching...
No Matches
panic.c
Go to the documentation of this file.
1#include "panic.h"
2#include "config.h"
3#include "string.h"
4#include "fbconsole.h"
5#include "elf.h"
6
7extern const char* LAST_INIT_STEP;
8
9struct StackFrame {
12};
13
15
16// \cond panic_functions
17static void panic_message_impl(const char* message, uint64_t rbp, bool rbp_given) {
19 logf("panic", "An error occured and LF OS has to be halted. More info below:");
20
21 logf("panic", "LF OS build: %s", BUILD_ID);
22 logf("panic", "Last init step: %s", LAST_INIT_STEP);
23 logf("panic", "Error message: %s\n", message);
24
25 struct StackFrame* frame;
26
27 if(!rbp_given) {
28 asm("mov %%rbp,%0":"=r"(frame));
29 }
30 else {
31 frame = (struct StackFrame*)rbp;
32 }
33
34 if(frame) {
35 logi("panic", "Stack trace that led to this error:");
36
37 int i = 20;
38 while(frame && frame->rip > 0xFFFF800000000000 && --i) {
39 char symbol[256];
40 size_t symbol_size = sizeof(symbol);
41 if(!elf_symbolize(kernel_symbols, frame->rip, &symbol_size, symbol)) {
42 symbol[0] = 0;
43 }
44
45 logi("panic", " 0x%016x %s", frame->rip, symbol);
46
47 if(frame->prev != frame) {
48 frame = frame->prev;
49 }
50 else {
51 frame = 0;
52 }
53 }
54 }
55}
56// \endcond
57
58void panic(void) {
59 // \cond panic_functions
60 panic_message("Unknown error");
61 // \endcond
62}
63
64void panic_message(const char* message) {
65 // \cond panic_functions
66 panic_message_impl(message, 0, false);
67 while(1) {
68 asm("hlt");
69 }
70 // \endcond
71}
72
73void panic_cpu(const cpu_state* cpu) {
74 // \cond panic_functions
75 const char* exceptions[] = {
76 "Division by zero",
77 "Debug",
78 "NMI",
79 "Breakpoint",
80 "Overflow",
81 "Bound range exceeded",
82 "Invalid Opcode",
83 "Device not available",
84 "Double fault",
85 "Coprocessor segment overrun",
86 "Invalid TSS",
87 "Segment not present",
88 "Stack-segment fault",
89 "General protection fault",
90 "Page fault",
91 "reserved",
92 "x87 floating point exception",
93 "Alignment check",
94 "Machine check",
95 "SIMD floating point exception",
96 "Virtualization exception",
97 "reserved",
98 "reserved",
99 "reserved",
100 "reserved",
101 "reserved",
102 "reserved",
103 "reserved",
104 "reserved",
105 "Security exception",
106 "reserved"
107 };
108
109 char cr2_msg[256] = { 0 };
110
111 if(cpu->interrupt == 0x0E) {
112 uint64_t cr2;
113 asm("mov %%cr2, %0":"=r"(cr2));
114
115 char symbol[256];
116 size_t symbol_size = sizeof(symbol);
117 if(elf_symbolize(kernel_symbols, cr2, &symbol_size, symbol)) {
118 ksnprintf(cr2_msg, 256, " @ 0x%016x, %s", cr2, symbol);
119 }
120 else {
121 ksnprintf(cr2_msg, 256, " @ 0x%016x", cr2);
122 }
123
124 }
125
126 char message[256];
127 ksnprintf(message, 256, "Interrupt: 0x%02x (%s), error: 0x%04x%s", cpu->interrupt, exceptions[cpu->interrupt], cpu->error_code, cr2_msg);
128 panic_message_impl(message, cpu->rbp, true);
129
130 char symbol[256];
131 size_t symbol_size = sizeof(symbol);
132 if(!elf_symbolize(kernel_symbols, cpu->rip, &symbol_size, symbol)) {
133 symbol[0] = 0;
134 }
135
136 logi("panic", "RIP: 0x%016x %s", cpu->rip, symbol);
137
138 DUMP_CPU(cpu);
139
140 while(1) {
141 asm("hlt");
142 }
143 // \endcond
144}
145
uint64_t ptr_t
Definition arch.h:17
unsigned long uint64_t
Definition arch.h:14
#define DUMP_CPU(cpu)
Definition cpu.h:33
uint64_t interrupt
Definition cpu.h:24
uint64_t rip
Definition cpu.h:26
uint64_t rbp
Definition cpu.h:18
uint64_t error_code
Definition cpu.h:24
Definition cpu.h:7
bool elf_symbolize(void *symbol_data, ptr_t addr, size_t *symbol_size, char *symbol)
Definition elf.c:133
void fbconsole_back_to_kernel(void)
Definition fbconsole.c:348
size_t ksnprintf(char *buffer, size_t buffer_size, const char *format,...)
Definition string.c:339
#define logi(component, fmt,...)
Definition log.h:36
#define logf(component, fmt,...)
Definition log.h:60
void panic_message(const char *message)
Definition panic.c:64
ptr_t rip
Definition panic.c:11
void panic_cpu(const cpu_state *cpu)
Definition panic.c:73
void * kernel_symbols
Definition panic.c:14
const char * LAST_INIT_STEP
Definition main.c:21
void panic(void)
Definition panic.c:58
struct StackFrame * prev
Definition panic.c:10
static bool struct Message * message
Definition syscalls.h:338