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