VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/UefiCpuPkg/Library/CpuExceptionHandlerLib/PeiCpuException.c

Last change on this file was 108794, checked in by vboxsync, 3 weeks ago

Devices/EFI/FirmwareNew: Merge edk2-stable202502 from the vendor branch and make it build for the important platforms, bugref:4643

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1/** @file
2 CPU exception handler library implementation for PEIM module.
3
4Copyright (c) 2016 - 2022, Intel Corporation. All rights reserved.<BR>
5SPDX-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
16CONST UINTN mDoFarReturnFlag = 0;
17
18typedef 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**/
32EXCEPTION_HANDLER_DATA *
33GetExceptionHandlerData (
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**/
57VOID
58SetExceptionHandlerData (
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 if (Exception0StubHeader == NULL) {
75 ASSERT (Exception0StubHeader != NULL);
76 return;
77 }
78
79 CopyMem (
80 Exception0StubHeader->ExceptionStubHeader,
81 (VOID *)ArchGetIdtHandler (&IdtTable[0]),
82 sizeof (Exception0StubHeader->ExceptionStubHeader)
83 );
84 Exception0StubHeader->ExceptionHandlerData = ExceptionHandlerData;
85 ArchUpdateIdtEntry (&IdtTable[0], (UINTN)Exception0StubHeader->ExceptionStubHeader);
86}
87
88/**
89 Common exception handler.
90
91 @param ExceptionType Exception type.
92 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
93**/
94VOID
95EFIAPI
96CommonExceptionHandler (
97 IN EFI_EXCEPTION_TYPE ExceptionType,
98 IN EFI_SYSTEM_CONTEXT SystemContext
99 )
100{
101 EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
102
103 ExceptionHandlerData = GetExceptionHandlerData ();
104 CommonExceptionHandlerWorker (ExceptionType, SystemContext, ExceptionHandlerData);
105}
106
107/**
108 Registers a function to be called from the processor interrupt handler.
109
110 This function registers and enables the handler specified by InterruptHandler for a processor
111 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
112 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
113 The installed handler is called once for each processor interrupt or exception.
114 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() is invoked,
115 otherwise EFI_UNSUPPORTED returned.
116
117 @param[in] InterruptType Defines which interrupt or exception to hook.
118 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
119 when a processor interrupt occurs. If this parameter is NULL, then the handler
120 will be uninstalled.
121
122 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
123 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
124 previously installed.
125 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
126 previously installed.
127 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
128 or this function is not supported.
129**/
130EFI_STATUS
131EFIAPI
132RegisterCpuInterruptHandler (
133 IN EFI_EXCEPTION_TYPE InterruptType,
134 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
135 )
136{
137 EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
138
139 ExceptionHandlerData = GetExceptionHandlerData ();
140 return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, ExceptionHandlerData);
141}
142
143/**
144 Initializes all CPU exceptions entries and provides the default exception handlers.
145
146 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
147 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
148 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
149 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
150 Note: Before invoking this API, caller must allocate memory for IDT table and load
151 IDTR by AsmWriteIdtr().
152
153 @param[in] VectorInfo Pointer to reserved vector list.
154
155 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
156 with default exception handlers.
157 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
158 @retval EFI_UNSUPPORTED This function is not supported.
159
160**/
161EFI_STATUS
162EFIAPI
163InitializeCpuExceptionHandlers (
164 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
165 )
166{
167 EFI_STATUS Status;
168 EXCEPTION_HANDLER_DATA *ExceptionHandlerData;
169 RESERVED_VECTORS_DATA *ReservedVectors;
170
171 ReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM);
172 if (ReservedVectors == NULL) {
173 ASSERT (ReservedVectors != NULL);
174 return EFI_OUT_OF_RESOURCES;
175 }
176
177 ExceptionHandlerData = AllocatePool (sizeof (EXCEPTION_HANDLER_DATA));
178 if (ExceptionHandlerData == NULL) {
179 ASSERT (ExceptionHandlerData != NULL);
180 FreePool (ReservedVectors);
181 return EFI_OUT_OF_RESOURCES;
182 }
183
184 ExceptionHandlerData->IdtEntryCount = CPU_EXCEPTION_NUM;
185 ExceptionHandlerData->ReservedVectors = ReservedVectors;
186 ExceptionHandlerData->ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * ExceptionHandlerData->IdtEntryCount);
187 InitializeSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);
188
189 Status = InitializeCpuExceptionHandlersWorker (VectorInfo, ExceptionHandlerData);
190 if (EFI_ERROR (Status)) {
191 FreePool (ReservedVectors);
192 FreePool (ExceptionHandlerData);
193 return Status;
194 }
195
196 SetExceptionHandlerData (ExceptionHandlerData);
197 return EFI_SUCCESS;
198}
199
200/**
201 Setup separate stacks for certain exception handlers.
202 If the input Buffer and BufferSize are both NULL, use global variable if possible.
203
204 @param[in] Buffer Point to buffer used to separate exception stack.
205 @param[in, out] BufferSize On input, it indicates the byte size of Buffer.
206 If the size is not enough, the return status will
207 be EFI_BUFFER_TOO_SMALL, and output BufferSize
208 will be the size it needs.
209
210 @retval EFI_SUCCESS The stacks are assigned successfully.
211 @retval EFI_UNSUPPORTED This function is not supported.
212 @retval EFI_BUFFER_TOO_SMALL This BufferSize is too small.
213**/
214EFI_STATUS
215EFIAPI
216InitializeSeparateExceptionStacks (
217 IN VOID *Buffer,
218 IN OUT UINTN *BufferSize
219 )
220{
221 if ((Buffer == NULL) && (BufferSize == NULL)) {
222 return EFI_UNSUPPORTED;
223 }
224
225 return ArchSetupExceptionStack (Buffer, BufferSize);
226}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette