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