]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiCpuException.c
UefiCpuPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / SecPeiCpuException.c
CommitLineData
8f07f895 1/** @file\r
e41aad15 2 CPU exception handler library implemenation for SEC/PEIM modules.\r
8f07f895 3\r
7230212a 4Copyright (c) 2012 - 2018, Intel Corporation. All rights reserved.<BR>\r
0acd8697 5SPDX-License-Identifier: BSD-2-Clause-Patent\r
8f07f895 6\r
7**/\r
8\r
9#include <PiPei.h>\r
10#include "CpuExceptionCommon.h"\r
11\r
e41aad15 12CONST UINTN mDoFarReturnFlag = 0;\r
8f07f895 13\r
14/**\r
15 Common exception handler.\r
16\r
17 @param ExceptionType Exception type.\r
18 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r
19**/\r
20VOID\r
21EFIAPI\r
22CommonExceptionHandler (\r
dd563742 23 IN EFI_EXCEPTION_TYPE ExceptionType,\r
8f07f895 24 IN EFI_SYSTEM_CONTEXT SystemContext\r
25 )\r
26{\r
7230212a
RN
27 //\r
28 // Initialize the serial port before dumping.\r
29 //\r
30 SerialPortInitialize ();\r
8f07f895 31 //\r
32 // Display ExceptionType, CPU information and Image information\r
dd563742 33 //\r
1b2f7b3e 34 DumpImageAndCpuContent (ExceptionType, SystemContext);\r
dd563742 35\r
8f07f895 36 //\r
37 // Enter a dead loop.\r
38 //\r
39 CpuDeadLoop ();\r
40}\r
41\r
42/**\r
e41aad15 43 Initializes all CPU exceptions entries and provides the default exception handlers.\r
dd563742 44\r
e41aad15
JF
45 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
46 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
dd563742 47 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
e41aad15 48 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
dd563742 49 Note: Before invoking this API, caller must allocate memory for IDT table and load\r
a9c7ab95 50 IDTR by AsmWriteIdtr().\r
e41aad15
JF
51\r
52 @param[in] VectorInfo Pointer to reserved vector list.\r
dd563742
JF
53\r
54 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r
e41aad15
JF
55 with default exception handlers.\r
56 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
57 @retval EFI_UNSUPPORTED This function is not supported.\r
58\r
8f07f895 59**/\r
e41aad15
JF
60EFI_STATUS\r
61EFIAPI\r
62InitializeCpuExceptionHandlers (\r
63 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
64 )\r
65{\r
dd563742 66 EFI_STATUS Status;\r
e41aad15
JF
67 RESERVED_VECTORS_DATA ReservedVectorData[CPU_EXCEPTION_NUM];\r
68 IA32_DESCRIPTOR IdtDescriptor;\r
69 UINTN IdtEntryCount;\r
70 UINT16 CodeSegment;\r
71 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;\r
72 IA32_IDT_GATE_DESCRIPTOR *IdtTable;\r
73 UINTN Index;\r
74 UINTN InterruptHandler;\r
75\r
76 if (VectorInfo != NULL) {\r
77 SetMem ((VOID *) ReservedVectorData, sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM, 0xff);\r
78 Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectorData, CPU_EXCEPTION_NUM);\r
79 if (EFI_ERROR (Status)) {\r
80 return EFI_INVALID_PARAMETER;\r
81 }\r
82 }\r
83 //\r
84 // Read IDT descriptor and calculate IDT size\r
85 //\r
86 AsmReadIdtr (&IdtDescriptor);\r
87 IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);\r
88 if (IdtEntryCount > CPU_EXCEPTION_NUM) {\r
89 //\r
90 // CPU exeption library only setup CPU_EXCEPTION_NUM exception handler at most\r
91 //\r
92 IdtEntryCount = CPU_EXCEPTION_NUM;\r
93 }\r
94 //\r
95 // Use current CS as the segment selector of interrupt gate in IDT\r
96 //\r
97 CodeSegment = AsmReadCs ();\r
98\r
99 AsmGetTemplateAddressMap (&TemplateMap);\r
100 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;\r
101 for (Index = 0; Index < IdtEntryCount; Index ++) {\r
102 IdtTable[Index].Bits.Selector = CodeSegment;\r
103 //\r
104 // Check reserved vectors attributes if has, only EFI_VECTOR_HANDOFF_DO_NOT_HOOK\r
105 // supported in this instance\r
106 //\r
107 if (VectorInfo != NULL) {\r
108 if (ReservedVectorData[Index].Attribute == EFI_VECTOR_HANDOFF_DO_NOT_HOOK) {\r
109 continue;\r
110 }\r
111 }\r
112 //\r
113 // Update IDT entry\r
114 //\r
115 InterruptHandler = TemplateMap.ExceptionStart + Index * TemplateMap.ExceptionStubHeaderSize;\r
116 ArchUpdateIdtEntry (&IdtTable[Index], InterruptHandler);\r
117 }\r
118 return EFI_SUCCESS;\r
119}\r
120\r
121/**\r
122 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.\r
dd563742 123\r
e41aad15
JF
124 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
125 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
dd563742 126 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
e41aad15
JF
127 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
128\r
129 @param[in] VectorInfo Pointer to reserved vector list.\r
dd563742
JF
130\r
131 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized\r
e41aad15
JF
132 with default interrupt/exception handlers.\r
133 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
134 @retval EFI_UNSUPPORTED This function is not supported.\r
135\r
136**/\r
137EFI_STATUS\r
8f07f895 138EFIAPI\r
e41aad15
JF
139InitializeCpuInterruptHandlers (\r
140 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
8f07f895 141 )\r
142{\r
e41aad15 143 return EFI_UNSUPPORTED;\r
8f07f895 144}\r
e41aad15
JF
145\r
146/**\r
147 Registers a function to be called from the processor interrupt handler.\r
148\r
dd563742
JF
149 This function registers and enables the handler specified by InterruptHandler for a processor\r
150 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the\r
151 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.\r
e41aad15
JF
152 The installed handler is called once for each processor interrupt or exception.\r
153 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r
154 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r
155\r
156 @param[in] InterruptType Defines which interrupt or exception to hook.\r
157 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
158 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
159 will be uninstalled.\r
160\r
161 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
162 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
163 previously installed.\r
164 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
165 previously installed.\r
166 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,\r
167 or this function is not supported.\r
3f25e6ea 168**/\r
e41aad15
JF
169EFI_STATUS\r
170EFIAPI\r
171RegisterCpuInterruptHandler (\r
172 IN EFI_EXCEPTION_TYPE InterruptType,\r
173 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
174 )\r
175{\r
176 return EFI_UNSUPPORTED;\r
0ff5aa9c
JW
177}\r
178\r
179/**\r
180 Initializes all CPU exceptions entries with optional extra initializations.\r
181\r
182 By default, this method should include all functionalities implemented by\r
183 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.\r
184 This could be done by calling InitializeCpuExceptionHandlers() directly\r
185 in this method besides the extra works.\r
186\r
187 InitData is optional and its use and content are processor arch dependent.\r
188 The typical usage of it is to convey resources which have to be reserved\r
189 elsewhere and are necessary for the extra initializations of exception.\r
190\r
191 @param[in] VectorInfo Pointer to reserved vector list.\r
192 @param[in] InitData Pointer to data optional for extra initializations\r
193 of exception.\r
194\r
195 @retval EFI_SUCCESS The exceptions have been successfully\r
196 initialized.\r
197 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid\r
198 content.\r
199\r
200**/\r
201EFI_STATUS\r
202EFIAPI\r
203InitializeCpuExceptionHandlersEx (\r
204 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,\r
205 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL\r
206 )\r
207{\r
208 return InitializeCpuExceptionHandlers (VectorInfo);\r
209}\r