]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ArchExceptionHandler.c
664472a6678ecf1fa76af84f8fffab3f2f59bf51
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / X64 / ArchExceptionHandler.c
1 /** @file
2 x64 CPU Exception Hanlder.
3
4 Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "CpuExceptionCommon.h"
16
17 /**
18 Internal function to setup CPU exception handlers.
19
20 **/
21 VOID
22 InternalSetupCpuExceptionHandlers (
23 VOID
24 )
25 {
26 IA32_DESCRIPTOR IdtDescriptor;
27 UINTN IdtSize;
28 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;
29 UINT16 CodeSegment;
30 IA32_IDT_GATE_DESCRIPTOR *IdtEntry;
31 UINTN Index;
32 UINTN InterruptHandler;
33
34 //
35 // Read IDT descriptor and calculate IDT size
36 //
37 AsmReadIdtr (&IdtDescriptor);
38 IdtSize = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);
39 if (IdtSize > CPU_EXCEPTION_NUM) {
40 //
41 // CPU exeption library only setup CPU_EXCEPTION_NUM exception handler at most
42 //
43 IdtSize = CPU_EXCEPTION_NUM;
44 }
45
46 //
47 // Use current CS as the segment selector of interrupt gate in IDT
48 //
49 CodeSegment = AsmReadCs ();
50 IdtEntry = (IA32_IDT_GATE_DESCRIPTOR *) IdtDescriptor.Base;
51
52 GetTemplateAddressMap (&TemplateMap);
53
54 for (Index = 0; Index < IdtSize; Index ++) {
55 InterruptHandler = TemplateMap.ExceptionStart + Index * TemplateMap.ExceptionStubHeaderSize;
56 IdtEntry[Index].Bits.OffsetLow = (UINT16)(UINTN)InterruptHandler;
57 IdtEntry[Index].Bits.OffsetHigh = (UINT16)((UINTN)InterruptHandler >> 16);
58 IdtEntry[Index].Bits.OffsetUpper = (UINT32)((UINTN)InterruptHandler >> 32);
59 IdtEntry[Index].Bits.Selector = CodeSegment;
60 IdtEntry[Index].Bits.GateType = IA32_IDT_GATE_TYPE_INTERRUPT_32;
61 }
62 }
63
64 /**
65 Common exception handler.
66
67 @param ExceptionType Exception type.
68 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
69 **/
70 VOID
71 DumpCpuContent (
72 IN UINTN ExceptionType,
73 IN EFI_SYSTEM_CONTEXT SystemContext
74 )
75 {
76 UINTN ImageBase;
77 UINTN EntryPoint;
78
79 InternalPrintMessage (
80 "!!!! X64 Exception Type - %016lx CPU Apic ID - %08x !!!!\n",
81 ExceptionType,
82 GetApicId ()
83 );
84 InternalPrintMessage (
85 "RIP - %016lx, CS - %016lx, RFLAGS - %016lx\n",
86 SystemContext.SystemContextX64->Rip,
87 SystemContext.SystemContextX64->Cs,
88 SystemContext.SystemContextX64->Rflags
89 );
90 if (mErrorCodeFlag & (1 << ExceptionType)) {
91 InternalPrintMessage (
92 "ExceptionData - %016lx\n",
93 SystemContext.SystemContextX64->ExceptionData
94 );
95 }
96 InternalPrintMessage (
97 "RAX - %016lx, RCX - %016lx, RDX - %016lx\n",
98 SystemContext.SystemContextX64->Rax,
99 SystemContext.SystemContextX64->Rcx,
100 SystemContext.SystemContextX64->Rdx
101 );
102 InternalPrintMessage (
103 "RBX - %016lx, RSP - %016lx, RBP - %016lx\n",
104 SystemContext.SystemContextX64->Rbx,
105 SystemContext.SystemContextX64->Rsp,
106 SystemContext.SystemContextX64->Rbp
107 );
108 InternalPrintMessage (
109 "RSI - %016lx, RDI - %016lx\n",
110 SystemContext.SystemContextX64->Rsi,
111 SystemContext.SystemContextX64->Rdi
112 );
113 InternalPrintMessage (
114 "R8 - %016lx, R9 - %016lx, R10 - %016lx\n",
115 SystemContext.SystemContextX64->R8,
116 SystemContext.SystemContextX64->R9,
117 SystemContext.SystemContextX64->R10
118 );
119 InternalPrintMessage (
120 "R11 - %016lx, R12 - %016lx, R13 - %016lx\n",
121 SystemContext.SystemContextX64->R11,
122 SystemContext.SystemContextX64->R12,
123 SystemContext.SystemContextX64->R13
124 );
125 InternalPrintMessage (
126 "R14 - %016lx, R15 - %016lx\n",
127 SystemContext.SystemContextX64->R14,
128 SystemContext.SystemContextX64->R15
129 );
130 InternalPrintMessage (
131 "DS - %016lx, ES - %016lx, FS - %016lx\n",
132 SystemContext.SystemContextX64->Ds,
133 SystemContext.SystemContextX64->Es,
134 SystemContext.SystemContextX64->Fs
135 );
136 InternalPrintMessage (
137 "GS - %016lx, SS - %016lx\n",
138 SystemContext.SystemContextX64->Gs,
139 SystemContext.SystemContextX64->Ss
140 );
141 InternalPrintMessage (
142 "CR0 - %016lx, CR2 - %016lx, CR3 - %016lx\n",
143 SystemContext.SystemContextX64->Cr0,
144 SystemContext.SystemContextX64->Cr2,
145 SystemContext.SystemContextX64->Cr3
146 );
147 InternalPrintMessage (
148 "CR4 - %016lx, CR8 - %016lx\n",
149 SystemContext.SystemContextX64->Cr4,
150 SystemContext.SystemContextX64->Cr8
151 );
152 InternalPrintMessage (
153 "DR0 - %016lx, DR1 - %016lx, DR2 - %016lx\n",
154 SystemContext.SystemContextX64->Dr0,
155 SystemContext.SystemContextX64->Dr1,
156 SystemContext.SystemContextX64->Dr2
157 );
158 InternalPrintMessage (
159 "DR3 - %016lx, DR6 - %016lx, DR7 - %016lx\n",
160 SystemContext.SystemContextX64->Dr3,
161 SystemContext.SystemContextX64->Dr6,
162 SystemContext.SystemContextX64->Dr7
163 );
164 InternalPrintMessage (
165 "GDTR - %016lx %016lx, LDTR - %016lx\n",
166 SystemContext.SystemContextX64->Gdtr[0],
167 SystemContext.SystemContextX64->Gdtr[1],
168 SystemContext.SystemContextX64->Ldtr
169 );
170 InternalPrintMessage (
171 "IDTR - %016lx %016lx, TR - %016lx\n",
172 SystemContext.SystemContextX64->Idtr[0],
173 SystemContext.SystemContextX64->Idtr[1],
174 SystemContext.SystemContextX64->Tr
175 );
176 InternalPrintMessage (
177 "FXSAVE_STATE - %016lx\n",
178 &SystemContext.SystemContextX64->FxSaveState
179 );
180
181 //
182 // Find module image base and module entry point by RIP
183 //
184 ImageBase = FindModuleImageBase (SystemContext.SystemContextX64->Rip, &EntryPoint);
185 if (ImageBase != 0) {
186 InternalPrintMessage (
187 " (ImageBase=%016lx, EntryPoint=%016lx) !!!!\n",
188 ImageBase,
189 EntryPoint
190 );
191 }
192 }