TianoCore EDK2 master
Loading...
Searching...
No Matches
CpuExceptionHandlerTest.h File Reference

Go to the source code of this file.

Data Structures

struct  CPU_REGISTER_BUFFER
 
union  MP_SERVICES
 
struct  EXCEPTION_STACK_SWITCH_CONTEXT
 
struct  GENERAL_REGISTER
 
struct  GENERAL_REGISTER_IA32
 

Macros

#define UNIT_TEST_APP_NAME   "Cpu Exception Handler Lib Unit Tests"
 
#define UNIT_TEST_APP_VERSION   "1.0"
 
#define CPU_INTERRUPT_NUM   256
 
#define SPEC_MAX_EXCEPTION_NUM   22
 
#define CR4_RESERVED_BIT   BIT15
 

Functions

VOID * InitializeBspIdt (VOID)
 
VOID EFIAPI TriggerINTnException (IN EFI_EXCEPTION_TYPE ExceptionType)
 
VOID EFIAPI TriggerGPException (UINTN Cr4ReservedBit)
 
VOID EFIAPI TriggerPFException (UINTN PFAddress)
 
VOID EFIAPI AdjustRipForFaultHandler (IN EFI_EXCEPTION_TYPE ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext)
 
VOID EFIAPI AsmTestConsistencyOfCpuContext (IN EFI_EXCEPTION_TYPE ExceptionType, IN UINTN FaultParameter OPTIONAL)
 
VOID EFIAPI AdjustCpuContextHandler (IN EFI_EXCEPTION_TYPE ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext)
 
UNIT_TEST_STATUS CompareCpuContext (VOID)
 
EFI_STATUS GetMpServices (OUT MP_SERVICES *MpServices)
 
EFI_STATUS AddCommonTestCase (IN UNIT_TEST_FRAMEWORK_HANDLE Framework)
 
EFI_STATUS MpServicesUnitTestStartupAllAPs (IN MP_SERVICES MpServices, IN EFI_AP_PROCEDURE Procedure, IN BOOLEAN SingleThread, IN UINTN TimeoutInMicroSeconds, IN VOID *ProcedureArgument)
 
EFI_STATUS MpServicesUnitTestStartupThisAP (IN MP_SERVICES MpServices, IN EFI_AP_PROCEDURE Procedure, IN UINTN ProcessorNumber, IN UINTN TimeoutInMicroSeconds, IN VOID *ProcedureArgument)
 
EFI_STATUS MpServicesUnitTestWhoAmI (IN MP_SERVICES MpServices, OUT UINTN *ProcessorNumber)
 
EFI_STATUS MpServicesUnitTestGetNumberOfProcessors (IN MP_SERVICES MpServices, OUT UINTN *NumberOfProcessors, OUT UINTN *NumberOfEnabledProcessors)
 
VOID EFIAPI TriggerStackOverflow (VOID)
 
VOID EFIAPI CpuStackGuardExceptionHandler (IN EFI_EXCEPTION_TYPE ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext)
 

Variables

UINTN mFaultInstructionLength
 
EFI_EXCEPTION_TYPE mExceptionType
 
UINTN mRspAddress []
 

Detailed Description

Copyright (c) 2022, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent

Four test cases are created in this Unit Test module. a.Test if exception handler can be registered/unregistered for no error code exception In this test case, only no error code exception is triggered and tested by INTn instruction. The special hanlder for these exception will modify a global variable for check.

b.Test if exception handler can be registered/unregistered for GP and PF. In this test case, GP exception is triggered and tested by setting CR4_RESERVED_BIT to 1. PF exception is triggered and tested by writting to not-present or RO addres. The special hanlder for these exceptions will set a global vartiable for check and adjust Rip to return from fault exception.

c.Test if Cpu Context is consistent before and after exception. In this test case:

  1. Set Cpu register to mExpectedContextInHandler before exception.
  2. Trigger exception specified by ExceptionType.
  3. Store SystemContext in mActualContextInHandler and set SystemContext to mExpectedContextAfterException in handler.
  4. After return from exception, store Cpu registers in mActualContextAfterException. The expectation is:
  1. Register values in mActualContextInHandler are the same with register values in mExpectedContextInHandler.
  2. Register values in mActualContextAfterException are the same with register values mActualContextAfterException.

d.Test if stack overflow can be captured by CpuStackGuard in both Bsp and AP. In this test case, stack overflow is triggered by a funtion which calls itself continuously. This test case triggers stack overflow in both BSP and AP. All AP use same Idt with Bsp. The expectation is:

  1. PF exception is triggered (leading to a DF if sepereated stack is not prepared for PF) when Rsp <= StackBase + SIZE_4KB since [StackBase, StackBase + SIZE_4KB] is marked as not present in page table when PcdCpuStackGuard is TRUE.
  2. Stack for PF/DF exception handler in both Bsp and AP is succussfully switched by InitializeSeparateExceptionStacks.

