2 * QTest testcase for the MC146818 real-time clock
4 * Copyright IBM, Corp. 2012
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
14 #include "hw/mc146818rtc_regs.h"
22 static uint8_t base
= 0x70;
24 static int bcd2dec(int value
)
26 return (((value
>> 4) & 0x0F) * 10) + (value
& 0x0F);
29 static int dec2bcd(int value
)
31 return ((value
/ 10) << 4) | (value
% 10);
34 static uint8_t cmos_read(uint8_t reg
)
40 static void cmos_write(uint8_t reg
, uint8_t val
)
46 static int tm_cmp(struct tm
*lhs
, struct tm
*rhs
)
51 memcpy(&d1
, lhs
, sizeof(d1
));
52 memcpy(&d2
, rhs
, sizeof(d2
));
67 static void print_tm(struct tm
*tm
)
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
);
75 static void cmos_get_date_time(struct tm
*date
)
77 int base_year
= 2000, hour_offset
;
78 int sec
, min
, hour
, mday
, mon
, year
;
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
);
89 if ((cmos_read(RTC_REG_B
) & REG_B_DM
) == 0) {
101 if ((cmos_read(0x0B) & REG_B_24H
) == 0) {
102 if (hour
>= hour_offset
) {
109 localtime_r(&ts
, &dummy
);
111 date
->tm_isdst
= dummy
.tm_isdst
;
114 date
->tm_hour
= hour
;
115 date
->tm_mday
= mday
;
116 date
->tm_mon
= mon
- 1;
117 date
->tm_year
= base_year
+ year
- 1900;
123 static void check_time(int wiggle
)
125 struct tm start
, date
[4], end
;
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
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.
143 gmtime_r(&ts
, &start
);
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]);
153 if (tm_cmp(&date
[0], &date
[1]) == 0) {
155 } else if (tm_cmp(&date
[1], &date
[2]) == 0) {
157 } else if (tm_cmp(&date
[2], &date
[3]) == 0) {
160 g_assert_not_reached();
163 if (!(tm_cmp(&start
, datep
) <= 0 && tm_cmp(datep
, &end
) <= 0)) {
166 start
.tm_isdst
= datep
->tm_isdst
;
168 t
= (long)mktime(datep
);
169 s
= (long)mktime(&start
);
171 g_test_message("RTC is %ld second(s) behind wall-clock\n", (s
- t
));
173 g_test_message("RTC is %ld second(s) ahead of wall-clock\n", (t
- s
));
176 g_assert_cmpint(ABS(t
- s
), <=, wiggle
);
180 static int wiggle
= 2;
182 static void set_year_20xx(void)
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);
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);
204 if (sizeof(time_t) == 4) {
208 /* Set a date in 2080 to ensure there is no year-2038 overflow. */
209 cmos_write(RTC_REG_A
, 0x76);
210 cmos_write(RTC_YEAR
, 0x80);
211 cmos_write(RTC_REG_A
, 0x26);
213 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
214 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
215 g_assert_cmpint(cmos_read(RTC_SECONDS
), >=, 0x58);
216 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
217 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
218 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x80);
219 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x20);
221 cmos_write(RTC_REG_A
, 0x76);
222 cmos_write(RTC_YEAR
, 0x11);
223 cmos_write(RTC_REG_A
, 0x26);
225 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
226 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
227 g_assert_cmpint(cmos_read(RTC_SECONDS
), >=, 0x58);
228 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
229 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
230 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x11);
231 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x20);
234 static void set_year_1980(void)
237 cmos_write(RTC_REG_B
, cmos_read(RTC_REG_B
) & ~REG_B_DM
);
238 cmos_write(RTC_REG_A
, 0x76);
239 cmos_write(RTC_YEAR
, 0x80);
240 cmos_write(RTC_CENTURY
, 0x19);
241 cmos_write(RTC_MONTH
, 0x02);
242 cmos_write(RTC_DAY_OF_MONTH
, 0x02);
243 cmos_write(RTC_HOURS
, 0x02);
244 cmos_write(RTC_MINUTES
, 0x04);
245 cmos_write(RTC_SECONDS
, 0x58);
246 cmos_write(RTC_REG_A
, 0x26);
248 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
249 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
250 g_assert_cmpint(cmos_read(RTC_SECONDS
), >=, 0x58);
251 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
252 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
253 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x80);
254 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x19);
257 static void bcd_check_time(void)
260 cmos_write(RTC_REG_B
, cmos_read(RTC_REG_B
) & ~REG_B_DM
);
264 static void dec_check_time(void)
267 cmos_write(RTC_REG_B
, cmos_read(RTC_REG_B
) | REG_B_DM
);
271 static void set_alarm_time(struct tm
*tm
)
277 if ((cmos_read(RTC_REG_B
) & REG_B_DM
) == 0) {
281 cmos_write(RTC_SECONDS_ALARM
, sec
);
282 cmos_write(RTC_MINUTES_ALARM
, RTC_ALARM_DONT_CARE
);
283 cmos_write(RTC_HOURS_ALARM
, RTC_ALARM_DONT_CARE
);
286 static void alarm_time(void)
296 cmos_write(RTC_REG_B
, cmos_read(RTC_REG_B
) | REG_B_DM
);
298 g_assert(!get_irq(RTC_ISA_IRQ
));
299 cmos_read(RTC_REG_C
);
301 now
.tm_sec
= (now
.tm_sec
+ 2) % 60;
302 set_alarm_time(&now
);
303 cmos_write(RTC_REG_B
, cmos_read(RTC_REG_B
) | REG_B_AIE
);
305 for (i
= 0; i
< 2 + wiggle
; i
++) {
306 if (get_irq(RTC_ISA_IRQ
)) {
310 clock_step(1000000000);
313 g_assert(get_irq(RTC_ISA_IRQ
));
314 g_assert((cmos_read(RTC_REG_C
) & REG_C_AF
) != 0);
315 g_assert(cmos_read(RTC_REG_C
) == 0);
318 /* success if no crash or abort */
319 static void fuzz_registers(void)
323 for (i
= 0; i
< 1000; i
++) {
326 reg
= (uint8_t)g_test_rand_int_range(0, 16);
327 val
= (uint8_t)g_test_rand_int_range(0, 256);
329 cmos_write(reg
, val
);
334 static void register_b_set_flag(void)
336 /* Enable binary-coded decimal (BCD) mode and SET flag in Register B*/
337 cmos_write(RTC_REG_B
, (cmos_read(RTC_REG_B
) & ~REG_B_DM
) | REG_B_SET
);
339 cmos_write(RTC_REG_A
, 0x76);
340 cmos_write(RTC_YEAR
, 0x11);
341 cmos_write(RTC_CENTURY
, 0x20);
342 cmos_write(RTC_MONTH
, 0x02);
343 cmos_write(RTC_DAY_OF_MONTH
, 0x02);
344 cmos_write(RTC_HOURS
, 0x02);
345 cmos_write(RTC_MINUTES
, 0x04);
346 cmos_write(RTC_SECONDS
, 0x58);
347 cmos_write(RTC_REG_A
, 0x26);
349 /* Since SET flag is still enabled, these are equality checks. */
350 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
351 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
352 g_assert_cmpint(cmos_read(RTC_SECONDS
), ==, 0x58);
353 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
354 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
355 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x11);
356 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x20);
358 /* Disable SET flag in Register B */
359 cmos_write(RTC_REG_B
, cmos_read(RTC_REG_B
) & ~REG_B_SET
);
361 g_assert_cmpint(cmos_read(RTC_HOURS
), ==, 0x02);
362 g_assert_cmpint(cmos_read(RTC_MINUTES
), ==, 0x04);
364 /* Since SET flag is disabled, this is an inequality check.
365 * We (reasonably) assume that no (sexagesimal) overflow occurs. */
366 g_assert_cmpint(cmos_read(RTC_SECONDS
), >=, 0x58);
367 g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH
), ==, 0x02);
368 g_assert_cmpint(cmos_read(RTC_MONTH
), ==, 0x02);
369 g_assert_cmpint(cmos_read(RTC_YEAR
), ==, 0x11);
370 g_assert_cmpint(cmos_read(RTC_CENTURY
), ==, 0x20);
373 int main(int argc
, char **argv
)
375 QTestState
*s
= NULL
;
378 g_test_init(&argc
, &argv
, NULL
);
380 s
= qtest_start("-display none -rtc clock=vm");
381 qtest_irq_intercept_in(s
, "ioapic");
383 qtest_add_func("/rtc/bcd/check-time", bcd_check_time
);
384 qtest_add_func("/rtc/dec/check-time", dec_check_time
);
385 qtest_add_func("/rtc/alarm-time", alarm_time
);
386 qtest_add_func("/rtc/set-year/20xx", set_year_20xx
);
387 qtest_add_func("/rtc/set-year/1980", set_year_1980
);
388 qtest_add_func("/rtc/register_b_set_flag", register_b_set_flag
);
389 qtest_add_func("/rtc/fuzz-registers", fuzz_registers
);