ReactOS  0.4.15-dev-4869-g35a816a
i386bug.c
Go to the documentation of this file.
1 
2 #include <freeldr.h>
3 
4 #include <debug.h>
5 
6 typedef struct _FRAME
7 {
8  struct _FRAME* Next;
10 } FRAME;
11 
13 {
14  "Exception 00: DIVIDE BY ZERO",
15  "Exception 01: DEBUG EXCEPTION",
16  "Exception 02: NON-MASKABLE INTERRUPT EXCEPTION",
17  "Exception 03: BREAKPOINT (INT 3)",
18  "Exception 04: OVERFLOW",
19  "Exception 05: BOUND EXCEPTION",
20  "Exception 06: INVALID OPCODE",
21  "Exception 07: FPU NOT AVAILABLE",
22  "Exception 08: DOUBLE FAULT",
23  "Exception 09: COPROCESSOR SEGMENT OVERRUN",
24  "Exception 0A: INVALID TSS",
25  "Exception 0B: SEGMENT NOT PRESENT",
26  "Exception 0C: STACK EXCEPTION",
27  "Exception 0D: GENERAL PROTECTION FAULT",
28  "Exception 0E: PAGE FAULT",
29  "Exception 0F: Reserved",
30  "Exception 10: COPROCESSOR ERROR",
31  "Exception 11: ALIGNMENT CHECK",
32  "Exception 12: MACHINE CHECK"
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 */
40 
41 static void
43 {
45 
47 
48  for (; *pszText != ANSI_NULL; ++pszText)
49  {
50  if (*pszText == '\n')
51  {
52  i386_ScreenPosX = 0;
54  continue;
55  }
56 
58  if (++i386_ScreenPosX >= Width)
59  {
60  i386_ScreenPosX = 0;
62  }
63  // FIXME: Implement vertical screen scrolling if we are at the end of the screen.
64  }
65 }
66 
67 static void
69 {
70  CHAR Buffer[512];
71 
72  _vsnprintf(Buffer, sizeof(Buffer), Format, args);
73  Buffer[sizeof(Buffer) - 1] = ANSI_NULL;
74 
76 }
77 
78 static void
79 PrintText(const CHAR *Format, ...)
80 {
81  va_list argptr;
82 
83  va_start(argptr, Format);
84  PrintTextV(Format, argptr);
85  va_end(argptr);
86 }
87 
88 static void
90 {
91  FRAME* Frame;
92 
93  PrintText("Frames:\n");
94  for (Frame =
95 #ifdef _M_IX86
96  (FRAME*)TrapFrame->Ebp;
97 #else
98  (FRAME*)TrapFrame->TrapFrame;
99 #endif
100  Frame != NULL && (ULONG_PTR)Frame < STACKADDR;
101  Frame = Frame->Next)
102  {
103  PrintText("%p ", Frame->Address);
104  }
105 }
106 
107 void
108 NTAPI
110 {
111  PUCHAR InstructionPointer;
112 
115  i386_ScreenPosX = 0;
116  i386_ScreenPosY = 0;
117 
118  PrintText("An error occured in " VERSION "\n"
119  "Report this error on the ReactOS Bug Tracker: https://jira.reactos.org\n\n"
120  "0x%02lx: %s\n\n", TrapIndex, i386ExceptionDescriptionText[TrapIndex]);
121 
122 #ifdef _M_IX86
123  PrintText("EAX: %.8lx ESP: %.8lx CR0: %.8lx DR0: %.8lx\n",
124  TrapFrame->Eax, TrapFrame->HardwareEsp, Special->Cr0, TrapFrame->Dr0);
125  PrintText("EBX: %.8lx EBP: %.8lx CR1: ???????? DR1: %.8lx\n",
126  TrapFrame->Ebx, TrapFrame->Ebp, TrapFrame->Dr1);
127  PrintText("ECX: %.8lx ESI: %.8lx CR2: %.8lx DR2: %.8lx\n",
128  TrapFrame->Ecx, TrapFrame->Esi, Special->Cr2, TrapFrame->Dr2);
129  PrintText("EDX: %.8lx EDI: %.8lx CR3: %.8lx DR3: %.8lx\n",
130  TrapFrame->Edx, TrapFrame->Edi, Special->Cr3, TrapFrame->Dr3);
131  PrintText("%*s CR4: %.8lx DR6: %.8lx\n",
132  41, "", Special->Cr4, TrapFrame->Dr6);
133  PrintText("%*s DR7: %.8lx\n",
134  62, "", TrapFrame->Dr7);
135 
136  /* NOTE: Segment registers are intrinsically 16 bits. Even if the x86
137  * KTRAP_FRAME structure stores them as ULONG, only their lower 16 bits
138  * are initialized. We thus cast them to USHORT before display. */
139  PrintText(" CS: %.4lx EIP: %.8lx\n",
140  (USHORT)TrapFrame->SegCs, TrapFrame->Eip);
141  PrintText(" DS: %.4lx ERROR CODE: %.8lx\n",
142  (USHORT)TrapFrame->SegDs, TrapFrame->ErrCode);
143  PrintText(" ES: %.4lx EFLAGS: %.8lx\n",
144  (USHORT)TrapFrame->SegEs, TrapFrame->EFlags);
145  PrintText(" FS: %.4lx GDTR Base: %.8lx Limit: %.4x\n",
146  // " FS: %.4lx GDTR: Base %.8lx Limit %.4x\n"
147  (USHORT)TrapFrame->SegFs, Special->Gdtr.Base, Special->Gdtr.Limit);
148  PrintText(" GS: %.4lx IDTR Base: %.8lx Limit: %.4x\n",
149  // " GS: %.4lx IDTR: Base %.8lx Limit %.4x\n",
150  (USHORT)TrapFrame->SegGs, Special->Idtr.Base, Special->Idtr.Limit);
151  PrintText(" SS: %.4lx LDTR: %.4lx TR: %.4lx\n\n",
152  (USHORT)TrapFrame->HardwareSegSs, Special->Ldtr, Special->Tr);
153 #else
154  PrintText("RAX: %.8lx R8: %.8lx R12: %.8lx RSI: %.8lx\n",
155  TrapFrame->Rax, TrapFrame->R8, 0, TrapFrame->Rsi);
156  PrintText("RBX: %.8lx R9: %.8lx R13: %.8lx RDI: %.8lx\n",
157  TrapFrame->Rbx, TrapFrame->R9, 0, TrapFrame->Rdi);
158  PrintText("RCX: %.8lx R10: %.8lx R14: %.8lx RBP: %.8lx\n",
159  TrapFrame->Rcx, TrapFrame->R10, 0, TrapFrame->Rbp);
160  PrintText("RDX: %.8lx R11: %.8lx R15: %.8lx RSP: %.8lx\n",
161  TrapFrame->Rdx, TrapFrame->R11, 0, TrapFrame->Rsp);
162 
163  PrintText(" CS: %.4lx RIP: %.8lx\n",
164  TrapFrame->SegCs, TrapFrame->Rip);
165  PrintText(" DS: %.4lx ERROR CODE: %.8lx\n",
166  TrapFrame->SegDs, TrapFrame->ErrorCode);
167  PrintText(" ES: %.4lx EFLAGS: %.8lx\n",
168  TrapFrame->SegEs, TrapFrame->EFlags);
169  PrintText(" FS: %.4lx GDTR Base: %.8lx Limit: %.4x\n",
170  TrapFrame->SegFs, Special->Gdtr.Base, Special->Gdtr.Limit);
171  PrintText(" GS: %.4lx IDTR Base: %.8lx Limit: %.4x\n",
172  TrapFrame->SegGs, Special->Idtr.Base, Special->Idtr.Limit);
173  PrintText(" SS: %.4lx LDTR: %.4lx TR: %.4lx\n\n",
174  TrapFrame->SegSs, Special->Ldtr, Special->Tr);
175 #endif
176 
177  /* Display the stack frames */
178  i386PrintFrames(TrapFrame);
179 
180 #ifdef _M_IX86
181  InstructionPointer = (PUCHAR)TrapFrame->Eip;
182 #else
183  InstructionPointer = (PUCHAR)TrapFrame->Rip;
184 #endif
185  /* Adjust IP for #BP (INT 03) or #OF to point to the offending instruction */
186  if ((TrapIndex == 3) || (TrapIndex == 4))
187  InstructionPointer--;
188 
189  PrintText("\nInstruction stream: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n",
190  InstructionPointer[0], InstructionPointer[1],
191  InstructionPointer[2], InstructionPointer[3],
192  InstructionPointer[4], InstructionPointer[5],
193  InstructionPointer[6], InstructionPointer[7]);
194 }
195 
196 VOID
198  ULONG BugCode,
199  PCHAR File,
200  ULONG Line,
201  PSTR Format,
202  ...)
203 {
204  va_list argptr;
205 
208  i386_ScreenPosX = 0;
209  i386_ScreenPosY = 0;
210 
211  PrintText("A problem has been detected and FreeLoader boot has been aborted.\n\n");
212 
213  PrintText("%ld: %s\n\n", BugCode, BugCodeStrings[BugCode]);
214 
215  if (File)
216  {
217  PrintText("Location: %s:%ld\n\n", File, Line);
218  }
219 
220  va_start(argptr, Format);
221  PrintTextV(Format, argptr);
222  va_end(argptr);
223 
224  _disable();
225  __halt();
226  for (;;);
227 }
228 
229 void
230 NTAPI
232  ULONG BugCode,
233  PCHAR File,
234  ULONG Line)
235 {
238  i386_ScreenPosX = 0;
239  i386_ScreenPosY = 0;
240 
241  PrintText("A problem has been detected and FreeLoader boot has been aborted.\n\n");
242 
243  PrintText("%ld: %s\n\n", BugCode, BugCodeStrings[BugCode]);
244 
245  if (File)
246  {
247  PrintText("Location: %s:%ld\n\n", File, Line);
248  }
249 
250  PrintText("Bug Information:\n %p\n %p\n %p\n %p\n %p\n\n",
252 
253  _disable();
254  __halt();
255  for (;;);
256 }
257 
258 void
259 NTAPI
261 {
262  FrLdrBugCheckEx(BugCode, 0, 0);
263 }
UINT64 R8
Definition: ketypes.h:326
char * BugCodeStrings[]
Definition: debug.c:515
signed char * PCHAR
Definition: retypes.h:7
VOID FrLdrBugCheckWithMessage(ULONG BugCode, PCHAR File, ULONG Line, PSTR Format,...)
Definition: i386bug.c:197
UINT64 Rbp
Definition: ketypes.h:377
UINT64 R10
Definition: ketypes.h:328
_Must_inspect_result_ typedef _In_ PVOID Unused
Definition: iotypes.h:1166
void NTAPI i386PrintExceptionText(ULONG TrapIndex, PKTRAP_FRAME TrapFrame, PKSPECIAL_REGISTERS Special)
Definition: i386bug.c:109
UINT64 Rbx
Definition: ketypes.h:374
UINT64 R9
Definition: ketypes.h:327
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
_In_ HFONT _Out_ PUINT _Out_ PUINT Width
Definition: font.h:124
#define MachVideoGetDisplaySize(W, H, D)
Definition: machine.h:96
unsigned char * PUCHAR
Definition: retypes.h:3
char CHAR
Definition: xmlstorage.h:175
__INTRIN_INLINE void __halt(void)
Definition: intrin_x86.h:1706
USHORT SegFs
Definition: ketypes.h:371
ULONG HardwareSegSs
Definition: ketypes.h:272
ULONG Esi
Definition: ketypes.h:264
Definition: match.c:390
PVOID Base
Definition: ketypes.h:491
ULONG Edi
Definition: ketypes.h:263
USHORT Limit
Definition: ketypes.h:490
uint32_t ULONG_PTR
Definition: typedefs.h:65
else
Definition: tritemp.h:161
#define STACKADDR
Definition: x86common.h:16
UINT64 Dr2
Definition: ketypes.h:349
ULONG Eax
Definition: ketypes.h:259
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define va_end(ap)
Definition: acmsvcex.h:90
UINT64 Rsi
Definition: ketypes.h:376
USHORT SegGs
Definition: ketypes.h:372
#define FALSE
Definition: types.h:117
#define ANSI_NULL
UINT64 Rax
Definition: ketypes.h:323
#define MachVideoPutChar(Ch, Attr, X, Y)
Definition: machine.h:106
USHORT SegEs
Definition: ketypes.h:370
static void PrintTextV(const CHAR *Format, va_list args)
Definition: i386bug.c:68
struct _FRAME * Next
Definition: i386bug.c:8
USHORT SegCs
Definition: ketypes.h:385
char * va_list
Definition: acmsvcex.h:78
Definition: bufpool.h:45
KDESCRIPTOR Gdtr
Definition: ketypes.h:511
void NTAPI FrLdrBugCheck(ULONG BugCode)
Definition: i386bug.c:260
ULONG HardwareEsp
Definition: ketypes.h:271
void NTAPI FrLdrBugCheckEx(ULONG BugCode, PCHAR File, ULONG Line)
Definition: i386bug.c:231
UINT64 Dr6
Definition: ketypes.h:351
va_start(ap, x)
static void i386PrintFrames(PKTRAP_FRAME TrapFrame)
Definition: i386bug.c:89
UINT64 Dr3
Definition: ketypes.h:350
struct _FRAME FRAME
Definition: ncftp.h:79
#define MachVideoClearScreen(Attr)
Definition: machine.h:92
UINT64 Rdx
Definition: ketypes.h:325
UINT64 Dr1
Definition: ketypes.h:348
#define MachVideoHideShowTextCursor(Show)
Definition: machine.h:104
USHORT SegDs
Definition: ketypes.h:369
ULONG Ecx
Definition: ketypes.h:258
ULONG Eip
Definition: ketypes.h:268
UINT64 Dr0
Definition: ketypes.h:347
UINT64 R11
Definition: ketypes.h:329
PVOID Address
Definition: i386bug.c:9
USHORT SegSs
Definition: ketypes.h:392
ULONG Ebx
Definition: ketypes.h:265
unsigned short USHORT
Definition: pedump.c:61
signed char * PSTR
Definition: retypes.h:7
UINT64 Rsp
Definition: ketypes.h:391
#define SCREEN_ATTR
Definition: i386bug.c:35
#define NULL
Definition: types.h:112
UINT64 Rdi
Definition: ketypes.h:375
#define _vsnprintf
Definition: xmlstorage.h:202
UINT64 ErrorCode
Definition: ketypes.h:380
UINT64 Rip
Definition: ketypes.h:384
void __cdecl _disable(void)
Definition: intrin_arm.h:365
unsigned int ULONG
Definition: retypes.h:1
ULONG_PTR BugCheckInfo[5]
Definition: debug.c:523
static void PrintText(const CHAR *Format,...)
Definition: i386bug.c:79
Definition: File.h:15
UINT64 Dr7
Definition: ketypes.h:352
static const CHAR * i386ExceptionDescriptionText[]
Definition: i386bug.c:12
UINT64 Rcx
Definition: ketypes.h:324
static ULONG i386_ScreenPosY
Definition: i386bug.c:39
static ULONG i386_ScreenPosX
Definition: i386bug.c:38
ULONG ErrCode
Definition: ketypes.h:267
ULONG EFlags
Definition: ketypes.h:389
#define VERSION
Definition: rdesktop.h:45
UINT64 TrapFrame
Definition: ketypes.h:373
ULONG Edx
Definition: ketypes.h:257
ULONG Ebp
Definition: ketypes.h:266
Definition: i386bug.c:6
KDESCRIPTOR Idtr
Definition: ketypes.h:512
static void i386PrintText(CHAR *pszText)
Definition: i386bug.c:42