Kernel/x86-64 - Updated and debug
[tpg/acess2.git] / 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
9 #define MAX_BACKTRACE   6
10
11 // === IMPORTS ===
12 void    MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
13 void    Error_Backtrace(Uint IP, Uint BP);
14
15 // === PROTOTYPES ===
16 void    Error_Handler(tRegs *Regs);
17
18 // === GLOBALS ==
19 const char * const csaERROR_NAMES[] = {
20         "Divide By Zero", "Debug", "NMI Exception", "INT3",
21         "INTO", "Out of Bounds", "Invalid Opcode", "Coprocessor not avaliable",
22         "Double Fault", "Coprocessor Segment Overrun", "Bad TSS", "Segment Not Present",
23         "Stack Fault Exception", "GPF", "#PF", "Reserved",
24         "Floating Point Exception", "Alignment Check Exception", "Machine Check Exception",     "Reserved",
25         "Reserved", "Reserved", "Reserved", "Reserved",
26         "Reserved", "Reserved", "Reserved", "Reserved",
27         "Reserved", "Reserved", "Reserved", "Reserved"
28         };
29
30 // === CODE ===
31 void Error_Handler(tRegs *Regs)
32 {
33         Uint    cr;
34         
35         if( Regs->IntNum == 14 ) {
36                 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
37                 MM_PageFault(cr, Regs->ErrorCode, Regs);
38                 return ;
39         }
40         
41         Debug_KernelPanic();
42         
43         Log("CPU Error %x, Code: 0x%x", Regs->IntNum, Regs->ErrorCode);
44 //      Log(" - %s", csaERROR_NAMES[Regs->IntNum]);
45         Log(" CS:RIP = 0x%04x:%016llx", Regs->CS, Regs->RIP);
46         Log(" SS:RSP = 0x%04x:%016llx", Regs->SS, Regs->RSP);
47         Log(" RFLAGS = 0x%016llx", Regs->RFlags);
48         
49         Log(" RAX %016llx RCX %016llx RDX %016llx RBX %016llx",
50                 Regs->RAX, Regs->RCX, Regs->RDX, Regs->RBX);
51         Log(" RSP %016llx RBP %016llx RSI %016llx RDI %016llx",
52                 Regs->RSP, Regs->RBP, Regs->RSP, Regs->RDI);
53         Log(" R8  %016llx R9  %016llx R10 %016llx R11 %016llx",
54                 Regs->R8, Regs->R9, Regs->R10, Regs->R11);
55         Log(" R12 %016llx R13 %016llx R14 %016llx R15 %016llx",
56                 Regs->R12, Regs->R13, Regs->R14, Regs->R15);
57         Log(" FS %04x GS %04x", Regs->FS, Regs->GS);
58         
59         
60         // Control Registers
61         __asm__ __volatile__ ("mov %%cr0, %0":"=r"(cr));
62         Warning(" CR0 0x%08x", cr);
63         __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
64         Warning(" CR2 0x%016llx", cr);
65         __asm__ __volatile__ ("mov %%cr3, %0":"=r"(cr));
66         Warning(" CR3 0x%016llx", cr);
67         __asm__ __volatile__ ("mov %%cr4, %0":"=r"(cr));
68         Warning(" CR4 0x%08x", cr);
69         
70         switch( Regs->IntNum )
71         {
72         case 6: // #UD
73                 Warning(" Offending bytes: %02x %02x %02x %02x",
74                         *(Uint8*)(Regs->RIP+0), *(Uint8*)(Regs->RIP+1),
75                         *(Uint8*)(Regs->RIP+2), *(Uint8*)(Regs->RIP+3)
76                         );
77                 break;
78         }
79         
80         __asm__ __volatile__ ("cli");
81         for(;;)
82                 __asm__ __volatile__ ("hlt");
83 }
84
85 /**
86  * \fn void Error_Backtrace(Uint eip, Uint ebp)
87  * \brief Unrolls the stack to trace execution
88  * \param eip   Current Instruction Pointer
89  * \param ebp   Current Base Pointer (Stack Frame)
90  */
91 void Error_Backtrace(Uint IP, Uint BP)
92 {
93          int    i = 0;
94         
95         //if(eip < 0xC0000000 && eip > 0x1000)
96         //{
97         //      LogF("Backtrace: User - 0x%x\n", eip);
98         //      return;
99         //}
100         
101         if( IP > MM_USER_MAX && IP < MM_KERNEL_CODE
102          && (MM_MODULE_MIN > IP || IP > MM_MODULE_MAX)
103                 )
104         {
105                 LogF("Backtrace: Data Area - %p\n", IP);
106                 return;
107         }
108         
109         //str = Debug_GetSymbol(eip, &delta);
110         //if(str == NULL)
111                 LogF("Backtrace: %p", IP);
112         //else
113         //      LogF("Backtrace: %s+0x%x", str, delta);
114         if( !MM_GetPhysAddr(BP) )
115         {
116                 LogF("\nBacktrace: Invalid BP, stopping\n");
117                 return;
118         }
119         
120         
121         while( MM_GetPhysAddr(BP) && MM_GetPhysAddr(BP+8+7) && i < MAX_BACKTRACE )
122         {
123                 //str = Debug_GetSymbol(*(Uint*)(ebp+4), &delta);
124                 //if(str == NULL)
125                         LogF(" >> 0x%llx", ((Uint*)BP)[1]);
126                 //else
127                 //      LogF(" >> %s+0x%x", str, delta);
128                 BP = ((Uint*)BP)[0];
129                 i++;
130         }
131         LogF("\n");
132 }

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