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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
// Copyright 2017 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::cmp::min;
use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;

use anyhow::anyhow;
use anyhow::Context;
use base::custom_serde::deserialize_seq_to_arr;
use base::custom_serde::serialize_arr;
use base::error;
use base::info;
use base::Event;
use base::EventToken;
use base::Timer;
use base::TimerTrait;
use base::Tube;
use base::WaitContext;
use base::WorkerThread;
use chrono::DateTime;
use chrono::Datelike;
use chrono::TimeZone;
use chrono::Timelike;
use chrono::Utc;
use metrics::log_metric;
use metrics::MetricEventType;
use serde::Deserialize;
use serde::Serialize;
use sync::Mutex;
use vm_control::VmResponse;

use crate::pci::CrosvmDeviceId;
use crate::BusAccessInfo;
use crate::BusDevice;
use crate::DeviceId;
use crate::IrqEdgeEvent;
use crate::Suspendable;

pub const RTC_IRQ: u8 = 8;

const INDEX_MASK: u8 = 0x7f;
const INDEX_OFFSET: u64 = 0x0;
const DATA_OFFSET: u64 = 0x1;
const DATA_LEN: usize = 128;

const RTC_REG_SEC: u8 = 0x0;
const RTC_REG_ALARM_SEC: u8 = 0x1;
const RTC_REG_MIN: u8 = 0x2;
const RTC_REG_ALARM_MIN: u8 = 0x3;
const RTC_REG_HOUR: u8 = 0x4;
const RTC_REG_ALARM_HOUR: u8 = 0x5;
const RTC_REG_WEEK_DAY: u8 = 0x6;
const RTC_REG_DAY: u8 = 0x7;
const RTC_REG_MONTH: u8 = 0x8;
const RTC_REG_YEAR: u8 = 0x9;
pub const RTC_REG_CENTURY: u8 = 0x32;
pub const RTC_REG_ALARM_DAY: u8 = 0x33;
pub const RTC_REG_ALARM_MONTH: u8 = 0x34;

const RTC_REG_B: u8 = 0x0b;
const RTC_REG_B_UNSUPPORTED: u8 = 0xdd;
const RTC_REG_B_24_HOUR_MODE: u8 = 0x02;
const RTC_REG_B_ALARM_ENABLE: u8 = 0x20;

const RTC_REG_C: u8 = 0x0c;
const RTC_REG_C_IRQF: u8 = 0x80;
const RTC_REG_C_AF: u8 = 0x20;

const RTC_REG_D: u8 = 0x0d;
const RTC_REG_D_VRT: u8 = 0x80; // RAM and time valid

pub type CmosNowFn = fn() -> DateTime<Utc>;

// Alarm state shared between Cmos and the alarm worker thread.
struct AlarmState {
    alarm: Timer,
    vm_control: Tube,
    irq: IrqEdgeEvent,
    armed_time: Instant,
    clear_evt: Option<Event>,
}

impl AlarmState {
    fn trigger_rtc_interrupt(&self) -> anyhow::Result<Event> {
        self.irq.trigger().context("failed to trigger irq")?;

        let elapsed = self.armed_time.elapsed().as_millis();
        log_metric(
            MetricEventType::RtcWakeup,
            elapsed.try_into().unwrap_or(i64::MAX),
        );

        let msg = vm_control::VmRequest::Rtc {
            clear_evt: Event::new().context("failed to create clear event")?,
        };

        // The Linux kernel expects wakeups to come via ACPI when ACPI is enabled. There's
        // no real way to determine that here, so just send this unconditionally.
        self.vm_control.send(&msg).context("send failed")?;

        let vm_control::VmRequest::Rtc { clear_evt } = msg else {
            unreachable!("message type failure");
        };

        match self.vm_control.recv().context("recv failed")? {
            VmResponse::Ok => Ok(clear_evt),
            resp => Err(anyhow!("unexpected rtc response: {:?}", resp)),
        }
    }
}

