Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/system/rtc.c
Line
Count
Source
1
/*
2
 * RTC configuration and clock read
3
 *
4
 * Copyright (c) 2003-2020 QEMU contributors
5
 *
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:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
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
22
 * THE SOFTWARE.
23
 */
24
25
#include "qemu/osdep.h"
26
#include "qemu/cutils.h"
27
#include "qapi/error.h"
28
#include "qemu/error-report.h"
29
#include "qemu/option.h"
30
#include "qemu/timer.h"
31
#include "qom/compat-properties.h"
32
#include "qom/object.h"
33
#include "system/replay.h"
34
#include "system/system.h"
35
#include "system/rtc.h"
36
#include "hw/rtc/mc146818rtc.h"
37
38
static enum {
39
    RTC_BASE_UTC,
40
    RTC_BASE_LOCALTIME,
41
    RTC_BASE_DATETIME,
42
} rtc_base_type = RTC_BASE_UTC;
43
static time_t rtc_ref_start_datetime;
44
static time_t rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
45
static time_t rtc_host_datetime_offset = -1; /* valid & used only with
46
                                             RTC_BASE_DATETIME */
47
QEMUClockType rtc_clock;
48
/***********************************************************/
49
/* RTC reference time/date access */
50
static time_t qemu_ref_timedate(QEMUClockType clock)
51
61
{
52
61
    time_t value = qemu_clock_get_ms(clock) / 1000;
53
61
    switch (clock) {
54
0
    case QEMU_CLOCK_REALTIME:
55
0
        value -= rtc_realtime_clock_offset;
56
        /* fall through */
57
0
    case QEMU_CLOCK_VIRTUAL:
58
0
        value += rtc_ref_start_datetime;
59
0
        break;
60
61
    case QEMU_CLOCK_HOST:
61
61
        if (rtc_base_type == RTC_BASE_DATETIME) {
62
0
            value -= rtc_host_datetime_offset;
63
0
        }
64
61
        break;
65
0
    default:
66
0
        g_assert_not_reached();
67
61
    }
68
61
    return value;
69
61
}
70
71
void qemu_get_timedate(struct tm *tm, time_t offset)
72
61
{
73
61
    time_t ti = qemu_ref_timedate(rtc_clock);
74
75
61
    ti += offset;
76
77
61
    switch (rtc_base_type) {
78
0
    case RTC_BASE_DATETIME:
79
61
    case RTC_BASE_UTC:
80
61
        gmtime_r(&ti, tm);
81
61
        break;
82
0
    case RTC_BASE_LOCALTIME:
83
0
        localtime_r(&ti, tm);
84
0
        break;
85
61
    }
86
61
}
87
88
time_t qemu_timedate_diff(struct tm *tm)
89
0
{
90
0
    time_t seconds;
91
92
0
    switch (rtc_base_type) {
93
0
    case RTC_BASE_DATETIME:
94
0
    case RTC_BASE_UTC:
95
0
        seconds = mktimegm(tm);
96
0
        break;
97
0
    case RTC_BASE_LOCALTIME:
98
0
    {
99
0
        struct tm tmp = *tm;
100
0
        tmp.tm_isdst = -1; /* use timezone to figure it out */
101
0
        seconds = mktime(&tmp);
102
0
        break;
103
0
    }
104
0
    default:
105
0
        abort();
106
0
    }
107
108
0
    return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
109
0
}
110
111
static void configure_rtc_base_datetime(const char *startdate)
112
0
{
113
0
    time_t rtc_start_datetime;
114
0
    struct tm tm;
115
116
0
    if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
117
0
               &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
118
        /* OK */
119
0
    } else if (sscanf(startdate, "%d-%d-%d",
120
0
                      &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
121
0
        tm.tm_hour = 0;
122
0
        tm.tm_min = 0;
123
0
        tm.tm_sec = 0;
124
0
    } else {
125
0
        goto date_fail;
126
0
    }
127
0
    tm.tm_year -= 1900;
128
0
    tm.tm_mon--;
129
0
    rtc_start_datetime = mktimegm(&tm);
130
0
    if (rtc_start_datetime == -1) {
131
0
    date_fail:
132
0
        error_report("invalid datetime format");
133
0
        error_printf("valid formats: "
134
0
                     "'2006-06-17T16:01:21' or '2006-06-17'\n");
135
0
        exit(1);
136
0
    }
137
0
    rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
138
0
    rtc_ref_start_datetime = rtc_start_datetime;
139
0
}
140
141
void configure_rtc(QemuOpts *opts)
142
61
{
143
61
    const char *value;
144
145
    /* Set defaults */
146
61
    rtc_clock = QEMU_CLOCK_HOST;
147
61
    rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
148
61
    rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
149
150
61
    value = qemu_opt_get(opts, "base");
151
61
    if (value) {
152
0
        if (!strcmp(value, "utc")) {
153
0
            rtc_base_type = RTC_BASE_UTC;
154
0
        } else if (!strcmp(value, "localtime")) {
155
0
            rtc_base_type = RTC_BASE_LOCALTIME;
156
0
            replay_add_blocker("-rtc base=localtime");
157
0
        } else {
158
0
            rtc_base_type = RTC_BASE_DATETIME;
159
0
            configure_rtc_base_datetime(value);
160
0
        }
161
0
    }
162
61
    value = qemu_opt_get(opts, "clock");
163
61
    if (value) {
164
0
        if (!strcmp(value, "host")) {
165
0
            rtc_clock = QEMU_CLOCK_HOST;
166
0
        } else if (!strcmp(value, "rt")) {
167
0
            rtc_clock = QEMU_CLOCK_REALTIME;
168
0
        } else if (!strcmp(value, "vm")) {
169
0
            rtc_clock = QEMU_CLOCK_VIRTUAL;
170
0
        } else {
171
0
            error_report("invalid option value '%s'", value);
172
0
            exit(1);
173
0
        }
174
0
    }
175
61
    value = qemu_opt_get(opts, "driftfix");
176
61
    if (value) {
177
0
        if (!strcmp(value, "slew")) {
178
0
            object_register_sugar_prop(TYPE_MC146818_RTC,
179
0
                                       "lost_tick_policy",
180
0
                                       "slew",
181
0
                                       false);
182
0
            if (!object_class_by_name(TYPE_MC146818_RTC)) {
183
0
                warn_report("driftfix 'slew' is not available with this machine");
184
0
            }
185
0
        } else if (!strcmp(value, "none")) {
186
            /* discard is default */
187
0
        } else {
188
0
            error_report("invalid option value '%s'", value);
189
0
            exit(1);
190
0
        }
191
0
    }
192
61
}