]>
Commit | Line | Data |
---|---|---|
d1aaf543 AL |
1 | /* |
2 | * QTest testcase for the MC146818 real-time clock | |
3 | * | |
4 | * Copyright IBM, Corp. 2012 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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; | |
a05ddd92 | 118 | #ifndef __sun__ |
d1aaf543 | 119 | date->tm_gmtoff = 0; |
a05ddd92 | 120 | #endif |
d1aaf543 AL |
121 | |
122 | ts = mktime(date); | |
123 | } | |
124 | ||
125 | static void check_time(int wiggle) | |
126 | { | |
127 | struct tm start, date[4], end; | |
128 | struct tm *datep; | |
129 | time_t ts; | |
130 | ||
131 | /* | |
132 | * This check assumes a few things. First, we cannot guarantee that we get | |
133 | * a consistent reading from the wall clock because we may hit an edge of | |
134 | * the clock while reading. To work around this, we read four clock readings | |
135 | * such that at least two of them should match. We need to assume that one | |
136 | * reading is corrupt so we need four readings to ensure that we have at | |
137 | * least two consecutive identical readings | |
138 | * | |
139 | * It's also possible that we'll cross an edge reading the host clock so | |
140 | * simply check to make sure that the clock reading is within the period of | |
141 | * when we expect it to be. | |
142 | */ | |
143 | ||
144 | ts = time(NULL); | |
145 | gmtime_r(&ts, &start); | |
146 | ||
147 | cmos_get_date_time(&date[0]); | |
148 | cmos_get_date_time(&date[1]); | |
149 | cmos_get_date_time(&date[2]); | |
150 | cmos_get_date_time(&date[3]); | |
151 | ||
152 | ts = time(NULL); | |
153 | gmtime_r(&ts, &end); | |
154 | ||
155 | if (tm_cmp(&date[0], &date[1]) == 0) { | |
156 | datep = &date[0]; | |
157 | } else if (tm_cmp(&date[1], &date[2]) == 0) { | |
158 | datep = &date[1]; | |
159 | } else if (tm_cmp(&date[2], &date[3]) == 0) { | |
160 | datep = &date[2]; | |
161 | } else { | |
162 | g_assert_not_reached(); | |
163 | } | |
164 | ||
165 | if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) { | |
02b3efcb | 166 | long t, s; |
d1aaf543 AL |
167 | |
168 | start.tm_isdst = datep->tm_isdst; | |
169 | ||
02b3efcb BS |
170 | t = (long)mktime(datep); |
171 | s = (long)mktime(&start); | |
d1aaf543 AL |
172 | if (t < s) { |
173 | g_test_message("RTC is %ld second(s) behind wall-clock\n", (s - t)); | |
174 | } else { | |
175 | g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t - s)); | |
176 | } | |
177 | ||
178 | g_assert_cmpint(ABS(t - s), <=, wiggle); | |
179 | } | |
180 | } | |
181 | ||
182 | static int wiggle = 2; | |
183 | ||
b8994faf | 184 | static void set_year_20xx(void) |
b6db4aca PB |
185 | { |
186 | /* Set BCD mode */ | |
187 | cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_DM); | |
188 | cmos_write(RTC_REG_A, 0x76); | |
189 | cmos_write(RTC_YEAR, 0x11); | |
b8994faf | 190 | cmos_write(RTC_CENTURY, 0x20); |
b6db4aca PB |
191 | cmos_write(RTC_MONTH, 0x02); |
192 | cmos_write(RTC_DAY_OF_MONTH, 0x02); | |
193 | cmos_write(RTC_HOURS, 0x02); | |
194 | cmos_write(RTC_MINUTES, 0x04); | |
195 | cmos_write(RTC_SECONDS, 0x58); | |
196 | cmos_write(RTC_REG_A, 0x26); | |
197 | ||
198 | g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02); | |
199 | g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04); | |
200 | g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58); | |
201 | g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02); | |
202 | g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02); | |
203 | g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x11); | |
b8994faf | 204 | g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20); |
b6db4aca | 205 | |
4e45deed GH |
206 | if (sizeof(time_t) == 4) { |
207 | return; | |
208 | } | |
209 | ||
b6db4aca PB |
210 | /* Set a date in 2080 to ensure there is no year-2038 overflow. */ |
211 | cmos_write(RTC_REG_A, 0x76); | |
212 | cmos_write(RTC_YEAR, 0x80); | |
213 | cmos_write(RTC_REG_A, 0x26); | |
214 | ||
215 | g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02); | |
216 | g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04); | |
217 | g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58); | |
218 | g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02); | |
219 | g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02); | |
220 | g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x80); | |
b8994faf | 221 | g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20); |
b6db4aca PB |
222 | |
223 | cmos_write(RTC_REG_A, 0x76); | |
224 | cmos_write(RTC_YEAR, 0x11); | |
225 | cmos_write(RTC_REG_A, 0x26); | |
226 | ||
227 | g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02); | |
228 | g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04); | |
229 | g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58); | |
230 | g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02); | |
231 | g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02); | |
232 | g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x11); | |
b8994faf PB |
233 | g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20); |
234 | } | |
235 | ||
236 | static void set_year_1980(void) | |
237 | { | |
238 | /* Set BCD mode */ | |
239 | cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_DM); | |
240 | cmos_write(RTC_REG_A, 0x76); | |
241 | cmos_write(RTC_YEAR, 0x80); | |
242 | cmos_write(RTC_CENTURY, 0x19); | |
243 | cmos_write(RTC_MONTH, 0x02); | |
244 | cmos_write(RTC_DAY_OF_MONTH, 0x02); | |
245 | cmos_write(RTC_HOURS, 0x02); | |
246 | cmos_write(RTC_MINUTES, 0x04); | |
247 | cmos_write(RTC_SECONDS, 0x58); | |
248 | cmos_write(RTC_REG_A, 0x26); | |
249 | ||
250 | g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02); | |
251 | g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04); | |
252 | g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58); | |
253 | g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02); | |
254 | g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02); | |
255 | g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x80); | |
256 | g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x19); | |
b6db4aca PB |
257 | } |
258 | ||
d1aaf543 AL |
259 | static void bcd_check_time(void) |
260 | { | |
261 | /* Set BCD mode */ | |
262 | cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_DM); | |
263 | check_time(wiggle); | |
264 | } | |
265 | ||
266 | static void dec_check_time(void) | |
267 | { | |
268 | /* Set DEC mode */ | |
269 | cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM); | |
270 | check_time(wiggle); | |
271 | } | |
272 | ||
273 | static void set_alarm_time(struct tm *tm) | |
274 | { | |
275 | int sec; | |
276 | ||
277 | sec = tm->tm_sec; | |
278 | ||
279 | if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) { | |
280 | sec = dec2bcd(sec); | |
281 | } | |
282 | ||
283 | cmos_write(RTC_SECONDS_ALARM, sec); | |
284 | cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE); | |
285 | cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE); | |
286 | } | |
287 | ||
288 | static void alarm_time(void) | |
289 | { | |
290 | struct tm now; | |
291 | time_t ts; | |
292 | int i; | |
293 | ||
294 | ts = time(NULL); | |
295 | gmtime_r(&ts, &now); | |
296 | ||
297 | /* set DEC mode */ | |
298 | cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_DM); | |
299 | ||
300 | g_assert(!get_irq(RTC_ISA_IRQ)); | |
301 | cmos_read(RTC_REG_C); | |
302 | ||
303 | now.tm_sec = (now.tm_sec + 2) % 60; | |
304 | set_alarm_time(&now); | |
305 | cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE); | |
306 | ||
307 | for (i = 0; i < 2 + wiggle; i++) { | |
308 | if (get_irq(RTC_ISA_IRQ)) { | |
309 | break; | |
310 | } | |
311 | ||
312 | clock_step(1000000000); | |
313 | } | |
314 | ||
315 | g_assert(get_irq(RTC_ISA_IRQ)); | |
316 | g_assert((cmos_read(RTC_REG_C) & REG_C_AF) != 0); | |
317 | g_assert(cmos_read(RTC_REG_C) == 0); | |
318 | } | |
319 | ||
85215d41 BS |
320 | /* success if no crash or abort */ |
321 | static void fuzz_registers(void) | |
322 | { | |
323 | unsigned int i; | |
324 | ||
325 | for (i = 0; i < 1000; i++) { | |
326 | uint8_t reg, val; | |
327 | ||
328 | reg = (uint8_t)g_test_rand_int_range(0, 16); | |
329 | val = (uint8_t)g_test_rand_int_range(0, 256); | |
330 | ||
331 | cmos_write(reg, val); | |
332 | cmos_read(reg); | |
333 | } | |
334 | } | |
335 | ||
02c6ccc6 AH |
336 | static void register_b_set_flag(void) |
337 | { | |
338 | /* Enable binary-coded decimal (BCD) mode and SET flag in Register B*/ | |
339 | cmos_write(RTC_REG_B, (cmos_read(RTC_REG_B) & ~REG_B_DM) | REG_B_SET); | |
340 | ||
341 | cmos_write(RTC_REG_A, 0x76); | |
342 | cmos_write(RTC_YEAR, 0x11); | |
343 | cmos_write(RTC_CENTURY, 0x20); | |
344 | cmos_write(RTC_MONTH, 0x02); | |
345 | cmos_write(RTC_DAY_OF_MONTH, 0x02); | |
346 | cmos_write(RTC_HOURS, 0x02); | |
347 | cmos_write(RTC_MINUTES, 0x04); | |
348 | cmos_write(RTC_SECONDS, 0x58); | |
349 | cmos_write(RTC_REG_A, 0x26); | |
350 | ||
351 | /* Since SET flag is still enabled, these are equality checks. */ | |
352 | g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02); | |
353 | g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04); | |
354 | g_assert_cmpint(cmos_read(RTC_SECONDS), ==, 0x58); | |
355 | g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02); | |
356 | g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02); | |
357 | g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x11); | |
358 | g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20); | |
359 | ||
360 | /* Disable SET flag in Register B */ | |
361 | cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_SET); | |
362 | ||
363 | g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02); | |
364 | g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04); | |
365 | ||
366 | /* Since SET flag is disabled, this is an inequality check. | |
367 | * We (reasonably) assume that no (sexagesimal) overflow occurs. */ | |
368 | g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58); | |
369 | g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02); | |
370 | g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02); | |
371 | g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x11); | |
372 | g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20); | |
373 | } | |
374 | ||
d1aaf543 AL |
375 | int main(int argc, char **argv) |
376 | { | |
377 | QTestState *s = NULL; | |
378 | int ret; | |
379 | ||
380 | g_test_init(&argc, &argv, NULL); | |
381 | ||
382 | s = qtest_start("-display none -rtc clock=vm"); | |
383 | qtest_irq_intercept_in(s, "ioapic"); | |
384 | ||
385 | qtest_add_func("/rtc/bcd/check-time", bcd_check_time); | |
386 | qtest_add_func("/rtc/dec/check-time", dec_check_time); | |
387 | qtest_add_func("/rtc/alarm-time", alarm_time); | |
b8994faf PB |
388 | qtest_add_func("/rtc/set-year/20xx", set_year_20xx); |
389 | qtest_add_func("/rtc/set-year/1980", set_year_1980); | |
02c6ccc6 | 390 | qtest_add_func("/rtc/register_b_set_flag", register_b_set_flag); |
85215d41 | 391 | qtest_add_func("/rtc/fuzz-registers", fuzz_registers); |
d1aaf543 AL |
392 | ret = g_test_run(); |
393 | ||
394 | if (s) { | |
395 | qtest_quit(s); | |
396 | } | |
397 | ||
398 | return ret; | |
399 | } |