Skip to contentSkip to conversation

Prevent one cause of "MP-BIOS bug: 8254 timer"... message from linux

PATCH Applied Don Slutz 3 participants 15 messages May 2, 2014 – May 8, 2014

lore
Share & tools
lore

Don Slutz

author
#0 lore raw

v4 cover Prevent one cause of "MP-BIOS bug: 8254 timer"... message from linux

Changes v2 to v3:

Drop v3 patch #8 "hvm/hpet: Use signed divide in hpet_get_comparator."
Either v3 patch #11 "hvm/hpet: handle 1st period special" or v4 patch #10 "hvm/hpet: Detect comparator values in the past" make this patch not needed.

Replace v3 patch #11 "hvm/hpet: handle 1st period special" with
v4 patch #10 "hvm/hpet: Detect comparator values in the past"

My changes:

#1 "hvm/hpet: Add manual unit test code."
Run the unit test in many modes by default.
Add new skip_error_on_load and debug flag 0x0100
because patch #10 "hvm/hpet: Detect comparator values in the past" does not handle the 63 bit boundary case.
#7 "hvm/hpet: Init comparator64 like comparator."
Added paragraph starting "A disabled hpet" to commit message.

Jan Beulich:
#1 "hvm/hpet: Add manual unit test code."
Use make shortcuts.
#2 "hvm/hpet: Only call guest_time_hpet(h) one time per"
Reviewed-by
#3 "hvm/hpet: Only set comparator or period not both."
Reviewed-by
#4 "hvm/hpet: Correctly limit period to a maximum."
Reviewed-by
#5 "hvm/hpet: In hpet_save, correctly compute mc64."
Reviewed-by
#6 "hvm/hpet: In hpet_save, call hpet_get_comparator"
Correctly set guest_time.
Fix formatting of comments.
#8 "hvm/hpet: comparator can only change when master"
Acked-by

Tim Deegan:
#10 "hvm/hpet: Detect comparator values in the past"
Acked-by

Changes v2 to v3:

Add new patch #8 "hvm/hpet: Use signed divide in hpet_get_comparator."

Re-order to group changes in the same area:

#5, #6 are both about hpet_save

#8, #9, #10, and #11 are all about corner cases of the lines in hpet_get_comparator():

elapsed = master clock ...
comparator += (elapsed / period) * period

#4 "hvm/hpet: Correctly limit period to a maximum."

     was v2 #9
  #5 "hvm/hpet: In hpet_save, correctly compute mc64."
     was v2 #4
  #6 "hvm/hpet: In hpet_save, call hpet_get_comparator."
     was v2 #7 "hvm/hpet: Call hpet_get_comparator during hpet_save."
  #7 "hvm/hpet: Init comparator64 like comparator."
     was v2 #5
  #9 "hvm/hpet: comparator can only change when master"
     was v2 #6
  #10 "hvm/hpet: Prevent master clock equal to comparator"
      was v2 #8
  #11 "hvm/hpet: handle 1st period special"
      was v2 #10

My changes:
#1 "hvm/hpet: Add manual unit test code.":
Make it optional.
Add hpet_check_stopped() testing.
Adjust print_error messages.
#6 "hvm/hpet: In hpet_save, call hpet_get_comparator.":
Reword subject from "hvm/hpet: Call hpet_get_comparator during hpet_save."
#11 "hvm/hpet: handle 1st period special":
More setting of first_mc64 & first_enabled when needed. Switch to bool_t.

Jan Beulich:
#1 "hvm/hpet: Add manual unit test code.":
Add Makefile.
Better commit message.
#2 "hvm/hpet: Only call guest_time_hpet(h) one time per":
Did not add Reviewed-by do to amount of change Added passing of guest_time to hpet_read64() and
hpet_stop_timer().
Dropped mc_starting.
#3 "hvm/hpet: Only set comparator or period not both.":
Only have 2 blocks of code.
Set comparator64 before truncation
#6 "hvm/hpet: In hpet_save, call hpet_get_comparator.":
Better commit message.
#9 "hvm/hpet: comparator can only change when master":
Better commit message.
#11 "hvm/hpet: handle 1st period special":
Better commit message.

Changes v1 to v2:

Drop the patch "hpet: Act more like real hardware" from v1 for several reasons:

1) Only "fixes" less then 50% of the problem: diff < 0 or diff > too many periods.

2) I have a better fix in #2 "hvm/hpet: Only call guest_time_hpet(h) one time per action."

3) Reverts a previous bug fix.

So all these patches are new and fix various bugs.

#1 "hvm/hpet: Add manual unit test code.":
Is optional. I used it to validate the changes did what I expected.

v1: info that still applies

Based on the proposed fix in QEMU:

http://marc.info/?l=qemu-devel&m=139304386331192&w=2

That was provided for:

http://marc.info/?l=qemu-devel&m=139295851107140&w=2

