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