]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
MdeModulePkg/DxeIpl: disable paging before creating new page table
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / PeiCpuException.c
CommitLineData
a81abf16
JF
1/** @file\r
2 CPU exception handler library implementation for PEIM module.\r
3\r
374168ae 4Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
a81abf16
JF
5This program and the accompanying materials are licensed and made available under\r
6the terms and conditions of the BSD License that accompanies this distribution.\r
7The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php.\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiPei.h>\r
16#include "CpuExceptionCommon.h"\r
17#include <Library/DebugLib.h>\r
18#include <Library/HobLib.h>\r
19#include <Library/MemoryAllocationLib.h>\r
20\r
a81abf16
JF
21CONST UINTN mDoFarReturnFlag = 0;\r
22\r
374168ae
RN
23typedef struct {\r
24 UINT8 ExceptionStubHeader[HOOKAFTER_STUB_SIZE];\r
25 EXCEPTION_HANDLER_DATA *ExceptionHandlerData;\r
26} EXCEPTION0_STUB_HEADER;\r
a81abf16
JF
27\r
28/**\r
374168ae
RN
29 Get exception handler data pointer from IDT[0].\r
30\r
31 The exception #0 stub header is duplicated in an allocated pool with extra 4-byte/8-byte to store the\r
32 exception handler data. The new allocated memory layout follows structure EXCEPTION0_STUB_HEADER.\r
33 The code assumes that all processors uses the same exception handler for #0 exception.\r
a81abf16 34\r
dd563742 35 @return pointer to exception handler data.\r
a81abf16
JF
36**/\r
37EXCEPTION_HANDLER_DATA *\r
38GetExceptionHandlerData (\r
39 VOID\r
40 )\r
41{\r
374168ae
RN
42 IA32_DESCRIPTOR IdtDescriptor;\r
43 IA32_IDT_GATE_DESCRIPTOR *IdtTable;\r
44 EXCEPTION0_STUB_HEADER *Exception0StubHeader;\r
45\r
46 AsmReadIdtr (&IdtDescriptor);\r
47 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;\r
48 \r
49 Exception0StubHeader = (EXCEPTION0_STUB_HEADER *)ArchGetIdtHandler (&IdtTable[0]);\r
50 return Exception0StubHeader->ExceptionHandlerData;\r
51}\r
a81abf16 52\r
374168ae
RN
53/**\r
54 Set exception handler data pointer to IDT[0].\r
55\r
56 The exception #0 stub header is duplicated in an allocated pool with extra 4-byte/8-byte to store the\r
57 exception handler data. The new allocated memory layout follows structure EXCEPTION0_STUB_HEADER.\r
58 The code assumes that all processors uses the same exception handler for #0 exception.\r
59\r
60 @param pointer to exception handler data.\r
61**/\r
62VOID\r
63SetExceptionHandlerData (\r
64 IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData\r
65 )\r
66{\r
67 EXCEPTION0_STUB_HEADER *Exception0StubHeader;\r
68 IA32_DESCRIPTOR IdtDescriptor;\r
69 IA32_IDT_GATE_DESCRIPTOR *IdtTable;\r
70 //\r
71 // Duplicate the exception #0 stub header in pool and cache the ExceptionHandlerData just after the stub header.\r
72 // So AP can get the ExceptionHandlerData by reading the IDT[0].\r
73 //\r
74 AsmReadIdtr (&IdtDescriptor);\r
75 IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;\r
76 \r
77 Exception0StubHeader = AllocatePool (sizeof (*Exception0StubHeader));\r
78 ASSERT (Exception0StubHeader != NULL);\r
79 CopyMem (\r
80 Exception0StubHeader->ExceptionStubHeader,\r
81 (VOID *)ArchGetIdtHandler (&IdtTable[0]),\r
82 sizeof (Exception0StubHeader->ExceptionStubHeader)\r
83 );\r
84 Exception0StubHeader->ExceptionHandlerData = ExceptionHandlerData;\r
85 ArchUpdateIdtEntry (&IdtTable[0], (UINTN)Exception0StubHeader->ExceptionStubHeader);\r
a81abf16
JF
86}\r
87\r
88/**\r
89 Common exception handler.\r
90\r
91 @param ExceptionType Exception type.\r
92 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.\r
93**/\r
94VOID\r
95EFIAPI\r
96CommonExceptionHandler (\r
dd563742 97 IN EFI_EXCEPTION_TYPE ExceptionType,\r
a81abf16
JF
98 IN EFI_SYSTEM_CONTEXT SystemContext\r
99 )\r
100{\r
101 EXCEPTION_HANDLER_DATA *ExceptionHandlerData;\r
102\r
103 ExceptionHandlerData = GetExceptionHandlerData ();\r
104 CommonExceptionHandlerWorker (ExceptionType, SystemContext, ExceptionHandlerData);\r
105}\r
106\r
107/**\r
108 Initializes all CPU exceptions entries and provides the default exception handlers.\r
dd563742 109\r
a81abf16
JF
110 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
111 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
dd563742 112 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
a81abf16 113 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
dd563742 114 Note: Before invoking this API, caller must allocate memory for IDT table and load\r
a81abf16
JF
115 IDTR by AsmWriteIdtr().\r
116\r
117 @param[in] VectorInfo Pointer to reserved vector list.\r
dd563742
JF
118\r
119 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized\r
a81abf16
JF
120 with default exception handlers.\r
121 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
122 @retval EFI_UNSUPPORTED This function is not supported.\r
123\r
124**/\r
125EFI_STATUS\r
126EFIAPI\r
127InitializeCpuExceptionHandlers (\r
128 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
129 )\r
130{\r
131 EFI_STATUS Status;\r
132 EXCEPTION_HANDLER_DATA *ExceptionHandlerData;\r
133 RESERVED_VECTORS_DATA *ReservedVectors;\r
134\r
135 ReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM);\r
136 ASSERT (ReservedVectors != NULL);\r
137\r
138 ExceptionHandlerData = AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));\r
139 ASSERT (ExceptionHandlerData != NULL);\r
140 ExceptionHandlerData->ReservedVectors = ReservedVectors;\r
141 ExceptionHandlerData->ExternalInterruptHandler = NULL;\r
142 InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);\r
143\r
144 Status = InitializeCpuExceptionHandlersWorker (VectorInfo, ExceptionHandlerData);\r
145 if (EFI_ERROR (Status)) {\r
146 FreePool (ReservedVectors);\r
147 FreePool (ExceptionHandlerData);\r
148 return Status;\r
149 }\r
150\r
374168ae 151 SetExceptionHandlerData (ExceptionHandlerData);\r
a81abf16
JF
152 return EFI_SUCCESS;\r
153}\r
154\r
155/**\r
156 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.\r
dd563742 157\r
a81abf16
JF
158 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to\r
159 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.\r
dd563742 160 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.\r
a81abf16
JF
161 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.\r
162\r
163 @param[in] VectorInfo Pointer to reserved vector list.\r
dd563742
JF
164\r
165 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized\r
a81abf16
JF
166 with default interrupt/exception handlers.\r
167 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.\r
168 @retval EFI_UNSUPPORTED This function is not supported.\r
169\r
170**/\r
171EFI_STATUS\r
172EFIAPI\r
173InitializeCpuInterruptHandlers (\r
174 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL\r
175 )\r
176{\r
177 return EFI_UNSUPPORTED;\r
178}\r
179\r
180/**\r
181 Registers a function to be called from the processor interrupt handler.\r
182\r
dd563742
JF
183 This function registers and enables the handler specified by InterruptHandler for a processor\r
184 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the\r
185 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.\r
a81abf16
JF
186 The installed handler is called once for each processor interrupt or exception.\r
187 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r
188 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r
189\r
190 @param[in] InterruptType Defines which interrupt or exception to hook.\r
191 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
192 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
193 will be uninstalled.\r
194\r
195 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
196 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
197 previously installed.\r
198 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
199 previously installed.\r
200 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,\r
201 or this function is not supported.\r
202**/\r
203EFI_STATUS\r
204EFIAPI\r
205RegisterCpuInterruptHandler (\r
206 IN EFI_EXCEPTION_TYPE InterruptType,\r
207 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
208 )\r
209{\r
210 return EFI_UNSUPPORTED;\r
0ff5aa9c
JW
211}\r
212\r
213/**\r
214 Initializes all CPU exceptions entries with optional extra initializations.\r
215\r
216 By default, this method should include all functionalities implemented by\r
217 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.\r
218 This could be done by calling InitializeCpuExceptionHandlers() directly\r
219 in this method besides the extra works.\r
220\r
221 InitData is optional and its use and content are processor arch dependent.\r
222 The typical usage of it is to convey resources which have to be reserved\r
223 elsewhere and are necessary for the extra initializations of exception.\r
224\r
225 @param[in] VectorInfo Pointer to reserved vector list.\r
226 @param[in] InitData Pointer to data optional for extra initializations\r
227 of exception.\r
228\r
229 @retval EFI_SUCCESS The exceptions have been successfully\r
230 initialized.\r
231 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid\r
232 content.\r
233\r
234**/\r
235EFI_STATUS\r
236EFIAPI\r
237InitializeCpuExceptionHandlersEx (\r
238 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,\r
239 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL\r
240 )\r
241{\r
242 return InitializeCpuExceptionHandlers (VectorInfo);\r
243}\r