/// A CMOS/RTC device commonly seen on x86 I/O port 0x70/0x71.
#[derive(Serialize)]
pub struct Cmos {
    index: u8,
    #[serde(serialize_with = "serialize_arr")]
    data: [u8; DATA_LEN],
    #[serde(skip_serializing)] // skip serializing time function.
    now_fn: CmosNowFn,
    // alarm_time is re-loaded from data on deserialization, so there's
    // no need to explicitly serialize it.
    #[serde(skip_serializing)]
    alarm_time: Option<DateTime<Utc>>,
    // alarm_state fields are either constant across snapshotting or
    // reloaded from |data| on restore, so no need to serialize.
    #[serde(skip_serializing)]
    alarm_state: Arc<Mutex<AlarmState>>,
    #[serde(skip_serializing)] // skip serializing the worker thread
    worker: Option<WorkerThread<()>>,
}

impl Cmos {
    /// Constructs a CMOS/RTC device with initial data.
    /// `mem_below_4g` is the size of memory in bytes below the 32-bit gap.
    /// `mem_above_4g` is the size of memory in bytes above the 32-bit gap.
    /// `now_fn` is a function that returns the current date and time.
    pub fn new(
        mem_below_4g: u64,
        mem_above_4g: u64,
        now_fn: CmosNowFn,
        vm_control: Tube,
        irq: IrqEdgeEvent,
    ) -> anyhow::Result<Cmos> {
        let mut data = [0u8; DATA_LEN];

        data[0x0B] = RTC_REG_B_24_HOUR_MODE; // Status Register B: 24-hour mode

        // Extended memory from 16 MB to 4 GB in units of 64 KB
        let ext_mem = min(
            0xFFFF,
            mem_below_4g.saturating_sub(16 * 1024 * 1024) / (64 * 1024),
        );
        data[0x34] = ext_mem as u8;
        data[0x35] = (ext_mem >> 8) as u8;

        // High memory (> 4GB) in units of 64 KB
        let high_mem = min(0xFFFFFF, mem_above_4g / (64 * 1024));
        data[0x5b] = high_mem as u8;
        data[0x5c] = (high_mem >> 8) as u8;
        data[0x5d] = (high_mem >> 16) as u8;

        Ok(Cmos {
            index: 0,
            data,
            now_fn,
            alarm_time: None,
            alarm_state: Arc::new(Mutex::new(AlarmState {
                alarm: Timer::new().context("cmos timer")?,
                irq,
                vm_control,
                // Not actually armed, but simpler than wrapping with an Option.
                armed_time: Instant::now(),
                clear_evt: None,
            })),
            worker: None,
        })
    }

    fn spawn_worker(&mut self, alarm_state: Arc<Mutex<AlarmState>>) {
        self.worker = Some(WorkerThread::start("CMOS_alarm", move |kill_evt| {
            if let Err(e) = run_cmos_worker(alarm_state, kill_evt) {
                error!("Failed to spawn worker {:?}", e);
            }
        }));
    }

    fn set_alarm(&mut self) {
        let mut state = self.alarm_state.lock();
        if self.data[RTC_REG_B as usize] & RTC_REG_B_ALARM_ENABLE != 0 {
            let now = (self.now_fn)();
            let target = alarm_from_registers(now.year(), &self.data).and_then(|this_year| {
                // There is no year register for the alarm. If the alarm target has
                // already passed this year, then the next time it will occur is next
                // year.
                //
                // Note that there is something of a race condition here. If |now|
                // advances while the driver is configuring the alarm, then an alarm that
                // should only be one second in the future could become one year in the
                // future. Unfortunately there isn't anything in the rtc-cmos hardware
                // specification that lets us handle this race condition in the device, so
                // we just have to rely on the driver to deal with it.
                if this_year < now {
                    alarm_from_registers(now.year() + 1, &self.data)
                } else {
                    Some(this_year)
                }
            });
            if let Some(target) = target {
                if Some(target) != self.alarm_time {
                    self.alarm_time = Some(target);
                    state.armed_time = Instant::now();

                    let duration = target
                        .signed_duration_since(now)
                        .to_std()
                        .unwrap_or(Duration::new(0, 0));
                    if let Err(e) = state.alarm.reset_oneshot(duration) {
                        error!("Failed to set alarm {:?}", e);
                    }
                }
            }
        } else if self.alarm_time.take().is_some() {
            if let Err(e) = state.alarm.clear() {
                error!("Failed to clear alarm {:?}", e);
            }
            if let Some(clear_evt) = state.clear_evt.take() {
                if let Err(e) = clear_evt.signal() {
                    error!("failed to clear rtc pm signal {:?}", e);
                }
            }
        }

        let needs_worker = self.alarm_time.is_some();
        drop(state);

        if needs_worker && self.worker.is_none() {
            self.spawn_worker(self.alarm_state.clone());
        }
    }
}

