starnix_core/time/utc.rs
1// Copyright 2023 The Fuchsia Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Starnix-specific UTC clock implementation.
6//!
7//! UTC clock behaves differently in Fuchsia to what Starnix programs expect. This module abstracts
8//! the differences away. It provides a UTC clock that always runs. In contrast to Fuchsia UTC
9//! clock, which gets started only when the system is reasonably confident that the clock reading
10//! is accurate.
11//!
12//! The paths in this module are somewhat hot, so we document typical measured performance in order
13//! to remain performance-aware in this code. Assume that all the performance notes are made using
14//! the same baseline device. If you need to add or change performance notes, verify first how far
15//! removed your device is from the baseline.
16//!
17//! Starnix UTC clock is started from [backstop][ff] on initialization, and jumps to actual UTC once
18//! Fuchsia provides actual UTC value.
19//!
20//! Consult the [Fuchsia UTC clock specification][ff] for details about UTC clock behavior
21//! specifically on Fuchsia.
22//!
23//! [ff]: https://fuchsia.dev/fuchsia-src/concepts/kernel/time/utc/behavior#differences_from_other_operating_systems
24
25use fidl_fuchsia_time as fftime;
26use fuchsia_component::client::connect_to_protocol_sync;
27use fuchsia_runtime::{
28 UtcClock as UtcClockHandle, UtcClockTransform, UtcInstant, UtcTimeline, zx_utc_reference_get,
29};
30use mapped_clock::MappedClock;
31use starnix_logging::{log_info, log_warn};
32use starnix_sync::Mutex;
33use std::sync::LazyLock;
34use zx::{self as zx, HandleBased, Rights, Unowned};
35
36type MemoryMappedClock = MappedClock<zx::BootTimeline, fuchsia_runtime::UtcTimeline>;
37
38/// The basic rights to use when creating or duplicating a UTC clock. Restrict these
39/// on a case-by-case basis only.
40///
41/// Rights:
42///
43/// - `Rights::DUPLICATE`, `Rights::TRANSFER`: used to forward the UTC clock in runners.
44/// - `Rights::READ`: used to read the clock indication.
45/// - `Rights::WAIT`: used to wait on signals such as "clock is updated" or "clock is started".
46/// - `Rights::MAP`, `Rights::INSPECT`: used to memory-map the UTC clock.
47///
48/// The `Rights::WRITE` is notably absent, since on Fuchsia this right is given to particular
49/// components only and a writable clock can not be obtained via procargs.
50pub static UTC_CLOCK_BASIC_RIGHTS: std::sync::LazyLock<zx::Rights> =
51 std::sync::LazyLock::new(|| {
52 Rights::DUPLICATE
53 | Rights::READ
54 | Rights::WAIT
55 | Rights::TRANSFER
56 | Rights::MAP
57 | Rights::INSPECT
58 });
59
60// Stores a vendored handle from a test fixture. In normal operation the value here must be
61// `None`. In some Starnix container tests, we inject a custom UTC clock that the tests
62// manipulate. This is a very special circumstance, so we log warnings accordingly.
63static VENDORED_UTC_HANDLE_FOR_TESTS: LazyLock<Option<UtcClockHandle>> = LazyLock::new(|| {
64 connect_to_protocol_sync::<fftime::MaintenanceMarker>()
65 .inspect_err(|err| {
66 log_info!("could not connect to fuchsia.time.Maintenance, this is expected to work only in special test code: {err:?}");
67 })
68 .map(|proxy: fftime::MaintenanceSynchronousProxy| {
69 // Even in test code, the handle we obtain here will typically not be writable. The
70 // test fixture will ensure this is the case.
71 proxy.get_writable_utc_clock(zx::MonotonicInstant::INFINITE)
72 .inspect_err(|err| {log_warn!("while getting UTC clock: {err:?}");})
73 .map(|handle: zx::Clock| {
74 // Verify that the handle koid matches with the handle koid logged by the UTC vendor component.
75 log_warn!("Starnix kernel is using a vendored UTC handle. This is acceptable ONLY in tests.");
76 log_warn!("Vendored UTC clock handle koid: {:?}", handle.koid());
77 // Make sure to remove unneeded rights, even if we know that the test fixture will
78 // give us proper handle rights.
79 handle.replace_handle(*UTC_CLOCK_BASIC_RIGHTS)
80 .map(|handle| handle.cast())
81 .inspect_err(|err| {
82 panic!("Could not replace UTC handle for vendored UTC clock: {err:?}");
83 }).ok()
84 }).unwrap_or(None)
85 }).unwrap_or(None)
86});
87
88fn utc_clock() -> Unowned<'static, UtcClockHandle> {
89 VENDORED_UTC_HANDLE_FOR_TESTS.as_ref().map(|handle| Unowned::new(handle)).unwrap_or_else(|| {
90 // SAFETY: basic FFI call which returns either a valid handle or ZX_HANDLE_INVALID.
91 unsafe {
92 let handle = zx_utc_reference_get();
93 Unowned::from_raw_handle(handle)
94 }
95 })
96}
97
98fn duplicate_utc_clock_handle(rights: zx::Rights) -> Result<UtcClockHandle, zx::Status> {
99 utc_clock().duplicate(rights)
100}
101
102// Check whether the UTC clock is started based on actual clock read. If you need something
103// faster, cache the `read` value. Takes about `350ns` to complete.
104fn check_mapped_clock_started(
105 clock: &MemoryMappedClock,
106 backstop: UtcInstant,
107) -> (bool, UtcInstant) {
108 let read = clock.read().expect("clock is readable");
109 (read != backstop, read)
110}
111
112// Returns the details of `clock`.
113// Takes around `500ns`.
114fn get_utc_clock_details(
115 clock: &MemoryMappedClock,
116) -> zx::ClockDetails<zx::BootTimeline, UtcTimeline> {
117 // 500ns.
118 clock.get_details().expect("clock details are readable")
119}
120
121// The implementation of a UTC clock that is offered to programs in a Starnix container.
122//
123// Many Linux APIs need a running UTC clock to function. Since there can be a delay until the UTC
124// clock in Zircon starts up (https://fxbug.dev/42081426), Starnix provides a synthetic utc clock
125// initially, Once the UTC clock is started, the synthetic utc clock is replaced by a real utc
126// clock.
127#[derive(Debug)]
128pub struct UtcClock {
129 // The real underlying Fuchsia UTC clock. This clock may never start,
130 // see module-level documentation for details.
131 real_utc_clock: UtcClockHandle,
132 // The memory mapped clock derived from `real_utc_clock`.
133 // Operations on this clock are up to 3x faster than on the companion
134 // zx::Clock` object.
135 mapped_clock: MemoryMappedClock,
136 // The UTC clock transform from boot timeline to UTC timeline, used while
137 // `real_utc_clock` is not started. This clock starts from UTC backstop
138 // on boot, and progresses with a nominal 1sec/1sec rate.
139 synthetic_transform: UtcClockTransform,
140 // The UTC backstop value. This is the earliest UTC value that may ever be
141 // shown by any UTC clock in Fuchsia.
142 backstop: UtcInstant,
143}
144
145impl UtcClock {
146 /// Creates a new `UtcClock` instance.
147 ///
148 /// The `real_utc_clock` is a handle to an underlying Fuchsia UTC clock. It will
149 /// be used once started.
150 pub fn new(real_utc_clock: UtcClockHandle) -> Self {
151 let backstop = real_utc_clock.get_details().unwrap().backstop;
152 let synthetic_transform = zx::ClockTransformation {
153 // The boot timeline always starts at zero on boot.
154 reference_offset: zx::BootInstant::ZERO,
155 // By definition, absent other information, a zero reference offset
156 // represents a backstop UTC time instant.
157 synthetic_offset: backstop,
158 // Default rate of 1 synthetic second per 1 reference second disregards
159 // any device variations.
160 rate: zx::sys::zx_clock_rate_t { synthetic_ticks: 1, reference_ticks: 1 },
161 };
162
163 let vmar_parent = fuchsia_runtime::vmar_root_self();
164 let real_utc_clock_clone = real_utc_clock
165 .duplicate_handle(zx::Rights::SAME_RIGHTS)
166 .expect("UTC clock duplication should work");
167 let mapped_clock: MemoryMappedClock =
168 MappedClock::try_new(real_utc_clock_clone, &vmar_parent, zx::VmarFlags::PERM_READ)
169 .expect("failed to map clock into VMAR");
170 let (is_real_utc_clock_started, _) = check_mapped_clock_started(&mapped_clock, backstop);
171 let utc_clock = Self { real_utc_clock, mapped_clock, synthetic_transform, backstop };
172 if !is_real_utc_clock_started {
173 log_warn!(
174 "Waiting for real UTC clock to start, using synthetic clock in the meantime."
175 );
176 }
177 utc_clock
178 }
179
180 fn duplicate_real_utc_clock_handle(
181 &self,
182 rights: zx::Rights,
183 ) -> Result<UtcClockHandle, zx::Status> {
184 self.real_utc_clock.duplicate_handle(rights)
185 }
186
187 /// A slower way to verify whether the real UTC clock has started.
188 ///
189 /// This call takes about `350ns` to complete, refer to the benchmarks
190 /// at `//src/lib/mapped-clock/benchmarks`.
191 fn check_real_utc_clock_started(&self) -> (bool, UtcInstant) {
192 // 350ns.
193 check_mapped_clock_started(&self.mapped_clock, self.backstop)
194 }
195
196 /// Returns the current Starnix UTC time.
197 ///
198 /// In Starnix, UTC time is always running. It is started from backstop
199 /// at Starnix boot, and adjusted to actual UTC once Fuchsia UTC clock
200 /// is started.
201 pub fn now(&self) -> UtcInstant {
202 // 350 ns.
203 let (is_started, utc_now) = self.check_real_utc_clock_started();
204 if is_started {
205 utc_now
206 } else {
207 let boot_time = zx::BootInstant::get();
208 // Utc time is calculated using the same (constant) transform as the one stored in vdso
209 // code. This ensures that the result of `now()` is the same as in
210 // `calculate_utc_time_nsec` in `vdso_calculate_utc.cc`.
211 self.synthetic_transform.apply(boot_time)
212 }
213 }
214
215 /// Estimates the boot time corresponding to `utc`.
216 ///
217 /// # Returns
218 /// - zx::BootInstant: estimated boot time;
219 /// - bool: true if the system UTC clock has been started.
220 ///
221 /// Takes about 900ns worst case.
222 pub fn estimate_boot_time(&self, utc: UtcInstant) -> (zx::BootInstant, bool) {
223 // 350 ns.
224 // Could be reduced on average by caching `started`.
225 let (started, _) = self.check_real_utc_clock_started();
226 let estimated_boot = if started {
227 // 500ns.
228 let details = get_utc_clock_details(&self.mapped_clock);
229 details.reference_to_synthetic.apply_inverse(utc)
230 } else {
231 self.synthetic_transform.apply_inverse(utc)
232 };
233 (estimated_boot, started)
234 }
235}
236
237static UTC_CLOCK: LazyLock<Mutex<UtcClock>> = LazyLock::new(|| {
238 Mutex::new(UtcClock::new(duplicate_utc_clock_handle(zx::Rights::SAME_RIGHTS).unwrap()))
239});
240
241/// Creates a copy of the UTC clock handle currently in use in Starnix.
242///
243/// Ensure that you are not reading UTC clock for Starnix use from this clock,
244/// use the [utc_now] function instead.
245pub fn duplicate_real_utc_clock_handle() -> Result<UtcClockHandle, zx::Status> {
246 let lock = (*UTC_CLOCK).lock();
247 // Maybe reduce rights here?
248 (*lock).duplicate_real_utc_clock_handle(zx::Rights::SAME_RIGHTS)
249}
250
251/// Returns the current UTC time based on the Starnix UTC clock.
252///
253/// The Starnix UTC clock is always started. This is in contrast to Fuchsia's
254/// UTC clock which may spend an undefined amount of wall-clock time stuck at
255/// [backstop] time reading.
256///
257/// To ensure an uniform reading of the Starnix UTC clock, always use this
258/// function call if you need to know Starnix's view of the current wall time.
259///
260/// [backstop]: https://fuchsia.dev/fuchsia-src/concepts/kernel/time/utc/behavior#differences_from_other_operating_systems
261pub fn utc_now() -> UtcInstant {
262 #[cfg(test)]
263 {
264 if let Some(test_time) = UTC_CLOCK_OVERRIDE_FOR_TESTING
265 .with(|cell| cell.borrow().as_ref().map(|test_clock| test_clock.read().unwrap()))
266 {
267 return test_time;
268 }
269 }
270 (*UTC_CLOCK).lock().now()
271}
272
273/// Estimates the boot time corresponding to `utc`, based on the currently
274/// operating Starnix UTC clock.
275///
276/// # Returns
277/// - zx::BootInstant: estimated boot time;
278/// - bool: true if the system UTC clock has been started.
279pub fn estimate_boot_deadline_from_utc(utc: UtcInstant) -> (zx::BootInstant, bool) {
280 #[cfg(test)]
281 {
282 if let Some(test_time) = UTC_CLOCK_OVERRIDE_FOR_TESTING.with(|cell| {
283 cell.borrow().as_ref().map(|test_clock| {
284 test_clock.get_details().unwrap().reference_to_synthetic.apply_inverse(utc)
285 })
286 }) {
287 return (test_time, true);
288 }
289 }
290 (*UTC_CLOCK).lock().estimate_boot_time(utc)
291}
292
293#[cfg(test)]
294thread_local! {
295 static UTC_CLOCK_OVERRIDE_FOR_TESTING: std::cell::RefCell<Option<UtcClockHandle>> =
296 std::cell::RefCell::new(None);
297}
298
299/// A guard that temporarily overrides the UTC clock for testing.
300///
301/// When this guard is created, it replaces the global UTC clock with a test clock. When the guard
302/// is dropped, the original clock is restored.
303#[cfg(test)]
304pub struct UtcClockOverrideGuard(());
305
306#[cfg(test)]
307impl UtcClockOverrideGuard {
308 /// Creates a new `UtcClockOverrideGuard`.
309 ///
310 /// This function replaces the global UTC clock with `test_clock`. The original clock is
311 /// restored when the returned guard is dropped.
312 pub fn new(test_clock: UtcClockHandle) -> Self {
313 UTC_CLOCK_OVERRIDE_FOR_TESTING.with(|cell| {
314 assert_eq!(*cell.borrow(), None); // We don't expect a previously set clock override when using this type.
315 *cell.borrow_mut() = Some(test_clock);
316 });
317 Self(())
318 }
319}
320
321#[cfg(test)]
322impl Drop for UtcClockOverrideGuard {
323 fn drop(&mut self) {
324 UTC_CLOCK_OVERRIDE_FOR_TESTING.with(|cell| {
325 *cell.borrow_mut() = None;
326 });
327 }
328}