Definition in file CpuExceptionHandlerTest.h.

Macro Definition Documentation

◆ CPU_INTERRUPT_NUM

#define CPU_INTERRUPT_NUM   256

Definition at line 58 of file CpuExceptionHandlerTest.h.

◆ CR4_RESERVED_BIT

#define CR4_RESERVED_BIT   BIT15

Definition at line 60 of file CpuExceptionHandlerTest.h.

◆ SPEC_MAX_EXCEPTION_NUM

#define SPEC_MAX_EXCEPTION_NUM   22

Definition at line 59 of file CpuExceptionHandlerTest.h.

◆ UNIT_TEST_APP_NAME

#define UNIT_TEST_APP_NAME   "Cpu Exception Handler Lib Unit Tests"

Definition at line 55 of file CpuExceptionHandlerTest.h.

◆ UNIT_TEST_APP_VERSION

#define UNIT_TEST_APP_VERSION   "1.0"

Definition at line 56 of file CpuExceptionHandlerTest.h.

Function Documentation

◆ AddCommonTestCase()

EFI_STATUS AddCommonTestCase ( IN UNIT_TEST_FRAMEWORK_HANDLE  Framework)

Create CpuExceptionLibUnitTestSuite and add test case.

Parameters
[in]FrameworkHandleUnit test framework.
Returns
EFI_SUCCESS The unit test suite was created.
Return values
EFI_OUT_OF_RESOURCESThere are not enough resources available to initialize the unit test suite.

Definition at line 828 of file CpuExceptionHandlerTestCommon.c.

◆ AdjustCpuContextHandler()

VOID EFIAPI AdjustCpuContextHandler ( IN EFI_EXCEPTION_TYPE  ExceptionType,
IN EFI_SYSTEM_CONTEXT  SystemContext 
)

Special handler for ConsistencyOfCpuContext test case. General register in SystemContext is modified to mExpectedContextInHandler in this handler.

Parameters
ExceptionTypeException type.
SystemContextPointer to EFI_SYSTEM_CONTEXT.

Special handler for ConsistencyOfCpuContext test case.

Parameters
ExceptionTypeException type.
SystemContextPointer to EFI_SYSTEM_CONTEXT.

Definition at line 49 of file ArchExceptionHandlerTest.c.

◆ AdjustRipForFaultHandler()

VOID EFIAPI AdjustRipForFaultHandler ( IN EFI_EXCEPTION_TYPE  ExceptionType,
IN EFI_SYSTEM_CONTEXT  SystemContext 
)

Special handler for fault exception. This handler sets Rip/Eip in SystemContext to the instruction address after the exception instruction.

Parameters
ExceptionTypeException type.
SystemContextPointer to EFI_SYSTEM_CONTEXT.

Special handler for fault exception. Rip/Eip in SystemContext will be modified to the instruction after the exception instruction.

Parameters
ExceptionTypeException type.
SystemContextPointer to EFI_SYSTEM_CONTEXT.

Definition at line 32 of file ArchExceptionHandlerTest.c.

◆ AsmTestConsistencyOfCpuContext()

VOID EFIAPI AsmTestConsistencyOfCpuContext ( IN EFI_EXCEPTION_TYPE  ExceptionType,
IN UINTN FaultParameter  OPTIONAL 
)

Test consistency of Cpu context. Four steps:

  1. Set Cpu register to mExpectedContextInHandler before exception.
  2. Trigger exception specified by ExceptionType.
  3. Store SystemContext in mActualContextInHandler and set SystemContext to mExpectedContextAfterException in handler.
  4. After return from exception, store Cpu registers in mActualContextAfterException.

Rcx/Ecx in mExpectedContextInHandler is decided by different exception type runtime since Rcx/Ecx is needed in assembly code. For GP and PF, Rcx/Ecx is set to FaultParameter. For other exception triggered by INTn, Rcx/Ecx is set to ExceptionType.

Parameters
[in]ExceptionTypeException type.
[in]FaultParameterParameter for GP and PF. OPTIONAL

◆ CompareCpuContext()

UNIT_TEST_STATUS CompareCpuContext ( VOID  )

Compare cpu context in ConsistencyOfCpuContext test case. 1.Compare mActualContextInHandler with mExpectedContextInHandler. 2.Compare mActualContextAfterException with mActualContextAfterException.

