[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[xen staging-4.12] xen/arm: remove physical timer offset



commit a46cd06d92e698a71e4c845301a19468265809d3
Author:     Jeff Kubascik <jeff.kubascik@xxxxxxxxxxxxxxx>
AuthorDate: Tue Jan 21 10:07:03 2020 -0500
Commit:     Stefano Stabellini <sstabellini@xxxxxxxxxx>
CommitDate: Fri Apr 10 09:34:11 2020 -0700

    xen/arm: remove physical timer offset
    
    The physical timer traps apply an offset so that time starts at 0 for
    the guest. However, this offset is not currently applied to the physical
    counter. Per the ARMv8 Reference Manual (ARM DDI 0487E.a), section
    D11.2.4 Timers, the "Offset" between the counter and timer should be
    zero for a physical timer. This removes the offset to make the timer and
    counter consistent.
    
    This also cleans up the physical timer implementation to better match
    the virtual timer - both cval's now hold the hardware value.
    
    In the case the guest sets cval to a time before Xen started, the correct
    behavior is to expire the timer immediately. To do this, we set the expires
    argument of set_timer to zero.
    
    Signed-off-by: Jeff Kubascik <jeff.kubascik@xxxxxxxxxxxxxxx>
    Acked-by: Julien Grall <julien@xxxxxxx>
    (cherry picked from commit f14f55b7ee295277c8dd09e37e0fa0902ccf7eb4)
---
 xen/arch/arm/vtimer.c        | 46 +++++++++++++++++++++++++++++---------------
 xen/include/asm-arm/domain.h |  3 ---
 2 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/xen/arch/arm/vtimer.c b/xen/arch/arm/vtimer.c
index c99dd237d1..b15f304c1e 100644
--- a/xen/arch/arm/vtimer.c
+++ b/xen/arch/arm/vtimer.c
@@ -62,7 +62,6 @@ static void virt_timer_expired(void *data)
 
 int domain_vtimer_init(struct domain *d, struct xen_arch_domainconfig *config)
 {
-    d->arch.phys_timer_base.offset = NOW();
     d->arch.virt_timer_base.offset = READ_SYSREG64(CNTPCT_EL0);
     d->time_offset_seconds = ticks_to_ns(d->arch.virt_timer_base.offset - 
boot_count);
     do_div(d->time_offset_seconds, 1000000000);
@@ -108,7 +107,6 @@ int vcpu_vtimer_init(struct vcpu *v)
 
     init_timer(&t->timer, phys_timer_expired, t, v->processor);
     t->ctl = 0;
-    t->cval = NOW();
     t->irq = d0
         ? timer_get_irq(TIMER_PHYS_NONSECURE_PPI)
         : GUEST_TIMER_PHYS_NS_PPI;
@@ -169,6 +167,7 @@ int virt_timer_restore(struct vcpu *v)
 static bool vtimer_cntp_ctl(struct cpu_user_regs *regs, uint32_t *r, bool read)
 {
     struct vcpu *v = current;
+    s_time_t expires;
 
     if ( !ACCESS_ALLOWED(regs, EL0PTEN) )
         return false;
@@ -186,8 +185,13 @@ static bool vtimer_cntp_ctl(struct cpu_user_regs *regs, 
uint32_t *r, bool read)
 
         if ( v->arch.phys_timer.ctl & CNTx_CTL_ENABLE )
         {
-            set_timer(&v->arch.phys_timer.timer,
-                      v->arch.phys_timer.cval + 
v->domain->arch.phys_timer_base.offset);
+            /*
+             * If cval is before the point Xen started, expire timer
+             * immediately.
+             */
+            expires = v->arch.phys_timer.cval > boot_count
+                      ? ticks_to_ns(v->arch.phys_timer.cval - boot_count) : 0;
+            set_timer(&v->arch.phys_timer.timer, expires);
         }
         else
             stop_timer(&v->arch.phys_timer.timer);
@@ -199,26 +203,31 @@ static bool vtimer_cntp_tval(struct cpu_user_regs *regs, 
uint32_t *r,
                              bool read)
 {
     struct vcpu *v = current;
-    s_time_t now;
+    uint64_t cntpct;
+    s_time_t expires;
 
     if ( !ACCESS_ALLOWED(regs, EL0PTEN) )
         return false;
 
-    now = NOW() - v->domain->arch.phys_timer_base.offset;
+    cntpct = get_cycles();
 
     if ( read )
     {
-        *r = (uint32_t)(ns_to_ticks(v->arch.phys_timer.cval - now) & 
0xffffffffull);
+        *r = (uint32_t)((v->arch.phys_timer.cval - cntpct) & 0xffffffffull);
     }
     else
     {
-        v->arch.phys_timer.cval = now + ticks_to_ns(*r);
+        v->arch.phys_timer.cval = cntpct + *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->domain->arch.phys_timer_base.offset);
+            /*
+             * If cval is before the point Xen started, expire timer
+             * immediately.
+             */
+            expires = v->arch.phys_timer.cval > boot_count
+                      ? ticks_to_ns(v->arch.phys_timer.cval - boot_count) : 0;
+            set_timer(&v->arch.phys_timer.timer, expires);
         }
     }
     return true;
@@ -228,23 +237,28 @@ static bool vtimer_cntp_cval(struct cpu_user_regs *regs, 
uint64_t *r,
                              bool read)
 {
     struct vcpu *v = current;
+    s_time_t expires;
 
     if ( !ACCESS_ALLOWED(regs, EL0PTEN) )
         return false;
 
     if ( read )
     {
-        *r = ns_to_ticks(v->arch.phys_timer.cval);
+        *r = v->arch.phys_timer.cval;
     }
     else
     {
-        v->arch.phys_timer.cval = ticks_to_ns(*r);
+        v->arch.phys_timer.cval = *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->domain->arch.phys_timer_base.offset);
+            /*
+             * If cval is before the point Xen started, expire timer
+             * immediately.
+             */
+            expires = v->arch.phys_timer.cval > boot_count
+                      ? ticks_to_ns(v->arch.phys_timer.cval - boot_count) : 0;
+            set_timer(&v->arch.phys_timer.timer, expires);
         }
     }
     return true;
diff --git a/xen/include/asm-arm/domain.h b/xen/include/asm-arm/domain.h
index def28606c9..5b49509e80 100644
--- a/xen/include/asm-arm/domain.h
+++ b/xen/include/asm-arm/domain.h
@@ -66,9 +66,6 @@ struct arch_domain
 
     struct {
         uint64_t offset;
-    } phys_timer_base;
-    struct {
-        uint64_t offset;
     } virt_timer_base;
 
     struct vgic_dist vgic;
--
generated by git-patchbot for /home/xen/git/xen.git#staging-4.12



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.