Sync to trunk r65566.
[reactos.git] / boot / freeldr / freeldr / arch / i386 / i386bug.c
1
2 #include <freeldr.h>
3
4 #define NDEBUG
5 #include <debug.h>
6
7 typedef struct _FRAME
8 {
9 struct _FRAME *Next;
10 void *Address;
11 } FRAME;
12
13 char *i386ExceptionDescriptionText[] =
14 {
15 "Exception 00: DIVIDE BY ZERO\n\n",
16 "Exception 01: DEBUG EXCEPTION\n\n",
17 "Exception 02: NON-MASKABLE INTERRUPT EXCEPTION\n\n",
18 "Exception 03: BREAKPOINT (INT 3)\n\n",
19 "Exception 04: OVERFLOW\n\n",
20 "Exception 05: BOUND EXCEPTION\n\n",
21 "Exception 06: INVALID OPCODE\n\n",
22 "Exception 07: FPU NOT AVAILABLE\n\n",
23 "Exception 08: DOUBLE FAULT\n\n",
24 "Exception 09: COPROCESSOR SEGMENT OVERRUN\n\n",
25 "Exception 0A: INVALID TSS\n\n",
26 "Exception 0B: SEGMENT NOT PRESENT\n\n",
27 "Exception 0C: STACK EXCEPTION\n\n",
28 "Exception 0D: GENERAL PROTECTION FAULT\n\n",
29 "Exception 0E: PAGE FAULT\n\n",
30 "Exception 0F: Reserved\n\n",
31 "Exception 10: COPROCESSOR ERROR\n\n",
32 "Exception 11: ALIGNMENT CHECK\n\n",
33 "Exception 12: MACHINE CHECK\n\n"
34 };
35
36 #define SCREEN_ATTR 0x1f
37 void
38 i386PrintChar(char chr, ULONG x, ULONG y)
39 {
40 MachVideoPutChar(chr, SCREEN_ATTR, x, y);
41 }
42
43 /* Used to store the current X and Y position on the screen */
44 ULONG i386_ScreenPosX = 0;
45 ULONG i386_ScreenPosY = 0;
46
47 void
48 i386PrintText(char *pszText)
49 {
50 char chr;
51 while (1)
52 {
53 chr = *pszText++;
54
55 if (chr == 0) break;
56 if (chr == '\n')
57 {
58 i386_ScreenPosY++;
59 i386_ScreenPosX = 0;
60 continue;
61 }
62
63 MachVideoPutChar(chr, SCREEN_ATTR, i386_ScreenPosX, i386_ScreenPosY);
64 i386_ScreenPosX++;
65 }
66 }
67
68 void
69 PrintText(const char *format, ...)
70 {
71 va_list argptr;
72 char buffer[256];
73
74 va_start(argptr, format);
75 _vsnprintf(buffer, sizeof(buffer), format, argptr);
76 buffer[sizeof(buffer) - 1] = 0;
77 va_end(argptr);
78 i386PrintText(buffer);
79 }
80
81 void
82 i386PrintFrames(PKTRAP_FRAME TrapFrame)
83 {
84 FRAME *Frame;
85
86 PrintText("Frames:\n");
87 #ifdef _M_IX86
88 for (Frame = (FRAME*)TrapFrame->Ebp;
89 Frame != 0 && (ULONG_PTR)Frame < STACKADDR;
90 Frame = Frame->Next)
91 #else
92 for (Frame = (FRAME*)TrapFrame->TrapFrame;
93 Frame != 0 && (ULONG_PTR)Frame < STACKADDR;
94 Frame = Frame->Next)
95 #endif
96 {
97 PrintText("%p ", Frame->Address);
98 }
99 }
100
101 void
102 NTAPI
103 i386PrintExceptionText(ULONG TrapIndex, PKTRAP_FRAME TrapFrame, PKSPECIAL_REGISTERS Special)
104 {
105 PUCHAR InstructionPointer;
106
107 MachVideoClearScreen(SCREEN_ATTR);
108 i386_ScreenPosX = 0;
109 i386_ScreenPosY = 0;
110
111 PrintText("An error occured in " VERSION "\n"
112 "Report this error to the ReactOS Development mailing list <ros-dev@reactos.org>\n\n"
113 "0x%02lx: %s\n", TrapIndex, i386ExceptionDescriptionText[TrapIndex]);
114 #ifdef _M_IX86
115 PrintText("EAX: %.8lx ESP: %.8lx CR0: %.8lx DR0: %.8lx\n",
116 TrapFrame->Eax, TrapFrame->HardwareEsp, Special->Cr0, TrapFrame->Dr0);
117 PrintText("EBX: %.8lx EBP: %.8lx CR1: ???????? DR1: %.8lx\n",
118 TrapFrame->Ebx, TrapFrame->Ebp, TrapFrame->Dr1);
119 PrintText("ECX: %.8lx ESI: %.8lx CR2: %.8lx DR2: %.8lx\n",
120 TrapFrame->Ecx, TrapFrame->Esi, Special->Cr2, TrapFrame->Dr2);
121 PrintText("EDX: %.8lx EDI: %.8lx CR3: %.8lx DR3: %.8lx\n",
122 TrapFrame->Edx, TrapFrame->Edi, Special->Cr3, TrapFrame->Dr3);
123 PrintText(" DR6: %.8lx\n",
124 TrapFrame->Dr6);
125 PrintText(" DR7: %.8lx\n\n",
126 TrapFrame->Dr7);
127 PrintText("CS: %.4lx EIP: %.8lx\n",
128 TrapFrame->SegCs, TrapFrame->Eip);
129 PrintText("DS: %.4lx ERROR CODE: %.8lx\n",
130 TrapFrame->SegDs, TrapFrame->ErrCode);
131 PrintText("ES: %.4lx EFLAGS: %.8lx\n",
132 TrapFrame->SegEs, TrapFrame->EFlags);
133 PrintText("FS: %.4lx GDTR Base: %.8lx Limit: %.4x\n",
134 TrapFrame->SegFs, Special->Gdtr.Base, Special->Gdtr.Limit);
135 PrintText("GS: %.4lx IDTR Base: %.8lx Limit: %.4x\n",
136 TrapFrame->SegGs, Special->Idtr.Base, Special->Idtr.Limit);
137 PrintText("SS: %.4lx LDTR: %.4lx TR: %.4lx\n\n",
138 TrapFrame->HardwareSegSs, Special->Ldtr, Special->Idtr.Limit);
139
140 i386PrintFrames(TrapFrame); // Display frames
141 InstructionPointer = (PUCHAR)TrapFrame->Eip;
142 #else
143 PrintText("RAX: %.8lx R8: %.8lx R12: %.8lx RSI: %.8lx\n",
144 TrapFrame->Rax, TrapFrame->R8, 0, TrapFrame->Rsi);
145 PrintText("RBX: %.8lx R9: %.8lx R13: %.8lx RDI: %.8lx\n",
146 TrapFrame->Rbx, TrapFrame->R9, 0, TrapFrame->Rdi);
147 PrintText("RCX: %.8lx R10: %.8lx R14: %.8lx RBP: %.8lx\n",
148 TrapFrame->Rcx, TrapFrame->R10, 0, TrapFrame->Rbp);
149 PrintText("RDX: %.8lx R11: %.8lx R15: %.8lx RSP: %.8lx\n",
150 TrapFrame->Rdx, TrapFrame->R11, 0, TrapFrame->Rsp);
151
152 PrintText("CS: %.4lx RIP: %.8lx\n",
153 TrapFrame->SegCs, TrapFrame->Rip);
154 PrintText("DS: %.4lx ERROR CODE: %.8lx\n",
155 TrapFrame->SegDs, TrapFrame->ErrorCode);
156 PrintText("ES: %.4lx EFLAGS: %.8lx\n",
157 TrapFrame->SegEs, TrapFrame->EFlags);
158 PrintText("FS: %.4lx GDTR Base: %.8lx Limit: %.4x\n",
159 TrapFrame->SegFs, Special->Gdtr.Base, Special->Gdtr.Limit);
160 PrintText("GS: %.4lx IDTR Base: %.8lx Limit: %.4x\n",
161 TrapFrame->SegGs, Special->Idtr.Base, Special->Idtr.Limit);
162 PrintText("SS: %.4lx LDTR: %.4lx TR: %.4lx\n\n",
163 TrapFrame->SegSs, Special->Ldtr, Special->Idtr.Limit);
164 InstructionPointer = (PUCHAR)TrapFrame->Rip;
165 #endif
166 PrintText("\nInstructionstream: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x \n",
167 InstructionPointer[0], InstructionPointer[1],
168 InstructionPointer[2], InstructionPointer[3],
169 InstructionPointer[4], InstructionPointer[5],
170 InstructionPointer[6], InstructionPointer[7]);
171 }
172
173 VOID
174 NTAPI
175 FrLdrBugCheckWithMessage(
176 ULONG BugCode,
177 PCHAR File,
178 ULONG Line,
179 PSTR Format,
180 ...)
181 {
182 CHAR Buffer[1024];
183 va_list argptr;
184
185 /* Blue screen for the win */
186 MachVideoClearScreen(SCREEN_ATTR);
187 i386_ScreenPosX = 0;
188 i386_ScreenPosY = 0;
189
190 PrintText("A problem has been detected and FreeLoader boot has been aborted.\n\n");
191
192 PrintText("%ld: %s\n\n", BugCode, BugCodeStrings[BugCode]);
193
194 if (File)
195 {
196 PrintText("Location: %s:%ld\n\n", File, Line);
197 }
198
199 va_start(argptr, Format);
200 _vsnprintf(Buffer, sizeof(Buffer), Format, argptr);
201 va_end(argptr);
202 Buffer[sizeof(Buffer) - 1] = 0;
203
204 i386PrintText(Buffer);
205
206 _disable();
207 __halt();
208 for (;;);
209 }
210
211 void
212 NTAPI
213 FrLdrBugCheckEx(
214 ULONG BugCode,
215 PCHAR File,
216 ULONG Line)
217 {
218 MachVideoClearScreen(SCREEN_ATTR);
219 i386_ScreenPosX = 0;
220 i386_ScreenPosY = 0;
221
222 PrintText("A problem has been detected and FreeLoader boot has been aborted.\n\n");
223
224 PrintText("%ld: %s\n\n", BugCode, BugCodeStrings[BugCode]);
225
226 if (File)
227 {
228 PrintText("Location: %s:%ld\n\n", File, Line);
229 }
230
231 PrintText("Bug Information:\n %p\n %p\n %p\n %p\n %p\n\n",
232 BugCheckInfo[0], BugCheckInfo[1], BugCheckInfo[2], BugCheckInfo[3], BugCheckInfo[4]);
233
234 for (;;);
235 }
236
237 void
238 NTAPI
239 FrLdrBugCheck(ULONG BugCode)
240 {
241 FrLdrBugCheckEx(BugCode, 0, 0);
242 }
243