hpet: recover timer offset correctly
PATCH 1/1 hpet: recover timer offset correctly
HPET saves its state by calculating the current time and recovers timer offset using this calculated value. But these calculations include divisions and multiplications. Therefore the timer state cannot be recovered precise enough.
This patch introduces saving of the original value of the offset to preserve the determinism of the timer.
--- hw/timer/hpet.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
hw/timer/hpet.c +9 -3
@@ -221,7 +221,9 @@ static int hpet_pre_save(void *opaque)HPETState *s = opaque;/* save current counter value */ - s->hpet_counter =hpet_get_ticks (s); +if (hpet_enabled (s)) { + s->hpet_counter =hpet_get_ticks (s); + }return 0 ; }
@@ -252,7 +254,10 @@ static int hpet_post_load(void *opaque, int version_id)HPETState *s = opaque;/* Recalculate the offset between the main counter and guest time */ - s->hpet_offset =ticks_to_ns (s->hpet_counter) -qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); +if (version_id <=2 ) { + s->hpet_offset =ticks_to_ns (s->hpet_counter) + -qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); + }/* Push number of timers into capability returned via HPET_ID */ s->capability &= ~HPET_ID_NUM_TIM_MASK ;
@@ -303,7 +308,7 @@ static const VMStateDescription vmstate_hpet_timer = {static const VMStateDescription vmstate_hpet = { .name ="hpet" , - .version_id =2 , + .version_id =3 , .minimum_version_id =1 , .pre_save = hpet_pre_save, .pre_load = hpet_pre_load,
@@ -312,6 +317,7 @@ static const VMStateDescription vmstate_hpet = {VMSTATE_UINT64 (config, HPETState),VMSTATE_UINT64 (isr, HPETState),VMSTATE_UINT64 (hpet_counter, HPETState), +VMSTATE_UINT64_V (hpet_offset, HPETState,3 ),VMSTATE_UINT8_V (num_timers, HPETState,2 ),VMSTATE_VALIDATE ("num_timers in range" , hpet_validate_num_timers), VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,
Paolo Bonzini
AppliedQuoted 52 lines · Pavel Dovgalyuk
HPET saves its state by calculating the current time and recovers timer offset using this calculated value. But these calculations include divisions and multiplications. Therefore the timer state cannot be recovered precise enough.
This patch introduces saving of the original value of the offset to preserve the determinism of the timer.Signed-off-by Maria Klimushenkova <[email protected]>Signed-off-by Pavel Dovgalyuk <[email protected]>--- hw/timer/hpet.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)hw/timer/hpet.c +8
-3@@ -221,7 +221,9 @@ static int hpet_pre_save(void *opaque)HPETState *s = opaque;/* save current counter value */ - s->hpet_counter =hpet_get_ticks (s); +if (hpet_enabled (s)) { + s->hpet_counter =hpet_get_ticks (s); + }return 0 ; }@@ -252,7 +254,10 @@ static int hpet_post_load(void *opaque, int version_id)HPETState *s = opaque;/* Recalculate the offset between the main counter and guest time */ - s->hpet_offset =ticks_to_ns (s->hpet_counter) -qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); +if (version_id <=2 ) { + s->hpet_offset =ticks_to_ns (s->hpet_counter) + -qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); + }/* Push number of timers into capability returned via HPET_ID */ s->capability &= ~HPET_ID_NUM_TIM_MASK ;@@ -303,7 +308,7 @@ static const VMStateDescription vmstate_hpet_timer = {static const VMStateDescription vmstate_hpet = { .name ="hpet" , - .version_id =2 , + .version_id =3 , .minimum_version_id =1 , .pre_save = hpet_pre_save, .pre_load = hpet_pre_load,
hw/timer/hpet.c static const VMStateDescription vmstate_hpet = { · line 323
Quoted 7 lines
hw/timer/hpet.c +1
-0@@ -312,6 +317,7 @@ static const VMStateDescription vmstate_hpet = {VMSTATE_UINT64 (config, HPETState),VMSTATE_UINT64 (isr, HPETState),VMSTATE_UINT64 (hpet_counter, HPETState), +VMSTATE_UINT64_V (hpet_offset, HPETState,3 ),VMSTATE_UINT8_V (num_timers, HPETState,2 ),VMSTATE_VALIDATE ("num_timers in range" , hpet_validate_num_timers), VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,
Queued, thanks.
However, note that Maria should have been the author in the patch. I've fixed that.
Paolo
Quoted 6 lines · * Pavel Dovgalyuk ([email protected]) wrote:
HPET saves its state by calculating the current time and recovers timer offset using this calculated value. But these calculations include divisions and multiplications. Therefore the timer state cannot be recovered precise enough.
This patch introduces saving of the original value of the offset to preserve the determinism of the timer.
Please put the extra data in a subsection and tie the subsection to a property enabled by the machine type. That avoids breaking backwards migration compatibility.
Dave
Quoted 45 lines
Signed-off-by Maria Klimushenkova <[email protected]>Signed-off-by Pavel Dovgalyuk <[email protected]>--- hw/timer/hpet.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)hw/timer/hpet.c +8
-3@@ -221,7 +221,9 @@ static int hpet_pre_save(void *opaque)HPETState *s = opaque;/* save current counter value */ - s->hpet_counter =hpet_get_ticks (s); +if (hpet_enabled (s)) { + s->hpet_counter =hpet_get_ticks (s); + }return 0 ; }@@ -252,7 +254,10 @@ static int hpet_post_load(void *opaque, int version_id)HPETState *s = opaque;/* Recalculate the offset between the main counter and guest time */ - s->hpet_offset =ticks_to_ns (s->hpet_counter) -qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); +if (version_id <=2 ) { + s->hpet_offset =ticks_to_ns (s->hpet_counter) + -qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); + }/* Push number of timers into capability returned via HPET_ID */ s->capability &= ~HPET_ID_NUM_TIM_MASK ;@@ -303,7 +308,7 @@ static const VMStateDescription vmstate_hpet_timer = {static const VMStateDescription vmstate_hpet = { .name ="hpet" , - .version_id =2 , + .version_id =3 , .minimum_version_id =1 , .pre_save = hpet_pre_save, .pre_load = hpet_pre_load,
hw/timer/hpet.c static const VMStateDescription vmstate_hpet = { · line 323
Quoted 7 lines
hw/timer/hpet.c +1
-0@@ -312,6 +317,7 @@ static const VMStateDescription vmstate_hpet = {VMSTATE_UINT64 (config, HPETState),VMSTATE_UINT64 (isr, HPETState),VMSTATE_UINT64 (hpet_counter, HPETState), +VMSTATE_UINT64_V (hpet_offset, HPETState,3 ),VMSTATE_UINT8_V (num_timers, HPETState,2 ),VMSTATE_VALIDATE ("num_timers in range" , hpet_validate_num_timers), VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 9 lines
From: Dr. David Alan Gilbert [mailto:[email protected]]
Quoted 6 lines · * Pavel Dovgalyuk ([email protected]) wrote:
HPET saves its state by calculating the current time and recovers timer offset using this calculated value. But these calculations include divisions and multiplications. Therefore the timer state cannot be recovered precise enough.
This patch introduces saving of the original value of the offset to preserve the determinism of the timer.
Quoted 3 lines
Please put the extra data in a subsection and tie the subsection to a property enabled by the machine type. That avoids breaking backwards migration compatibility.
I thought about it and haven't found how to preserve the backward migration compatibility without breaking everything else.
Loading old (or compatible) snapshot without changing the version will corrupt the offset in post_load.
Pavel Dovgalyuk
Quoted 55 lines
Quoted 46 lines
Signed-off-by Maria Klimushenkova <[email protected]>Signed-off-by Pavel Dovgalyuk <[email protected]>--- hw/timer/hpet.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)hw/timer/hpet.c +8
-3@@ -221,7 +221,9 @@ static int hpet_pre_save(void *opaque)HPETState *s = opaque;/* save current counter value */ - s->hpet_counter =hpet_get_ticks (s); +if (hpet_enabled (s)) { + s->hpet_counter =hpet_get_ticks (s); + }return 0 ; }@@ -252,7 +254,10 @@ static int hpet_post_load(void *opaque, int version_id)HPETState *s = opaque;/* Recalculate the offset between the main counter and guest time */ - s->hpet_offset =ticks_to_ns (s->hpet_counter) -qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); +if (version_id <=2 ) { + s->hpet_offset =ticks_to_ns (s->hpet_counter) + -qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); + }/* Push number of timers into capability returned via HPET_ID */ s->capability &= ~HPET_ID_NUM_TIM_MASK ;@@ -303,7 +308,7 @@ static const VMStateDescription vmstate_hpet_timer = {static const VMStateDescription vmstate_hpet = { .name ="hpet" , - .version_id =2 , + .version_id =3 , .minimum_version_id =1 , .pre_save = hpet_pre_save, .pre_load = hpet_pre_load,hw/timer/hpet.c static const VMStateDescription vmstate_hpet = { · line 323
Quoted 7 lines
hw/timer/hpet.c +1
-0@@ -312,6 +317,7 @@ static const VMStateDescription vmstate_hpet = {VMSTATE_UINT64 (config, HPETState),VMSTATE_UINT64 (isr, HPETState),VMSTATE_UINT64 (hpet_counter, HPETState), +VMSTATE_UINT64_V (hpet_offset, HPETState,3 ),VMSTATE_UINT8_V (num_timers, HPETState,2 ),VMSTATE_VALIDATE ("num_timers in range" , hpet_validate_num_timers), VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 9 lines · * Pavel Dovgalyuk ([email protected]) wrote:
Quoted 9 lines
From: Dr. David Alan Gilbert [mailto:[email protected]]
Quoted 6 lines · * Pavel Dovgalyuk ([email protected]) wrote:
HPET saves its state by calculating the current time and recovers timer offset using this calculated value. But these calculations include divisions and multiplications. Therefore the timer state cannot be recovered precise enough.
This patch introduces saving of the original value of the offset to preserve the determinism of the timer.
Quoted 8 lines
Quoted 3 lines
Please put the extra data in a subsection and tie the subsection to a property enabled by the machine type. That avoids breaking backwards migration compatibility.
I thought about it and haven't found how to preserve the backward migration compatibility without breaking everything else.
Loading old (or compatible) snapshot without changing the version will corrupt the offset inpost_load.
There are lots of ways; for example you could initialise hpet_offset to a dummy value in a pre-load and spot that in the post-load instead of the version_id, or set a flag in a post-load on the subsection.
Dave
Quoted 57 lines
Pavel Dovgalyuk
Quoted 55 lines
Quoted 46 lines
Signed-off-by Maria Klimushenkova <[email protected]>Signed-off-by Pavel Dovgalyuk <[email protected]>--- hw/timer/hpet.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)hw/timer/hpet.c +8
-3@@ -221,7 +221,9 @@ static int hpet_pre_save(void *opaque)HPETState *s = opaque;/* save current counter value */ - s->hpet_counter =hpet_get_ticks (s); +if (hpet_enabled (s)) { + s->hpet_counter =hpet_get_ticks (s); + }return 0 ; }@@ -252,7 +254,10 @@ static int hpet_post_load(void *opaque, int version_id)HPETState *s = opaque;/* Recalculate the offset between the main counter and guest time */ - s->hpet_offset =ticks_to_ns (s->hpet_counter) -qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); +if (version_id <=2 ) { + s->hpet_offset =ticks_to_ns (s->hpet_counter) + -qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); + }/* Push number of timers into capability returned via HPET_ID */ s->capability &= ~HPET_ID_NUM_TIM_MASK ;@@ -303,7 +308,7 @@ static const VMStateDescription vmstate_hpet_timer = {static const VMStateDescription vmstate_hpet = { .name ="hpet" , - .version_id =2 , + .version_id =3 , .minimum_version_id =1 , .pre_save = hpet_pre_save, .pre_load = hpet_pre_load,hw/timer/hpet.c static const VMStateDescription vmstate_hpet = { · line 323
Quoted 7 lines
hw/timer/hpet.c +1
-0@@ -312,6 +317,7 @@ static const VMStateDescription vmstate_hpet = {VMSTATE_UINT64 (config, HPETState),VMSTATE_UINT64 (isr, HPETState),VMSTATE_UINT64 (hpet_counter, HPETState), +VMSTATE_UINT64_V (hpet_offset, HPETState,3 ),VMSTATE_UINT8_V (num_timers, HPETState,2 ),VMSTATE_VALIDATE ("num_timers in range" , hpet_validate_num_timers), VMSTATE_STRUCT_VARRAY_UINT8(timer, HPETState, num_timers, 0,--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Quoted 16 lines · * Pavel Dovgalyuk ([email protected]) wrote:
Quoted 13 lines
Quoted 12 lines
From: Dr. David Alan Gilbert [mailto:[email protected]]
Quoted 6 lines · * Pavel Dovgalyuk ([email protected]) wrote:
HPET saves its state by calculating the current time and recovers timer offset using this calculated value. But these calculations include divisions and multiplications. Therefore the timer state cannot be recovered precise enough.
This patch introduces saving of the original value of the offset to preserve the determinism of the timer.Please put the extra data in a subsection and tie the subsection to a property enabled by the machine type. That avoids breaking backwards migration compatibility.
Quoted 8 lines
Quoted 4 lines
I thought about it and haven't found how to preserve the backward migration compatibility without breaking everything else.
Loading old (or compatible) snapshot without changing the version will corrupt the offset inpost_load.There are lots of ways; for example you could initialise
hpet_offsetto a dummy value in a pre-load and spot that in the post-load instead of theversion_id, or set a flag in a post-load on the subsection.
Thanks. Please check the second version.
Pavel Dovgalyuk