]> git.proxmox.com Git - qemu.git/blob - tests/rtc-test.c
Merge branch 'ppc-for-upstream' of git://repo.or.cz/qemu/agraf
[qemu.git] / tests / rtc-test.c
1 /*
2 * QTest testcase for the MC146818 real-time clock
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
11 *
12 */
13 #include "libqtest.h"
14 #include "hw/mc146818rtc_regs.h"
15
16 #include <glib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 static uint8_t base = 0x70;
23
24 static int bcd2dec(int value)
25 {
26 return (((value >> 4) & 0x0F) * 10) + (value & 0x0F);
27 }
28
29 static int dec2bcd(int value)
30 {
31 return ((value / 10) << 4) | (value % 10);
32 }
33
34 static uint8_t cmos_read(uint8_t reg)
35 {
36 outb(base + 0, reg);
37 return inb(base + 1);
38 }
39
40 static void cmos_write(uint8_t reg, uint8_t val)
41 {
42 outb(base + 0, reg);
43 outb(base + 1, val);
44 }
45
46 static int tm_cmp(struct tm *lhs, struct tm *rhs)
47 {
48 time_t a, b;
49 struct tm d1, d2;
50
51 memcpy(&d1, lhs, sizeof(d1));
52 memcpy(&d2, rhs, sizeof(d2));
53
54 a = mktime(&d1);
55 b = mktime(&d2);
56
57 if (a < b) {
58 return -1;
59 } else if (a > b) {
60 return 1;
61 }
62
63 return 0;
64 }
65
66 #if 0
67 static void print_tm(struct tm *tm)
68 {
69 printf("%04d-%02d-%02d %02d:%02d:%02d\n",
70 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
71 tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
72 }
73 #endif
74
75 static void cmos_get_date_time(struct tm *date)
76 {
77 int base_year = 2000, hour_offset;
78 int sec, min, hour, mday, mon, year;
79 time_t ts;
80 struct tm dummy;
81
82 sec = cmos_read(RTC_SECONDS);
83 min = cmos_read(RTC_MINUTES);
84 hour = cmos_read(RTC_HOURS);
85 mday = cmos_read(RTC_DAY_OF_MONTH);
86 mon = cmos_read(RTC_MONTH);
87 year = cmos_read(RTC_YEAR);
88
89 if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) {
90 sec = bcd2dec(sec);
91 min = bcd2dec(min);
92 hour = bcd2dec(hour);
93 mday = bcd2dec(mday);
94 mon = bcd2dec(mon);
95 year = bcd2dec(year);
96 hour_offset = 80;
97 } else {
98 hour_offset = 0x80;
99 }
100
101 if ((cmos_read(0x0B) & REG_B_24H) == 0) {
102 if (hour >= hour_offset) {
103 hour -= hour_offset;
104 hour += 12;
105 }
106 }
107
108 ts = time(NULL);
109 localtime_r(&ts, &dummy);
110
111 date->tm_isdst = dummy.tm_isdst;
112 date->tm_sec = sec;
113 date->tm_min = min;
114 date->tm_hour = hour;
115 date->tm_mday = mday;
116 date->tm_mon = mon - 1;
117 date->tm_year = base_year + year - 1900;
118 date->tm_gmtoff = 0;
119
120 ts = mktime(date);
121 }
122
123 static void check_time(int wiggle)
124 {
125 struct tm start, date[4], end;
126 struct tm *datep;
127 time_t ts;
128
129 /*
130 * This check assumes a few things. First, we cannot guarantee that we get
131 * a consistent reading from the wall clock because we may hit an edge of
132 * the clock while reading. To work around this, we read four clock readings
133 * such that at least two of them should match. We need to assume that one
134 * reading is corrupt so we need four readings to ensure that we have at
135 * least two consecutive identical readings
136 *
137 * It's also possible that we'll cross an edge reading the host clock so
138 * simply check to make sure that the clock reading is within the period of
139 * when we expect it to be.
140 */
141
142 ts = time(NULL);
143 gmtime_r(&ts, &start);
144
145 cmos_get_date_time(&date[0]);
146 cmos_get_date_time(&date[1]);
147 cmos_get_date_time(&date[2]);
148 cmos_get_date_time(&date[3]);
149
150 ts = time(NULL);
151 gmtime_r(&ts, &end);
152
153 if (tm_cmp(&date[0], &date[1]) == 0) {
154 datep = &date[0];
155 } else if (tm_cmp(&date[1], &date[2]) == 0) {
156 datep = &date[1];
157 } else if (tm_cmp(&date[2], &date[3]) == 0) {
158 datep = &date[2];
159 } else {
160 g_assert_not_reached();
161 }
162
163 if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) {
164 long t, s;
165
166 start.tm_isdst = datep->tm_isdst;
167
168 t = (long)mktime(datep);
169 s = (long)mktime(&start);
170 if (t < s) {
171 g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t));
172 } else {
173 g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s));
174 }
175
176 g_assert_cmpint(ABS(t - s), <=, wiggle);
177 }
178 }
179
180 static int wiggle = 2;
181
182 static void set_year_20xx(void)
183 {
184 /* Set BCD mode */
185 cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_DM);
186 cmos_write(RTC_REG_A, 0x76);
187 cmos_write(RTC_YEAR, 0x11);
188 cmos_write(RTC_CENTURY, 0x20);
189 cmos_write(RTC_MONTH, 0x02);
190 cmos_write(RTC_DAY_OF_MONTH, 0x02);
191 cmos_write(RTC_HOURS, 0x02);
192 cmos_write(RTC_MINUTES, 0x04);
193 cmos_write(RTC_SECONDS, 0x58);
194 cmos_write(RTC_REG_A, 0x26);
195
196 g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02);
197 g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04);
198 g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58);
199 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02);
200 g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02);
201 g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x11);
202 g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20);
203
204 /* Set a date in 2080 to ensure there is no year-2038 overflow. */
205 cmos_write(RTC_REG_A, 0x76);
206 cmos_write(RTC_YEAR, 0x80);
207 cmos_write(RTC_REG_A, 0x26);
208
209 g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02);
210 g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04);
211 g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58);
212 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02);
213 g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02);
214 g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x80);
215 g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20);
216
217 cmos_write(RTC_REG_A, 0x76);
218 cmos_write(RTC_YEAR, 0x11);
219 cmos_write(RTC_REG_A, 0x26);
220
221 g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02);
222 g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04);
223 g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58);
224 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02);
225 g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02);
226 g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x11);
227 g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20);
228 }
229
230 static void set_year_1980(void)
231 {
232 /* Set BCD mode */
233 cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_DM);
234 cmos_write(RTC_REG_A, 0x76);
235 cmos_write(RTC_YEAR, 0x80);
236 cmos_write(RTC_CENTURY, 0x19);
237 cmos_write(RTC_MONTH, 0x02);
238 cmos_write(RTC_DAY_OF_MONTH, 0x02);
239 cmos_write(RTC_HOURS, 0x02);
240 cmos_write(RTC_MINUTES, 0x04);
241 cmos_write(RTC_SECONDS, 0x58);
242 cmos_write(RTC_REG_A, 0x26);
243
244 g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02);
245 g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04);
246 g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58);
247 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02);
248 g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02);
249 g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x80);
250 g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x19);
251 }
252
253 static void bcd_check_time(void)
254 {
255 /* Set BCD mode */
256 cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_DM);
257 check_time(wiggle);
258 }
259
260 static void dec_check_time(void)
261 {
262 /* Set DEC mode */
263 cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
264 check_time(wiggle);
265 }
266
267 static void set_alarm_time(struct tm *tm)
268 {
269 int sec;
270
271 sec = tm->tm_sec;
272
273 if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) {
274 sec = dec2bcd(sec);
275 }
276
277 cmos_write(RTC_SECONDS_ALARM, sec);
278 cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE);
279 cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE);
280 }
281
282 static void alarm_time(void)
283 {
284 struct tm now;
285 time_t ts;
286 int i;
287
288 ts = time(NULL);
289 gmtime_r(&ts, &now);
290
291 /* set DEC mode */
292 cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM);
293
294 g_assert(!get_irq(RTC_ISA_IRQ));
295 cmos_read(RTC_REG_C);
296
297 now.tm_sec = (now.tm_sec + 2) % 60;
298 set_alarm_time(&now);
299 cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);
300
301 for (i = 0; i < 2 + wiggle; i++) {
302 if (get_irq(RTC_ISA_IRQ)) {
303 break;
304 }
305
306 clock_step(1000000000);
307 }
308
309 g_assert(get_irq(RTC_ISA_IRQ));
310 g_assert((cmos_read(RTC_REG_C) & REG_C_AF) != 0);
311 g_assert(cmos_read(RTC_REG_C) == 0);
312 }
313
314 /* success if no crash or abort */
315 static void fuzz_registers(void)
316 {
317 unsigned int i;
318
319 for (i = 0; i < 1000; i++) {
320 uint8_t reg, val;
321
322 reg = (uint8_t)g_test_rand_int_range(0, 16);
323 val = (uint8_t)g_test_rand_int_range(0, 256);
324
325 cmos_write(reg, val);
326 cmos_read(reg);
327 }
328 }
329
330 int main(int argc, char **argv)
331 {
332 QTestState *s = NULL;
333 int ret;
334
335 g_test_init(&argc, &argv, NULL);
336
337 s = qtest_start("-display none -rtc clock=vm");
338 qtest_irq_intercept_in(s, "ioapic");
339
340 qtest_add_func("/rtc/bcd/check-time", bcd_check_time);
341 qtest_add_func("/rtc/dec/check-time", dec_check_time);
342 qtest_add_func("/rtc/alarm-time", alarm_time);
343 qtest_add_func("/rtc/set-year/20xx", set_year_20xx);
344 qtest_add_func("/rtc/set-year/1980", set_year_1980);
345 qtest_add_func("/rtc/fuzz-registers", fuzz_registers);
346 ret = g_test_run();
347
348 if (s) {
349 qtest_quit(s);
350 }
351
352 return ret;
353 }