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