From patchwork Thu Oct 18 07:12:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artem Pisarenko X-Patchwork-Id: 10646795 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 B9CAA17D4 for ; Thu, 18 Oct 2018 07:14:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A78B8204FB for ; Thu, 18 Oct 2018 07:14:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9BA0D2522B; Thu, 18 Oct 2018 07:14:54 +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 3F3F3204FB for ; Thu, 18 Oct 2018 07:14:54 +0000 (UTC) Received: from localhost ([::1]:40602 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gD2WD-0005Fo-GC for patchwork-qemu-devel@patchwork.kernel.org; Thu, 18 Oct 2018 03:14:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:32968) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gD2Ut-0004Oh-1A for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gD2Us-0008H3-7C for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:31 -0400 Received: from mail-lj1-x241.google.com ([2a00:1450:4864:20::241]:36507) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gD2Ur-0008FB-Up for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:30 -0400 Received: by mail-lj1-x241.google.com with SMTP id p89-v6so26725951ljb.3 for ; Thu, 18 Oct 2018 00:13:29 -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 :in-reply-to:references; bh=FLqkhNN/tldODA+vTtpezLR8Rd8RwfY+oIHHL84yThw=; b=h4ZMJah+3tK1WO74Zr7DZfnEmih7xljNrSEK3xzmJ/II9fk9tK/W/VtgLy/4E7gHDW sMCgR1RVYvbS6qqE2QaN5ih/SCuClNfLmQGNUySw88C4gFJsRTgvVR3ARJjuS9s1w8m4 6ZN7SUuT+S8ZNpHRSgjorswwrPOCaFKrgw0t1EPtQPBU4dgB5j5fhku65UcPlprcLj4B VyofU4JNkgGTsedu992GUXTL+eWVmBuvfmT2zAQzXB1wVZkv8fafs3ffPMn3iVjfwzH2 2TXQAHmm7OhSLD44JC2Hd3TM16liaCu/QTSerrzjsGWi4QPGLfuLIn8G2DwBpjH1708f BAYA== 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:in-reply-to:references; bh=FLqkhNN/tldODA+vTtpezLR8Rd8RwfY+oIHHL84yThw=; b=pxM0Ze3d4fGOObo6UgNhuwEKQ3rre4cecdjybrRB2PBvOAp3fN01wrNEM64m9TSUXR cTVQPTLqEj9Nx3VH7PrRqlGEB78N1T1NtU4rL8JtlsAmQUB83/Pxlc4b+nHDAw8fkv9K gNOb0tCW4Os2w/12KN1oO8bHn1DbMwQNi3Y2/mItzPUP+9CQydfHZ4UwTPuiA/A19FK/ rTeZ3f8WDKla9IsnkLQQijTEtxvlFIa0FVUuyPjuUgz4Bc2cvzKREm1TO0/KpgnwCi29 yY2azCoSrGKXIS5rngHQsEWpN8cQ70FB4mi7WipFg3+qDWxYEkcHM4xkWdF74/ATBO8Q x/SA== X-Gm-Message-State: ABuFfojGTOEuoHVpedsUdhdp8GyCROl8TfJ5qQmsVh7wdrtRv4itVpR7 z91Dpeilpnyr/QbEbqZuBXW93k/bLxg= X-Google-Smtp-Source: ACcGV61FKHKLunawZzptaageNOils+VfYRpJmlGr2GToDjqsN8OimAL0pjhf2WXc4/tEu2AAriof/g== X-Received: by 2002:a2e:2a43:: with SMTP id q64-v6mr18492671ljq.153.1539846808257; Thu, 18 Oct 2018 00:13:28 -0700 (PDT) Received: from localhost.localdomain ([77.221.221.49]) by smtp.gmail.com with ESMTPSA id r4-v6sm4445426ljg.12.2018.10.18.00.13.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 18 Oct 2018 00:13:27 -0700 (PDT) From: Artem Pisarenko To: qemu-devel@nongnu.org Date: Thu, 18 Oct 2018 13:12:52 +0600 Message-Id: <1b245c6c0803d4bf11dcbf9eb32f34af8c2bd0b4.1539846575.git.artem.k.pisarenko@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: 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::241 Subject: [Qemu-devel] [PATCH v3 1/4] vl: improve/fix documentation related to RTC function 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: Artem Pisarenko Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Documentation describing -rtc option updated to better match current implementation and highlight some important specifics. Signed-off-by: Artem Pisarenko --- qemu-options.hx | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/qemu-options.hx b/qemu-options.hx index f139459..4a9c065 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -3458,25 +3458,29 @@ HXCOMM Silently ignored for compatibility DEF("clock", HAS_ARG, QEMU_OPTION_clock, "", QEMU_ARCH_ALL) DEF("rtc", HAS_ARG, QEMU_OPTION_rtc, \ - "-rtc [base=utc|localtime|date][,clock=host|rt|vm][,driftfix=none|slew]\n" \ + "-rtc [base=utc|localtime|][,clock=host|rt|vm][,driftfix=none|slew]\n" \ " set the RTC base and clock, enable drift fix for clock ticks (x86 only)\n", QEMU_ARCH_ALL) STEXI -@item -rtc [base=utc|localtime|@var{date}][,clock=host|vm][,driftfix=none|slew] +@item -rtc [base=utc|localtime|@var{datetime}][,clock=host|rt|vm][,driftfix=none|slew] @findex -rtc Specify @option{base} as @code{utc} or @code{localtime} to let the RTC start at the current UTC or local time, respectively. @code{localtime} is required for correct date in -MS-DOS or Windows. To start at a specific point in time, provide @var{date} in the +MS-DOS or Windows. To start at a specific point in time, provide @var{datetime} in the format @code{2006-06-17T16:01:21} or @code{2006-06-17}. The default base is UTC. By default the RTC is driven by the host system time. This allows using of the RTC as accurate reference clock inside the guest, specifically if the host time is smoothly following an accurate external reference clock, e.g. via NTP. If you want to isolate the guest time from the host, you can set @option{clock} -to @code{rt} instead. To even prevent it from progressing during suspension, -you can set it to @code{vm}. +to @code{rt} instead, which provides host monotonic clock if host support it. +To even prevent it from progressing during suspension, you can set it to +@code{vm} (virtual clock). Virtual clock is the clock seen by guest, +In icount mode of emulation its (long term) speed will be different from +any host clock, when icount configured to non-auto value or virtual cpu sleeping +is off, and no synchronization algorithm is active. Enable @option{driftfix} (i386 targets only) if you experience time drift problems, specifically with Windows' ACPI HAL. This option will try to figure out how From patchwork Thu Oct 18 07:12:53 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Artem Pisarenko X-Patchwork-Id: 10646797 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 664F117D4 for ; Thu, 18 Oct 2018 07:15:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4D9DF21FAD for ; Thu, 18 Oct 2018 07:15:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3D4882522B; Thu, 18 Oct 2018 07:15:09 +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 B42AC204FB for ; Thu, 18 Oct 2018 07:15:08 +0000 (UTC) Received: from localhost ([::1]:40605 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gD2WR-0005S7-V7 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 18 Oct 2018 03:15:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33015) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gD2V4-0004XS-9e for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gD2Uz-0008V9-8j for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:42 -0400 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]:34177) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gD2Uz-0008RU-0E for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:37 -0400 Received: by mail-lf1-x129.google.com with SMTP id n26-v6so3072938lfl.1 for ; Thu, 18 Oct 2018 00:13:36 -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=ddJ5zRtYlVtYtiBfL/OHyIASkhKnfJAXo1rrTA7IZ5c=; b=LB4S3W8nIZO3ZuiJ6T7idbRRbmVxR93+YU4hFs2rAAKdno8ZOyzBz7gGUUdN05PHt/ SF1Kgya+5GZmwgwi52ra8REvPnqO68fcUcupKti4Us5tF+rAT2lwINRmAxJ7X9TYEtk9 CvCbRZP17mfnazVeN0aesLU0umdV9k3Izc5snWHv0UaCN5es13w4y0rBD/fZejVfO4TO gbmnzxH0GB2iSwexinWqX7WaQzDJl+uDlVA404JBHo26t2aPpHV55nzlqFNeCccSb1nX 8vBcClHl6owN3FBzPaZHuBinkgn+2RWz0cv07YZP17rXVU5WEHGWfPstQ8qaYyy1UqJ/ CecQ== 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=ddJ5zRtYlVtYtiBfL/OHyIASkhKnfJAXo1rrTA7IZ5c=; b=aet12VaTr9SM0di4uZzAOZaPHqtNr9D20KiNujiw7ExW/9fx8wOWb/rLqBnehA36rr R2JsjsCdwTcTs4J9Dvhvw5nincc2DhIcc68hypbVD5RR/vM9YGUmQuVCpRepO+mcl7Ff h27RIrNUosUNRhovzwNe7fQZGvXER5fNcBVQtyMoeBG1KH4iKP924HcZ+XsumgbwYewT wpX9VDb7BCSxRuZnbm996i6GGlU6R4MmfaHheyNBlKfq+h/CjfPzvpJfz+rXZforLrXH JAP5jaUyErP3Jv57n/X6JzWoHtFHtCsBWrgkkO/Sgo1Dp0DpUk6tmO8Ojc45sGWHY79m KI9w== X-Gm-Message-State: ABuFfog4mJZ63c1CsEbvk/KX/y1F/tCJy8qYv05qWzI9ry+34xUWhwmE mqSZpqe6DjqLK45/WMcBJEyI0GG1ThU= X-Google-Smtp-Source: ACcGV619OKcooCyYGSIXMrDSX04Cl5wnOQAb0jHiHLl0F9pjW0aX9wEPwQay8nNIZenOTw5NhljIpw== X-Received: by 2002:a19:6803:: with SMTP id d3-v6mr6314171lfc.45.1539846814526; Thu, 18 Oct 2018 00:13:34 -0700 (PDT) Received: from localhost.localdomain ([77.221.221.49]) by smtp.gmail.com with ESMTPSA id r4-v6sm4445426ljg.12.2018.10.18.00.13.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 18 Oct 2018 00:13:33 -0700 (PDT) From: Artem Pisarenko To: qemu-devel@nongnu.org Date: Thu, 18 Oct 2018 13:12:53 +0600 Message-Id: <9330a48899f997431a34460014886d118a7c0960.1539846575.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::129 Subject: [Qemu-devel] [PATCH v3 2/4] vl: refactor -rtc option references 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 Improve code readability and prepare for fixing bug #1797033 Signed-off-by: Artem Pisarenko --- Notes: v2: fixed compiler warning vl.c | 85 ++++++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/vl.c b/vl.c index 4e25c78..10c4275 100644 --- a/vl.c +++ b/vl.c @@ -147,8 +147,13 @@ bool enable_cpu_pm = false; int nb_nics; NICInfo nd_table[MAX_NICS]; int autostart; -static int rtc_utc = 1; -static int rtc_date_offset = -1; /* -1 means no change */ +static enum { + RTC_BASE_UTC, + 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 */ QEMUClockType rtc_clock; int vga_interface_type = VGA_NONE; static DisplayOptions dpy; @@ -782,26 +787,30 @@ void qemu_system_vmstop_request(RunState state) /***********************************************************/ /* real time host monotonic timer */ -static time_t qemu_time(void) +static time_t qemu_timedate(void) { return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000; } /***********************************************************/ -/* host time/date access */ +/* RTC reference time/date access */ void qemu_get_timedate(struct tm *tm, int offset) { - time_t ti = qemu_time(); + time_t ti = qemu_timedate(); ti += offset; - if (rtc_date_offset == -1) { - if (rtc_utc) - gmtime_r(&ti, tm); - else - localtime_r(&ti, tm); - } else { - ti -= rtc_date_offset; + + switch (rtc_base_type) { + 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; } } @@ -809,23 +818,33 @@ int qemu_timedate_diff(struct tm *tm) { time_t seconds; - if (rtc_date_offset == -1) - if (rtc_utc) - seconds = mktimegm(tm); - else { - struct tm tmp = *tm; - tmp.tm_isdst = -1; /* use timezone to figure it out */ - seconds = mktime(&tmp); - } - else - seconds = mktimegm(tm) + rtc_date_offset; + switch (rtc_base_type) { + case RTC_BASE_UTC: + seconds = mktimegm(tm); + break; + case RTC_BASE_LOCALTIME: + { + struct tm tmp = *tm; + tmp.tm_isdst = -1; /* use timezone to figure it out */ + 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(); + seconds = -1; + break; + } - return seconds - qemu_time(); + return seconds - qemu_timedate(); } -static void configure_rtc_date_offset(const char *startdate) +static void configure_rtc_host_datetime_offset(const char *startdate) { - time_t rtc_start_date; + time_t rtc_start_datetime; struct tm tm; if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon, @@ -841,15 +860,16 @@ static void configure_rtc_date_offset(const char *startdate) } tm.tm_year -= 1900; tm.tm_mon--; - rtc_start_date = mktimegm(&tm); - if (rtc_start_date == -1) { + rtc_start_datetime = mktimegm(&tm); + if (rtc_start_datetime == -1) { date_fail: - error_report("invalid date format"); + error_report("invalid datetime format"); error_printf("valid formats: " "'2006-06-17T16:01:21' or '2006-06-17'\n"); exit(1); } - rtc_date_offset = qemu_time() - rtc_start_date; + rtc_host_datetime_offset = (qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000) + - rtc_start_datetime; } static void configure_rtc(QemuOpts *opts) @@ -859,15 +879,16 @@ static void configure_rtc(QemuOpts *opts) value = qemu_opt_get(opts, "base"); if (value) { if (!strcmp(value, "utc")) { - rtc_utc = 1; + rtc_base_type = RTC_BASE_UTC; } else if (!strcmp(value, "localtime")) { Error *blocker = NULL; - rtc_utc = 0; + rtc_base_type = RTC_BASE_LOCALTIME; error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "-rtc base=localtime"); replay_add_blocker(blocker); } else { - configure_rtc_date_offset(value); + rtc_base_type = RTC_BASE_DATETIME; + configure_rtc_host_datetime_offset(value); } } value = qemu_opt_get(opts, "clock"); From patchwork Thu Oct 18 07:12:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Artem Pisarenko X-Patchwork-Id: 10646799 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 278B7109C for ; Thu, 18 Oct 2018 07:18:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 046E4205A8 for ; Thu, 18 Oct 2018 07:18:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E93C92434C; Thu, 18 Oct 2018 07:18:09 +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 6957C205A8 for ; Thu, 18 Oct 2018 07:18:09 +0000 (UTC) Received: from localhost ([::1]:40619 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gD2ZM-0007wl-PC for patchwork-qemu-devel@patchwork.kernel.org; Thu, 18 Oct 2018 03:18:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33012) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gD2V4-0004XQ-8M for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gD2V1-000072-0H for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:42 -0400 Received: from mail-lj1-x22a.google.com ([2a00:1450:4864:20::22a]:34828) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gD2V0-0008Ud-NQ for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:38 -0400 Received: by mail-lj1-x22a.google.com with SMTP id o14-v6so26722100ljj.2 for ; Thu, 18 Oct 2018 00:13:37 -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=eRdR1nQ6/lQOg/KSyixMHnD+nyuoXkf8i5L0/iCg7Dw=; b=CBz594NgmTQ2HNI+VDK+SNsfO2U+Xc1j/hB/CXU9Pc8AFEAxt2R1RJRE1+6Hy+FHM5 ANfzNJtVjh1vUmm7QoQ8iZ2R2OMVNl2RqbOLFQfV0w7uR7OdYqQ6SdLpPjXZVXxNz2KW S59WWY0J46TCJdmsVjeYhXAO2SlGggOQFKPzawmn7osxXwOxvJ/5dJUEs03XqR6XqRd8 6gkndytcQxwdGComurC1o3fFZiMWoZf4QZoBjsXWomZRBNeyAHMGw+L9iQJ+4IILRx8g zXsM3nts/OBLObfpk5GXWum0ACW9kYk2HBc5uqhQ9ELKr2k+Srx1m/7k6wtgHc2I6IUE E7kA== 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=eRdR1nQ6/lQOg/KSyixMHnD+nyuoXkf8i5L0/iCg7Dw=; b=tBuoUg7YM5NdzuxsIn9m8CfuPXXCdNl4fcJbzd6XXzFyPlwrj0s5Hq2dW8Oj4Wp67C OFeOKOgP0sDOdcNtI3h2Y4YOCV2uUq0UTgIh/X+piAHmASO45HBn15wQ4stDwFzMYUSE QzLWL3bl0sN/8drtBrIX7gQM8oBVEmi9E0RHSzoJS6lHNj2q68zJFu4PeVF9FDVZtZIe pARzW7KkVBSlrWcCJoQS8Ye3Y9KYR5v4guUNv7EZc7o7zcslkn/rtsvPxKAgsMirm4iH BKA7PMbAJZqq9XZH90kG4DS2+k9O7KGK+grWvpEzGVxMpOlyChBsPBORCNrqf4ObpIOd Hujw== X-Gm-Message-State: ABuFfogx1E7vWTX631nOIg0Lsg1WnepKC9Hdp0w7OeM5kwWF+za6fb3l HAmpTty1m+JkfbNYCPOsJT6Q7qU9wp0= X-Google-Smtp-Source: ACcGV63PFHztrZ/PWKwA19ZshmXciQE0kvt/woY7mOn08J+Z0bY6hImVTol/su83ftRAUG80QxGPSQ== X-Received: by 2002:a2e:9047:: with SMTP id n7-v6mr16692143ljg.10.1539846816108; Thu, 18 Oct 2018 00:13:36 -0700 (PDT) Received: from localhost.localdomain ([77.221.221.49]) by smtp.gmail.com with ESMTPSA id r4-v6sm4445426ljg.12.2018.10.18.00.13.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 18 Oct 2018 00:13:35 -0700 (PDT) From: Artem Pisarenko To: qemu-devel@nongnu.org Date: Thu, 18 Oct 2018 13:12:54 +0600 Message-Id: <1d963c3e013dfedafa1f6edb9fb219b7e49e39da.1539846575.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::22a Subject: [Qemu-devel] [PATCH v3 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(); From patchwork Thu Oct 18 07:12:55 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artem Pisarenko X-Patchwork-Id: 10646801 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 A37D3109C for ; Thu, 18 Oct 2018 07:20:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 87FE328516 for ; Thu, 18 Oct 2018 07:20:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7C62F2852B; Thu, 18 Oct 2018 07:20:01 +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 18B7528516 for ; Thu, 18 Oct 2018 07:20:01 +0000 (UTC) Received: from localhost ([::1]:40627 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gD2bA-0000Vu-88 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 18 Oct 2018 03:20:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33014) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gD2V4-0004XR-9f for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gD2V2-0000AA-MG for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:42 -0400 Received: from mail-lf1-x141.google.com ([2a00:1450:4864:20::141]:38440) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gD2V1-00005Z-6k for qemu-devel@nongnu.org; Thu, 18 Oct 2018 03:13:40 -0400 Received: by mail-lf1-x141.google.com with SMTP id x24-v6so6009464lfe.5 for ; Thu, 18 Oct 2018 00:13:39 -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 :in-reply-to:references; bh=+4YIQzikY7eCJOvD47I5wo8oEHs+wdfAXhWlAO5cskE=; b=oudz1FHXzxiIK8obuQYtPHN7Ail+F6+zfKfJR2beTuRmUH6QMzbibd5ohnFHpXZ4W6 aGbq51gMCSD1dp8WmY+ViqDECWwKLqq7jPWe8Ejajm/1D6CWLHC90uNYg9h60waOwwIU 7S78+4JVi4S2JWmWKFY28oWMzL3P+7I65nUTgrETkOt2PYqxJIOc07uUtNDtd5KkX1Pb 6TFxrIgMj0Qydpa2Lv++WxPnHMXx5RlVrvklA3nss8EktjLX0qeT5850mlv7pgqZvSHC Dv/D0nn8lrI0lQ4/waECK62K7LFL8CpUAusHvK2eHWT1peJ/jHNQrwDqxO4nUcNRydy4 Ektw== 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:in-reply-to:references; bh=+4YIQzikY7eCJOvD47I5wo8oEHs+wdfAXhWlAO5cskE=; b=qJe1FIsRi2rSo4yAi1lOgYa86ybYH0aWoBUrkHmWjEBJisduw3ftnHu3jN2HxkQKn6 hI4O7LhIYabBTACMF337079WmDfCI9l64PCvMClSWD32X/1Axy/OCyIc+heNlWLJ5loE W0J1s/tfkNJIL85c8586gv4s9fW/9o4S5ExqsDlmdwubQtb33ygWAZIO7nWRNlHhmJhT 0bQJvynM9yG8VpmR/FM3WRYmxi5/2PVJCn+d1j4YacaDr1xIsFruErGL4mewoqOj6+xl DiO+X0ukcf/RgTZ9iY3uNejgULVJI959ZrdOmBgpJ67hQi8u4fO24m2EHCQhhfIWtgGl tNow== X-Gm-Message-State: ABuFfoiVvj0qdV/Du9gSWnyrYne0CtOZJnHgGS6QNdUIPt4Rx6ncOP1a GMmnXub+4Fd8ZXCUrV9tD+c80il+qHU= X-Google-Smtp-Source: ACcGV62FcX8gA5eJEDQUTbhvCNmgyLzYdRjzC7dq6nQgyov9rWrX/4AtICp24EcETb52yrIJBrHkaA== X-Received: by 2002:a19:d408:: with SMTP id l8-v6mr18474354lfg.25.1539846817606; Thu, 18 Oct 2018 00:13:37 -0700 (PDT) Received: from localhost.localdomain ([77.221.221.49]) by smtp.gmail.com with ESMTPSA id r4-v6sm4445426ljg.12.2018.10.18.00.13.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 18 Oct 2018 00:13:36 -0700 (PDT) From: Artem Pisarenko To: qemu-devel@nongnu.org Date: Thu, 18 Oct 2018 13:12:55 +0600 Message-Id: <1fc12c77e8b7115d3842919a8b586d9cbe4efca6.1539846575.git.artem.k.pisarenko@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: 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::141 Subject: [Qemu-devel] [PATCH v3 4/4] vl, qapi: offset calculation in RTC_CHANGE event reverted 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: Artem Pisarenko , Markus Armbruster , Paolo Bonzini Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Return value of qemu_timedate_diff(), used for calculation offset in QAPI 'RTC_CHANGE' event, restored to keep compatibility. Since it wasn't documented that difference is relative to host clock advancement, this change also adds important note to 'RTC_CHANGE' event description to highlight established implementation specifics. Signed-off-by: Artem Pisarenko --- qapi/misc.json | 3 ++- vl.c | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/qapi/misc.json b/qapi/misc.json index ada9af5..d0f5381 100644 --- a/qapi/misc.json +++ b/qapi/misc.json @@ -3070,7 +3070,8 @@ # Emitted when the guest changes the RTC time. # # @offset: offset between base RTC clock (as specified by -rtc base), and -# new RTC clock value +# new RTC clock value. Note that value will be different depending +# on clock chosen to drive RTC (specified by -rtc clock). # # Note: This event is rate-limited. # diff --git a/vl.c b/vl.c index 78a8a68..c350aba 100644 --- a/vl.c +++ b/vl.c @@ -788,10 +788,10 @@ void qemu_system_vmstop_request(RunState state) /***********************************************************/ /* RTC reference time/date access */ -static time_t qemu_ref_timedate(void) +static time_t qemu_ref_timedate(QEMUClockType clock) { - time_t value = qemu_clock_get_ms(rtc_clock) / 1000; - switch (rtc_clock) { + time_t value = qemu_clock_get_ms(clock) / 1000; + switch (clock) { case QEMU_CLOCK_REALTIME: value -= rtc_realtime_clock_offset; /* no break */ @@ -811,7 +811,7 @@ static time_t qemu_ref_timedate(void) void qemu_get_timedate(struct tm *tm, int offset) { - time_t ti = qemu_ref_timedate(); + time_t ti = qemu_ref_timedate(rtc_clock); ti += offset; @@ -849,7 +849,7 @@ int qemu_timedate_diff(struct tm *tm) break; } - return seconds - qemu_ref_timedate(); + return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST); } static void configure_rtc_base_datetime(const char *startdate)