]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
CpuException: Add InitializeSeparateExceptionStacks
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / SecPeiCpuException.c
1 /** @file
2 CPU exception handler library implemenation for SEC/PEIM modules.
3
4 Copyright (c) 2012 - 2022, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
10 #include <Library/VmgExitLib.h>
11 #include "CpuExceptionCommon.h"
12
13 CONST UINTN mDoFarReturnFlag = 0;
14
15 /**
16 Common exception handler.
17
18 @param ExceptionType Exception type.
19 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
20 **/
21 VOID
22 EFIAPI
23 CommonExceptionHandler (
24 IN EFI_EXCEPTION_TYPE ExceptionType,
25 IN EFI_SYSTEM_CONTEXT SystemContext
26 )
27 {
28 EFI_STATUS Status;
29
30 switch (ExceptionType) {
31 case VC_EXCEPTION:
32 //
33 // #VC needs to be handled immediately upon enabling exception handling
34 // and therefore can't use the RegisterCpuInterruptHandler() interface
35 // (which isn't supported under Sec and Pei anyway).
36 //
37 // Handle the #VC:
38 // On EFI_SUCCESS - Exception has been handled, return
39 // On other - ExceptionType contains (possibly new) exception
40 // value
41 //
42 Status = VmgExitHandleVc (&ExceptionType, SystemContext);
43 if (!EFI_ERROR (Status)) {
44 return;
45 }
46
47 break;
48
49 case VE_EXCEPTION:
50 //
51 // #VE needs to be handled immediately upon enabling exception handling
52 // and therefore can't use the RegisterCpuInterruptHandler() interface
53 // (which isn't supported under Sec and Pei anyway).
54 //
55 // Handle the #VE:
56 // On EFI_SUCCESS - Exception has been handled, return
57 // On other - ExceptionType contains (possibly new) exception
58 // value
59 //
60 Status = VmTdExitHandleVe (&ExceptionType, SystemContext);
61 if (!EFI_ERROR (Status)) {
62 return;
63 }
64
65 break;
66
67 default:
68 break;
69 }
70
71 //
72 // Initialize the serial port before dumping.
73 //
74 SerialPortInitialize ();
75 //
76 // Display ExceptionType, CPU information and Image information
77 //
78 DumpImageAndCpuContent (ExceptionType, SystemContext);
79
80 //
81 // Enter a dead loop.
82 //
83 CpuDeadLoop ();
84 }
85
86 /**
87 Initializes all CPU exceptions entries and provides the default exception handlers.
88
89 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
90 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
91 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
92 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
93 Note: Before invoking this API, caller must allocate memory for IDT table and load
94 IDTR by AsmWriteIdtr().
95
96 @param[in] VectorInfo Pointer to reserved vector list.
97
98 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
99 with default exception handlers.
100 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
101 @retval EFI_UNSUPPORTED This function is not supported.
102
103 **/
104 EFI_STATUS
105 EFIAPI
106 InitializeCpuExceptionHandlers (
107 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
108 )
109 {
110 EFI_STATUS Status;
111 RESERVED_VECTORS_DATA ReservedVectorData[CPU_EXCEPTION_NUM];
112 IA32_DESCRIPTOR IdtDescriptor;
113 UINTN IdtEntryCount;
114 UINT16 CodeSegment;
115 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;
116 IA32_IDT_GATE_DESCRIPTOR *IdtTable;
117 UINTN Index;
118 UINTN InterruptHandler;
119
120 if (VectorInfo != NULL) {
121 SetMem ((VOID *)ReservedVectorData, sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM, 0xff);
122 Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectorData, CPU_EXCEPTION_NUM);
123 if (EFI_ERROR (Status)) {
124 return EFI_INVALID_PARAMETER;
125 }
126 }
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_EXCEPTION_NUM) {
134 //
135 // CPU exception library only setup CPU_EXCEPTION_NUM exception handler at most
136 //
137 IdtEntryCount = CPU_EXCEPTION_NUM;
138 }
139
140 //
141 // Use current CS as the segment selector of interrupt gate in IDT
142 //
143 CodeSegment = AsmReadCs ();
144
145 AsmGetTemplateAddressMap (&TemplateMap);
146 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
147 for (Index = 0; Index < IdtEntryCount; Index++) {
148 IdtTable[Index].Bits.Selector = CodeSegment;
149 //
150 // Check reserved vectors attributes if has, only EFI_VECTOR_HANDOFF_DO_NOT_HOOK
151 // supported in this instance
152 //
153 if (VectorInfo != NULL) {
154 if (ReservedVectorData[Index].Attribute == EFI_VECTOR_HANDOFF_DO_NOT_HOOK) {
155 continue;
156 }
157 }
158
159 //
160 // Update IDT entry
161 //
162 InterruptHandler = TemplateMap.ExceptionStart + Index * TemplateMap.ExceptionStubHeaderSize;
163 ArchUpdateIdtEntry (&IdtTable[Index], InterruptHandler);
164 }
165
166 return EFI_SUCCESS;
167 }
168
169 /**
170 Registers a function to be called from the processor interrupt handler.
171
172 This function registers and enables the handler specified by InterruptHandler for a processor
173 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
174 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
175 The installed handler is called once for each processor interrupt or exception.
176 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() is invoked,
177 otherwise EFI_UNSUPPORTED returned.
178
179 @param[in] InterruptType Defines which interrupt or exception to hook.
180 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
181 when a processor interrupt occurs. If this parameter is NULL, then the handler
182 will be uninstalled.
183
184 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
185 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
186 previously installed.
187 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
188 previously installed.
189 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
190 or this function is not supported.
191 **/
192 EFI_STATUS
193 EFIAPI
194 RegisterCpuInterruptHandler (
195 IN EFI_EXCEPTION_TYPE InterruptType,
196 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
197 )
198 {
199 return EFI_UNSUPPORTED;
200 }
201
202 /**
203 Setup separate stacks for certain exception handlers.
204
205 InitData is optional and processor arch dependent.
206
207 @param[in] InitData Pointer to data optional for information about how
208 to assign stacks for certain exception handlers.
209
210 @retval EFI_SUCCESS The stacks are assigned successfully.
211 @retval EFI_UNSUPPORTED This function is not supported.
212
213 **/
214 EFI_STATUS
215 EFIAPI
216 InitializeSeparateExceptionStacks (
217 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
218 )
219 {
220 return EFI_UNSUPPORTED;
221 }