Just a little cleanup
[tpg/acess2.git] / KernelLand / Kernel / arch / x86_64 / errors.c
1 /*
2  * Acess2 x86_64 Project
3  * - Error Handling
4  */
5 #include <acess.h>
6 #include <proc.h>
7 #include <mm_virt.h>
8 #include <threads_int.h>        // Needed for SSE handling
9
10 #define MAX_BACKTRACE   6
11
12 // === IMPORTS ===
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);
17
18 // === PROTOTYPES ===
19 void    Error_Handler(tRegs *Regs);
20
21 // === GLOBALS ==
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"
31         };
32
33 // === CODE ===
34 void Error_Handler(tRegs *Regs)
35 {
36         Uint    cr;
37
38         if( Regs->IntNum == 7 )
39         {
40                 tThread *thread = Proc_GetCurThread();
41                 if(!thread->SavedState.bSSEModified)
42                 {
43                         Proc_EnableSSE();
44                         if(!thread->SavedState.SSE)
45                                 thread->SavedState.SSE = malloc(sizeof(tSSEState) + 0xF);
46                         else
47                                 Proc_RestoreSSE( ((Uint)thread->SavedState.SSE + 0xF) & ~0xF );
48                         thread->SavedState.bSSEModified = 1;
49 //                      __asm__ __volatile__ ("sti");
50                         return ;
51                 }
52                 // oops, SSE enabled but a #NM, bad news
53         }
54         
55         if( Regs->IntNum == 14 ) {
56                 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
57                 if( MM_PageFault(cr, Regs->ErrorCode, Regs) == 0 )
58                         return ;
59         }
60         else {
61                 Debug_KernelPanic();
62
63                 Error_Backtrace(Regs->RIP, Regs->RBP);
64         }
65         
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);
71         
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);
81         
82         
83         // Control Registers
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);
92         
93         switch( Regs->IntNum )
94         {
95         case 6: // #UD
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)
99                         );
100                 break;
101         }
102         
103         __asm__ __volatile__ ("cli");
104         for(;;)
105                 __asm__ __volatile__ ("hlt");
106 }
107
108 /**
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)
113  */
114 void Error_Backtrace(Uint IP, Uint BP)
115 {
116          int    i = 0;
117         
118         //if(eip < 0xC0000000 && eip > 0x1000)
119         //{
120         //      LogF("Backtrace: User - 0x%x\n", eip);
121         //      return;
122         //}
123         
124         if( IP > USER_MAX && IP < MM_KERNEL_CODE
125          && (MM_MODULE_MIN > IP || IP > MM_MODULE_MAX)
126                 )
127         {
128                 LogF("Backtrace: Data Area - %p\n", IP);
129                 return;
130         }
131         
132         //str = Debug_GetSymbol(eip, &delta);
133         //if(str == NULL)
134                 LogF("Backtrace: %p", IP);
135         //else
136         //      LogF("Backtrace: %s+0x%x", str, delta);
137         if( !MM_GetPhysAddr(BP) )
138         {
139                 LogF("\nBacktrace: Invalid BP, stopping\n");
140                 return;
141         }
142         
143         
144         while( MM_GetPhysAddr(BP) && MM_GetPhysAddr(BP+8+7) && i < MAX_BACKTRACE )
145         {
146                 //str = Debug_GetSymbol(*(Uint*)(ebp+4), &delta);
147                 //if(str == NULL)
148                         LogF(" >> 0x%llx", ((Uint*)BP)[1]);
149                 //else
150                 //      LogF(" >> %s+0x%x", str, delta);
151                 BP = ((Uint*)BP)[0];
152                 i++;
153         }
154         LogF("\n");
155 }

UCC git Repository :: git.ucc.asn.au