Which is very close to a bug I have been looking into and asked some questions about in:

http://lists.xen.org/archives/html/xen-devel/2014-02/msg01787.html

Don Slutz (10):
hvm/hpet: Add manual unit test code.
hvm/hpet: Only call guest_time_hpet(h) one time per action. hvm/hpet: Only set comparator or period not both. hvm/hpet: Correctly limit period to a maximum. hvm/hpet: In hpet_save, correctly compute mc64. hvm/hpet: In hpet_save, call hpet_get_comparator. hvm/hpet: Init comparator64 like comparator.
hvm/hpet: comparator can only change when master clock is enabled. hvm/hpet: Prevent master clock equal to comparator while enabled hvm/hpet: Detect comparator values in the past

 tools/tests/vhpet/.gitignore |   4 +
 tools/tests/vhpet/Makefile   |  36 ++
 tools/tests/vhpet/emul.h     | 416 +++++++++++++++++++++++
 tools/tests/vhpet/main.c     | 776 +++++++++++++++++++++++++++++++++++++++++++
 xen/arch/x86/hvm/hpet.c      | 124 ++++---
 5 files changed, 1313 insertions(+), 43 deletions(-)
 create mode 100644 tools/tests/vhpet/.gitignore
 create mode 100644 tools/tests/vhpet/Makefile
 create mode 100644 tools/tests/vhpet/emul.h
 create mode 100644 tools/tests/vhpet/main.c
Signature · 1 line
1.8.4
author
#1 lore raw

PATCH v4 1/10 hvm/hpet: Add manual unit test code.

Add the code at tools/tests/vhpet.

See comment in tools/tests/vhpet/main.c for details on running either in a xen source tree or elsewhere.

A basic in source tree usage is:

make -C tools/tests/vhpet run

Does repro the bug:

..MP-BIOS bug: 8254 timer not connected to IO-APIC

The make file includes coping hpet.c and hpet.h from the source tree. hpet.c is then modifed to remove all include file and add the emul.h include file.

The manual test code has only a few automatic checks that output messages to stderr:

1) Possible ..MP-BIOS bug: 8254 timer...
if 1st period is not <= the expected value

2) hpet_set_mode(%ld): T%d Error: Set ... if read of comparator != write of comparator in

3) hpet_check_stopped(%ld): T%d Error: Set ...
if read != write

4) main(%ld): With clock stopped mc64 changed: ...
if hpet_save returns different master clock values when called more then once.

It also generates a lot of output, which is why the sugested way to use includes a redirect of stdout to a file.

---
v4:
Use make shortcuts.
Run the unit test in many modes by default.
Add new skip_error_on_load and debug flag 0x0100
because patch #10 "hvm/hpet: Detect comparator values in the past" does not handle the 63 bit boundary case.

 tools/tests/vhpet/.gitignore |   4 +
 tools/tests/vhpet/Makefile   |  36 ++
 tools/tests/vhpet/emul.h     | 416 +++++++++++++++++++++++
 tools/tests/vhpet/main.c     | 776 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1232 insertions(+)
 create mode 100644 tools/tests/vhpet/.gitignore
 create mode 100644 tools/tests/vhpet/Makefile
 create mode 100644 tools/tests/vhpet/emul.h
 create mode 100644 tools/tests/vhpet/main.c
