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