All of lore.kernel.org
 help / color / mirror / Atom feed
From: Igor Mammedov <imammedo@redhat.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [PATCH 31/36] vl: extract softmmu/rtc.c
Date: Fri, 27 Nov 2020 13:43:01 +0100	[thread overview]
Message-ID: <20201127134301.4c16886b@redhat.com> (raw)
In-Reply-To: <20201123141435.2726558-32-pbonzini@redhat.com>

On Mon, 23 Nov 2020 09:14:30 -0500
Paolo Bonzini <pbonzini@redhat.com> wrote:

> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Reviewed-by: Igor Mammedov <imammedo@redhat.com>

> ---
>  include/sysemu/sysemu.h |   2 +
>  softmmu/meson.build     |   1 +
>  softmmu/rtc.c           | 190 ++++++++++++++++++++++++++++++++++++++++
>  softmmu/vl.c            | 156 ---------------------------------
>  4 files changed, 193 insertions(+), 156 deletions(-)
>  create mode 100644 softmmu/rtc.c
> 
> diff --git a/include/sysemu/sysemu.h b/include/sysemu/sysemu.h
> index 1b62deaf2b..18cf586cd0 100644
> --- a/include/sysemu/sysemu.h
> +++ b/include/sysemu/sysemu.h
> @@ -22,6 +22,8 @@ void qemu_run_machine_init_done_notifiers(void);
>  void qemu_add_machine_init_done_notifier(Notifier *notify);
>  void qemu_remove_machine_init_done_notifier(Notifier *notify);
>  
> +void configure_rtc(QemuOpts *opts);
> +
>  extern int autostart;
>  
>  typedef enum {
> diff --git a/softmmu/meson.build b/softmmu/meson.build
> index 7b52339e7a..d098d89653 100644
> --- a/softmmu/meson.build
> +++ b/softmmu/meson.build
> @@ -6,6 +6,7 @@ specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files(
>    'datadir.c',
>    'physmem.c',
>    'ioport.c',
> +  'rtc.c',
>    'memory.c',
>    'memory_mapping.c',
>    'qtest.c',
> diff --git a/softmmu/rtc.c b/softmmu/rtc.c
> new file mode 100644
> index 0000000000..e1e15ef613
> --- /dev/null
> +++ b/softmmu/rtc.c
> @@ -0,0 +1,190 @@
> +/*
> + * RTC configuration and clock read
> + *
> + * Copyright (c) 2003-2020 QEMU contributors
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the "Software"), to deal
> + * in the Software without restriction, including without limitation the rights
> + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> + * copies of the Software, and to permit persons to whom the Software is
> + * furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> + * THE SOFTWARE.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "qemu-common.h"
> +#include "qemu/cutils.h"
> +#include "qapi/error.h"
> +#include "qapi/qmp/qerror.h"
> +#include "qemu/error-report.h"
> +#include "qemu/option.h"
> +#include "qemu/timer.h"
> +#include "qom/object.h"
> +#include "sysemu/replay.h"
> +#include "sysemu/sysemu.h"
> +
> +static enum {
> +    RTC_BASE_UTC,
> +    RTC_BASE_LOCALTIME,
> +    RTC_BASE_DATETIME,
> +} rtc_base_type = RTC_BASE_UTC;
> +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;
> +/***********************************************************/
> +/* RTC reference time/date access */
> +static time_t qemu_ref_timedate(QEMUClockType clock)
> +{
> +    time_t value = qemu_clock_get_ms(clock) / 1000;
> +    switch (clock) {
> +    case QEMU_CLOCK_REALTIME:
> +        value -= rtc_realtime_clock_offset;
> +        /* fall through */
> +    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_ref_timedate(rtc_clock);
> +
> +    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;
> +    }
> +}
> +
> +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;
> +    case RTC_BASE_LOCALTIME:
> +    {
> +        struct tm tmp = *tm;
> +        tmp.tm_isdst = -1; /* use timezone to figure it out */
> +        seconds = mktime(&tmp);
> +        break;
> +    }
> +    default:
> +        abort();
> +    }
> +
> +    return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
> +}
> +
> +static void configure_rtc_base_datetime(const char *startdate)
> +{
> +    time_t rtc_start_datetime;
> +    struct tm tm;
> +
> +    if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
> +               &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
> +        /* OK */
> +    } else if (sscanf(startdate, "%d-%d-%d",
> +                      &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
> +        tm.tm_hour = 0;
> +        tm.tm_min = 0;
> +        tm.tm_sec = 0;
> +    } else {
> +        goto date_fail;
> +    }
> +    tm.tm_year -= 1900;
> +    tm.tm_mon--;
> +    rtc_start_datetime = mktimegm(&tm);
> +    if (rtc_start_datetime == -1) {
> +    date_fail:
> +        error_report("invalid datetime format");
> +        error_printf("valid formats: "
> +                     "'2006-06-17T16:01:21' or '2006-06-17'\n");
> +        exit(1);
> +    }
> +    rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
> +    rtc_ref_start_datetime = rtc_start_datetime;
> +}
> +
> +void configure_rtc(QemuOpts *opts)
> +{
> +    const char *value;
> +
> +    /* Set defaults */
> +    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;
> +
> +    value = qemu_opt_get(opts, "base");
> +    if (value) {
> +        if (!strcmp(value, "utc")) {
> +            rtc_base_type = RTC_BASE_UTC;
> +        } else if (!strcmp(value, "localtime")) {
> +            Error *blocker = NULL;
> +            rtc_base_type = RTC_BASE_LOCALTIME;
> +            error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
> +                      "-rtc base=localtime");
> +            replay_add_blocker(blocker);
> +        } else {
> +            rtc_base_type = RTC_BASE_DATETIME;
> +            configure_rtc_base_datetime(value);
> +        }
> +    }
> +    value = qemu_opt_get(opts, "clock");
> +    if (value) {
> +        if (!strcmp(value, "host")) {
> +            rtc_clock = QEMU_CLOCK_HOST;
> +        } else if (!strcmp(value, "rt")) {
> +            rtc_clock = QEMU_CLOCK_REALTIME;
> +        } else if (!strcmp(value, "vm")) {
> +            rtc_clock = QEMU_CLOCK_VIRTUAL;
> +        } else {
> +            error_report("invalid option value '%s'", value);
> +            exit(1);
> +        }
> +    }
> +    value = qemu_opt_get(opts, "driftfix");
> +    if (value) {
> +        if (!strcmp(value, "slew")) {
> +            object_register_sugar_prop("mc146818rtc",
> +                                       "lost_tick_policy",
> +                                       "slew");
> +        } else if (!strcmp(value, "none")) {
> +            /* discard is default */
> +        } else {
> +            error_report("invalid option value '%s'", value);
> +            exit(1);
> +        }
> +    }
> +}
> diff --git a/softmmu/vl.c b/softmmu/vl.c
> index 7e13bb4a59..c9bb205c42 100644
> --- a/softmmu/vl.c
> +++ b/softmmu/vl.c
> @@ -152,16 +152,6 @@ bool enable_cpu_pm = false;
>  int nb_nics;
>  NICInfo nd_table[MAX_NICS];
>  int autostart = 1;
> -static enum {
> -    RTC_BASE_UTC,
> -    RTC_BASE_LOCALTIME,
> -    RTC_BASE_DATETIME,
> -} rtc_base_type = RTC_BASE_UTC;
> -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 const char *vga_model = NULL;
>  static DisplayOptions dpy;
> @@ -766,152 +756,6 @@ void qemu_system_vmstop_request(RunState state)
>      qemu_mutex_unlock(&vmstop_lock);
>      qemu_notify_event();
>  }
> -
> -/***********************************************************/
> -/* RTC reference time/date access */
> -static time_t qemu_ref_timedate(QEMUClockType clock)
> -{
> -    time_t value = qemu_clock_get_ms(clock) / 1000;
> -    switch (clock) {
> -    case QEMU_CLOCK_REALTIME:
> -        value -= rtc_realtime_clock_offset;
> -        /* fall through */
> -    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_ref_timedate(rtc_clock);
> -
> -    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;
> -    }
> -}
> -
> -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;
> -    case RTC_BASE_LOCALTIME:
> -    {
> -        struct tm tmp = *tm;
> -        tmp.tm_isdst = -1; /* use timezone to figure it out */
> -        seconds = mktime(&tmp);
> -        break;
> -    }
> -    default:
> -        abort();
> -    }
> -
> -    return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
> -}
> -
> -static void configure_rtc_base_datetime(const char *startdate)
> -{
> -    time_t rtc_start_datetime;
> -    struct tm tm;
> -
> -    if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
> -               &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
> -        /* OK */
> -    } else if (sscanf(startdate, "%d-%d-%d",
> -                      &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
> -        tm.tm_hour = 0;
> -        tm.tm_min = 0;
> -        tm.tm_sec = 0;
> -    } else {
> -        goto date_fail;
> -    }
> -    tm.tm_year -= 1900;
> -    tm.tm_mon--;
> -    rtc_start_datetime = mktimegm(&tm);
> -    if (rtc_start_datetime == -1) {
> -    date_fail:
> -        error_report("invalid datetime format");
> -        error_printf("valid formats: "
> -                     "'2006-06-17T16:01:21' or '2006-06-17'\n");
> -        exit(1);
> -    }
> -    rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
> -    rtc_ref_start_datetime = rtc_start_datetime;
> -}
> -
> -static void configure_rtc(QemuOpts *opts)
> -{
> -    const char *value;
> -
> -    /* Set defaults */
> -    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;
> -
> -    value = qemu_opt_get(opts, "base");
> -    if (value) {
> -        if (!strcmp(value, "utc")) {
> -            rtc_base_type = RTC_BASE_UTC;
> -        } else if (!strcmp(value, "localtime")) {
> -            Error *blocker = NULL;
> -            rtc_base_type = RTC_BASE_LOCALTIME;
> -            error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
> -                      "-rtc base=localtime");
> -            replay_add_blocker(blocker);
> -        } else {
> -            rtc_base_type = RTC_BASE_DATETIME;
> -            configure_rtc_base_datetime(value);
> -        }
> -    }
> -    value = qemu_opt_get(opts, "clock");
> -    if (value) {
> -        if (!strcmp(value, "host")) {
> -            rtc_clock = QEMU_CLOCK_HOST;
> -        } else if (!strcmp(value, "rt")) {
> -            rtc_clock = QEMU_CLOCK_REALTIME;
> -        } else if (!strcmp(value, "vm")) {
> -            rtc_clock = QEMU_CLOCK_VIRTUAL;
> -        } else {
> -            error_report("invalid option value '%s'", value);
> -            exit(1);
> -        }
> -    }
> -    value = qemu_opt_get(opts, "driftfix");
> -    if (value) {
> -        if (!strcmp(value, "slew")) {
> -            object_register_sugar_prop("mc146818rtc",
> -                                       "lost_tick_policy",
> -                                       "slew");
> -        } else if (!strcmp(value, "none")) {
> -            /* discard is default */
> -        } else {
> -            error_report("invalid option value '%s'", value);
> -            exit(1);
> -        }
> -    }
> -}
> -
>  static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
>  {
>      const char *proc_name;



  reply	other threads:[~2020-11-27 12:44 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-23 14:13 [PATCH v3 00/36] cleanup qemu_init and make sense of command line processing Paolo Bonzini
2020-11-23 14:14 ` [PATCH 01/36] vl: extract validation of -smp to machine.c Paolo Bonzini
2020-11-23 14:14 ` [PATCH 02/36] vl: remove bogus check Paolo Bonzini
2020-11-23 14:14 ` [PATCH 03/36] vl: split various early command line options to a separate function Paolo Bonzini
2020-11-26 16:47   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 04/36] vl: move various initialization routines out of qemu_init Paolo Bonzini
2020-11-23 14:14 ` [PATCH 05/36] vl: extract qemu_init_subsystems Paolo Bonzini
2020-11-23 14:14 ` [PATCH 06/36] vl: move prelaunch part of qemu_init to new functions Paolo Bonzini
2020-11-23 14:14 ` [PATCH 07/36] vl: extract various command line validation snippets to a new function Paolo Bonzini
2020-11-23 14:14 ` [PATCH 08/36] vl: preconfig and loadvm are mutually exclusive Paolo Bonzini
2020-11-23 14:14 ` [PATCH 09/36] vl: extract various command line desugaring snippets to a new function Paolo Bonzini
2020-11-23 14:14 ` [PATCH 10/36] qemu-option: restrict qemu_opts_set to merge-lists QemuOpts Paolo Bonzini
2020-11-23 14:14 ` [PATCH 11/36] vl: create "-net nic -net user" default earlier Paolo Bonzini
2020-11-23 14:14 ` [PATCH 12/36] vl: load plugins as late as possible Paolo Bonzini
2020-11-26 16:54   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 13/36] vl: move semihosting command line fallback to qemu_init_board Paolo Bonzini
2020-11-26 17:10   ` Igor Mammedov
2020-11-27  5:03     ` Paolo Bonzini
2020-11-27 10:31       ` Igor Mammedov
2020-11-27 11:22         ` Paolo Bonzini
2020-11-27 12:12           ` Igor Mammedov
2020-11-27 12:22             ` Paolo Bonzini
2020-11-23 14:14 ` [PATCH 14/36] vl: extract default devices to separate functions Paolo Bonzini
2020-11-26 17:29   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 15/36] vl: move CHECKPOINT_INIT after preconfig Paolo Bonzini
2020-11-26 17:36   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 16/36] vl: separate qemu_create_early_backends Paolo Bonzini
2020-11-23 14:14 ` [PATCH 17/36] vl: separate qemu_create_late_backends Paolo Bonzini
2020-11-23 14:14 ` [PATCH 18/36] vl: separate qemu_create_machine Paolo Bonzini
2020-11-23 14:14 ` [PATCH 19/36] vl: separate qemu_apply_machine_options Paolo Bonzini
2020-11-23 14:14 ` [PATCH 20/36] vl: separate qemu_resolve_machine_memdev Paolo Bonzini
2020-11-26 17:39   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 21/36] vl: initialize displays before preconfig loop Paolo Bonzini
2020-11-26 17:51   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 22/36] vl: move -global check earlier Paolo Bonzini
2020-11-26 17:55   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 23/36] migration, vl: start migration via qmp_migrate_incoming Paolo Bonzini
2020-11-26 18:04   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 24/36] vl: start VM via qmp_cont Paolo Bonzini
2020-11-23 14:14 ` [PATCH 25/36] hmp: introduce cmd_available Paolo Bonzini
2020-11-23 14:14 ` [PATCH 26/36] remove preconfig state Paolo Bonzini
2020-11-26 18:55   ` Igor Mammedov
2020-11-27  5:19     ` Paolo Bonzini
2020-11-27 10:50       ` Igor Mammedov
2020-11-27 11:50         ` Paolo Bonzini
2020-11-30 12:41           ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 27/36] vl: remove separate preconfig main_loop Paolo Bonzini
2020-11-30 12:46   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 28/36] vl: allow -incoming defer with -preconfig Paolo Bonzini
2020-11-27 10:51   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 29/36] vl: extract softmmu/datadir.c Paolo Bonzini
2020-11-27 12:06   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 30/36] vl: extract machine done notifiers Paolo Bonzini
2020-11-27 12:14   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 31/36] vl: extract softmmu/rtc.c Paolo Bonzini
2020-11-27 12:43   ` Igor Mammedov [this message]
2020-11-23 14:14 ` [PATCH 32/36] vl: extract softmmu/runstate.c Paolo Bonzini
2020-11-27 12:59   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 33/36] vl: extract softmmu/globals.c Paolo Bonzini
2020-11-27 13:19   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 34/36] vl: remove serial_max_hds Paolo Bonzini
2020-11-27 13:11   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 35/36] vl: clean up -boot variables Paolo Bonzini
2020-11-27 13:12   ` Igor Mammedov
2020-11-23 14:14 ` [PATCH 36/36] vl: move all generic initialization out of vl.c Paolo Bonzini
2020-11-27 13:30   ` Igor Mammedov
2020-11-27 12:00 ` [PATCH 37/36] machine: introduce MachineInitPhase Paolo Bonzini
2020-11-27 13:29   ` Igor Mammedov
2020-11-27 15:29     ` Paolo Bonzini
2020-11-30 12:50 ` [PATCH v3 00/36] cleanup qemu_init and make sense of command line processing Igor Mammedov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201127134301.4c16886b@redhat.com \
    --to=imammedo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.