TianoCore EDK2 master
Loading...
Searching...
No Matches
ArchExceptionHandlerTest.c
Go to the documentation of this file.
1
10
11GENERAL_REGISTER_IA32 mActualContextInHandler;
12GENERAL_REGISTER_IA32 mActualContextAfterException;
13
14//
15// In TestCpuContextConsistency, Cpu registers will be set to mExpectedContextInHandler/mExpectedContextAfterException.
16// Ecx in mExpectedContextInHandler is set runtime since Ecx is needed in assembly code.
17// For GP and PF, Ecx is set to FaultParameter. For other exception triggered by INTn, Ecx is set to ExceptionType.
18//
19GENERAL_REGISTER_IA32 mExpectedContextInHandler = { 1, 2, 3, 4, 5, 0 };
20GENERAL_REGISTER_IA32 mExpectedContextAfterException = { 11, 12, 13, 14, 15, 16 };
21
30VOID
31EFIAPI
33 IN EFI_EXCEPTION_TYPE ExceptionType,
34 IN EFI_SYSTEM_CONTEXT SystemContext
35 )
36{
37 mExceptionType = ExceptionType;
38 SystemContext.SystemContextIa32->Eip += mFaultInstructionLength;
39}
40
47VOID
48EFIAPI
50 IN EFI_EXCEPTION_TYPE ExceptionType,
51 IN EFI_SYSTEM_CONTEXT SystemContext
52 )
53{
54 //
55 // Store SystemContext in exception handler.
56 //
57 mActualContextInHandler.Edi = SystemContext.SystemContextIa32->Edi;
58 mActualContextInHandler.Esi = SystemContext.SystemContextIa32->Esi;
59 mActualContextInHandler.Ebx = SystemContext.SystemContextIa32->Ebx;
60 mActualContextInHandler.Edx = SystemContext.SystemContextIa32->Edx;
61 mActualContextInHandler.Ecx = SystemContext.SystemContextIa32->Ecx;
62 mActualContextInHandler.Eax = SystemContext.SystemContextIa32->Eax;
63
64 //
65 // Modify cpu context. These registers will be stored in mActualContextAfterException.
66 // Do not handle Esp and Ebp in SystemContext. CpuExceptionHandlerLib doesn't set Esp and
67 // Esp register to the value in SystemContext.
68 //
69 SystemContext.SystemContextIa32->Edi = mExpectedContextAfterException.Edi;
70 SystemContext.SystemContextIa32->Esi = mExpectedContextAfterException.Esi;
71 SystemContext.SystemContextIa32->Ebx = mExpectedContextAfterException.Ebx;
72 SystemContext.SystemContextIa32->Edx = mExpectedContextAfterException.Edx;
73 SystemContext.SystemContextIa32->Ecx = mExpectedContextAfterException.Ecx;
74 SystemContext.SystemContextIa32->Eax = mExpectedContextAfterException.Eax;
75
76 //
77 // When fault exception happens, eip/rip points to the faulting instruction.
78 // For now, olny GP and PF are tested in fault exception.
79 //
80 if ((ExceptionType == EXCEPT_IA32_PAGE_FAULT) || (ExceptionType == EXCEPT_IA32_GP_FAULT)) {
81 AdjustRipForFaultHandler (ExceptionType, SystemContext);
82 }
83}
84
95 VOID
96 )
97{
98 UT_ASSERT_EQUAL (mActualContextInHandler.Edi, mExpectedContextInHandler.Edi);
99 UT_ASSERT_EQUAL (mActualContextInHandler.Esi, mExpectedContextInHandler.Esi);
100 UT_ASSERT_EQUAL (mActualContextInHandler.Ebx, mExpectedContextInHandler.Ebx);
101 UT_ASSERT_EQUAL (mActualContextInHandler.Edx, mExpectedContextInHandler.Edx);
102 UT_ASSERT_EQUAL (mActualContextInHandler.Ecx, mExpectedContextInHandler.Ecx);
103 UT_ASSERT_EQUAL (mActualContextInHandler.Eax, mExpectedContextInHandler.Eax);
104
105 UT_ASSERT_EQUAL (mActualContextAfterException.Edi, mExpectedContextAfterException.Edi);
106 UT_ASSERT_EQUAL (mActualContextAfterException.Esi, mExpectedContextAfterException.Esi);
107 UT_ASSERT_EQUAL (mActualContextAfterException.Ebx, mExpectedContextAfterException.Ebx);
108 UT_ASSERT_EQUAL (mActualContextAfterException.Edx, mExpectedContextAfterException.Edx);
109 UT_ASSERT_EQUAL (mActualContextAfterException.Ecx, mExpectedContextAfterException.Ecx);
110 UT_ASSERT_EQUAL (mActualContextAfterException.Eax, mExpectedContextAfterException.Eax);
111 return UNIT_TEST_PASSED;
112}
113
121VOID
122EFIAPI
124 IN EFI_EXCEPTION_TYPE ExceptionType,
125 IN EFI_SYSTEM_CONTEXT SystemContext
126 )
127{
128 UINTN LocalVariable;
129
130 AdjustRipForFaultHandler (ExceptionType, SystemContext);
131 mRspAddress[0] = (UINTN)SystemContext.SystemContextIa32->Esp;
132 mRspAddress[1] = (UINTN)(&LocalVariable);
133
134 return;
135}
UINT64 UINTN
VOID EFIAPI AdjustCpuContextHandler(IN EFI_EXCEPTION_TYPE ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext)
VOID EFIAPI AdjustRipForFaultHandler(IN EFI_EXCEPTION_TYPE ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext)
VOID EFIAPI CpuStackGuardExceptionHandler(IN EFI_EXCEPTION_TYPE ExceptionType, IN EFI_SYSTEM_CONTEXT SystemContext)
UNIT_TEST_STATUS CompareCpuContext(VOID)
#define IN
Definition: Base.h:279
INTN EFI_EXCEPTION_TYPE
Definition: DebugSupport.h:35
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16