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.
24 #include "ke/module.h"
25 #include "mm/memory.h"
26 #include "sys/terminal.h"
29 static int first_try
= 1;
31 extern uint32_t kernel_directory
[1024];
37 static void kePrintString(int x
, int y
, char *s
)
39 int length
= strlen(s
);
40 if (x
+ length
> width
) length
= width
- x
;
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
)
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
)
60 if ((number
>= 10) && (width
- x
> 0))
62 written
= kePrintDec(x
, y
, number
/ 10);
66 screen
[y
* width
* 2 + x
* 2] = '0' + (number
% 10);
70 static int keIsAccessible(uintptr_t addr
)
72 if (0x1000 - (addr
& 0xFFF) < sizeof(uintptr_t))
73 return !mmKernelPageIsFree(addr
& ~0xFFF) && !mmKernelPageIsFree((addr
+ 0x1000) & ~0xFFF);
75 return !mmKernelPageIsFree(addr
& ~0xFFF);
78 static void kePrintStackFrameEIP(int y
, uintptr_t eip
)
80 kePrintHex(29, y
, eip
);
84 int status
= keResolveAddress(eip
, &function
, &offset
);
87 kePrintString(38, y
, function
);
88 kePrintString(38 + strlen(function
) + 1, y
, "+");
89 kePrintHex(38 + strlen(function
) + 3, y
, offset
);
94 status
= keResolveModuleAddress(eip
, &module
, &function
, &offset
);
97 if (strcmp(function
, ""))
99 kePrintString(38, y
, module
);
100 kePrintString(38 + strlen(module
) + 1, y
, "+");
101 kePrintHex(38 + strlen(module
) + 3, y
, offset
);
105 kePrintString(38, y
, module
);
106 kePrintString(38 + strlen(module
) + 1, y
, "+");
107 kePrintHex(38 + strlen(module
) + 3, y
, offset
);
112 kePrintString(38, y
, "<unknown>");
117 static uintptr_t kePrintStackFrame(int y
, uintptr_t ebp
, uintptr_t eip
)
121 // Add one line with the current function
122 kePrintStackFrameEIP(y
, eip
);
125 kePrintHex(20, y
, ebp
);
126 if (ebp
< 0xC0000000) return 0;
127 if (keIsAccessible(ebp
))
129 uintptr_t nextebp
= *((uintptr_t*)ebp
);
131 if (keIsAccessible(ebp
+ 4))
133 eip
= *((uintptr_t*)ebp
+ 1);
134 kePrintStackFrameEIP(y
, eip
);
141 void kePanic(IntStackFrame
*isf
, int error
)
145 // The crash handler crashed.
151 keAPICBroadcast(0x31, 0);
152 // Switch to kernel address space
153 asm volatile("mov %%eax, %%cr3" :: "a" ((uintptr_t)kernel_directory
- MM_KERNEL_BASE
));
155 screen
= sysTerminalPreparePanic(&width
, &height
);
156 memset(screen
, 0, width
* height
* 2);
158 for (i
= 0; i
< width
* height
; i
++)
160 screen
[i
* 2 + 1] = 0x17;
164 kePrintString(1, 1, "Panic: ");
168 kePrintString(8, 1, "Error ");
169 kePrintDec(14, 1, error
);
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)
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
);
220 kePrintString(20, 5, "Stack:");
228 asm volatile("mov %%ebp, %0" : "=r"(ebp
));
233 kePrintStackFrame(y
, isf
->ebp
, isf
->eip
);
236 while ((ebp
= kePrintStackFrame(y
, ebp
, 0)) != 0)
239 if (y
== height
- 1) break;