aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / crates / aienos-kernel / src / timer.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Pure architectural timer arithmetic and injectable EL1 register access. |
| 17 | |
| 18 | /// Convert nanoseconds to counter ticks, rounding up so a requested delay is not shortened. |
| 19 | pub const fn ns_to_ticks(ns: u64, frequency_hz: u64) -> Option<u64> { |
| 20 | if frequency_hz == 0 { |
| 21 | return None; |
| 22 | } |
| 23 | let product = (ns as u128) * (frequency_hz as u128); |
| 24 | let ticks = product.saturating_add(999_999_999) / 1_000_000_000; |
| 25 | if ticks > u64::MAX as u128 { |
| 26 | None |
| 27 | } else { |
| 28 | Some(ticks as u64) |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// Convert counter ticks to nanoseconds, truncating sub-nanosecond fractions. |
| 33 | pub const fn ticks_to_ns(ticks: u64, frequency_hz: u64) -> Option<u64> { |
| 34 | if frequency_hz == 0 { |
| 35 | return None; |
| 36 | } |
| 37 | let ns = (ticks as u128) * 1_000_000_000 / frequency_hz as u128; |
| 38 | if ns > u64::MAX as u128 { |
| 39 | None |
| 40 | } else { |
| 41 | Some(ns as u64) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Return the absolute counter deadline after `delay_ns`, with checked overflow. |
| 46 | pub const fn deadline_after(now: u64, delay_ns: u64, frequency_hz: u64) -> Option<u64> { |
| 47 | match ns_to_ticks(delay_ns, frequency_hz) { |
| 48 | Some(delta) => now.checked_add(delta), |
| 49 | None => None, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// Architectural EL1 physical counter and timer access. |
| 54 | pub trait TimerRegisters { |
| 55 | fn frequency(&self) -> u64; |
| 56 | fn counter(&self) -> u64; |
| 57 | fn set_compare(&mut self, deadline: u64); |
| 58 | fn enable_timer(&mut self, enabled: bool); |
| 59 | } |
| 60 | |
| 61 | /// AArch64 EL1 physical timer access. EL2 bootstrap must grant EL1 timer access. |
| 62 | #[cfg(target_arch = "aarch64")] |
| 63 | pub struct Aarch64TimerRegisters; |
| 64 | #[cfg(target_arch = "aarch64")] |
| 65 | impl TimerRegisters for Aarch64TimerRegisters { |
| 66 | fn frequency(&self) -> u64 { |
| 67 | let value; |
| 68 | unsafe { |
| 69 | core::arch::asm!("mrs {0}, cntfrq_el0", out(reg) value, options(nomem, nostack)); |
| 70 | } |
| 71 | value |
| 72 | } |
| 73 | fn counter(&self) -> u64 { |
| 74 | let value; |
| 75 | unsafe { |
| 76 | core::arch::asm!("mrs {0}, cntpct_el0", out(reg) value, options(nomem, nostack)); |
| 77 | } |
| 78 | value |
| 79 | } |
| 80 | fn set_compare(&mut self, deadline: u64) { |
| 81 | unsafe { |
| 82 | core::arch::asm!("msr cntp_cval_el0, {0}", "isb", in(reg) deadline, options(nostack)); |
| 83 | } |
| 84 | } |
| 85 | fn enable_timer(&mut self, enabled: bool) { |
| 86 | let control = u64::from(enabled); |
| 87 | unsafe { |
| 88 | core::arch::asm!("msr cntp_ctl_el0, {0}", "isb", in(reg) control, options(nostack)); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod tests { |
| 95 | use super::*; |
| 96 | #[test] |
| 97 | fn conversion_rounding_and_frequency_validation() { |
| 98 | assert_eq!(ns_to_ticks(1, 1_000_000), Some(1)); |
| 99 | assert_eq!(ns_to_ticks(1_000, 1_000_000), Some(1)); |
| 100 | assert_eq!(ticks_to_ns(1_000, 1_000_000), Some(1_000_000)); |
| 101 | assert_eq!(ns_to_ticks(1, 0), None); |
| 102 | } |
| 103 | #[test] |
| 104 | fn deadline_detects_overflow() { |
| 105 | assert_eq!(deadline_after(50, 10, 1_000_000_000), Some(60)); |
| 106 | assert_eq!(deadline_after(u64::MAX, 1, 1_000_000_000), None); |
| 107 | assert_eq!(deadline_after(0, u64::MAX, u64::MAX), None); |
| 108 | } |
| 109 | } |