]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
UefiCpuPkg/CpuExceptionHandlerLib: Add stack switch support
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / DxeException.c
CommitLineData
e41aad15
JF
1/** @file\r
2 CPU exception handler library implemenation for DXE modules.\r
3\r
1b2f7b3e 4 Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>\r
e41aad15
JF
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16#include "CpuExceptionCommon.h"\r
17#include <Library/DebugLib.h>\r
18#include <Library/MemoryAllocationLib.h>\r
19\r
20CONST UINTN mDoFarReturnFlag = 0;\r
21\r
d91225cf
JF
22RESERVED_VECTORS_DATA mReservedVectorsData[CPU_EXCEPTION_NUM];\r
23EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];\r
24UINTN mEnabledInterruptNum = 0;\r
25\r
2c5873fe 26EXCEPTION_HANDLER_DATA mExceptionHandlerData;\r
e41aad15 27\r
0ff5aa9c
JW
28UINT8 mNewStack[CPU_STACK_SWITCH_EXCEPTION_NUMBER *\r
29 CPU_KNOWN_GOOD_STACK_SIZE];\r
30UINT8 mNewGdt[CPU_TSS_GDT_SIZE];\r
31\r
44ecbc28
JF
32/**\r
33 Common exception handler.\r
34\r
35 @param ExceptionType Exception type.\r
36 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r
37**/\r
38VOID\r
39EFIAPI\r
40CommonExceptionHandler (\r
dd563742 41 IN EFI_EXCEPTION_TYPE ExceptionType,\r
44ecbc28
JF
42 IN EFI_SYSTEM_CONTEXT SystemContext\r
43 )\r
44{\r
45 CommonExceptionHandlerWorker (ExceptionType, SystemContext, &mExceptionHandlerData);\r
46}\r
47\r
e41aad15
JF
48/**\r
49 Initializes all CPU exceptions entries and provides the default exception handlers.\r
dd563742 50\r
e41aad15
JF
51 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
52 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
dd563742 53 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
e41aad15
JF
54 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
55\r
56 @param[in] VectorInfo Pointer to reserved vector list.\r
dd563742
JF
57\r
58 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r
e41aad15
JF
59 with default exception handlers.\r
60 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
61 @retval EFI_UNSUPPORTED This function is not supported.\r
62\r
63**/\r
64EFI_STATUS\r
65EFIAPI\r
66InitializeCpuExceptionHandlers (\r
67 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
68 )\r
69{\r
ab95e54d
JF
70 mExceptionHandlerData.ReservedVectors = mReservedVectorsData;\r
71 mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;\r
72 InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);\r
73 return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);\r
e41aad15
JF
74}\r
75\r
76/**\r
77 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.\r
dd563742 78\r
e41aad15
JF
79 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
80 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
dd563742 81 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
e41aad15
JF
82 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
83\r
84 @param[in] VectorInfo Pointer to reserved vector list.\r
dd563742
JF
85\r
86 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized\r
e41aad15
JF
87 with default interrupt/exception handlers.\r
88 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
89 @retval EFI_UNSUPPORTED This function is not supported.\r
90\r
91**/\r
92EFI_STATUS\r
93EFIAPI\r
94InitializeCpuInterruptHandlers (\r
95 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
96 )\r
97{\r
98 EFI_STATUS Status;\r
99 IA32_IDT_GATE_DESCRIPTOR *IdtTable;\r
100 IA32_DESCRIPTOR IdtDescriptor;\r
101 UINTN IdtEntryCount;\r
102 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;\r
103 UINTN Index;\r
104 UINTN InterruptEntry;\r
105 UINT8 *InterruptEntryCode;\r
9db15f81
JF
106 RESERVED_VECTORS_DATA *ReservedVectors;\r
107 EFI_CPU_INTERRUPT_HANDLER *ExternalInterruptHandler;\r
e41aad15 108\r
9db15f81
JF
109 ReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM);\r
110 ASSERT (ReservedVectors != NULL);\r
111 SetMem ((VOID *) ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);\r
e41aad15 112 if (VectorInfo != NULL) {\r
9db15f81 113 Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_INTERRUPT_NUM);\r
e41aad15 114 if (EFI_ERROR (Status)) {\r
9db15f81 115 FreePool (ReservedVectors);\r
e41aad15
JF
116 return EFI_INVALID_PARAMETER;\r
117 }\r
118 }\r
44ecbc28 119\r
9db15f81
JF
120 ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);\r
121 ASSERT (ExternalInterruptHandler != NULL);\r
e41aad15
JF
122\r
123 //\r
124 // Read IDT descriptor and calculate IDT size\r
125 //\r
126 AsmReadIdtr (&IdtDescriptor);\r
127 IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);\r
128 if (IdtEntryCount > CPU_INTERRUPT_NUM) {\r
129 IdtEntryCount = CPU_INTERRUPT_NUM;\r
130 }\r
131 //\r
132 // Create Interrupt Descriptor Table and Copy the old IDT table in\r
133 //\r
134 IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM);\r
135 ASSERT (IdtTable != NULL);\r
136 CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof (IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);\r
137\r
138 AsmGetTemplateAddressMap (&TemplateMap);\r
139 ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);\r
140 InterruptEntryCode = AllocatePool (TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM);\r
141 ASSERT (InterruptEntryCode != NULL);\r
dd563742 142\r
e41aad15
JF
143 InterruptEntry = (UINTN) InterruptEntryCode;\r
144 for (Index = 0; Index < CPU_INTERRUPT_NUM; Index ++) {\r
145 CopyMem (\r
146 (VOID *) InterruptEntry,\r
147 (VOID *) TemplateMap.ExceptionStart,\r
148 TemplateMap.ExceptionStubHeaderSize\r
149 );\r
07da1ac8 150 AsmVectorNumFixup ((VOID *) InterruptEntry, (UINT8) Index, (VOID *) TemplateMap.ExceptionStart);\r
e41aad15
JF
151 InterruptEntry += TemplateMap.ExceptionStubHeaderSize;\r
152 }\r
153\r
154 TemplateMap.ExceptionStart = (UINTN) InterruptEntryCode;\r
9db15f81
JF
155 mExceptionHandlerData.IdtEntryCount = CPU_INTERRUPT_NUM;\r
156 mExceptionHandlerData.ReservedVectors = ReservedVectors;\r
157 mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;\r
158 InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);\r
159\r
160 UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);\r
e41aad15
JF
161\r
162 //\r
163 // Load Interrupt Descriptor Table\r
164 //\r
165 IdtDescriptor.Base = (UINTN) IdtTable;\r
166 IdtDescriptor.Limit = (UINT16) (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM - 1);\r
167 AsmWriteIdtr ((IA32_DESCRIPTOR *) &IdtDescriptor);\r
168\r
169 return EFI_SUCCESS;\r
170}\r
171\r
172/**\r
173 Registers a function to be called from the processor interrupt handler.\r
174\r
dd563742
JF
175 This function registers and enables the handler specified by InterruptHandler for a processor\r
176 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the\r
177 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.\r
e41aad15
JF
178 The installed handler is called once for each processor interrupt or exception.\r
179 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r
180 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r
181\r
182 @param[in] InterruptType Defines which interrupt or exception to hook.\r
183 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
184 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
185 will be uninstalled.\r
186\r
187 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
188 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
189 previously installed.\r
190 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
191 previously installed.\r
192 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,\r
193 or this function is not supported.\r
3f25e6ea 194**/\r
e41aad15
JF
195EFI_STATUS\r
196EFIAPI\r
197RegisterCpuInterruptHandler (\r
198 IN EFI_EXCEPTION_TYPE InterruptType,\r
199 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
200 )\r
201{\r
670f13af 202 return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, &mExceptionHandlerData);\r
e41aad15 203}\r
0ff5aa9c
JW
204\r
205/**\r
206 Initializes CPU exceptions entries and setup stack switch for given exceptions.\r
207\r
208 This method will call InitializeCpuExceptionHandlers() to setup default\r
209 exception handlers unless indicated not to do it explicitly.\r
210\r
211 If InitData is passed with NULL, this method will use the resource reserved\r
212 by global variables to initialize it; Otherwise it will use data in InitData\r
213 to setup stack switch. This is for the different use cases in DxeCore and\r
214 Cpu MP exception initialization.\r
215\r
216 @param[in] VectorInfo Pointer to reserved vector list.\r
217 @param[in] InitData Pointer to data required to setup stack switch for\r
218 given exceptions.\r
219\r
220 @retval EFI_SUCCESS The exceptions have been successfully\r
221 initialized.\r
222 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid\r
223 content.\r
224\r
225**/\r
226EFI_STATUS\r
227EFIAPI\r
228InitializeCpuExceptionHandlersEx (\r
229 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,\r
230 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL\r
231 )\r
232{\r
233 EFI_STATUS Status;\r
234 CPU_EXCEPTION_INIT_DATA EssData;\r
235 IA32_DESCRIPTOR Idtr;\r
236 IA32_DESCRIPTOR Gdtr;\r
237\r
238 //\r
239 // To avoid repeat initialization of default handlers, the caller should pass\r
240 // an extended init data with InitDefaultHandlers set to FALSE. There's no\r
241 // need to call this method to just initialize default handlers. Call non-ex\r
242 // version instead; or this method must be implemented as a simple wrapper of\r
243 // non-ex version of it, if this version has to be called.\r
244 //\r
245 if (InitData == NULL || InitData->X64.InitDefaultHandlers) {\r
246 Status = InitializeCpuExceptionHandlers (VectorInfo);\r
247 } else {\r
248 Status = EFI_SUCCESS;\r
249 }\r
250\r
251 if (!EFI_ERROR (Status)) {\r
252 //\r
253 // Initializing stack switch is only necessary for Stack Guard functionality.\r
254 //\r
255 if (PcdGetBool (PcdCpuStackGuard)) {\r
256 if (InitData == NULL) {\r
257 SetMem (mNewGdt, sizeof (mNewGdt), 0);\r
258\r
259 AsmReadIdtr (&Idtr);\r
260 AsmReadGdtr (&Gdtr);\r
261\r
262 EssData.X64.Revision = CPU_EXCEPTION_INIT_DATA_REV;\r
263 EssData.X64.KnownGoodStackTop = (UINTN)mNewStack;\r
264 EssData.X64.KnownGoodStackSize = CPU_KNOWN_GOOD_STACK_SIZE;\r
265 EssData.X64.StackSwitchExceptions = CPU_STACK_SWITCH_EXCEPTION_LIST;\r
266 EssData.X64.StackSwitchExceptionNumber = CPU_STACK_SWITCH_EXCEPTION_NUMBER;\r
267 EssData.X64.IdtTable = (VOID *)Idtr.Base;\r
268 EssData.X64.IdtTableSize = Idtr.Limit + 1;\r
269 EssData.X64.GdtTable = mNewGdt;\r
270 EssData.X64.GdtTableSize = sizeof (mNewGdt);\r
271 EssData.X64.ExceptionTssDesc = mNewGdt + Gdtr.Limit + 1;\r
272 EssData.X64.ExceptionTssDescSize = CPU_TSS_DESC_SIZE;\r
273 EssData.X64.ExceptionTss = mNewGdt + Gdtr.Limit + 1 + CPU_TSS_DESC_SIZE;\r
274 EssData.X64.ExceptionTssSize = CPU_TSS_SIZE;\r
275\r
276 InitData = &EssData;\r
277 }\r
278 Status = ArchSetupExcpetionStack (InitData);\r
279 }\r
280 }\r
281\r
282 return Status;\r
283}\r