]> git.proxmox.com Git - mirror_edk2.git/blame - UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeException.c
UefiCpuPkg/ExceptionLib: Update UpdateIdtTable()
[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
9db15f81
JF
86 RESERVED_VECTORS_DATA *ReservedVectors;\r
87 EFI_CPU_INTERRUPT_HANDLER *ExternalInterruptHandler;\r
e41aad15 88\r
9db15f81
JF
89 ReservedVectors = AllocatePool (sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM);\r
90 ASSERT (ReservedVectors != NULL);\r
91 SetMem ((VOID *) ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_INTERRUPT_NUM, 0xff);\r
e41aad15 92 if (VectorInfo != NULL) {\r
9db15f81 93 Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_INTERRUPT_NUM);\r
e41aad15 94 if (EFI_ERROR (Status)) {\r
9db15f81 95 FreePool (ReservedVectors);\r
e41aad15
JF
96 return EFI_INVALID_PARAMETER;\r
97 }\r
98 }\r
99 InitializeSpinLock (&mDisplayMessageSpinLock);\r
9db15f81
JF
100 ExternalInterruptHandler = AllocateZeroPool (sizeof (EFI_CPU_INTERRUPT_HANDLER) * CPU_INTERRUPT_NUM);\r
101 ASSERT (ExternalInterruptHandler != NULL);\r
e41aad15
JF
102\r
103 //\r
104 // Read IDT descriptor and calculate IDT size\r
105 //\r
106 AsmReadIdtr (&IdtDescriptor);\r
107 IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);\r
108 if (IdtEntryCount > CPU_INTERRUPT_NUM) {\r
109 IdtEntryCount = CPU_INTERRUPT_NUM;\r
110 }\r
111 //\r
112 // Create Interrupt Descriptor Table and Copy the old IDT table in\r
113 //\r
114 IdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM);\r
115 ASSERT (IdtTable != NULL);\r
116 CopyMem (IdtTable, (VOID *)IdtDescriptor.Base, sizeof (IA32_IDT_GATE_DESCRIPTOR) * IdtEntryCount);\r
117\r
118 AsmGetTemplateAddressMap (&TemplateMap);\r
119 ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);\r
120 InterruptEntryCode = AllocatePool (TemplateMap.ExceptionStubHeaderSize * CPU_INTERRUPT_NUM);\r
121 ASSERT (InterruptEntryCode != NULL);\r
122 \r
123 InterruptEntry = (UINTN) InterruptEntryCode;\r
124 for (Index = 0; Index < CPU_INTERRUPT_NUM; Index ++) {\r
125 CopyMem (\r
126 (VOID *) InterruptEntry,\r
127 (VOID *) TemplateMap.ExceptionStart,\r
128 TemplateMap.ExceptionStubHeaderSize\r
129 );\r
07da1ac8 130 AsmVectorNumFixup ((VOID *) InterruptEntry, (UINT8) Index, (VOID *) TemplateMap.ExceptionStart);\r
e41aad15
JF
131 InterruptEntry += TemplateMap.ExceptionStubHeaderSize;\r
132 }\r
133\r
134 TemplateMap.ExceptionStart = (UINTN) InterruptEntryCode;\r
9db15f81
JF
135 mExceptionHandlerData.IdtEntryCount = CPU_INTERRUPT_NUM;\r
136 mExceptionHandlerData.ReservedVectors = ReservedVectors;\r
137 mExceptionHandlerData.ExternalInterruptHandler = ExternalInterruptHandler;\r
138 InitializeSpinLock (&mExceptionHandlerData.DisplayMessageSpinLock);\r
139\r
140 UpdateIdtTable (IdtTable, &TemplateMap, &mExceptionHandlerData);\r
e41aad15
JF
141\r
142 //\r
143 // Load Interrupt Descriptor Table\r
144 //\r
145 IdtDescriptor.Base = (UINTN) IdtTable;\r
146 IdtDescriptor.Limit = (UINT16) (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM - 1);\r
147 AsmWriteIdtr ((IA32_DESCRIPTOR *) &IdtDescriptor);\r
148\r
149 return EFI_SUCCESS;\r
150}\r
151\r
152/**\r
153 Registers a function to be called from the processor interrupt handler.\r
154\r
155 This function registers and enables the handler specified by InterruptHandler for a processor \r
156 interrupt or exception type specified by InterruptType. If InterruptHandler is NULL, then the \r
157 handler for the processor interrupt or exception type specified by InterruptType is uninstalled. \r
158 The installed handler is called once for each processor interrupt or exception.\r
159 NOTE: This function should be invoked after InitializeCpuExceptionHandlers() or\r
160 InitializeCpuInterruptHandlers() invoked, otherwise EFI_UNSUPPORTED returned.\r
161\r
162 @param[in] InterruptType Defines which interrupt or exception to hook.\r
163 @param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called\r
164 when a processor interrupt occurs. If this parameter is NULL, then the handler\r
165 will be uninstalled.\r
166\r
167 @retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.\r
168 @retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was\r
169 previously installed.\r
170 @retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not\r
171 previously installed.\r
172 @retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,\r
173 or this function is not supported.\r
3f25e6ea 174**/\r
e41aad15
JF
175EFI_STATUS\r
176EFIAPI\r
177RegisterCpuInterruptHandler (\r
178 IN EFI_EXCEPTION_TYPE InterruptType,\r
179 IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler\r
180 )\r
181{\r
182 return RegisterCpuInterruptHandlerWorker (InterruptType, InterruptHandler);\r
183}\r