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