1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
/*
* QTest testcase for the MC146818 real-time clock
*
* Copyright IBM, Corp. 2012
*
* Authors:
* Anthony Liguori <aliguori@us.ibm.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*
*/
#include "qemu/osdep.h"
#include "libqtest-single.h"
#include "qemu/timer.h"
#include "hw/rtc/mc146818rtc.h"
#include "hw/rtc/mc146818rtc_regs.h"
#define UIP_HOLD_LENGTH (8 * NANOSECONDS_PER_SECOND / 32768)
static uint8_t base = 0x70;
static int bcd2dec(int value)
{
return (((value >> 4) & 0x0F) * 10) + (value & 0x0F);
}
static uint8_t cmos_read(uint8_t reg)
{
outb(base + 0, reg);
return inb(base + 1);
}
static void cmos_write(uint8_t reg, uint8_t val)
{
outb(base + 0, reg);
outb(base + 1, val);
}
static int tm_cmp(struct tm *lhs, struct tm *rhs)
{
time_t a, b;
struct tm d1, d2;
memcpy(&d1, lhs, sizeof(d1));
memcpy(&d2, rhs, sizeof(d2));
a = mktime(&d1);
b = mktime(&d2);
if (a < b) {
return -1;
} else if (a > b) {
return 1;
}
return 0;
}
#if 0
static void print_tm(struct tm *tm)
{
printf("%04d-%02d-%02d %02d:%02d:%02d\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_gmtoff);
}
#endif
static void cmos_get_date_time(struct tm *date)
{
int base_year = 2000, hour_offset;
int sec, min, hour, mday, mon, year;
time_t ts;
struct tm dummy;
sec = cmos_read(RTC_SECONDS);
min = cmos_read(RTC_MINUTES);
hour = cmos_read(RTC_HOURS);
mday = cmos_read(RTC_DAY_OF_MONTH);
mon = cmos_read(RTC_MONTH);
year = cmos_read(RTC_YEAR);
if ((cmos_read(RTC_REG_B) & REG_B_DM) == 0) {
sec = bcd2dec(sec);
min = bcd2dec(min);
hour = bcd2dec(hour);
mday = bcd2dec(mday);
mon = bcd2dec(mon);
year = bcd2dec(year);
hour_offset = 80;
} else {
hour_offset = 0x80;
}
if ((cmos_read(0x0B) & REG_B_24H) == 0) {
if (hour >= hour_offset) {
hour -= hour_offset;
hour += 12;
}
}
ts = time(NULL);
localtime_r(&ts, &dummy);
date->tm_isdst = dummy.tm_isdst;
date->tm_sec = sec;
date->tm_min = min;
date->tm_hour = hour;
date->tm_mday = mday;
date->tm_mon = mon - 1;
date->tm_year = base_year + year - 1900;
#ifndef __sun__
date->tm_gmtoff = 0;
#endif
ts = mktime(date);
}
static void check_time(int wiggle)
{
struct tm start, date[4], end;
struct tm *datep;
time_t ts;
/*
* This check assumes a few things. First, we cannot guarantee that we get
* a consistent reading from the wall clock because we may hit an edge of
* the clock while reading. To work around this, we read four clock readings
* such that at least two of them should match. We need to assume that one
* reading is corrupt so we need four readings to ensure that we have at
* least two consecutive identical readings
*
* It's also possible that we'll cross an edge reading the host clock so
* simply check to make sure that the clock reading is within the period of
* when we expect it to be.
*/
ts = time(NULL);
gmtime_r(&ts, &start);
cmos_get_date_time(&date[0]);
cmos_get_date_time(&date[1]);
cmos_get_date_time(&date[2]);
cmos_get_date_time(&date[3]);
ts = time(NULL);
gmtime_r(&ts, &end);
if (tm_cmp(&date[0], &date[1]) == 0) {
datep = &date[0];
} else if (tm_cmp(&date[1], &date[2]) == 0) {
datep = &date[1];
} else if (tm_cmp(&date[2], &date[3]) == 0) {
datep = &date[2];
} else {
g_assert_not_reached();
}
if (!(tm_cmp(&start, datep) <= 0 && tm_cmp(datep, &end) <= 0)) {
long t, s;
start.tm_isdst = datep->tm_isdst;
t = (long)mktime(datep);
s = (long)mktime(&start);
if (t < s) {
g_test_message("RTC is %ld second(s) behind wall-clock", (s - t));
} else {
g_test_message("RTC is %ld second(s) ahead of wall-clock", (t - s));
}
g_assert_cmpint(ABS(t - s), <=, wiggle);
}
}
static int wiggle = 2;
static void set_year_20xx(void)
{
/* Set BCD mode */
cmos_write(RTC_REG_B, REG_B_24H);
cmos_write(RTC_REG_A, 0x76);
cmos_write(RTC_YEAR, 0x11);
cmos_write(RTC_CENTURY, 0x20);
cmos_write(RTC_MONTH, 0x02);
cmos_write(RTC_DAY_OF_MONTH, 0x02);
cmos_write(RTC_HOURS, 0x02);
cmos_write(RTC_MINUTES, 0x04);
cmos_write(RTC_SECONDS, 0x58);
cmos_write(RTC_REG_A, 0x26);
g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04);
g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58);
g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x11);
g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20);
if (sizeof(time_t) == 4) {
return;
}
/* Set a date in 2080 to ensure there is no year-2038 overflow. */
cmos_write(RTC_REG_A, 0x76);
cmos_write(RTC_YEAR, 0x80);
cmos_write(RTC_REG_A, 0x26);
g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04);
g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58);
g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x80);
g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20);
cmos_write(RTC_REG_A, 0x76);
cmos_write(RTC_YEAR, 0x11);
cmos_write(RTC_REG_A, 0x26);
g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04);
g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58);
g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x11);
g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20);
}
static void set_year_1980(void)
{
/* Set BCD mode */
cmos_write(RTC_REG_B, REG_B_24H);
cmos_write(RTC_REG_A, 0x76);
cmos_write(RTC_YEAR, 0x80);
cmos_write(RTC_CENTURY, 0x19);
cmos_write(RTC_MONTH, 0x02);
cmos_write(RTC_DAY_OF_MONTH, 0x02);
cmos_write(RTC_HOURS, 0x02);
cmos_write(RTC_MINUTES, 0x04);
cmos_write(RTC_SECONDS, 0x58);
cmos_write(RTC_REG_A, 0x26);
g_assert_cmpint(cmos_read(RTC_HOURS), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_MINUTES), ==, 0x04);
g_assert_cmpint(cmos_read(RTC_SECONDS), >=, 0x58);
g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_MONTH), ==, 0x02);
g_assert_cmpint(cmos_read(RTC_YEAR), ==, 0x80);
g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x19);
}
static void bcd_check_time(void)
{
/* Set BCD mode */
cmos_write(RTC_REG_B, REG_B_24H);
check_time(wiggle);
}
static void dec_check_time(void)
{
/* Set DEC mode */
cmos_write(RTC_REG_B, REG_B_24H | REG_B_DM);
check_time(wiggle);
}
static void alarm_time(void)
{
struct tm now;
time_t ts;
int i;
ts = time(NULL);
gmtime_r(&ts, &now);
/* set DEC mode */
cmos_write(RTC_REG_B, REG_B_24H | REG_B_DM);
g_assert(!get_irq(RTC_ISA_IRQ));
cmos_read(RTC_REG_C);
now.tm_sec = (now.tm_sec + 2) % 60;
cmos_write(RTC_SECONDS_ALARM, now.tm_sec);
cmos_write(RTC_MINUTES_ALARM, RTC_ALARM_DONT_CARE);
cmos_write(RTC_HOURS_ALARM, RTC_ALARM_DONT_CARE);
cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_AIE);
for (i = 0; i < 2 + wiggle; i++) {
if (get_irq(RTC_ISA_IRQ)) {
break;
}
clock_step(NANOSECONDS_PER_SECOND);
}
g_assert(get_irq(RTC_ISA_IRQ));
g_assert((cmos_read(RTC_REG_C) & REG_C_AF) != 0);
g_assert(cmos_read(RTC_REG_C) == 0);
}
static void set_time_regs(int h, int m, int s)
{
cmos_write(RTC_HOURS, h);
cmos_write(RTC_MINUTES, m);
cmos_write(RTC_SECONDS, s);
}
static void set_time(int mode, int h, int m, int s)
{
cmos_write(RTC_REG_B, mode);
cmos_write(RTC_REG_A, 0x76);
set_time_regs(h, m, s);
cmos_write(RTC_REG_A, 0x26);
}
static void set_datetime_bcd(int h, int min, int s, int d, int m, int y)
{
cmos_write(RTC_HOURS, h);
cmos_write(RTC_MINUTES, min);
cmos_write(RTC_SECONDS, s);
cmos_write(RTC_YEAR, y & 0xFF);
cmos_write(RTC_CENTURY, y >> 8);
cmos_write(RTC_MONTH, m);
cmos_write(RTC_DAY_OF_MONTH, d);
}
static void set_datetime_dec(int h, int min, int s, int d, int m, int y)
{
cmos_write(RTC_HOURS, h);
cmos_write(RTC_MINUTES, min);
cmos_write(RTC_SECONDS, s);
cmos_write(RTC_YEAR, y % 100);
cmos_write(RTC_CENTURY, y / 100);
cmos_write(RTC_MONTH, m);
cmos_write(RTC_DAY_OF_MONTH, d);
}
static void set_datetime(int mode, int h, int min, int s, int d, int m, int y)
{
cmos_write(RTC_REG_B, mode);
cmos_write(RTC_REG_A, 0x76);
if (mode & REG_B_DM) {
set_datetime_dec(h, min, s, d, m, y);
} else {
set_datetime_bcd(h, min, s, d, m, y);
}
cmos_write(RTC_REG_A, 0x26);
}
#define assert_time(h, m, s) \
do { \
g_assert_cmpint(cmos_read(RTC_HOURS), ==, h); \
g_assert_cmpint(cmos_read(RTC_MINUTES), ==, m); \
g_assert_cmpint(cmos_read(RTC_SECONDS), ==, s); \
} while(0)
#define assert_datetime_bcd(h, min, s, d, m, y) \
do { \
g_assert_cmpint(cmos_read(RTC_HOURS), ==, h); \
g_assert_cmpint(cmos_read(RTC_MINUTES), ==, min); \
g_assert_cmpint(cmos_read(RTC_SECONDS), ==, s); \
g_assert_cmpint(cmos_read(RTC_DAY_OF_MONTH), ==, d); \
g_assert_cmpint(cmos_read(RTC_MONTH), ==, m); \
g_assert_cmpint(cmos_read(RTC_YEAR), ==, (y & 0xFF)); \
g_assert_cmpint(cmos_read(RTC_CENTURY), ==, (y >> 8)); \
} while(0)
static void basic_12h_bcd(void)
{
/* set BCD 12 hour mode */
set_time(0, 0x81, 0x59, 0x00);
clock_step(1000000000LL);
assert_time(0x81, 0x59, 0x01);
clock_step(59000000000LL);
assert_time(0x82, 0x00, 0x00);
/* test BCD wraparound */
set_time(0, 0x09, 0x59, 0x59);
clock_step(60000000000LL);
assert_time(0x10, 0x00, 0x59);
/* 12 AM -> 1 AM */
set_time(0, 0x12, 0x59, 0x59);
clock_step(1000000000LL);
assert_time(0x01, 0x00, 0x00);
/* 12 PM -> 1 PM */
set_time(0, 0x92, 0x59, 0x59);
clock_step(1000000000LL);
assert_time(0x81, 0x00, 0x00);
/* 11 AM -> 12 PM */
set_time(0, 0x11, 0x59, 0x59);
clock_step(1000000000LL);
assert_time(0x92, 0x00, 0x00);
/* TODO: test day wraparound */
/* 11 PM -> 12 AM */
set_time(0, 0x91, 0x59, 0x59);
clock_step(1000000000LL);
assert_time(0x12, 0x00, 0x00);
/* TODO: test day wraparound */
}
static void basic_12h_dec(void)
{
/* set decimal 12 hour mode */
set_time(REG_B_DM, 0x81, 59, 0);
clock_step(1000000000LL);
assert_time(0x81, 59, 1);
clock_step(59000000000LL);
assert_time(0x82, 0, 0);
/* 12 PM -> 1 PM */
set_time(REG_B_DM, 0x8c, 59, 59);
clock_step(1000000000LL);
assert_time(0x81, 0, 0);
/* 12 AM -> 1 AM */
set_time(REG_B_DM, 0x0c, 59, 59);
clock_step(1000000000LL);
assert_time(0x01, 0, 0);
/* 11 AM -> 12 PM */
set_time(REG_B_DM, 0x0b, 59, 59);
clock_step(1000000000LL);
assert_time(0x8c, 0, 0);
/* 11 PM -> 12 AM */
set_time(REG_B_DM, 0x8b, 59, 59);
clock_step(1000000000LL);
assert_time(0x0c, 0, 0);
/* TODO: test day wraparound */
}
static void basic_24h_bcd(void)
{
/* set BCD 24 hour mode */
set_time(REG_B_24H, 0x09, 0x59, 0x00);
clock_step(1000000000LL);
assert_time(0x09, 0x59, 0x01);
clock_step(59000000000LL);
assert_time(0x10, 0x00, 0x00);
/* test BCD wraparound */
set_time(REG_B_24H, 0x09, 0x59, 0x00);
clock_step(60000000000LL);
assert_time(0x10, 0x00, 0x00);
/* TODO: test day wraparound */
set_time(REG_B_24H, 0x23, 0x59, 0x00);
clock_step(60000000000LL);
assert_time(0x00, 0x00, 0x00);
}
static void basic_24h_dec(void)
{
/* set decimal 24 hour mode */
set_time(REG_B_24H | REG_B_DM, 9, 59, 0);
clock_step(1000000000LL);
assert_time(9, 59, 1);
clock_step(59000000000LL);
assert_time(10, 0, 0);
/* test BCD wraparound */
set_time(REG_B_24H | REG_B_DM, 9, 59, 0);
clock_step(60000000000LL);
assert_time(10, 0, 0);
/* TODO: test day wraparound */
set_time(REG_B_24H | REG_B_DM, 23, 59, 0);
clock_step(60000000000LL);
assert_time(0, 0, 0);
}
static void am_pm_alarm(void)
{
cmos_write(RTC_MINUTES_ALARM, 0xC0);
cmos_write(RTC_SECONDS_ALARM, 0xC0);
/* set BCD 12 hour mode */
cmos_write(RTC_REG_B, 0);
/* Set time and alarm hour. */
cmos_write(RTC_REG_A, 0x76);
cmos_write(RTC_HOURS_ALARM, 0x82);
cmos_write(RTC_HOURS, 0x81);
cmos_write(RTC_MINUTES, 0x59);
cmos_write(RTC_SECONDS, 0x00);
cmos_read(RTC_REG_C);
cmos_write(RTC_REG_A, 0x26);
/* Check that alarm triggers when AM/PM is set. */
clock_step(60000000000LL);
g_assert(cmos_read(RTC_HOURS) == 0x82);
g_assert((cmos_read(RTC_REG_C) & REG_C_AF) != 0);
/*
* Each of the following two tests takes over 60 seconds due to the time
* needed to report the PIT interrupts. Unfortunately, our PIT device
* model keeps counting even when GATE=0, so we cannot simply disable
* it in main().
*/
if (g_test_quick()) {
return;
}
/* set DEC 12 hour mode */
cmos_write(RTC_REG_B, REG_B_DM);
/* Set time and alarm hour. */
cmos_write(RTC_REG_A, 0x76);
cmos_write(RTC_HOURS_ALARM, 0x82);
cmos_write(RTC_HOURS, 3);
cmos_write(RTC_MINUTES, 0);
cmos_write(RTC_SECONDS, 0);
cmos_read(RTC_REG_C);
cmos_write(RTC_REG_A, 0x26);
/* Check that alarm triggers. */
clock_step(3600 * 11 * 1000000000LL);
g_assert(cmos_read(RTC_HOURS) == 0x82);
g_assert((cmos_read(RTC_REG_C) & REG_C_AF) != 0);
/* Same as above, with inverted HOURS and HOURS_ALARM. */
cmos_write(RTC_REG_A, 0x76);
cmos_write(RTC_HOURS_ALARM, 2);
cmos_write(RTC_HOURS, 3);
cmos_write(RTC_MINUTES, 0);
cmos_write(RTC_SECONDS, 0);
cmos_read(RTC_REG_C);
cmos_write(RTC_REG_A, 0x26);
/* Check that alarm does not trigger if hours differ only by AM/PM. */
clock_step(3600 * 11 * 1000000000LL);
g_assert(cmos_read(RTC_HOURS) == 0x82);
g_assert((cmos_read(RTC_REG_C) & REG_C_AF) == 0);
}
/* success if no crash or abort */
static void fuzz_registers(void)
{
unsigned int i;
for (i = 0; i < 1000; i++) {
uint8_t reg, val;
reg = (uint8_t)g_test_rand_int_range(0, 16);
val = (uint8_t)g_test_rand_int_range(0, 256);
cmos_write(reg, val);
cmos_read(reg);
}
}
static void register_b_set_flag(void)
{
if (cmos_read(RTC_REG_A) & REG_A_UIP) {
clock_step(UIP_HOLD_LENGTH + NANOSECONDS_PER_SECOND / 5);
}
g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);
/* Enable binary-coded decimal (BCD) mode and SET flag in Register B*/
cmos_write(RTC_REG_B, REG_B_24H | REG_B_SET);
set_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
/* Since SET flag is still enabled, time does not advance. */
clock_step(1000000000LL);
assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
/* Disable SET flag in Register B */
cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_SET);
assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
/* Since SET flag is disabled, the clock now advances. */
clock_step(1000000000LL);
assert_datetime_bcd(0x02, 0x04, 0x59, 0x02, 0x02, 0x2011);
}
static void divider_reset(void)
{
/* Enable binary-coded decimal (BCD) mode in Register B*/
cmos_write(RTC_REG_B, REG_B_24H);
/* Enter divider reset */
cmos_write(RTC_REG_A, 0x76);
set_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
/* Since divider reset flag is still enabled, these are equality checks. */
clock_step(1000000000LL);
assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
/* The first update ends 500 ms after divider reset */
cmos_write(RTC_REG_A, 0x26);
clock_step(500000000LL - UIP_HOLD_LENGTH - 1);
g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);
assert_datetime_bcd(0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
clock_step(1);
g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, !=, 0);
clock_step(UIP_HOLD_LENGTH);
g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);
assert_datetime_bcd(0x02, 0x04, 0x59, 0x02, 0x02, 0x2011);
}
static void uip_stuck(void)
{
set_datetime(REG_B_24H, 0x02, 0x04, 0x58, 0x02, 0x02, 0x2011);
/* The first update ends 500 ms after divider reset */
(void)cmos_read(RTC_REG_C);
clock_step(500000000LL);
g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);
assert_datetime_bcd(0x02, 0x04, 0x59, 0x02, 0x02, 0x2011);
/* UF is now set. */
cmos_write(RTC_HOURS_ALARM, 0x02);
cmos_write(RTC_MINUTES_ALARM, 0xC0);
cmos_write(RTC_SECONDS_ALARM, 0xC0);
/* Because the alarm will fire soon, reading register A will latch UIP. */
clock_step(1000000000LL - UIP_HOLD_LENGTH / 2);
g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, !=, 0);
/* Move the alarm far away. This must not cause UIP to remain stuck! */
cmos_write(RTC_HOURS_ALARM, 0x03);
clock_step(UIP_HOLD_LENGTH);
g_assert_cmpint(cmos_read(RTC_REG_A) & REG_A_UIP, ==, 0);
}
#define RTC_PERIOD_CODE1 13 /* 8 Hz */
#define RTC_PERIOD_CODE2 15 /* 2 Hz */
#define RTC_PERIOD_TEST_NR 50
static uint64_t wait_periodic_interrupt(uint64_t real_time)
{
while (!get_irq(RTC_ISA_IRQ)) {
real_time = clock_step_next();
}
g_assert((cmos_read(RTC_REG_C) & REG_C_PF) != 0);
return real_time;
}
static void periodic_timer(void)
{
int i;
uint64_t period_clocks, period_time, start_time, real_time;
/* disable all interrupts. */
cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) &
~(REG_B_PIE | REG_B_AIE | REG_B_UIE));
cmos_write(RTC_REG_A, RTC_PERIOD_CODE1);
/* enable periodic interrupt after properly configure the period. */
cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_PIE);
start_time = real_time = clock_step_next();
for (i = 0; i < RTC_PERIOD_TEST_NR; i++) {
cmos_write(RTC_REG_A, RTC_PERIOD_CODE1);
real_time = wait_periodic_interrupt(real_time);
cmos_write(RTC_REG_A, RTC_PERIOD_CODE2);
real_time = wait_periodic_interrupt(real_time);
}
period_clocks = periodic_period_to_clock(RTC_PERIOD_CODE1) +
periodic_period_to_clock(RTC_PERIOD_CODE2);
period_clocks *= RTC_PERIOD_TEST_NR;
period_time = periodic_clock_to_ns(period_clocks);
real_time -= start_time;
g_assert_cmpint(ABS((int64_t)(real_time - period_time)), <=,
NANOSECONDS_PER_SECOND * 0.5);
}
int main(int argc, char **argv)
{
QTestState *s = NULL;
int ret;
g_test_init(&argc, &argv, NULL);
s = qtest_start("-rtc clock=vm");
qtest_irq_intercept_in(s, "ioapic");
qtest_add_func("/rtc/check-time/bcd", bcd_check_time);
qtest_add_func("/rtc/check-time/dec", dec_check_time);
qtest_add_func("/rtc/alarm/interrupt", alarm_time);
qtest_add_func("/rtc/alarm/am-pm", am_pm_alarm);
qtest_add_func("/rtc/basic/dec-24h", basic_24h_dec);
qtest_add_func("/rtc/basic/bcd-24h", basic_24h_bcd);
qtest_add_func("/rtc/basic/dec-12h", basic_12h_dec);
qtest_add_func("/rtc/basic/bcd-12h", basic_12h_bcd);
qtest_add_func("/rtc/set-year/20xx", set_year_20xx);
qtest_add_func("/rtc/set-year/1980", set_year_1980);
qtest_add_func("/rtc/update/register_b_set_flag", register_b_set_flag);
qtest_add_func("/rtc/update/divider-reset", divider_reset);
qtest_add_func("/rtc/update/uip-stuck", uip_stuck);
qtest_add_func("/rtc/misc/fuzz-registers", fuzz_registers);
qtest_add_func("/rtc/periodic/interrupt", periodic_timer);
ret = g_test_run();
if (s) {
qtest_quit(s);
}
return ret;
}
|