Return values
UNIT_TEST_PASSEDThe Unit test has completed and it was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Compare cpu context in ConsistencyOfCpuContext test case. 1.Compare mActualContextInHandler with mExpectedContextInHandler. 2.Compare mActualContextAfterException with mExpectedContextAfterException.

Return values
UNIT_TEST_PASSEDThe Unit test has completed and it was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 94 of file ArchExceptionHandlerTest.c.

◆ CpuStackGuardExceptionHandler()

VOID EFIAPI CpuStackGuardExceptionHandler ( IN EFI_EXCEPTION_TYPE  ExceptionType,
IN EFI_SYSTEM_CONTEXT  SystemContext 
)

Special handler for CpuStackGuard test case.

Parameters
ExceptionTypeException type.
SystemContextPointer to EFI_SYSTEM_CONTEXT.

Definition at line 123 of file ArchExceptionHandlerTest.c.

◆ GetMpServices()

EFI_STATUS GetMpServices ( OUT MP_SERVICES MpServices)

Get EFI_MP_SERVICES_PROTOCOL/EDKII_PEI_MP_SERVICES2_PPI pointer.

Parameters
[out]MpServicesPointer to the MP_SERVICES buffer
Return values
EFI_SUCCESSEFI_MP_SERVICES_PROTOCOL/PPI interface is returned
EFI_NOT_FOUNDEFI_MP_SERVICES_PROTOCOL/PPI interface is not found

Get EFI_MP_SERVICES_PROTOCOL pointer.

Parameters
[out]MpServicesPointer to the buffer where EFI_MP_SERVICES_PROTOCOL is stored
Return values
EFI_SUCCESSEFI_MP_SERVICES_PROTOCOL interface is returned
EFI_NOT_FOUNDEFI_MP_SERVICES_PROTOCOL interface is not found

Get EDKII_PEI_MP_SERVICES2_PPI pointer.

Parameters
[out]MpServicesPointer to the buffer where EDKII_PEI_MP_SERVICES2_PPI is stored
Return values
EFI_SUCCESSEDKII_PEI_MP_SERVICES2_PPI interface is returned
EFI_NOT_FOUNDEDKII_PEI_MP_SERVICES2_PPI interface is not found

Definition at line 141 of file DxeCpuExceptionHandlerUnitTest.c.

◆ InitializeBspIdt()

VOID * InitializeBspIdt ( VOID  )

Initialize Bsp Idt with a new Idt table and return the IA32_DESCRIPTOR buffer. In PEIM, store original PeiServicePointer before new Idt table.

Returns
Pointer to the allocated IA32_DESCRIPTOR buffer.

Definition at line 20 of file DxeCpuExceptionHandlerUnitTest.c.

◆ MpServicesUnitTestGetNumberOfProcessors()

EFI_STATUS MpServicesUnitTestGetNumberOfProcessors ( IN MP_SERVICES  MpServices,
OUT UINTN NumberOfProcessors,
OUT UINTN NumberOfEnabledProcessors 
)

Retrieve the number of logical processor in the platform and the number of those logical processors that are enabled on this boot.

Parameters
[in]MpServicesMP_SERVICES structure.
[out]NumberOfProcessorsPointer to the total number of logical processors in the system, including the BSP and disabled APs.
[out]NumberOfEnabledProcessorsPointer to the number of processors in the system that are enabled.
Return values
EFI_SUCCESSRetrieve the number of logical processor successfully
OthersRetrieve the number of logical processor unsuccessfully

Definition at line 51 of file DxeCpuExceptionHandlerUnitTest.c.

◆ MpServicesUnitTestStartupAllAPs()

EFI_STATUS MpServicesUnitTestStartupAllAPs ( IN MP_SERVICES  MpServices,
IN EFI_AP_PROCEDURE  Procedure,
IN BOOLEAN  SingleThread,
IN UINTN  TimeoutInMicroSeconds,
IN VOID *  ProcedureArgument 
)

Execute a caller provided function on all enabled APs.

Parameters
[in]MpServicesMP_SERVICES structure.
[in]ProcedurePointer to the function to be run on enabled APs of the system.
[in]SingleThreadIf TRUE, then all the enabled APs execute the function specified by Procedure one by one, in ascending order of processor handle number. If FALSE, then all the enabled APs execute the function specified by Procedure simultaneously.
[in]TimeoutInMicrosecondsIndicates the time limit in microseconds for APs to return from Procedure, for blocking mode only. Zero means infinity.
[in]ProcedureArgumentThe parameter passed into Procedure for all APs.
Return values
EFI_SUCCESSExecute a caller provided function on all enabled APs successfully
OthersExecute a caller provided function on all enabled APs unsuccessfully

