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