]> git.proxmox.com Git - mirror_edk2.git/blob - UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerUnitTest.c
UefiCpuPkg: Add Unit tests for DxeCpuExceptionHandlerLib
[mirror_edk2.git] / UefiCpuPkg / Library / CpuExceptionHandlerLib / UnitTest / DxeCpuExceptionHandlerUnitTest.c
1 /** @file
2 Unit tests of the CpuExceptionHandlerLib.
3
4 Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include "CpuExceptionHandlerTest.h"
10 #include <Library/UefiBootServicesTableLib.h>
11
12 /**
13 Initialize Bsp Idt with a new Idt table and return the IA32_DESCRIPTOR buffer.
14 In PEIM, store original PeiServicePointer before new Idt table.
15
16 @return Pointer to the allocated IA32_DESCRIPTOR buffer.
17 **/
18 VOID *
19 InitializeBspIdt (
20 VOID
21 )
22 {
23 UINTN *NewIdtTable;
24 IA32_DESCRIPTOR *Idtr;
25
26 Idtr = AllocateZeroPool (sizeof (IA32_DESCRIPTOR));
27 ASSERT (Idtr != NULL);
28 NewIdtTable = AllocateZeroPool (sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM);
29 ASSERT (NewIdtTable != NULL);
30 Idtr->Base = (UINTN)NewIdtTable;
31 Idtr->Limit = (UINT16)(sizeof (IA32_IDT_GATE_DESCRIPTOR) * CPU_INTERRUPT_NUM - 1);
32
33 AsmWriteIdtr (Idtr);
34 return Idtr;
35 }
36
37 /**
38 Retrieve the number of logical processor in the platform and the number of those logical processors that
39 are enabled on this boot.
40
41 @param[in] MpServices MP_SERVICES structure.
42 @param[out] NumberOfProcessors Pointer to the total number of logical processors in the system, including
43 the BSP and disabled APs.
44 @param[out] NumberOfEnabledProcessors Pointer to the number of processors in the system that are enabled.
45
46 @retval EFI_SUCCESS Retrieve the number of logical processor successfully
47 @retval Others Retrieve the number of logical processor unsuccessfully
48 **/
49 EFI_STATUS
50 MpServicesUnitTestGetNumberOfProcessors (
51 IN MP_SERVICES MpServices,
52 OUT UINTN *NumberOfProcessors,
53 OUT UINTN *NumberOfEnabledProcessors
54 )
55 {
56 return MpServices.Protocol->GetNumberOfProcessors (MpServices.Protocol, NumberOfProcessors, NumberOfEnabledProcessors);
57 }
58
59 /**
60 Get the handle number for the calling processor.
61
62 @param[in] MpServices MP_SERVICES structure.
63 @param[out] ProcessorNumber The handle number for the calling processor.
64
65 @retval EFI_SUCCESS Get the handle number for the calling processor successfully.
66 @retval Others Get the handle number for the calling processor unsuccessfully.
67 **/
68 EFI_STATUS
69 MpServicesUnitTestWhoAmI (
70 IN MP_SERVICES MpServices,
71 OUT UINTN *ProcessorNumber
72 )
73 {
74 return MpServices.Protocol->WhoAmI (MpServices.Protocol, ProcessorNumber);
75 }
76
77 /**
78 Caller gets one enabled AP to execute a caller-provided function.
79
80 @param[in] MpServices MP_SERVICES structure.
81 @param[in] Procedure Pointer to the function to be run on enabled APs of the system.
82 @param[in] ProcessorNumber The handle number of the AP.
83 @param[in] TimeoutInMicroSeconds Indicates the time limit in microseconds for APs to return from Procedure,
84 for blocking mode only. Zero means infinity.
85 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.
86
87
88 @retval EFI_SUCCESS Caller gets one enabled AP to execute a caller-provided function successfully
89 @retval Others Caller gets one enabled AP to execute a caller-provided function unsuccessfully
90 **/
91 EFI_STATUS
92 MpServicesUnitTestStartupThisAP (
93 IN MP_SERVICES MpServices,
94 IN EFI_AP_PROCEDURE Procedure,
95 IN UINTN ProcessorNumber,
96 IN UINTN TimeoutInMicroSeconds,
97 IN VOID *ProcedureArgument
98 )
99 {
100 return MpServices.Protocol->StartupThisAP (MpServices.Protocol, Procedure, ProcessorNumber, NULL, TimeoutInMicroSeconds, ProcedureArgument, NULL);
101 }
102
103 /**
104 Execute a caller provided function on all enabled APs.
105
106 @param[in] MpServices MP_SERVICES structure.
107 @param[in] Procedure Pointer to the function to be run on enabled APs of the system.
108 @param[in] SingleThread If TRUE, then all the enabled APs execute the function specified by Procedure
109 one by one, in ascending order of processor handle number.
110 If FALSE, then all the enabled APs execute the function specified by Procedure
111 simultaneously.
112 @param[in] TimeoutInMicroSeconds Indicates the time limit in microseconds for APs to return from Procedure,
113 for blocking mode only. Zero means infinity.
114 @param[in] ProcedureArgument The parameter passed into Procedure for all APs.
115
116 @retval EFI_SUCCESS Execute a caller provided function on all enabled APs successfully
117 @retval Others Execute a caller provided function on all enabled APs unsuccessfully
118 **/
119 EFI_STATUS
120 MpServicesUnitTestStartupAllAPs (
121 IN MP_SERVICES MpServices,
122 IN EFI_AP_PROCEDURE Procedure,
123 IN BOOLEAN SingleThread,
124 IN UINTN TimeoutInMicroSeconds,
125 IN VOID *ProcedureArgument
126 )
127 {
128 return MpServices.Protocol->StartupAllAPs (MpServices.Protocol, Procedure, SingleThread, NULL, TimeoutInMicroSeconds, ProcedureArgument, NULL);
129 }
130
131 /**
132 Get EFI_MP_SERVICES_PROTOCOL pointer.
133
134 @param[out] MpServices Pointer to the buffer where EFI_MP_SERVICES_PROTOCOL is stored
135
136 @retval EFI_SUCCESS EFI_MP_SERVICES_PROTOCOL interface is returned
137 @retval EFI_NOT_FOUND EFI_MP_SERVICES_PROTOCOL interface is not found
138 **/
139 EFI_STATUS
140 GetMpServices (
141 OUT MP_SERVICES *MpServices
142 )
143 {
144 return gBS->LocateProtocol (&gEfiMpServiceProtocolGuid, NULL, (VOID **)&MpServices->Protocol);
145 }
146
147 /**
148 Entry for CpuExceptionHandlerDxeTest driver.
149
150 @param ImageHandle Image handle this driver.
151 @param SystemTable Pointer to the System Table.
152
153 @retval EFI_SUCCESS The driver executed normally.
154
155 **/
156 EFI_STATUS
157 EFIAPI
158 CpuExceptionHandlerTestEntry (
159 IN EFI_HANDLE ImageHandle,
160 IN EFI_SYSTEM_TABLE *SystemTable
161 )
162 {
163 EFI_STATUS Status;
164 UNIT_TEST_FRAMEWORK_HANDLE Framework;
165
166 Framework = NULL;
167
168 DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
169
170 //
171 // Start setting up the test framework for running the tests.
172 //
173 Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);
174 if (EFI_ERROR (Status)) {
175 DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
176 goto EXIT;
177 }
178
179 Status = AddCommonTestCase (Framework);
180 if (EFI_ERROR (Status)) {
181 DEBUG ((DEBUG_ERROR, "Failed in AddCommonTestCase. Status = %r\n", Status));
182 goto EXIT;
183 }
184
185 //
186 // Execute the tests.
187 //
188 Status = RunAllTestSuites (Framework);
189
190 EXIT:
191 if (Framework) {
192 FreeUnitTestFramework (Framework);
193 }
194
195 return Status;
196 }