xref: /cloud-hypervisor/devices/src/legacy/cmos.rs (revision f67b3f79ea19c9a66e04074cbbf5d292f6529e43)
1 // Copyright 2017 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use libc::{clock_gettime, gmtime_r, timespec, tm, CLOCK_REALTIME};
6 use std::cmp::min;
7 use std::mem;
8 use std::sync::{Arc, Barrier};
9 use vm_device::BusDevice;
10 
11 // https://github.com/rust-lang/libc/issues/1848
12 #[cfg_attr(target_env = "musl", allow(deprecated))]
13 use libc::time_t;
14 
15 const INDEX_MASK: u8 = 0x7f;
16 const INDEX_OFFSET: u64 = 0x0;
17 const DATA_OFFSET: u64 = 0x1;
18 const DATA_LEN: usize = 128;
19 
20 /// A CMOS/RTC device commonly seen on x86 I/O port 0x70/0x71.
21 pub struct Cmos {
22     index: u8,
23     data: [u8; DATA_LEN],
24 }
25 
26 impl Cmos {
27     /// Constructs a CMOS/RTC device with initial data.
28     /// `mem_below_4g` is the size of memory in bytes below the 32-bit gap.
29     /// `mem_above_4g` is the size of memory in bytes above the 32-bit gap.
30     pub fn new(mem_below_4g: u64, mem_above_4g: u64) -> Cmos {
31         let mut data = [0u8; DATA_LEN];
32 
33         // Extended memory from 16 MB to 4 GB in units of 64 KB
34         let ext_mem = min(
35             0xFFFF,
36             mem_below_4g.saturating_sub(16 * 1024 * 1024) / (64 * 1024),
37         );
38         data[0x34] = ext_mem as u8;
39         data[0x35] = (ext_mem >> 8) as u8;
40 
41         // High memory (> 4GB) in units of 64 KB
42         let high_mem = min(0x00FF_FFFF, mem_above_4g / (64 * 1024));
43         data[0x5b] = high_mem as u8;
44         data[0x5c] = (high_mem >> 8) as u8;
45         data[0x5d] = (high_mem >> 16) as u8;
46 
47         Cmos { index: 0, data }
48     }
49 }
50 
51 impl BusDevice for Cmos {
52     fn write(&mut self, _base: u64, offset: u64, data: &[u8]) -> Option<Arc<Barrier>> {
53         if data.len() != 1 {
54             warn!("Invalid write size on CMOS device: {}", data.len());
55             return None;
56         }
57 
58         match offset {
59             INDEX_OFFSET => self.index = data[0] & INDEX_MASK,
60             DATA_OFFSET => self.data[self.index as usize] = data[0],
61             o => warn!("bad write offset on CMOS device: {}", o),
62         };
63         None
64     }
65 
66     fn read(&mut self, _base: u64, offset: u64, data: &mut [u8]) {
67         fn to_bcd(v: u8) -> u8 {
68             assert!(v < 100);
69             ((v / 10) << 4) | (v % 10)
70         }
71 
72         if data.len() != 1 {
73             warn!("Invalid read size on CMOS device: {}", data.len());
74             return;
75         }
76 
77         data[0] = match offset {
78             INDEX_OFFSET => self.index,
79             DATA_OFFSET => {
80                 let seconds;
81                 let minutes;
82                 let hours;
83                 let week_day;
84                 let day;
85                 let month;
86                 let year;
87                 // The clock_gettime and gmtime_r calls are safe as long as the structs they are
88                 // given are large enough, and neither of them fail. It is safe to zero initialize
89                 // the tm and timespec struct because it contains only plain data.
90                 let update_in_progress = unsafe {
91                     let mut timespec: timespec = mem::zeroed();
92                     clock_gettime(CLOCK_REALTIME, &mut timespec as *mut _);
93 
94                     // https://github.com/rust-lang/libc/issues/1848
95                     #[cfg_attr(target_env = "musl", allow(deprecated))]
96                     let now: time_t = timespec.tv_sec;
97                     let mut tm: tm = mem::zeroed();
98                     gmtime_r(&now, &mut tm as *mut _);
99 
100                     // The following lines of code are safe but depend on tm being in scope.
101                     seconds = tm.tm_sec;
102                     minutes = tm.tm_min;
103                     hours = tm.tm_hour;
104                     week_day = tm.tm_wday + 1;
105                     day = tm.tm_mday;
106                     month = tm.tm_mon + 1;
107                     year = tm.tm_year;
108 
109                     // Update in Progress bit held for last 224us of each second
110                     const NANOSECONDS_PER_SECOND: i64 = 1_000_000_000;
111                     const UIP_HOLD_LENGTH: i64 = 8 * NANOSECONDS_PER_SECOND / 32768;
112                     timespec.tv_nsec >= (NANOSECONDS_PER_SECOND - UIP_HOLD_LENGTH)
113                 };
114                 match self.index {
115                     0x00 => to_bcd(seconds as u8),
116                     0x02 => to_bcd(minutes as u8),
117                     0x04 => to_bcd(hours as u8),
118                     0x06 => to_bcd(week_day as u8),
119                     0x07 => to_bcd(day as u8),
120                     0x08 => to_bcd(month as u8),
121                     0x09 => to_bcd((year % 100) as u8),
122                     // Bit 5 for 32kHz clock. Bit 7 for Update in Progress
123                     0x0a => 1 << 5 | (update_in_progress as u8) << 7,
124                     0x32 => to_bcd(((year + 1900) / 100) as u8),
125                     _ => {
126                         // self.index is always guaranteed to be in range via INDEX_MASK.
127                         self.data[(self.index & INDEX_MASK) as usize]
128                     }
129                 }
130             }
131             o => {
132                 warn!("bad read offset on CMOS device: {}", o);
133                 0
134             }
135         }
136     }
137 }
138