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