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