2 * RTC configuration and clock read
4 * Copyright (c) 2003-2020 QEMU contributors
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu/cutils.h"
27 #include "qapi/error.h"
28 #include "qapi/qmp/qerror.h"
29 #include "qemu/error-report.h"
30 #include "qemu/option.h"
31 #include "qemu/timer.h"
32 #include "qom/object.h"
33 #include "sysemu/replay.h"
34 #include "sysemu/sysemu.h"
35 #include "sysemu/rtc.h"
36 #include "hw/rtc/mc146818rtc.h"
42 } rtc_base_type
= RTC_BASE_UTC
;
43 static time_t rtc_ref_start_datetime
;
44 static int rtc_realtime_clock_offset
; /* used only with QEMU_CLOCK_REALTIME */
45 static int rtc_host_datetime_offset
= -1; /* valid & used only with
47 QEMUClockType rtc_clock
;
48 /***********************************************************/
49 /* RTC reference time/date access */
50 static time_t qemu_ref_timedate(QEMUClockType clock
)
52 time_t value
= qemu_clock_get_ms(clock
) / 1000;
54 case QEMU_CLOCK_REALTIME
:
55 value
-= rtc_realtime_clock_offset
;
57 case QEMU_CLOCK_VIRTUAL
:
58 value
+= rtc_ref_start_datetime
;
61 if (rtc_base_type
== RTC_BASE_DATETIME
) {
62 value
-= rtc_host_datetime_offset
;
71 void qemu_get_timedate(struct tm
*tm
, int offset
)
73 time_t ti
= qemu_ref_timedate(rtc_clock
);
77 switch (rtc_base_type
) {
78 case RTC_BASE_DATETIME
:
82 case RTC_BASE_LOCALTIME
:
88 int qemu_timedate_diff(struct tm
*tm
)
92 switch (rtc_base_type
) {
93 case RTC_BASE_DATETIME
:
95 seconds
= mktimegm(tm
);
97 case RTC_BASE_LOCALTIME
:
100 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
101 seconds
= mktime(&tmp
);
108 return seconds
- qemu_ref_timedate(QEMU_CLOCK_HOST
);
111 static void configure_rtc_base_datetime(const char *startdate
)
113 time_t rtc_start_datetime
;
116 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d", &tm
.tm_year
, &tm
.tm_mon
,
117 &tm
.tm_mday
, &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
) == 6) {
119 } else if (sscanf(startdate
, "%d-%d-%d",
120 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
) == 3) {
129 rtc_start_datetime
= mktimegm(&tm
);
130 if (rtc_start_datetime
== -1) {
132 error_report("invalid datetime format");
133 error_printf("valid formats: "
134 "'2006-06-17T16:01:21' or '2006-06-17'\n");
137 rtc_host_datetime_offset
= rtc_ref_start_datetime
- rtc_start_datetime
;
138 rtc_ref_start_datetime
= rtc_start_datetime
;
141 void configure_rtc(QemuOpts
*opts
)
146 rtc_clock
= QEMU_CLOCK_HOST
;
147 rtc_ref_start_datetime
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) / 1000;
148 rtc_realtime_clock_offset
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) / 1000;
150 value
= qemu_opt_get(opts
, "base");
152 if (!strcmp(value
, "utc")) {
153 rtc_base_type
= RTC_BASE_UTC
;
154 } else if (!strcmp(value
, "localtime")) {
155 Error
*blocker
= NULL
;
156 rtc_base_type
= RTC_BASE_LOCALTIME
;
157 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
,
158 "-rtc base=localtime");
159 replay_add_blocker(blocker
);
161 rtc_base_type
= RTC_BASE_DATETIME
;
162 configure_rtc_base_datetime(value
);
165 value
= qemu_opt_get(opts
, "clock");
167 if (!strcmp(value
, "host")) {
168 rtc_clock
= QEMU_CLOCK_HOST
;
169 } else if (!strcmp(value
, "rt")) {
170 rtc_clock
= QEMU_CLOCK_REALTIME
;
171 } else if (!strcmp(value
, "vm")) {
172 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
174 error_report("invalid option value '%s'", value
);
178 value
= qemu_opt_get(opts
, "driftfix");
180 if (!strcmp(value
, "slew")) {
181 object_register_sugar_prop(TYPE_MC146818_RTC
,
185 if (!object_class_by_name(TYPE_MC146818_RTC
)) {
186 warn_report("driftfix 'slew' is not available with this machine");
188 } else if (!strcmp(value
, "none")) {
189 /* discard is default */
191 error_report("invalid option value '%s'", value
);