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