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 MM_DumpTables(0, KERNEL_BASE);
120 switch( Regs->int_num )
123 case 0: Threads_Fault(FAULT_DIV0); break;
125 case 6: Threads_Fault(FAULT_OPCODE); break;
127 case 13: Threads_Fault(FAULT_ACCESS); break;
128 // Floating Point Exception
129 case 16: Threads_Fault(FAULT_FLOAT); break;
131 default: Threads_Fault(FAULT_MISC); break;
138 LogF("CPU %i Error %i - %s, Code: 0x%x - At %08x\n",
140 Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code,
143 //Warning("CPU Error %i - %s, Code: 0x%x",
144 // Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code);
145 //Warning(" CS:EIP = 0x%04x:%08x", Regs->cs, Regs->eip);
146 __ASM__ ("xchg %bx, %bx");
148 Warning(" SS:ESP = 0x0010:%08x", (Uint)Regs+sizeof(tRegs));
150 Warning(" SS:ESP = 0x%04x:%08x", Regs->ss, Regs->esp);
151 Warning(" EFLAGS = 0x%08x", Regs->eflags);
152 Warning(" EAX %08x ECX %08x EDX %08x EBX %08x",
153 Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
154 Warning(" ESP %08x EBP %08x ESI %08x EDI %08x",
155 Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
156 Warning(" DS %04x ES %04x FS %04x GS %04x",
157 Regs->ds, Regs->es, Regs->fs, Regs->gs);
160 __asm__ __volatile__ ("mov %%cr0, %0":"=r"(cr));
161 Warning(" CR0 0x%08x", cr);
162 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
163 Warning(" CR2 0x%08x", cr);
164 __asm__ __volatile__ ("mov %%cr3, %0":"=r"(cr));
165 Warning(" CR3 0x%08x", cr);
167 switch( Regs->int_num )
170 Warning(" Offending bytes: %02x %02x %02x %02x",
171 *(Uint8*)(Regs->eip+0), *(Uint8*)(Regs->eip+1),
172 *(Uint8*)(Regs->eip+2), *(Uint8*)(Regs->eip+3));
176 // Print Stack Backtrace
177 Error_Backtrace(Regs->eip, Regs->ebp);
179 // Dump running threads
182 for(;;) __asm__ __volatile__ ("hlt");
185 void Proc_PrintBacktrace(void)
188 __asm__ __volatile__ ("mov %%ebp, %0" : "=r" (ebp));
189 Error_Backtrace( *(Uint32*)(ebp+4), *(Uint32*)ebp );
193 * \fn void Error_Backtrace(Uint eip, Uint ebp)
194 * \brief Unrolls the stack to trace execution
195 * \param eip Current Instruction Pointer
196 * \param ebp Current Base Pointer (Stack Frame)
198 void Error_Backtrace(Uint eip, Uint ebp)
204 //if(eip < 0xC0000000 && eip > 0x1000)
206 // LogF("Backtrace: User - 0x%x\n", eip);
213 LogF("Backtrace: Data Area - 0x%x\n", eip);
219 LogF("Backtrace: Kernel Module - 0x%x\n", eip);
224 //str = Debug_GetSymbol(eip, &delta);
226 LogF("Backtrace: 0x%x", eip);
228 // LogF("Backtrace: %s+0x%x", str, delta);
229 if(!MM_GetPhysAddr(ebp))
231 LogF("\nBacktrace: Invalid EBP, stopping\n");
236 while( MM_GetPhysAddr(ebp) && i < MAX_BACKTRACE )
238 if( ebp >= MM_KERNEL_STACKS_END ) break;
239 //str = Debug_GetSymbol(*(Uint*)(ebp+4), &delta);
241 LogF(" >> 0x%x", *(Uint*)(ebp+4));
243 // LogF(" >> %s+0x%x", str, delta);
251 * \fn void StartupPrint(char *Str)
252 * \brief Str String to print
253 * \note WHY IS THIS HERE?!?!
255 void StartupPrint(char *Str)
257 Uint16 *buf = (void*)0xC00B8000;
262 buf[line*80 + i++] = *Str | 0x0700;
266 // Clear the rest of the line
268 buf[line*80 + i++] = 0x0720;
274 memcpy(buf, &buf[80], 80*24*2);
275 memset(&buf[80*24], 0, 80*2);
280 EXPORT(__stack_chk_fail);