8dc90b0186499f8bb041ec87541811033f1cc837
[tpg/acess2.git] / errors.c
1 /*
2  * Acess2 - x86 Architecture
3  * arch/x86/errors.c
4  * - CPU Error Handler
5  */
6 #include <acess.h>
7 #include <proc.h>
8 #include <mm_virt.h>
9
10 // === CONSTANTS ===
11 #define MAX_BACKTRACE   8       //!< Maximum distance to trace the stack backwards
12
13 // === IMPORTS ===
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);
22
23 // === PROTOTYPES ===
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);
29
30 // === GLOBALS ===
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"
40         };
41
42 // === CODE ===
43 /**
44  * \brief Keeps GCC happy
45  */
46 void __stack_chk_fail(void)
47 {
48         Panic("FATAL ERROR: Stack Check Failed\n");
49         for(;;);
50 }
51
52 /**
53  * \fn void ErrorHandler(tRegs *Regs)
54  * \brief General Error Handler
55  * \param Regs  Register state at error
56  */
57 void ErrorHandler(tRegs *Regs)
58 {
59         Uint    cr;
60         
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);
65         
66         __asm__ __volatile__ ("cli");
67         
68         // Debug exception (used for single-stepping)
69         if(Regs->int_num == 1)
70         {
71                 static Uint32   lastEIP = 0;
72                 tThread *thread = Proc_GetCurThread();
73                 if( Regs->eip == lastEIP )
74                         return;
75                 Log("%p(%i %s) IP=%08x", thread, thread->TID, thread->ThreadName, Regs->eip);
76                 lastEIP = Regs->eip;
77                 return ;
78         }
79         
80         // Page Fault
81         if(Regs->int_num == 14)
82         {
83                 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
84                 MM_PageFault( cr, Regs->err_code, Regs );
85                 return ;
86         }
87
88         // #NM - Coprocessor unavaliable
89         if(Regs->int_num == 7)
90         {
91                 tThread *thread = Proc_GetCurThread();
92                 if(!thread->SavedState.bSSEModified)
93                 {
94                         Proc_EnableSSE();
95                         if(!thread->SavedState.SSE)
96                                 thread->SavedState.SSE = malloc(sizeof(tSSEState) + 0xF);
97                         else
98                                 Proc_RestoreSSE( ((Uint)thread->SavedState.SSE + 0xF) & ~0xF );
99                         thread->SavedState.bSSEModified = 1;
100                         __asm__ __volatile__ ("sti");
101                         return ;
102                 }
103                 // oops, SSE enabled but a #NM, bad news
104         }
105         
106         // VM8086 GPF
107         if(Regs->int_num == 13 && Regs->eflags & 0x20000)
108         {
109                 VM8086_GPF(Regs);
110                 return ;
111         }
112         
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);
120                 
121                 MM_DumpTables(0, KERNEL_BASE);
122                 switch( Regs->int_num )
123                 {
124                 // Division by Zero
125                 case  0:        Threads_Fault(FAULT_DIV0);      break;
126                 // Invalid opcode
127                 case  6:        Threads_Fault(FAULT_OPCODE);    break;
128                 // GPF
129                 case 13:        Threads_Fault(FAULT_ACCESS);    break;
130                 // Floating Point Exception
131                 case 16:        Threads_Fault(FAULT_FLOAT);     break;
132                 
133                 default:        Threads_Fault(FAULT_MISC);      break;
134                 }
135                 return ;
136         }
137         
138         Debug_KernelPanic();
139         
140         LogF("CPU %i Error %i - %s, Code: 0x%x - At %08x\n",
141                 GetCPUNum(),
142                 Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code,
143                 Regs->eip);
144         
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");
149         if(Regs->cs == 0x08)
150                 Warning(" SS:ESP = 0x0010:%08x", (Uint)Regs+sizeof(tRegs));
151         else
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);
160         
161         // Control Registers
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);
168         
169         switch( Regs->int_num )
170         {
171         case 6: // #UD
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));
175                 break;
176         }
177         
178         // Print Stack Backtrace
179         Error_Backtrace(Regs->eip, Regs->ebp);
180         
181         // Dump running threads
182         Threads_Dump();
183         
184         for(;;) __asm__ __volatile__ ("hlt");
185 }
186
187 void Proc_PrintBacktrace(void)
188 {
189         Uint32  ebp;
190         __asm__ __volatile__ ("mov %%ebp, %0" : "=r" (ebp));
191         Error_Backtrace( *(Uint32*)(ebp+4), *(Uint32*)ebp );
192 }
193
194 /**
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)
199  */
200 void Error_Backtrace(Uint eip, Uint ebp)
201 {
202          int    i = 0;
203 //      Uint    delta = 0;
204 //      char    *str = NULL;
205         
206         //if(eip < 0xC0000000 && eip > 0x1000)
207         //{
208         //      LogF("Backtrace: User - 0x%x\n", eip);
209         //      return;
210         //}
211
212         #if 0   
213         if(eip > 0xE0000000)
214         {
215                 LogF("Backtrace: Data Area - 0x%x\n", eip);
216                 return;
217         }
218         
219         if(eip > 0xC8000000)
220         {
221                 LogF("Backtrace: Kernel Module - 0x%x\n", eip);
222                 return;
223         }
224         #endif  
225
226         //str = Debug_GetSymbol(eip, &delta);
227 //      if(str == NULL)
228                 LogF("Backtrace: 0x%x", eip);
229 //      else
230 //              LogF("Backtrace: %s+0x%x", str, delta);
231         if(!MM_GetPhysAddr((void*)ebp))
232         {
233                 LogF("\nBacktrace: Invalid EBP %p, stopping\n", ebp);
234                 return;
235         }
236         
237         
238         while( MM_GetPhysAddr((void*)ebp) && i < MAX_BACKTRACE )
239         {
240                 if( ebp >= MM_KERNEL_STACKS_END )       break;
241                 //str = Debug_GetSymbol(*(Uint*)(ebp+4), &delta);
242 //              if(str == NULL)
243                         LogF(" >> 0x%x", *(Uint*)(ebp+4));
244 //              else
245 //                      LogF(" >> %s+0x%x", str, delta);
246                 ebp = *(Uint*)ebp;
247                 i++;
248         }
249         LogF("\r\n");
250 }
251
252 /**
253  * \fn void StartupPrint(char *Str)
254  * \brief Str   String to print
255  * \note WHY IS THIS HERE?!?!
256  */
257 void StartupPrint(char *Str)
258 {
259         Uint16  *buf = (void*)0xC00B8000;
260          int    i = 0;
261         static int      line = 0;
262         while(*Str)
263         {
264                 buf[line*80 + i++] = *Str | 0x0700;
265                 Str ++;
266         }
267         
268         // Clear the rest of the line
269         while(i < 80)
270                 buf[line*80 + i++] = 0x0720;
271         
272         line ++;
273         if(line == 25)
274         {
275                 line --;
276                 memcpy(buf, &buf[80], 80*24*2);
277                 memset(&buf[80*24], 0, 80*2);
278         }
279 }
280
281 // === EXPORTS ===
282 EXPORT(__stack_chk_fail);

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