Skip to contentSkip to conversation

hpet: add support for level triggered interrupts

RFC PATCH Superseded Roger Pau Monne 4 participants 17 messages Feb 23, 2018 – Mar 1, 2018 x86

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover hpet: add support for level triggered interrupts

Hello,

This series add support for level triggered HPET interrupts, which are mandatory according to the spec.

First patch is a change for vpt in order to execute the timer callbacks even if the interrupt is not injected because the IRQ is masked. Second patch implements support for level triggered interrupts in HPET and finally patch 3 is for XTF in order to add a basic HPET functionality test.

The whole series is RFC because I've mainly tested it using the XTF test provided in patch 3. Also it slightly changes (fixes) the behavior of other timers because callbacks are now always executed.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw

RFC PATCH 1/3 x86/vpt: execute callbacks for masked interrupts

Execute periodic_time callbacks even if the interrupt is not actually injected because the IRQ is masked.

Current callbacks from emulated timer devices only update emulated registers, which from my reading of the specs should happen regardless of whether the interrupt has been injected or not.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefan Bader <[email protected]>

---
 xen/arch/x86/hvm/vpt.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/vpt.c +29 -1
index 181f4cb631..1a24fbaa44 100644
@@ -247,9 +247,31 @@ static void pt_timer_fn(void *data)
     pt_unlock(pt);
 }
 
