2 * Acess2 x86_64 Project
8 #include <threads_int.h> // Needed for SSE handling
10 #define MAX_BACKTRACE 6
13 extern int MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
14 extern void Error_Backtrace(Uint IP, Uint BP);
15 extern void Proc_EnableSSE(void);
16 extern void Proc_RestoreSSE(Uint32 Data);
19 void Error_Handler(tRegs *Regs);
22 const char * const csaERROR_NAMES[] = {
23 "Divide By Zero", "Debug", "NMI Exception", "INT3",
24 "INTO", "Out of Bounds", "Invalid Opcode", "Coprocessor not avaliable",
25 "Double Fault", "Coprocessor Segment Overrun", "Bad TSS", "Segment Not Present",
26 "Stack Fault Exception", "GPF", "#PF", "Reserved",
27 "Floating Point Exception", "Alignment Check Exception", "Machine Check Exception", "Reserved",
28 "Reserved", "Reserved", "Reserved", "Reserved",
29 "Reserved", "Reserved", "Reserved", "Reserved",
30 "Reserved", "Reserved", "Reserved", "Reserved"
34 void Error_Handler(tRegs *Regs)
38 if( Regs->IntNum == 7 )
40 tThread *thread = Proc_GetCurThread();
41 if(!thread->SavedState.bSSEModified)
44 if(!thread->SavedState.SSE)
45 thread->SavedState.SSE = malloc(sizeof(tSSEState) + 0xF);
47 Proc_RestoreSSE( ((Uint)thread->SavedState.SSE + 0xF) & ~0xF );
48 thread->SavedState.bSSEModified = 1;
49 // __asm__ __volatile__ ("sti");
52 // oops, SSE enabled but a #NM, bad news
55 if( Regs->IntNum == 14 ) {
56 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
57 if( MM_PageFault(cr, Regs->ErrorCode, Regs) == 0 )
63 Error_Backtrace(Regs->RIP, Regs->RBP);
66 Log("CPU Error %x, Code: 0x%x", Regs->IntNum, Regs->ErrorCode);
67 Log(" - %s", csaERROR_NAMES[Regs->IntNum]);
68 Log(" CS:RIP = 0x%04x:%016llx", Regs->CS, Regs->RIP);
69 Log(" SS:RSP = 0x%04x:%016llx", Regs->SS, Regs->RSP);
70 Log(" RFLAGS = 0x%016llx", Regs->RFlags);
72 Log(" RAX %016llx RCX %016llx RDX %016llx RBX %016llx",
73 Regs->RAX, Regs->RCX, Regs->RDX, Regs->RBX);
74 Log(" RSP %016llx RBP %016llx RSI %016llx RDI %016llx",
75 Regs->RSP, Regs->RBP, Regs->RSP, Regs->RDI);
76 Log(" R8 %016llx R9 %016llx R10 %016llx R11 %016llx",
77 Regs->R8, Regs->R9, Regs->R10, Regs->R11);
78 Log(" R12 %016llx R13 %016llx R14 %016llx R15 %016llx",
79 Regs->R12, Regs->R13, Regs->R14, Regs->R15);
80 Log(" FS %04x GS %04x", Regs->FS, Regs->GS);
84 __asm__ __volatile__ ("mov %%cr0, %0":"=r"(cr));
85 Warning(" CR0 0x%08x", cr);
86 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
87 Warning(" CR2 0x%016llx", cr);
88 __asm__ __volatile__ ("mov %%cr3, %0":"=r"(cr));
89 Warning(" CR3 0x%016llx", cr);
90 __asm__ __volatile__ ("mov %%cr4, %0":"=r"(cr));
91 Warning(" CR4 0x%08x", cr);
93 switch( Regs->IntNum )
96 Warning(" Offending bytes: %02x %02x %02x %02x",
97 *(Uint8*)(Regs->RIP+0), *(Uint8*)(Regs->RIP+1),
98 *(Uint8*)(Regs->RIP+2), *(Uint8*)(Regs->RIP+3)
103 __asm__ __volatile__ ("cli");
105 __asm__ __volatile__ ("hlt");
109 * \fn void Error_Backtrace(Uint eip, Uint ebp)
110 * \brief Unrolls the stack to trace execution
111 * \param eip Current Instruction Pointer
112 * \param ebp Current Base Pointer (Stack Frame)
114 void Error_Backtrace(Uint IP, Uint BP)
118 //if(eip < 0xC0000000 && eip > 0x1000)
120 // LogF("Backtrace: User - 0x%x\n", eip);
124 if( IP > USER_MAX && IP < MM_KERNEL_CODE
125 && (MM_MODULE_MIN > IP || IP > MM_MODULE_MAX)
128 LogF("Backtrace: Data Area - %p\n", IP);
132 //str = Debug_GetSymbol(eip, &delta);
134 LogF("Backtrace: %p", IP);
136 // LogF("Backtrace: %s+0x%x", str, delta);
137 if( !MM_GetPhysAddr(BP) )
139 LogF("\nBacktrace: Invalid BP, stopping\n");
144 while( MM_GetPhysAddr(BP) && MM_GetPhysAddr(BP+8+7) && i < MAX_BACKTRACE )
146 //str = Debug_GetSymbol(*(Uint*)(ebp+4), &delta);
148 LogF(" >> 0x%llx", ((Uint*)BP)[1]);
150 // LogF(" >> %s+0x%x", str, delta);