fn run_cmos_worker(alarm_state: Arc<Mutex<AlarmState>>, kill_evt: Event) -> anyhow::Result<()> {
    #[derive(EventToken)]
    enum Token {
        Alarm,
        Kill,
    }

    let wait_ctx: WaitContext<Token> = WaitContext::build_with(&[
        (&alarm_state.lock().alarm, Token::Alarm),
        (&kill_evt, Token::Kill),
    ])
    .context("worker context failed")?;

    loop {
        let events = wait_ctx.wait().context("wait failed")?;
        let mut state = alarm_state.lock();
        for event in events.iter().filter(|e| e.is_readable) {
            match event.token {
                Token::Alarm => {
                    if state.alarm.mark_waited().context("timer ack failed")? {
                        continue;
                    }

                    match state.trigger_rtc_interrupt() {
                        Ok(clear_evt) => state.clear_evt = Some(clear_evt),
                        Err(e) => error!("Failed to send rtc {:?}", e),
                    }
                }
                Token::Kill => return Ok(()),
            }
        }
    }
}

fn from_bcd(v: u8) -> Option<u32> {
    let ones = (v & 0xf) as u32;
    let tens = (v >> 4) as u32;
    if ones < 10 && tens < 10 {
        Some(10 * tens + ones)
    } else {
        None
    }
}

fn alarm_from_registers(year: i32, data: &[u8; DATA_LEN]) -> Option<DateTime<Utc>> {
    Utc.with_ymd_and_hms(
        year,
        from_bcd(data[RTC_REG_ALARM_MONTH as usize])?,
        from_bcd(data[RTC_REG_ALARM_DAY as usize])?,
        from_bcd(data[RTC_REG_ALARM_HOUR as usize])?,
        from_bcd(data[RTC_REG_ALARM_MIN as usize])?,
        from_bcd(data[RTC_REG_ALARM_SEC as usize])?,
    )
    .single()
}

impl BusDevice for Cmos {
    fn device_id(&self) -> DeviceId {
        CrosvmDeviceId::Cmos.into()
    }

    fn debug_label(&self) -> String {
        "cmos".to_owned()
    }

    fn write(&mut self, info: BusAccessInfo, data: &[u8]) {
        if data.len() != 1 {
            return;
        }

        match info.offset {
            INDEX_OFFSET => self.index = data[0] & INDEX_MASK,
            DATA_OFFSET => {
                let mut data = data[0];
                if self.index == RTC_REG_B {
                    // The features which we don't support are:
                    //   0x80 (SET)  - disable clock updates (i.e. let guest configure the clock)
                    //   0x40 (PIE)  - enable periodic interrupts
                    //   0x10 (IUE)  - enable interrupts after clock updates
                    //   0x08 (SQWE) - enable square wave generation
                    //   0x04 (DM)   - use binary data format (instead of BCD)
                    //   0x01 (DSE)  - control daylight savings (we just do what the host does)
                    if data & RTC_REG_B_UNSUPPORTED != 0 {
                        info!(
                            "Ignoring unsupported bits: {:x}",
                            data & RTC_REG_B_UNSUPPORTED
                        );
                        data &= !RTC_REG_B_UNSUPPORTED;
                    }
                    if data & RTC_REG_B_24_HOUR_MODE == 0 {
                        info!("12-hour mode unsupported");
                        data |= RTC_REG_B_24_HOUR_MODE;
                    }
                }

                self.data[self.index as usize] = data;

                if self.index == RTC_REG_B {
                    self.set_alarm();
                }
            }
            o => panic!("bad write offset on CMOS device: {}", o),
        }
    }

