Re: [PATCH 2/2] KVM: arm64: timers: Adjust CVAL of a ptimer across guest entry and exits

From: Marc Zyngier
Date: Thu Aug 17 2023 - 04:28:33 EST


[Fixing Christoffer's email address]

On Thu, 17 Aug 2023 07:03:14 +0100,
Ganapatrao Kulkarni <gankulkarni@xxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> As per FEAT_ECV, when HCR_EL2.{E2H, TGE} == {1, 1}, Enhanced Counter
> Virtualization functionality is disabled and CNTPOFF_EL2 value is treated
> as zero. On VHE host, E2H and TGE are set, hence it is required
> to adjust CVAL by incrementing it by CNTPOFF_EL2 after guest
> exit to avoid false physical timer interrupts and also
> decrement/restore CVAL before the guest entry.

No, this is wrong. Neither E2H nor TGE have any impact on writing to
CNTPOFF_EL2, nor does it have an impact on CNTP_CVAL_EL0. Just read
the pseudocode to convince yourself.

CNTPOFF_EL2 is applied at exactly two points: when SW is reading
CNTPCT_EL0 from EL1 while {E2H,TGE}=={1, 0} and when the HW is
comparing CNTPCT_EL0 with the CNTP_CVAL_EL0. In both cases the offset
is subtracted from the counter. And that's the point where the running
EL matters. Which means that CNTPOFF_EL2 behaves exactly like
CNTVOFF_EL2. No ifs, no buts.

The behaviour you are describing tends to indicate that your HW is
applying CNTPOFF_EL2 to CVAL, which would be a gold plated bug.

It doesn't mean that the KVM code is correct either. I'm seeing pretty
bad behaviours on a machine without CNTPOFF_EL2. But what you are
suggesting is IMO a misunderstanding of the architecture.

Thanks,

M.

--
Without deviation from the norm, progress is not possible.