From patchwork Wed Oct 17 12:23:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Artem Pisarenko X-Patchwork-Id: 10645395 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C2772109C for ; Wed, 17 Oct 2018 12:26:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B47A22AED9 for ; Wed, 17 Oct 2018 12:26:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A2F6F2AED8; Wed, 17 Oct 2018 12:26:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 200552AED8 for ; Wed, 17 Oct 2018 12:26:19 +0000 (UTC) Received: from localhost ([::1]:36337 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gCku2-0003iR-AF for patchwork-qemu-devel@patchwork.kernel.org; Wed, 17 Oct 2018 08:26:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41344) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gCksf-0002bI-NF for qemu-devel@nongnu.org; Wed, 17 Oct 2018 08:24:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gCkse-00078Y-O9 for qemu-devel@nongnu.org; Wed, 17 Oct 2018 08:24:53 -0400 Received: from mail-lj1-x236.google.com ([2a00:1450:4864:20::236]:42577) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gCkse-00077Y-GO for qemu-devel@nongnu.org; Wed, 17 Oct 2018 08:24:52 -0400 Received: by mail-lj1-x236.google.com with SMTP id y71-v6so24116357lje.9 for ; Wed, 17 Oct 2018 05:24:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:in-reply-to:references:content-transfer-encoding; bh=MCM1zBAgDKGmY6C66wTdYlaeKrnybPqhU4VHrj7g+j8=; b=i6ld8l9zPV6uuplBj1tubWcX8biZlt85tfGSS7qnVmEb8odOdFdVmuoSKnEj3MPIIx Aojm/9tE/j38ZadxBi9XFta7bQq4Ed3c26fjYrmcc5pm8XoQ72gnzzi8nxaG7RQbKu8c wrM3AW4ISA1AvHHk8eME4yE5ztpACMAuVAbdvldUCsKQTELWxVTQp8X/HgcwPv4ogiOg gMc6HzPldpEI5CHkEKVrqdtGD3y5sR22wJLvB2/o9phAhT44QE77HYGFH58h/2a9z9nb t0dQgKV0U96uFuxiQUQjcxM3l+bKKcwqFNKXwrMXPtsRJlt+sp0+W0Sbrn2y7uR7/pwb bu0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:in-reply-to:references :content-transfer-encoding; bh=MCM1zBAgDKGmY6C66wTdYlaeKrnybPqhU4VHrj7g+j8=; b=oWtOMuPW3klKhyvEVoUhn+nvP3t5TZO0icjo2Dw6cxQNrSRj/nXBT8Wi1k9fxqXy4u hMSwVxqwHC07+EKJ+HHaa0DER0KSRmzLzh1YyRWtPg4XqYK9vexGX9View+0mpvDAwX+ UlGYeJhqUgjfEeG35tIRd7AkE4hTSsRkpYGypiRtp5MJghwbUNLx2htxlf3TTvI+IW+6 S850XRaWXcxUtOnFTrFvdgIorERnd7PIBre2AgelPBbpudH8oLNX5haqll6kfCsLKLnJ pv53Tq3RwinefprXJ9MPT6N6WHZv3afRILnUy00EjuBvEoApT6+d22A2ZnOoQH4Kimht 02Nw== X-Gm-Message-State: ABuFfoinibq1Blnix5ZyoS/s0R16FJ7A2HsVwufW9dv2gaKGNLwQllvR Ttz1cUp6Mdj0Ynb6DiuzzT248ekiWBQ= X-Google-Smtp-Source: ACcGV60Zz2Tr4nofmVTnGjXmePJc6WpE88UqGZpe7iLwtpI8HSDAMUPM6/APsvHKnZ4ipNcZly6G7w== X-Received: by 2002:a2e:6e08:: with SMTP id j8-v6mr18257103ljc.61.1539779090797; Wed, 17 Oct 2018 05:24:50 -0700 (PDT) Received: from localhost.localdomain ([77.221.221.49]) by smtp.gmail.com with ESMTPSA id z67-v6sm3704118lfa.14.2018.10.17.05.24.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 17 Oct 2018 05:24:50 -0700 (PDT) From: Artem Pisarenko To: qemu-devel@nongnu.org Date: Wed, 17 Oct 2018 18:23:02 +0600 Message-Id: <4632b5a4c97dcf4ccdb11ec0c4fe3a26e792ffa6.1539778764.git.artem.k.pisarenko@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: MIME-Version: 1.0 In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::236 Subject: [Qemu-devel] [PATCH v2 3/4] Fixes RTC bug with base datetime shifts in clock=vm 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: Paolo Bonzini , Artem Pisarenko Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This makes all current "-rtc" option parameters combinations produce fixed/unambiguous RTC timedate reference for hardware emulation frontends. It restores determinism of guest execution when used with clock=vm and specified base value. Buglink: https://bugs.launchpad.net/qemu/+bug/1797033 Signed-off-by: Artem Pisarenko --- vl.c | 58 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/vl.c b/vl.c index 10c4275..78a8a68 100644 --- a/vl.c +++ b/vl.c @@ -152,8 +152,10 @@ static enum { RTC_BASE_LOCALTIME, RTC_BASE_DATETIME, } rtc_base_type = RTC_BASE_UTC; -static int rtc_host_datetime_offset = -1; /* valid only for host rtc_clock and - rtc_base_type=RTC_BASE_DATETIME */ +static time_t rtc_ref_start_datetime; +static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */ +static int rtc_host_datetime_offset = -1; /* valid & used only with + RTC_BASE_DATETIME */ QEMUClockType rtc_clock; int vga_interface_type = VGA_NONE; static DisplayOptions dpy; @@ -785,32 +787,42 @@ void qemu_system_vmstop_request(RunState state) } /***********************************************************/ -/* real time host monotonic timer */ - -static time_t qemu_timedate(void) -{ - return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000; -} - -/***********************************************************/ /* RTC reference time/date access */ +static time_t qemu_ref_timedate(void) +{ + time_t value = qemu_clock_get_ms(rtc_clock) / 1000; + switch (rtc_clock) { + case QEMU_CLOCK_REALTIME: + value -= rtc_realtime_clock_offset; + /* no break */ + case QEMU_CLOCK_VIRTUAL: + value += rtc_ref_start_datetime; + break; + case QEMU_CLOCK_HOST: + if (rtc_base_type == RTC_BASE_DATETIME) { + value -= rtc_host_datetime_offset; + } + break; + default: + assert(0); + } + return value; +} + void qemu_get_timedate(struct tm *tm, int offset) { - time_t ti = qemu_timedate(); + time_t ti = qemu_ref_timedate(); ti += offset; switch (rtc_base_type) { + case RTC_BASE_DATETIME: case RTC_BASE_UTC: gmtime_r(&ti, tm); break; case RTC_BASE_LOCALTIME: localtime_r(&ti, tm); break; - case RTC_BASE_DATETIME: - ti -= rtc_host_datetime_offset; - gmtime_r(&ti, tm); - break; } } @@ -819,6 +831,7 @@ int qemu_timedate_diff(struct tm *tm) time_t seconds; switch (rtc_base_type) { + case RTC_BASE_DATETIME: case RTC_BASE_UTC: seconds = mktimegm(tm); break; @@ -829,9 +842,6 @@ int qemu_timedate_diff(struct tm *tm) seconds = mktime(&tmp); break; } - case RTC_BASE_DATETIME: - seconds = mktimegm(tm) + rtc_host_datetime_offset; - break; default: /* gcc complains: ‘seconds’ may be used uninitialized */ g_assert_not_reached(); @@ -839,10 +849,10 @@ int qemu_timedate_diff(struct tm *tm) break; } - return seconds - qemu_timedate(); + return seconds - qemu_ref_timedate(); } -static void configure_rtc_host_datetime_offset(const char *startdate) +static void configure_rtc_base_datetime(const char *startdate) { time_t rtc_start_datetime; struct tm tm; @@ -868,8 +878,8 @@ static void configure_rtc_host_datetime_offset(const char *startdate) "'2006-06-17T16:01:21' or '2006-06-17'\n"); exit(1); } - rtc_host_datetime_offset = (qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000) - - rtc_start_datetime; + rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime; + rtc_ref_start_datetime = rtc_start_datetime; } static void configure_rtc(QemuOpts *opts) @@ -888,7 +898,7 @@ static void configure_rtc(QemuOpts *opts) replay_add_blocker(blocker); } else { rtc_base_type = RTC_BASE_DATETIME; - configure_rtc_host_datetime_offset(value); + configure_rtc_base_datetime(value); } } value = qemu_opt_get(opts, "clock"); @@ -3039,6 +3049,8 @@ int main(int argc, char **argv, char **envp) exit(1); } rtc_clock = QEMU_CLOCK_HOST; + rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000; + rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000; QLIST_INIT (&vm_change_state_head); os_setup_early_signal_handling();