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