Kernel/armv7 - Added IRQ stack, changed IRQs to be disabled until after handling
[tpg/acess2.git] / 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
9 // === CONSTANTS ===
10 #define MAX_BACKTRACE   8       //!< Maximum distance to trace the stack backwards
11
12 // === IMPORTS ===
13 extern void     MM_PageFault(Uint Addr, Uint ErrorCode, tRegs *Regs);
14 extern void     VM8086_GPF(tRegs *Regs);
15 extern void     Threads_Dump(void);
16 extern void     Threads_Fault(int Num);
17 extern int      GetCPUNum(void);
18 extern void     MM_DumpTables(tVAddr, tVAddr);
19
20 // === PROTOTYPES ===
21 void    __stack_chk_fail(void);
22 void    ErrorHandler(tRegs *Regs);
23 void    Proc_PrintBacktrace(void);
24 void    Error_Backtrace(Uint eip, Uint ebp);
25 void    StartupPrint(char *Str);
26
27 // === GLOBALS ===
28 const char *csaERROR_NAMES[] = {
29         "Divide By Zero", "Debug", "NMI Exception", "INT3",
30         "INTO Instr - Overflow", "BOUND Instr - Out of Bounds", "Invalid Opcode", "Coprocessor not avaliable",
31         "Double Fault", "Coprocessor Segment Overrun", "Bad TSS", "Segment Not Present",
32         "Stack Fault Exception", "GPF", "#PF", "Reserved",
33         "Floating Point Exception", "Alignment Check Exception", "Machine Check Exception",     "Reserved",
34         "Reserved", "Reserved", "Reserved", "Reserved",
35         "Reserved", "Reserved", "Reserved", "Reserved",
36         "Reserved", "Reserved", "Reserved", "Reserved"
37         };
38
39 // === CODE ===
40 /**
41  * \brief Keeps GCC happy
42  */
43 void __stack_chk_fail(void)
44 {
45         Panic("FATAL ERROR: Stack Check Failed\n");
46         for(;;);
47 }
48
49 /**
50  * \fn void ErrorHandler(tRegs *Regs)
51  * \brief General Error Handler
52  * \param Regs  Register state at error
53  */
54 void ErrorHandler(tRegs *Regs)
55 {
56         Uint    cr;
57         
58         //if( Regs && !(Regs->int_num == 13 && Regs->eflags & 0x20000) )
59         //      __asm__ __volatile__ ("xchg %bx, %bx");
60         //Log_Debug("X86", "Regs = %p", Regs);
61         //Log_Debug("X86", "Error %i at 0x%08x", Regs->int_num, Regs->eip);
62         
63         __asm__ __volatile__ ("cli");
64         
65         // Debug exception (used for single-stepping)
66         if(Regs->int_num == 1)
67         {
68                 static Uint32   lastEIP = 0;
69                 tThread *thread = Proc_GetCurThread();
70                 if( Regs->eip == lastEIP )
71                         return;
72                 Log("%p(%i %s) IP=%08x", thread, thread->TID, thread->ThreadName, Regs->eip);
73                 lastEIP = Regs->eip;
74                 return ;
75         }
76         
77         // Page Fault
78         if(Regs->int_num == 14)
79         {
80                 __asm__ __volatile__ ("sti");   // Should be OK, TODO: Test
81                 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
82                 MM_PageFault( cr, Regs->err_code, Regs );
83                 return ;
84         }
85         
86         // VM8086 GPF
87         if(Regs->int_num == 13 && Regs->eflags & 0x20000)
88         {
89                 VM8086_GPF(Regs);
90                 return ;
91         }
92         
93         // Check if it's a user mode fault
94         if( Regs->eip < KERNEL_BASE || (Regs->cs & 3) == 3 ) {
95                 Log_Warning("Arch", "User Fault -  %s, Code: 0x%x",
96                         csaERROR_NAMES[Regs->int_num], Regs->err_code);
97                 Log_Warning("Arch", "at CS:EIP %04x:%08x",
98                         Regs->cs, Regs->eip);
99                 MM_DumpTables(0, KERNEL_BASE);
100                 switch( Regs->int_num )
101                 {
102                 // Division by Zero
103                 case  0:        Threads_Fault(FAULT_DIV0);      break;
104                 // Invalid opcode
105                 case  6:        Threads_Fault(FAULT_OPCODE);    break;
106                 // GPF
107                 case 13:        Threads_Fault(FAULT_ACCESS);    break;
108                 // Floating Point Exception
109                 case 16:        Threads_Fault(FAULT_FLOAT);     break;
110                 
111                 default:        Threads_Fault(FAULT_MISC);      break;
112                 }
113                 return ;
114         }
115         
116         Debug_KernelPanic();
117         
118         LogF("CPU %i Error %i - %s, Code: 0x%x - At %08x",
119                 GetCPUNum(),
120                 Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code,
121                 Regs->eip);
122         
123         //Warning("CPU Error %i - %s, Code: 0x%x",
124         //      Regs->int_num, csaERROR_NAMES[Regs->int_num], Regs->err_code);
125         //Warning(" CS:EIP = 0x%04x:%08x", Regs->cs, Regs->eip);
126         __ASM__ ("xchg %bx, %bx");
127         if(Regs->cs == 0x08)
128                 Warning(" SS:ESP = 0x0010:%08x", (Uint)Regs+sizeof(tRegs));
129         else
130                 Warning(" SS:ESP = 0x%04x:%08x", Regs->ss, Regs->esp);
131         Warning(" EFLAGS = 0x%08x", Regs->eflags);
132         Warning(" EAX %08x ECX %08x EDX %08x EBX %08x",
133                 Regs->eax, Regs->ecx, Regs->edx, Regs->ebx);
134         Warning(" ESP %08x EBP %08x ESI %08x EDI %08x",
135                 Regs->esp, Regs->ebp, Regs->esi, Regs->edi);
136         Warning(" DS %04x ES %04x FS %04x GS %04x",
137                 Regs->ds, Regs->es, Regs->fs, Regs->gs);
138         
139         // Control Registers
140         __asm__ __volatile__ ("mov %%cr0, %0":"=r"(cr));
141         Warning(" CR0 0x%08x", cr);
142         __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
143         Warning(" CR2 0x%08x", cr);
144         __asm__ __volatile__ ("mov %%cr3, %0":"=r"(cr));
145         Warning(" CR3 0x%08x", cr);
146         
147         switch( Regs->int_num )
148         {
149         case 6: // #UD
150                 Warning(" Offending bytes: %02x %02x %02x %02x",
151                         *(Uint8*)Regs->eip+0, *(Uint8*)Regs->eip+1,
152                         *(Uint8*)Regs->eip+2, *(Uint8*)Regs->eip+3);
153                 break;
154         }
155         
156         // Print Stack Backtrace
157         Error_Backtrace(Regs->eip, Regs->ebp);
158         
159         // Dump running threads
160         Threads_Dump();
161         
162         for(;;) __asm__ __volatile__ ("hlt");
163 }
164
165 void Proc_PrintBacktrace(void)
166 {
167         Uint32  ebp;
168         __asm__ __volatile__ ("mov %%ebp, %0" : "=r" (ebp));
169         Error_Backtrace( *(Uint32*)(ebp+4), *(Uint32*)ebp );
170 }
171
172 /**
173  * \fn void Error_Backtrace(Uint eip, Uint ebp)
174  * \brief Unrolls the stack to trace execution
175  * \param eip   Current Instruction Pointer
176  * \param ebp   Current Base Pointer (Stack Frame)
177  */
178 void Error_Backtrace(Uint eip, Uint ebp)
179 {
180          int    i = 0;
181         Uint    delta = 0;
182         char    *str = NULL;
183         
184         //if(eip < 0xC0000000 && eip > 0x1000)
185         //{
186         //      LogF("Backtrace: User - 0x%x\n", eip);
187         //      return;
188         //}
189         
190         if(eip > 0xE0000000)
191         {
192                 LogF("Backtrace: Data Area - 0x%x\n", eip);
193                 return;
194         }
195         
196         if(eip > 0xC8000000)
197         {
198                 LogF("Backtrace: Kernel Module - 0x%x\n", eip);
199                 return;
200         }
201         
202         //str = Debug_GetSymbol(eip, &delta);
203         if(str == NULL)
204                 LogF("Backtrace: 0x%x", eip);
205         else
206                 LogF("Backtrace: %s+0x%x", str, delta);
207         if(!MM_GetPhysAddr(ebp))
208         {
209                 LogF("\nBacktrace: Invalid EBP, stopping\n");
210                 return;
211         }
212         
213         
214         while( MM_GetPhysAddr(ebp) && i < MAX_BACKTRACE )
215         {
216                 //str = Debug_GetSymbol(*(Uint*)(ebp+4), &delta);
217                 if(str == NULL)
218                         LogF(" >> 0x%x", *(Uint*)(ebp+4));
219                 else
220                         LogF(" >> %s+0x%x", str, delta);
221                 ebp = *(Uint*)ebp;
222                 i++;
223         }
224         LogF("\n");
225 }
226
227 /**
228  * \fn void StartupPrint(char *Str)
229  * \brief Str   String to print
230  * \note WHY IS THIS HERE?!?!
231  */
232 void StartupPrint(char *Str)
233 {
234         Uint16  *buf = (void*)0xC00B8000;
235          int    i = 0;
236         static int      line = 0;
237         while(*Str)
238         {
239                 buf[line*80 + i++] = *Str | 0x0700;
240                 Str ++;
241         }
242         
243         // Clear the rest of the line
244         while(i < 80)
245                 buf[line*80 + i++] = 0x0720;
246         
247         line ++;
248         if(line == 25)
249         {
250                 line --;
251                 memcpy(buf, &buf[80], 80*24*2);
252                 memset(&buf[80*24], 0, 80*2);
253         }
254 }
255
256 // === EXPORTS ===
257 EXPORT(__stack_chk_fail);

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