2012-03-15 06:24:07 +01:00
|
|
|
/** @file
|
2016-05-24 05:00:17 +02:00
|
|
|
CPU Exception Library provides PEI/DXE/SMM CPU common exception handler.
|
2012-03-15 06:24:07 +01:00
|
|
|
|
2016-05-24 05:00:17 +02:00
|
|
|
Copyright (c) 2012 - 2016, Intel Corporation. All rights reserved.<BR>
|
2012-03-15 06:24:07 +01:00
|
|
|
This program and the accompanying materials are licensed and made available under
|
|
|
|
the terms and conditions of the BSD License that accompanies this distribution.
|
|
|
|
The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php.
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "CpuExceptionCommon.h"
|
2013-11-22 07:24:41 +01:00
|
|
|
#include <Library/DebugLib.h>
|
2012-03-15 06:24:07 +01:00
|
|
|
|
|
|
|
/**
|
2016-05-24 15:31:11 +02:00
|
|
|
Internal worker function for common exception handler.
|
2012-03-15 06:24:07 +01:00
|
|
|
|
2016-05-24 15:31:11 +02:00
|
|
|
@param ExceptionType Exception type.
|
|
|
|
@param SystemContext Pointer to EFI_SYSTEM_CONTEXT.
|
|
|
|
@param ExceptionHandlerData Pointer to exception handler data.
|
2012-03-15 06:24:07 +01:00
|
|
|
**/
|
|
|
|
VOID
|
2016-05-24 15:31:11 +02:00
|
|
|
CommonExceptionHandlerWorker (
|
2013-11-22 07:24:41 +01:00
|
|
|
IN EFI_EXCEPTION_TYPE ExceptionType,
|
2016-05-24 15:31:11 +02:00
|
|
|
IN EFI_SYSTEM_CONTEXT SystemContext,
|
|
|
|
IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
|
2012-03-15 06:24:07 +01:00
|
|
|
)
|
|
|
|
{
|
2013-11-22 07:24:41 +01:00
|
|
|
EXCEPTION_HANDLER_CONTEXT *ExceptionHandlerContext;
|
2016-05-24 15:31:11 +02:00
|
|
|
RESERVED_VECTORS_DATA *ReservedVectors;
|
|
|
|
EFI_CPU_INTERRUPT_HANDLER *ExternalInterruptHandler;
|
2013-11-22 07:24:41 +01:00
|
|
|
|
2016-05-24 15:31:11 +02:00
|
|
|
ExceptionHandlerContext = (EXCEPTION_HANDLER_CONTEXT *) (UINTN) (SystemContext.SystemContextIa32);
|
|
|
|
ReservedVectors = ExceptionHandlerData->ReservedVectors;
|
|
|
|
ExternalInterruptHandler = ExceptionHandlerData->ExternalInterruptHandler;
|
2013-11-22 07:24:41 +01:00
|
|
|
|
2016-05-24 15:31:11 +02:00
|
|
|
switch (ReservedVectors[ExceptionType].Attribute) {
|
2013-11-22 07:24:41 +01:00
|
|
|
case EFI_VECTOR_HANDOFF_HOOK_BEFORE:
|
|
|
|
//
|
|
|
|
// Need to jmp to old IDT handler after this exception handler
|
|
|
|
//
|
|
|
|
ExceptionHandlerContext->ExceptionDataFlag = (mErrorCodeFlag & (1 << ExceptionType)) ? TRUE : FALSE;
|
2016-05-24 15:31:11 +02:00
|
|
|
ExceptionHandlerContext->OldIdtHandler = ReservedVectors[ExceptionType].ExceptonHandler;
|
2013-11-22 07:24:41 +01:00
|
|
|
break;
|
|
|
|
case EFI_VECTOR_HANDOFF_HOOK_AFTER:
|
|
|
|
while (TRUE) {
|
|
|
|
//
|
|
|
|
// If if anyone has gotten SPIN_LOCK for owner running hook after
|
|
|
|
//
|
2016-05-24 15:31:11 +02:00
|
|
|
if (AcquireSpinLockOrFail (&ReservedVectors[ExceptionType].SpinLock)) {
|
2013-11-22 07:24:41 +01:00
|
|
|
//
|
|
|
|
// Need to execute old IDT handler before running this exception handler
|
|
|
|
//
|
2016-05-24 15:31:11 +02:00
|
|
|
ReservedVectors[ExceptionType].ApicId = GetApicId ();
|
2013-11-22 07:24:41 +01:00
|
|
|
ArchSaveExceptionContext (ExceptionType, SystemContext);
|
|
|
|
ExceptionHandlerContext->ExceptionDataFlag = (mErrorCodeFlag & (1 << ExceptionType)) ? TRUE : FALSE;
|
2016-05-24 15:31:11 +02:00
|
|
|
ExceptionHandlerContext->OldIdtHandler = ReservedVectors[ExceptionType].ExceptonHandler;
|
2013-11-22 07:24:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// If failed to acquire SPIN_LOCK, check if it was locked by processor itself
|
|
|
|
//
|
2016-05-24 15:31:11 +02:00
|
|
|
if (ReservedVectors[ExceptionType].ApicId == GetApicId ()) {
|
2013-11-22 07:24:41 +01:00
|
|
|
//
|
|
|
|
// Old IDT handler has been executed, then retore CPU exception content to
|
|
|
|
// run new exception handler.
|
|
|
|
//
|
|
|
|
ArchRestoreExceptionContext (ExceptionType, SystemContext);
|
|
|
|
//
|
|
|
|
// Rlease spin lock for ApicId
|
|
|
|
//
|
2016-05-24 15:31:11 +02:00
|
|
|
ReleaseSpinLock (&ReservedVectors[ExceptionType].SpinLock);
|
2013-11-22 07:24:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
CpuPause ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xffffffff:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//
|
|
|
|
// It should never reach here
|
|
|
|
//
|
|
|
|
CpuDeadLoop ();
|
|
|
|
break;
|
2012-03-15 06:24:07 +01:00
|
|
|
}
|
2013-11-22 07:24:41 +01:00
|
|
|
|
2016-05-24 15:31:11 +02:00
|
|
|
if (ExternalInterruptHandler != NULL &&
|
|
|
|
ExternalInterruptHandler[ExceptionType] != NULL) {
|
|
|
|
(ExternalInterruptHandler[ExceptionType]) (ExceptionType, SystemContext);
|
2013-12-20 06:42:41 +01:00
|
|
|
} else if (ExceptionType < CPU_EXCEPTION_NUM) {
|
2013-11-22 07:24:41 +01:00
|
|
|
//
|
|
|
|
// Get Spinlock to display CPU information
|
|
|
|
//
|
2016-05-24 15:31:11 +02:00
|
|
|
while (!AcquireSpinLockOrFail (&ExceptionHandlerData->DisplayMessageSpinLock)) {
|
2013-11-22 07:24:41 +01:00
|
|
|
CpuPause ();
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Display ExceptionType, CPU information and Image information
|
|
|
|
//
|
|
|
|
DumpCpuContent (ExceptionType, SystemContext);
|
|
|
|
//
|
|
|
|
// Release Spinlock of output message
|
|
|
|
//
|
2016-05-24 15:31:11 +02:00
|
|
|
ReleaseSpinLock (&ExceptionHandlerData->DisplayMessageSpinLock);
|
2013-11-22 07:24:41 +01:00
|
|
|
//
|
|
|
|
// Enter a dead loop if needn't to execute old IDT handler further
|
|
|
|
//
|
2016-05-24 15:31:11 +02:00
|
|
|
if (ReservedVectors[ExceptionType].Attribute != EFI_VECTOR_HANDOFF_HOOK_BEFORE) {
|
2013-11-22 07:24:41 +01:00
|
|
|
CpuDeadLoop ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-15 06:24:07 +01:00
|
|
|
|
2013-11-22 07:24:41 +01:00
|
|
|
/**
|
|
|
|
Internal worker function to update IDT entries accordling to vector attributes.
|
|
|
|
|
2016-05-24 15:09:22 +02:00
|
|
|
@param[in] IdtTable Pointer to IDT table.
|
|
|
|
@param[in] TemplateMap Pointer to a buffer where the address map is
|
|
|
|
returned.
|
|
|
|
@param[in] ExceptionHandlerData Pointer to exception handler data.
|
2013-11-22 07:24:41 +01:00
|
|
|
|
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
UpdateIdtTable (
|
|
|
|
IN IA32_IDT_GATE_DESCRIPTOR *IdtTable,
|
|
|
|
IN EXCEPTION_HANDLER_TEMPLATE_MAP *TemplateMap,
|
2016-05-24 15:09:22 +02:00
|
|
|
IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
|
2013-11-22 07:24:41 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
UINT16 CodeSegment;
|
|
|
|
UINTN Index;
|
|
|
|
UINTN InterruptHandler;
|
2016-05-24 15:09:22 +02:00
|
|
|
RESERVED_VECTORS_DATA *ReservedVectors;
|
2012-03-15 06:24:07 +01:00
|
|
|
|
2016-05-24 15:09:22 +02:00
|
|
|
ReservedVectors = ExceptionHandlerData->ReservedVectors;
|
2012-03-15 06:24:07 +01:00
|
|
|
//
|
2013-11-22 07:24:41 +01:00
|
|
|
// Use current CS as the segment selector of interrupt gate in IDT
|
2012-03-15 06:24:07 +01:00
|
|
|
//
|
2013-11-22 07:24:41 +01:00
|
|
|
CodeSegment = AsmReadCs ();
|
2012-03-15 06:24:07 +01:00
|
|
|
|
2016-05-24 15:09:22 +02:00
|
|
|
for (Index = 0; Index < ExceptionHandlerData->IdtEntryCount; Index ++) {
|
2013-11-22 07:24:41 +01:00
|
|
|
IdtTable[Index].Bits.Selector = CodeSegment;
|
|
|
|
//
|
|
|
|
// Check reserved vectors attributes
|
|
|
|
//
|
2016-05-24 15:09:22 +02:00
|
|
|
switch (ReservedVectors[Index].Attribute) {
|
2013-11-22 07:24:41 +01:00
|
|
|
case EFI_VECTOR_HANDOFF_DO_NOT_HOOK:
|
|
|
|
//
|
|
|
|
// Keep original IDT entry
|
|
|
|
//
|
|
|
|
continue;
|
|
|
|
case EFI_VECTOR_HANDOFF_HOOK_AFTER:
|
2016-05-24 15:09:22 +02:00
|
|
|
InitializeSpinLock (&ReservedVectors[Index].SpinLock);
|
2013-11-22 07:24:41 +01:00
|
|
|
CopyMem (
|
2016-05-24 15:09:22 +02:00
|
|
|
(VOID *) ReservedVectors[Index].HookAfterStubHeaderCode,
|
2013-11-22 07:24:41 +01:00
|
|
|
(VOID *) TemplateMap->HookAfterStubHeaderStart,
|
|
|
|
TemplateMap->ExceptionStubHeaderSize
|
|
|
|
);
|
2014-09-09 08:50:51 +02:00
|
|
|
AsmVectorNumFixup (
|
2016-05-24 15:09:22 +02:00
|
|
|
(VOID *) ReservedVectors[Index].HookAfterStubHeaderCode,
|
2014-09-09 08:50:51 +02:00
|
|
|
(UINT8) Index,
|
|
|
|
(VOID *) TemplateMap->HookAfterStubHeaderStart
|
|
|
|
);
|
2013-11-22 07:24:41 +01:00
|
|
|
//
|
|
|
|
// Go on the following code
|
|
|
|
//
|
|
|
|
case EFI_VECTOR_HANDOFF_HOOK_BEFORE:
|
|
|
|
//
|
|
|
|
// Save original IDT handler address
|
|
|
|
//
|
2016-05-24 15:09:22 +02:00
|
|
|
ReservedVectors[Index].ExceptonHandler = ArchGetIdtHandler (&IdtTable[Index]);
|
2013-11-22 07:24:41 +01:00
|
|
|
//
|
|
|
|
// Go on the following code
|
|
|
|
//
|
|
|
|
default:
|
|
|
|
//
|
|
|
|
// Update new IDT entry
|
|
|
|
//
|
|
|
|
InterruptHandler = TemplateMap->ExceptionStart + Index * TemplateMap->ExceptionStubHeaderSize;
|
|
|
|
ArchUpdateIdtEntry (&IdtTable[Index], InterruptHandler);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-15 06:24:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-22 07:24:41 +01:00
|
|
|
Internal worker function to initialize exception handler.
|
2012-03-15 06:24:07 +01:00
|
|
|
|
2016-05-24 15:00:02 +02:00
|
|
|
@param[in] VectorInfo Pointer to reserved vector list.
|
|
|
|
@param[in, out] ExceptionHandlerData Pointer to exception handler data.
|
2012-03-15 06:24:07 +01:00
|
|
|
|
2013-11-22 07:24:41 +01:00
|
|
|
@retval EFI_SUCCESS CPU Exception Entries have been successfully initialized
|
|
|
|
with default exception handlers.
|
|
|
|
@retval EFI_INVALID_PARAMETER VectorInfo includes the invalid content if VectorInfo is not NULL.
|
|
|
|
@retval EFI_UNSUPPORTED This function is not supported.
|
|
|
|
|
2012-03-15 06:24:07 +01:00
|
|
|
**/
|
2013-11-22 07:24:41 +01:00
|
|
|
EFI_STATUS
|
|
|
|
InitializeCpuExceptionHandlersWorker (
|
2016-05-24 15:00:02 +02:00
|
|
|
IN EFI_VECTOR_HANDOFF_INFO *VectorInfo OPTIONAL,
|
|
|
|
IN OUT EXCEPTION_HANDLER_DATA *ExceptionHandlerData
|
2012-03-15 06:24:07 +01:00
|
|
|
)
|
|
|
|
{
|
2013-11-22 07:24:41 +01:00
|
|
|
EFI_STATUS Status;
|
|
|
|
IA32_DESCRIPTOR IdtDescriptor;
|
|
|
|
UINTN IdtEntryCount;
|
|
|
|
EXCEPTION_HANDLER_TEMPLATE_MAP TemplateMap;
|
|
|
|
IA32_IDT_GATE_DESCRIPTOR *IdtTable;
|
2016-05-24 15:00:02 +02:00
|
|
|
RESERVED_VECTORS_DATA *ReservedVectors;
|
2013-11-22 07:24:41 +01:00
|
|
|
|
2016-05-24 15:00:02 +02:00
|
|
|
ReservedVectors = ExceptionHandlerData->ReservedVectors;
|
|
|
|
SetMem ((VOID *) ReservedVectors, sizeof (RESERVED_VECTORS_DATA) * CPU_EXCEPTION_NUM, 0xff);
|
2013-11-22 07:24:41 +01:00
|
|
|
if (VectorInfo != NULL) {
|
2016-05-24 15:00:02 +02:00
|
|
|
Status = ReadAndVerifyVectorInfo (VectorInfo, ReservedVectors, CPU_EXCEPTION_NUM);
|
2013-11-22 07:24:41 +01:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Read IDT descriptor and calculate IDT size
|
|
|
|
//
|
|
|
|
AsmReadIdtr (&IdtDescriptor);
|
|
|
|
IdtEntryCount = (IdtDescriptor.Limit + 1) / sizeof (IA32_IDT_GATE_DESCRIPTOR);
|
|
|
|
if (IdtEntryCount > CPU_EXCEPTION_NUM) {
|
|
|
|
//
|
|
|
|
// CPU exeption library only setup CPU_EXCEPTION_NUM exception handler at most
|
|
|
|
//
|
|
|
|
IdtEntryCount = CPU_EXCEPTION_NUM;
|
|
|
|
}
|
|
|
|
|
|
|
|
IdtTable = (IA32_IDT_GATE_DESCRIPTOR *) IdtDescriptor.Base;
|
|
|
|
AsmGetTemplateAddressMap (&TemplateMap);
|
|
|
|
ASSERT (TemplateMap.ExceptionStubHeaderSize <= HOOKAFTER_STUB_SIZE);
|
2016-05-24 15:09:22 +02:00
|
|
|
|
|
|
|
ExceptionHandlerData->IdtEntryCount = IdtEntryCount;
|
|
|
|
UpdateIdtTable (IdtTable, &TemplateMap, ExceptionHandlerData);
|
2016-05-24 15:35:24 +02:00
|
|
|
|
2013-11-22 07:24:41 +01:00
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Registers a function to be called from the processor interrupt handler.
|
|
|
|
|
2016-05-24 15:25:17 +02:00
|
|
|
@param[in] InterruptType Defines which interrupt or exception to hook.
|
|
|
|
@param[in] InterruptHandler A pointer to a function of type EFI_CPU_INTERRUPT_HANDLER that is called
|
|
|
|
when a processor interrupt occurs. If this parameter is NULL, then the handler
|
|
|
|
will be uninstalled
|
|
|
|
@param[in] ExceptionHandlerData Pointer to exception handler data.
|
2013-11-22 07:24:41 +01:00
|
|
|
|
|
|
|
@retval EFI_SUCCESS The handler for the processor interrupt was successfully installed or uninstalled.
|
|
|
|
@retval EFI_ALREADY_STARTED InterruptHandler is not NULL, and a handler for InterruptType was
|
|
|
|
previously installed.
|
|
|
|
@retval EFI_INVALID_PARAMETER InterruptHandler is NULL, and a handler for InterruptType was not
|
|
|
|
previously installed.
|
|
|
|
@retval EFI_UNSUPPORTED The interrupt specified by InterruptType is not supported,
|
|
|
|
or this function is not supported.
|
2013-12-06 02:13:11 +01:00
|
|
|
**/
|
2013-11-22 07:24:41 +01:00
|
|
|
EFI_STATUS
|
|
|
|
RegisterCpuInterruptHandlerWorker (
|
|
|
|
IN EFI_EXCEPTION_TYPE InterruptType,
|
2016-05-24 15:25:17 +02:00
|
|
|
IN EFI_CPU_INTERRUPT_HANDLER InterruptHandler,
|
|
|
|
IN EXCEPTION_HANDLER_DATA *ExceptionHandlerData
|
2013-11-22 07:24:41 +01:00
|
|
|
)
|
|
|
|
{
|
2016-05-24 15:25:17 +02:00
|
|
|
UINTN EnabledInterruptNum;
|
|
|
|
RESERVED_VECTORS_DATA *ReservedVectors;
|
|
|
|
EFI_CPU_INTERRUPT_HANDLER *ExternalInterruptHandler;
|
|
|
|
|
|
|
|
EnabledInterruptNum = ExceptionHandlerData->IdtEntryCount;
|
|
|
|
ReservedVectors = ExceptionHandlerData->ReservedVectors;
|
|
|
|
ExternalInterruptHandler = ExceptionHandlerData->ExternalInterruptHandler;
|
|
|
|
|
|
|
|
if (InterruptType < 0 || InterruptType >= (EFI_EXCEPTION_TYPE)EnabledInterruptNum ||
|
|
|
|
ReservedVectors[InterruptType].Attribute == EFI_VECTOR_HANDOFF_DO_NOT_HOOK) {
|
2013-11-22 07:24:41 +01:00
|
|
|
return EFI_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
2016-05-24 15:25:17 +02:00
|
|
|
if (InterruptHandler == NULL && ExternalInterruptHandler[InterruptType] == NULL) {
|
2013-11-22 07:24:41 +01:00
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2016-05-24 15:25:17 +02:00
|
|
|
if (InterruptHandler != NULL && ExternalInterruptHandler[InterruptType] != NULL) {
|
2013-11-22 07:24:41 +01:00
|
|
|
return EFI_ALREADY_STARTED;
|
|
|
|
}
|
|
|
|
|
2016-05-24 15:25:17 +02:00
|
|
|
ExternalInterruptHandler[InterruptType] = InterruptHandler;
|
2013-11-22 07:24:41 +01:00
|
|
|
return EFI_SUCCESS;
|
2012-03-15 06:24:07 +01:00
|
|
|
}
|
|
|
|
|