Skip to content

Commit 7b6b463

Browse files
soccertackMarc Zyngier
authored andcommitted
KVM: arm/arm64: Emulate the EL1 phys timer registers
Emulate read and write operations to CNTP_TVAL, CNTP_CVAL and CNTP_CTL. Now VMs are able to use the EL1 physical timer. Signed-off-by: Jintack Lim <jintack@cs.columbia.edu> Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent c9a3c58 commit 7b6b463

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

arch/arm64/kvm/sys_regs.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,23 +824,54 @@ static bool access_cntp_tval(struct kvm_vcpu *vcpu,
824824
struct sys_reg_params *p,
825825
const struct sys_reg_desc *r)
826826
{
827-
kvm_inject_undefined(vcpu);
827+
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
828+
u64 now = kvm_phys_timer_read();
829+
830+
if (p->is_write)
831+
ptimer->cnt_cval = p->regval + now;
832+
else
833+
p->regval = ptimer->cnt_cval - now;
834+
828835
return true;
829836
}
830837

831838
static bool access_cntp_ctl(struct kvm_vcpu *vcpu,
832839
struct sys_reg_params *p,
833840
const struct sys_reg_desc *r)
834841
{
835-
kvm_inject_undefined(vcpu);
842+
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
843+
844+
if (p->is_write) {
845+
/* ISTATUS bit is read-only */
846+
ptimer->cnt_ctl = p->regval & ~ARCH_TIMER_CTRL_IT_STAT;
847+
} else {
848+
u64 now = kvm_phys_timer_read();
849+
850+
p->regval = ptimer->cnt_ctl;
851+
/*
852+
* Set ISTATUS bit if it's expired.
853+
* Note that according to ARMv8 ARM Issue A.k, ISTATUS bit is
854+
* UNKNOWN when ENABLE bit is 0, so we chose to set ISTATUS bit
855+
* regardless of ENABLE bit for our implementation convenience.
856+
*/
857+
if (ptimer->cnt_cval <= now)
858+
p->regval |= ARCH_TIMER_CTRL_IT_STAT;
859+
}
860+
836861
return true;
837862
}
838863

839864
static bool access_cntp_cval(struct kvm_vcpu *vcpu,
840865
struct sys_reg_params *p,
841866
const struct sys_reg_desc *r)
842867
{
843-
kvm_inject_undefined(vcpu);
868+
struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
869+
870+
if (p->is_write)
871+
ptimer->cnt_cval = p->regval;
872+
else
873+
p->regval = ptimer->cnt_cval;
874+
844875
return true;
845876
}
846877

include/kvm/arm_arch_timer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx);
7272
void kvm_timer_schedule(struct kvm_vcpu *vcpu);
7373
void kvm_timer_unschedule(struct kvm_vcpu *vcpu);
7474

75+
u64 kvm_phys_timer_read(void);
76+
7577
void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu);
7678

7779
void kvm_timer_init_vhe(void);

virt/kvm/arm/arch_timer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu)
4040
vcpu_vtimer(vcpu)->active_cleared_last = false;
4141
}
4242

43-
static u64 kvm_phys_timer_read(void)
43+
u64 kvm_phys_timer_read(void)
4444
{
4545
return timecounter->cc->read(timecounter->cc);
4646
}

0 commit comments

Comments
 (0)