xen/arm: phys_timer fixes
PATCH v2 1/1 xen/arm: phys_timer fixes
Do not unmask the emulated phys_timer when the related Xen timer expires.
Do not inject the phys_timer interrupt if it is masked.
Do not let the user set CNTx_CTL_PENDING directly.
Set CNTx_CTL_PENDING when the phys_timer expires and clear it when the phys_timer is disabled or the compare value is changed.
Define offset and cval as uint64_t given that they can't be negative and they are used as uint64_t arguments.
Changes in v2:
- do not let the user set CNTx_CTL_PENDING directly;
- set CNTx_CTL_PENDING when the phys_timer expires and clear it when the phys_timer is disabled or the compare value is changed.
--- xen/arch/arm/vtimer.c | 8 ++++++-- xen/include/asm-arm/domain.h | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-)
xen/arch/arm/vtimer.c +6 -2
@@ -33,8 +33,8 @@ static void phys_timer_expired(void *data) {struct vtimer *t = data; t->ctl |= CNTx_CTL_PENDING; - t->ctl &= ~CNTx_CTL_MASK; -vgic_vcpu_inject_irq (t->v,30 ,1 ); +if ( !(t->ctl & CNTx_CTL_MASK) ) +vgic_vcpu_inject_irq (t->v,30 ,1 ); }static void virt_timer_expired (void *data)
@@ -117,6 +117,9 @@ static int vtimer_emulate_32(struct cpu_user_regs *regs, union hsr hsr) }else { + *r &= ~CNTx_CTL_PENDING; +if ( *r & CNTx_CTL_ENABLE ) + *r |= v->arch.phys_timer.ctl & CNTx_CTL_PENDING; v->arch.phys_timer.ctl = *r;if ( v->arch.phys_timer.ctl & CNTx_CTL_ENABLE )
@@ -141,6 +144,7 @@ static int vtimer_emulate_32(struct cpu_user_regs *regs, union hsr hsr) v->arch.phys_timer.cval = now +ticks_to_ns (*r);if ( v->arch.phys_timer.ctl & CNTx_CTL_ENABLE ) { + v->arch.phys_timer.ctl &= ~CNTx_CTL_PENDING;set_timer (&v->arch.phys_timer.timer, v->arch.phys_timer.cval + v->arch.phys_timer.offset); }
xen/include/asm-arm/domain.h +2 -2
@@ -79,8 +79,8 @@ struct vtimer {int irq;struct timer timer;uint32_t ctl; -s_time_t offset; -s_time_t cval; +uint64_t offset; +uint64_t cval; };struct arch_vcpu
Signature · 1 line
1.7.2.5
Worth mentioning for the peanut gallery that we have a new copy of the ARM ARM which has none of the previous ambiguities!
Quoted 9 lines · Stefano Stabellini
Do not unmask the emulated
phys_timerwhen the related Xen timer expires.
Do not inject thephys_timerinterrupt if it is masked.Do not let the user set
CNTx_CTL_PENDINGdirectly.Set
CNTx_CTL_PENDINGwhen thephys_timerexpires and clear it when thephys_timeris disabled or the compare value is changed.
Quoted 8 lines
Define offset and cval as
uint64_tgiven that they can't be negative and they are used asuint64_targuments.Changes in v2:
- do not let the user setCNTx_CTL_PENDINGdirectly;
- setCNTx_CTL_PENDINGwhen thephys_timerexpires and clear it when thephys_timeris disabled or the compare value is changed.
I don't see the clear on disable in the patch?
WRT "when the compare value is changed", should it depend on what it is changed to? IOW if it is in the past should the timer appear to have fired already?
Quoted 23 lines
Signed-off-by Stefano Stabellini <[email protected]>--- xen/arch/arm/vtimer.c | 8 ++++++-- xen/include/asm-arm/domain.h | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-)xen/arch/arm/vtimer.c +2
-2@@ -33,8 +33,8 @@ static void phys_timer_expired(void *data) {struct vtimer *t = data; t->ctl |= CNTx_CTL_PENDING; - t->ctl &= ~CNTx_CTL_MASK; -vgic_vcpu_inject_irq (t->v,30 ,1 ); +if ( !(t->ctl & CNTx_CTL_MASK) ) +vgic_vcpu_inject_irq (t->v,30 ,1 ); }static void virt_timer_expired (void *data)
xen/arch/arm/vtimer.c static int vtimer_emulate_32(struct cpu_user_regs *regs, union hsr hsr) · line 122
Quoted 6 lines
xen/arch/arm/vtimer.c +3
-0@@ -117,6 +117,9 @@ static int vtimer_emulate_32(struct cpu_user_regs *regs, union hsr hsr) }else { + *r &= ~CNTx_CTL_PENDING; +if ( *r & CNTx_CTL_ENABLE ) + *r |= v->arch.phys_timer.ctl & CNTx_CTL_PENDING;
This will end up changing whichever register the guest used for the write, which isn't part of the expected behaviour.
Quoted 26 lines
xen/arch/arm/vtimer.c +1
-0v->arch.phys_timer.ctl = *r;if ( v->arch.phys_timer.ctl & CNTx_CTL_ENABLE )@@ -141,6 +144,7 @@ static int vtimer_emulate_32(struct cpu_user_regs *regs, union hsr hsr) v->arch.phys_timer.cval = now +ticks_to_ns (*r);if ( v->arch.phys_timer.ctl & CNTx_CTL_ENABLE ) { + v->arch.phys_timer.ctl &= ~CNTx_CTL_PENDING;set_timer (&v->arch.phys_timer.timer, v->arch.phys_timer.cval + v->arch.phys_timer.offset); }xen/include/asm-arm/domain.h +2
-2@@ -79,8 +79,8 @@ struct vtimer {int irq;struct timer timer;uint32_t ctl; -s_time_t offset; -s_time_t cval; +uint64_t offset; +uint64_t cval; };struct arch_vcpu
Quoted 17 lines · Ian Campbell
Worth mentioning for the peanut gallery that we have a new copy of the ARM ARM which has none of the previous ambiguities!
Quoted 13 lines · Stefano Stabellini
Do not unmask the emulated
phys_timerwhen the related Xen timer expires.
Do not inject thephys_timerinterrupt if it is masked.Do not let the user set
CNTx_CTL_PENDINGdirectly.Set
CNTx_CTL_PENDINGwhen thephys_timerexpires and clear it when thephys_timeris disabled or the compare value is changed.Define offset and cval as
uint64_tgiven that they can't be negative and they are used asuint64_targuments.
Quoted 6 lines
Quoted 4 lines
Changes in v2:
- do not let the user setCNTx_CTL_PENDINGdirectly;
- setCNTx_CTL_PENDINGwhen thephys_timerexpires and clear it when thephys_timeris disabled or the compare value is changed.I don't see the clear on disable in the patch?
I'll mark below where I am doing the bit clear
Quoted 3 lines
WRT "when the compare value is changed", should it depend on what it is changed to? IOW if it is in the past should the timer appear to have fired already?
No, because if it is set in the past the internal hypervisor timer will fire off immediately, and we set again the pending bit in the timer handler.
Quoted 20 lines
Quoted 20 lines
Signed-off-by Stefano Stabellini <[email protected]>--- xen/arch/arm/vtimer.c | 8 ++++++-- xen/include/asm-arm/domain.h | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-)xen/arch/arm/vtimer.c +2
-2@@ -33,8 +33,8 @@ static void phys_timer_expired(void *data) {struct vtimer *t = data; t->ctl |= CNTx_CTL_PENDING; - t->ctl &= ~CNTx_CTL_MASK; -vgic_vcpu_inject_irq (t->v,30 ,1 ); +if ( !(t->ctl & CNTx_CTL_MASK) ) +vgic_vcpu_inject_irq (t->v,30 ,1 ); }
Quoted 6 lines
xen/arch/arm/vtimer.c static int vtimer_emulate_32(struct cpu_user_regs *regs, union hsr hsr) · line 120
Quoted 6 lines
xen/arch/arm/vtimer.c +1
-0static void virt_timer_expired (void *data)@@ -117,6 +117,9 @@ static int vtimer_emulate_32(struct cpu_user_regs *regs, union hsr hsr) }else { + *r &= ~CNTx_CTL_PENDING;
^ clear
Quoted 5 lines
xen/arch/arm/vtimer.c static int vtimer_emulate_32(struct cpu_user_regs *regs, union hsr hsr) · line 122
Quoted 2 lines
xen/arch/arm/vtimer.c +2
-0+if ( *r & CNTx_CTL_ENABLE ) + *r |= v->arch.phys_timer.ctl & CNTx_CTL_PENDING;This will end up changing whichever register the guest used for the write, which isn't part of the expected behaviour.
I'll need to make a local copy of that register
Quoted 8 lines
xen/arch/arm/vtimer.c static int vtimer_emulate_32(struct cpu_user_regs *regs, union hsr hsr) · line 147
Quoted 8 lines
xen/arch/arm/vtimer.c +1
-0v->arch.phys_timer.ctl = *r;if ( v->arch.phys_timer.ctl & CNTx_CTL_ENABLE )@@ -141,6 +144,7 @@ static int vtimer_emulate_32(struct cpu_user_regs *regs, union hsr hsr) v->arch.phys_timer.cval = now +ticks_to_ns (*r);if ( v->arch.phys_timer.ctl & CNTx_CTL_ENABLE ) { + v->arch.phys_timer.ctl &= ~CNTx_CTL_PENDING;
^ clear