+static void execute_callbacks(struct vcpu *v, struct list_head *tm)
+{
+    spin_lock(&v->arch.hvm_vcpu.tm_lock);
+    while ( !list_empty(tm) )
+    {
+        struct periodic_time *pt = list_first_entry(tm, struct periodic_time,
+                                                    list);
+        time_cb *cb = pt->cb;
+        void *cb_priv = pt->priv;
+
+        list_del(&pt->list);
+        pt->on_list = 0;
+        spin_unlock(&v->arch.hvm_vcpu.tm_lock);
+
+        cb(v, cb_priv);
+
+        spin_lock(&v->arch.hvm_vcpu.tm_lock);
+    }
+    spin_unlock(&v->arch.hvm_vcpu.tm_lock);
+}
+
 int pt_update_irq(struct vcpu *v)
 {
     struct list_head *head = &v->arch.hvm_vcpu.tm_list;
+    LIST_HEAD(purged);
     struct periodic_time *pt, *temp, *earliest_pt;
     uint64_t max_lag;
     int irq, is_lapic, pt_vector;
@@ -267,7 +289,10 @@ int pt_update_irq(struct vcpu *v)
             {
                 /* suspend timer emulation */
                 list_del(&pt->list);
-                pt->on_list = 0;
+                if ( pt->cb )
+                    list_add(&pt->list, &purged);
+                else
+                    pt->on_list = 0;
             }
             else
             {
@@ -283,6 +308,7 @@ int pt_update_irq(struct vcpu *v)
     if ( earliest_pt == NULL )
     {
         spin_unlock(&v->arch.hvm_vcpu.tm_lock);
+        execute_callbacks(v, &purged);
         return -1;
     }
 
@@ -292,6 +318,8 @@ int pt_update_irq(struct vcpu *v)
 
     spin_unlock(&v->arch.hvm_vcpu.tm_lock);
 
+    execute_callbacks(v, &purged);
+
     /*
      * If periodic timer interrut is handled by lapic, its vector in
      * IRR is returned and used to set eoi_exit_bitmap for virtual
Signature · 7 lines
2.16.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [PATCH RFC 1/3] x86/vpt: execute callbacks for masked interrupts

Quoted 39 lines · Roger Pau Monne

Execute periodic_time callbacks even if the interrupt is not actually injected because the IRQ is masked.

Current callbacks from emulated timer devices only update emulated registers, which from my reading of the specs should happen regardless of whether the interrupt has been injected or not.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefan Bader <[email protected]>

---
 xen/arch/x86/hvm/vpt.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/vpt.c +15 -0
index 181f4cb631..1a24fbaa44 100644
@@ -247,9 +247,31 @@ static void pt_timer_fn(void *data)
     pt_unlock(pt);
 }
 
+static void execute_callbacks(struct vcpu *v, struct list_head *tm)
+{
+    spin_lock(&v->arch.hvm_vcpu.tm_lock);
+    while ( !list_empty(tm) )
+    {
+        struct periodic_time *pt = list_first_entry(tm, struct periodic_time,
+                                                    list);
+        time_cb *cb = pt->cb;
+        void *cb_priv = pt->priv;
+
+        list_del(&pt->list);
+        pt->on_list = 0;
+        spin_unlock(&v->arch.hvm_vcpu.tm_lock);
+
+        cb(v, cb_priv);

xen/arch/x86/hvm/vpt.c static void pt_timer_fn(void *data) · line 274

Quoted 10 lines
xen/arch/x86/hvm/vpt.c +7 -0
+
+        spin_lock(&v->arch.hvm_vcpu.tm_lock);
+    }
+    spin_unlock(&v->arch.hvm_vcpu.tm_lock);
+}
+
 int pt_update_irq(struct vcpu *v)
 {
     struct list_head *head = &v->arch.hvm_vcpu.tm_list;
+    LIST_HEAD(purged);

to_purge?

Quoted 16 lines
xen/arch/x86/hvm/vpt.c +4 -1
     struct periodic_time *pt, *temp, *earliest_pt;
     uint64_t max_lag;
     int irq, is_lapic, pt_vector;
@@ -267,7 +289,10 @@ int pt_update_irq(struct vcpu *v)
             {
                 /* suspend timer emulation */
                 list_del(&pt->list);
-                pt->on_list = 0;
+                if ( pt->cb )
+                    list_add(&pt->list, &purged);
+                else
+                    pt->on_list = 0;
             }
             else
             {

xen/arch/x86/hvm/vpt.c int pt_update_irq(struct vcpu *v) · line 311

Quoted 4 lines
xen/arch/x86/hvm/vpt.c +1 -0
@@ -283,6 +308,7 @@ int pt_update_irq(struct vcpu *v)
     if ( earliest_pt == NULL )
     {
         spin_unlock(&v->arch.hvm_vcpu.tm_lock);
+        execute_callbacks(v, &purged);

It would be better to check if the list is not empty before calling the function to avoid the extra lock / unlock.

(Haven't checked if the basic premise of this patch is compatible with the spec)

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [PATCH RFC 1/3] x86/vpt: execute callbacks for masked interrupts

Quoted 44 lines · Wei Liu
Quoted 43 lines · Roger Pau Monne

Execute periodic_time callbacks even if the interrupt is not actually injected because the IRQ is masked.

Current callbacks from emulated timer devices only update emulated registers, which from my reading of the specs should happen regardless of whether the interrupt has been injected or not.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefan Bader <[email protected]>

---
 xen/arch/x86/hvm/vpt.c | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/vpt.c +19 -0
index 181f4cb631..1a24fbaa44 100644
@@ -247,9 +247,31 @@ static void pt_timer_fn(void *data)
     pt_unlock(pt);
 }
 
+static void execute_callbacks(struct vcpu *v, struct list_head *tm)
+{
+    spin_lock(&v->arch.hvm_vcpu.tm_lock);
+    while ( !list_empty(tm) )
+    {
+        struct periodic_time *pt = list_first_entry(tm, struct periodic_time,
+                                                    list);
+        time_cb *cb = pt->cb;
+        void *cb_priv = pt->priv;
+
+        list_del(&pt->list);
+        pt->on_list = 0;
+        spin_unlock(&v->arch.hvm_vcpu.tm_lock);
+
+        cb(v, cb_priv);
+
+        spin_lock(&v->arch.hvm_vcpu.tm_lock);
+    }
+    spin_unlock(&v->arch.hvm_vcpu.tm_lock);
Quoted 8 lines

xen/arch/x86/hvm/vpt.c static void pt_timer_fn(void *data) · line 274

Quoted 6 lines
xen/arch/x86/hvm/vpt.c +3 -0
+}
+
 int pt_update_irq(struct vcpu *v)
 {
     struct list_head *head = &v->arch.hvm_vcpu.tm_list;
+    LIST_HEAD(purged);

to_purge?

My point is that they have already been purged from the pt->list, but I really don't have a preference.

Quoted 15 lines
Quoted 15 lines
xen/arch/x86/hvm/vpt.c +4 -1
     struct periodic_time *pt, *temp, *earliest_pt;
     uint64_t max_lag;
     int irq, is_lapic, pt_vector;
@@ -267,7 +289,10 @@ int pt_update_irq(struct vcpu *v)
             {
                 /* suspend timer emulation */
                 list_del(&pt->list);
-                pt->on_list = 0;
+                if ( pt->cb )
+                    list_add(&pt->list, &purged);
+                else
+                    pt->on_list = 0;
             }
             else
             {
Quoted 8 lines

xen/arch/x86/hvm/vpt.c int pt_update_irq(struct vcpu *v) · line 311

Quoted 5 lines
xen/arch/x86/hvm/vpt.c +1 -0
@@ -283,6 +308,7 @@ int pt_update_irq(struct vcpu *v)
     if ( earliest_pt == NULL )
     {
         spin_unlock(&v->arch.hvm_vcpu.tm_lock);
+        execute_callbacks(v, &purged);

It would be better to check if the list is not empty before calling the function to avoid the extra lock / unlock.

The lock is also protecting the 'purged' list, so I think that for consistency the lock needs to be held before accessing it.

Since this is only a empty check *and* there can't be any additions to the list at this point I guess it would be safe to test for emptiness without holding the lock, but I find it kind of confusing.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

Re: [PATCH RFC 1/3] x86/vpt: execute callbacks for masked interrupts

Quoted 37 lines
Quoted 1 line
Quoted 1 line · On 26.02.18 at 13:48, <[email protected]> wrote:
Quoted 7 lines · Wei Liu
Quoted 4 lines · Roger Pau Monne

int pt_update_irq(struct vcpu *v)

 {
     struct list_head *head = &v->arch.hvm_vcpu.tm_list;

+ LIST_HEAD(purged);

to_purge?

My point is that they have already been purged from the pt->list, but I really don't have a preference.

Quoted 15 lines
Quoted 15 lines
     struct periodic_time *pt, *temp, *earliest_pt;
     uint64_t max_lag;
     int irq, is_lapic, pt_vector;
xen/arch/x86/hvm/vpt.c +4 -1
@@ -267,7 +289,10 @@ int pt_update_irq(struct vcpu *v)
             {
                 /* suspend timer emulation */
                 list_del(&pt->list);
-                pt->on_list = 0;
+                if ( pt->cb )
+                    list_add(&pt->list, &purged);
+                else
+                    pt->on_list = 0;
             }
             else
             {
Quoted 8 lines

xen/arch/x86/hvm/vpt.c int pt_update_irq(struct vcpu *v) · line 311

Quoted 5 lines
xen/arch/x86/hvm/vpt.c +1 -0
@@ -283,6 +308,7 @@ int pt_update_irq(struct vcpu *v)
     if ( earliest_pt == NULL )
     {
         spin_unlock(&v->arch.hvm_vcpu.tm_lock);
+        execute_callbacks(v, &purged);

It would be better to check if the list is not empty before calling the function to avoid the extra lock / unlock.

Quoted 2 lines

The lock is also protecting the 'purged' list, so I think that for consistency the lock needs to be held before accessing it.

But that's a local list, isn't it? No-one else can access it.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

Re: [PATCH RFC 1/3] x86/vpt: execute callbacks for masked interrupts

Quoted 37 lines · Jan Beulich
Quoted 37 lines
Quoted 1 line
Quoted 1 line · On 26.02.18 at 13:48, <[email protected]> wrote:
Quoted 7 lines · Wei Liu
Quoted 4 lines · Roger Pau Monne

int pt_update_irq(struct vcpu *v)

 {
     struct list_head *head = &v->arch.hvm_vcpu.tm_list;

+ LIST_HEAD(purged);

to_purge?

My point is that they have already been purged from the pt->list, but I really don't have a preference.

Quoted 23 lines
Quoted 16 lines
     struct periodic_time *pt, *temp, *earliest_pt;
     uint64_t max_lag;
     int irq, is_lapic, pt_vector;
xen/arch/x86/hvm/vpt.c +4 -1
@@ -267,7 +289,10 @@ int pt_update_irq(struct vcpu *v)
             {
                 /* suspend timer emulation */
                 list_del(&pt->list);
-                pt->on_list = 0;
+                if ( pt->cb )
+                    list_add(&pt->list, &purged);
+                else
+                    pt->on_list = 0;
             }
             else
             {

xen/arch/x86/hvm/vpt.c int pt_update_irq(struct vcpu *v) · line 311

Quoted 4 lines
xen/arch/x86/hvm/vpt.c +1 -0
@@ -283,6 +308,7 @@ int pt_update_irq(struct vcpu *v)
     if ( earliest_pt == NULL )
     {
         spin_unlock(&v->arch.hvm_vcpu.tm_lock);
+        execute_callbacks(v, &purged);

It would be better to check if the list is not empty before calling the function to avoid the extra lock / unlock.

Quoted 4 lines
Quoted 2 lines

The lock is also protecting the 'purged' list, so I think that for consistency the lock needs to be held before accessing it.

But that's a local list, isn't it? No-one else can access it.

destroy_periodic_time can still remove items from this list, if a timer that's on the 'purged' list is destroyed between added to the list and executing the callback.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

RFC PATCH 2/3 vhpet: add support for level triggered interrupts

In reply to Roger Pau Monne

Level triggered interrupts are not an optional feature of HPET, and must be implemented in order to comply with the HPET specification.

Implement them by adding a callback to the timer which sets the interrupt bit in the general interrupt status register. Further interrupts (in case of periodic mode) will not be injected until the bit is cleared.

In order to reset the interrupts when the status bit is clear Xen must also detect accesses to such register.

While there convert tn and i in hpet_write to unsigned.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Stefan Bader <[email protected]>

---
 xen/arch/x86/hvm/hpet.c | 46 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 12 deletions(-)
xen/arch/x86/hvm/hpet.c +34 -12
index f7aed7f69e..1cfd72592e 100644
@@ -220,6 +220,17 @@ static void hpet_stop_timer(HPETState *h, unsigned int tn,
     hpet_get_comparator(h, tn, guest_time);
 }
 
+static void hpet_timer_fired(struct vcpu *v, void *data)
+{
+    unsigned int tn = (unsigned int)data;
+    HPETState *h = vcpu_vhpet(v);
+
+    write_lock(&h->lock);
+    ASSERT(!test_bit(tn, &h->hpet.isr));
+    __set_bit(tn, &h->hpet.isr);
+    write_unlock(&h->lock);
+}
+
 /* the number of HPET tick that stands for
  * 1/(2^10) second, namely, 0.9765625 milliseconds */
 #define  HPET_TINY_TIME_SPAN  ((h->stime_freq >> 10) / STIME_PER_HPET_TICK)
@@ -241,7 +252,7 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,
         pit_stop_channel0_irq(&vhpet_domain(h)->arch.vpit);
     }
 
-    if ( !timer_enabled(h, tn) )
+    if ( !timer_enabled(h, tn) || test_bit(tn, &h->hpet.isr) )
         return;
 
     tn_cmp   = hpet_get_comparator(h, tn, guest_time);
@@ -277,8 +288,12 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,
      * timer we also need the period which may be different because time may
      * have elapsed between the time the comparator was written and the timer
      * being enabled (now).
+     *
+     * NB: set periodic timers as oneshot if interrupt type is set to level
+     * because the user must ack the interrupt (by writing 1 to the interrupt
+     * status register) before another interrupt can be delivered.
      */
-    oneshot = !timer_is_periodic(h, tn);
+    oneshot = !timer_is_periodic(h, tn) || timer_level(h, tn);
     TRACE_2_LONG_4D(TRC_HVM_EMUL_HPET_START_TIMER, tn, irq,
                     TRC_PAR_LONG(hpet_tick_to_ns(h, diff)),
                     TRC_PAR_LONG(oneshot ? 0LL :
@@ -286,7 +301,9 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,
     create_periodic_time(vhpet_vcpu(h), &h->pt[tn],
                          hpet_tick_to_ns(h, diff),
                          oneshot ? 0 : hpet_tick_to_ns(h, h->hpet.period[tn]),
-                         irq, NULL, NULL);
+                         irq,
+                         timer_level(h, tn) ? hpet_timer_fired : NULL,
+                         (void *)(unsigned long)tn);
 }
 
 static inline uint64_t hpet_fixup_reg(
@@ -304,7 +321,7 @@ static int hpet_write(
     HPETState *h = vcpu_vhpet(v);
     uint64_t old_val, new_val;
     uint64_t guest_time;
-    int tn, i;
+    unsigned int tn, i;
 
     /* Acculumate a bit mask of timers whos state is changed by this write. */
     unsigned long start_timers = 0;
@@ -360,6 +377,19 @@ static int hpet_write(
         }
         break;
 
+    case HPET_STATUS:
+        /* write 1 to clear. */
+        while (new_val)
+        {
+            i = find_first_set_bit(new_val);
+            if ( i >= HPET_TIMER_NUM )
+                break;
+            __clear_bit(i, &new_val);
+            if ( __test_and_clear_bit(i, &h->hpet.isr) )
+                set_start_timer(i);
+        }
+        break;
+
     case HPET_COUNTER:
         h->hpet.mc64 = new_val;
         if ( hpet_enabled(h) )
@@ -379,14 +409,6 @@ static int hpet_write(
 
         h->hpet.timers[tn].config = hpet_fixup_reg(new_val, old_val, 0x3f4e);
 
-        if ( timer_level(h, tn) )
-        {
-            gdprintk(XENLOG_ERR,
-                     "HPET: level triggered interrupt not supported now\n");
-            domain_crash(current->domain);
-            break;
-        }
-
         if ( new_val & HPET_TN_32BIT )
         {
             h->hpet.timers[tn].cmp = (uint32_t)h->hpet.timers[tn].cmp;
Signature · 7 lines
2.16.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

RFC PATCH 3/3 xtf: add minimal HPET functionality test

In reply to Roger Pau Monne

Add a basic HPET functionality test, note that this test requires the HPET to support level triggered interrupts.

Further improvements should add support for interrupt delivery, and testing all the available timers.

---
Cc: Andrew Cooper <[email protected]>

---
 arch/x86/include/arch/lib.h |  14 ++++
 docs/all-tests.dox          |   2 +
 tests/hpet/Makefile         |   9 +++
 tests/hpet/main.c           | 187 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 212 insertions(+)
 create mode 100644 tests/hpet/Makefile
 create mode 100644 tests/hpet/main.c
arch/x86/include/arch/lib.h +14 -0
index 6714bdc..3400890 100644
@@ -392,6 +392,20 @@ static inline void write_xcr0(uint64_t xcr0)
     xsetbv(0, xcr0);
 }
 
+static inline uint64_t rdtsc(void)
+{
+    uint32_t low, high;
+
+    asm volatile ("rdtsc" : "=a" (low), "=d" (high));
+
+    return ((uint64_t)high << 32) | low;
+}
+
+static inline void pause(void)
+{
+    asm volatile ("pause");
+}
+
 #endif /* XTF_X86_LIB_H */
 
 /*
docs/all-tests.dox +2 -0
index 355cb80..122840c 100644
@@ -127,4 +127,6 @@ guest breakout.
 @subpage test-nested-svm - Nested SVM tests.
 
 @subpage test-nested-vmx - Nested VT-x tests.
+
+@subpage test-hpet - HPET functional test.
 */
tests/hpet/Makefile new file +9 -0
new file mode 100644
index 0000000..934e63c
@@ -0,0 +1,9 @@
+include $(ROOT)/build/common.mk
+
+NAME      := hpet
+CATEGORY  := utility
+TEST-ENVS := hvm32
+
+obj-perenv += main.o
+
+include $(ROOT)/build/gen.mk
tests/hpet/main.c new file +187 -0

187 lines not on this page · raw message

Signature · 7 lines
2.16.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#8 lore raw

Re: [PATCH RFC 3/3] xtf: add minimal HPET functionality test

Quoted 23 lines · Roger Pau Monne

Add a basic HPET functionality test, note that this test requires the HPET to support level triggered interrupts.

Further improvements should add support for interrupt delivery, and testing all the available timers.

---
Cc: Andrew Cooper <[email protected]>

---
 arch/x86/include/arch/lib.h |  14 ++++
 docs/all-tests.dox          |   2 +
 tests/hpet/Makefile         |   9 +++
 tests/hpet/main.c           | 187 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 212 insertions(+)
 create mode 100644 tests/hpet/Makefile
 create mode 100644 tests/hpet/main.c
arch/x86/include/arch/lib.h +0 -0
index 6714bdc..3400890 100644

arch/x86/include/arch/lib.h static inline void write_xcr0(uint64_t xcr0) · line 400

Quoted 9 lines
arch/x86/include/arch/lib.h +6 -0
@@ -392,6 +392,20 @@ static inline void write_xcr0(uint64_t xcr0)
     xsetbv(0, xcr0);
 }
 
+static inline uint64_t rdtsc(void)
+{
+    uint32_t low, high;
+
+    asm volatile ("rdtsc" : "=a" (low), "=d" (high));
+

You probably need to add lfence or mfence. See rdtsc_ordered in Xen.

Quoted 3 lines
arch/x86/include/arch/lib.h +3 -0
+    return ((uint64_t)high << 32) | low;
+}
+

[...]

Quoted 14 lines
arch/x86/include/arch/lib.h +14 -0
+static void set_freq(void)
+{
+    uint32_t eax, ebx, ecx, edx, base;
+    bool found = false;
+
+    /* Get tsc frequency from cpuid. */
+    for ( base = XEN_CPUID_FIRST_LEAF;
+          base < XEN_CPUID_FIRST_LEAF + 0x10000; base += 0x100 )
+    {
+        cpuid(base, &eax, &ebx, &ecx, &edx);
+
+        if ( (ebx == XEN_CPUID_SIGNATURE_EBX) &&
+             (ecx == XEN_CPUID_SIGNATURE_ECX) &&
+             (edx == XEN_CPUID_SIGNATURE_EDX) &&

arch/x86/include/arch/lib.h

Quoted 10 lines
arch/x86/include/arch/lib.h +10 -0
+             ((eax - base) >= 2) )
+        {
+            found = true;
+            break;
+        }
+    }
+
+    if ( !found )
+        panic("Unable to locate Xen CPUID leaves\n");
+

Finding Xen leaves should live in its own function and move to common code if possible.

arch/x86/include/arch/lib.h

Quoted 12 lines
arch/x86/include/arch/lib.h +12 -0
+    cpuid_count(base + 3, 0, &eax, &ebx, &freq, &edx);
+    printk("TSC frequency %ukHz\n", freq);
+}
+
+/* Busy-wait implementation based on tsc value. */
+static void wait(unsigned int ms)
+{
+    uint64_t end = rdtsc() + (uint64_t)ms * (uint64_t)freq;
+
+    while ( rdtsc() < end )
+        pause();
+}

Rename to mdelay and move to a helper file?

Quoted 3 lines
arch/x86/include/arch/lib.h +3 -0
+
+void test_main(void)
+{

[...]

arch/x86/include/arch/lib.h

Quoted 6 lines
arch/x86/include/arch/lib.h +6 -0
+    HPET_REG(HPET_Tn_CFG(nr)) &= ~HPET_TN_LEVEL;
+    HPET_REG(HPET_STATUS) = 1 << nr;
+    wait(200);
+    if ( ((HPET_REG(HPET_STATUS) >> nr) & 1) )
+        return xtf_failure("Fail: Status bit set for edge interrupt in periodic mode\n");
+

Is it possible to use shorter time in the test? This test as-is will run for 1 or 2 seconds which is a bit long as micro-kernel testing.

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

Re: [PATCH RFC 3/3] xtf: add minimal HPET functionality test

Quoted 27 lines · Wei Liu
Quoted 26 lines · Roger Pau Monne

Add a basic HPET functionality test, note that this test requires the HPET to support level triggered interrupts.

Further improvements should add support for interrupt delivery, and testing all the available timers.

---
Cc: Andrew Cooper <[email protected]>

---
 arch/x86/include/arch/lib.h |  14 ++++
 docs/all-tests.dox          |   2 +
 tests/hpet/Makefile         |   9 +++
 tests/hpet/main.c           | 187 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 212 insertions(+)
 create mode 100644 tests/hpet/Makefile
 create mode 100644 tests/hpet/main.c
arch/x86/include/arch/lib.h +0 -0
index 6714bdc..3400890 100644
@@ -392,6 +392,20 @@ static inline void write_xcr0(uint64_t xcr0)
     xsetbv(0, xcr0);
 }
 
Quoted 8 lines

arch/x86/include/arch/lib.h static inline void write_xcr0(uint64_t xcr0) · line 400

Quoted 6 lines
arch/x86/include/arch/lib.h +6 -0
+static inline uint64_t rdtsc(void)
+{
+    uint32_t low, high;
+
+    asm volatile ("rdtsc" : "=a" (low), "=d" (high));
+

You probably need to add lfence or mfence. See rdtsc_ordered in Xen.

Oh, OK that's news to me. I guess just using a lfence before it should be fine.

Quoted 23 lines
Quoted 3 lines
arch/x86/include/arch/lib.h +3 -0
+    return ((uint64_t)high << 32) | low;
+}
+

[...]

Quoted 19 lines
arch/x86/include/arch/lib.h +19 -0
+static void set_freq(void)
+{
+    uint32_t eax, ebx, ecx, edx, base;
+    bool found = false;
+
+    /* Get tsc frequency from cpuid. */
+    for ( base = XEN_CPUID_FIRST_LEAF;
+          base < XEN_CPUID_FIRST_LEAF + 0x10000; base += 0x100 )
+    {
+        cpuid(base, &eax, &ebx, &ecx, &edx);
+
+        if ( (ebx == XEN_CPUID_SIGNATURE_EBX) &&
+             (ecx == XEN_CPUID_SIGNATURE_ECX) &&
+             (edx == XEN_CPUID_SIGNATURE_EDX) &&
+             ((eax - base) >= 2) )
+        {
+            found = true;
+            break;
+        }
Quoted 8 lines

arch/x86/include/arch/lib.h

Quoted 5 lines
arch/x86/include/arch/lib.h +5 -0
+    }
+
+    if ( !found )
+        panic("Unable to locate Xen CPUID leaves\n");
+

Finding Xen leaves should live in its own function and move to common code if possible.

There's already one in common code. The right way to fix this seems to be to store 'base' and use it in further cpuid related calls.

Quoted 6 lines
Quoted 6 lines
arch/x86/include/arch/lib.h +6 -0
+    cpuid_count(base + 3, 0, &eax, &ebx, &freq, &edx);
+    printk("TSC frequency %ukHz\n", freq);
+}
+
+/* Busy-wait implementation based on tsc value. */
+static void wait(unsigned int ms)
Quoted 8 lines

arch/x86/include/arch/lib.h

Quoted 6 lines
arch/x86/include/arch/lib.h +6 -0
+{
+    uint64_t end = rdtsc() + (uint64_t)ms * (uint64_t)freq;
+
+    while ( rdtsc() < end )
+        pause();
+}

Rename to mdelay and move to a helper file?

OK, wasn't sure whether this would be helpful or not.

Quoted 11 lines
Quoted 3 lines
arch/x86/include/arch/lib.h +3 -0
+
+void test_main(void)
+{

[...]

arch/x86/include/arch/lib.h

Quoted 6 lines
arch/x86/include/arch/lib.h +6 -0
+    HPET_REG(HPET_Tn_CFG(nr)) &= ~HPET_TN_LEVEL;
+    HPET_REG(HPET_STATUS) = 1 << nr;
+    wait(200);
+    if ( ((HPET_REG(HPET_STATUS) >> nr) & 1) )
+        return xtf_failure("Fail: Status bit set for edge interrupt in periodic mode\n");
+
Quoted 2 lines

Is it possible to use shorter time in the test? This test as-is will run for 1 or 2 seconds which is a bit long as micro-kernel testing.

Likely, I can reduce the periods and waits to something smaller. Let me use a define and then we can tweak this, but using a 1ms period should be fine.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

Re: [PATCH RFC 3/3] xtf: add minimal HPET functionality test

Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 28.02.18 at 16:37, <[email protected]> wrote:
Quoted 7 lines · Wei Liu
Quoted 6 lines · Roger Pau Monne
arch/x86/include/arch/lib.h +0 -0
@@ -392,6 +392,20 @@ static inline void write_xcr0(uint64_t xcr0)
     xsetbv(0, xcr0);
 }
 
Quoted 8 lines

arch/x86/include/arch/lib.h static inline void write_xcr0(uint64_t xcr0) · line 400

Quoted 6 lines
arch/x86/include/arch/lib.h +6 -0
+static inline uint64_t rdtsc(void)
+{
+    uint32_t low, high;
+
+    asm volatile ("rdtsc" : "=a" (low), "=d" (high));
+

You probably need to add lfence or mfence. See rdtsc_ordered in Xen.

Quoted 2 lines

Oh, OK that's news to me. I guess just using a lfence before it should be fine.

Except that on AMD, without LFENCE made dispatch serializing, you'd need MFENCE.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw

Re: [PATCH RFC 3/3] xtf: add minimal HPET functionality test

Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 28.02.18 at 16:37, <[email protected]> wrote:
Quoted 15 lines · Wei Liu

arch/x86/include/arch/lib.h static inline void write_xcr0(uint64_t xcr0) · line 400

Quoted 12 lines · Roger Pau Monne
arch/x86/include/arch/lib.h +6 -0
@@ -392,6 +392,20 @@ static inline void write_xcr0(uint64_t xcr0)
     xsetbv(0, xcr0);
 }
 
+static inline uint64_t rdtsc(void)
+{
+    uint32_t low, high;
+
+    asm volatile ("rdtsc" : "=a" (low), "=d" (high));
+

You probably need to add lfence or mfence. See rdtsc_ordered in Xen.

Quoted 5 lines
Quoted 2 lines

Oh, OK that's news to me. I guess just using a lfence before it should be fine.

Except that on AMD, without LFENCE made dispatch serializing, you'd need MFENCE.

So using just MFENCE will work everywhere since that's more restrictive than a lfence?

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw

Re: [PATCH RFC 3/3] xtf: add minimal HPET functionality test

Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 01.03.18 at 10:55, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 28.02.18 at 16:37, <[email protected]> wrote:
Quoted 15 lines · Wei Liu

arch/x86/include/arch/lib.h static inline void write_xcr0(uint64_t xcr0) · line 400

Quoted 12 lines · Roger Pau Monne
arch/x86/include/arch/lib.h +6 -0
@@ -392,6 +392,20 @@ static inline void write_xcr0(uint64_t xcr0)
     xsetbv(0, xcr0);
 }
 
+static inline uint64_t rdtsc(void)
+{
+    uint32_t low, high;
+
+    asm volatile ("rdtsc" : "=a" (low), "=d" (high));
+

You probably need to add lfence or mfence. See rdtsc_ordered in Xen.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Oh, OK that's news to me. I guess just using a lfence before it should be fine.

Except that on AMD, without LFENCE made dispatch serializing, you'd need MFENCE.

So using just MFENCE will work everywhere since that's more restrictive than a lfence?

Sadly no. I think that's explained in the description of the hypervisor patches introducing rdtsc_ordered(), but you can also simply read up on the LFENCE/MFENCE specifics on the Intel SDM instruction description. In particular: "MFENCE does not serialize the instruction stream." Yet that's what we're after here. MFENCE is a serializing instruction on AMD hardware only.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw

Re: [PATCH RFC 3/3] xtf: add minimal HPET functionality test

Quoted 29 lines · Jan Beulich
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 01.03.18 at 10:55, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 18 lines
Quoted 1 line
Quoted 1 line · On 28.02.18 at 16:37, <[email protected]> wrote:
Quoted 15 lines · Wei Liu

arch/x86/include/arch/lib.h static inline void write_xcr0(uint64_t xcr0) · line 400

Quoted 12 lines · Roger Pau Monne
arch/x86/include/arch/lib.h +6 -0
@@ -392,6 +392,20 @@ static inline void write_xcr0(uint64_t xcr0)
     xsetbv(0, xcr0);
 }
 
+static inline uint64_t rdtsc(void)
+{
+    uint32_t low, high;
+
+    asm volatile ("rdtsc" : "=a" (low), "=d" (high));
+

You probably need to add lfence or mfence. See rdtsc_ordered in Xen.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Oh, OK that's news to me. I guess just using a lfence before it should be fine.

Except that on AMD, without LFENCE made dispatch serializing, you'd need MFENCE.

So using just MFENCE will work everywhere since that's more restrictive than a lfence?

Quoted 6 lines

Sadly no. I think that's explained in the description of the hypervisor patches introducing rdtsc_ordered(), but you can also simply read up on the LFENCE/MFENCE specifics on the Intel SDM instruction description. In particular: "MFENCE does not serialize the instruction stream." Yet that's what we're after here. MFENCE is a serializing instruction on AMD hardware only.

Hm, nice. Then I basically need to issue both a LFENCE and MFENCE because I don't plan to introduce an alternatives framework to XTF ATM.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#14 lore raw

Re: [PATCH RFC 3/3] xtf: add minimal HPET functionality test

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 01.03.18 at 11:14, <[email protected]> wrote:

Hm, nice. Then I basically need to issue both a LFENCE and MFENCE because I don't plan to introduce an alternatives framework to XTF ATM.

Or you could skip the test if you find LFENCE isn't serializing on AMD. After all the hypervisor now makes it serializing wherever it can, and it is serializing on the two families where the respective MSR doesn't exist. Of course the question is whether seeing the raw (host) value of that MSR is actually something such a test should be permitted to rely upon; Andrew may have very specific thoughts on this.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#15 lore raw

Re: [PATCH RFC 3/3] xtf: add minimal HPET functionality test

In reply to Roger Pau Monne
Quoted 23 lines · Roger Pau Monne

Add a basic HPET functionality test, note that this test requires the HPET to support level triggered interrupts.

Further improvements should add support for interrupt delivery, and testing all the available timers.

---
Cc: Andrew Cooper <[email protected]>

---
 arch/x86/include/arch/lib.h |  14 ++++
 docs/all-tests.dox          |   2 +
 tests/hpet/Makefile         |   9 +++
 tests/hpet/main.c           | 187 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 212 insertions(+)
 create mode 100644 tests/hpet/Makefile
 create mode 100644 tests/hpet/main.c
arch/x86/include/arch/lib.h +0 -0
index 6714bdc..3400890 100644

arch/x86/include/arch/lib.h static inline void write_xcr0(uint64_t xcr0) · line 399

Quoted 8 lines
arch/x86/include/arch/lib.h +5 -0
@@ -392,6 +392,20 @@ static inline void write_xcr0(uint64_t xcr0)
     xsetbv(0, xcr0);
 }
 
+static inline uint64_t rdtsc(void)
+{
+    uint32_t low, high;
+
+    asm volatile ("rdtsc" : "=a" (low), "=d" (high));

For my own timing purposes, I've been using rdtscp because it is strictly more helpful, but this isn't a general solution.

For rdtsc, (contrary to the way the other thread is progressing), what matters is a dispatch serialising event, which is different to an architecturally serialising event.

The easiest fix for now is to unconditionally use mfence, leaving a comment saying that this should be lfence on Intel and when the AMD pipeline is configured correctly.  Please name the function rdtscp_ordered() though, to distinguish it from a plain rdtsc instruction.

Quoted 17 lines
arch/x86/include/arch/lib.h +9 -0
+
+    return ((uint64_t)high << 32) | low;
+}
+
+static inline void pause(void)
+{
+    asm volatile ("pause");
+}
+
 #endif /* XTF_X86_LIB_H */
 
 /*
docs/all-tests.dox +0 -0
index 355cb80..122840c 100644

docs/all-tests.dox guest breakout. · line 131

Quoted 5 lines
docs/all-tests.dox +2 -0
@@ -127,4 +127,6 @@ guest breakout.
 @subpage test-nested-svm - Nested SVM tests.
 
 @subpage test-nested-vmx - Nested VT-x tests.
+
+@subpage test-hpet - HPET functional test.

This page is sorted by test category first, but this is the "in-development" section.

FWIW, I think "in-development" is probably a better category than utility, because we will eventually want to get this test into automation.

Quoted 57 lines
docs/all-tests.dox +0 -0
 */
tests/hpet/Makefile new file +9 -0
new file mode 100644
index 0000000..934e63c
@@ -0,0 +1,9 @@
+include $(ROOT)/build/common.mk
+
+NAME      := hpet
+CATEGORY  := utility
+TEST-ENVS := hvm32
+
+obj-perenv += main.o
+
+include $(ROOT)/build/gen.mk
tests/hpet/main.c new file +35 -0
new file mode 100644
index 0000000..57be410
@@ -0,0 +1,187 @@
+/**
+ * @file tests/hpet/main.c
+ * @ref test-hpet
+ *
+ * @page test-hpet hpet
+ *
+ * HPET functionality testing.
+ *
+ * Quite limited, currently only Timer N is tested. No interrupt delivery
+ * tests.
+ *
+ * @see tests/hpet/main.c
+ */
+#include <xtf.h>
+
+#define HPET_BASE_ADDRESS       0xfed00000
+
+#define HPET_ID                 0
+#define HPET_ID_NUMBER          0x1f00
+#define HPET_ID_NUMBER_SHIFT    8
+
+#define HPET_PERIOD             0x004
+#define HPET_MAX_PERIOD         0x05f5e100
+
+#define HPET_CFG                0x010
+#define HPET_CFG_ENABLE         0x001
+
+#define HPET_STATUS             0x020
+
+#define HPET_COUNTER            0x0f0
+
+#define HPET_Tn_CFG(n)          (0x100 + (n) * 0x20)
+#define HPET_TN_LEVEL           0x002
+#define HPET_TN_ENABLE          0x004
+#define HPET_TN_PERIODIC        0x008

tests/hpet/main.c line 45

Quoted 10 lines
tests/hpet/main.c new file +10 -0
+#define HPET_TN_32BIT           0x100
+#define HPET_TN_ROUTE_SHIFT     9
+
+#define HPET_Tn_CMP(n)          (0x108 + (n) * 0x20)
+
+/*
+ * NB: should probably be an explicit movl, but clang seems to generate good
+ * code.
+ */
+#define HPET_REG(reg) (*(volatile uint32_t *)(_p(HPET_BASE_ADDRESS) + (reg)))

A lot of the above should be in a dedicated hpet driver, rather than in the test.  See the selftest test_driver_init(), and apic.{h,c} which is the closes similar example.

That said, HPET registers are in general 64 bits wide rather than 32. It is probably best to split the basic hpet infrastructure into a separate patch from the test.

Quoted 25 lines
tests/hpet/main.c new file +25 -0
+
+#define MS_TO_NS                1000000
+/* p is in fs */
+#define MS_TO_TICKS(ms, p)      (((ms) * MS_TO_NS) / ((p) / 1000000))
+
+const char test_title[] = "Test HPET";
+
+static uint32_t freq;
+
+static void set_freq(void)
+{
+    uint32_t eax, ebx, ecx, edx, base;
+    bool found = false;
+
+    /* Get tsc frequency from cpuid. */
+    for ( base = XEN_CPUID_FIRST_LEAF;
+          base < XEN_CPUID_FIRST_LEAF + 0x10000; base += 0x100 )
+    {
+        cpuid(base, &eax, &ebx, &ecx, &edx);
+
+        if ( (ebx == XEN_CPUID_SIGNATURE_EBX) &&
+             (ecx == XEN_CPUID_SIGNATURE_ECX) &&
+             (edx == XEN_CPUID_SIGNATURE_EDX) &&
+             ((eax - base) >= 2) )
+        {

tests/hpet/main.c line 80

Quoted 10 lines
tests/hpet/main.c new file +10 -0
+            found = true;
+            break;
+        }
+    }
+
+    if ( !found )
+        panic("Unable to locate Xen CPUID leaves\n");
+
+    cpuid_count(base + 3, 0, &eax, &ebx, &freq, &edx);
+    printk("TSC frequency %ukHz\n", freq);

Calculate what you need in arch/x86/setup.c and export it via arch/x86/include/arch/cpuid.h

However, you can't rely on the frequency being constant.  It might be better to busy wait on the percpu wallclock instead, if you don't want to sort out interrupts.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#16 lore raw

Re: [PATCH RFC 3/3] xtf: add minimal HPET functionality test

Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 01.03.18 at 12:46, <[email protected]> wrote:
Quoted 6 lines · Roger Pau Monne

Add a basic HPET functionality test, note that this test requires the HPET to support level triggered interrupts.

Further improvements should add support for interrupt delivery, and testing all the available timers.

Quoted 8 lines

---
Cc: Andrew Cooper <[email protected]>

---
 arch/x86/include/arch/lib.h |  14 ++++
 docs/all-tests.dox          |   2 +
 tests/hpet/Makefile         |   9 +++
 tests/hpet/main.c           | 187 

++++++++++++++++++++++++++++++++++++++++++++

Quoted 9 lines
 4 files changed, 212 insertions(+)
 create mode 100644 tests/hpet/Makefile
 create mode 100644 tests/hpet/main.c
arch/x86/include/arch/lib.h +0 -0
index 6714bdc..3400890 100644

arch/x86/include/arch/lib.h static inline void write_xcr0(uint64_t xcr0) · line 399

Quoted 8 lines
arch/x86/include/arch/lib.h +5 -0
@@ -392,6 +392,20 @@ static inline void write_xcr0(uint64_t xcr0)
     xsetbv(0, xcr0);
 }
 
+static inline uint64_t rdtsc(void)
+{
+    uint32_t low, high;
+
+    asm volatile ("rdtsc" : "=a" (low), "=d" (high));

For my own timing purposes, I've been using rdtscp because it is strictly more helpful, but this isn't a general solution.

Quoted 8 lines

For rdtsc, (contrary to the way the other thread is progressing), what matters is a dispatch serialising event, which is different to an architecturally serialising event.

The easiest fix for now is to unconditionally use mfence, leaving a comment saying that this should be lfence on Intel and when the AMD pipeline is configured correctly. Please name the function rdtscp_ordered() though, to distinguish it from a plain rdtsc instruction.

Interesting. Right above you say "dispatch serialising", but then you suggest MFENCE, which isn't on Intel? It's those differences that I've been trying to explain on that other part of the thread (apparently with little success, seeing your "contrary to").

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel