x86_64 fixes
[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
8 // === IMPORTS ===
9 void    MM_PageFault(tVAddr Addr, Uint ErrorCode, tRegs *Regs);
10
11 // === PROTOTYPES ===
12 void    Error_Handler(tRegs *Regs);
13
14 // === GLOBALS ==
15 const char * const csaERROR_NAMES[] = {
16         "Divide By Zero", "Debug", "NMI Exception", "INT3",
17         "INTO", "Out of Bounds", "Invalid Opcode", "Coprocessor not avaliable",
18         "Double Fault", "Coprocessor Segment Overrun", "Bad TSS", "Segment Not Present",
19         "Stack Fault Exception", "GPF", "#PF", "Reserved",
20         "Floating Point Exception", "Alignment Check Exception", "Machine Check Exception",     "Reserved",
21         "Reserved", "Reserved", "Reserved", "Reserved",
22         "Reserved", "Reserved", "Reserved", "Reserved",
23         "Reserved", "Reserved", "Reserved", "Reserved"
24         };
25
26 // === CODE ===
27 void Error_Handler(tRegs *Regs)
28 {
29         Uint    cr;
30         
31         if( Regs->IntNum == 14 ) {
32                 __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
33                 MM_PageFault(cr, Regs->ErrorCode, Regs);
34                 return ;
35         }
36         
37         Debug_KernelPanic();
38         
39         Log("CPU Error %x, Code: 0x%x", Regs->IntNum, Regs->ErrorCode);
40         Log(" - %s", csaERROR_NAMES[Regs->IntNum]);
41         Log(" CS:RIP = 0x%04x:%016llx", Regs->CS, Regs->RIP);
42         Log(" SS:RSP = 0x%04x:%016llx", Regs->SS, Regs->RSP);
43         Log(" RFLAGS = 0x%016llx", Regs->RFlags);
44         
45         Log(" RAX %016llx RCX %016llx RDX %016llx RBX %016llx",
46                 Regs->RAX, Regs->RCX, Regs->RDX, Regs->RBX);
47         Log(" RSP %016llx RBP %016llx RSI %016llx RDI %016llx",
48                 Regs->RSP, Regs->RBP, Regs->RSP, Regs->RDI);
49         Log(" R8  %016llx R9  %016llx R10 %016llx R11 %016llx",
50                 Regs->R8, Regs->R9, Regs->R10, Regs->R11);
51         Log(" R12 %016llx R13 %016llx R14 %016llx R15 %016llx",
52                 Regs->R12, Regs->R13, Regs->R14, Regs->R15);
53         Log(" FS %04x GS %04x", Regs->FS, Regs->GS);
54         
55         
56         // Control Registers
57         __asm__ __volatile__ ("mov %%cr0, %0":"=r"(cr));
58         Warning(" CR0 0x%08x", cr);
59         __asm__ __volatile__ ("mov %%cr2, %0":"=r"(cr));
60         Warning(" CR2 0x%016llx", cr);
61         __asm__ __volatile__ ("mov %%cr3, %0":"=r"(cr));
62         Warning(" CR3 0x%016llx", cr);
63         __asm__ __volatile__ ("mov %%cr4, %0":"=r"(cr));
64         Warning(" CR4 0x%08x", cr);
65         
66         switch( Regs->IntNum )
67         {
68         case 6: // #UD
69                 Warning(" Offending bytes: %02x %02x %02x %02x",
70                         *(Uint8*)(Regs->RIP+0), *(Uint8*)(Regs->RIP+1),
71                         *(Uint8*)(Regs->RIP+2), *(Uint8*)(Regs->RIP+3)
72                         );
73                 break;
74         }
75         
76         __asm__ __volatile__ ("cli");
77         for(;;)
78                 __asm__ __volatile__ ("hlt");
79 }

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