From nobody Tue Oct 28 01:48:39 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 151565922781117.770297416872836; Thu, 11 Jan 2018 00:27:07 -0800 (PST) Received: from localhost ([::1]:52947 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZYCW-0000cu-OB for importer@patchew.org; Thu, 11 Jan 2018 03:27:04 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:45553) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZYAa-0007t5-4q for qemu-devel@nongnu.org; Thu, 11 Jan 2018 03:25:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eZYAT-00009W-Sq for qemu-devel@nongnu.org; Thu, 11 Jan 2018 03:25:04 -0500 Received: from mail.ispras.ru ([83.149.199.45]:41446) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZYAT-00009F-Fd for qemu-devel@nongnu.org; Thu, 11 Jan 2018 03:24:57 -0500 Received: from [127.0.1.1] (unknown [85.142.117.226]) by mail.ispras.ru (Postfix) with ESMTPSA id A3BB654006B; Thu, 11 Jan 2018 11:24:56 +0300 (MSK) From: Pavel Dovgalyuk To: qemu-devel@nongnu.org Date: Thu, 11 Jan 2018 11:24:58 +0300 Message-ID: <20180111082458.27295.43885.stgit@pasha-VirtualBox> In-Reply-To: <20180111082452.27295.85707.stgit@pasha-VirtualBox> References: <20180111082452.27295.85707.stgit@pasha-VirtualBox> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 83.149.199.45 Subject: [Qemu-devel] [RFC PATCH v3 01/30] hpet: recover timer offset correctly X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, peter.maydell@linaro.org, pavel.dovgaluk@ispras.ru, mst@redhat.com, jasowang@redhat.com, quintela@redhat.com, zuban32s@gmail.com, maria.klimushenkova@ispras.ru, dovgaluk@ispras.ru, kraxel@redhat.com, boost.lists@gmail.com, pbonzini@redhat.com, alex.bennee@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 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: Pavel Dovgalyuk Signed-off-by: Maria Klimushenkova Reviewed-by: Juan Quintela -- v3: Added compat property for correct migration. --- hw/timer/hpet.c | 30 ++++++++++++++++++++++++++++-- include/hw/compat.h | 6 +++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/hw/timer/hpet.c b/hw/timer/hpet.c index 577371b..d97436b 100644 --- a/hw/timer/hpet.c +++ b/hw/timer/hpet.c @@ -70,6 +70,7 @@ typedef struct HPETState { =20 MemoryRegion iomem; uint64_t hpet_offset; + bool hpet_offset_saved; qemu_irq irqs[HPET_NUM_IRQ_ROUTES]; uint32_t flags; uint8_t rtc_irq_level; @@ -221,7 +222,9 @@ static int hpet_pre_save(void *opaque) HPETState *s =3D opaque; =20 /* save current counter value */ - s->hpet_counter =3D hpet_get_ticks(s); + if (hpet_enabled(s)) { + s->hpet_counter =3D hpet_get_ticks(s); + } =20 return 0; } @@ -252,7 +255,10 @@ static int hpet_post_load(void *opaque, int version_id) HPETState *s =3D opaque; =20 /* Recalculate the offset between the main counter and guest time */ - s->hpet_offset =3D ticks_to_ns(s->hpet_counter) - qemu_clock_get_ns(QE= MU_CLOCK_VIRTUAL); + if (!s->hpet_offset_saved) { + s->hpet_offset =3D ticks_to_ns(s->hpet_counter) + - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + } =20 /* Push number of timers into capability returned via HPET_ID */ s->capability &=3D ~HPET_ID_NUM_TIM_MASK; @@ -267,6 +273,13 @@ static int hpet_post_load(void *opaque, int version_id) return 0; } =20 +static bool hpet_offset_needed(void *opaque) +{ + HPETState *s =3D opaque; + + return hpet_enabled(s) && s->hpet_offset_saved; +} + static bool hpet_rtc_irq_level_needed(void *opaque) { HPETState *s =3D opaque; @@ -285,6 +298,17 @@ static const VMStateDescription vmstate_hpet_rtc_irq_l= evel =3D { } }; =20 +static const VMStateDescription vmstate_hpet_offset =3D { + .name =3D "hpet/offset", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D hpet_offset_needed, + .fields =3D (VMStateField[]) { + VMSTATE_UINT64(hpet_offset, HPETState), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_hpet_timer =3D { .name =3D "hpet_timer", .version_id =3D 1, @@ -320,6 +344,7 @@ static const VMStateDescription vmstate_hpet =3D { }, .subsections =3D (const VMStateDescription*[]) { &vmstate_hpet_rtc_irq_level, + &vmstate_hpet_offset, NULL } }; @@ -762,6 +787,7 @@ static Property hpet_device_properties[] =3D { DEFINE_PROP_UINT8("timers", HPETState, num_timers, HPET_MIN_TIMERS), DEFINE_PROP_BIT("msi", HPETState, flags, HPET_MSI_SUPPORT, false), DEFINE_PROP_UINT32(HPET_INTCAP, HPETState, intcap, 0), + DEFINE_PROP_BOOL("hpet-offset-saved", HPETState, hpet_offset_saved, tr= ue), DEFINE_PROP_END_OF_LIST(), }; =20 diff --git a/include/hw/compat.h b/include/hw/compat.h index 263de97..7f31850 100644 --- a/include/hw/compat.h +++ b/include/hw/compat.h @@ -2,7 +2,11 @@ #define HW_COMPAT_H =20 #define HW_COMPAT_2_11 \ - /* empty */ + {\ + .driver =3D "hpet",\ + .property =3D "hpet-offset-saved",\ + .value =3D "false",\ + }, =20 #define HW_COMPAT_2_10 \ {\