From nobody Tue Oct 28 02:08:43 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; dkim=fail; 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 1516113176654245.6680648446503; Tue, 16 Jan 2018 06:32:56 -0800 (PST) Received: from localhost ([::1]:39196 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebSIJ-0006fX-OK for importer@patchew.org; Tue, 16 Jan 2018 09:32:55 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50090) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ebS3i-0002cZ-L1 for qemu-devel@nongnu.org; Tue, 16 Jan 2018 09:17:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ebS3d-0004RF-KG for qemu-devel@nongnu.org; Tue, 16 Jan 2018 09:17:50 -0500 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]:34575) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ebS3d-0004Qh-DV for qemu-devel@nongnu.org; Tue, 16 Jan 2018 09:17:45 -0500 Received: by mail-wr0-x242.google.com with SMTP id 36so15365583wrh.1 for ; Tue, 16 Jan 2018 06:17:45 -0800 (PST) Received: from 640k.lan (dynamic-adsl-78-12-229-84.clienti.tiscali.it. [78.12.229.84]) by smtp.gmail.com with ESMTPSA id u10sm1537758wrg.6.2018.01.16.06.17.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Jan 2018 06:17:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=bmG7iYeTKWh5qC/+uc4rcvxPtTZqkBEflhjbSu7hadI=; b=Z1qaq2/1oNtymowj9S99E12cXPBs8zLhsGH3fDTl7khO3R8z/WMq8kqyGhF9TepCuY tncg4Zuhg52fBukGzXeX7PgiRjSVijMsxFCWAgzDf3feF4GW5oAFoMNYlJsyj4P4zdst NKfdTuJkStPnh8YMkbduh+JKW11ByOV49WNm39jXymr/XnM0aSPTj4Fb1QJ36+qNC6cR K6HgloOBHVvvXoOfTZ6KdV/hcIEc5OrDlu+tDA4dm3/hfEnyrGgFohwK68niy2/x6oHJ IaMuwqXooQ/IRdEjK1m9UUGx+4w+Bbn+bbgVqB00SxIFsCQSuBZfwHppZZ6Q+cr6XjvU B7cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=bmG7iYeTKWh5qC/+uc4rcvxPtTZqkBEflhjbSu7hadI=; b=Zdt+7/4LeV3fg6y1Gyw1XuwTaDH2F17Ac8k79v/JB37gYiFicTW42CYaMnYQICbh9I hyK8Q7oLseIoC80pkMFusILGXcgVyzNxDuBsEeCEJID/da/0MX+2BEqs4QZLw53yIr8U P4+VP7DdJflXJqctqIMmWN8F0RGkUabM/H8CkLPKU7m58a7NvIa5uSWO1TQLH3d1a1zF PtAvs8OoACActcDgMk3wbZs+RNpbVm7kTGCDtYDW3ZKLvTQlTUeq8966iiFS3umhSToC CJSM0efGhWXA/96h+YJDNCD+mQpMouYgnTgywx73DcDbtWXrL4n8SbThYqJJr715pvp9 R/ZQ== X-Gm-Message-State: AKwxytfS5/9qtCt6kk1B8IGskB05Fo1yVwjH4IowOadeFewpNAKnc96N K+m9kBW3Qwgm8HlK/8Jv4vDF3wfJ X-Google-Smtp-Source: ACJfBouDfhiu4JnwIcZFHu62wDkAlAmDvPrEcT/AjWFfqj3s5NZsxyeEGaHLrOt9FPHluy6bRUEZwA== X-Received: by 10.223.139.221 with SMTP id w29mr5254783wra.223.1516112263989; Tue, 16 Jan 2018 06:17:43 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 16 Jan 2018 15:16:46 +0100 Message-Id: <1516112253-14480-5-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1516112253-14480-1-git-send-email-pbonzini@redhat.com> References: <1516112253-14480-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::242 Subject: [Qemu-devel] [PULL 04/51] 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: Maria Klimushenkova , Pavel Dovgalyuk Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: 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: Pavel Dovgalyuk Signed-off-by: Maria Klimushenkova Reviewed-by: Juan Quintela -- v3: Added compat property for correct migration. Signed-off-by: Paolo Bonzini --- 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 \ {\ --=20 1.8.3.1