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