Skip to contentSkip to conversation

xen/arm: support the ARM generic virtual timer

PATCH Superseded Stefano Stabellini 3 participants 6 messages Jan 9, 2013 – Feb 14, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 xen/arm: support the ARM generic virtual timer

Save and restore the virtual timer registers during the context switch. At save time initialize an internal Xen timer to make sure that Xen schedules the guest vcpu at the time of the next virtual timer interrupt.
Receive the virtual timer interrupt into the hypervisor and inject it into the running guest.

---
 xen/arch/arm/domain.c        |   13 ++----
 xen/arch/arm/gic.c           |    4 +-
 xen/arch/arm/time.c          |   14 ++++++-
 xen/arch/arm/vtimer.c        |   91 +++++++++++++++++++++++++++++++-----------
 xen/arch/arm/vtimer.h        |    2 +
 xen/include/asm-arm/domain.h |   23 ++++++-----
 xen/include/asm-arm/time.h   |    3 +
 7 files changed, 105 insertions(+), 45 deletions(-)
xen/arch/arm/domain.c +5 -8
index c5292c7..193a931 100644
@@ -51,9 +51,7 @@ static void ctxt_switch_from(struct vcpu *p)
     p->arch.tpidrprw = READ_CP32(TPIDRPRW);
 
     /* Arch timer */
-    p->arch.cntvoff = READ_CP64(CNTVOFF);
-    p->arch.cntv_cval = READ_CP64(CNTV_CVAL);
-    p->arch.cntv_ctl = READ_CP32(CNTV_CTL);
+    virt_timer_save(p);
 
     /* XXX only save these if ThumbEE e.g. ID_PFR0.THUMB_EE_SUPPORT */
     p->arch.teecr = READ_CP32(TEECR);
@@ -134,11 +132,6 @@ static void ctxt_switch_to(struct vcpu *n)
     WRITE_CP32(n->arch.mair1, MAIR1);
     isb();
 
-    /* Arch timer */
-    WRITE_CP64(n->arch.cntvoff, CNTVOFF);
-    WRITE_CP64(n->arch.cntv_cval, CNTV_CVAL);
-    WRITE_CP32(n->arch.cntv_ctl, CNTV_CTL);
-
     /* Control Registers */
     WRITE_CP32(n->arch.actlr, ACTLR);
     WRITE_CP32(n->arch.sctlr, SCTLR);
@@ -165,6 +158,10 @@ static void ctxt_switch_to(struct vcpu *n)
 
     WRITE_CP32(hcr, HCR);
     isb();
+
+    /* This is could trigger an hardware interrupt from the virtual
+     * timer. The interrupt needs to be injected into the guest. */
+    virt_timer_restore(n);
 }
 
 static void schedule_tail(struct vcpu *prev)
xen/arch/arm/gic.c +3 -1
index 81188f0..8c00a1c 100644
@@ -373,7 +373,9 @@ void gic_route_ppis(void)
     gic_route_irq(25, 1, 1u << smp_processor_id(), 0xa0);
     /* Hypervisor Timer */
     gic_route_irq(26, 1, 1u << smp_processor_id(), 0xa0);
-    /* Timer */
+    /* Virtual Timer */
+    gic_route_irq(27, 1, 1u << smp_processor_id(), 0xa0);
+    /* Physical Timer */
     gic_route_irq(30, 1, 1u << smp_processor_id(), 0xa0);
 }
 
xen/arch/arm/time.c +13 -1
index b6d7015..9515b42 100644
@@ -24,8 +24,11 @@
 #include <xen/lib.h>
 #include <xen/mm.h>
 #include <xen/softirq.h>
+#include <xen/sched.h>
 #include <xen/time.h>
 #include <asm/system.h>
+#include <asm/time.h>
+#include "gic.h"
 
 /*
  * Unfortunately the hypervisor timer interrupt appears to be buggy in
@@ -34,10 +37,11 @@
  */
 #define USE_HYP_TIMER 1
 
+uint64_t __read_mostly boot_count;
+
 /* For fine-grained timekeeping, we use the ARM "Generic Timer", a
  * register-mapped time source in the SoC. */
 static uint32_t __read_mostly cntfrq;      /* Ticks per second */