tools/tests/vhpet/.gitignore new file +4 -0
new file mode 100644
index 0000000..4cefa62
@@ -0,0 +1,4 @@
+test_vhpet
+test_vhpet.out
+hpet.h
+hpet.c
tools/tests/vhpet/Makefile new file +36 -0
new file mode 100644
index 0000000..763409d
@@ -0,0 +1,36 @@
+
+XEN_ROOT=$(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+TARGET := test_vhpet
+
+.PHONY: all
+all: $(TARGET)
+
+.PHONY: run
+run: $(TARGET)
+	./$(TARGET) > $(TARGET).out
+	./$(TARGET) 0 > $(TARGET).0.out
+	./$(TARGET) 0 1 0 20 > $(TARGET).0.1.0.20.out
+	./$(TARGET) 200 > $(TARGET).200.out
+	./$(TARGET) 200 1 0 20 > $(TARGET).200.1.0.20.out
+	./$(TARGET) 300 5 > $(TARGET).200.5.out
+	./$(TARGET) 400 4 2000 > $(TARGET).200.4.2000.out
+	./$(TARGET) 0 1 0 20 0x0103 > $(TARGET).0.1.0.20.0x0103.out
+	./$(TARGET) 200 1 0 20 0x0103 > $(TARGET).200.1.0.20.0x0103.out
+
+$(TARGET): hpet.c main.c hpet.h emul.h Makefile
+	$(HOSTCC) -g -o $@ hpet.c main.c
+
+.PHONY: clean
+clean:
+	rm -rf $(TARGET) $(TARGET).out *.o *~ core* hpet.h hpet.c
+
+.PHONY: install
+install:
+
+hpet.h: $(XEN_ROOT)/xen/include/asm-x86/hpet.h
+	cp $< $@
+
+hpet.c: $(XEN_ROOT)/xen/arch/x86/hvm/hpet.c
+	sed -e "/#include/d" -e "1i#include \"emul.h\"\n" <$< >$@
tools/tests/vhpet/emul.h new file +416 -0

416 lines not on this page · raw message

tools/tests/vhpet/main.c new file +776 -0

776 lines not on this page · raw message

Signature · 1 line
1.8.4
author
#2 lore raw

PATCH v4 2/10 hvm/hpet: Only call guest_time_hpet(h) one time per action.

In reply to Don Slutz

This call is expensive and will cause extra time to pass.

The software-developers-hpet-spec-1-0a.pdf does not say how long it takes after the main clock is enabled before the first change of the master clock. Therefore multiple calls to guest_time_hpet(h) are not needed. Since each timer is started by a loop, each ones start time will change on the multple calls. In the real hardware, there is not delta based on which timer.

Without this change it is possible for an HVM guest running linux to get the message:

..MP-BIOS bug: 8254 timer not connected to IO-APIC

On the guest console(s); and the guest will panic.

Also Xen hypervisor console with be flooded with:

vioapic.c:352:d1 Unsupported delivery mode 7
vioapic.c:352:d1 Unsupported delivery mode 7
vioapic.c:352:d1 Unsupported delivery mode 7

---
 xen/arch/x86/hvm/hpet.c | 55 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 21 deletions(-)
xen/arch/x86/hvm/hpet.c +34 -21
index 42c93f3..4fd6470 100644
@@ -75,17 +75,18 @@
     ((timer_config(h, n) & HPET_TN_INT_ROUTE_CAP_MASK) \
         >> HPET_TN_INT_ROUTE_CAP_SHIFT)
 
-static inline uint64_t hpet_read_maincounter(HPETState *h)
+static inline uint64_t hpet_read_maincounter(HPETState *h, uint64_t guest_time)
 {
     ASSERT(spin_is_locked(&h->lock));
 
     if ( hpet_enabled(h) )
-        return guest_time_hpet(h) + h->mc_offset;
+        return guest_time + h->mc_offset;
     else
         return h->hpet.mc64;
 }
 
-static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn)
+static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn,
+                                    uint64_t guest_time)
 {
     uint64_t comparator;
     uint64_t elapsed;
@@ -97,7 +98,8 @@ static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn)
         uint64_t period = h->hpet.period[tn];
         if (period)
         {
-            elapsed = hpet_read_maincounter(h) + period - 1 - comparator;
+            elapsed = hpet_read_maincounter(h, guest_time) +
+                period - 1 - comparator;
             comparator += (elapsed / period) * period;
             h->hpet.comparator64[tn] = comparator;
         }
@@ -109,7 +111,8 @@ static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn)
     h->hpet.timers[tn].cmp = comparator;
     return comparator;
 }
