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