UefiCpuPkg: CpuTimerDxeRiscV64: fix tick duration accounting

The TimerDxe implementation doesn't account for the physical
time passed due to timer handler execution or (perhaps even
more importantly) time spent with interrupts masked.

Other implementations (e.g. like the Arm one) do. If the
timer tick is always incremented at a fixed rate, then
you can slow down UEFI's perception of time by running
long sections of code in a critical section.

Cc: Daniel Schaefer <git@danielschaefer.me>
Reviewed-by: Sunil V L <sunilvl@ventanamicro.com>
Signed-off-by: Andrei Warkentin <andrei.warkentin@intel.com>
This commit is contained in:
Andrei Warkentin 2023-02-17 18:44:51 -06:00 committed by mergify[bot]
parent db0a3087a5
commit 5ad2592ab3

View File

@ -41,6 +41,7 @@ STATIC EFI_TIMER_NOTIFY mTimerNotifyFunction;
// The current period of the timer interrupt // The current period of the timer interrupt
// //
STATIC UINT64 mTimerPeriod = 0; STATIC UINT64 mTimerPeriod = 0;
STATIC UINT64 mLastPeriodStart = 0;
/** /**
Timer Interrupt Handler. Timer Interrupt Handler.
@ -56,25 +57,33 @@ TimerInterruptHandler (
) )
{ {
EFI_TPL OriginalTPL; EFI_TPL OriginalTPL;
UINT64 RiscvTimer; UINT64 PeriodStart;
PeriodStart = RiscVReadTimer ();
OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL); OriginalTPL = gBS->RaiseTPL (TPL_HIGH_LEVEL);
if (mTimerNotifyFunction != NULL) { if (mTimerNotifyFunction != NULL) {
mTimerNotifyFunction (mTimerPeriod); //
// For any number of reasons, the ticks could be coming
// in slower than mTimerPeriod. For example, some code
// is doing a *lot* of stuff inside an EFI_TPL_HIGH
// critical section, and this should not cause the EFI
// time to increment slower. So when we take an interrupt,
// account for the actual time passed.
//
mTimerNotifyFunction (PeriodStart - mLastPeriodStart);
} }
RiscVDisableTimerInterrupt (); // Disable SMode timer int
RiscVClearPendingTimerInterrupt ();
if (mTimerPeriod == 0) { if (mTimerPeriod == 0) {
RiscVDisableTimerInterrupt ();
gBS->RestoreTPL (OriginalTPL); gBS->RestoreTPL (OriginalTPL);
RiscVDisableTimerInterrupt (); // Disable SMode timer int
return; return;
} }
RiscvTimer = RiscVReadTimer (); mLastPeriodStart = PeriodStart;
SbiSetTimer (RiscvTimer += mTimerPeriod); SbiSetTimer (PeriodStart += mTimerPeriod);
gBS->RestoreTPL (OriginalTPL);
RiscVEnableTimerInterrupt (); // enable SMode timer int RiscVEnableTimerInterrupt (); // enable SMode timer int
gBS->RestoreTPL (OriginalTPL);
} }
/** /**
@ -154,8 +163,6 @@ TimerDriverSetTimerPeriod (
IN UINT64 TimerPeriod IN UINT64 TimerPeriod
) )
{ {
UINT64 RiscvTimer;
DEBUG ((DEBUG_INFO, "TimerDriverSetTimerPeriod(0x%lx)\n", TimerPeriod)); DEBUG ((DEBUG_INFO, "TimerDriverSetTimerPeriod(0x%lx)\n", TimerPeriod));
if (TimerPeriod == 0) { if (TimerPeriod == 0) {
@ -165,8 +172,8 @@ TimerDriverSetTimerPeriod (
} }
mTimerPeriod = TimerPeriod / 10; // convert unit from 100ns to 1us mTimerPeriod = TimerPeriod / 10; // convert unit from 100ns to 1us
RiscvTimer = RiscVReadTimer (); mLastPeriodStart = RiscVReadTimer ();
SbiSetTimer (RiscvTimer + mTimerPeriod); SbiSetTimer (mLastPeriodStart + mTimerPeriod);
mCpu->EnableInterrupt (mCpu); mCpu->EnableInterrupt (mCpu);
RiscVEnableTimerInterrupt (); // enable SMode timer int RiscVEnableTimerInterrupt (); // enable SMode timer int