-static inline uint64_t hpet_read64(HPETState *h, unsigned long addr)
+static inline uint64_t hpet_read64(HPETState *h, unsigned long addr,
+                                   uint64_t guest_time)
 {
     addr &= ~7;
 
@@ -122,7 +125,7 @@ static inline uint64_t hpet_read64(HPETState *h, unsigned long addr)
     case HPET_STATUS:
         return h->hpet.isr;
     case HPET_COUNTER:
-        return hpet_read_maincounter(h);
+        return hpet_read_maincounter(h, guest_time);
     case HPET_Tn_CFG(0):
     case HPET_Tn_CFG(1):
     case HPET_Tn_CFG(2):
@@ -130,7 +133,7 @@ static inline uint64_t hpet_read64(HPETState *h, unsigned long addr)
     case HPET_Tn_CMP(0):
     case HPET_Tn_CMP(1):
     case HPET_Tn_CMP(2):
-        return hpet_get_comparator(h, HPET_TN(CMP, addr));
+        return hpet_get_comparator(h, HPET_TN(CMP, addr), guest_time);
     case HPET_Tn_ROUTE(0):
     case HPET_Tn_ROUTE(1):
     case HPET_Tn_ROUTE(2):
@@ -176,7 +179,7 @@ static int hpet_read(
 
     spin_lock(&h->lock);
 
-    val = hpet_read64(h, addr);
+    val = hpet_read64(h, addr, guest_time_hpet(h));
 
     result = val;
     if ( length != 8 )
@@ -189,7 +192,8 @@ static int hpet_read(
     return X86EMUL_OKAY;
 }
 
-static void hpet_stop_timer(HPETState *h, unsigned int tn)
+static void hpet_stop_timer(HPETState *h, unsigned int tn,
+                            uint64_t guest_time)
 {
     ASSERT(tn < HPET_TIMER_NUM);
     ASSERT(spin_is_locked(&h->lock));
@@ -197,14 +201,15 @@ static void hpet_stop_timer(HPETState *h, unsigned int tn)
     destroy_periodic_time(&h->pt[tn]);
     /* read the comparator to get it updated so a read while stopped will
      * return the expected value. */
-    hpet_get_comparator(h, tn);
+    hpet_get_comparator(h, tn, guest_time);
 }
 
 /* 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)
 
-static void hpet_set_timer(HPETState *h, unsigned int tn)
+static void hpet_set_timer(HPETState *h, unsigned int tn,
+                           uint64_t guest_time)
 {
     uint64_t tn_cmp, cur_tick, diff;
     unsigned int irq;
@@ -223,8 +228,8 @@ static void hpet_set_timer(HPETState *h, unsigned int tn)
     if ( !timer_enabled(h, tn) )
         return;
 
-    tn_cmp   = hpet_get_comparator(h, tn);
-    cur_tick = hpet_read_maincounter(h);
+    tn_cmp   = hpet_get_comparator(h, tn, guest_time);
+    cur_tick = hpet_read_maincounter(h, guest_time);
     if ( timer_is_32bit(h, tn) )
     {
         tn_cmp   = (uint32_t)tn_cmp;
@@ -282,6 +287,7 @@ static int hpet_write(
 {
     HPETState *h = vcpu_vhpet(v);
     uint64_t old_val, new_val;
+    uint64_t guest_time;
     int tn, i;
 
     /* Acculumate a bit mask of timers whos state is changed by this write. */
@@ -298,7 +304,8 @@ static int hpet_write(
 
     spin_lock(&h->lock);
 
-    old_val = hpet_read64(h, addr);
+    guest_time = guest_time_hpet(h);
+    old_val = hpet_read64(h, addr, guest_time);
     new_val = val;
     if ( length != 8 )
         new_val = hpet_fixup_reg(
@@ -313,7 +320,7 @@ static int hpet_write(
         if ( !(old_val & HPET_CFG_ENABLE) && (new_val & HPET_CFG_ENABLE) )
         {
             /* Enable main counter and interrupt generation. */
-            h->mc_offset = h->hpet.mc64 - guest_time_hpet(h);
+            h->mc_offset = h->hpet.mc64 - guest_time;
             for ( i = 0; i < HPET_TIMER_NUM; i++ )
             {
                 h->hpet.comparator64[i] =
@@ -327,7 +334,7 @@ static int hpet_write(
         else if ( (old_val & HPET_CFG_ENABLE) && !(new_val & HPET_CFG_ENABLE) )
         {
             /* Halt main counter and disable interrupt generation. */
-            h->hpet.mc64 = h->mc_offset + guest_time_hpet(h);
+            h->hpet.mc64 = h->mc_offset + guest_time;
             for ( i = 0; i < HPET_TIMER_NUM; i++ )
                 if ( timer_enabled(h, i) )
                     set_stop_timer(i);
@@ -441,14 +448,14 @@ static int hpet_write(
     {
         i = find_first_set_bit(stop_timers);
         __clear_bit(i, &stop_timers);
-        hpet_stop_timer(h, i);
+        hpet_stop_timer(h, i, guest_time);
     }
 
     while (start_timers)
     {
         i = find_first_set_bit(start_timers);
         __clear_bit(i, &start_timers);
-        hpet_set_timer(h, i);
+        hpet_set_timer(h, i, guest_time);
     }
 
 #undef set_stop_timer
@@ -524,6 +531,7 @@ static int hpet_load(struct domain *d, hvm_domain_context_t *h)
     HPETState *hp = domain_vhpet(d);
     struct hvm_hw_hpet *rec;
     uint64_t cmp;
+    uint64_t guest_time;
     int i;
 
     spin_lock(&hp->lock);
@@ -562,14 +570,15 @@ static int hpet_load(struct domain *d, hvm_domain_context_t *h)
 #undef C
 
     /* Recalculate the offset between the main counter and guest time */
-    hp->mc_offset = hp->hpet.mc64 - guest_time_hpet(hp);
+    guest_time = guest_time_hpet(hp);
+    hp->mc_offset = hp->hpet.mc64 - guest_time;
 
     /* restart all timers */
 
     if ( hpet_enabled(hp) )
         for ( i = 0; i < HPET_TIMER_NUM; i++ )
             if ( timer_enabled(hp, i) )
-                hpet_set_timer(hp, i);
+                hpet_set_timer(hp, i, guest_time);
 
     spin_unlock(&hp->lock);
 
@@ -617,9 +626,13 @@ void hpet_deinit(struct domain *d)
     spin_lock(&h->lock);
 
     if ( hpet_enabled(h) )
+    {
+        uint64_t guest_time = guest_time_hpet(h);
+
         for ( i = 0; i < HPET_TIMER_NUM; i++ )
             if ( timer_enabled(h, i) )
-                hpet_stop_timer(h, i);
+                hpet_stop_timer(h, i, guest_time);
+    }
 
     spin_unlock(&h->lock);
 }
Signature · 1 line
1.8.4
author
#3 lore raw

PATCH v4 3/10 hvm/hpet: Only set comparator or period not both.

In reply to Don Slutz

The current code sets both. If setting the comparator also set comparator64 (the hidden version).

Based on:

software-developers-hpet-spec-1-0a.pdf

A write call should only change comparator or period, not both.

---
 xen/arch/x86/hvm/hpet.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
xen/arch/x86/hvm/hpet.c +21 -15
index 4fd6470..910d87d 100644
@@ -401,20 +401,8 @@ static int hpet_write(
     case HPET_Tn_CMP(1):
     case HPET_Tn_CMP(2):
         tn = HPET_TN(CMP, addr);
-        if ( timer_is_32bit(h, tn) )
-            new_val = (uint32_t)new_val;
-        h->hpet.timers[tn].cmp = new_val;
-        if ( h->hpet.timers[tn].config & HPET_TN_SETVAL )
-            /*
-             * When SETVAL is one, software is able to "directly set a periodic
-             * timer's accumulator."  That is, set the comparator without
-             * adjusting the period.  Much the same as just setting the
-             * comparator on an enabled one-shot timer.
-             *
-             * This configuration bit clears when the comparator is written.
-             */
-            h->hpet.timers[tn].config &= ~HPET_TN_SETVAL;
-        else if ( timer_is_periodic(h, tn) )
+        if ( timer_is_periodic(h, tn) &&
+             !(h->hpet.timers[tn].config & HPET_TN_SETVAL) )
         {
             /*
              * Clamp period to reasonable min/max values:
@@ -426,7 +414,25 @@ static int hpet_write(
             new_val &= (timer_is_32bit(h, tn) ? ~0u : ~0ull) >> 1;
             h->hpet.period[tn] = new_val;
         }
-        h->hpet.comparator64[tn] = new_val;
+        else
+        {
+            /*
+             * When SETVAL is one, software is able to "directly set
+             * a periodic timer's accumulator."  That is, set the
+             * comparator without adjusting the period.  Much the
+             * same as just setting the comparator on an enabled
+             * one-shot timer.
+             *
+             * This configuration bit clears when the comparator is
+             * written.
+             */
+            h->hpet.timers[tn].config &= ~HPET_TN_SETVAL;
+            h->hpet.comparator64[tn] = new_val;
+            /* truncate if timer is in 32 bit mode */
+            if ( timer_is_32bit(h, tn) )
+                new_val = (uint32_t)new_val;
+            h->hpet.timers[tn].cmp = new_val;
+        }
         if ( hpet_enabled(h) && timer_enabled(h, tn) )
             set_restart_timer(tn);
         break;
Signature · 1 line
1.8.4
author
#4 lore raw

PATCH v4 4/10 hvm/hpet: Correctly limit period to a maximum.

In reply to Don Slutz

In the code section after the comment:

/*
* Clamp period to reasonable min/max values:
* - minimum is 100us, same as timers controlled by vpt.c
* - maximum is to prevent overflow in time_after() calculations */

The current maximum limit actually allows "bad" values like 0 and 1. This is because it uses a mask not a maximum.

---
 xen/arch/x86/hvm/hpet.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/hpet.c +4 -1
index 910d87d..c64c547 100644
@@ -404,6 +404,8 @@ static int hpet_write(
         if ( timer_is_periodic(h, tn) &&
              !(h->hpet.timers[tn].config & HPET_TN_SETVAL) )
         {
+            uint64_t max_period = (timer_is_32bit(h, tn) ? ~0u : ~0ull) >> 1;
+
             /*
              * Clamp period to reasonable min/max values:
              *  - minimum is 100us, same as timers controlled by vpt.c
@@ -411,7 +413,8 @@ static int hpet_write(
              */
             if ( hpet_tick_to_ns(h, new_val) < MICROSECS(100) )
                 new_val = (MICROSECS(100) << 10) / h->hpet_to_ns_scale;
-            new_val &= (timer_is_32bit(h, tn) ? ~0u : ~0ull) >> 1;
+            if ( new_val > max_period )
+                new_val = max_period;
             h->hpet.period[tn] = new_val;
         }
         else
Signature · 1 line
1.8.4
author
#5 lore raw

PATCH v4 5/10 hvm/hpet: In hpet_save, correctly compute mc64.

In reply to Don Slutz

When the master clock is not enabled, mc64 has the right value.

Basicly do the same thing as hpet_read_maincounter().

---
 xen/arch/x86/hvm/hpet.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/hpet.c +2 -1
index c64c547..0789947 100644
@@ -499,7 +499,8 @@ static int hpet_save(struct domain *d, hvm_domain_context_t *h)
     spin_lock(&hp->lock);
 
     /* Write the proper value into the main counter */
-    hp->hpet.mc64 = hp->mc_offset + guest_time_hpet(hp);
+    if ( hpet_enabled(hp) )
+        hp->hpet.mc64 = hp->mc_offset + guest_time_hpet(hp);
 
     /* Save the HPET registers */
     rc = _hvm_init_entry(h, HVM_SAVE_CODE(HPET), 0, HVM_SAVE_LENGTH(HPET));
Signature · 1 line
1.8.4
author
#6 lore raw

PATCH v4 6/10 hvm/hpet: In hpet_save, call hpet_get_comparator.

In reply to Don Slutz

This changes save data to consistent/expected values. It is not technically required because hpet_get_comparator() will adjust from any value to the correct value. And hpet_get_comparator() is effectivly called in hpet_load via hpet_set_timer.

However it does look strange to people that the output from xen-hvmctx for the comparator values do not change when the master clock does.

The software-developers-hpet-spec-1-0a.pdf says that the comparator will allways be greater than master clock for a periodic timer.

---
v4:
Correctly set guest_time.
Fix formatting of comments.

 xen/arch/x86/hvm/hpet.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hpet.c +15 -3
index 0789947..b611304 100644
@@ -495,12 +495,14 @@ static int hpet_save(struct domain *d, hvm_domain_context_t *h)
 {
     HPETState *hp = domain_vhpet(d);
     int rc;
+    uint64_t guest_time;
 
     spin_lock(&hp->lock);
+    guest_time = guest_time_hpet(hp);
 
     /* Write the proper value into the main counter */
     if ( hpet_enabled(hp) )
-        hp->hpet.mc64 = hp->mc_offset + guest_time_hpet(hp);
+        hp->hpet.mc64 = hp->mc_offset + guest_time;
 
     /* Save the HPET registers */
     rc = _hvm_init_entry(h, HVM_SAVE_CODE(HPET), 0, HVM_SAVE_LENGTH(HPET));
@@ -524,8 +526,18 @@ static int hpet_save(struct domain *d, hvm_domain_context_t *h)
         C(period[1]);
         C(period[2]);
 #undef C
-        /* save the 64 bit comparator in the 64 bit timer[n].cmp field
-         * regardless of whether or not the timer is in 32 bit mode. */
+        /*
+         * read the comparator to get it updated so hpet_save will
+         * return the expected value.
+         */
+        hpet_get_comparator(hp, 0, guest_time);
+        hpet_get_comparator(hp, 1, guest_time);
+        hpet_get_comparator(hp, 2, guest_time);
+        /*
+         * save the 64 bit comparator in the 64 bit timer[n].cmp
+         * field regardless of whether or not the timer is in 32 bit
+         * mode.
+         */
         rec->timers[0].cmp = hp->hpet.comparator64[0];
         rec->timers[1].cmp = hp->hpet.comparator64[1];
         rec->timers[2].cmp = hp->hpet.comparator64[2];
Signature · 1 line
1.8.4
Acked-by on v4 6/10
#7 lore raw

Re: [PATCH v4 06/10] hvm/hpet: In hpet_save, call hpet_get_comparator.

Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 02.05.14 at 22:18, <[email protected]> wrote:

This changes save data to consistent/expected values. It is not technically required because hpet_get_comparator() will adjust from any value to the correct value. And hpet_get_comparator() is effectivly called in hpet_load via hpet_set_timer.

Quoted 8 lines

However it does look strange to people that the output from xen-hvmctx for the comparator values do not change when the master clock does.

The software-developers-hpet-spec-1-0a.pdf says that the comparator will allways be greater than master clock for a periodic timer.

Quoted 52 lines

---
v4:
Correctly set guest_time.
Fix formatting of comments.

 xen/arch/x86/hvm/hpet.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/hpet.c +3 -1
index 0789947..b611304 100644
@@ -495,12 +495,14 @@ static int hpet_save(struct domain *d, 
 hvm_domain_context_t *h)
 {
     HPETState *hp = domain_vhpet(d);
     int rc;
+    uint64_t guest_time;
 
     spin_lock(&hp->lock);
+    guest_time = guest_time_hpet(hp);
 
     /* Write the proper value into the main counter */
     if ( hpet_enabled(hp) )
-        hp->hpet.mc64 = hp->mc_offset + guest_time_hpet(hp);
+        hp->hpet.mc64 = hp->mc_offset + guest_time;
 
     /* Save the HPET registers */
     rc = _hvm_init_entry(h, HVM_SAVE_CODE(HPET), 0, HVM_SAVE_LENGTH(HPET));
xen/arch/x86/hvm/hpet.c +12 -2
@@ -524,8 +526,18 @@ static int hpet_save(struct domain *d, hvm_domain_context_t *h)
         C(period[1]);
         C(period[2]);
 #undef C
-        /* save the 64 bit comparator in the 64 bit timer[n].cmp field
-         * regardless of whether or not the timer is in 32 bit mode. */
+        /*
+         * read the comparator to get it updated so hpet_save will
+         * return the expected value.
+         */
+        hpet_get_comparator(hp, 0, guest_time);
+        hpet_get_comparator(hp, 1, guest_time);
+        hpet_get_comparator(hp, 2, guest_time);
+        /*
+         * save the 64 bit comparator in the 64 bit timer[n].cmp
+         * field regardless of whether or not the timer is in 32 bit
+         * mode.
+         */
         rec->timers[0].cmp = hp->hpet.comparator64[0];
         rec->timers[1].cmp = hp->hpet.comparator64[1];
         rec->timers[2].cmp = hp->hpet.comparator64[2];
Signature · 1 line
1.8.4
author
#8 lore raw

PATCH v4 7/10 hvm/hpet: Init comparator64 like comparator.

In reply to Don Slutz

The software-developers-hpet-spec-1-0a.pdf says that the comparator starts as all 1's. Also make the hidden register comparator64 the same.

Since only the hidden register comparator64 is used by hpet_save, it needs to start out with the right value.

A disabled hpet (like when a guest is starting), should start with the value the spec says. Both the guest (via reading the comparator) and an administrator using xen-hvmctx, will see all 0's not all 1's.

---
v4:
Added paragraph starting "A disabled hpet" to commit message.

 xen/arch/x86/hvm/hpet.c | 1 +
 1 file changed, 1 insertion(+)
xen/arch/x86/hvm/hpet.c +1 -0
index b611304..741fb7a 100644
@@ -636,6 +636,7 @@ void hpet_init(struct vcpu *v)
         h->hpet.timers[i].config =
             HPET_TN_INT_ROUTE_CAP | HPET_TN_64BIT_CAP | HPET_TN_PERIODIC_CAP;
         h->hpet.timers[i].cmp = ~0ULL;
+        h->hpet.comparator64[i] = ~0ULL;
         h->pt[i].source = PTSRC_isa;
     }
 }
Signature · 1 line
1.8.4
Acked-by on v4 7/10
#9 lore raw

Re: [PATCH v4 07/10] hvm/hpet: Init comparator64 like comparator.

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 02.05.14 at 22:18, <[email protected]> wrote:

The software-developers-hpet-spec-1-0a.pdf says that the comparator starts as all 1's. Also make the hidden register comparator64 the same.

Since only the hidden register comparator64 is used by hpet_save, it needs to start out with the right value.

Quoted 6 lines

A disabled hpet (like when a guest is starting), should start with the value the spec says. Both the guest (via reading the comparator) and an administrator using xen-hvmctx, will see all 0's not all 1's.

Quoted 22 lines

---
v4:
Added paragraph starting "A disabled hpet" to commit message.

 xen/arch/x86/hvm/hpet.c | 1 +
 1 file changed, 1 insertion(+)
xen/arch/x86/hvm/hpet.c +1 -0
index b611304..741fb7a 100644
@@ -636,6 +636,7 @@ void hpet_init(struct vcpu *v)
         h->hpet.timers[i].config =
             HPET_TN_INT_ROUTE_CAP | HPET_TN_64BIT_CAP | 
 HPET_TN_PERIODIC_CAP;
         h->hpet.timers[i].cmp = ~0ULL;
+        h->hpet.comparator64[i] = ~0ULL;
         h->pt[i].source = PTSRC_isa;
     }

}

Signature · 1 line
1.8.4
author
#10 lore raw

PATCH v4 8/10 hvm/hpet: comparator can only change when master clock is enabled.

In reply to Don Slutz

This is based on software-developers-hpet-spec-1-0a.pdf saying:

When the main counter value matches the value in the timer's comparator register, an interrupt can be generated. The hardware will then automatically increase the value in the compare register by the last value written to that register.

When the overall enable is off (the main count is halted), none of the compare registers should change.

The code lines:

    elapsed = hpet_read_maincounter(h, guest_time) +
        period - 1 - comparator;
    comparator += (elapsed / period) * period;

are what matter here. They will always adjust comparator to be no more then one period away.

Using some numbers to help show the issue:

hpet_read_maincounter(h, guest_time) = 67752
period = 62500
comparator = 255252 == 67752 + 3 * 62500

comparator : 255252
elapsed : -125001
elapsed/period : -2
comparator_delta : -125000
new comparator : 130252

---
 xen/arch/x86/hvm/hpet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/hpet.c +1 -1
index 741fb7a..f938da9 100644
@@ -92,7 +92,7 @@ static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn,
     uint64_t elapsed;
 
     comparator = h->hpet.comparator64[tn];
-    if ( timer_is_periodic(h, tn) )
+    if ( hpet_enabled(h) && timer_is_periodic(h, tn) )
     {
         /* update comparator by number of periods elapsed since last update */
         uint64_t period = h->hpet.period[tn];
Signature · 1 line
1.8.4
author
#11 lore raw

PATCH v4 9/10 hvm/hpet: Prevent master clock equal to comparator while enabled

In reply to Don Slutz

Based on the software-developers-hpet-spec-1-0a.pdf, the comparator for a periodic timer will change to the new value when it matches the master clock. The current code here uses a very standard rounding formula of "((x + y - 1) / y) * y". This is wrong because in this case you need to go to the next comparator value when "x" equals "y". Not when "x + 1" equals "y". In this case "y" is the period and "x" is the master clock.

The code lines:

    elapsed = hpet_read_maincounter(h, guest_time) +
        period - 1 - comparator;
    comparator += (elapsed / period) * period;

are what matter here.

Using some numbers to help show the issue:

hpet_read_maincounter(h, guest_time) = 130252
period = 62500

comparator : 130252
elapsed : 62499
elapsed/period : 0
comparator_delta : 0
new comparator : 130252

---
 xen/arch/x86/hvm/hpet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/hpet.c +1 -1
index f938da9..2a99fed 100644
@@ -99,7 +99,7 @@ static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn,
         if (period)
         {
             elapsed = hpet_read_maincounter(h, guest_time) +
-                period - 1 - comparator;
+                period - comparator;
             comparator += (elapsed / period) * period;
             h->hpet.comparator64[tn] = comparator;
         }
Signature · 1 line
1.8.4
Acked-by on v4 9/10
#12 lore raw

Re: [PATCH v4 09/10] hvm/hpet: Prevent master clock equal to comparator while enabled

Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 02.05.14 at 22:18, <[email protected]> wrote:

Based on the software-developers-hpet-spec-1-0a.pdf, the comparator for a periodic timer will change to the new value when it matches the master clock. The current code here uses a very standard rounding formula of "((x + y - 1) / y) * y". This is wrong because in this case you need to go to the next comparator value when "x" equals "y". Not when "x + 1" equals "y". In this case "y" is the period and "x" is the master clock.

The code lines:

    elapsed = hpet_read_maincounter(h, guest_time) +
        period - 1 - comparator;
    comparator += (elapsed / period) * period;

are what matter here.

Using some numbers to help show the issue:

hpet_read_maincounter(h, guest_time) = 130252
period = 62500

Quoted 7 lines

comparator : 130252
elapsed : 62499
elapsed/period : 0
comparator_delta : 0
new comparator : 130252

Quoted 20 lines
---
 xen/arch/x86/hvm/hpet.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/hpet.c +1 -1
index f938da9..2a99fed 100644
@@ -99,7 +99,7 @@ static uint64_t hpet_get_comparator(HPETState *h, unsigned 
 int tn,
         if (period)
         {
             elapsed = hpet_read_maincounter(h, guest_time) +
-                period - 1 - comparator;
+                period - comparator;
             comparator += (elapsed / period) * period;
             h->hpet.comparator64[tn] = comparator;
         }
Signature · 1 line
1.8.4
author
#13 lore raw

PATCH v4 10/10 hvm/hpet: Detect comparator values in the past

In reply to Don Slutz

This statement only works using 64-bit arithmetic for the main

                                     63

counter never changing by more then 2 . (Which is a boundary case that should not happen in my life time.)

---
 xen/arch/x86/hvm/hpet.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/hpet.c +6 -4
index 2a99fed..167994e 100644
@@ -98,10 +98,12 @@ static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn,
         uint64_t period = h->hpet.period[tn];
         if (period)
         {
-            elapsed = hpet_read_maincounter(h, guest_time) +
-                period - comparator;
-            comparator += (elapsed / period) * period;
-            h->hpet.comparator64[tn] = comparator;
+            elapsed = hpet_read_maincounter(h, guest_time) - comparator;
+            if ( (int64_t)elapsed >= 0 )
+            {
+                comparator += ((elapsed + period) / period) * period;
+                h->hpet.comparator64[tn] = comparator;
+            }
         }
     }
 
Signature · 1 line
1.8.4
Applied
#14 lore raw
In reply to Don Slutz
Quoted 11 lines · Don Slutz

Don Slutz (10):
hvm/hpet: Add manual unit test code.
hvm/hpet: Only call guest_time_hpet(h) one time per action. hvm/hpet: Only set comparator or period not both. hvm/hpet: Correctly limit period to a maximum. hvm/hpet: In hpet_save, correctly compute mc64. hvm/hpet: In hpet_save, call hpet_get_comparator. hvm/hpet: Init comparator64 like comparator.
hvm/hpet: comparator can only change when master clock is enabled. hvm/hpet: Prevent master clock equal to comparator while enabled hvm/hpet: Detect comparator values in the past

Applied, thanks.

Tim.