-static uint64_t __read_mostly boot_count;  /* Counter value at boot time */
 
 /*static inline*/ s_time_t ticks_to_ns(uint64_t ticks)
 {
@@ -153,6 +157,13 @@ static void timer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
     }
 }
 
+static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
+{
+    current->arch.virt_timer.ctl = READ_CP32(CNTV_CTL);
+    WRITE_CP32(current->arch.virt_timer.ctl | CNTx_CTL_MASK, CNTV_CTL);
+    vgic_vcpu_inject_irq(current, irq, 1);
+}
+
 /* Set up the timer interrupt on this CPU */
 void __cpuinit init_timer_interrupt(void)
 {
@@ -172,6 +183,7 @@ void __cpuinit init_timer_interrupt(void)
 
     /* XXX Need to find this IRQ number from devicetree? */
     request_irq(26, timer_interrupt, 0, "hyptimer", NULL);
+    request_irq(27, vtimer_interrupt, 0, "virtimer", NULL);
     request_irq(30, timer_interrupt, 0, "phytimer", NULL);
 }
 
xen/arch/arm/vtimer.c +67 -24

137 lines not on this page · raw message

xen/arch/arm/vtimer.h +2 -0
index d87bb25..faebd68 100644
@@ -22,6 +22,8 @@
 
 extern int vcpu_vtimer_init(struct vcpu *v);
 extern int vtimer_emulate(struct cpu_user_regs *regs, union hsr hsr);
+extern int virt_timer_save(struct vcpu *v);
+extern int virt_timer_restore(struct vcpu *v);
 
 #endif
 
xen/include/asm-arm/domain.h +12 -11
index 114a8f6..577ad19 100644
@@ -3,6 +3,7 @@
 
 #include <xen/config.h>
 #include <xen/cache.h>
+#include <xen/sched.h>
 #include <asm/page.h>
 #include <asm/p2m.h>
 #include <public/hvm/params.h>
@@ -69,6 +70,15 @@ struct arch_domain
 
 }  __cacheline_aligned;
 
+struct vtimer {
+        struct vcpu *v;
+        int irq;
+        struct timer timer;
+        uint32_t ctl;
+        s_time_t offset;
+        s_time_t cval;
+};
+
 struct arch_vcpu
 {
     struct {
@@ -118,11 +128,6 @@ struct arch_vcpu
     uint32_t teecr, teehbr;
     uint32_t joscr, jmcr;
 
-    /* Arch timers */
-    uint64_t cntvoff;
-    uint64_t cntv_cval;
-    uint32_t cntv_ctl;
-
     /* CP 15 */
     uint32_t csselr;
 
@@ -157,12 +162,8 @@ struct arch_vcpu
         spinlock_t lock;
     } vgic;
 
-    struct {
-        struct timer timer;
-        uint32_t ctl;
-        s_time_t offset;
-        s_time_t cval;
-    } vtimer;
+    struct vtimer phys_timer;
+    struct vtimer virt_timer;
 }  __cacheline_aligned;
 
 void vcpu_show_execution_state(struct vcpu *);
xen/include/asm-arm/time.h +3 -0
index 8cc9e78..0d4a052 100644
@@ -15,6 +15,9 @@ struct tm wallclock_time(void);
 /* Set up the timer interrupt on this CPU */
 extern void __cpuinit init_timer_interrupt(void);
 
