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