1 | /** @file
|
---|
2 | CPU exception handler library implementation for PEIM module.
|
---|
3 |
|
---|
4 | Copyright (c) 2016 - 2022, 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 | #include <Library/DebugLib.h>
|
---|
12 | #include <Library/HobLib.h>
|
---|
13 | #include <Library/MemoryAllocationLib.h>
|
---|
14 | #include <Library/PcdLib.h>
|
---|
15 |
|
---|
16 | CONST UINTN mDoFarReturnFlag = 0;
|
---|
17 |
|
---|
18 | typedef struct {
|
---|
19 | UINT8 ExceptionStubHeader[HOOKAFTER_STUB_SIZE];
|
---|
20 | EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
|
---|
21 | } EXCEPTION0_STUB_HEADER;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | Get exception handler data pointer from IDT[0].
|
---|
25 |
|
---|
26 | The exception #0 stub header is duplicated in an allocated pool with extra 4-byte/8-byte to store the
|
---|
27 | exception handler data. The new allocated memory layout follows structure EXCEPTION0_STUB_HEADER.
|
---|
28 | The code assumes that all processors uses the same exception handler for #0 exception.
|
---|
29 |
|
---|
30 | @return pointer to exception handler data.
|
---|
31 | **/
|
---|
32 | EXCEPTION_HANDLER_DATA *
|
---|
33 | GetExceptionHandlerData (
|
---|
34 | VOID
|
---|
35 | )
|
---|
36 | {
|
---|
37 | IA32_DESCRIPTOR IdtDescriptor;
|
---|
38 | IA32_IDT_GATE_DESCRIPTOR *IdtTable;
|
---|
39 | EXCEPTION0_STUB_HEADER *Exception0StubHeader;
|
---|
40 |
|
---|
41 | AsmReadIdtr (&IdtDescriptor);
|
---|
42 | IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
|
---|
43 |
|
---|
44 | Exception0StubHeader = (EXCEPTION0_STUB_HEADER *)ArchGetIdtHandler (&IdtTable[0]);
|
---|
45 | return Exception0StubHeader->ExceptionHandlerData;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | Set exception handler data pointer to IDT[0].
|
---|
50 |
|
---|
51 | The exception #0 stub header is duplicated in an allocated pool with extra 4-byte/8-byte to store the
|
---|
52 | exception handler data. The new allocated memory layout follows structure EXCEPTION0_STUB_HEADER.
|
---|
53 | The code assumes that all processors uses the same exception handler for #0 exception.
|
---|
54 |
|
---|
55 | @param ExceptionHandlerData pointer to exception handler data.
|
---|
56 | **/
|
---|
57 | VOID
|
---|
58 | SetExceptionHandlerData (
|
---|
59 | IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
|
---|
60 | )
|
---|
61 | {
|
---|
62 | EXCEPTION0_STUB_HEADER *Exception0StubHeader;
|
---|
63 | IA32_DESCRIPTOR IdtDescriptor;
|
---|
64 | IA32_IDT_GATE_DESCRIPTOR *IdtTable;
|
---|
65 |
|
---|
66 | //
|
---|
67 | // Duplicate the exception #0 stub header in pool and cache the ExceptionHandlerData just after the stub header.
|
---|
68 | // So AP can get the ExceptionHandlerData by reading the IDT[0].
|
---|
69 | //
|
---|
70 | AsmReadIdtr (&IdtDescriptor);
|
---|
71 | IdtTable = (IA32_IDT_GATE_DESCRIPTOR *)IdtDescriptor.Base;
|
---|
72 |
|
---|
73 | Exception0StubHeader = AllocatePool (sizeof (*Exception0StubHeader));
|
---|
74 | ASSERT (Exception0StubHeader != NULL);
|
---|
75 | CopyMem (
|
---|
76 | Exception0StubHeader->ExceptionStubHeader,
|
---|
77 | (VOID *)ArchGetIdtHandler (&IdtTable[0]),
|
---|
78 | sizeof (Exception0StubHeader->ExceptionStubHeader)
|
---|
79 | );
|
---|
80 | Exception0StubHeader->ExceptionHandlerData = ExceptionHandlerData;
|
---|
81 | ArchUpdateIdtEntry (&IdtTable[0], (UINTN)Exception0StubHeader->ExceptionStubHeader);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | Common exception handler.
|
---|
86 |
|
---|
87 | @param ExceptionType Exception type.
|
---|
88 | @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
|
---|
89 | **/
|
---|
90 | VOID
|
---|
91 | EFIAPI
|
---|
92 | CommonExceptionHandler (
|
---|
93 | IN EFI_EXCEPTION_TYPE ExceptionType,
|
---|
94 | IN EFI_SYSTEM_CONTEXT SystemContext
|
---|
95 | )
|
---|
96 | {
|
---|
97 | EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
|
---|
98 |
|
---|
99 | ExceptionHandlerData = GetExceptionHandlerData ();
|
---|
100 | CommonExceptionHandlerWorker (ExceptionType, SystemContext, ExceptionHandlerData);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | Registers a function to be called from the processor interrupt handler.
|
---|
105 |
|
---|
106 | This function registers and enables the handler specified by InterruptHandler for a processor
|
---|
107 | interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
|
---|
108 | handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
|
---|
109 | The installed handler is called once for each processor interrupt or exception.
|
---|
110 | NOTE: This function should be invoked after InitializeCpuExceptionHandlers() is invoked,
|
---|
111 | otherwise EFI_UNSUPPORTED returned.
|
---|
112 |
|
---|
113 | @param[in] InterruptType Defines which interrupt or exception to hook.
|
---|
114 | @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
|
---|
115 | when a processor interrupt occurs. If this parameter is NULL, then the handler
|
---|
116 | will be uninstalled.
|
---|
117 |
|
---|
118 | @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
|
---|
119 | @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
|
---|
120 | previously installed.
|
---|
121 | @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
|
---|
122 | previously installed.
|
---|
123 | @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
|
---|
124 | or this function is not supported.
|
---|
125 | **/
|
---|
126 | EFI_STATUS
|
---|
127 | EFIAPI
|
---|
128 | RegisterCpuInterruptHandler (
|
---|
129 | IN EFI_EXCEPTION_TYPE InterruptType,
|
---|
130 | IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
|
---|
131 | )
|
---|
132 | {
|
---|
133 | EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
|
---|
134 |
|
---|
135 | ExceptionHandlerData = GetExceptionHandlerData ();
|
---|
136 | return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, ExceptionHandlerData);
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | Initializes all CPU exceptions entries and provides the default exception handlers.
|
---|
141 |
|
---|
142 | Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
|
---|
143 | persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
|
---|
144 | If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
|
---|
145 | If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
|
---|
146 | Note: Before invoking this API, caller must allocate memory for IDT table and load
|
---|
147 | IDTR by AsmWriteIdtr().
|
---|
148 |
|
---|
149 | @param[in] VectorInfo Pointer to reserved vector list.
|
---|
150 |
|
---|
151 | @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
|
---|
152 | with default exception handlers.
|
---|
153 | @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
|
---|
154 | @retval EFI_UNSUPPORTED This function is not supported.
|
---|
155 |
|
---|
156 | **/
|
---|
157 | EFI_STATUS
|
---|
158 | EFIAPI
|
---|
159 | InitializeCpuExceptionHandlers (
|
---|
160 | IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
|
---|
161 | )
|
---|
162 | {
|
---|
163 | EFI_STATUS Status;
|
---|
164 | EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
|
---|
165 | RESERVED_VECTORS_DATA *ReservedVectors;
|
---|
166 |
|
---|
167 | ReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM);
|
---|
168 | ASSERT (ReservedVectors != NULL);
|
---|
169 |
|
---|
170 | ExceptionHandlerData = AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));
|
---|
171 | ASSERT (ExceptionHandlerData != NULL);
|
---|
172 | ExceptionHandlerData->IdtEntryCount = CPU_EXCEPTION_NUM;
|
---|
173 | ExceptionHandlerData->ReservedVectors = ReservedVectors;
|
---|
174 | ExceptionHandlerData->ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * ExceptionHandlerData->IdtEntryCount);
|
---|
175 | InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);
|
---|
176 |
|
---|
177 | Status = InitializeCpuExceptionHandlersWorker (VectorInfo, ExceptionHandlerData);
|
---|
178 | if (EFI_ERROR (Status)) {
|
---|
179 | FreePool (ReservedVectors);
|
---|
180 | FreePool (ExceptionHandlerData);
|
---|
181 | return Status;
|
---|
182 | }
|
---|
183 |
|
---|
184 | SetExceptionHandlerData (ExceptionHandlerData);
|
---|
185 | return EFI_SUCCESS;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | Setup separate stacks for certain exception handlers.
|
---|
190 | If the input Buffer and BufferSize are both NULL, use global variable if possible.
|
---|
191 |
|
---|
192 | @param[in] Buffer Point to buffer used to separate exception stack.
|
---|
193 | @param[in, out] BufferSize On input, it indicates the byte size of Buffer.
|
---|
194 | If the size is not enough, the return status will
|
---|
195 | be EFI_BUFFER_TOO_SMALL, and output BufferSize
|
---|
196 | will be the size it needs.
|
---|
197 |
|
---|
198 | @retval EFI_SUCCESS The stacks are assigned successfully.
|
---|
199 | @retval EFI_UNSUPPORTED This function is not supported.
|
---|
200 | @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small.
|
---|
201 | **/
|
---|
202 | EFI_STATUS
|
---|
203 | EFIAPI
|
---|
204 | InitializeSeparateExceptionStacks (
|
---|
205 | IN VOID *Buffer,
|
---|
206 | IN OUT UINTN *BufferSize
|
---|
207 | )
|
---|
208 | {
|
---|
209 | if ((Buffer == NULL) && (BufferSize == NULL)) {
|
---|
210 | return EFI_UNSUPPORTED;
|
---|
211 | }
|
---|
212 |
|
---|
213 | return ArchSetupExceptionStack (Buffer, BufferSize);
|
---|
214 | }
|
---|