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