2 * Acess2 - x86 Architecture
11 #define MAX_BACKTRACE 8 //!< Maximum distance to trace the stack backwards
14 extern void MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
15 extern void VM8086_GPF(tRegs *Regs);
16 extern void Threads_Dump(void);
17 extern void Threads_Fault(int Num);
18 extern int GetCPUNum(void);
19 extern void MM_DumpTables(tVAddr, tVAddr);
20 extern void Proc_EnableSSE(void);
21 extern void Proc_RestoreSSE(Uint32 Data);
24 void __stack_chk_fail(void);
25 void ErrorHandler(tRegs *Regs);
26 void Proc_PrintBacktrace(void);
27 void Error_Backtrace(Uint eip, Uint ebp);
28 void StartupPrint(char *Str);
31 const char *csaERROR_NAMES[] = {
32 "Divide By Zero", "Debug", "NMI Exception", "INT3",
33 "INTO Instr - Overflow", "BOUND Instr - Out of Bounds", "Invalid Opcode", "Coprocessor not avaliable",
34 "Double Fault", "Coprocessor Segment Overrun", "Bad TSS", "Segment Not Present",
35 "Stack Fault Exception", "GPF", "#PF", "Reserved",
36 "Floating Point Exception", "Alignment Check Exception", "Machine Check Exception", "Reserved",
37 "Reserved", "Reserved", "Reserved", "Reserved",
38 "Reserved", "Reserved", "Reserved", "Reserved",
39 "Reserved", "Reserved", "Reserved", "Reserved"
44 * \brief Keeps GCC happy
46 void __stack_chk_fail(void)
48 Panic("FATAL ERROR: Stack Check Failed\n");
53 * \fn void ErrorHandler(tRegs *Regs)
54 * \brief General Error Handler
55 * \param Regs Register state at error
57 void ErrorHandler(tRegs *Regs)
61 //if( Regs && !(Regs->int_num == 13 && Regs->eflags & 0x20000) )
62 // __asm__ __volatile__ ("xchg %bx, %bx");
63 //Log_Debug("X86", "Regs = %p", Regs);
64 //Log_Debug("X86", "Error %i at 0x%08x", Regs->int_num, Regs->eip);
66 __asm__ __volatile__ ("cli");
68 // Debug exception (used for single-stepping)
69 if(Regs->int_num == 1)
71 static Uint32 lastEIP = 0;
72 tThread *thread = Proc_GetCurThread();
73 if( Regs->eip == lastEIP )
75 Log("%p(%i %s) IP=%08x", thread, thread->TID, thread->ThreadName, Regs->eip);
81 if(Regs->int_num == 14)
83 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
84 MM_PageFault( cr, Regs->err_code, Regs );
88 // #NM - Coprocessor unavaliable
89 if(Regs->int_num == 7)
91 tThread *thread = Proc_GetCurThread();
92 if(!thread->SavedState.bSSEModified)
95 if(!thread->SavedState.SSE)
96 thread->SavedState.SSE = malloc(sizeof(tSSEState) + 0xF);
98 Proc_RestoreSSE( ((Uint)thread->SavedState.SSE + 0xF) & ~0xF );
99 thread->SavedState.bSSEModified = 1;
100 __asm__ __volatile__ ("sti");
103 // oops, SSE enabled but a #NM, bad news
107 if(Regs->int_num == 13 && Regs->eflags & 0x20000)
113 // Check if it's a user mode fault
114 if( (Regs->cs & 3) == 3 ) {
115 Log_Warning("Arch", "User Fault - %s, Code: 0x%x",
116 csaERROR_NAMES[Regs->int_num], Regs->err_code);
117 Log_Warning("Arch", "at CS:EIP %04x:%08x",
118 Regs->cs, Regs->eip);
119 Error_Backtrace(Regs->eip, Regs->ebp);
121 MM_DumpTables(0, KERNEL_BASE);
122 switch( Regs->int_num )
125 case 0: Threads_Fault(FAULT_DIV0); break;
127 case 6: Threads_Fault(FAULT_OPCODE); break;
129 case 13: Threads_Fault(FAULT_ACCESS); break;
130 // Floating Point Exception
131 case 16: Threads_Fault(FAULT_FLOAT); break;
133 default: Threads_Fault(FAULT_MISC); break;
140 LogF("CPU %i Error %i - %s, Code: 0x%x - At %08x\n",
142 Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code,
145 //Warning("CPU Error %i - %s, Code: 0x%x",
146 // Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code);
147 //Warning(" CS:EIP = 0x%04x:%08x", Regs->cs, Regs->eip);
148 __ASM__ ("xchg %bx, %bx");
150 Warning(" SS:ESP = 0x0010:%08x", (Uint)Regs+sizeof(tRegs));
152 Warning(" SS:ESP = 0x%04x:%08x", Regs->ss, Regs->esp);
153 Warning(" EFLAGS = 0x%08x", Regs->eflags);
154 Warning(" EAX %08x ECX %08x EDX %08x EBX %08x",
155 Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
156 Warning(" ESP %08x EBP %08x ESI %08x EDI %08x",
157 Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
158 Warning(" DS %04x ES %04x FS %04x GS %04x",
159 Regs->ds, Regs->es, Regs->fs, Regs->gs);
162 __asm__ __volatile__ ("mov %%cr0, %0":"=r"(cr));
163 Warning(" CR0 0x%08x", cr);
164 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
165 Warning(" CR2 0x%08x", cr);
166 __asm__ __volatile__ ("mov %%cr3, %0":"=r"(cr));
167 Warning(" CR3 0x%08x", cr);
169 switch( Regs->int_num )
172 Warning(" Offending bytes: %02x %02x %02x %02x",
173 *(Uint8*)(Regs->eip+0), *(Uint8*)(Regs->eip+1),
174 *(Uint8*)(Regs->eip+2), *(Uint8*)(Regs->eip+3));
178 // Print Stack Backtrace
179 Error_Backtrace(Regs->eip, Regs->ebp);
181 // Dump running threads
184 for(;;) __asm__ __volatile__ ("hlt");
187 void Proc_PrintBacktrace(void)
190 __asm__ __volatile__ ("mov %%ebp, %0" : "=r" (ebp));
191 Error_Backtrace( *(Uint32*)(ebp+4), *(Uint32*)ebp );
195 * \fn void Error_Backtrace(Uint eip, Uint ebp)
196 * \brief Unrolls the stack to trace execution
197 * \param eip Current Instruction Pointer
198 * \param ebp Current Base Pointer (Stack Frame)
200 void Error_Backtrace(Uint eip, Uint ebp)
206 //if(eip < 0xC0000000 && eip > 0x1000)
208 // LogF("Backtrace: User - 0x%x\n", eip);
215 LogF("Backtrace: Data Area - 0x%x\n", eip);
221 LogF("Backtrace: Kernel Module - 0x%x\n", eip);
226 //str = Debug_GetSymbol(eip, &delta);
228 LogF("Backtrace: 0x%x", eip);
230 // LogF("Backtrace: %s+0x%x", str, delta);
231 if(!MM_GetPhysAddr((void*)ebp))
233 LogF("\nBacktrace: Invalid EBP %p, stopping\n", ebp);
238 while( MM_GetPhysAddr((void*)ebp) && i < MAX_BACKTRACE )
240 if( ebp >= MM_KERNEL_STACKS_END ) break;
241 //str = Debug_GetSymbol(*(Uint*)(ebp+4), &delta);
243 LogF(" >> 0x%x", *(Uint*)(ebp+4));
245 // LogF(" >> %s+0x%x", str, delta);
253 * \fn void StartupPrint(char *Str)
254 * \brief Str String to print
255 * \note WHY IS THIS HERE?!?!
257 void StartupPrint(char *Str)
259 Uint16 *buf = (void*)0xC00B8000;
264 buf[line*80 + i++] = *Str | 0x0700;
268 // Clear the rest of the line
270 buf[line*80 + i++] = 0x0720;
276 memcpy(buf, &buf[80], 80*24*2);
277 memset(&buf[80*24], 0, 80*2);
282 EXPORT(__stack_chk_fail);