- Fixed memory management.
[planlOS.git] / system / kernel / ke / panic.c
blob2b7f361b8d0986ba2440543c9999ee062412c8ae
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "ke/panic.h"
23 #include "ke/apic.h"
24 #include "ke/module.h"
25 #include "mm/memory.h"
26 #include "sys/terminal.h"
27 #include <string.h>
29 static int first_try = 1;
31 extern uint32_t kernel_directory[1024];
33 static int width;
34 static int height;
35 static char *screen;
37 static void kePrintString(int x, int y, char *s)
39 int length = strlen(s);
40 if (x + length > width) length = width - x;
41 int i;
42 for (i = 0; i < length; i++)
44 screen[y * width * 2 + (x + i) * 2] = s[i];
47 static char *digits = "0123456789abcdef";
48 static void kePrintHex(int x, int y, uint32_t number)
50 int i = 0;
51 if (x > width - 8) i = 8 - (width - x);
52 for (i = 0; i < 8; i++)
54 screen[y * width * 2 + (x + 7 - i) * 2] = digits[(number >> (i * 4)) & 0xF];
57 static int kePrintDec(int x, int y, uint32_t number)
59 int written = 0;
60 if ((number >= 10) && (width - x > 0))
62 written = kePrintDec(x, y, number / 10);
63 x += written;
65 written++;
66 screen[y * width * 2 + x * 2] = '0' + (number % 10);
67 return written;
70 static int keIsAccessible(uintptr_t addr)
72 if (0x1000 - (addr & 0xFFF) < sizeof(uintptr_t))
73 return !mmKernelPageIsFree(addr & ~0xFFF) && !mmKernelPageIsFree((addr + 0x1000) & ~0xFFF);
74 else
75 return !mmKernelPageIsFree(addr & ~0xFFF);
78 static void kePrintStackFrameEIP(int y, uintptr_t eip)
80 kePrintHex(29, y, eip);
81 // Print function
82 char *function = 0;
83 uint32_t offset = 0;
84 int status = keResolveAddress(eip, &function, &offset);
85 if (!status)
87 kePrintString(38, y, function);
88 kePrintString(38 + strlen(function) + 1, y, "+");
89 kePrintHex(38 + strlen(function) + 3, y, offset);
91 else
93 char *module = 0;
94 status = keResolveModuleAddress(eip, &module, &function, &offset);
95 if (!status)
97 if (strcmp(function, ""))
99 kePrintString(38, y, module);
100 kePrintString(38 + strlen(module) + 1, y, "+");
101 kePrintHex(38 + strlen(module) + 3, y, offset);
103 else
105 kePrintString(38, y, module);
106 kePrintString(38 + strlen(module) + 1, y, "+");
107 kePrintHex(38 + strlen(module) + 3, y, offset);
110 else
112 kePrintString(38, y, "<unknown>");
117 static uintptr_t kePrintStackFrame(int y, uintptr_t ebp, uintptr_t eip)
119 if (eip)
121 // Add one line with the current function
122 kePrintStackFrameEIP(y, eip);
123 y++;
125 kePrintHex(20, y, ebp);
126 if (ebp < 0xC0000000) return 0;
127 if (keIsAccessible(ebp))
129 uintptr_t nextebp = *((uintptr_t*)ebp);
130 // Get calling code
131 if (keIsAccessible(ebp + 4))
133 eip = *((uintptr_t*)ebp + 1);
134 kePrintStackFrameEIP(y, eip);
136 return nextebp;
138 return 0;
141 void kePanic(IntStackFrame *isf, int error)
143 if (!first_try)
145 // The crash handler crashed.
146 asm("cli");
147 asm("hlt");
149 first_try = 0;
150 // Stop other CPUs
151 keAPICBroadcast(0x31, 0);
152 // Switch to kernel address space
153 asm volatile("mov %%eax, %%cr3" :: "a" ((uintptr_t)kernel_directory - MM_KERNEL_BASE));
154 // Get screen info
155 screen = sysTerminalPreparePanic(&width, &height);
156 memset(screen, 0, width * height * 2);
157 int i;
158 for (i = 0; i < width * height; i++)
160 screen[i * 2 + 1] = 0x17;
163 // Draw info
164 kePrintString(1, 1, "Panic: ");
166 if (error)
168 kePrintString(8, 1, "Error ");
169 kePrintDec(14, 1, error);
171 else
173 // Registers
174 kePrintString(8, 1, "Exception ");
175 kePrintDec(19, 1, isf->intno);
176 kePrintString(8, 2, "Error code");
177 kePrintHex(19, 2, isf->error);
178 if (isf->intno == 14)
180 uint32_t cr2;
181 asm("mov %%cr2, %0":"=r"(cr2));
182 kePrintString(8, 3, "Address");
183 kePrintHex(19, 3, cr2);
185 kePrintString(1, 5, "cs");
186 kePrintHex(8, 5, isf->cs);
187 kePrintString(1, 6, "eip");
188 kePrintHex(8, 6, isf->eip);
189 kePrintString(1, 7, "ss");
190 kePrintHex(8, 7, isf->ss);
191 kePrintString(1, 8, "esp");
192 kePrintHex(8, 8, isf->esp);
193 kePrintString(1, 9, "ebp");
194 kePrintHex(8, 9, isf->ebp);
195 kePrintString(1, 10, "eax");
196 kePrintHex(8, 10, isf->eax);
197 kePrintString(1, 11, "ebx");
198 kePrintHex(8, 11, isf->ebx);
199 kePrintString(1, 12, "ecx");
200 kePrintHex(8, 12, isf->ecx);
201 kePrintString(1, 13, "edx");
202 kePrintHex(8, 13, isf->edx);
203 kePrintString(1, 14, "esi");
204 kePrintHex(8, 14, isf->esi);
205 kePrintString(1, 15, "edi");
206 kePrintHex(8, 15, isf->edi);
207 kePrintString(1, 16, "ds");
208 kePrintHex(8, 16, isf->ds);
209 kePrintString(1, 17, "es");
210 kePrintHex(8, 17, isf->es);
211 kePrintString(1, 18, "fs");
212 kePrintHex(8, 18, isf->fs);
213 kePrintString(1, 19, "gs");
214 kePrintHex(8, 19, isf->gs);
215 kePrintString(1, 20, "eflags");
216 kePrintHex(8, 20, isf->eflags);
219 // Stack trace
220 kePrintString(20, 5, "Stack:");
221 uintptr_t ebp = 0;
222 if (isf)
224 ebp = isf->ebp;
226 else
228 asm volatile("mov %%ebp, %0" : "=r"(ebp));
230 int y = 6;
231 if (isf)
233 kePrintStackFrame(y, isf->ebp, isf->eip);
234 y++;
236 while ((ebp = kePrintStackFrame(y, ebp, 0)) != 0)
238 y++;
239 if (y == height - 1) break;
242 // Stop current CPU
243 asm("cli");
244 asm("hlt");