+/* Counter value at boot time */
+extern uint64_t boot_count;
+
 #endif /* __ARM_TIME_H__ */
 /*
  * Local variables:
Signature · 1 line
1.7.2.5
#1 lore raw

xen/arch/arm/time.c static void timer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 163

Quoted 4 lines · Stefano Stabellini
xen/arch/arm/time.c +4 -0
+static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
+{
+    current->arch.virt_timer.ctl = READ_CP32(CNTV_CTL);
+    WRITE_CP32(current->arch.virt_timer.ctl | CNTx_CTL_MASK, CNTV_CTL);

This is masking the vtimer interrupt in a way that's visible to the currently running guest. Is that going to confuse the guest?

When we talked about this before I had imagined that the masking would happen in the GIC, where the guest doesn't see it.

Quoted 8 lines
xen/arch/arm/time.c +3 -0
+    vgic_vcpu_inject_irq(current, irq, 1);
+}
+
 /* Set up the timer interrupt on this CPU */
 void __cpuinit init_timer_interrupt(void)
 {
@@ -172,6 +183,7 @@ void __cpuinit init_timer_interrupt(void)
 

xen/arch/arm/time.c void __cpuinit init_timer_interrupt(void)

Quoted 10 lines
xen/arch/arm/time.c +5 -0
     /* XXX Need to find this IRQ number from devicetree? */
     request_irq(26, timer_interrupt, 0, "hyptimer", NULL);
+    request_irq(27, vtimer_interrupt, 0, "virtimer", NULL);
     request_irq(30, timer_interrupt, 0, "phytimer", NULL);
 }
 
+static void virt_timer_expired(void *data)
+{
+    struct vtimer *t = data;
+    vcpu_wake(t->v);

Shouldn't this also inject the irq? Otherwise when an unscheduled guest's timer goes off, we take two interrupts - one for the hyp timer to call this function and then another immediately after virt_timer_restore() which causes us to inject the vtimer irq. If we injected the irq here (and arranged to mask the vtimer irq) we'd avoid a second interrupt.

Tim.

#2 lore raw
Quoted 11 lines · Tim Deegan

xen/arch/arm/time.c static void timer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 163

Quoted 4 lines · Stefano Stabellini
xen/arch/arm/time.c +4 -0
+static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
+{
+    current->arch.virt_timer.ctl = READ_CP32(CNTV_CTL);
+    WRITE_CP32(current->arch.virt_timer.ctl | CNTx_CTL_MASK, CNTV_CTL);

This is masking the vtimer interrupt in a way that's visible to the currently running guest. Is that going to confuse the guest?

When we talked about this before I had imagined that the masking would happen in the GIC, where the guest doesn't see it.

I know it is not ideal but it is safe, it is not creating any problems to Linux and it is even the same thing that KVM does).
More importantly I just couldn't get the GIC strategy to work correctly (actually it work decently well on the physical Versatile Express but it hangs the emulator, this tells me that it is not the way it was intended to be used by the ARM people).

Quoted 14 lines
Quoted 14 lines
xen/arch/arm/time.c +4 -0
+    vgic_vcpu_inject_irq(current, irq, 1);
+}
+
 /* Set up the timer interrupt on this CPU */
 void __cpuinit init_timer_interrupt(void)
 {
@@ -172,6 +183,7 @@ void __cpuinit init_timer_interrupt(void)
 
     /* XXX Need to find this IRQ number from devicetree? */
     request_irq(26, timer_interrupt, 0, "hyptimer", NULL);
+    request_irq(27, vtimer_interrupt, 0, "virtimer", NULL);
     request_irq(30, timer_interrupt, 0, "phytimer", NULL);
 }
 
Quoted 11 lines

xen/arch/arm/vtimer.c line 42

Quoted 4 lines
xen/arch/arm/vtimer.c +4 -0
+static void virt_timer_expired(void *data)
+{
+    struct vtimer *t = data;
+    vcpu_wake(t->v);

Shouldn't this also inject the irq? Otherwise when an unscheduled guest's timer goes off, we take two interrupts - one for the hyp timer to call this function and then another immediately after virt_timer_restore() which causes us to inject the vtimer irq. If we injected the irq here (and arranged to mask the vtimer irq) we'd avoid a second interrupt.

This might be a good idea, I'll give it a try.

#3 lore raw
Quoted 10 lines · Stefano Stabellini
Quoted 9 lines · Tim Deegan

xen/arch/arm/time.c static void timer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 163

Quoted 4 lines · Stefano Stabellini
xen/arch/arm/time.c +4 -0
+static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
+{
+    current->arch.virt_timer.ctl = READ_CP32(CNTV_CTL);
+    WRITE_CP32(current->arch.virt_timer.ctl | CNTx_CTL_MASK, CNTV_CTL);

This is masking the vtimer interrupt in a way that's visible to the currently running guest. Is that going to confuse the guest?

Quoted 5 lines
Quoted 2 lines

When we talked about this before I had imagined that the masking would happen in the GIC, where the guest doesn't see it.

I know it is not ideal but it is safe, it is not creating any problems to Linux and it is even the same thing that KVM does).

Grrr. So it's safe unless you run something other than linux, or KVM devs change their minds...

Do we have an interface document to describe the PV machine that we provide to a guest? If so, this divergence from the spec should be described there. If not, we really should. :)