Execute a caller provided function on all enabled APs.

Parameters
[in]MpServicesMP_SERVICES structure.
[in]ProcedurePointer to the function to be run on enabled APs of the system.
[in]SingleThreadIf TRUE, then all the enabled APs execute the function specified by Procedure one by one, in ascending order of processor handle number. If FALSE, then all the enabled APs execute the function specified by Procedure simultaneously.
[in]TimeoutInMicroSecondsIndicates the time limit in microseconds for APs to return from Procedure, for blocking mode only. Zero means infinity.
[in]ProcedureArgumentThe parameter passed into Procedure for all APs.
Return values
EFI_SUCCESSExecute a caller provided function on all enabled APs successfully
OthersExecute a caller provided function on all enabled APs unsuccessfully

Definition at line 121 of file DxeCpuExceptionHandlerUnitTest.c.

◆ MpServicesUnitTestStartupThisAP()

EFI_STATUS MpServicesUnitTestStartupThisAP ( IN MP_SERVICES  MpServices,
IN EFI_AP_PROCEDURE  Procedure,
IN UINTN  ProcessorNumber,
IN UINTN  TimeoutInMicroSeconds,
IN VOID *  ProcedureArgument 
)

Caller gets one enabled AP to execute a caller-provided function.

Parameters
[in]MpServicesMP_SERVICES structure.
[in]ProcedurePointer to the function to be run on enabled APs of the system.
[in]ProcessorNumberThe handle number of the AP.
[in]TimeoutInMicrosecondsIndicates the time limit in microseconds for APs to return from Procedure, for blocking mode only. Zero means infinity.
[in]ProcedureArgumentThe parameter passed into Procedure for all APs.
Return values
EFI_SUCCESSCaller gets one enabled AP to execute a caller-provided function successfully
OthersCaller gets one enabled AP to execute a caller-provided function unsuccessfully

Caller gets one enabled AP to execute a caller-provided function.

Parameters
[in]MpServicesMP_SERVICES structure.
[in]ProcedurePointer to the function to be run on enabled APs of the system.
[in]ProcessorNumberThe handle number of the AP.
[in]TimeoutInMicroSecondsIndicates the time limit in microseconds for APs to return from Procedure, for blocking mode only. Zero means infinity.
[in]ProcedureArgumentThe parameter passed into Procedure for all APs.
Return values
EFI_SUCCESSCaller gets one enabled AP to execute a caller-provided function successfully
OthersCaller gets one enabled AP to execute a caller-provided function unsuccessfully

Definition at line 93 of file DxeCpuExceptionHandlerUnitTest.c.

◆ MpServicesUnitTestWhoAmI()

EFI_STATUS MpServicesUnitTestWhoAmI ( IN MP_SERVICES  MpServices,
OUT UINTN ProcessorNumber 
)

Get the handle number for the calling processor.

Parameters
[in]MpServicesMP_SERVICES structure.
[out]ProcessorNumberThe handle number for the calling processor.
Return values
EFI_SUCCESSGet the handle number for the calling processor successfully.
OthersGet the handle number for the calling processor unsuccessfully.

Definition at line 70 of file DxeCpuExceptionHandlerUnitTest.c.

◆ TriggerGPException()

VOID EFIAPI TriggerGPException ( UINTN  Cr4ReservedBit)

Trigger GP exception by setting CR4_RESERVED_BIT to 1.

Parameters
[in]Cr4ReservedBitCr4 reserved bit.

◆ TriggerINTnException()

VOID EFIAPI TriggerINTnException ( IN EFI_EXCEPTION_TYPE  ExceptionType)

Trigger no error code exception by INT n instruction.

Parameters
[in]ExceptionTypeNo error code exception type.

◆ TriggerPFException()

VOID EFIAPI TriggerPFException ( UINTN  PFAddress)

Trigger PF exception by write to not present or ReadOnly address.

Parameters
[in]PFAddressNot present or ReadOnly address in page table.

◆ TriggerStackOverflow()

VOID EFIAPI TriggerStackOverflow ( VOID  )

Trigger stack overflow by calling itself continuously.

Variable Documentation

◆ mExceptionType

EFI_EXCEPTION_TYPE mExceptionType
extern

Definition at line 15 of file CpuExceptionHandlerTestCommon.c.

◆ mFaultInstructionLength

UINTN mFaultInstructionLength
extern

Definition at line 14 of file CpuExceptionHandlerTestCommon.c.

◆ mRspAddress

UINTN mRspAddress[]
extern

Definition at line 17 of file CpuExceptionHandlerTestCommon.c.