From patchwork Fri Jan 12 12:24:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 859854 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="fgpEkHeN"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zJ2Hs0gZJz9s7s for ; Fri, 12 Jan 2018 23:34:31 +1100 (AEDT) Received: from localhost ([::1]:38362 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZyXU-0004dp-Tf for incoming@patchwork.ozlabs.org; Fri, 12 Jan 2018 07:34:28 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33469) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eZyNg-0004vI-2Y for qemu-devel@nongnu.org; Fri, 12 Jan 2018 07:24:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eZyNf-00009D-1p for qemu-devel@nongnu.org; Fri, 12 Jan 2018 07:24:20 -0500 Received: from mail-wr0-x242.google.com ([2a00:1450:400c:c0c::242]:35240) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eZyNe-00008U-SR for qemu-devel@nongnu.org; Fri, 12 Jan 2018 07:24:19 -0500 Received: by mail-wr0-x242.google.com with SMTP id g38so1967535wrd.2 for ; Fri, 12 Jan 2018 04:24:18 -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=fgpEkHeNs7WN9obl0FjUiTjF1aY6X+uXi8jkta4qXG9R7BzMy2iKb4e6ock0pTc8UZ 33DgPg6F+ao9KbPBMYSvjtm/kW1UNVh7VEEW2RqTdqtOfPJe+/SlWdXP/rsBsan/nYkT llA6MET4yyNEQjcUo+h2qzb1W89V2RA8QNCRjFCjKwQGu+KTUn5aMFJipPbkrrs5O2h4 0synDzk4ujvPbofyfEg3a7oEk4dFd53ET3r/IPWAqMu5t6aznKnbu2vmZEeIsOgnCDPm Wq6HWWtLOiShqE/wm++yiBQu1YnQaiLmmxScZj/z0sNRglz1da1L+ed70Sf6CnoJOjYc UVUQ== 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=AYTC4idS8eMhZ1jjgb3TaeePGRtVFwMeRKPILO3XAJ2B5tic3TqqQL/T64se9lUJ7r 5dOuefSK68ICKxrsQz90bd38wZz59EnT3fDBOrorPTb/gxDV7EeWWu8yeouQjayoe2+E 0I4U+3f5yjE+6yiir4G5ir3TZIi8zoKSN7prifMKwSyhxYVN1vuf344JvxIWE2LSjscI v+5/ehZ4X/HpeDthq8Az5+6AYQi/FEHqosBhyKS53UPC+Ge56Zv2OwaUhkt/hwPDOujJ ZSl5Z+YcRLqs3gi4VleaFqWcDzFaJmzZ4ruez2bHqioGb2hdv1b43Y8kRS9jlIFP0/2y R8Ig== X-Gm-Message-State: AKGB3mKDbSaHKtPvFbGiJYOG45vRAW9qD2pq+gfIrJEaKDkYrmfbiPjB 20iS9BI+HQkyOX8Gb41BQWHjY5ZR X-Google-Smtp-Source: ACJfBosedpICV5l2oxZWrG6Q/TVAn+NxZIxYEDnFa1BwonxNhyNSjvVta8IzFK+bSu6rkTiIj+Jp+g== X-Received: by 10.223.172.199 with SMTP id o65mr15941340wrc.215.1515759857554; Fri, 12 Jan 2018 04:24:17 -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 w195sm1740188wmw.46.2018.01.12.04.24.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 04:24:16 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 12 Jan 2018 13:24:13 +0100 Message-Id: <1515759853-20360-2-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1515759853-20360-1-git-send-email-pbonzini@redhat.com> References: <1515759853-20360-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/52] 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 { 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 = 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 +255,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 (!s->hpet_offset_saved) { + 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; @@ -267,6 +273,13 @@ static int hpet_post_load(void *opaque, int version_id) return 0; } +static bool hpet_offset_needed(void *opaque) +{ + HPETState *s = opaque; + + return hpet_enabled(s) && s->hpet_offset_saved; +} + static bool hpet_rtc_irq_level_needed(void *opaque) { HPETState *s = opaque; @@ -285,6 +298,17 @@ static const VMStateDescription vmstate_hpet_rtc_irq_level = { } }; +static const VMStateDescription vmstate_hpet_offset = { + .name = "hpet/offset", + .version_id = 1, + .minimum_version_id = 1, + .needed = hpet_offset_needed, + .fields = (VMStateField[]) { + VMSTATE_UINT64(hpet_offset, HPETState), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_hpet_timer = { .name = "hpet_timer", .version_id = 1, @@ -320,6 +344,7 @@ static const VMStateDescription vmstate_hpet = { }, .subsections = (const VMStateDescription*[]) { &vmstate_hpet_rtc_irq_level, + &vmstate_hpet_offset, NULL } }; @@ -762,6 +787,7 @@ static Property hpet_device_properties[] = { 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, true), DEFINE_PROP_END_OF_LIST(), }; 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 #define HW_COMPAT_2_11 \ - /* empty */ + {\ + .driver = "hpet",\ + .property = "hpet-offset-saved",\ + .value = "false",\ + }, #define HW_COMPAT_2_10 \ {\