Kernel/VFS - Fixed missuse of strcmp that caused userland compilations to break
[tpg/acess2.git] / KernelLand / Kernel / arch / x86 / 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                 MM_DumpTables(0, KERNEL_BASE);
120                 switch( Regs->int_num )
121                 {
122                 // Division by Zero
123                 case  0:        Threads_Fault(FAULT_DIV0);      break;
124                 // Invalid opcode
125                 case  6:        Threads_Fault(FAULT_OPCODE);    break;
126                 // GPF
127                 case 13:        Threads_Fault(FAULT_ACCESS);    break;
128                 // Floating Point Exception
129                 case 16:        Threads_Fault(FAULT_FLOAT);     break;
130                 
131                 default:        Threads_Fault(FAULT_MISC);      break;
132                 }
133                 return ;
134         }
135         
136         Debug_KernelPanic();
137         
138         LogF("CPU %i Error %i - %s, Code: 0x%x - At %08x\n",
139                 GetCPUNum(),
140                 Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code,
141                 Regs->eip);
142         
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");
147         if(Regs->cs == 0x08)
148                 Warning(" SS:ESP = 0x0010:%08x", (Uint)Regs+sizeof(tRegs));
149         else
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);
158         
159         // Control Registers
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);
166         
167         switch( Regs->int_num )
168         {
169         case 6: // #UD
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));
173                 break;
174         }
175         
176         // Print Stack Backtrace
177         Error_Backtrace(Regs->eip, Regs->ebp);
178         
179         // Dump running threads
180         Threads_Dump();
181         
182         for(;;) __asm__ __volatile__ ("hlt");
183 }
184
185 void Proc_PrintBacktrace(void)
186 {
187         Uint32  ebp;
188         __asm__ __volatile__ ("mov %%ebp, %0" : "=r" (ebp));
189         Error_Backtrace( *(Uint32*)(ebp+4), *(Uint32*)ebp );
190 }
191
192 /**
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)
197  */
198 void Error_Backtrace(Uint eip, Uint ebp)
199 {
200          int    i = 0;
201 //      Uint    delta = 0;
202 //      char    *str = NULL;
203         
204         //if(eip < 0xC0000000 && eip > 0x1000)
205         //{
206         //      LogF("Backtrace: User - 0x%x\n", eip);
207         //      return;
208         //}
209
210         #if 0   
211         if(eip > 0xE0000000)
212         {
213                 LogF("Backtrace: Data Area - 0x%x\n", eip);
214                 return;
215         }
216         
217         if(eip > 0xC8000000)
218         {
219                 LogF("Backtrace: Kernel Module - 0x%x\n", eip);
220                 return;
221         }
222         #endif  
223
224         //str = Debug_GetSymbol(eip, &delta);
225 //      if(str == NULL)
226                 LogF("Backtrace: 0x%x", eip);
227 //      else
228 //              LogF("Backtrace: %s+0x%x", str, delta);
229         if(!MM_GetPhysAddr(ebp))
230         {
231                 LogF("\nBacktrace: Invalid EBP, stopping\n");
232                 return;
233         }
234         
235         
236         while( MM_GetPhysAddr(ebp) && i < MAX_BACKTRACE )
237         {
238                 if( ebp >= MM_KERNEL_STACKS_END )       break;
239                 //str = Debug_GetSymbol(*(Uint*)(ebp+4), &delta);
240 //              if(str == NULL)
241                         LogF(" >> 0x%x", *(Uint*)(ebp+4));
242 //              else
243 //                      LogF(" >> %s+0x%x", str, delta);
244                 ebp = *(Uint*)ebp;
245                 i++;
246         }
247         LogF("\r\n");
248 }
249
250 /**
251  * \fn void StartupPrint(char *Str)
252  * \brief Str   String to print
253  * \note WHY IS THIS HERE?!?!
254  */
255 void StartupPrint(char *Str)
256 {
257         Uint16  *buf = (void*)0xC00B8000;
258          int    i = 0;
259         static int      line = 0;
260         while(*Str)
261         {
262                 buf[line*80 + i++] = *Str | 0x0700;
263                 Str ++;
264         }
265         
266         // Clear the rest of the line
267         while(i < 80)
268                 buf[line*80 + i++] = 0x0720;
269         
270         line ++;
271         if(line == 25)
272         {
273                 line --;
274                 memcpy(buf, &buf[80], 80*24*2);
275                 memset(&buf[80*24], 0, 80*2);
276         }
277 }
278
279 // === EXPORTS ===
280 EXPORT(__stack_chk_fail);

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