    fn read(&mut self, info: BusAccessInfo, data: &mut [u8]) {
        fn to_bcd(v: u8) -> u8 {
            assert!(v < 100);
            ((v / 10) << 4) | (v % 10)
        }

        if data.len() != 1 {
            return;
        }

        data[0] = match info.offset {
            INDEX_OFFSET => self.index,
            DATA_OFFSET => {
                let now = (self.now_fn)();
                let seconds = now.second(); // 0..=59
                let minutes = now.minute(); // 0..=59
                let hours = now.hour(); // 0..=23 (24-hour mode only)
                let week_day = now.weekday().number_from_sunday(); // 1 (Sun) ..= 7 (Sat)
                let day = now.day(); // 1..=31
                let month = now.month(); // 1..=12
                let year = now.year();
                match self.index {
                    RTC_REG_SEC => to_bcd(seconds as u8),
                    RTC_REG_MIN => to_bcd(minutes as u8),
                    RTC_REG_HOUR => to_bcd(hours as u8),
                    RTC_REG_WEEK_DAY => to_bcd(week_day as u8),
                    RTC_REG_DAY => to_bcd(day as u8),
                    RTC_REG_MONTH => to_bcd(month as u8),
                    RTC_REG_YEAR => to_bcd((year % 100) as u8),
                    RTC_REG_CENTURY => to_bcd((year / 100) as u8),
                    RTC_REG_C => {
                        if self
                            .alarm_time
                            .map_or(false, |alarm_time| alarm_time <= now)
                        {
                            // Reading from RTC_REG_C resets interrupts, so clear the
                            // status bits. The IrqEdgeEvent is reset automatically.
                            self.alarm_time.take();
                            RTC_REG_C_IRQF | RTC_REG_C_AF
                        } else {
                            0
                        }
                    }
                    RTC_REG_D => RTC_REG_D_VRT,
                    _ => {
                        // self.index is always guaranteed to be in range via INDEX_MASK.
                        self.data[(self.index & INDEX_MASK) as usize]
                    }
                }
            }
            o => panic!("bad read offset on CMOS device: {}", o),
        }
    }
}

impl Suspendable for Cmos {
    fn snapshot(&mut self) -> anyhow::Result<serde_json::Value> {
        serde_json::to_value(self).context("failed to serialize Cmos")
    }

    fn restore(&mut self, data: serde_json::Value) -> anyhow::Result<()> {
        #[derive(Deserialize)]
        struct CmosIndex {
            index: u8,
            #[serde(deserialize_with = "deserialize_seq_to_arr")]
            data: [u8; DATA_LEN],
        }

        let deser: CmosIndex =
            serde_json::from_value(data).context("failed to deserialize Cmos")?;
        self.index = deser.index;
        self.data = deser.data;
        self.set_alarm();

        Ok(())
    }

    fn sleep(&mut self) -> anyhow::Result<()> {
        if let Some(worker) = self.worker.take() {
            worker.stop();
        }
        Ok(())
    }

