]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c
6f271983f2d27d4842368674b57e8c9dfe6b6690
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / PeiCpuException.c
1 /** @file
2 CPU exception handler library implementation for PEIM module.
3
4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 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 <PiPei.h>
16 #include "CpuExceptionCommon.h"
17 #include <Library/DebugLib.h>
18 #include <Library/HobLib.h>
19 #include <Library/MemoryAllocationLib.h>
20
21 CONST UINTN mDoFarReturnFlag = 0;
22
23 EFI_GUID mCpuExceptrionHandlerLibHobGuid = CPU_EXCEPTION_HANDLER_LIB_HOB_GUID;
24
25 /**
26 Get exception handler data pointer from GUIDed HOb.
27
28 @return pointer to exception handler data.
29 **/
30 EXCEPTION_HANDLER_DATA *
31 GetExceptionHandlerData (
32 VOID
33 )
34 {
35 EFI_HOB_GUID_TYPE *GuidHob;
36 VOID *DataInHob;
37 EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
38
39 ExceptionHandlerData = NULL;
40 GuidHob = GetFirstGuidHob (&mCpuExceptrionHandlerLibHobGuid);
41 if (GuidHob != NULL) {
42 DataInHob = GET_GUID_HOB_DATA (GuidHob);
43 ExceptionHandlerData = (EXCEPTION_HANDLER_DATA *)(*(UINTN *)DataInHob);
44 }
45 ASSERT (ExceptionHandlerData != NULL);
46 return ExceptionHandlerData;
47 }
48
49 /**
50 Common exception handler.
51
52 @param ExceptionType Exception type.
53 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
54 **/
55 VOID
56 EFIAPI
57 CommonExceptionHandler (
58 IN EFI_EXCEPTION_TYPE ExceptionType,
59 IN EFI_SYSTEM_CONTEXT SystemContext
60 )
61 {
62 EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
63
64 ExceptionHandlerData = GetExceptionHandlerData ();
65 CommonExceptionHandlerWorker (ExceptionType, SystemContext, ExceptionHandlerData);
66 }
67
68 /**
69 Initializes all CPU exceptions entries and provides the default exception handlers.
70
71 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
72 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
73 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
74 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
75 Note: Before invoking this API, caller must allocate memory for IDT table and load
76 IDTR by AsmWriteIdtr().
77
78 @param[in] VectorInfo Pointer to reserved vector list.
79
80 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
81 with default exception handlers.
82 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
83 @retval EFI_UNSUPPORTED This function is not supported.
84
85 **/
86 EFI_STATUS
87 EFIAPI
88 InitializeCpuExceptionHandlers (
89 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
90 )
91 {
92 EFI_STATUS Status;
93 EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
94 RESERVED_VECTORS_DATA *ReservedVectors;
95
96 ReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM);
97 ASSERT (ReservedVectors != NULL);
98
99 ExceptionHandlerData = AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));
100 ASSERT (ExceptionHandlerData != NULL);
101 ExceptionHandlerData->ReservedVectors = ReservedVectors;
102 ExceptionHandlerData->ExternalInterruptHandler = NULL;
103 InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);
104
105 Status = InitializeCpuExceptionHandlersWorker (VectorInfo, ExceptionHandlerData);
106 if (EFI_ERROR (Status)) {
107 FreePool (ReservedVectors);
108 FreePool (ExceptionHandlerData);
109 return Status;
110 }
111
112 //
113 // Build location of CPU MP DATA buffer in HOB
114 //
115 BuildGuidDataHob (
116 &mCpuExceptrionHandlerLibHobGuid,
117 (VOID *)&ExceptionHandlerData,
118 sizeof(UINT64)
119 );
120
121 return EFI_SUCCESS;
122 }
123
124 /**
125 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
126
127 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
128 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
129 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
130 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
131
132 @param[in] VectorInfo Pointer to reserved vector list.
133
134 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
135 with default interrupt/exception handlers.
136 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
137 @retval EFI_UNSUPPORTED This function is not supported.
138
139 **/
140 EFI_STATUS
141 EFIAPI
142 InitializeCpuInterruptHandlers (
143 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
144 )
145 {
146 return EFI_UNSUPPORTED;
147 }
148
149 /**
150 Registers a function to be called from the processor interrupt handler.
151
152 This function registers and enables the handler specified by InterruptHandler for a processor
153 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
154 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
155 The installed handler is called once for each processor interrupt or exception.
156 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
157 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
158
159 @param[in] InterruptType Defines which interrupt or exception to hook.
160 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
161 when a processor interrupt occurs. If this parameter is NULL, then the handler
162 will be uninstalled.
163
164 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
165 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
166 previously installed.
167 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
168 previously installed.
169 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
170 or this function is not supported.
171 **/
172 EFI_STATUS
173 EFIAPI
174 RegisterCpuInterruptHandler (
175 IN EFI_EXCEPTION_TYPE InterruptType,
176 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
177 )
178 {
179 return EFI_UNSUPPORTED;
180 }
181
182 /**
183 Initializes all CPU exceptions entries with optional extra initializations.
184
185 By default, this method should include all functionalities implemented by
186 InitializeCpuExceptionHandlers(), plus extra initialization works, if any.
187 This could be done by calling InitializeCpuExceptionHandlers() directly
188 in this method besides the extra works.
189
190 InitData is optional and its use and content are processor arch dependent.
191 The typical usage of it is to convey resources which have to be reserved
192 elsewhere and are necessary for the extra initializations of exception.
193
194 @param[in] VectorInfo Pointer to reserved vector list.
195 @param[in] InitData Pointer to data optional for extra initializations
196 of exception.
197
198 @retval EFI_SUCCESS The exceptions have been successfully
199 initialized.
200 @retval EFI_INVALID_PARAMETER VectorInfo or InitData contains invalid
201 content.
202
203 **/
204 EFI_STATUS
205 EFIAPI
206 InitializeCpuExceptionHandlersEx (
207 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
208 IN CPU_EXCEPTION_INIT_DATA *InitData OPTIONAL
209 )
210 {
211 return InitializeCpuExceptionHandlers (VectorInfo);
212 }