Tim.

#4 lore raw
Quoted 17 lines · Tim Deegan
Quoted 10 lines · Stefano Stabellini
Quoted 9 lines · Tim Deegan

xen/arch/arm/time.c static void timer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 163

Quoted 4 lines · Stefano Stabellini
xen/arch/arm/time.c +4 -0
+static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
+{
+    current->arch.virt_timer.ctl = READ_CP32(CNTV_CTL);
+    WRITE_CP32(current->arch.virt_timer.ctl | CNTx_CTL_MASK, CNTV_CTL);

This is masking the vtimer interrupt in a way that's visible to the currently running guest. Is that going to confuse the guest?

Quoted 5 lines
Quoted 2 lines

When we talked about this before I had imagined that the masking would happen in the GIC, where the guest doesn't see it.

I know it is not ideal but it is safe, it is not creating any problems to Linux and it is even the same thing that KVM does).

Quoted 6 lines

Grrr. So it's safe unless you run something other than linux, or KVM devs change their minds...

Do we have an interface document to describe the PV machine that we provide to a guest? If so, this divergence from the spec should be described there. If not, we really should. :)

That's a good idea

#5 lore raw
In reply to Stefano Stabellini
Quoted 13 lines · Stefano Stabellini
Quoted 11 lines · Tim Deegan

xen/arch/arm/time.c static void timer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs) · line 163

Quoted 4 lines · Stefano Stabellini
xen/arch/arm/time.c +4 -0
+static void vtimer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
+{
+    current->arch.virt_timer.ctl = READ_CP32(CNTV_CTL);
+    WRITE_CP32(current->arch.virt_timer.ctl | CNTx_CTL_MASK, CNTV_CTL);

This is masking the vtimer interrupt in a way that's visible to the currently running guest. Is that going to confuse the guest?

When we talked about this before I had imagined that the masking would happen in the GIC, where the guest doesn't see it.

Quoted 6 lines

I know it is not ideal but it is safe, it is not creating any problems to Linux and it is even the same thing that KVM does).
More importantly I just couldn't get the GIC strategy to work correctly (actually it work decently well on the physical Versatile Express but it hangs the emulator, this tells me that it is not the way it was intended to be used by the ARM people).

I think we should ask ARM about this, it could just be a model bug. I'm sure that they would be interested in cases where the virtualness leaks out of the hardware like this.

Ian.

Quoted 29 lines
Quoted 16 lines
Quoted 16 lines
xen/arch/arm/time.c +4 -0
+    vgic_vcpu_inject_irq(current, irq, 1);
+}
+
 /* Set up the timer interrupt on this CPU */
 void __cpuinit init_timer_interrupt(void)
 {
@@ -172,6 +183,7 @@ void __cpuinit init_timer_interrupt(void)
 
     /* XXX Need to find this IRQ number from devicetree? */
     request_irq(26, timer_interrupt, 0, "hyptimer", NULL);
+    request_irq(27, vtimer_interrupt, 0, "virtimer", NULL);
     request_irq(30, timer_interrupt, 0, "phytimer", NULL);
 }
 
Quoted 11 lines

xen/arch/arm/vtimer.c line 42

Quoted 4 lines
xen/arch/arm/vtimer.c +4 -0
+static void virt_timer_expired(void *data)
+{
+    struct vtimer *t = data;
+    vcpu_wake(t->v);

Shouldn't this also inject the irq? Otherwise when an unscheduled guest's timer goes off, we take two interrupts - one for the hyp timer to call this function and then another immediately after virt_timer_restore() which causes us to inject the vtimer irq. If we injected the irq here (and arranged to mask the vtimer irq) we'd avoid a second interrupt.

This might be a good idea, I'll give it a try.