Skip to content

Commit c3be3a4

Browse files
author
Marc Zyngier
committed
KVM: arm64: Move CNT*CT_EL0 userspace accessors to generic infrastructure
Moving the counter registers is a bit more involved than for the control and comparator (there is no shadow data for the counter), but still pretty manageable. Reviewed-by: Joey Gouly <joey.gouly@arm.com> Signed-off-by: Marc Zyngier <maz@kernel.org>
1 parent 8af1989 commit c3be3a4

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

arch/arm64/kvm/guest.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -592,19 +592,12 @@ static unsigned long num_core_regs(const struct kvm_vcpu *vcpu)
592592
}
593593

594594
static const u64 timer_reg_list[] = {
595-
KVM_REG_ARM_TIMER_CNT,
596-
KVM_REG_ARM_PTIMER_CNT,
597595
};
598596

599597
#define NUM_TIMER_REGS ARRAY_SIZE(timer_reg_list)
600598

601599
static bool is_timer_reg(u64 index)
602600
{
603-
switch (index) {
604-
case KVM_REG_ARM_TIMER_CNT:
605-
case KVM_REG_ARM_PTIMER_CNT:
606-
return true;
607-
}
608601
return false;
609602
}
610603

arch/arm64/kvm/sys_regs.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,12 +1605,38 @@ static int arch_timer_set_user(struct kvm_vcpu *vcpu,
16051605
case SYS_CNTHP_CTL_EL2:
16061606
val &= ~ARCH_TIMER_CTRL_IT_STAT;
16071607
break;
1608+
case SYS_CNTVCT_EL0:
1609+
if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags))
1610+
timer_set_offset(vcpu_vtimer(vcpu), kvm_phys_timer_read() - val);
1611+
return 0;
1612+
case SYS_CNTPCT_EL0:
1613+
if (!test_bit(KVM_ARCH_FLAG_VM_COUNTER_OFFSET, &vcpu->kvm->arch.flags))
1614+
timer_set_offset(vcpu_ptimer(vcpu), kvm_phys_timer_read() - val);
1615+
return 0;
16081616
}
16091617

16101618
__vcpu_assign_sys_reg(vcpu, rd->reg, val);
16111619
return 0;
16121620
}
16131621

1622+
static int arch_timer_get_user(struct kvm_vcpu *vcpu,
1623+
const struct sys_reg_desc *rd,
1624+
u64 *val)
1625+
{
1626+
switch (reg_to_encoding(rd)) {
1627+
case SYS_CNTVCT_EL0:
1628+
*val = kvm_phys_timer_read() - timer_get_offset(vcpu_vtimer(vcpu));
1629+
break;
1630+
case SYS_CNTPCT_EL0:
1631+
*val = kvm_phys_timer_read() - timer_get_offset(vcpu_ptimer(vcpu));
1632+
break;
1633+
default:
1634+
*val = __vcpu_sys_reg(vcpu, rd->reg);
1635+
}
1636+
1637+
return 0;
1638+
}
1639+
16141640
static s64 kvm_arm64_ftr_safe_value(u32 id, const struct arm64_ftr_bits *ftrp,
16151641
s64 new, s64 cur)
16161642
{
@@ -2539,7 +2565,7 @@ static bool bad_redir_trap(struct kvm_vcpu *vcpu,
25392565

25402566
#define TIMER_REG(name, vis) \
25412567
SYS_REG_USER_FILTER(name, access_arch_timer, reset_val, 0, \
2542-
NULL, arch_timer_set_user, vis)
2568+
arch_timer_get_user, arch_timer_set_user, vis)
25432569

25442570
/*
25452571
* Since reset() callback and field val are not used for idregs, they will be
@@ -3506,8 +3532,10 @@ static const struct sys_reg_desc sys_reg_descs[] = {
35063532
AMU_AMEVTYPER1_EL0(14),
35073533
AMU_AMEVTYPER1_EL0(15),
35083534

3509-
{ SYS_DESC(SYS_CNTPCT_EL0), access_arch_timer },
3510-
{ SYS_DESC(SYS_CNTVCT_EL0), access_arch_timer },
3535+
{ SYS_DESC(SYS_CNTPCT_EL0), .access = access_arch_timer,
3536+
.get_user = arch_timer_get_user, .set_user = arch_timer_set_user },
3537+
{ SYS_DESC(SYS_CNTVCT_EL0), .access = access_arch_timer,
3538+
.get_user = arch_timer_get_user, .set_user = arch_timer_set_user },
35113539
{ SYS_DESC(SYS_CNTPCTSS_EL0), access_arch_timer },
35123540
{ SYS_DESC(SYS_CNTVCTSS_EL0), access_arch_timer },
35133541
{ SYS_DESC(SYS_CNTP_TVAL_EL0), access_arch_timer },

0 commit comments

Comments
 (0)