TianoCore EDK2 master
Loading...
Searching...
No Matches
ArchExceptionHandlerTest.c
Go to the documentation of this file.
1
10
11GENERAL_REGISTER mActualContextInHandler;
12GENERAL_REGISTER mActualContextAfterException;
13
14//
15// In TestCpuContextConsistency, Cpu registers will be set to mExpectedContextInHandler/mExpectedContextAfterException.
16// Rcx in mExpectedContextInHandler is set runtime since Rcx is needed in assembly code.
17// For GP and PF, Rcx is set to FaultParameter. For other exception triggered by INTn, Rcx is set to ExceptionType.
18//
19GENERAL_REGISTER mExpectedContextInHandler = { 1, 2, 3, 4, 5, 0, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe };
20GENERAL_REGISTER mExpectedContextAfterException = { 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e };
21
29VOID
30EFIAPI
32 IN EFI_EXCEPTION_TYPE ExceptionType,
33 IN EFI_SYSTEM_CONTEXT SystemContext
34 )
35{
36 mExceptionType = ExceptionType;
37 SystemContext.SystemContextX64->Rip += mFaultInstructionLength;
38}
39
46VOID
47EFIAPI
49 IN EFI_EXCEPTION_TYPE ExceptionType,
50 IN EFI_SYSTEM_CONTEXT SystemContext
51 )
52{
53 //
54 // Store SystemContext in mActualContextInHandler.
55 //
56 mActualContextInHandler.Rdi = SystemContext.SystemContextX64->Rdi;
57 mActualContextInHandler.Rsi = SystemContext.SystemContextX64->Rsi;
58 mActualContextInHandler.Rbx = SystemContext.SystemContextX64->Rbx;
59 mActualContextInHandler.Rdx = SystemContext.SystemContextX64->Rdx;
60 mActualContextInHandler.Rcx = SystemContext.SystemContextX64->Rcx;
61 mActualContextInHandler.Rax = SystemContext.SystemContextX64->Rax;
62 mActualContextInHandler.R8 = SystemContext.SystemContextX64->R8;
63 mActualContextInHandler.R9 = SystemContext.SystemContextX64->R9;
64 mActualContextInHandler.R10 = SystemContext.SystemContextX64->R10;
65 mActualContextInHandler.R11 = SystemContext.SystemContextX64->R11;
66 mActualContextInHandler.R12 = SystemContext.SystemContextX64->R12;
67 mActualContextInHandler.R13 = SystemContext.SystemContextX64->R13;
68 mActualContextInHandler.R14 = SystemContext.SystemContextX64->R14;
69 mActualContextInHandler.R15 = SystemContext.SystemContextX64->R15;
70
71 //
72 // Modify cpu context. These registers will be stored in mActualContextAfterException.
73 // Do not handle Rsp and Rbp. CpuExceptionHandlerLib doesn't set Rsp and Rbp register
74 // to the value in SystemContext.
75 //
76 SystemContext.SystemContextX64->Rdi = mExpectedContextAfterException.Rdi;
77 SystemContext.SystemContextX64->Rsi = mExpectedContextAfterException.Rsi;
78 SystemContext.SystemContextX64->Rbx = mExpectedContextAfterException.Rbx;
79 SystemContext.SystemContextX64->Rdx = mExpectedContextAfterException.Rdx;
80 SystemContext.SystemContextX64->Rcx = mExpectedContextAfterException.Rcx;
81 SystemContext.SystemContextX64->Rax = mExpectedContextAfterException.Rax;
82 SystemContext.SystemContextX64->R8 = mExpectedContextAfterException.R8;
83 SystemContext.SystemContextX64->R9 = mExpectedContextAfterException.R9;
84 SystemContext.SystemContextX64->R10 = mExpectedContextAfterException.R10;
85 SystemContext.SystemContextX64->R11 = mExpectedContextAfterException.R11;
86 SystemContext.SystemContextX64->R12 = mExpectedContextAfterException.R12;
87 SystemContext.SystemContextX64->R13 = mExpectedContextAfterException.R13;
88 SystemContext.SystemContextX64->R14 = mExpectedContextAfterException.R14;
89 SystemContext.SystemContextX64->R15 = mExpectedContextAfterException.R15;
90
91 //
92 // When fault exception happens, eip/rip points to the faulting instruction.
93 // For now, olny GP and PF are tested in fault exception.
94 //
95 if ((ExceptionType == EXCEPT_IA32_PAGE_FAULT) || (ExceptionType == EXCEPT_IA32_GP_FAULT)) {
96 AdjustRipForFaultHandler (ExceptionType, SystemContext);
97 }
98}
99
110 VOID
111 )
112{
113 UT_ASSERT_EQUAL (mActualContextInHandler.Rdi, mExpectedContextInHandler.Rdi);
114 UT_ASSERT_EQUAL (mActualContextInHandler.Rsi, mExpectedContextInHandler.Rsi);
115 UT_ASSERT_EQUAL (mActualContextInHandler.Rbx, mExpectedContextInHandler.Rbx);
116 UT_ASSERT_EQUAL (mActualContextInHandler.Rdx, mExpectedContextInHandler.Rdx);
117 UT_ASSERT_EQUAL (mActualContextInHandler.Rcx, mExpectedContextInHandler.Rcx);
118 UT_ASSERT_EQUAL (mActualContextInHandler.Rax, mExpectedContextInHandler.Rax);
119 UT_ASSERT_EQUAL (mActualContextInHandler.R8, mExpectedContextInHandler.R8);
120 UT_ASSERT_EQUAL (mActualContextInHandler.R9, mExpectedContextInHandler.R9);
121 UT_ASSERT_EQUAL (mActualContextInHandler.R10, mExpectedContextInHandler.R10);
122 UT_ASSERT_EQUAL (mActualContextInHandler.R11, mExpectedContextInHandler.R11);
123 UT_ASSERT_EQUAL (mActualContextInHandler.R12, mExpectedContextInHandler.R12);
124 UT_ASSERT_EQUAL (mActualContextInHandler.R13, mExpectedContextInHandler.R13);
125 UT_ASSERT_EQUAL (mActualContextInHandler.R14, mExpectedContextInHandler.R14);
126 UT_ASSERT_EQUAL (mActualContextInHandler.R15, mExpectedContextInHandler.R15);
127
128 UT_ASSERT_EQUAL (mActualContextAfterException.Rdi, mExpectedContextAfterException.Rdi);
129 UT_ASSERT_EQUAL (mActualContextAfterException.Rsi, mExpectedContextAfterException.Rsi);
130 UT_ASSERT_EQUAL (mActualContextAfterException.Rbx, mExpectedContextAfterException.Rbx);
131 UT_ASSERT_EQUAL (mActualContextAfterException.Rdx, mExpectedContextAfterException.Rdx);
132 UT_ASSERT_EQUAL (mActualContextAfterException.Rcx, mExpectedContextAfterException.Rcx);
133 UT_ASSERT_EQUAL (mActualContextAfterException.Rax, mExpectedContextAfterException.Rax);
134 UT_ASSERT_EQUAL (mActualContextAfterException.R8, mExpectedContextAfterException.R8);
135 UT_ASSERT_EQUAL (mActualContextAfterException.R9, mExpectedContextAfterException.R9);
136 UT_ASSERT_EQUAL (mActualContextAfterException.R10, mExpectedContextAfterException.R10);
137 UT_ASSERT_EQUAL (mActualContextAfterException.R11, mExpectedContextAfterException.R11);
138 UT_ASSERT_EQUAL (mActualContextAfterException.R12, mExpectedContextAfterException.R12);
139 UT_ASSERT_EQUAL (mActualContextAfterException.R13, mExpectedContextAfterException.R13);
140 UT_ASSERT_EQUAL (mActualContextAfterException.R14, mExpectedContextAfterException.R14);
141 UT_ASSERT_EQUAL (mActualContextAfterException.R15, mExpectedContextAfterException.R15);
142 return UNIT_TEST_PASSED;
143}
144
152VOID
153EFIAPI
155 IN EFI_EXCEPTION_TYPE ExceptionType,
156 IN EFI_SYSTEM_CONTEXT SystemContext
157 )
158{
159 UINTN LocalVariable;
160
161 AdjustRipForFaultHandler (ExceptionType, SystemContext);
162 mRspAddress[0] = (UINTN)SystemContext.SystemContextX64->Rsp;
163 mRspAddress[1] = (UINTN)(&LocalVariable);
164
165 return;
166}
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