LumaOS  v0.1.116
An experimental x86-64 OS
Loading...
Searching...
No Matches
panic.c
Go to the documentation of this file.
1
9#include <kernel/vars.h>
10#include <lib/kstring.h>
11#include <kernel/kprintf.h>
12#include <kernel/panic.h>
13
17static void hcf(void) {
18 for (;;) {
19 asm ("hlt"); // Halt the cpu
20 }
21}
22
23__attribute__((noreturn)) void panic_at(const char *file, int line_int, const char *func, ...) {
24
25 asm volatile ("cli"); // 1. Disable Interrupts
26
27 kprintf("\n\n================\n= KERNEL PANIC =\n================\n"); // 2. Print Banner
28
29 va_list ap;
30 va_start(ap, func);
31
32 char *fmt = va_arg(ap, char *); // Format string is the first argument
33
34 uint64_t line_len = 0; // The length of the string that will hold the line num
35 uint64_t next = line_int; // The next number to continue the length calculation
36
37 // Get number length
38 while (next != 0) {
39 next /= 10;
40 line_len++;
41 }
42
43 char line[line_len + 1]; // Initialize a string of line_length + 1 length
44
45 int_to_str(line_int, line); // Convert the int to a string and store it in line
46
47 char txt[strlen(file) + line_len + strlen(fmt) + strlen(func) + 9]; // A string to hold all the information
48 txt[0] = '\0'; // Initialize is with a \0
49
50 // Build the string with all the info
51 strcat(txt, file);
52 strcat(txt, ": ");
53 strcat(txt, line);
54 strcat(txt, " -> ");
55 strcat(txt, func);
56 strcat(txt, ": ");
57 strcat(txt, fmt);
58
59
60 kvprintf(txt, ap); // 3. Print location and message
61
62 // 4. Print control registers, and stack and frame pointers
63
64 uint64_t cr0;
65 asm volatile ("mov %%cr0, %0" : "=r"(cr0));
66
67 uint64_t cr2;
68 asm volatile ("mov %%cr2, %0" : "=r"(cr2));
69
70 uint64_t cr3;
71 asm volatile ("mov %%cr3, %0" : "=r"(cr3));
72
73 uint64_t cr4;
74 asm volatile ("mov %%cr4, %0" : "=r"(cr4));
75
76 void *rbp = __builtin_frame_address(0);
77
78 uintptr_t stack_pointer;
79 asm volatile ("mov %%rsp, %0" : "=r"(stack_pointer));
80
81 kprintf("\n\nCR0: 0x%x\nCR2: 0x%x\nCR3: 0x%x\nCR4: 0x%x\n\nFP: %p\nSP: %p\n\n", cr0, cr2, cr3, cr4, rbp, (void *) stack_pointer);
82
83 // 5. Backtrace
84
85 kprintf("Stack Backtrace:\n");
86
87 #define MAX_DEPTH 16
88
89 for (int i = 0; i < MAX_DEPTH; i++) {
90 uint64_t stack_frame = (uint64_t) rbp;
91 uint64_t offset = get_hddm_offset();
92
93 // Validate frame alignment and lower boundary.
94 if (stack_frame == 0 ||
95 stack_frame % 8 != 0 ||
96 stack_frame < offset) {
97 break;
98 }
99
100 // Ensure both frame entries are accessible here.
101 uint64_t previous_rbp = ((uint64_t *)stack_frame)[0];
102 uint64_t return_addr = ((uint64_t *)stack_frame)[1];
103
104 if (return_addr == 0)
105 break;
106
107 kprintf("%16p: Return Addr %16p\n",
108 (void *) stack_frame,
109 (void *) return_addr);
110
111 if (previous_rbp == 0 ||
112 previous_rbp <= stack_frame) {
113 break;
114 }
115
116 rbp = (uint64_t *) previous_rbp;
117 }
118
119 kprintf("\n==========================\n= END OF LOG =\n==========================\n");
120
121 va_end(ap);
122
123 hcf();
124}
Tools for printing to the console and serial output.
void kprintf(const char *fmt,...) __attribute__((format(printf
Wrapper for a standard print.
void void kvprintf(const char *fmt, va_list ap)
Handles printing a formatted string.
Definition kprintf.c:28
Utilities for memory and string operations.
size_t strlen(const char *str)
Calculates the length of a string.
Definition kstring.c:92
char * strcat(char *str1, const char *str2)
Concatenates (joins) 2 strings.
Definition kstring.c:107
char * int_to_str(uint64_t value, char *buff)
Converts an integer to a string.
Definition kstring.c:120
static void hcf(void)
Halts the CPU.
Definition panic.c:17
Handler for kernel panics.
Kernel wide variables.
uint64_t get_hddm_offset(void)
Gets the offset for the HDDM.
Definition vars.c:18