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