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