2 * Acess2 - x86 Architecture
10 #define MAX_BACKTRACE 8 //!< Maximum distance to trace the stack backwards
13 extern void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
14 extern void VM8086_GPF(tRegs *Regs);
15 extern void Threads_Dump(void);
16 extern void Threads_Fault(int Num);
17 extern int GetCPUNum(void);
18 extern void MM_DumpTables(tVAddr, tVAddr);
21 void __stack_chk_fail(void);
22 void ErrorHandler(tRegs *Regs);
23 void Proc_PrintBacktrace(void);
24 void Error_Backtrace(Uint eip, Uint ebp);
25 void StartupPrint(char *Str);
28 const char *csaERROR_NAMES[] = {
29 "Divide By Zero", "Debug", "NMI Exception", "INT3",
30 "INTO Instr - Overflow", "BOUND Instr - Out of Bounds", "Invalid Opcode", "Coprocessor not avaliable",
31 "Double Fault", "Coprocessor Segment Overrun", "Bad TSS", "Segment Not Present",
32 "Stack Fault Exception", "GPF", "#PF", "Reserved",
33 "Floating Point Exception", "Alignment Check Exception", "Machine Check Exception", "Reserved",
34 "Reserved", "Reserved", "Reserved", "Reserved",
35 "Reserved", "Reserved", "Reserved", "Reserved",
36 "Reserved", "Reserved", "Reserved", "Reserved"
41 * \brief Keeps GCC happy
43 void __stack_chk_fail(void)
45 Panic("FATAL ERROR: Stack Check Failed\n");
50 * \fn void ErrorHandler(tRegs *Regs)
51 * \brief General Error Handler
52 * \param Regs Register state at error
54 void ErrorHandler(tRegs *Regs)
58 //if( Regs && !(Regs->int_num == 13 && Regs->eflags & 0x20000) )
59 // __asm__ __volatile__ ("xchg %bx, %bx");
60 //Log_Debug("X86", "Regs = %p", Regs);
61 //Log_Debug("X86", "Error %i at 0x%08x", Regs->int_num, Regs->eip);
63 __asm__ __volatile__ ("cli");
65 // Debug exception (used for single-stepping)
66 if(Regs->int_num == 1)
68 static Uint32 lastEIP = 0;
69 tThread *thread = Proc_GetCurThread();
70 if( Regs->eip == lastEIP )
72 Log("%p(%i %s) IP=%08x", thread, thread->TID, thread->ThreadName, Regs->eip);
78 if(Regs->int_num == 14)
80 __asm__ __volatile__ ("sti"); // Should be OK, TODO: Test
81 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
82 MM_PageFault( cr, Regs->err_code, Regs );
87 if(Regs->int_num == 13 && Regs->eflags & 0x20000)
93 // Check if it's a user mode fault
94 if( Regs->eip < KERNEL_BASE || (Regs->cs & 3) == 3 ) {
95 Log_Warning("Arch", "User Fault - %s, Code: 0x%x",
96 csaERROR_NAMES[Regs->int_num], Regs->err_code);
97 Log_Warning("Arch", "at CS:EIP %04x:%08x",
99 MM_DumpTables(0, KERNEL_BASE);
100 switch( Regs->int_num )
103 case 0: Threads_Fault(FAULT_DIV0); break;
105 case 6: Threads_Fault(FAULT_OPCODE); break;
107 case 13: Threads_Fault(FAULT_ACCESS); break;
108 // Floating Point Exception
109 case 16: Threads_Fault(FAULT_FLOAT); break;
111 default: Threads_Fault(FAULT_MISC); break;
118 LogF("CPU %i Error %i - %s, Code: 0x%x - At %08x",
120 Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code,
123 //Warning("CPU Error %i - %s, Code: 0x%x",
124 // Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code);
125 //Warning(" CS:EIP = 0x%04x:%08x", Regs->cs, Regs->eip);
126 __ASM__ ("xchg %bx, %bx");
128 Warning(" SS:ESP = 0x0010:%08x", (Uint)Regs+sizeof(tRegs));
130 Warning(" SS:ESP = 0x%04x:%08x", Regs->ss, Regs->esp);
131 Warning(" EFLAGS = 0x%08x", Regs->eflags);
132 Warning(" EAX %08x ECX %08x EDX %08x EBX %08x",
133 Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
134 Warning(" ESP %08x EBP %08x ESI %08x EDI %08x",
135 Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
136 Warning(" DS %04x ES %04x FS %04x GS %04x",
137 Regs->ds, Regs->es, Regs->fs, Regs->gs);
140 __asm__ __volatile__ ("mov %%cr0, %0":"=r"(cr));
141 Warning(" CR0 0x%08x", cr);
142 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
143 Warning(" CR2 0x%08x", cr);
144 __asm__ __volatile__ ("mov %%cr3, %0":"=r"(cr));
145 Warning(" CR3 0x%08x", cr);
147 switch( Regs->int_num )
150 Warning(" Offending bytes: %02x %02x %02x %02x",
151 *(Uint8*)Regs->eip+0, *(Uint8*)Regs->eip+1,
152 *(Uint8*)Regs->eip+2, *(Uint8*)Regs->eip+3);
156 // Print Stack Backtrace
157 Error_Backtrace(Regs->eip, Regs->ebp);
159 // Dump running threads
162 for(;;) __asm__ __volatile__ ("hlt");
165 void Proc_PrintBacktrace(void)
168 __asm__ __volatile__ ("mov %%ebp, %0" : "=r" (ebp));
169 Error_Backtrace( *(Uint32*)(ebp+4), *(Uint32*)ebp );
173 * \fn void Error_Backtrace(Uint eip, Uint ebp)
174 * \brief Unrolls the stack to trace execution
175 * \param eip Current Instruction Pointer
176 * \param ebp Current Base Pointer (Stack Frame)
178 void Error_Backtrace(Uint eip, Uint ebp)
184 //if(eip < 0xC0000000 && eip > 0x1000)
186 // LogF("Backtrace: User - 0x%x\n", eip);
192 LogF("Backtrace: Data Area - 0x%x\n", eip);
198 LogF("Backtrace: Kernel Module - 0x%x\n", eip);
202 //str = Debug_GetSymbol(eip, &delta);
204 LogF("Backtrace: 0x%x", eip);
206 LogF("Backtrace: %s+0x%x", str, delta);
207 if(!MM_GetPhysAddr(ebp))
209 LogF("\nBacktrace: Invalid EBP, stopping\n");
214 while( MM_GetPhysAddr(ebp) && i < MAX_BACKTRACE )
216 //str = Debug_GetSymbol(*(Uint*)(ebp+4), &delta);
218 LogF(" >> 0x%x", *(Uint*)(ebp+4));
220 LogF(" >> %s+0x%x", str, delta);
228 * \fn void StartupPrint(char *Str)
229 * \brief Str String to print
230 * \note WHY IS THIS HERE?!?!
232 void StartupPrint(char *Str)
234 Uint16 *buf = (void*)0xC00B8000;
239 buf[line*80 + i++] = *Str | 0x0700;
243 // Clear the rest of the line
245 buf[line*80 + i++] = 0x0720;
251 memcpy(buf, &buf[80], 80*24*2);
252 memset(&buf[80*24], 0, 80*2);
257 EXPORT(__stack_chk_fail);