[edk2-devel] [Patch V2] UefiCpuPkg: Restore HpetTimer after CpuExceptionHandlerLib test

duntan posted 1 patch 1 year, 5 months ago
Failed in applying to current master (apply log)
UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerLibUnitTest.inf |  1 +
UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerUnitTest.c      | 30 +++++++++++++++++++++++++++++-
2 files changed, 30 insertions(+), 1 deletion(-)
[edk2-devel] [Patch V2] UefiCpuPkg: Restore HpetTimer after CpuExceptionHandlerLib test
Posted by duntan 1 year, 5 months ago
Disable/Restore HpetTimer before and after running the Dxe
CpuExceptionHandlerLib unit test module. During the UnitTest, a
new Idt is initialized for the test. There is no handler for timer
intrrupt in this new idt. After the test module, HpetTimer does
not work any more since the comparator value register and main
counter value register for timer does not match. To fix this issue,
disable/restore HpetTimer before and after Unit Test if HpetTimer
driver has been dispatched. We don't need to send Apic Eoi in this
unit test module.When disabling timer, after RaiseTPL(), if there
is a pending timer interrupt, bit64 of Interrupt Request Register
(IRR) will be set to 1 to indicate there is a pending timer
interrupt. After RestoreTPL(), CPU will handle the pending
interrupt in IRR.Then TimerInterruptHandler calls SendApicEoi().

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
---
 UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerLibUnitTest.inf |  1 +
 UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerUnitTest.c      | 30 +++++++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerLibUnitTest.inf b/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerLibUnitTest.inf
index e3dbe7b9ab..a904eb2504 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerLibUnitTest.inf
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerLibUnitTest.inf
@@ -53,6 +53,7 @@
 
 [Protocols]
   gEfiMpServiceProtocolGuid
+  gEfiTimerArchProtocolGuid
 
 [Depex]
   gEfiMpServiceProtocolGuid
diff --git a/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerUnitTest.c b/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerUnitTest.c
index 917fc549bf..1cec3ed809 100644
--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerUnitTest.c
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHandlerUnitTest.c
@@ -8,6 +8,7 @@
 
 #include "CpuExceptionHandlerTest.h"
 #include <Library/UefiBootServicesTableLib.h>
