]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
UefiCpuPkg/ExceptionLib: Add CommonExceptionHandlerWorker()
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / DxeException.c
1 /** @file
2 CPU exception handler library implemenation for DXE modules.
3
4 Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. 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 <PiDxe.h>
16 #include "CpuExceptionCommon.h"
17 #include <Library/DebugLib.h>
18 #include <Library/MemoryAllocationLib.h>
19
20 CONST UINTN mDoFarReturnFlag = 0;
21
22 extern EFI_CPU_INTERRUPT_HANDLER *mExternalInterruptHandler;
23 extern RESERVED_VECTORS_DATA mReservedVectorsData[CPU_EXCEPTION_NUM];
24 extern EFI_CPU_INTERRUPT_HANDLER mExternalInterruptHandlerTable[CPU_EXCEPTION_NUM];
25 EXCEPTION_HANDLER_DATA mExceptionHandlerData;
26
27 /**
28 Common exception handler.
29
30 @param ExceptionType Exception type.
31 @param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
32 **/
33 VOID
34 EFIAPI
35 CommonExceptionHandler (
36 IN EFI_EXCEPTION_TYPE ExceptionType,
37 IN EFI_SYSTEM_CONTEXT SystemContext
38 )
39 {
40 CommonExceptionHandlerWorker (ExceptionType, SystemContext, &mExceptionHandlerData);
41 }
42
43 /**
44 Initializes all CPU exceptions entries and provides the default exception handlers.
45
46 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
47 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
48 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
49 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
50
51 @param[in] VectorInfo Pointer to reserved vector list.
52
53 @retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
54 with default exception handlers.
55 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
56 @retval EFI_UNSUPPORTED This function is not supported.
57
58 **/
59 EFI_STATUS
60 EFIAPI
61 InitializeCpuExceptionHandlers (
62 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
63 )
64 {
65 mExceptionHandlerData.ReservedVectors = mReservedVectorsData;
66 mExceptionHandlerData.ExternalInterruptHandler = mExternalInterruptHandlerTable;
67 InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
68 return InitializeCpuExceptionHandlersWorker (VectorInfo, &mExceptionHandlerData);
69 }
70
71 /**
72 Initializes all CPU interrupt/exceptions entries and provides the default interrupt/exception handlers.
73
74 Caller should try to get an array of interrupt and/or exception vectors that are in use and need to
75 persist by EFI_VECTOR_HANDOFF_INFO defined in PI 1.3 specification.
76 If caller cannot get reserved vector list or it does not exists, set VectorInfo to NULL.
77 If VectorInfo is not NULL, the exception vectors will be initialized per vector attribute accordingly.
78
79 @param[in] VectorInfo Pointer to reserved vector list.
80
81 @retval EFI_SUCCESS All CPU interrupt/exception entries have been successfully initialized
82 with default interrupt/exception handlers.
83 @retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
84 @retval EFI_UNSUPPORTED This function is not supported.
85
86 **/
87 EFI_STATUS
88 EFIAPI
89 InitializeCpuInterruptHandlers (
90 IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL
91 )
92 {
93 EFI_STATUS Status;
94 IA32_IDT_GATE_DESCRIPTOR *IdtTable;
95 IA32_DESCRIPTOR IdtDescriptor;
96 UINTN IdtEntryCount;
97 EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;
98 UINTN Index;
99 UINTN InterruptEntry;
100 UINT8 *InterruptEntryCode;
101 RESERVED_VECTORS_DATA *ReservedVectors;
102 EFI_CPU_INTERRUPT_HANDLER *ExternalInterruptHandler;
103
104 ReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM);
105 ASSERT (ReservedVectors != NULL);
106 SetMem ((VOID *) ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);
107 if (VectorInfo != NULL) {
108 Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_INTERRUPT_NUM);
109 if (EFI_ERROR (Status)) {
110 FreePool (ReservedVectors);
111 return EFI_INVALID_PARAMETER;
112 }
113 }
114
115 ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);
116 ASSERT (ExternalInterruptHandler != NULL);
117
118 //
119 // Read IDT descriptor and calculate IDT size
120 //
121 AsmReadIdtr (&IdtDescriptor);
122 IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);
123 if (IdtEntryCount > CPU_INTERRUPT_NUM) {
124 IdtEntryCount = CPU_INTERRUPT_NUM;
125 }
126 //
127 // Create Interrupt Descriptor Table and Copy the old IDT table in
128 //
129 IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM);
130 ASSERT (IdtTable != NULL);
131 CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof (IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);
132
133 AsmGetTemplateAddressMap (&TemplateMap);
134 ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
135 InterruptEntryCode = AllocatePool (TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM);
136 ASSERT (InterruptEntryCode != NULL);
137
138 InterruptEntry = (UINTN) InterruptEntryCode;
139 for (Index = 0; Index < CPU_INTERRUPT_NUM; Index ++) {
140 CopyMem (
141 (VOID *) InterruptEntry,
142 (VOID *) TemplateMap.ExceptionStart,
143 TemplateMap.ExceptionStubHeaderSize
144 );
145 AsmVectorNumFixup ((VOID *) InterruptEntry, (UINT8) Index, (VOID *) TemplateMap.ExceptionStart);
146 InterruptEntry += TemplateMap.ExceptionStubHeaderSize;
147 }
148
149 TemplateMap.ExceptionStart = (UINTN) InterruptEntryCode;
150 mExceptionHandlerData.IdtEntryCount = CPU_INTERRUPT_NUM;
151 mExceptionHandlerData.ReservedVectors = ReservedVectors;
152 mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;
153 InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);
154
155 UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);
156
157 //
158 // Load Interrupt Descriptor Table
159 //
160 IdtDescriptor.Base = (UINTN) IdtTable;
161 IdtDescriptor.Limit = (UINT16) (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM - 1);
162 AsmWriteIdtr ((IA32_DESCRIPTOR *) &IdtDescriptor);
163
164 return EFI_SUCCESS;
165 }
166
167 /**
168 Registers a function to be called from the processor interrupt handler.
169
170 This function registers and enables the handler specified by InterruptHandler for a processor
171 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the
172 handler for the processor interrupt or exception type specified by InterruptType is uninstalled.
173 The installed handler is called once for each processor interrupt or exception.
174 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or
175 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.
176
177 @param[in] InterruptType Defines which interrupt or exception to hook.
178 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
179 when a processor interrupt occurs. If this parameter is NULL, then the handler
180 will be uninstalled.
181
182 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
183 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
184 previously installed.
185 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
186 previously installed.
187 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
188 or this function is not supported.
189 **/
190 EFI_STATUS
191 EFIAPI
192 RegisterCpuInterruptHandler (
193 IN EFI_EXCEPTION_TYPE InterruptType,
194 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler
195 )
196 {
197 return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler, &mExceptionHandlerData);
198 }