-
Notifications
You must be signed in to change notification settings - Fork 13.2k
/
Copy pathtime.rs
272 lines (222 loc) · 9.31 KB
/
time.rs
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
use crate::time::Duration;
const SECS_IN_MINUTE: u64 = 60;
const SECS_IN_HOUR: u64 = SECS_IN_MINUTE * 60;
const SECS_IN_DAY: u64 = SECS_IN_HOUR * 24;
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct Instant(Duration);
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct SystemTime(Duration);
pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
impl Instant {
pub fn now() -> Instant {
// If we have a timestamp protocol, use it.
if let Some(x) = instant_internal::timestamp_protocol() {
return x;
}
if let Some(x) = instant_internal::platform_specific() {
return x;
}
panic!("time not implemented on this platform")
}
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
self.0.checked_sub(other.0)
}
pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
Some(Instant(self.0.checked_add(*other)?))
}
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
Some(Instant(self.0.checked_sub(*other)?))
}
}
impl SystemTime {
pub(crate) const ZERO: SystemTime = SystemTime(Duration::ZERO);
pub(crate) const fn new(t: r_efi::efi::Time) -> Self {
Self(system_time_internal::uefi_time_to_duration(t))
}
pub fn now() -> SystemTime {
system_time_internal::now()
.unwrap_or_else(|| panic!("time not implemented on this platform"))
}
#[expect(dead_code)]
pub(crate) const fn to_uefi_time(&self, daylight: u8, timezone: i16) -> r_efi::efi::Time {
system_time_internal::uefi_time_from_duration(self.0, daylight, timezone)
}
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
}
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
Some(SystemTime(self.0.checked_add(*other)?))
}
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
Some(SystemTime(self.0.checked_sub(*other)?))
}
}
pub(crate) mod system_time_internal {
use r_efi::efi::{RuntimeServices, Time};
use super::super::helpers;
use super::*;
use crate::mem::MaybeUninit;
use crate::ptr::NonNull;
pub fn now() -> Option<SystemTime> {
let runtime_services: NonNull<RuntimeServices> = helpers::runtime_services()?;
let mut t: MaybeUninit<Time> = MaybeUninit::uninit();
let r = unsafe {
((*runtime_services.as_ptr()).get_time)(t.as_mut_ptr(), crate::ptr::null_mut())
};
if r.is_error() {
return None;
}
let t = unsafe { t.assume_init() };
Some(SystemTime::new(t))
}
// This algorithm is based on the one described in the post
// https://blog.reverberate.org/2020/05/12/optimizing-date-algorithms.html
pub(crate) const fn uefi_time_to_duration(t: r_efi::system::Time) -> Duration {
assert!(t.month <= 12);
assert!(t.month != 0);
const YEAR_BASE: u32 = 4800; /* Before min year, multiple of 400. */
// Calculate the number of days since 1/1/1970
// Use 1 March as the start
let (m_adj, overflow): (u32, bool) = (t.month as u32).overflowing_sub(3);
let (carry, adjust): (u32, u32) = if overflow { (1, 12) } else { (0, 0) };
let y_adj: u32 = (t.year as u32) + YEAR_BASE - carry;
let month_days: u32 = (m_adj.wrapping_add(adjust) * 62719 + 769) / 2048;
let leap_days: u32 = y_adj / 4 - y_adj / 100 + y_adj / 400;
let days: u32 = y_adj * 365 + leap_days + month_days + (t.day as u32 - 1) - 2472632;
let localtime_epoch: u64 = (days as u64) * SECS_IN_DAY
+ (t.second as u64)
+ (t.minute as u64) * SECS_IN_MINUTE
+ (t.hour as u64) * SECS_IN_HOUR;
let utc_epoch: u64 = if t.timezone == r_efi::efi::UNSPECIFIED_TIMEZONE {
localtime_epoch
} else {
(localtime_epoch as i64 + (t.timezone as i64) * SECS_IN_MINUTE as i64) as u64
};
Duration::new(utc_epoch, t.nanosecond)
}
/// This algorithm is taken from: http://howardhinnant.github.io/date_algorithms.html
pub(crate) const fn uefi_time_from_duration(
dur: crate::time::Duration,
daylight: u8,
timezone: i16,
) -> r_efi::system::Time {
const SECS_IN_MINUTE: u64 = 60;
const SECS_IN_HOUR: u64 = SECS_IN_MINUTE * 60;
const SECS_IN_DAY: u64 = SECS_IN_HOUR * 24;
let secs = if timezone == r_efi::efi::UNSPECIFIED_TIMEZONE {
dur.as_secs()
} else {
dur.as_secs().checked_add_signed(-timezone as i64).unwrap()
};
let days = secs / SECS_IN_DAY;
let remaining_secs = secs % SECS_IN_DAY;
let z = days + 719468;
let era = z / 146097;
let doe = z - (era * 146097);
let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
let mut y = yoe + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let d = doy - (153 * mp + 2) / 5 + 1;
let m = if mp < 10 { mp + 3 } else { mp - 9 };
if m <= 2 {
y += 1;
}
r_efi::system::Time {
year: y as u16,
month: m as u8,
day: d as u8,
hour: (remaining_secs / SECS_IN_HOUR) as u8,
minute: ((remaining_secs % SECS_IN_HOUR) / SECS_IN_MINUTE) as u8,
second: ((remaining_secs % SECS_IN_HOUR) % SECS_IN_MINUTE) as u8,
pad1: 0,
nanosecond: dur.subsec_nanos(),
timezone,
daylight,
pad2: 0,
}
}
}
pub(crate) mod instant_internal {
use r_efi::protocols::timestamp;
use super::super::helpers;
use super::*;
use crate::mem::MaybeUninit;
use crate::ptr::NonNull;
use crate::sync::atomic::{AtomicPtr, Ordering};
use crate::sys_common::mul_div_u64;
const NS_PER_SEC: u64 = 1_000_000_000;
pub fn timestamp_protocol() -> Option<Instant> {
fn try_handle(handle: NonNull<crate::ffi::c_void>) -> Option<u64> {
let protocol: NonNull<timestamp::Protocol> =
helpers::open_protocol(handle, timestamp::PROTOCOL_GUID).ok()?;
let mut properties: MaybeUninit<timestamp::Properties> = MaybeUninit::uninit();
let r = unsafe { ((*protocol.as_ptr()).get_properties)(properties.as_mut_ptr()) };
if r.is_error() {
return None;
}
let freq = unsafe { properties.assume_init().frequency };
let ts = unsafe { ((*protocol.as_ptr()).get_timestamp)() };
Some(mul_div_u64(ts, NS_PER_SEC, freq))
}
static LAST_VALID_HANDLE: AtomicPtr<crate::ffi::c_void> =
AtomicPtr::new(crate::ptr::null_mut());
if let Some(handle) = NonNull::new(LAST_VALID_HANDLE.load(Ordering::Acquire)) {
if let Some(ns) = try_handle(handle) {
return Some(Instant(Duration::from_nanos(ns)));
}
}
if let Ok(handles) = helpers::locate_handles(timestamp::PROTOCOL_GUID) {
for handle in handles {
if let Some(ns) = try_handle(handle) {
LAST_VALID_HANDLE.store(handle.as_ptr(), Ordering::Release);
return Some(Instant(Duration::from_nanos(ns)));
}
}
}
None
}
pub fn platform_specific() -> Option<Instant> {
cfg_if::cfg_if! {
if #[cfg(any(target_arch = "x86_64", target_arch = "x86"))] {
timestamp_rdtsc().map(Instant)
} else {
None
}
}
}
#[cfg(target_arch = "x86_64")]
fn timestamp_rdtsc() -> Option<Duration> {
static FREQUENCY: crate::sync::OnceLock<u64> = crate::sync::OnceLock::new();
// Get Frequency in Mhz
// Inspired by [`edk2/UefiCpuPkg/Library/CpuTimerLib/CpuTimerLib.c`](https://github.com/tianocore/edk2/blob/master/UefiCpuPkg/Library/CpuTimerLib/CpuTimerLib.c)
let freq = FREQUENCY
.get_or_try_init(|| {
let cpuid = unsafe { crate::arch::x86_64::__cpuid(0x15) };
if cpuid.eax == 0 || cpuid.ebx == 0 || cpuid.ecx == 0 {
return Err(());
}
Ok(mul_div_u64(cpuid.ecx as u64, cpuid.ebx as u64, cpuid.eax as u64))
})
.ok()?;
let ts = unsafe { crate::arch::x86_64::_rdtsc() };
let ns = mul_div_u64(ts, 1000, *freq);
Some(Duration::from_nanos(ns))
}
#[cfg(target_arch = "x86")]
fn timestamp_rdtsc() -> Option<Duration> {
static FREQUENCY: crate::sync::OnceLock<u64> = crate::sync::OnceLock::new();
let freq = FREQUENCY
.get_or_try_init(|| {
let cpuid = unsafe { crate::arch::x86::__cpuid(0x15) };
if cpuid.eax == 0 || cpuid.ebx == 0 || cpuid.ecx == 0 {
return Err(());
}
Ok(mul_div_u64(cpuid.ecx as u64, cpuid.ebx as u64, cpuid.eax as u64))
})
.ok()?;
let ts = unsafe { crate::arch::x86::_rdtsc() };
let ns = mul_div_u64(ts, 1000, *freq);
Some(Duration::from_nanos(ns))
}
}