+#include <Protocol/Timer.h>
 
 /**
   Initialize Bsp Idt with a new Idt table and return the IA32_DESCRIPTOR buffer.
@@ -162,8 +163,12 @@ CpuExceptionHandlerTestEntry (
 {
   EFI_STATUS                  Status;
   UNIT_TEST_FRAMEWORK_HANDLE  Framework;
+  EFI_TIMER_ARCH_PROTOCOL     *TimerArchProtocol;
+  UINT64                      TimerPeriod;
 
-  Framework = NULL;
+  Framework         = NULL;
+  TimerArchProtocol = NULL;
+  TimerPeriod       = 0;
 
   DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
 
@@ -182,11 +187,34 @@ CpuExceptionHandlerTestEntry (
     goto EXIT;
   }
 
+  //
+  // If HpetTimer driver has been dispatched, disable HpetTimer before Unit Test.
+  //
+  gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID **)&TimerArchProtocol);
+  if (TimerArchProtocol != NULL) {
+    Status = TimerArchProtocol->GetTimerPeriod (TimerArchProtocol, &TimerPeriod);
+    ASSERT_EFI_ERROR (Status);
+    if (TimerPeriod > 0) {
+      DEBUG ((DEBUG_INFO, "HpetTimer has been dispatched. Disable HpetTimer.\n"));
+      Status = TimerArchProtocol->SetTimerPeriod (TimerArchProtocol, 0);
+      ASSERT_EFI_ERROR (Status);
+    }
+  }
+
   //
   // Execute the tests.
   //
   Status = RunAllTestSuites (Framework);
 
+  //
+  // Restore HpetTimer after Unit Test.
+  //
+  if ((TimerArchProtocol != NULL) && (TimerPeriod > 0)) {
+    DEBUG ((DEBUG_INFO, "Restore HpetTimer after DxeCpuExceptionHandlerLib UnitTest.\n"));
+    Status = TimerArchProtocol->SetTimerPeriod (TimerArchProtocol, TimerPeriod);
+    ASSERT_EFI_ERROR (Status);
+  }
+
 EXIT:
   if (Framework) {
     FreeUnitTestFramework (Framework);
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#95663): https://edk2.groups.io/g/devel/message/95663
Mute This Topic: https://groups.io/mt/94619956/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-
Re: [edk2-devel] [Patch V2] UefiCpuPkg: Restore HpetTimer after CpuExceptionHandlerLib test
Posted by Ni, Ray 1 year, 5 months ago
Reviewed-by: Ray Ni <ray.ni@intel.com>

> -----Original Message-----
> From: Tan, Dun <dun.tan@intel.com>
> Sent: Friday, October 28, 2022 11:51 AM
> To: devel@edk2.groups.io
> Cc: Dong, Eric <eric.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Kumar,
> Rahul R <rahul.r.kumar@intel.com>; Kinney, Michael D
> <michael.d.kinney@intel.com>
> Subject: [Patch V2] UefiCpuPkg: Restore HpetTimer after
> CpuExceptionHandlerLib test
> 
> Disable/Restore HpetTimer before and after running the Dxe
> CpuExceptionHandlerLib unit test module. During the UnitTest, a
> new Idt is initialized for the test. There is no handler for timer
> intrrupt in this new idt. After the test module, HpetTimer does
> not work any more since the comparator value register and main
> counter value register for timer does not match. To fix this issue,
> disable/restore HpetTimer before and after Unit Test if HpetTimer
> driver has been dispatched. We don't need to send Apic Eoi in this
> unit test module.When disabling timer, after RaiseTPL(), if there
> is a pending timer interrupt, bit64 of Interrupt Request Register
> (IRR) will be set to 1 to indicate there is a pending timer
> interrupt. After RestoreTPL(), CPU will handle the pending
> interrupt in IRR.Then TimerInterruptHandler calls SendApicEoi().
> 
> Signed-off-by: Dun Tan <dun.tan@intel.com>
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Rahul Kumar <rahul1.kumar@intel.com>
> Cc: Michael D Kinney <michael.d.kinney@intel.com>
> ---
> 
> UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHan
> dlerLibUnitTest.inf |  1 +
> 
> UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionHan
> dlerUnitTest.c      | 30 +++++++++++++++++++++++++++++-
>  2 files changed, 30 insertions(+), 1 deletion(-)
> 
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionH
> andlerLibUnitTest.inf
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionH
> andlerLibUnitTest.inf
> index e3dbe7b9ab..a904eb2504 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionH
> andlerLibUnitTest.inf
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionH
> andlerLibUnitTest.inf
> @@ -53,6 +53,7 @@
> 
>  [Protocols]
>    gEfiMpServiceProtocolGuid
> +  gEfiTimerArchProtocolGuid
> 
>  [Depex]
>    gEfiMpServiceProtocolGuid
> diff --git
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionH
> andlerUnitTest.c
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionH
> andlerUnitTest.c
> index 917fc549bf..1cec3ed809 100644
> ---
> a/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionH
> andlerUnitTest.c
> +++
> b/UefiCpuPkg/Library/CpuExceptionHandlerLib/UnitTest/DxeCpuExceptionH
> andlerUnitTest.c
> @@ -8,6 +8,7 @@
> 
>  #include "CpuExceptionHandlerTest.h"
>  #include <Library/UefiBootServicesTableLib.h>
> +#include <Protocol/Timer.h>
> 
>  /**
>    Initialize Bsp Idt with a new Idt table and return the IA32_DESCRIPTOR
> buffer.
> @@ -162,8 +163,12 @@ CpuExceptionHandlerTestEntry (
>  {
>    EFI_STATUS                  Status;
>    UNIT_TEST_FRAMEWORK_HANDLE  Framework;
> +  EFI_TIMER_ARCH_PROTOCOL     *TimerArchProtocol;
> +  UINT64                      TimerPeriod;
> 
> -  Framework = NULL;
> +  Framework         = NULL;
> +  TimerArchProtocol = NULL;
> +  TimerPeriod       = 0;
> 
>    DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME,
> UNIT_TEST_APP_VERSION));
> 
> @@ -182,11 +187,34 @@ CpuExceptionHandlerTestEntry (
>      goto EXIT;
>    }
> 
> +  //
> +  // If HpetTimer driver has been dispatched, disable HpetTimer before Unit
> Test.
> +  //
> +  gBS->LocateProtocol (&gEfiTimerArchProtocolGuid, NULL, (VOID
> **)&TimerArchProtocol);
> +  if (TimerArchProtocol != NULL) {
> +    Status = TimerArchProtocol->GetTimerPeriod (TimerArchProtocol,
> &TimerPeriod);
> +    ASSERT_EFI_ERROR (Status);
> +    if (TimerPeriod > 0) {
> +      DEBUG ((DEBUG_INFO, "HpetTimer has been dispatched. Disable
> HpetTimer.\n"));
> +      Status = TimerArchProtocol->SetTimerPeriod (TimerArchProtocol, 0);
> +      ASSERT_EFI_ERROR (Status);
> +    }
> +  }
> +
>    //
>    // Execute the tests.
>    //
>    Status = RunAllTestSuites (Framework);
> 
> +  //
> +  // Restore HpetTimer after Unit Test.
> +  //
> +  if ((TimerArchProtocol != NULL) && (TimerPeriod > 0)) {
> +    DEBUG ((DEBUG_INFO, "Restore HpetTimer after
> DxeCpuExceptionHandlerLib UnitTest.\n"));
> +    Status = TimerArchProtocol->SetTimerPeriod (TimerArchProtocol,
> TimerPeriod);
> +    ASSERT_EFI_ERROR (Status);
> +  }
> +
>  EXIT:
>    if (Framework) {
>      FreeUnitTestFramework (Framework);
> --
> 2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#95673): https://edk2.groups.io/g/devel/message/95673
Mute This Topic: https://groups.io/mt/94619956/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-