Prevent one cause of "MP-BIOS bug: 8254 timer"... message from linux
v3 cover Prevent one cause of "MP-BIOS bug: 8254 timer"... message from linux
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 (11):
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: Use signed divide in hpet_get_comparator. hvm/hpet: comparator can only change when master clock is enabled. hvm/hpet: Prevent master clock equal to comparator while enabled hvm/hpet: handle 1st period special
tools/tests/vhpet/.gitignore | 4 + tools/tests/vhpet/Makefile | 28 ++ tools/tests/vhpet/emul.h | 416 +++++++++++++++++++++++ tools/tests/vhpet/main.c | 768 ++++++++++++++++++++++++++++++++++++++++++ xen/arch/x86/hvm/hpet.c | 192 ++++++++--- xen/include/asm-x86/hvm/vpt.h | 2 + 6 files changed, 1359 insertions(+), 51 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
PATCH v3 1/11 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.
---
v3:
Add Makefile.
Better commit message.
Make it optional.
Add hpet_check_stopped() testing.
Adjust print_error messages.
tools/tests/vhpet/.gitignore | 4 + tools/tests/vhpet/Makefile | 28 ++ tools/tests/vhpet/emul.h | 416 +++++++++++++++++++++++ tools/tests/vhpet/main.c | 768 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1216 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
@@ -0,0 +1,4 @@ +test_vhpet +test_vhpet.out +hpet.h +hpet.c
tools/tests/vhpet/Makefile new file +28 -0
@@ -0,0 +1,28 @@ + +XEN_ROOT=$(CURDIR)/../../.. +include $(XEN_ROOT)/tools/Rules.mk + +TARGET := test_vhpet + +.PHONY: all +all: $(TARGET) + +.PHONY: run +run: $(TARGET) + ./$(TARGET) > $(TARGET).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 $(XEN_ROOT)/xen/include/asm-x86/hpet.h . + +hpet.c: $(XEN_ROOT)/xen/arch/x86/hvm/hpet.c + sed -e "/#include/d" -e "1i#include \"emul.h\"\n" <$(XEN_ROOT)/xen/arch/x86/hvm/hpet.c >hpet.c
tools/tests/vhpet/emul.h new file +416 -0
416 lines not on this page · raw message
tools/tests/vhpet/main.c new file +768 -0
768 lines not on this page · raw message
Signature · 1 line
1.8.4
Re: [optional PATCH v3 01/11] hvm/hpet: Add manual unit test code.
tools/tests/vhpet/Makefile line 28
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:42, <[email protected]> wrote:
tools/tests/vhpet/Makefile new file +5
-0+hpet.h: $(XEN_ROOT)/xen/include/asm-x86/hpet.h + cp $(XEN_ROOT)/xen/include/asm-x86/hpet.h . + +hpet.c: $(XEN_ROOT)/xen/arch/x86/hvm/hpet.c + sed -e "/#include/d" -e "1i#include \"emul.h\"\n" <$(XEN_ROOT)/xen/arch/x86/hvm/hpet.c >hpet.c
Just as a general remark (I'm not myself intending to review or commit this patch): You should try to use shortcuts when possible, i.e. $< and $@ in both above rules.
Jan
Re: [optional PATCH v3 01/11] hvm/hpet: Add manual unit test code.
Quoted 9 lines · Jan Beulich
tools/tests/vhpet/Makefile line 28
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:42, <[email protected]> wrote:
tools/tests/vhpet/Makefile new file +5
-0+hpet.h: $(XEN_ROOT)/xen/include/asm-x86/hpet.h + cp $(XEN_ROOT)/xen/include/asm-x86/hpet.h . + +hpet.c: $(XEN_ROOT)/xen/arch/x86/hvm/hpet.c + sed -e "/#include/d" -e "1i#include \"emul.h\"\n" <$(XEN_ROOT)/xen/arch/x86/hvm/hpet.c >hpet.cJust as a general remark (I'm not myself intending to review or commit this patch): You should try to use shortcuts when possible, i.e. $< and $@ in both above rules.
Ok
-Don Slutz
Quoted 7 lines
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
PATCH v3 2/11 hvm/hpet: Only call guest_time_hpet(h) one time per action.
In reply to Don SlutzThis 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
---
v3:
Did not add Reviewed-by (Jan Beulich) do to amount of change Added passing of guest_time to hpet_read64() and
hpet_stop_timer().
Dropped mc_starting.
xen/arch/x86/hvm/hpet.c | 55 ++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 21 deletions(-)
xen/arch/x86/hvm/hpet.c +34 -21
@@ -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
Re: [PATCH v3 02/11] hvm/hpet: Only call guest_time_hpet(h) one time per action.
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:42, <[email protected]> wrote:
v3:
Did not add Reviewed-by (Jan Beulich) do to amount of change Added passing ofguest_timetohpet_read64()andhpet_stop_timer().
But that wasn't as a result of the review of v2, was it? Especially for the hpet_read64() case I fear this is going a little too far, since now you call the supposedly expensive function even when the value isn't needed. I suppose you could resolve this by passing a known-invalid value to the function from hpet_read() (so it knows to call guest_time_hpet() itself) and use the always-preset variant on from hpet_write().
Otoh I admit that the only supposedly frequently use operations ought to be the two ones needing the value, so perhaps the overhead for the other ones is tolerable. Let's see whether others have any opinion on this.
Apart from that I'm still fine with the patch.
Jan
Re: [PATCH v3 02/11] hvm/hpet: Only call guest_time_hpet(h) one time per action.
Quoted 6 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:42, <[email protected]> wrote:
v3:
Did not add Reviewed-by (Jan Beulich) do to amount of change Added passing ofguest_timetohpet_read64()andhpet_stop_timer().But that wasn't as a result of the review of v2, was it?
Not on this thread. I was using gdb to get answers for a later patch.
Quoted 7 lines
Especially for
thehpet_read64()case I fear this is going a little too far, since now you call the supposedly expensive function even when the value isn't needed. I suppose you could resolve this by passing a known-invalid value to the function fromhpet_read()(so it knows to callguest_time_hpet()itself) and use the always-preset variant on fromhpet_write().
I also considered a case statement on the call (since the value is needed later when hpet_write also uses it). I do not think that any value is invalid.
Quoted 5 lines
Otoh I admit that the only supposedly frequently use operations ought to be the two ones needing the value, so perhaps the overhead for the other ones is tolerable. Let's see whether others have any opinion on this.
I also expect that the frequently used operations are the ones that need it. And it is the lower risk of add a bug. Will wait for any other comments.
-Don Slutz
Quoted 4 lines
Apart from that I'm still fine with the patch.
Jan
Re: [PATCH v3 02/11] hvm/hpet: Only call guest_time_hpet(h) one time per action.
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 23.04.14 at 17:42, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:42, <[email protected]> wrote:
v3:
Did not add Reviewed-by (Jan Beulich) do to amount of change Added passing ofguest_timetohpet_read64()andhpet_stop_timer().But that wasn't as a result of the review of v2, was it?
Not on this thread. I was using gdb to get answers for a later patch.
Quoted 7 lines
Especially for
thehpet_read64()case I fear this is going a little too far, since now you call the supposedly expensive function even when the value isn't needed. I suppose you could resolve this by passing a known-invalid value to the function fromhpet_read()(so it knows to callguest_time_hpet()itself) and use the always-preset variant on fromhpet_write().
Quoted 3 lines
I also considered a case statement on the call (since the value is needed later when
hpet_writealso uses it). I do not think that any value is invalid.
With
#define STIME_PER_HPET_TICK 16
#define guest_time_hpet(hpet) \
(hvm_get_guest_time(vhpet_vcpu(hpet)) / STIME_PER_HPET_TICK)
any value with one or more of the high 4 bits set would be invalid.
Jan
PATCH v3 3/11 hvm/hpet: Only set comparator or period not both.
In reply to Don SlutzThe 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.
---
v3:
Only have 2 blocks of code.
Set comparator64 before truncation
xen/arch/x86/hvm/hpet.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-)
xen/arch/x86/hvm/hpet.c +21 -15
@@ -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
Re: [PATCH v3 03/11] hvm/hpet: Only set comparator or period not both.
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:42, <[email protected]> wrote:
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.
Signed-off-by Don Slutz <[email protected]>
Quoted 64 lines
v3:
Only have 2 blocks of code.
Set comparator64 before truncationxen/arch/x86/hvm/hpet.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-)xen/arch/x86/hvm/hpet.c +0
-6@@ -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
xen/arch/x86/hvm/hpet.c +21
-9-* 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
PATCH v3 4/11 hvm/hpet: Correctly limit period to a maximum.
In reply to Don SlutzIn 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
@@ -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
Re: [PATCH v3 04/11] hvm/hpet: Correctly limit period to a maximum.
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:42, <[email protected]> wrote:
In the code section after the comment:
/*
* Clamp period to reasonable min/max values:
* - minimum is 100us, same as timers controlled byvpt.c
* - maximum is to prevent overflow intime_after()calculations */
Quoted 4 lines
The current maximum limit actually allows "bad" values like 0 and 1. This is because it uses a mask not a maximum.
Signed-off-by Don Slutz <[email protected]>
Quoted 28 lines
xen/arch/x86/hvm/hpet.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)xen/arch/x86/hvm/hpet.c +4
-1@@ -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
PATCH v3 5/11 hvm/hpet: In hpet_save, correctly compute mc64.
In reply to Don SlutzWhen 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
@@ -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
Re: [PATCH v3 05/11] hvm/hpet: In hpet_save, correctly compute mc64.
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:42, <[email protected]> wrote:
When the master clock is not enabled, mc64 has the right value.
Basicly do the same thing as
hpet_read_maincounter().Signed-off-by Don Slutz <[email protected]>
Quoted 20 lines
xen/arch/x86/hvm/hpet.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)xen/arch/x86/hvm/hpet.c +2
-1@@ -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
PATCH v3 6/11 hvm/hpet: In hpet_save, call hpet_get_comparator.
In reply to Don SlutzThis 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.
---
v3:
Better commit message.
Reword subject from "hvm/hpet: Call hpet_get_comparator during hpet_save."
xen/arch/x86/hvm/hpet.c | 7 +++++++ 1 file changed, 7 insertions(+)
xen/arch/x86/hvm/hpet.c +7 -0
@@ -495,6 +495,7 @@ 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);
@@ -524,6 +525,12 @@ static int hpet_save(struct domain *d, hvm_domain_context_t *h)C (period[1 ]);C (period[2 ]);#undef C +/* read the comparator to get it updated so hpet_save will +* return the expected value. */ + guest_time = hp->hpet.mc64 - hp->mc_offset; +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 ];
Signature · 1 line
1.8.4
Re: [PATCH v3 06/11] hvm/hpet: In hpet_save, call hpet_get_comparator.
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[email protected]> wrote:
xen/arch/x86/hvm/hpet.c +1
-0@@ -495,6 +495,7 @@ 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);
xen/arch/x86/hvm/hpet.c static int hpet_save(struct domain *d, hvm_domain_context_t *h) · line 530
Quoted 6 lines
xen/arch/x86/hvm/hpet.c +3
-0@@ -524,6 +525,12 @@ static int hpet_save(struct domain *d, hvm_domain_context_t *h)C (period[1 ]);C (period[2 ]);#undef C +/* read the comparator to get it updated so hpet_save will +* return the expected value. */ + guest_time = hp->hpet.mc64 - hp->mc_offset;
Wouldn't you be better off loading guest_time via guest_time_hpet() right after taking the lock, using the variable also in the assignment to mc64? I'm particularly suspecting an inconsistency in the !hpet_enabled() case (i.e. when mc64 doesn't get updated anymore). Iirc a later patch (in the earlier version of the series) makes the adjustment in hpet_get_comparator() also conditional upon the HPET being enabled, in which case the end effect would be identical (due to hpet_get_comparator() then becoming a nop for that case).
In any event, no matter that the immediately following comment is malformed too, please fix your comment style (and feel free to adjust the one below at once).
xen/arch/x86/hvm/hpet.c static int hpet_save(struct domain *d, hvm_domain_context_t *h) · line 536
Quoted 6 lines
xen/arch/x86/hvm/hpet.c +3
-0+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 ];
Jan
Re: [PATCH v3 06/11] hvm/hpet: In hpet_save, call hpet_get_comparator.
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[email protected]> wrote:
xen/arch/x86/hvm/hpet.c +1
-0@@ -495,6 +495,7 @@ 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);
Quoted 16 lines
xen/arch/x86/hvm/hpet.c static int hpet_save(struct domain *d, hvm_domain_context_t *h) · line 530
Quoted 7 lines
xen/arch/x86/hvm/hpet.c +3
-0@@ -524,6 +525,12 @@ static int hpet_save(struct domain *d, hvm_domain_context_t *h)C (period[1 ]);C (period[2 ]);#undef C +/* read the comparator to get it updated so hpet_save will +* return the expected value. */ + guest_time = hp->hpet.mc64 - hp->mc_offset;Wouldn't you be better off loading
guest_timeviaguest_time_hpet()right after taking the lock, using the variable also in the assignment to mc64? I'm particularly suspecting an inconsistency in the !hpet_enabled() case (i.e. when mc64 doesn't get updated anymore). Iirc a later patch (in the earlier version of the series) makes the adjustment inhpet_get_comparator()also conditional upon the HPET being enabled, in which case the end effect would be identical (due tohpet_get_comparator()then becoming a nop for that case).
You are right.
Quoted 3 lines
In any event, no matter that the immediately following comment is malformed too, please fix your comment style (and feel free to adjust the one below at once).
Will do.
-Don Slutz
Quoted 8 lines
xen/arch/x86/hvm/hpet.c static int hpet_save(struct domain *d, hvm_domain_context_t *h) · line 536
Quoted 6 lines
xen/arch/x86/hvm/hpet.c +3
-0+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 ];Jan
PATCH v3 7/11 hvm/hpet: Init comparator64 like comparator.
In reply to Don SlutzThe 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.
--- xen/arch/x86/hvm/hpet.c | 1 + 1 file changed, 1 insertion(+)
xen/arch/x86/hvm/hpet.c +1 -0
@@ -631,6 +631,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
Re: [PATCH v3 07/11] hvm/hpet: Init comparator64 like comparator.
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[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.
But the immediately preceding patch arranged for the field to get suitably updated, so is this change really doing anything but cosmetics?
Jan
Re: [PATCH v3 07/11] hvm/hpet: Init comparator64 like comparator.
Quoted 9 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[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.But the immediately preceding patch arranged for the field to get suitably updated, so is this change really doing anything but cosmetics?
The preceding patch only makes a difference when the hpet is enabled. A disabled hpet (like when a guest is starting), should start with the value the spec says.
Both hpet_save and the guest reading the comparator register use comparator64 to get the value. So instead of just adding the line with comparator64, I could also delete the setting of .cmp. I picked the simpler one (just add the init).
So either way, not doing this change and for example using xen-hvmctx on a guest that was started paused, you see all 0 not all 1.
-Don Slutz
Quoted 2 lines
Jan
PATCH v3 8/11 hvm/hpet: Use signed divide in hpet_get_comparator.
In reply to Don SlutzThis is done so that when comparator is less then or equal to one period it does not change.
The code lines:
elapsed = hpet_read_maincounter(h, guest_time) +
period - 1 - comparator;
comparator += (elapsed / period) * period;are what matter here. For almost all cases where "hpet_read_maincounter() + period - 1" is greater then "comparator", a signed divide will produce the same answer as an unsigned divide. One of the problem areas is when "hpet_read_maincounter() + period - 1" needs more then 64 bits to represent it. It includes cases where hpet_read_maincounter() has wrapped (I.E. needs more then 64 bits to correctly represent it), but "comparator" has not wrapped. However "elapsed" is correctly greater then zero as long as the mathematical result only takes 63 bits to represent it.
It is safe to declare period as a signed integer because the code already limits it to 63 bits.
Using some numbers to help show the issue:
hpet_read_maincounter(h, guest_time) = 67752
period = 62500
comparator = 130252 == 67752 + 62500
what unsigned signed
comparator : 130252 130252
elapsed : 18446744073709551615 -1
elapsed/period : 295147905179352 0comparator_delta : 18446744073709500000 0
new comparator : 78636 130252
--- xen/arch/x86/hvm/hpet.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/hpet.c +2 -2
@@ -89,13 +89,13 @@ static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn, uint64_t guest_time) {uint64_t comparator; -uint64_t elapsed; +int64_t elapsed; comparator = h->hpet.comparator64[tn];if (timer_is_periodic (h, tn) ) {/* update comparator by number of periods elapsed since last update */ -uint64_t period = h->hpet.period[tn]; +int64_t period = h->hpet.period[tn];if (period) { elapsed = hpet_read_maincounter(h, guest_time) +
Signature · 1 line
1.8.4
Re: [PATCH v3 08/11] hvm/hpet: Use signed divide in hpet_get_comparator.
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[email protected]> wrote:
This is done so that when comparator is less then or equal to one period it does not change.
The code lines:
elapsed = hpet_read_maincounter(h, guest_time) + period - 1 - comparator; comparator += (elapsed / period) * period;
Quoted 6 lines
are what matter here. For almost all cases where "
hpet_read_maincounter()+ period - 1" is greater then "comparator", a signed divide will produce the same answer as an unsigned divide. One of the problem areas is when "hpet_read_maincounter()+ period - 1" needs more then 64 bits to represent it. It includes cases wherehpet_read_maincounter()has
"It includes" implies there are other cases, yet I can't see any. (Also twice s/then/than/ I think.)
Quoted 2 lines
wrapped (I.E. needs more then 64 bits to correctly represent it), but "comparator" has not wrapped.
But can this happen in practice? Main counter and comparator shouldn't be very far apart, and hence other than "hpet_read_maincounter() + period - 1", "hpet_read_maincounter() + period - 1 - comparator" shouldn't ever require more than 64 bits to correctly represent it. All you really need to deal with is the case where hpet_read_maincounter() + period == comparator, i.e. the 1 being subtracted makes the expression degenerate. So you could simply make the computation and update dependent upon hpet_read_maincounter() + period > comparator.
And then again I don't really see why the subtraction of 1 is needed here in the first place.
Jan
Re: [PATCH v3 08/11] hvm/hpet: Use signed divide in hpet_get_comparator.
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[email protected]> wrote:
This is done so that when comparator is less then or equal to one period it does not change.
The code lines:
elapsed = hpet_read_maincounter(h, guest_time) + period - 1 - comparator; comparator += (elapsed / period) * period;
Quoted 8 lines
Quoted 6 lines
are what matter here. For almost all cases where "
hpet_read_maincounter()+ period - 1" is greater then "comparator", a signed divide will produce the same answer as an unsigned divide. One of the problem areas is when "hpet_read_maincounter()+ period - 1" needs more then 64 bits to represent it. It includes cases wherehpet_read_maincounter()has"It includes" implies there are other cases, yet I can't see any. (Also twice s/then/than/ I think.)
The boundary cases of 2's complement show up here (8 bits is just nice small numbers: -128 (signed) vs 128 (unsigned)). Ok.
Quoted 14 lines
Quoted 2 lines
wrapped (I.E. needs more then 64 bits to correctly represent it), but "comparator" has not wrapped.
But can this happen in practice? Main counter and comparator shouldn't be very far apart, and hence other than "
hpet_read_maincounter()+ period - 1", "hpet_read_maincounter()+ period - 1 - comparator" shouldn't ever require more than 64 bits to correctly represent it. All you really need to deal with is the case wherehpet_read_maincounter()+ period == comparator, i.e. the 1 being subtracted makes the expression degenerate. So you could simply make the computation and update dependent uponhpet_read_maincounter()+ period > comparator.
And then again I don't really see why the subtraction of 1 is needed here in the first place.
As you saw in patch #10, the -1 is an issue.
Doing patch #10 1st, removes the normal Linux case.
After more thinking on it, the last patch (#11) does make this patch (#8) not needed. The case
that is overlapping is:
If the 1st period's cmp is > 1 period by less then 2 periods (using numbers from #8 adjusted):
hpet_read_maincounter(h, guest_time) = 67752
period = 62500
comparator = 130253 == 67752 + 62500 + 1
what unsigned signed
comparator : 130253 130253
elapsed : 18446744073709551615 -1
elapsed/period : 295147905179352 0comparator_delta : 18446744073709500000 0
new comparator : 78637 130253
and
hpet_read_maincounter(h, guest_time) = 67752
period = 62500
comparator = 161502 == 67752 + 62500 + 31250
what unsigned signed
comparator : 161502 161502
elapsed : 18446744073709520366 -31250
elapsed/period : 295147905179352 0comparator_delta : 18446744073709500000 0
new comparator : 109886 161502
It does the right thing, but does not handle the general case.
But since the last patch also handles this case, I will be dropping this patch in v4.
-Don Slutz
Quoted 3 lines
Jan
PATCH v3 9/11 hvm/hpet: comparator can only change when master clock is enabled.
In reply to Don SlutzThis 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 : -2comparator_delta : -125000
new comparator : 130252
---
v3:
Better commit message.
xen/arch/x86/hvm/hpet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/hpet.c +1 -1
@@ -92,7 +92,7 @@ static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn,int64_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 */ int64_t period = h->hpet.period[tn];
Signature · 1 line
1.8.4
Re: [PATCH v3 09/11] hvm/hpet: comparator can only change when master clock is enabled.
Quoted 26 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[email protected]> wrote:
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
Quoted 7 lines
comparator : 255252
elapsed : -125001
elapsed/period : -2comparator_delta: -125000
new comparator : 130252Signed-off-by Don Slutz <[email protected]>
Quoted 24 lines
---
v3:
Better commit message.xen/arch/x86/hvm/hpet.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)xen/arch/x86/hvm/hpet.c +1
-1@@ -92,7 +92,7 @@ static uint64_t hpet_get_comparator(HPETState *h, unsignedint tn, int64_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 */
int64_t period = h->hpet.period[tn];Signature · 1 line
1.8.4
PATCH v3 10/11 hvm/hpet: Prevent master clock equal to comparator while enabled
In reply to Don SlutzBased 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 : 0comparator_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
@@ -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
Re: [PATCH v3 10/11] hvm/hpet: Prevent master clock equal to comparator while enabled
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[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.
Quoted 7 lines
The code lines:
elapsed = hpet_read_maincounter(h, guest_time) + period - 1 - comparator; comparator += (elapsed / period) * period;are what matter here.
So this is basically answering the question I raised on an earlier patch, and I think it implies that this earlier patch is then pointless (or should likely be folded with this one).
Jan
Re: [PATCH v3 10/11] hvm/hpet: Prevent master clock equal to comparator while enabled
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43,<[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:
Quoted 8 lines
Quoted 5 lines
elapsed = hpet_read_maincounter(h, guest_time) + period - 1 - comparator; comparator += (elapsed / period) * period;are what matter here.
So this is basically answering the question I raised on an earlier patch, and I think it implies that this earlier patch is then pointless (or should likely be folded with this one).
Yes, the -1 is wrong.
After more thinking on it, the last patch (#11) does make the previous patch (#8) not needed.
Looks like I will be dropping patch #8 "hvm/hpet: Use signed divide in hpet_get_comparator."
as not needed.
-Don Slutz
Quoted 2 lines
Jan
PATCH v3 11/11 hvm/hpet: handle 1st period special
In reply to Don SlutzThe software-developers-hpet-spec-1-0a.pdf says that the 1st interrupt is based on the setting of comparator. After that it will be on each period. Add code that checks for this special case.
Add a callback routine when this special case is active that will disable the special case on the 1st interrupt.
The code lines:
elapsed = mc + period - comparator;
comparator += (elapsed / period) * period;are what matter here.
Using some numbers to help show the issue:
mc = 67752
period = 62500
comparator : 380252
elapsed : -250000
elapsed/period : -4comparator_delta : -250000
new comparator : 130252
So this code needs to be skipped on the 1st period.
The change "hvm/hpet: In hpet_save, call hpet_get_comparator." that previously was "technically not required", is now required. Because hpet_load now handles 1st period differently, the hpet_save needs to provide the data as the software-developers-hpet-spec-1-0a.pdf implies.
---
v3:
Better commit message.
More setting of first_mc64 & first_enabled when needed. Switch to bool_t.
xen/arch/x86/hvm/hpet.c | 83 ++++++++++++++++++++++++++++++++++++------- xen/include/asm-x86/hvm/vpt.h | 2 ++ 2 files changed, 73 insertions(+), 12 deletions(-)
xen/arch/x86/hvm/hpet.c +71 -12
@@ -96,10 +96,18 @@ static uint64_t hpet_get_comparator(HPETState *h, unsigned int tn, {/* update comparator by number of periods elapsed since last update */ int64_t period = h->hpet.period[tn]; +uint64_t mc =hpet_read_maincounter (h, guest_time); + +if ( h->hpet.first_enabled[tn] ) + { +if ( mc >= h->hpet.first_mc64[tn] && mc < comparator ) + period =0 ;/* skip update */ +else + h->hpet.first_enabled[tn] =0 ; + }if (period) { - elapsed =hpet_read_maincounter (h, guest_time) + - period - comparator; + elapsed = mc + period - comparator; comparator += (elapsed / period) * period; h->hpet.comparator64[tn] = comparator; }
@@ -208,12 +216,18 @@ static void hpet_stop_timer(HPETState *h, unsigned int tn,* 1/(2^10) second, namely, 0.9765625 milliseconds */ #define HPET_TINY_TIME_SPAN ((h->stime_freq >> 10) / STIME_PER_HPET_TICK) +static void hpet_time_fired (struct vcpu *v,void *priv) +{ +bool_t *first_enabled_p = (bool_t *)priv; + + *first_enabled_p =0 ; +} +static void hpet_set_timer (HPETState *h,unsigned int tn,uint64_t guest_time) {uint64_t tn_cmp, cur_tick, diff;unsigned int irq; -unsigned int oneshot;ASSERT (tn <HPET_TIMER_NUM );ASSERT (spin_is_locked (&h->lock));
@@ -262,15 +276,34 @@ static void hpet_set_timer(HPETState *h, unsigned int tn,* have elapsed between the time the comparator was written and the timer * being enabled (now). */ - oneshot = !timer_is_periodic (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 : -hpet_tick_to_ns (h, h->hpet.period[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 ); + +if ( !timer_is_periodic (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 (0LL )); +create_periodic_time (vhpet_vcpu (h), &h->pt[tn], +hpet_tick_to_ns (h, diff), +0 , irq,NULL ,NULL ); + } +else + { +TRACE_2_LONG_4D (TRC_HVM_EMUL_HPET_START_TIMER , tn, irq, +TRC_PAR_LONG (hpet_tick_to_ns (h, diff)), +TRC_PAR_LONG ( +hpet_tick_to_ns (h, h->hpet.period[tn]))); +if ( h->hpet.first_enabled[tn] ) +create_periodic_time (vhpet_vcpu (h), &h->pt[tn], +hpet_tick_to_ns (h, diff), +hpet_tick_to_ns (h, h->hpet.period[tn]), + irq, hpet_time_fired, + &h->hpet.first_enabled[tn]); +else +create_periodic_time (vhpet_vcpu (h), &h->pt[tn], +hpet_tick_to_ns (h, diff), +hpet_tick_to_ns (h, h->hpet.period[tn]), + irq,NULL ,NULL ); + } } static inline uint64_t hpet_fixup_reg(
@@ -343,6 +376,15 @@ static int hpet_write(case HPET_COUNTER : h->hpet.mc64 = new_val; +for ( i =0 ; i <HPET_TIMER_NUM ; i++ ) +if (timer_enabled (h, i) ) + { +if (timer_is_periodic (h, i) && h->hpet.period[i] ) + { + h->hpet.first_mc64[i] = new_val; + h->hpet.first_enabled[i] =1 ; + } + }if (hpet_enabled (h) ) { gdprintk(XENLOG_WARNING,
@@ -416,6 +458,8 @@ static int hpet_write(if ( new_val > max_period ) new_val = max_period; h->hpet.period[tn] = new_val; + h->hpet.first_mc64[tn] =hpet_read_maincounter (h, guest_time); + h->hpet.first_enabled[tn] =1 ; }else {
@@ -435,6 +479,11 @@ static int hpet_write(if (timer_is_32bit (h, tn) ) new_val = (uint32_t )new_val; h->hpet.timers[tn].cmp = new_val; +if (timer_is_periodic (h, tn) && h->hpet.period[tn] ) + { + h->hpet.first_mc64[tn] =hpet_read_maincounter (h, guest_time); + h->hpet.first_enabled[tn] =1 ; + } }if (hpet_enabled (h) &&timer_enabled (h, tn) )set_restart_timer (tn);
@@ -583,6 +632,9 @@ static int hpet_load(struct domain *d, hvm_domain_context_t *h)if (timer_is_32bit (hp, i) ) cmp = (uint32_t )cmp; hp->hpet.timers[i].cmp = cmp; +/* Init hidden regs also */ + hp->hpet.first_mc64[i] =0 ; + hp->hpet.first_enabled[i] =0 ; }#undef C
@@ -595,7 +647,14 @@ static int hpet_load(struct domain *d, hvm_domain_context_t *h)if (hpet_enabled (hp) )for ( i =0 ; i <HPET_TIMER_NUM ; i++ )if (timer_enabled (hp, i) ) + { +if (timer_is_periodic (hp, i) && hp->hpet.period[i] ) + { + hp->hpet.first_mc64[i] = hp->hpet.mc64; + hp->hpet.first_enabled[i] =1 ; + }hpet_set_timer (hp, i, guest_time); + }spin_unlock (&hp->lock);
xen/include/asm-x86/hvm/vpt.h +2 -0
@@ -87,6 +87,8 @@ struct hpet_registers {/* Hidden register state */ uint64_t period[HPET_TIMER_NUM ];/* Last value written to comparator */ uint64_t comparator64[HPET_TIMER_NUM ];/* 64 bit running comparator */ +uint64_t first_mc64[HPET_TIMER_NUM ];/* 1st interval main counter */ +bool_t first_enabled[HPET_TIMER_NUM ];/* In 1st interval */ }; typedef struct HPETState {
Signature · 1 line
1.8.4
Re: [PATCH v3 11/11] hvm/hpet: handle 1st period special
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[email protected]> wrote:
v3:
Better commit message.
More setting offirst_mc64&first_enabledwhen needed. Switch tobool_t.xen/arch/x86/hvm/hpet.c | 83 ++++++++++++++++++++++++++++++++++++------- xen/include/asm-x86/hvm/vpt.h | 2 ++ 2 files changed, 73 insertions(+), 12 deletions(-)
I still can't help myself thinking that this must be achievable with less special casing - I just can't imagine that hardware also has this huge an amount of special case logic just for the first period.
xen/arch/x86/hvm/hpet.c static void hpet_stop_timer(HPETState *h, unsigned int tn, · line 221
Quoted 7 lines
xen/arch/x86/hvm/hpet.c +3
-0@@ -208,12 +216,18 @@ static void hpet_stop_timer(HPETState *h, unsigned int tn,* 1/(2^10) second, namely, 0.9765625 milliseconds */ #define HPET_TINY_TIME_SPAN ((h->stime_freq >> 10) / STIME_PER_HPET_TICK) +static void hpet_time_fired (struct vcpu *v,void *priv) +{ +bool_t *first_enabled_p = (bool_t *)priv;
Pointless cast.
Jan
Re: [PATCH v3 11/11] hvm/hpet: handle 1st period special
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[email protected]> wrote:
v3:
Better commit message.
More setting offirst_mc64&first_enabledwhen needed. Switch tobool_t.
Quoted 6 lines
Quoted 3 lines
xen/arch/x86/hvm/hpet.c | 83 ++++++++++++++++++++++++++++++++++++------- xen/include/asm-x86/hvm/vpt.h | 2 ++ 2 files changed, 73 insertions(+), 12 deletions(-)I still can't help myself thinking that this must be achievable with less special casing - I just can't imagine that hardware also has this huge an amount of special case logic just for the first period.
The hardware is simple. And the code to simulate it:
do_count()
{
mc++;
if ( mc == comparator )
handle_int();
if ( periodic )
comparator += period;
}
}is also simple. However this way has a very high cost in that you have to call do_count at the frequency of the master clock. Instead of the simple simulation, the current code uses arithmetic to compute the current state of mc and comparator. This is where the complexity comes in. During answering of comments on #8 (signed divide) and #10 (prevent mc equal), I noticed that what I coded here only works for expected case where mc is close to zero and comparator starts out > mc in unsigned. I will be sending a v4 with this issue fixed. New code (untested):
+ if ( h->hpet.first_enabled[tn] )
+ {
+ int64_t mc_diff = mc - h->hpet.first_mc64[tn];
+
+ if ( mc_diff >= 0 && mc_diff < comparator - h->hpet.first_mc64[tn] )
was:
+ if ( mc >= h->hpet.first_mc64[tn] && mc < comparator )
Quoted 8 lines
xen/arch/x86/hvm/hpet.c static void hpet_stop_timer(HPETState *h, unsigned int tn, · line 221
Quoted 7 lines
xen/arch/x86/hvm/hpet.c +3
-0@@ -208,12 +216,18 @@ static void hpet_stop_timer(HPETState *h, unsigned int tn,* 1/(2^10) second, namely, 0.9765625 milliseconds */ #define HPET_TINY_TIME_SPAN ((h->stime_freq >> 10) / STIME_PER_HPET_TICK) +static void hpet_time_fired (struct vcpu *v,void *priv) +{ +bool_t *first_enabled_p = (bool_t *)priv;Pointless cast.
Will drop.
-Don Slutz
Quoted 2 lines
Jan
Re: [PATCH v3 11/11] hvm/hpet: handle 1st period special
In reply to Jan BeulichQuoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[email protected]> wrote:
v3:
Better commit message.
More setting offirst_mc64&first_enabledwhen needed. Switch tobool_t.
Quoted 7 lines
Quoted 3 lines
xen/arch/x86/hvm/hpet.c | 83 ++++++++++++++++++++++++++++++++++++------- xen/include/asm-x86/hvm/vpt.h | 2 ++ 2 files changed, 73 insertions(+), 12 deletions(-)I still can't help myself thinking that this must be achievable with less special casing - I just can't imagine that hardware also has this huge an amount of special case logic just for the first period.
+1. I didn't follow the explanation on v1 very clearly, so perhaps you can correct my understanding, but I _think_ the issue you're describing is:
when we read the comparator, we try to wind it forward from its current value towards the main counter value, by adding as many periods as will fit.
If the guest sets the comparator >1 period away, we'll incorrectly warp the comparator to some foolish value as soon as we read it.
So, you're adding mechanism to detect the first interrupt after a comparator set (or when the main counter is being started, or on hpet load -- both of those rely on 'first_enabled' being essentialy harmless if set when it's _not_ the first interrupt, which suggests that maybe that's not the best name for the flag).
Couldn't it be fixed more simply by detecting comparator values in the past in hpet_get_comparator()?
Tim.
Re: [PATCH v3 11/11] hvm/hpet: handle 1st period special
Quoted 13 lines · Tim Deegan
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 17.04.14 at 19:43, <[email protected]> wrote:
v3:
Better commit message.
More setting offirst_mc64&first_enabledwhen needed. Switch tobool_t.Quoted 6 lines
Quoted 3 lines
xen/arch/x86/hvm/hpet.c | 83 ++++++++++++++++++++++++++++++++++++------- xen/include/asm-x86/hvm/vpt.h | 2 ++ 2 files changed, 73 insertions(+), 12 deletions(-)I still can't help myself thinking that this must be achievable with less special casing - I just can't imagine that hardware also has this huge an amount of special case logic just for the first period.
Quoted 7 lines
+1. I didn't follow the explanation on v1 very clearly, so perhaps you can correct my understanding, but I _think_ the issue you're describing is:
when we read the comparator, we try to wind it forward from its current value towards the main counter value, by adding as many periods as will fit.
Yes.
Quoted 2 lines
If the guest sets the comparator >1 period away, we'll incorrectly warp the comparator to some foolish value as soon as we read it.
Yes. The "foolish value" falls within the bounds of a period.
Quoted 5 lines
So, you're adding mechanism to detect the first interrupt after a comparator set (or when the main counter is being started, or on hpet load -- both of those rely on '
first_enabled' being essentialy harmless if set when it's _not_ the first interrupt, which suggests that maybe that's not the best name for the flag).
It is not harmless if it is set when _not_ in the first interval (before first interrupt). Since it is used by hpet_save during migration,
the correct values need to be saved. I do not care about the
name.
You are missing the case when the guest sets the main counter.
Quoted 2 lines
Couldn't it be fixed more simply by detecting comparator values in the past in
hpet_get_comparator()?
This statement only works using 64-bit arithmetic for the main counter never
63
changing by more then 2 . (Which is a boundary case that should not
happen in my life time.)
Without patch #11 (this patch) and without patch #8 (Use signed divide...) and adding:
commit 8c450bed530b13c3f3d18b9dafb3d1b1d69ac1f2
Author: Don Slutz <[email protected]> Date: Thu May 1 14:02:47 2014 -0400
hvm/hpet: Detect comparator values in the past
xen/arch/x86/hvm/hpet.c +6 -4
@@ -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; + } } }
Which I think was what you mean.
It looks to be "ok" (ignoring the boundary case). So should I send it as v4?
-Don Slutz
Quoted 1 line
Tim.
Tim Deegan
Acked-byRe: [PATCH v3 11/11] hvm/hpet: handle 1st period special
Quoted 45 lines · Don Slutz
Quoted 2 lines · Tim Deegan
Couldn't it be fixed more simply by detecting comparator values in the past in
hpet_get_comparator()?This statement only works using 64-bit arithmetic for the main counter never
63changing by more then 2 . (Which is a boundary case that should not
happen in my life time.)Without patch #11 (this patch) and without patch #8 (Use signed divide...) and adding:
commit
8c450bed530b13c3f3d18b9dafb3d1b1d69ac1f2Author: Don Slutz <[email protected]> Date: Thu May 1 14:02:47 2014 -0400hvm/hpet: Detect comparator values in the past
Signed-off-by Don Slutz <[email protected]>xen/arch/x86/hvm/hpet.c +6
-4@@ -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; + } } }
Quoted 4 lines
Which I think was what you mean.
It looks to be "ok" (ignoring the boundary case). So should I send it as v4?
Yes, that looks good. You can add
to it too.
Cheers,
Tim.