    fn wake(&mut self) -> anyhow::Result<()> {
        self.spawn_worker(self.alarm_state.clone());
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::suspendable_tests;

    fn read_reg(cmos: &mut Cmos, reg: u8) -> u8 {
        // Write register number to INDEX_OFFSET (0).
        cmos.write(
            BusAccessInfo {
                offset: 0,
                address: 0x70,
                id: 0,
            },
            &[reg],
        );

        // Read register value back from DATA_OFFSET (1).

        let mut data = [0u8];
        cmos.read(
            BusAccessInfo {
                offset: 1,
                address: 0x71,
                id: 0,
            },
            &mut data,
        );
        data[0]
    }

    fn write_reg(cmos: &mut Cmos, reg: u8, val: u8) {
        // Write register number to INDEX_OFFSET (0).
        cmos.write(
            BusAccessInfo {
                offset: 0,
                address: 0x70,
                id: 0,
            },
            &[reg],
        );

        // Write register value to DATA_OFFSET (1).

        let data = [val];
        cmos.write(
            BusAccessInfo {
                offset: 1,
                address: 0x71,
                id: 0,
            },
            &data,
        );
    }

    fn timestamp_to_datetime(timestamp: i64) -> DateTime<Utc> {
        DateTime::from_timestamp(timestamp, 0).unwrap()
    }

    fn test_now_party_like_its_1999() -> DateTime<Utc> {
        // 1999-12-31T23:59:59+00:00
        timestamp_to_datetime(946684799)
    }

    fn test_now_y2k_compliant() -> DateTime<Utc> {
        // 2000-01-01T00:00:00+00:00
        timestamp_to_datetime(946684800)
    }

    fn test_now_2016_before_leap_second() -> DateTime<Utc> {
        // 2016-12-31T23:59:59+00:00
        timestamp_to_datetime(1483228799)
    }

    fn test_now_2017_after_leap_second() -> DateTime<Utc> {
        // 2017-01-01T00:00:00+00:00
        timestamp_to_datetime(1483228800)
    }

    fn new_cmos_for_test(now_fn: CmosNowFn) -> Cmos {
        let irq = IrqEdgeEvent::new().unwrap();
        Cmos::new(1024, 0, now_fn, Tube::pair().unwrap().0, irq).unwrap()
    }

    #[test]
    fn cmos_write_index() {
        let mut cmos = new_cmos_for_test(test_now_party_like_its_1999);
        // Write index.
        cmos.write(
            BusAccessInfo {
                offset: 0,
                address: 0x71,
                id: 0,
            },
            &[0x41],
        );
        assert_eq!(cmos.index, 0x41);
    }

    #[test]
    fn cmos_write_data() {
        let mut cmos = new_cmos_for_test(test_now_party_like_its_1999);
        // Write data 0x01 at index 0x41.
        cmos.write(
            BusAccessInfo {
                offset: 0,
                address: 0x71,
                id: 0,
            },
            &[0x41],
        );
        cmos.write(
            BusAccessInfo {
                offset: 1,
                address: 0x71,
                id: 0,
            },
            &[0x01],
        );
        assert_eq!(cmos.data[0x41], 0x01);
    }

    fn modify_device(cmos: &mut Cmos) {
        let info_index = BusAccessInfo {
            offset: 0,
            address: 0x71,
            id: 0,
        };

        let info_data = BusAccessInfo {
            offset: 1,
            address: 0x71,
            id: 0,
        };
        // change index to 0x42.
        cmos.write(info_index, &[0x42]);
        cmos.write(info_data, &[0x01]);
    }

    #[test]
    fn cmos_date_time_1999() {
        let mut cmos = new_cmos_for_test(test_now_party_like_its_1999);
        assert_eq!(read_reg(&mut cmos, 0x00), 0x59); // seconds
        assert_eq!(read_reg(&mut cmos, 0x02), 0x59); // minutes
        assert_eq!(read_reg(&mut cmos, 0x04), 0x23); // hours
        assert_eq!(read_reg(&mut cmos, 0x06), 0x06); // day of week
        assert_eq!(read_reg(&mut cmos, 0x07), 0x31); // day of month
        assert_eq!(read_reg(&mut cmos, 0x08), 0x12); // month
        assert_eq!(read_reg(&mut cmos, 0x09), 0x99); // year
        assert_eq!(read_reg(&mut cmos, 0x32), 0x19); // century
    }

    #[test]
    fn cmos_date_time_2000() {
        let mut cmos = new_cmos_for_test(test_now_y2k_compliant);
        assert_eq!(read_reg(&mut cmos, 0x00), 0x00); // seconds
        assert_eq!(read_reg(&mut cmos, 0x02), 0x00); // minutes
        assert_eq!(read_reg(&mut cmos, 0x04), 0x00); // hours
        assert_eq!(read_reg(&mut cmos, 0x06), 0x07); // day of week
        assert_eq!(read_reg(&mut cmos, 0x07), 0x01); // day of month
        assert_eq!(read_reg(&mut cmos, 0x08), 0x01); // month
        assert_eq!(read_reg(&mut cmos, 0x09), 0x00); // year
        assert_eq!(read_reg(&mut cmos, 0x32), 0x20); // century
    }

    #[test]
    fn cmos_date_time_before_leap_second() {
        let mut cmos = new_cmos_for_test(test_now_2016_before_leap_second);
        assert_eq!(read_reg(&mut cmos, 0x00), 0x59); // seconds
        assert_eq!(read_reg(&mut cmos, 0x02), 0x59); // minutes
        assert_eq!(read_reg(&mut cmos, 0x04), 0x23); // hours
        assert_eq!(read_reg(&mut cmos, 0x06), 0x07); // day of week
        assert_eq!(read_reg(&mut cmos, 0x07), 0x31); // day of month
        assert_eq!(read_reg(&mut cmos, 0x08), 0x12); // month
        assert_eq!(read_reg(&mut cmos, 0x09), 0x16); // year
        assert_eq!(read_reg(&mut cmos, 0x32), 0x20); // century
    }

    #[test]
    fn cmos_date_time_after_leap_second() {
        let mut cmos = new_cmos_for_test(test_now_2017_after_leap_second);
        assert_eq!(read_reg(&mut cmos, 0x00), 0x00); // seconds
        assert_eq!(read_reg(&mut cmos, 0x02), 0x00); // minutes
        assert_eq!(read_reg(&mut cmos, 0x04), 0x00); // hours
        assert_eq!(read_reg(&mut cmos, 0x06), 0x01); // day of week
        assert_eq!(read_reg(&mut cmos, 0x07), 0x01); // day of month
        assert_eq!(read_reg(&mut cmos, 0x08), 0x01); // month
        assert_eq!(read_reg(&mut cmos, 0x09), 0x17); // year
        assert_eq!(read_reg(&mut cmos, 0x32), 0x20); // century
    }

    #[test]
    fn cmos_alarm() {
        // 2000-01-02T03:04:05+00:00
        let now_fn = || timestamp_to_datetime(946782245);
        let mut cmos = new_cmos_for_test(now_fn);

        // A date later this year
        write_reg(&mut cmos, 0x01, 0x06); // seconds
        write_reg(&mut cmos, 0x03, 0x05); // minutes
        write_reg(&mut cmos, 0x05, 0x04); // hours
        write_reg(&mut cmos, 0x33, 0x03); // day of month
        write_reg(&mut cmos, 0x34, 0x02); // month
        write_reg(&mut cmos, 0x0b, 0x20); // RTC_REG_B_ALARM_ENABLE
                                          // 2000-02-03T04:05:06+00:00
        assert_eq!(cmos.alarm_time, Some(timestamp_to_datetime(949550706)));

        // A date (one year - one second) in the future
        write_reg(&mut cmos, 0x01, 0x04); // seconds
        write_reg(&mut cmos, 0x03, 0x04); // minutes
        write_reg(&mut cmos, 0x05, 0x03); // hours
        write_reg(&mut cmos, 0x33, 0x02); // day of month
        write_reg(&mut cmos, 0x34, 0x01); // month
        write_reg(&mut cmos, 0x0b, 0x20); // RTC_REG_B_ALARM_ENABLE
                                          // 2001-01-02T03:04:04+00:00
        assert_eq!(cmos.alarm_time, Some(timestamp_to_datetime(978404644)));

        // The current time
        write_reg(&mut cmos, 0x01, 0x05); // seconds
        write_reg(&mut cmos, 0x03, 0x04); // minutes
        write_reg(&mut cmos, 0x05, 0x03); // hours
        write_reg(&mut cmos, 0x33, 0x02); // day of month
        write_reg(&mut cmos, 0x34, 0x01); // month
        write_reg(&mut cmos, 0x0b, 0x20); // RTC_REG_B_ALARM_ENABLE
        assert_eq!(cmos.alarm_time, Some(timestamp_to_datetime(946782245)));
        assert_eq!(read_reg(&mut cmos, 0x0c), 0xa0); // RTC_REG_C_IRQF | RTC_REG_C_AF
        assert_eq!(cmos.alarm_time, None);
        assert_eq!(read_reg(&mut cmos, 0x0c), 0);

        // Invalid BCD
        write_reg(&mut cmos, 0x01, 0xa0); // seconds
        write_reg(&mut cmos, 0x0b, 0x20); // RTC_REG_B_ALARM_ENABLE
        assert_eq!(cmos.alarm_time, None);
    }

    #[test]
    fn cmos_reg_d() {
        let mut cmos = new_cmos_for_test(test_now_party_like_its_1999);
        assert_eq!(read_reg(&mut cmos, 0x0d), 0x80) // RAM and time are valid
    }

    #[test]
    fn cmos_snapshot_restore() -> anyhow::Result<()> {
        // time function doesn't matter in this case.
        let mut cmos = new_cmos_for_test(test_now_party_like_its_1999);

        let info_index = BusAccessInfo {
            offset: 0,
            address: 0x71,
            id: 0,
        };

        let info_data = BusAccessInfo {
            offset: 1,
            address: 0x71,
            id: 0,
        };

        // change index to 0x41.
        cmos.write(info_index, &[0x41]);
        cmos.write(info_data, &[0x01]);

        let snap = cmos.snapshot().context("failed to snapshot Cmos")?;

        // change index to 0x42.
        cmos.write(info_index, &[0x42]);
        cmos.write(info_data, &[0x01]);

        // Restore Cmos.
        cmos.restore(snap).context("failed to restore Cmos")?;

        // after restore, the index should be 0x41, which was the index before snapshot was taken.
        assert_eq!(cmos.index, 0x41);
        assert_eq!(cmos.data[0x41], 0x01);
        assert_ne!(cmos.data[0x42], 0x01);
        Ok(())
    }

    #[test]
    fn cmos_sleep_wake() {
        // 2000-01-02T03:04:05+00:00
        let irq = IrqEdgeEvent::new().unwrap();
        let now_fn = || timestamp_to_datetime(946782245);
        let mut cmos = Cmos::new(1024, 0, now_fn, Tube::pair().unwrap().0, irq).unwrap();

        // A date later this year
        write_reg(&mut cmos, 0x01, 0x06); // seconds
        write_reg(&mut cmos, 0x03, 0x05); // minutes
        write_reg(&mut cmos, 0x05, 0x04); // hours
        write_reg(&mut cmos, 0x33, 0x03); // day of month
        write_reg(&mut cmos, 0x34, 0x02); // month
        write_reg(&mut cmos, 0x0b, 0x20); // RTC_REG_B_ALARM_ENABLE
                                          // 2000-02-03T04:05:06+00:00
        assert_eq!(cmos.alarm_time, Some(timestamp_to_datetime(949550706)));
        assert!(cmos.worker.is_some());

        cmos.sleep().unwrap();
        assert!(cmos.worker.is_none());

        cmos.wake().unwrap();
        assert!(cmos.worker.is_some());
    }

    suspendable_tests!(
        cmos1999,
        new_cmos_for_test(test_now_party_like_its_1999),
        modify_device
    );
    suspendable_tests!(
        cmos2k,
        new_cmos_for_test(test_now_y2k_compliant),
        modify_device
    );
    suspendable_tests!(
        cmos2016,
        new_cmos_for_test(test_now_2016_before_leap_second),
        modify_device
    );
    suspendable_tests!(
        cmos2017,
        new_cmos_for_test(test_now_2017_after_leap_second),
        modify_device
    );
}